11
This commit is contained in:
44
backend.py
44
backend.py
@@ -1631,6 +1631,7 @@ def check_and_restore_kernel(mount_point: str, distro_type: str, has_separate_bo
|
||||
chroot_cmd_prefix + ["dracut", "-f", f"/boot/initramfs-{kernel_ver}.img", kernel_ver],
|
||||
f"生成 initramfs for {kernel_ver}",
|
||||
timeout=120
|
||||
|
||||
)
|
||||
|
||||
if success:
|
||||
@@ -2165,6 +2166,49 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
|
||||
log_info(f"使用命令: {' '.join(grub_update_cmd)}")
|
||||
log_info(f"配置文件路径: {config_path}")
|
||||
|
||||
# ===== 确保 grubenv 文件存在 =====
|
||||
# CentOS/RHEL/Fedora 等系统需要 grubenv 文件,否则 grub2-mkconfig 会失败
|
||||
grubenv_dir = os.path.dirname(config_path) # /boot/grub2 或 /boot/grub
|
||||
grubenv_path = os.path.join(mount_point, grubenv_dir.lstrip('/'), "grubenv")
|
||||
|
||||
if not os.path.exists(grubenv_path):
|
||||
log_info(f"grubenv 文件不存在,尝试创建...")
|
||||
|
||||
# 方法1: 使用 grub2-editenv 创建
|
||||
editenv_cmd = None
|
||||
for cmd_name in ["grub2-editenv", "grub-editenv"]:
|
||||
for cmd_dir in ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]:
|
||||
test_path = os.path.join(mount_point, cmd_dir.lstrip('/'), cmd_name)
|
||||
if os.path.exists(test_path):
|
||||
editenv_cmd = os.path.join(cmd_dir, cmd_name)
|
||||
break
|
||||
if editenv_cmd:
|
||||
break
|
||||
|
||||
if editenv_cmd:
|
||||
success, _, stderr = run_command(
|
||||
chroot_cmd_prefix + [editenv_cmd, "/boot/grub2/grubenv", "create"],
|
||||
"创建 grubenv 文件",
|
||||
timeout=10
|
||||
)
|
||||
if success or os.path.exists(grubenv_path):
|
||||
log_success(f"✓ 成功创建 grubenv 文件")
|
||||
else:
|
||||
log_warning(f"grub2-editenv 创建失败: {stderr}")
|
||||
|
||||
# 方法2: 直接创建空文件(如果方法1失败)
|
||||
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}")
|
||||
|
||||
# 执行配置更新
|
||||
success, stdout, stderr = run_command(
|
||||
chroot_cmd_prefix + grub_update_cmd,
|
||||
|
||||
Reference in New Issue
Block a user