Last Updated: 2023-06-01 01:50:44 Thursday
-- TOC --
只有在MBR分区的情况下,Grub才有这3个Stage!
grub支持各种不同的cpu架构,在安装grub的时候,会指定cpu架构,boot路径等信息,这些信息决定了以下这3个stage具体所使用的的代码和参数。
预备知识: 从MBR到GPT
MBR扇区的512字节中,前446个就是stage1的代码。这部分代码相当于BIOS与Grub做个交接,移交控制权。
但是,446个字节,实在是干不了什么事情,因此stage1的作用,就是加载stage1.5的代码到内存,然后,再次移交控制权。
stage1编译后的代码,叫boot.img,刚好512字节。
As mentioned in the BIOS POST section, at the end of POST, BIOS searches the attached disks for a boot record, usually located in the Master Boot Record (MBR), it loads the first one it finds into memory and then starts execution of the boot record. The bootstrap code, i.e., GRUB2 stage 1, is very small because it must fit into the first 512-byte sector on the hard drive along with the partition table. The total amount of space allocated for the actual bootstrap code in a classic generic MBR is 446 bytes. The 446 Byte file for stage 1 is named boot.img and does not contain the partition table which is added to the boot record separately.
Because the boot record must be so small, it is also not very smart and does not understand filesystem structures. Therefore the sole purpose of stage 1 is to locate and load stage 1.5. In order to accomplish this, stage 1.5 of GRUB must be located in the space between the boot record itself and the first partition on the drive. After loading GRUB stage 1.5 into RAM, stage 1 turns control over to stage 1.5.
stage1代码的位置固定在MBR扇区,stage1.5代码的位置也是固定的,否则就没法从stage1跳转到stage1.5。想象一下,如果位置不固定,446个字节就要加载读取文件系统的驱动代码,这个空间太小了,做不到。
stage1.5的代码起始位置,紧挨着MBR扇区,就是MBR的下一个扇区。据说这段空间大小有32K,或者62个扇区。。。有点搞不清楚,反正一般情况下,硬盘的第一块分区,距离MBR扇区,会预留1M空间出来。为啥预留1M?
stage1.5的代码就在这个空间内,这个空间足够加载文件系统的驱动,可以在直接读取文件后,就向stage2移交控制权。
stage1.5编译后的代码,叫core.img。
As mentioned above, stage 1.5 of GRUB must be located in the space between the boot record itself and the first partition on the disk drive. This space was left unused historically for technical reasons. The first partition on the hard drive begins at sector 63 and with the MBR in sector 0, that leaves 62 512-byte sectors—31,744 bytes—in which to store the core.img file which is stage 1.5 of GRUB. The core.img file is 25,389 Bytes so there is plenty of space available between the MBR and the first disk partition in which to store it.
Because of the larger amount of code that can be accommodated for stage 1.5, it can have enough code to contain a few common filesystem drivers, such as the standard EXT and other Linux filesystems, FAT, and NTFS. The GRUB2 core.img is much more complex and capable than the older GRUB1 stage 1.5. This means that stage 2 of GRUB2 can be located on a standard EXT filesystem but it cannot be located on a logical volume. So the standard location for the stage 2 files is in the /boot filesystem, specifically
/boot/grub2
.Note that the /boot directory must be located on a filesystem that is supported by GRUB. Not all filesystems are. The function of stage 1.5 is to begin execution with the filesystem drivers necessary to locate the stage 2 files in the /boot filesystem and load the needed drivers.
这部分才是grub的最主要的功能体现,我们开机看到的启动界面,选择OS,Grub shell,就是这部分的功能。stage2的主要作用,就是启动Kernel。
All of the files for GRUB stage 2 are located in the /boot/grub2 directory and several subdirectories. GRUB2 does not have an image file like stages 1 and 1.5. Instead, it consists mostly of runtime kernel modules that are loaded as needed from the /boot/grub2/i386-pc directory.
The function of GRUB2 stage 2 is to locate and load a Linux kernel into RAM and turn control of the computer over to the kernel. The kernel and its associated files are located in the /boot directory. The kernel files are identifiable as they are all named starting with
vmlinuz
. You can list the contents of the /boot directory to see the currently installed kernels on your system.GRUB2, like GRUB1, supports booting from one of a selection of Linux kernels. The Red Hat package manager, DNF, supports keeping multiple versions of the kernel so that if a problem occurs with the newest one, an older version of the kernel can be booted. By default, GRUB provides a pre-boot menu of the installed kernels, including a rescue option and, if configured, a recovery option.
Stage 2 of GRUB2 loads the selected kernel into memory and turns control of the computer over to the kernel.
本文链接:https://cs.pynote.net/sf/grub/202110205/
-- EOF --
-- MORE --