diff --git a/backend.py b/backend.py index 24675a9..62e9c25 100644 --- a/backend.py +++ b/backend.py @@ -2283,6 +2283,45 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str, except Exception as e: log_warning(f"复制失败: {e}") + # 检查是否是符号链接(CentOS 8 等系统的 grubenv 可能是指向 EFI 分区的符号链接) + if not os.path.exists(grubenv_path) and os.path.islink(grubenv_path): + log_info(f"检测到 grubenv 是符号链接,尝试创建链接目标...") + try: + link_target = os.readlink(grubenv_path) + log_info(f" 链接目标: {link_target}") + + # 解析链接目标为绝对路径 + if link_target.startswith('/'): + # 绝对路径 + target_path = os.path.join(mount_point, link_target.lstrip('/')) + else: + # 相对路径(如 ../efi/EFI/centos/grubenv) + link_dir = os.path.dirname(grubenv_path) + target_path = os.path.normpath(os.path.join(link_dir, link_target)) + + log_info(f" 解析后的目标路径: {target_path}") + + # 创建目标目录 + target_dir = os.path.dirname(target_path) + if not os.path.exists(target_dir): + os.makedirs(target_dir, exist_ok=True) + log_info(f"✓ 创建目录: {target_dir}") + + # 创建目标文件(1024 字节的 GRUB 环境块) + grubenv_content = "# GRUB Environment Block\nsaved_entry=\n" + "#" * 1024 + "\n" + with open(target_path, 'w') as f: + f.write(grubenv_content) + log_success(f"✓ 创建符号链接目标文件: {target_path}") + + except Exception as e: + log_warning(f"创建符号链接目标失败: {e}") + # 如果创建符号链接目标失败,删除损坏的符号链接并创建普通文件 + try: + os.unlink(grubenv_path) + log_info(f" 已删除损坏的符号链接: {grubenv_path}") + except Exception as e2: + log_warning(f" 删除符号链接失败: {e2}") + # 如果仍未创建成功,使用 shell 命令在 chroot 内创建 if not os.path.exists(grubenv_path): log_info(f"尝试使用 shell 命令创建 grubenv 文件...")