Installing GRUB using grub-install

当遇到GRUB错误导致无法进入系统时,可以使用Ubuntu livecd进行修复。通过grub-install将GRUB重新安装到MBR。确保指定正确的设备文件,如'/dev/sda',并根据需要使用--boot-directory选项指定GRUB图像的存放目录。此过程涉及grub-mkimage和grub-setup,但直接使用这些命令可能风险较高,适合熟悉GRUB内部原理的用户。

 

平时总是弄得grub出错进不来系统,给我来个grub-rescue,我用livecd的Ubuntu进入系统后,重新安装grub到mbr,方法如下:

grub-install --boot-directory=/mnt/boot /dev/sda

其中的boot目录就是挂载的boot分区,如果没有boot分区,那就是根目录

下面是安装文档的原文:

3.1 Installing GRUB using grub-install

For information on where GRUB should be installed on PC BIOS platforms,see BIOS installation.

In order to install GRUB under a UNIX-like OS (suchas gnu), invoke the programgrub-install (see

[02:28:27] ✅ 密码正确,开始部署... [02:28:27] 🚀 优化系统资源使用:启用最大线程与内存 [02:28:27] 🧠 检测到 CPU 核心数: 32 [02:28:27] 🔧 执行: 📦 安装 pigz(并行 gzip) (最长等待 120 秒) E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem. [02:28:27] ❌ 命令失败 (退出码 100): 📦 安装 pigz(并行 gzip) [02:28:27] ⚡ 已启用多线程加速: 7z + pigz [02:28:27] 🔍 正在检测系统兼容性... [02:28:27] ✅ 操作系统: Ubuntu 20.04 (focal) [02:28:27] 🟢 版本受支持 [02:28:27] ✅ CPU 架构: x86_64 [02:28:27] 🔧 正在清理 APT 锁文件和占用进程... Setting up cryptsetup (2:2.2.2-3ubuntu2.5) ... Setting up linux-headers-5.4.0-216-generic (5.4.0-216.236) ... Setting up bind9-libs:amd64 (1:9.18.30-0ubuntu0.20.04.2) ... Setting up grub2-common (2.04-1ubuntu26.17) ... Setting up iptables (1.8.4-3ubuntu2.1) ... Setting up python3-apt (2.0.1ubuntu0.20.04.1) ... Setting up libasn1-8-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up gpgsm (2.2.19-3ubuntu2.5) ... Setting up ltrace (0.7.3-6.1ubuntu1.1) ... Setting up libnss3:amd64 (2:3.98-0ubuntu0.20.04.2) ... Setting up linux-headers-generic (5.4.0.216.208) ... Setting up libmbim-proxy (1.26.2-1~ubuntu20.04.1) ... Setting up kmod (27-1ubuntu2.1) ... update-initramfs: deferring update (trigger activated) Setting up libgpgme11:amd64 (1.13.1-7ubuntu2.2) ... Setting up libhcrypto4-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up grub-pc-bin (2.04-1ubuntu26.17) ... Setting up fdisk (2.34-0.1ubuntu9.6) ... Setting up python3-apport (2.20.11-0ubuntu27.31) ... Setting up grub-pc (2.04-1ubuntu26.17) ... Sourcing file `/etc/default/grub' Sourcing file `/etc/default/grub.d/50-cloudimg-settings.cfg' Sourcing file `/etc/default/grub.d/init-select.cfg' Generating grub configuration file ... Found linux image: /boot/vmlinuz-5.4.0-216-generic Found linux image: /boot/vmlinuz-5.4.0-110-generic Found initrd image: /boot/initrd.img-5.4.0-110-generic done Setting up unattended-upgrades (2.3ubuntu0.3) ... Setting up libwind0-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up python3-packaging (20.3-1) ... Setting up cryptsetup-run (2:2.2.2-3ubuntu2.5) ... Setting up python3-software-properties (0.99.9.12) ... Setting up python3-twisted (18.9.0-11ubuntu0.20.04.5) ... Setting up libpython3.8:amd64 (3.8.10-0ubuntu1~20.04.18) ... Setting up python3-requests (2.22.0-2ubuntu1.1) ... Setting up udev (245.4-4ubuntu3.24) ... update-initramfs: deferring update (trigger activated) Setting up sosreport (4.8.2-0ubuntu0~20.04.1) ... Setting up gpg-wks-server (2.2.19-3ubuntu2.5) ... Setting up kpartx (0.8.3-1ubuntu2.4) ... Setting up procps (2:3.3.16-1ubuntu2.4) ... Setting up initramfs-tools-core (0.136ubuntu6.8) ... Setting up bind9-host (1:9.18.30-0ubuntu0.20.04.2) ... Setting up ubuntu-pro-client (37.1ubuntu0~20.04) ... Setting up linux-image-5.4.0-216-generic (5.4.0-216.236) ... Setting up ubuntu-pro-client-l10n (37.1ubuntu0~20.04) ... Setting up initramfs-tools (0.136ubuntu6.8) ... update-initramfs: deferring update (trigger activated) Setting up linux-headers-virtual (5.4.0.216.208) ... Setting up grub-efi-amd64-signed (1.187.12~20.04+2.06-2ubuntu14.8) ... Installing grub to /boot/efi. Installing for x86_64-efi platform. grub-install: warning: EFI variables are not supported on this system.. Installation finished. No error reported. Setting up openssh-server (1:8.2p1-4ubuntu0.13) ... rescue-ssh.target is a disabled or a static unit, not starting it. Setting up libhx509-5-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up libqmi-glib5:amd64 (1.30.4-1~ubuntu20.04.1) ... Setting up landscape-common (23.02-0ubuntu1~20.04.4) ... Setting up shim-signed (1.40.10+15.8-0ubuntu1) ... update-alternatives: using /usr/lib/shim/shimx64.efi.signed.latest to provide /usr/lib/shim/shimx64.efi.signed (shimx64.efi.signed) in auto mode Installing grub to /boot/efi. Installing for x86_64-efi platform. grub-install: warning: EFI variables are not supported on this system.. Installation finished. No error reported. Setting up open-iscsi (2.0.874-7.1ubuntu6.5) ... grep: /lib/modules-load.d/open-iscsi.conf: No such file or directory Setting up software-properties-common (0.99.9.12) ... Setting up apport (2.20.11-0ubuntu27.31) ... Installing new version of config file /etc/init.d/apport ... apport-autoreport.service is a disabled or a static unit, not starting it. Setting up multipath-tools (0.8.3-1ubuntu2.4) ... Removing obsolete conffile /etc/init.d/multipath-tools ... Setting up ubuntu-advantage-tools (37.1ubuntu0~20.04) ... Removing obsolete conffile /etc/update-motd.d/88-esm-announce ... Removing obsolete conffile /etc/ubuntu-advantage/help_data.yaml ... Setting up bind9-dnsutils (1:9.18.30-0ubuntu0.20.04.2) ... Setting up linux-image-virtual (5.4.0.216.208) ... Setting up cryptsetup-initramfs (2:2.2.2-3ubuntu2.5) ... update-initramfs: deferring update (trigger activated) update-initramfs: deferring update (trigger activated) Setting up libkrb5-26-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up libqmi-proxy (1.30.4-1~ubuntu20.04.1) ... Setting up libheimntlm0-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up linux-virtual (5.4.0.216.208) ... Setting up libgssapi3-heimdal:amd64 (7.7.0+dfsg-1ubuntu1.4) ... Setting up modemmanager (1.18.6-1~ubuntu20.04.1) ... Installing new version of config file /etc/dbus-1/system.d/org.freedesktop.ModemManager1.conf ... Setting up libldap-2.4-2:amd64 (2.4.49+dfsg-2ubuntu1.10) ... Setting up libcurl3-gnutls:amd64 (7.68.0-1ubuntu2.25) ... Setting up dirmngr (2.2.19-3ubuntu2.5) ... Setting up gpg-wks-client (2.2.19-3ubuntu2.5) ... Setting up libfwupd2:amd64 (1.7.9-1~20.04.3) ... Setting up gnupg (2.2.19-3ubuntu2.5) ... Setting up libfwupdplugin5:amd64 (1.7.9-1~20.04.3) ... Setting up fwupd (1.7.9-1~20.04.3) ... Installing new version of config file /etc/fwupd/redfish.conf ... fwupd-offline-update.service is a disabled or a static unit not running, not starting it. fwupd-refresh.service is a disabled or a static unit not running, not starting it. fwupd.service is a disabled or a static unit not running, not starting it. Setting up systemd (245.4-4ubuntu3.24) ... Setting up netplan.io (0.104-0ubuntu2~20.04.6) ... Setting up systemd-timesyncd (245.4-4ubuntu3.24) ... Setting up python3-update-manager (1:20.04.10.23) ... Setting up systemd-sysv (245.4-4ubuntu3.24) ... Setting up cloud-init (24.4.1-0ubuntu0~20.04.3) ... Configuration file '/etc/cloud/cloud.cfg' ==> Modified (by you or by a script) since installation. ==> Package distributor has shipped an updated version. What would you like to do about it ? Your options are: Y or I : install the package maintainer's version N or O : keep your currently-installed version D : show the differences between the versions Z : start a shell to examine the situation The default action is to keep your current version. *** cloud.cfg (Y/I/N/O/D/Z) [default=N] ?
最新发布
12-03
你遇到的问题是: > **`E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.`** 并且在安装 `pigz` 时失败,后续虽然提示“已启用多线程加速”,但实际上 `pigz` 并未成功安装。 此外,在 `cloud-init` 配置过程中卡住,等待用户输入: ``` *** cloud.cfg (Y/I/N/O/D/Z) [default=N] ? ``` 这导致整个脚本**阻塞**,无法继续自动执行。 --- ## ✅ 根本原因分析 ### 1. `dpkg` 被中断 - 系统之前有未完成的 `apt` 或 `dpkg` 操作(如断电、强制终止安装)。 - 导致 `/var/lib/dpkg/lock` 存在或状态不一致。 - **即使你的脚本调用了 `ensure_apt_unlocked`,但没有处理 `dpkg --configure -a` 这个关键步骤。** ### 2. `cloud-init` 安装时交互式提示 - `cloud-init` 在更新时检测到配置文件被修改,会**暂停并等待人工选择**(Y/N)。 - **bash 脚本无法自动回应这种交互式问题,必须提前规避。** --- ## ✅ 解决方案:让脚本完全自动化 + 自动修复中断状态 我们需做三件事: 1. **非交互模式运行 APT** 2. **自动跳过或接受默认配置文件更新** 3. **在执行任何 apt 命令前,强制完成中断的 dpkg 配置** --- ## ✅ 修改建议:添加【全自动模式】预处理函数 将以下函数插入到脚本开头附近(例如在 `log()` 函数之后): ```bash # ------------------------------ # 🛠️ 强制进入非交互模式 & 自动修复 dpkg 中断 # ------------------------------ auto_fix_and_noninteractive() { log "🔧 设置非交互模式并修复中断的 dpkg..." # 设置环境变量,禁止交互 export DEBIAN_FRONTEND=noninteractive export DEBCONF_NONINTERACTIVE_SEEN=true export LC_ALL=C export LANG=C # 强制完成中断的配置 log "⚙️ 正在运行 dpkg --configure -a 以修复中断状态..." sudo dpkg --configure -a --force-confdef --force-confold >> "$LOG_FILE" 2>&1 # 清除可能残留的锁(再次确保) sudo killall -q apt apt-get dpkg || true sleep 2 sudo rm -f /var/lib/dpkg/lock* sudo rm -f /var/cache/apt/archives/lock log "✅ 系统已进入全自动非交互模式" } ``` --- ## ✅ 替换或增强 `ensure_apt_unlocked` 把原来的 `ensure_apt_unlocked()` 替换为更强大的版本: ```bash ensure_apt_unlocked() { log "🔧 正在清理 APT 锁文件和占用进程..." sudo killall -q apt apt-get dpkg || true sleep 2 sudo rm -f /var/lib/dpkg/lock /var/lib/dpkg/lock-frontend sudo rm -f /var/cache/apt/archives/lock sudo rm -rf /var/lib/dpkg/updates/* >/dev/null 2>&1 || true # 关键:自动完成未完成的配置,并自动保留旧配置或使用默认 sudo dpkg --configure -a --force-confdef --force-confold >>"$LOG_FILE" 2>&1 sudo apt clean log "✅ APT 锁已清除且中断状态已修复" } ``` --- ## ✅ 在主流程最开始调用非交互模式 在密码验证通过后,立即调用: ```bash # 0. 启用全自动非交互模式 auto_fix_and_noninteractive # 1. 优化资源使用 optimize_resources # 2. 系统检查 if ! check_compatibility; then ... ``` --- ## ✅ 对所有 `apt` 和 `dpkg` 命令添加非交互参数 修改所有类似: ```bash sudo apt-get install -y xxx ``` 为: ```bash sudo apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" xxx ``` ### ✅ 示例:修改 `install_p7zip` 函数 ```bash install_p7zip() { run_step_with_timeout "📦 安装 p7zip-full(用于高速解压)" \ "sudo apt-get install -y -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' p7zip-full" \ "ensure_apt_unlocked && sudo apt-get update && sudo apt-get install -y -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' p7zip-full" \ 300 } ``` > ✅ `--force-confdef`: 如果配置文件无本地修改,自动使用新版本 > ✅ `--force-confold`: 有本地修改时,保留旧版本(避免弹出 Y/N) --- ## ✅ 最终效果 经过上述修改后,脚本将: | 问题 | 是否解决 | 方法 | |------|----------|------| | `dpkg was interrupted` | ✅ | `dpkg --configure -a --force-conf*` | | `cloud.cfg` 卡住 | ✅ | 全局设置 `DEBIAN_FRONTEND=noninteractive` + `--force-confold` | | 无法自动安装 pigz | ✅ | 修复 dpkg 后可正常安装 | | 脚本被阻塞 | ✅ | 不再等待人工输入 | --- ## ✅ 推荐最终补丁顺序(整合进原脚本) 1. 添加函数 `auto_fix_and_noninteractive` 2. 增强 `ensure_apt_unlocked` 加上 `--force-conf*` 3. 设置全局环境变量: ```bash export DEBIAN_FRONTEND=noninteractive ``` 4. 所有 `apt-get install` 命令加上: ```bash -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值