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