This commit is contained in:
zj
2026-02-03 03:27:14 +08:00
parent 10cc7d1c91
commit c6c9ece1e9
7 changed files with 160 additions and 1389 deletions

View File

@@ -1,3 +1,4 @@
# lvm_operations.py
import subprocess
import logging
from PySide6.QtWidgets import QMessageBox
@@ -6,10 +7,11 @@ logger = logging.getLogger(__name__)
class LvmOperations:
def __init__(self):
pass # LVM操作不直接依赖SystemInfoManager通过参数传递所需信息
pass
def _execute_shell_command(self, command_list, error_message, root_privilege=True,
suppress_critical_dialog_on_stderr_match=None, input_data=None):
suppress_critical_dialog_on_stderr_match=None, input_data=None,
show_dialog=True): # <--- 新增 show_dialog 参数,默认为 True
"""
通用地运行一个 shell 命令,并处理错误。
:param command_list: 命令及其参数的列表。
@@ -18,11 +20,13 @@ class LvmOperations:
:param suppress_critical_dialog_on_stderr_match: 如果 stderr 包含此字符串,则不显示关键错误对话框。
可以是字符串或字符串元组/列表。
:param input_data: 传递给命令stdin的数据 (str)。
:param show_dialog: 如果为 False则不显示关键错误对话框。
:return: (True/False, stdout_str, stderr_str)
"""
if not all(isinstance(arg, str) for arg in command_list):
logger.error(f"命令列表包含非字符串元素: {command_list}")
QMessageBox.critical(None, "错误", f"内部错误:尝试执行的命令包含无效参数。\n命令详情: {command_list}")
if show_dialog: # <--- 根据 show_dialog 决定是否弹出对话框
QMessageBox.critical(None, "错误", f"内部错误:尝试执行的命令包含无效参数。\n命令详情: {command_list}")
return False, "", "内部错误:命令参数类型不正确。"
if root_privilege:
@@ -49,7 +53,6 @@ class LvmOperations:
logger.error(f"标准输出: {e.stdout.strip()}")
logger.error(f"标准错误: {stderr_output}")
# --- 修改开始:处理 suppress_critical_dialog_on_stderr_match 可以是字符串或元组/列表 ---
should_suppress_dialog = False
if suppress_critical_dialog_on_stderr_match:
if isinstance(suppress_critical_dialog_on_stderr_match, str):
@@ -59,24 +62,25 @@ class LvmOperations:
for pattern in suppress_critical_dialog_on_stderr_match:
if pattern in stderr_output:
should_suppress_dialog = True
break # 找到一个匹配就足够了
break
if should_suppress_dialog:
logger.info(f"错误信息 '{stderr_output}' 匹配抑制条件,不显示关键错误对话框。")
else:
if show_dialog and not should_suppress_dialog: # <--- 根据 show_dialog 决定是否弹出对话框
QMessageBox.critical(None, "错误", f"{error_message}\n错误详情: {stderr_output}")
# --- 修改结束 ---
return False, e.stdout.strip(), stderr_output
except FileNotFoundError:
QMessageBox.critical(None, "错误", f"命令 '{command_list[0]}' 未找到。请确保已安装相关工具。")
if show_dialog: # <--- 根据 show_dialog 决定是否弹出对话框
QMessageBox.critical(None, "错误", f"命令 '{command_list[0]}' 未找到。请确保已安装相关工具。")
logger.error(f"命令 '{command_list[0]}' 未找到。")
return False, "", f"命令 '{command_list[0]}' 未找到。"
except Exception as e:
QMessageBox.critical(None, "错误", f"执行命令时发生未知错误: {e}")
if show_dialog: # <--- 根据 show_dialog 决定是否弹出对话框
QMessageBox.critical(None, "错误", f"执行命令时发生未知错误: {e}")
logger.error(f"执行命令 {full_cmd_str} 时发生未知错误: {e}")
return False, "", str(e)
# 以下是 LvmOperations 中其他方法的代码,保持不变。
# ... (create_pv, delete_pv, create_vg, delete_vg, create_lv, delete_lv, activate_lv, deactivate_lv) ...
def create_pv(self, device_path):
"""
创建物理卷 (PV)。
@@ -361,3 +365,4 @@ class LvmOperations:
return True
else:
return False