Linux 引导修复,重建 EFI 分区引导并安装 GRUB 引导程序

我不道啊的头像
我不道啊
标签:
linuxLinux 系统EFI 分区重建引导Linux 引导修复EFI 引导修复

在 Linux 系统中,如果遇到 EFI 分区损坏或丢失的情况,可能会导致系统无法启动。本文将通过一步一步的操作,教你如何手动重建 EFI 分区并安装 GRUB 引导程序。这个过程适用于 Ubuntu 等常见的 Linux 发行版。

比较熟悉 Linux 可以直接滑到文章结尾看全步骤即可

前提条件:

  1. 准备一个 linux 启动 U 盘,比如安装系统时的 live 镜像启动盘。
  2. 通过 live 启动 U 盘系统启动机器。
  3. 挂载目标系统分区、EFI 分区(若该分区损坏,可以使用 gparted 重建一个有 EFI 标识的分区并挂载)、chroot 运行时必要系统目录
  4. 通过 chroot 切换到目标系统
  5. 使用 grub-install 重装或修复 EFI 分区
  6. 详细步骤按以下操作

操作步骤:

  1. 挂载分区

首先挂载你的主分区以及 EFI 分区。你需要确保能够访问系统文件及 EFI 引导分区。

复制
展开
sudo mount /dev/nvme1n1p1 /mnt
sudo mkdir -p /mnt/boot/efi
sudo mount /dev/nvme1n1p3 /mnt/boot/efi
  1. 挂载必需的系统目录

挂载 /dev, /proc, /sys 等目录,以便你能够使用一些系统工具如 chroot

复制
展开
sudo mount --bind /dev /mnt/dev
sudo mount --bind /dev/pts /mnt/dev/pts
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
sudo mount --bind /run /mnt/run
  1. 切换到 chroot 环境

使用 chroot 命令切换到已挂载的根文件系统环境

复制
展开
sudo chroot .
  1. 安装 GRUB 引导程序

chroot 环境中,执行 grub-install 命令来安装 GRUB 引导程序。

复制
展开
grub-install /boot/efi/
  1. 更新 GRUB 配置文件

使用 update-grub 命令更新 GRUB 配置文件,确保引导项正确添加。

复制
展开
update-grub

你会看到以下输出:

复制
展开
Sourcing file `/etc/default/grub`
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.14.0-29-generic
Found initrd image: /boot/initrd.img-6.14.0-29-generic
Found memtest86+ 64bit EFI image: /boot/memtest86+x64.efi
Warning: os-prober will be executed to detect other bootable partitions.
Its output will be used to detect bootable binaries on them and create new boot entries.
Adding boot menu entry for UEFI Firmware Settings ...
done
  1. 完成操作并重启

完成后,退出 chroot 环境并重新启动系统:

复制
展开
exit
sudo reboot

详细完整过程

步骤大致是将目标系统(已经无法正常引导启动)挂载到通过 U 盘启动的 live 系统,临时挂载目标系统分区、引导分区到 live 系统,通过 chroot 切换到目标系统,利用 grub 重新生成引导文件来修复系统

复制
展开
# 挂载目标系统分区至 live 系统的 /mnt
ubuntu@ubuntu:~$ sudo mount /dev/nvme1n1p1 /mnt
ubuntu@ubuntu:~$ cd /mnt/
ubuntu@ubuntu:/mnt$ 
# 创建 efi 分区临时挂载目录
ubuntu@ubuntu:/mnt$ sudo mkdir -p /mnt/boot/efi
# 挂载 efi 分区(待修复引导分区)到创建好的临时目录
ubuntu@ubuntu:/mnt$ sudo mount /dev/nvme1n1p3 /mnt/boot/efi
# bind 都是方便 chroot 运行必要操作
ubuntu@ubuntu:/mnt$ sudo mount --bind /dev /mnt/dev
ubuntu@ubuntu:/mnt$ sudo mount --bind /dev/pts /mnt/dev/pts
ubuntu@ubuntu:/mnt$ sudo mount --bind /proc /mnt/proc
ubuntu@ubuntu:/mnt$ sudo mount --bind /sys /mnt/sys
ubuntu@ubuntu:/mnt$ sudo mount --bind /run /mnt/run
# chroot 到目标系统
ubuntu@ubuntu:/mnt$ sudo chroot .
# 重新安装 grub
root@ubuntu:/# grub-install /boot/efi/
Installing for x86_64-efi platform.
grub-install: warning: EFI variables cannot be set on this system.
grub-install: warning: You will have to complete the GRUB setup manually.
Installation finished. No error reported.
# 更新 grub 生成引导信息
root@ubuntu:/# update-grub
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-6.14.0-29-generic
Found initrd image: /boot/initrd.img-6.14.0-29-generic
Found memtest86+ 64bit EFI image: /boot/memtest86+x64.efi
Warning: os-prober will be executed to detect other bootable partitions.
Its output will be used to detect bootable binaries on them and create new boot entries.\
Adding boot menu entry for UEFI Firmware Settings ...
done

作者:https://blog.xn--rpv331d.com/我不道啊

链接:https://blog.xn--rpv331d.com/我不道啊/blog/107

转载请保留文章出处...

No data
No data