This commit is contained in:
zj
2026-02-11 15:13:21 +08:00
parent d56c24275a
commit 363c448872
2 changed files with 62 additions and 3 deletions

Binary file not shown.

View File

@@ -978,9 +978,49 @@ def _auto_install_grub(mount_point: str, distro_type: str) -> bool:
timeout=300
)
# 即使 yum 报告"已安装",也要检查实际命令是否存在
# CentOS/RHEL 的命令可能在 /usr/sbin/ 下,需要确保 PATH 包含该路径
if success:
log_success(f"✓ GRUB 包安装成功")
return True
log_info("包安装命令执行完成,检查命令是否可用...")
# 尝试查找 grub 相关命令的实际路径
grub_paths = [
"/usr/sbin/grub2-install",
"/usr/sbin/grub2-mkconfig",
"/usr/sbin/grub-install",
"/usr/sbin/grub-mkconfig",
"/sbin/grub2-install",
"/sbin/grub2-mkconfig",
]
found_any = False
for grub_path in grub_paths:
full_path = mount_point + grub_path
if os.path.exists(full_path):
log_info(f" 找到 GRUB 命令: {grub_path}")
found_any = True
if found_any:
log_success(f"✓ GRUB 包已安装")
return True
else:
log_warning("包管理器报告成功,但未找到 GRUB 命令")
log_info("尝试重新安装...")
# 强制重新安装
if distro_type in ["centos", "rhel", "fedora", "rocky", "almalinux"]:
reinstall_cmd = chroot_cmd_prefix + ["yum", "reinstall", "-y"] + packages
run_command(reinstall_cmd, "强制重新安装 GRUB 包", timeout=300)
# 再次检查
for grub_path in grub_paths:
full_path = mount_point + grub_path
if os.path.exists(full_path):
log_info(f" 找到 GRUB 命令: {grub_path}")
return True
log_error("仍无法找到 GRUB 命令")
return False
else:
log_error(f"✗ GRUB 包安装失败: {stderr}")
return False
@@ -994,7 +1034,7 @@ def check_chroot_environment(mount_point: str, distro_type: str = "unknown") ->
"""
log_step("检查 chroot 环境", f"挂载点: {mount_point}")
# 检查关键命令是否存在
# 检查关键命令是否存在(在 PATH 中)
critical_commands = ["grub-install", "grub-mkconfig", "update-grub", "grub2-install", "grub2-mkconfig"]
found_commands = []
@@ -1007,6 +1047,25 @@ def check_chroot_environment(mount_point: str, distro_type: str = "unknown") ->
else:
log_debug(f" ✗ 未找到命令: {cmd}")
# 如果在 PATH 中未找到检查常见的sbin路径某些发行版sbin不在默认PATH中
if not found_commands:
log_info("在 PATH 中未找到命令,检查 sbin 目录...")
sbin_paths = [
"/usr/sbin/grub2-install",
"/usr/sbin/grub2-mkconfig",
"/usr/sbin/grub-install",
"/usr/sbin/grub-mkconfig",
"/sbin/grub2-install",
"/sbin/grub2-mkconfig",
]
for sbin_path in sbin_paths:
full_path = mount_point + sbin_path
if os.path.exists(full_path):
cmd_name = os.path.basename(sbin_path)
found_commands.append(cmd_name)
log_info(f" ✓ 找到命令: {sbin_path}")
if not found_commands:
log_warning(f"chroot 环境中未找到关键的 GRUB 命令")
log_info(f"尝试自动安装 GRUB 包...")