This commit is contained in:
zj
2026-02-12 03:14:09 +08:00
parent 8769ec5698
commit 3d3d1aac26

View File

@@ -2235,13 +2235,22 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
else:
grub_dir_name = "grub"
# 首先确保首选目录存在(通过主机直接创建
# 首先确保首选目录存在(使用 shell 命令,因为 /boot 可能是独立分区
preferred_dir = os.path.dirname(grubenv_path) # 正确路径的目录
try:
os.makedirs(preferred_dir, exist_ok=True)
log_info(f"✓ 确保目录存在: {preferred_dir}")
except Exception as e:
log_warning(f"创建目录失败: {e}")
# 使用 mkdir 命令创建目录(在 chroot 内,确保在正确的文件系统中)
grubenv_dir_in_chroot = os.path.dirname(config_path) # /boot/grub2 或 /boot/grub
success, _, _ = run_command(
chroot_cmd_prefix + ["mkdir", "-p", grubenv_dir_in_chroot],
f"确保 {grubenv_dir_in_chroot} 目录存在",
timeout=5
)
if success:
log_info(f"✓ 确保目录存在: {grubenv_dir_in_chroot}")
# 再次检查 grubenv_path主机路径
log_debug(f"检查 grubenv 路径: {grubenv_path}, 存在: {os.path.exists(grubenv_path)}")
log_debug(f"检查目录存在: {preferred_dir}, 存在: {os.path.exists(preferred_dir)}")
# 尝试多种可能的路径,检查是否已有 grubenv 在错误位置
possible_wrong_paths = [
@@ -2257,9 +2266,11 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
break
# 如果找到错误位置的 grubenv复制到正确位置
if wrong_path_found:
if wrong_path_found and not os.path.exists(grubenv_path):
try:
import shutil
# 确保目标目录存在(在主机上)
os.makedirs(os.path.dirname(grubenv_path), exist_ok=True)
shutil.copy2(wrong_path_found, grubenv_path)
log_success(f"✓ 复制 grubenv 到正确位置: {grubenv_path}")
except Exception as e:
@@ -2268,6 +2279,8 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
# 如果仍未创建成功,尝试直接创建
if not os.path.exists(grubenv_path):
try:
# 确保目录存在
os.makedirs(os.path.dirname(grubenv_path), exist_ok=True)
# 创建一个空的 grubenv 文件(包含必要的头部)
with open(grubenv_path, 'w') as f:
f.write("# GRUB Environment Block\n")