This commit is contained in:
zj
2026-02-12 03:22:36 +08:00
parent 7b20993078
commit 87702b1a24

View File

@@ -2283,19 +2283,36 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str,
except Exception as e: except Exception as e:
log_warning(f"复制失败: {e}") log_warning(f"复制失败: {e}")
# 如果仍未创建成功,尝试直接创建 # 如果仍未创建成功,使用 shell 命令在 chroot 内创建
if not os.path.exists(grubenv_path): if not os.path.exists(grubenv_path):
try: log_info(f"尝试使用 shell 命令创建 grubenv 文件...")
# 确保目录存在
os.makedirs(os.path.dirname(grubenv_path), exist_ok=True) # 方法: 使用 echo 和 tee 在 chroot 内创建文件
# 创建一个空的 grubenv 文件(包含必要的头部) grubenv_chroot_path = grubenv_dir_in_chroot + "/grubenv"
with open(grubenv_path, 'w') as f: grubenv_content = "# GRUB Environment Block\nsaved_entry=\n" + "#" * 1024 + "\n"
f.write("# GRUB Environment Block\n")
f.write("saved_entry=\n") # 使用 printf 创建文件(更可靠)
f.write("#" * 1024 + "\n") # 填充到至少1KB success, _, stderr = run_command(
chroot_cmd_prefix + ["sh", "-c", f'printf "%s" "{grubenv_content}" > "{grubenv_chroot_path}"'],
"使用 shell 创建 grubenv 文件",
timeout=5
)
if success or os.path.exists(grubenv_path):
log_success(f"✓ 手动创建 grubenv 文件成功: {grubenv_path}") log_success(f"✓ 手动创建 grubenv 文件成功: {grubenv_path}")
except Exception as e: else:
log_warning(f"创建 grubenv 文件失败: {e}") log_warning(f"Shell 创建失败: {stderr}")
# 最后尝试: 使用 touch 创建空文件
success, _, _ = run_command(
chroot_cmd_prefix + ["touch", grubenv_chroot_path],
"创建空 grubenv 文件",
timeout=5
)
if success or os.path.exists(grubenv_path):
log_success(f"✓ 创建空 grubenv 文件成功: {grubenv_path}")
else:
log_warning(f"所有方法创建 grubenv 文件均失败")
# 执行配置更新 # 执行配置更新
success, stdout, stderr = run_command( success, stdout, stderr = run_command(