From 96e231234ea04a94954417bd09600cabeaac30c5 Mon Sep 17 00:00:00 2001 From: Xi Ruoyao Date: Mon, 23 Dec 2024 15:10:44 +0800 Subject: [PATCH] grub: Add more details for GRUB booting procedure, provide how to grub-install for UEFI --- chapter10/grub.xml | 106 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 24 deletions(-) diff --git a/chapter10/grub.xml b/chapter10/grub.xml index 00be1d6c0..90dd4e908 100644 --- a/chapter10/grub.xml +++ b/chapter10/grub.xml @@ -73,10 +73,29 @@ xorriso -as cdrecord -v dev=/dev/cdrw blank=as_needed grub-img.iso Setting Up the Configuration - GRUB works by writing data to the first physical track of the - hard disk. This area is not part of any file system. The programs - there access GRUB modules in the boot partition. The default location - is /boot/grub/. + If booting the system via BIOS, GRUB works by writing a stub to + the first sector (named the Master Boot Record, or MBR) of the hard + disk. This area is not part of any file system. The BIOS loads and + execute the content of MBR, then the stub loads the main GRUB image from + the BIOS Boot Partition. The GRUB image is stored as raw data instead + of a file (there must be no file system on the BIOS Boot Partition), so + the stub don't need to support any file system and it can be made small + enough to fit in the MBR. + + If booting the system via UEFI, GRUB works by storing the main + GRUB image as a PE-COFF executable file at a standard location in the + EFI System Partition. The EFI firmware loads the file at the standard + location and execute it. + + Many GRUB functions (including booting a Linux kernel) are + not included in the main GRUB image. They are stored in some file + system as GRUB modules. This file system is usually mounted in a + way that the GRUB modules can be accessed at + /boot/grub on most Linux + distros. To avoid the chicken-and-egg problem, + grub-install embeds the modules necessary to + access this file system into the main GRUB image, so it can find + and load other modules. The location of the boot partition is a choice of the user that affects the configuration. One recommendation is to have a separate small @@ -100,25 +119,66 @@ xorriso -as cdrecord -v dev=/dev/cdrw blank=as_needed grub-img.isosda2. - Install the GRUB files into For booting via BIOS, first make sure the boot partition is mounted + (if using a separate one), and the BIOS Boot partition exists. + Then install the GRUB files into /boot/grub and set up the boot track: The following command will overwrite the current boot loader. Do not run the command if this is not desired, for example, if using a third party - boot manager to manage the Master Boot Record (MBR). + boot manager to manage the MBR. -grub-install /dev/sda +grub-install /dev/sda --target=i386-pc - - If the system has been booted using UEFI, - grub-install will try to install files for the - x86_64-efi target, but those files - have not been installed in . - If this is the case, add to the - command above. - + For booting via UEFI, first make sure the boot partition is mounted + (if using a separate one) and the EFI System Partition is mounted at + /boot/efi. Then install the GRUB + files into /boot/grub and the + main GRUB image into + /boot/efi/EFI/BOOT/BOOTX64.EFI: + + + The following command will overwrite the + /boot/efi/EFI/BOOT/BOOTX64.EFI file. + If it already exists, it's likely the entry of another boot loader + (for example the GRUB installation from the host distro, or the + Windows Boot Manager). Backup the file so it can be restored later, + or loaded as a secondary boot loader by the new GRUB installation + from LFS. + + + grub-install --target=x86_64-efi --removable + + + The command above assumes 64-bit UEFI firmware. + If you want to make the system bootable on a 32-bit UEFI firmware, + run the command again with x86_64-efi replaced by + i386-efi. + + + + The --removable option makes + grub-install use the standard location, + i.e. EFI/BOOT/BOOTX64.EFI (or + EFI/BOOT/BOOTIA32.EFI for + i386-efi), instead of the location GRUB prefers + (EFI/GRUB/GRUBX64.EFI or + EFI/GRUB/GRUBIA32.EFI). Using a non-standard + location would need to record the location in a EFI variable, but + LFS lacks the utility for manipulating EFI varaibles. + + + + The GRUB installation for BIOS, 64-bit UEFI, and 32-bit UEFI can + coexist and share the same configuration. So you can create both + the BIOS Boot Partition and the EFI System Partition, and install + GRUB for all the supported firmware types (i.e. running three + grub-install commands) if you are unsure about + your firmware type, or you plan to move the hard drive to a different + computer. + @@ -150,14 +210,11 @@ EOF ext2, ext3, and ext4 filesystems. - The grub-install command has embedded some modules - into the main GRUB image (installed into - the MBR or the GRUB BIOS partition) to access the other modules - (in /boot/grub/i386-pc) without - a chicken-or-egg issue, so with a typical configuration these two - modules are already embedded and those two insmod - commands will do nothing. But they do no harm anyway, and they may - be needed with some rare configurations. + As we've discussed, in a typical configuration these two + modules are already embedded in the main GRUB image by + grub-install and those two + insmod commands will do nothing. But they do no + harm anyway, and they may be needed with some rare configurations. @@ -165,7 +222,8 @@ EOF resolution and color depth of the VESA framebuffer to be passed to the kernel. It's necessary for the kernel SimpleDRM driver to use the VESA framebuffer. You can use a different resolution or color depth - value which better suits for your monitor. + value which better suits for your monitor. This line does nothing + when the system is booted via UEFI, but it does no harm anyway. From GRUB's perspective, the