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:
|
||||
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 创建
|
||||
editenv_cmd = None
|
||||
for cmd_name in ["grub2-editenv", "grub-editenv"]:
|
||||
@@ -2201,23 +2209,47 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
|
||||
"创建 grubenv 文件",
|
||||
timeout=10
|
||||
)
|
||||
if success or os.path.exists(grubenv_path):
|
||||
# 重新检查文件是否存在(可能在不同路径)
|
||||
if success:
|
||||
log_success(f"✓ 成功创建 grubenv 文件")
|
||||
else:
|
||||
log_warning(f"grub2-editenv 创建失败: {stderr}")
|
||||
|
||||
# 方法2: 直接创建空文件(如果方法1失败)
|
||||
# 方法2: 如果 /boot 是独立分区,尝试在挂载的主机路径创建
|
||||
if not os.path.exists(grubenv_path):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(grubenv_path), exist_ok=True)
|
||||
# 创建一个空的 grubenv 文件(包含必要的头部)
|
||||
with open(grubenv_path, 'w') as f:
|
||||
f.write("# GRUB Environment Block\n")
|
||||
f.write("saved_entry=\n")
|
||||
f.write("#" * 1024 + "\n") # 填充到至少1KB
|
||||
log_success(f"✓ 手动创建 grubenv 文件成功")
|
||||
except Exception as e:
|
||||
log_warning(f"手动创建 grubenv 文件失败: {e}")
|
||||
# 尝试多种可能的路径(处理独立 /boot 分区的情况)
|
||||
possible_grubenv_paths = [
|
||||
grubenv_path, # 标准路径
|
||||
os.path.join(mount_point, "boot", "grub2", "grubenv"),
|
||||
os.path.join(mount_point, "boot", "grub", "grubenv"),
|
||||
os.path.join(mount_point, "grub2", "grubenv"),
|
||||
os.path.join(mount_point, "grub", "grubenv"),
|
||||
]
|
||||
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user