13
This commit is contained in:
56
backend.py
56
backend.py
@@ -2184,6 +2184,14 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
|
|||||||
if success:
|
if success:
|
||||||
log_info(f"✓ 创建目录: {grubenv_dir_in_chroot}")
|
log_info(f"✓ 创建目录: {grubenv_dir_in_chroot}")
|
||||||
|
|
||||||
|
# 检查 /boot 是否已挂载(在 chroot 内)
|
||||||
|
success, stdout, _ = run_command(
|
||||||
|
chroot_cmd_prefix + ["mount", "|", "grep", "boot"],
|
||||||
|
"检查 /boot 挂载状态",
|
||||||
|
timeout=5
|
||||||
|
)
|
||||||
|
log_info(f"/boot 挂载状态: {stdout if success else '未显示或检查失败'}")
|
||||||
|
|
||||||
# 方法1: 使用 grub2-editenv 创建
|
# 方法1: 使用 grub2-editenv 创建
|
||||||
editenv_cmd = None
|
editenv_cmd = None
|
||||||
for cmd_name in ["grub2-editenv", "grub-editenv"]:
|
for cmd_name in ["grub2-editenv", "grub-editenv"]:
|
||||||
@@ -2201,23 +2209,47 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
|
|||||||
"创建 grubenv 文件",
|
"创建 grubenv 文件",
|
||||||
timeout=10
|
timeout=10
|
||||||
)
|
)
|
||||||
if success or os.path.exists(grubenv_path):
|
# 重新检查文件是否存在(可能在不同路径)
|
||||||
|
if success:
|
||||||
log_success(f"✓ 成功创建 grubenv 文件")
|
log_success(f"✓ 成功创建 grubenv 文件")
|
||||||
else:
|
else:
|
||||||
log_warning(f"grub2-editenv 创建失败: {stderr}")
|
log_warning(f"grub2-editenv 创建失败: {stderr}")
|
||||||
|
|
||||||
# 方法2: 直接创建空文件(如果方法1失败)
|
# 方法2: 如果 /boot 是独立分区,尝试在挂载的主机路径创建
|
||||||
if not os.path.exists(grubenv_path):
|
if not os.path.exists(grubenv_path):
|
||||||
try:
|
# 尝试多种可能的路径(处理独立 /boot 分区的情况)
|
||||||
os.makedirs(os.path.dirname(grubenv_path), exist_ok=True)
|
possible_grubenv_paths = [
|
||||||
# 创建一个空的 grubenv 文件(包含必要的头部)
|
grubenv_path, # 标准路径
|
||||||
with open(grubenv_path, 'w') as f:
|
os.path.join(mount_point, "boot", "grub2", "grubenv"),
|
||||||
f.write("# GRUB Environment Block\n")
|
os.path.join(mount_point, "boot", "grub", "grubenv"),
|
||||||
f.write("saved_entry=\n")
|
os.path.join(mount_point, "grub2", "grubenv"),
|
||||||
f.write("#" * 1024 + "\n") # 填充到至少1KB
|
os.path.join(mount_point, "grub", "grubenv"),
|
||||||
log_success(f"✓ 手动创建 grubenv 文件成功")
|
]
|
||||||
except Exception as e:
|
|
||||||
log_warning(f"手动创建 grubenv 文件失败: {e}")
|
for test_path in possible_grubenv_paths:
|
||||||
|
if os.path.exists(test_path):
|
||||||
|
log_info(f"✓ 发现 grubenv 已存在于: {test_path}")
|
||||||
|
grubenv_path = test_path
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# 没有找到现有文件,尝试创建
|
||||||
|
for test_path in possible_grubenv_paths:
|
||||||
|
try:
|
||||||
|
dir_path = os.path.dirname(test_path)
|
||||||
|
os.makedirs(dir_path, exist_ok=True)
|
||||||
|
# 创建一个空的 grubenv 文件(包含必要的头部)
|
||||||
|
with open(test_path, 'w') as f:
|
||||||
|
f.write("# GRUB Environment Block\n")
|
||||||
|
f.write("saved_entry=\n")
|
||||||
|
f.write("#" * 1024 + "\n") # 填充到至少1KB
|
||||||
|
log_success(f"✓ 手动创建 grubenv 文件成功: {test_path}")
|
||||||
|
grubenv_path = test_path
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
log_debug(f"尝试创建 {test_path} 失败: {e}")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
log_warning(f"所有路径创建 grubenv 文件均失败")
|
||||||
|
|
||||||
# 执行配置更新
|
# 执行配置更新
|
||||||
success, stdout, stderr = run_command(
|
success, stdout, stderr = run_command(
|
||||||
|
|||||||
Reference in New Issue
Block a user