GRUB配置文件解析与启动流程详解
首先,重要提示
grub.cfg 文件通常不是直接手动编辑的!
它是由 grub-mkconfig 工具根据 /etc/default/grub 配置文件和 /etc/grub.d/ 目录下的脚本自动生成的。手动修改会在下次更新 GRUB 时被覆盖。
GRUB 配置代码详细解析
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#### 第一部分:全局设置 ###
set timeout=10
set default="0"
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-abcdef12-3456-7890-abcd-ef1234567890' {recordfailload_videogfxmode $linux_gfx_modeinsmod gzioinsmod part_gptinsmod ext2set root='hd0,gpt2'if [ x$feature_platform_search_hint = xy ]; thensearch --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 abcdef12-3456-7890-abcd-ef1234567890elsesearch --no-floppy --fs-uuid --set=root abcdef12-3456-7890-abcd-ef1234567890filinux /boot/vmlinuz-5.15.0-91-generic root=UUID=abcdef12-3456-7890-abcd-ef1234567890 ro quiet splash $vt_handoffinitrd /boot/initrd.img-5.15.0-91-generic
}menuentry 'Ubuntu, with Linux 5.15.0-91-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-91-generic-recovery-abcdef12-3456-7890-abcd-ef1234567890' {recordfailload_videoinsmod gzioinsmod part_gptinsmod ext2set root='hd0,gpt2'if [ x$feature_platform_search_hint = xy ]; thensearch --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 abcdef12-3456-7890-abcd-ef1234567890elsesearch --no-floppy --fs-uuid --set=root abcdef12-3456-7890-abcd-ef1234567890fiecho 'Loading Linux 5.15.0-91-generic ...'linux /boot/vmlinuz-5.15.0-91-generic root=UUID=abcdef12-3456-7890-abcd-ef1234567890 ro recovery nomodesetecho 'Loading initial ramdisk ...'initrd /boot/initrd.img-5.15.0-91-generic
}### 第二部分:链加载其他操作系统(例如 Windows)###
menuentry 'Windows Boot Manager (on /dev/nvme0n1p1)' --class windows --class os $menuentry_id_option 'osprober-efi-1234-5678' {insmod part_gptinsmod fatinsmod search_fs_uuidinsmod chainsearch --fs-uuid --set=root 1234-5678chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}### 第三部分:内存测试工具等 ###
menuentry 'Memory test (memtest86+.efi)' {insmod part_gptinsmod fatsearch --no-floppy --fs-uuid --set=root 1234-5678chainloader /EFI/ubuntu/memtest86+.efi
}
menuentry 'Memory test (memtest86+.efi, serial console)' {insmod part_gptinsmod fatsearch --no-floppy --fs-uuid --set=root 1234-5678chainloader /EFI/ubuntu/memtest86+.efi console=ttyS0
}### 第四部分:UEFI 固件设置 ###
menuentry 'System Setup' {fwsetup
}
逐行详细解析
第一部分:全局设置
set timeout=10- 作用:设置 GRUB 启动菜单的等待时间(单位:秒)。如果在 10 秒内用户没有做出选择,将自动启动默认项。
set default="0"- 作用:设置默认启动的菜单项。
"0"表示第一个菜单项(Ubuntu),"1"表示第二个,以此类推。
- 作用:设置默认启动的菜单项。
第一个 menuentry:正常启动 Ubuntu
一个 menuentry 定义了一个启动选项。
-
menuentry 'Ubuntu' ... { ... }- 作用:定义一个名为 “Ubuntu” 的启动菜单项。后面的
--class用于主题的样式定义。
- 作用:定义一个名为 “Ubuntu” 的启动菜单项。后面的
-
recordfail- 作用:记录上一次启动是否失败。如果失败,下次启动时会自动停在 GRUB 菜单。
-
load_video和gfxmode $linux_gfx_mode- 作用:加载视频驱动并设置图形模式,为显示更高分辨率的启动画面(如 Plymouth 的 splash 屏幕)做准备。
-
insmod gzio,insmod part_gpt,insmod ext2- 作用:加载内核模块。这是 GRUB 功能扩展的方式。
gzio:提供对 gzip 压缩文件的解压支持(内核和 initrd 通常是压缩的)。part_gpt:提供对 GPT 分区表的识别能力。ext2:提供读取 ext2/3/4 文件系统的能力(因为/boot分区通常是 ext4)。
-
set root='hd0,gpt2'和search ...命令- 作用:定位根设备/根文件系统。这是最关键的一步。
set root='hd0,gpt2':这是一个提示。意思是“我猜根文件系统在第一个硬盘(hd0)的第二个 GPT 分区(gpt2)上”。search --fs-uuid --set=root ...:这是一个保险/精确查找。它会在所有存储设备上搜索具有指定 UUID(abcdef12...)的文件系统,并将root变量设置为找到的设备。UUID 是定位分区最可靠的方式,因为hd0这种标识可能会因硬盘插槽改变而变化。
-
linux /boot/vmlinuz-... root=UUID=... ro quiet splash $vt_handoff- 作用:加载 Linux 内核。这是启动过程的实质一步。
/boot/vmlinuz-...:指定内核镜像文件在root设备上的路径。root=UUID=abcdef12...:告诉内核,哪个分区是系统的根文件系统。内核启动后会将这个分区挂载为/。ro:以只读方式挂载根文件系统。这是为了在启动初期进行文件系统检查。quiet splash:控制内核的启动输出。quiet减少输出,splash显示图形启动画面。$vt_handoff:一个与虚拟终端(VT)切换相关的参数。
-
initrd /boot/initrd.img-...- 作用:加载初始化内存盘。
initrd是一个临时的根文件系统,它在内核启动前被加载到内存中。它包含了内核在启动早期阶段所必需的一些驱动和工具(例如,用于解密 LUKS 加密分区的驱动,或者用于识别软 RAID 的驱动),以便内核能够最终挂载并切换到真正的根文件系统(/)。
第二个 menuentry:恢复模式
这个条目与正常模式大部分相同,关键区别在于 linux 命令的参数:
recovery nomodeset- 作用:启动到单用户恢复模式。
recovery:告诉系统启动到 runlevel 1(单用户模式),不启动图形界面和大多数服务,直接以 root 权限进入一个救援 shell。nomodeset:告诉内核不要在启动初期接管显卡的模式设置,这对于某些显卡驱动有问题的机器非常有用,可以保证你能进入系统进行修复。
第二部分:链加载 Windows
insmod fat和insmod chain- 作用:加载 FAT 文件系统支持(因为 ESP 是 FAT32)和链加载模块。
search --fs-uuid --set=root 1234-5678- 作用:找到 Windows 的 EFI 系统分区(通过其 UUID)。
chainloader /EFI/Microsoft/Boot/bootmgfw.efi- 作用:链加载。这是启动不基于 GRUB 的操作系统(如 Windows)的标准方法。
- GRUB 不直接加载 Windows 内核。它只是简单地找到并启动 Windows 自己的引导加载程序
bootmgfw.efi,然后将控制权完全交给它。就像接力赛跑一样,GRUB 把接力棒(控制权)传给了 Windows Boot Manager。
第三和第四部分:工具和固件设置
- 内存测试:链加载
memtest86+.efi这个专门的内存测试工具。 - System Setup:
fwsetup- 作用:这是一个 UEFI 特有的命令。它会直接调用 UEFI 固件的接口,重启电脑并进入 UEFI/BIOS 设置界面。非常方便。
总结
这个 grub.cfg 文件完美地展示了 GRUB 作为 “引导管理器” 和 “引导加载器” 的双重角色:
- 引导管理器:通过
menuentry提供菜单,让用户选择要启动的操作系统或工具。 - 引导加载器:对于 Linux,它负责加载内核 (
linux) 和 initrd (initrd);对于其他系统,它通过chainloader移交控制权。
理解这个文件,你就理解了从 GRUB 菜单到 Linux 内核被加载起来的完整链条。当系统启动出现问题时(例如,内核文件丢失或 root 分区指定错误),你就可以通过 GRUB 命令行手动输入这些命令来进行紧急修复。
