This commit is contained in:
zj
2026-02-12 05:02:22 +08:00
parent 5f50d96b07
commit 95b1a9da17

View File

@@ -2060,23 +2060,8 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
grub_install_success = False grub_install_success = False
install_errors = [] install_errors = []
# ===== UEFI 安装 =====
if is_uefi or install_hybrid:
log_step("安装 UEFI GRUB")
# 检查 EFI 分区是否正确挂载
efi_check_path = os.path.join(mount_point, "boot/efi/EFI")
if os.path.exists(efi_check_path):
log_info(f"✓ EFI 目录存在: {efi_check_path}")
try:
contents = os.listdir(efi_check_path)
log_info(f" 当前 EFI 目录内容: {contents}")
except Exception as e:
log_warning(f" 无法列出 EFI 目录: {e}")
else:
log_warning(f"✗ EFI 目录不存在: {efi_check_path}")
# ===== 清空并重建 GRUB 环境(针对严重损坏的系统)===== # ===== 清空并重建 GRUB 环境(针对严重损坏的系统)=====
# 这个步骤在 UEFI 和 BIOS 模式下都需要
log_step("清空并重建 GRUB 环境") log_step("清空并重建 GRUB 环境")
# 确定正确的 GRUB 目录名 # 确定正确的 GRUB 目录名
@@ -2131,34 +2116,7 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
except Exception as e: except Exception as e:
log_warning(f"清理失败: {e}") log_warning(f"清理失败: {e}")
# 2. 清空 EFI 分区中的 GRUB 相关目录 # 3. 创建 grubenv 文件(普通文件,不是符号链接)
efi_cleanup_dirs = ["GRUB", "grub", "Boot", "boot", "centos", "redhat", "fedora"]
for efi_dir in efi_cleanup_dirs:
efi_dir_path = os.path.join(mount_point, "boot/efi/EFI", efi_dir)
if os.path.exists(efi_dir_path):
log_info(f"清空 EFI 目录: {efi_dir_path}")
try:
import shutil
shutil.rmtree(efi_dir_path)
log_success(f"✓ 已删除: {efi_dir_path}")
except Exception as e:
log_warning(f"删除失败: {e}")
# 3. 重建目录结构
log_info("重建 GRUB 目录结构...")
dirs_to_create = [
os.path.join(mount_point, "boot", grub_dir_name),
os.path.join(mount_point, "boot/efi/EFI", efi_bootloader_id),
os.path.join(mount_point, "boot/efi/EFI", "Boot"),
]
for dir_path in dirs_to_create:
try:
os.makedirs(dir_path, exist_ok=True)
log_info(f"✓ 创建目录: {dir_path}")
except Exception as e:
log_warning(f"创建目录失败: {e}")
# 4. 创建 grubenv 文件(普通文件,不是符号链接)
# GRUB2 要求环境块精确为 4096 字节(一个扇区) # GRUB2 要求环境块精确为 4096 字节(一个扇区)
grubenv_path = os.path.join(mount_point, "boot", grub_dir_name, "grubenv") grubenv_path = os.path.join(mount_point, "boot", grub_dir_name, "grubenv")
try: try:
@@ -2176,6 +2134,53 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
log_success("✓ GRUB 环境重建完成") log_success("✓ GRUB 环境重建完成")
# ===== UEFI 安装 =====
if is_uefi or install_hybrid:
log_step("安装 UEFI GRUB")
# 检查 EFI 分区是否正确挂载
efi_check_path = os.path.join(mount_point, "boot/efi/EFI")
if os.path.exists(efi_check_path):
log_info(f"✓ EFI 目录存在: {efi_check_path}")
try:
contents = os.listdir(efi_check_path)
log_info(f" 当前 EFI 目录内容: {contents}")
except Exception as e:
log_warning(f" 无法列出 EFI 目录: {e}")
else:
log_warning(f"✗ EFI 目录不存在: {efi_check_path}")
# ===== 清空 EFI 分区并重建UEFI 模式特有)=====
log_step("清空 EFI 分区并重建")
# 1. 清空 EFI 分区中的 GRUB 相关目录
efi_cleanup_dirs = ["GRUB", "grub", "Boot", "boot", "centos", "redhat", "fedora"]
for efi_dir in efi_cleanup_dirs:
efi_dir_path = os.path.join(mount_point, "boot/efi/EFI", efi_dir)
if os.path.exists(efi_dir_path):
log_info(f"清空 EFI 目录: {efi_dir_path}")
try:
import shutil
shutil.rmtree(efi_dir_path)
log_success(f"✓ 已删除: {efi_dir_path}")
except Exception as e:
log_warning(f"删除失败: {e}")
# 2. 重建 EFI 目录结构
log_info("重建 EFI 目录结构...")
dirs_to_create = [
os.path.join(mount_point, "boot/efi/EFI", efi_bootloader_id),
os.path.join(mount_point, "boot/efi/EFI", "Boot"),
]
for dir_path in dirs_to_create:
try:
os.makedirs(dir_path, exist_ok=True)
log_info(f"✓ 创建目录: {dir_path}")
except Exception as e:
log_warning(f"创建目录失败: {e}")
log_success("✓ GRUB 环境重建完成")
# 5. 为独立 /boot 分区创建 EFI 辅助 grub.cfg # 5. 为独立 /boot 分区创建 EFI 辅助 grub.cfg
has_separate_boot = os.path.ismount(os.path.join(mount_point, "boot")) has_separate_boot = os.path.ismount(os.path.join(mount_point, "boot"))
if has_separate_boot: if has_separate_boot: