diff --git a/backend.py b/backend.py index 34f949b..24675a9 100644 --- a/backend.py +++ b/backend.py @@ -2283,19 +2283,36 @@ def chroot_and_repair_grub(mount_point: str, target_disk: str, except Exception as e: log_warning(f"复制失败: {e}") - # 如果仍未创建成功,尝试直接创建 + # 如果仍未创建成功,使用 shell 命令在 chroot 内创建 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") - f.write("saved_entry=\n") - f.write("#" * 1024 + "\n") # 填充到至少1KB + log_info(f"尝试使用 shell 命令创建 grubenv 文件...") + + # 方法: 使用 echo 和 tee 在 chroot 内创建文件 + grubenv_chroot_path = grubenv_dir_in_chroot + "/grubenv" + grubenv_content = "# GRUB Environment Block\nsaved_entry=\n" + "#" * 1024 + "\n" + + # 使用 printf 创建文件(更可靠) + 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}") - except Exception as e: - log_warning(f"创建 grubenv 文件失败: {e}") + else: + 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(