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 首先查找这里)
helper_cfg_path = os.path.join(mount_point, f"boot/efi/EFI/{bootloader_id}/grub.cfg")
try:
if boot_uuid:
# 使用 UUID 搜索 /boot 分区
helper_cfg = f'''# Auto-generated by BootRepairTool
# For separate /boot partition (UUID: {boot_uuid})
# 使用通用 search --file 方法
helper_cfg = '''# BootRepairTool - Auto-find /boot partition
# Try to find grub2/grub.cfg on any partition
# Search for /boot partition by UUID
search --no-floppy --fs-uuid --set=root {boot_uuid}
set prefix=($root)/grub2
# Load main configuration
configfile ($root)/grub2/grub.cfg
'''
else:
# 回退:尝试常见分区号
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
if search --no-floppy --set=root --file /grub2/grub.cfg; then
set prefix=($root)/grub2
elif search --no-floppy --set=root --file /grub/grub.cfg; then
set prefix=($root)/grub
elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
set prefix=($root)/boot/grub2
elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
set prefix=($root)/boot/grub
fi
# Load main configuration
configfile ${prefix}/grub.cfg
if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg"
fi
'''
with open(helper_cfg_path, 'w') as f:
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
fallback_cfg_path = os.path.join(boot_dir, "grub.cfg")
try:
if boot_uuid:
fallback_cfg = f'''# BootRepairTool fallback config
search --no-floppy --fs-uuid --set=root {boot_uuid}
set prefix=($root)/grub2
configfile ($root)/grub2/grub.cfg
'''
else:
fallback_cfg = '''# BootRepairTool fallback config
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
# 使用通用 search --file 方法
fallback_cfg = '''# BootRepairTool - Auto-find /boot partition
# Try to find grub2/grub.cfg on any partition
if search --no-floppy --set=root --file /grub2/grub.cfg; then
set prefix=($root)/grub2
elif search --no-floppy --set=root --file /grub/grub.cfg; then
set prefix=($root)/grub
elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
set prefix=($root)/boot/grub2
elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
set prefix=($root)/boot/grub
fi
if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg"
fi
configfile ${prefix}/grub.cfg
'''
with open(fallback_cfg_path, 'w') as f:
f.write(fallback_cfg)
@@ -2185,46 +2176,36 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
if has_separate_boot:
log_info("为独立 /boot 分区创建 EFI 辅助 grub.cfg...")
# 获取 /boot 分区 UUID
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
# 为每个 EFI 目录创建 grub.cfg使用通用搜索方法
for efi_dir in [efi_bootloader_id, "Boot"]:
efi_cfg_dir = os.path.join(mount_point, "boot/efi/EFI", efi_dir)
if os.path.exists(efi_cfg_dir):
efi_cfg_path = os.path.join(efi_cfg_dir, "grub.cfg")
try:
if boot_uuid:
efi_cfg_content = f'''# BootRepairTool - Find /boot partition
search --no-floppy --fs-uuid --set=root {boot_uuid}
set prefix=($root)/grub2
configfile ($root)/grub2/grub.cfg
'''
else:
efi_cfg_content = '''# BootRepairTool - Find /boot partition
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
# 使用 search --file 通用搜索方法
efi_cfg_content = '''# BootRepairTool - Auto-find /boot partition
# Try to find grub2/grub.cfg on any partition
if search --no-floppy --set=root --file /grub2/grub.cfg; then
set prefix=($root)/grub2
elif search --no-floppy --set=root --file /grub/grub.cfg; then
set prefix=($root)/grub
elif search --no-floppy --set=root --file /boot/grub2/grub.cfg; then
set prefix=($root)/boot/grub2
elif search --no-floppy --set=root --file /boot/grub/grub.cfg; then
set prefix=($root)/boot/grub
fi
if [ -n "$root" ] && [ -n "$prefix" ]; then
configfile ${prefix}/grub.cfg
else
echo "Error: Could not find grub.cfg on any partition."
echo "Please check your /boot partition."
fi
configfile ${prefix}/grub.cfg
'''
with open(efi_cfg_path, 'w') as f:
f.write(efi_cfg_content)
log_success(f"✓ 创建 {efi_dir}/grub.cfg")
log_success(f"✓ 创建 {efi_dir}/grub.cfg (通用搜索)")
except Exception as e:
log_warning(f" 创建失败: {e}")
@@ -2716,9 +2697,14 @@ insmod part_gpt
insmod xfs
insmod fat
# Search for the /boot partition by UUID, then set root to the system root
search --no-floppy --fs-uuid --set=root {boot_uuid}
set prefix=($root)/grub2
# Search for the /boot partition by UUID, or try search by file as fallback
if search --no-floppy --fs-uuid --set=root {boot_uuid}; then
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)' {{
load_video