This commit is contained in:
zj
2026-02-12 04:47:30 +08:00
parent 3089bf955f
commit dcd2d242b6

View File

@@ -1397,37 +1397,25 @@ def _manual_install_efi_files(mount_point: str, efi_target: str, efi_grub_file:
# 创建 EFI 分区上的 grub.cfgGRUB 首先查找这里) # 创建 EFI 分区上的 grub.cfgGRUB 首先查找这里)
helper_cfg_path = os.path.join(mount_point, f"boot/efi/EFI/{bootloader_id}/grub.cfg") helper_cfg_path = os.path.join(mount_point, f"boot/efi/EFI/{bootloader_id}/grub.cfg")
try: try:
if boot_uuid: # 使用通用 search --file 方法
# 使用 UUID 搜索 /boot 分区 helper_cfg = '''# BootRepairTool - Auto-find /boot partition
helper_cfg = f'''# Auto-generated by BootRepairTool # Try to find grub2/grub.cfg on any partition
# For separate /boot partition (UUID: {boot_uuid})
# Search for /boot partition by UUID if search --no-floppy --set=root --file /grub2/grub.cfg; then
search --no-floppy --fs-uuid --set=root {boot_uuid}
set prefix=($root)/grub2 set prefix=($root)/grub2
elif search --no-floppy --set=root --file /grub/grub.cfg; then
# Load main configuration set prefix=($root)/grub
configfile ($root)/grub2/grub.cfg elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
''' set prefix=($root)/boot/grub2
else: elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
# 回退:尝试常见分区号 set prefix=($root)/boot/grub
helper_cfg = '''# Auto-generated by BootRepairTool
# For separate /boot partition
# Try to find /boot partition by looking for grub2 directory
if [ -f (hd0,gpt2)/grub2/grub.cfg ]; then
set root=(hd0,gpt2)
set prefix=(hd0,gpt2)/grub2
elif [ -f (hd0,2)/grub2/grub.cfg ]; then
set root=(hd0,2)
set prefix=(hd0,2)/grub2
elif [ -f (hd1,gpt2)/grub2/grub.cfg ]; then
set root=(hd1,gpt2)
set prefix=(hd1,gpt2)/grub2
fi fi
# Load main configuration if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg"
fi
''' '''
with open(helper_cfg_path, 'w') as f: with open(helper_cfg_path, 'w') as f:
f.write(helper_cfg) f.write(helper_cfg)
@@ -1549,22 +1537,25 @@ def install_efi_fallback(mount_point: str, efi_target: str, efi_grub_file: str,
# 在 Boot 目录创建 grub.cfg # 在 Boot 目录创建 grub.cfg
fallback_cfg_path = os.path.join(boot_dir, "grub.cfg") fallback_cfg_path = os.path.join(boot_dir, "grub.cfg")
try: try:
if boot_uuid: # 使用通用 search --file 方法
fallback_cfg = f'''# BootRepairTool fallback config fallback_cfg = '''# BootRepairTool - Auto-find /boot partition
search --no-floppy --fs-uuid --set=root {boot_uuid} # Try to find grub2/grub.cfg on any partition
if search --no-floppy --set=root --file /grub2/grub.cfg; then
set prefix=($root)/grub2 set prefix=($root)/grub2
configfile ($root)/grub2/grub.cfg elif search --no-floppy --set=root --file /grub/grub.cfg; then
''' set prefix=($root)/grub
else: elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
fallback_cfg = '''# BootRepairTool fallback config set prefix=($root)/boot/grub2
if [ -f (hd0,gpt2)/grub2/grub.cfg ]; then elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
set root=(hd0,gpt2) set prefix=($root)/boot/grub
set prefix=(hd0,gpt2)/grub2
elif [ -f (hd0,2)/grub2/grub.cfg ]; then
set root=(hd0,2)
set prefix=(hd0,2)/grub2
fi fi
if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg"
fi
''' '''
with open(fallback_cfg_path, 'w') as f: with open(fallback_cfg_path, 'w') as f:
f.write(fallback_cfg) f.write(fallback_cfg)
@@ -2185,46 +2176,36 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
if has_separate_boot: if has_separate_boot:
log_info("为独立 /boot 分区创建 EFI 辅助 grub.cfg...") log_info("为独立 /boot 分区创建 EFI 辅助 grub.cfg...")
# 获取 /boot 分区 UUID # 为每个 EFI 目录创建 grub.cfg使用通用搜索方法
boot_uuid = None
try:
success_uuid, uuid_out, _ = run_command(
["sudo", "blkid", "-s", "UUID", "-o", "value", f"{mount_point}/boot"],
"获取 /boot UUID",
timeout=10
)
if success_uuid:
boot_uuid = uuid_out.strip()
log_info(f" /boot UUID: {boot_uuid}")
except Exception as e:
log_warning(f" 获取 UUID 失败: {e}")
# 为每个 EFI 目录创建 grub.cfg
for efi_dir in [efi_bootloader_id, "Boot"]: for efi_dir in [efi_bootloader_id, "Boot"]:
efi_cfg_dir = os.path.join(mount_point, "boot/efi/EFI", efi_dir) efi_cfg_dir = os.path.join(mount_point, "boot/efi/EFI", efi_dir)
if os.path.exists(efi_cfg_dir): if os.path.exists(efi_cfg_dir):
efi_cfg_path = os.path.join(efi_cfg_dir, "grub.cfg") efi_cfg_path = os.path.join(efi_cfg_dir, "grub.cfg")
try: try:
if boot_uuid: # 使用 search --file 通用搜索方法
efi_cfg_content = f'''# BootRepairTool - Find /boot partition efi_cfg_content = '''# BootRepairTool - Auto-find /boot partition
search --no-floppy --fs-uuid --set=root {boot_uuid} # Try to find grub2/grub.cfg on any partition
if search --no-floppy --set=root --file /grub2/grub.cfg; then
set prefix=($root)/grub2 set prefix=($root)/grub2
configfile ($root)/grub2/grub.cfg elif search --no-floppy --set=root --file /grub/grub.cfg; then
''' set prefix=($root)/grub
else: elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
efi_cfg_content = '''# BootRepairTool - Find /boot partition set prefix=($root)/boot/grub2
if [ -f (hd0,gpt2)/grub2/grub.cfg ]; then elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
set root=(hd0,gpt2) set prefix=($root)/boot/grub
set prefix=(hd0,gpt2)/grub2
elif [ -f (hd0,2)/grub2/grub.cfg ]; then
set root=(hd0,2)
set prefix=(hd0,2)/grub2
fi fi
if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg on any partition."
echo "Please check your /boot partition."
fi
''' '''
with open(efi_cfg_path, 'w') as f: with open(efi_cfg_path, 'w') as f:
f.write(efi_cfg_content) f.write(efi_cfg_content)
log_success(f"✓ 创建 {efi_dir}/grub.cfg") log_success(f"✓ 创建 {efi_dir}/grub.cfg (通用搜索)")
except Exception as e: except Exception as e:
log_warning(f" 创建失败: {e}") log_warning(f" 创建失败: {e}")
@@ -2716,9 +2697,14 @@ insmod part_gpt
insmod xfs insmod xfs
insmod fat insmod fat
# Search for the /boot partition by UUID, then set root to the system root # Search for the /boot partition by UUID, or try search by file as fallback
search --no-floppy --fs-uuid --set=root {boot_uuid} if search --no-floppy --fs-uuid --set=root {boot_uuid}; then
set prefix=($root)/grub2 set prefix=($root)/grub2
else
# Fallback: search by file
search --no-floppy --set=root --file /grub2/grub.cfg
set prefix=($root)/grub2
fi
menuentry 'CentOS Linux (Auto)' {{ menuentry 'CentOS Linux (Auto)' {{
load_video load_video