grub.cfg修改后,无法进入grub引导
解决
此时需要修改或重新生成grub的配置文件,但是此时无法进入系统,所以需要通过刻录有arch镜像的u盘进入系统修改文件。
准备材料
一个有刻录有archlinux镜像的u盘
进入镜像系统
挂载系统根目录
查看系统分区分配情况,找到根目录的分区
fdisk -l
挂载根目录分区到/mnt目录
mount /dev/sda3 /mnt
arch-chroot 进入根目录,挂载boot目录
arch-chroot /mnt
挂载boot目录,grub相关文件放在boot目录中
mount /dev/sda2 /boot
安装并更新grub
sudo grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=mygrub
执行以上操作后,会在生成efi-directory/bootloader-id目录,grubx64.efi就安装在该目录中
重新生成grub.cfg文件
sudo vim /etc/default/grub
# 删除导致grub失败的几行
GRUB_DISABLE_SUBMENU=y
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
grub-mkconfig -o /boot/grub/grub.cfg
扩展
boot的目录结构
.
├── EFI
│ └── mygrub
├── grub
│ ├── fonts
│ ├── grub.cfg
│ ├── grubenv
│ ├── locale
│ ├── themes
│ └── x86_64-efi
├── initramfs-linux-fallback.img
├── initramfs-linux.img
├── initramfs-linux-zen-fallback.img
├── initramfs-linux-zen.img
├── System.map
├── vmlinuz-linux
└── vmlinuz-linux-zen
以上文件/文件夹分类好如下
# EFI相关文件
EFI(folder)
grub(folder)
# Kernel Symbol Table
System.map(file)
# linux kernel files
initramfs-linux-fallback.img(file)
initramfs-linux.img(file)
vmlinuz-linux(file)
# linux-zen kernel files
initramfs-linux-zen-fallback.img(file)
initramfs-linux-zen.img(file)
vmlinuz-linux-zen(file)
grub-install
sudo grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=mygrub
- target:efi类型,可使用命令'uname -m'查看,因为我电脑是x86_64所以类型为x86_64-efi
- efi-directory:EFI系统分区根目录
- bootloader-id: grub名称
# 执行成功输出
Installing for x86_64-efi platform.
Installation finished. No error reported.
# 在--efi-directory目录生成如下文件
EFI
└── mygrub
└── grubx64.efi
grub
├── fonts
├── grubenv
├── locale
├── themes
└── x86_64-efi
grub-mkconfig
# 自动检测系统的启动分区,如果有安装os-prober,那么还会检测其他可启动的分区,比如windows启动分区,将检测到的信息保存到/boot/grub/grub.cfg
grub-mkconfig -o /boot/grub/grub.cfg
# 执行输出
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-linux-zen
Found initrd image: /boot/initramfs-linux-zen.img
Found fallback initrd image(s) in /boot: initramfs-linux-zen-fallback.img
Found linux image: /boot/vmlinuz-linux
Found initrd image: /boot/initramfs-linux.img
Found fallback initrd image(s) in /boot: initramfs-linux-fallback.img
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.
Found Windows Boot Manager on /dev/nvme0n1p1@/efi/Microsoft/Boot/bootmgfw.efi
Adding boot menu entry for UEFI Firmware Settings ...
done
ESP(EFI System Partition)是在/boot还是/boot/efi?
EFI分区的格式是fat32,在archlinux中是不关心EFI在/boot或者/boot/efi的
注意:如果grub-install的--efi-directory参数为/boot/efi,那么/boot/efi必须是一个分区根目录,而不是/boot中的一个目录,否则grub引导不生效。
为什么要挂载根目录
既然修改内容只涉及到了/boot目录,为什么还要挂载/目录?
因为/目录有linux的基本环境,比如ls,cd等基础命令,如果/mnt直接挂载boot目录,那么arch-chroot /mnt将会失败。
参考
本文使用 markdown.com.cn 排版