8
This commit is contained in:
138
backend.py
138
backend.py
@@ -1176,16 +1176,17 @@ def check_and_install_efi_modules(mount_point: str, distro_type: str, is_uefi: b
|
||||
chroot_cmd_prefix = ["sudo", "chroot", mount_point]
|
||||
|
||||
# 根据发行版选择正确的 EFI 包
|
||||
# 注意:CentOS/RHEL 8+ 需要 shim 包来支持 UEFI Secure Boot
|
||||
efi_packages_map = {
|
||||
"centos": ["grub2-efi-x64", "grub2-efi-x64-modules"],
|
||||
"rhel": ["grub2-efi-x64", "grub2-efi-x64-modules"],
|
||||
"fedora": ["grub2-efi-x64", "grub2-efi-x64-modules"],
|
||||
"rocky": ["grub2-efi-x64", "grub2-efi-x64-modules"],
|
||||
"almalinux": ["grub2-efi-x64", "grub2-efi-x64-modules"],
|
||||
"debian": ["grub-efi-amd64"],
|
||||
"ubuntu": ["grub-efi-amd64"],
|
||||
"arch": ["grub"], # Arch 的 grub 包包含 EFI 支持
|
||||
"manjaro": ["grub"],
|
||||
"centos": ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64"],
|
||||
"rhel": ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64"],
|
||||
"fedora": ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64"],
|
||||
"rocky": ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64"],
|
||||
"almalinux": ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64"],
|
||||
"debian": ["grub-efi-amd64", "shim-signed"],
|
||||
"ubuntu": ["grub-efi-amd64", "shim-signed"],
|
||||
"arch": ["grub", "efibootmgr"], # Arch 的 grub 包包含 EFI 支持
|
||||
"manjaro": ["grub", "efibootmgr"],
|
||||
}
|
||||
|
||||
efi_packages = efi_packages_map.get(distro_type, [])
|
||||
@@ -1194,6 +1195,8 @@ def check_and_install_efi_modules(mount_point: str, distro_type: str, is_uefi: b
|
||||
log_warning(f"未知的发行版 '{distro_type}',无法自动安装 EFI 模块")
|
||||
return False, f"无法确定 {distro_type} 的 EFI 包名"
|
||||
|
||||
log_info(f"将安装以下 EFI 包: {', '.join(efi_packages)}")
|
||||
|
||||
# 安装 EFI 包前,先确保网络连接(复制 DNS 配置)
|
||||
resolv_source = "/etc/resolv.conf"
|
||||
resolv_target = os.path.join(mount_point, "etc/resolv.conf")
|
||||
@@ -1244,6 +1247,113 @@ def check_and_install_efi_modules(mount_point: str, distro_type: str, is_uefi: b
|
||||
return False, "UEFI 模块安装失败"
|
||||
|
||||
|
||||
def _manual_install_efi_files(mount_point: str, efi_target: str, efi_grub_file: str,
|
||||
efi_boot_file: str, bootloader_id: str = "GRUB") -> bool:
|
||||
"""
|
||||
手动安装 EFI 文件(当 grub2-install 因为 Secure Boot 不支持时使用)。
|
||||
直接从 /boot/efi/EFI/<distro>/ 复制已有的 EFI 文件,或使用 shim 链式加载。
|
||||
|
||||
返回: 是否成功
|
||||
"""
|
||||
log_step("手动安装 EFI 文件", f"目标: {efi_grub_file}")
|
||||
|
||||
efi_firmware_dir = os.path.join(mount_point, "boot/efi/EFI")
|
||||
|
||||
# 创建目标目录
|
||||
target_dir = os.path.join(efi_firmware_dir, bootloader_id)
|
||||
try:
|
||||
os.makedirs(target_dir, exist_ok=True)
|
||||
log_info(f"创建/确认 EFI 目录: {target_dir}")
|
||||
except Exception as e:
|
||||
log_error(f"创建 EFI 目录失败: {e}")
|
||||
return False
|
||||
|
||||
# 查找源 EFI 文件
|
||||
# 可能的源路径:
|
||||
source_paths = []
|
||||
|
||||
# 1. 从 /boot/efi/EFI/centos 或 /boot/efi/EFI/redhat 复制
|
||||
for subdir in ["centos", "redhat", "fedora", "rocky", "almalinux", "boot"]:
|
||||
source_paths.append(os.path.join(efi_firmware_dir, subdir, efi_grub_file))
|
||||
source_paths.append(os.path.join(efi_firmware_dir, subdir, "shimx64.efi"))
|
||||
source_paths.append(os.path.join(efi_firmware_dir, subdir, "shim.efi"))
|
||||
|
||||
# 2. 从 /boot/efi/ 直接查找
|
||||
source_paths.append(os.path.join(mount_point, "boot/efi", efi_grub_file))
|
||||
source_paths.append(os.path.join(mount_point, "boot/efi", "shimx64.efi"))
|
||||
|
||||
# 3. 从 /usr/lib/grub/x86_64-efi/ 复制 grubx64.efi
|
||||
grub_modules_dir = os.path.join(mount_point, f"usr/lib/grub/{efi_target}")
|
||||
source_paths.append(os.path.join(grub_modules_dir, "grubx64.efi"))
|
||||
|
||||
# 查找 shim 和 grub
|
||||
shim_source = None
|
||||
grub_source = None
|
||||
|
||||
for path in source_paths:
|
||||
if os.path.exists(path):
|
||||
log_info(f"找到 EFI 文件: {path}")
|
||||
if "shim" in path.lower():
|
||||
shim_source = path
|
||||
elif "grub" in path.lower():
|
||||
grub_source = path
|
||||
|
||||
# 复制文件
|
||||
try:
|
||||
if grub_source:
|
||||
grub_target = os.path.join(target_dir, efi_grub_file)
|
||||
shutil.copy2(grub_source, grub_target)
|
||||
log_success(f"✓ 复制 GRUB: {grub_source} -> {grub_target}")
|
||||
|
||||
if shim_source:
|
||||
# 如果使用 shim,复制 shim 并重命名为 grubx64.efi
|
||||
# 或者保留 shim 文件名并在 NVRAM 中注册
|
||||
shim_target = os.path.join(target_dir, "shimx64.efi")
|
||||
shutil.copy2(shim_source, shim_target)
|
||||
log_success(f"✓ 复制 Shim: {shim_source} -> {shim_target}")
|
||||
|
||||
# 同时复制为 bootloader 名称,便于启动
|
||||
if efi_grub_file != "shimx64.efi":
|
||||
shutil.copy2(shim_source, os.path.join(target_dir, efi_grub_file))
|
||||
|
||||
# 如果没有找到任何文件,尝试从 grub 模块生成
|
||||
if not grub_source and not shim_source:
|
||||
log_warning("未找到现有 EFI 文件,尝试使用 grub-mkimage 生成...")
|
||||
|
||||
# 构建 grub.efi 路径
|
||||
grub_efi_output = os.path.join(target_dir, efi_grub_file)
|
||||
|
||||
# 获取模块列表(必需模块)
|
||||
modules = "part_gpt part_msdos fat ext2 xfs btrfs normal boot linux configfile search search_fs_uuid search_fs_file"
|
||||
|
||||
# 尝试使用 grub-mkimage 生成 EFI 文件
|
||||
chroot_cmd_prefix = ["sudo", "chroot", mount_point]
|
||||
mkimage_cmd = chroot_cmd_prefix + [
|
||||
"grub2-mkimage",
|
||||
"-o", grub_efi_output,
|
||||
"-O", efi_target,
|
||||
"-p", "/boot/grub2",
|
||||
] + modules.split()
|
||||
|
||||
success, _, stderr = run_command(
|
||||
mkimage_cmd,
|
||||
"使用 grub-mkimage 生成 EFI 文件",
|
||||
timeout=60
|
||||
)
|
||||
|
||||
if success and os.path.exists(grub_efi_output):
|
||||
log_success(f"✓ 成功生成 EFI 文件: {grub_efi_output}")
|
||||
else:
|
||||
log_error(f"生成 EFI 文件失败: {stderr}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
log_error(f"手动安装 EFI 文件失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def install_efi_fallback(mount_point: str, efi_target: str, efi_grub_file: str,
|
||||
efi_boot_file: str, bootloader_id: str = "GRUB") -> bool:
|
||||
"""
|
||||
@@ -1920,6 +2030,16 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
|
||||
else:
|
||||
install_errors.append(f"no-nvram 模式: {stderr}")
|
||||
|
||||
# 如果所有 grub2-install 方法都失败,尝试手动复制 EFI 文件(针对 Secure Boot 问题)
|
||||
if not grub_install_success and "Secure Boot" in str(install_errors):
|
||||
log_info("grub2-install 不支持 Secure Boot,尝试手动安装 EFI 文件...")
|
||||
success = _manual_install_efi_files(mount_point, efi_target, efi_grub_file, efi_boot_file, bootloader_id)
|
||||
if success:
|
||||
grub_install_success = True
|
||||
log_success("✓ 手动 EFI 文件安装成功")
|
||||
else:
|
||||
log_error("手动 EFI 文件安装失败")
|
||||
|
||||
# 安装 EFI fallback(如果启用)
|
||||
if grub_install_success and use_fallback:
|
||||
log_info("安装 EFI fallback 文件...")
|
||||
|
||||
Reference in New Issue
Block a user