Configuring GRUB incorrectly can render your system inoperable without an alternate boot device such as a CD-ROM or bootable USB drive. This section is not required to boot your LFS system. You may just want to modify your current boot loader, e.g. Grub-Legacy, GRUB2, or LILO.
Ensure that an emergency boot disk is ready to “rescue” the computer if
the computer becomes unusable (un-bootable). If you do not already
have a boot device, you can create one. In order for the procedure
below to work, you need to jump ahead to BLFS and install
xorriso from the
libisoburn package.
cd /tmp grub-mkrescue --output=grub-img.iso xorriso -as cdrecord -v dev=/dev/cdrw blank=as_needed grub-img.iso
GRUB uses its own naming structure for drives and partitions in the
form of (hdn,m), where
n is the hard drive number
and m is the partition
number. The hard drive numbers start from zero, but the partition
numbers start from one for normal partitions (from five for
extended partitions). Note that this is different from earlier
versions where both numbers started from zero. For example,
partition sda1 is (hd0,1) to GRUB and sdb3 is (hd1,3). In contrast to Linux, GRUB does
not consider CD-ROM drives to be hard drives. For example, if using
a CD on hdb and a second hard drive
on hdc, that second hard drive would
still be (hd1).
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 executes 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 doesn'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 executes it.
Many GRUB functions (including booting the Linux kernel) are not
included in the main GRUB image. Instead, they are stored in a file
system as GRUB modules. That file system is usually mounted in a
way that the GRUB modules can be accessed in /boot/grub on most Linux distributions. 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 (suggested size is 200 MB) partition just for boot
information. That way for each build, whether it's LFS or a
commercial distro, it can access the same boot files and can access
can any booted system. If you choose to do this, you will need to
mount the separate partition, move all files in the current
/boot directory (e.g. the Linux
kernel you just built in the previous section) to the new
partition. You will then need to unmount the partition and remount
it as /boot. If you do this, be sure
to update /etc/fstab.
Leaving /boot on the current LFS
partition will also work, but configuration for multiple systems is
more difficult.
Using the above information, determine the appropriate designator
for the root partition (or boot partition, if a separate one is
used). For the following example, it is assumed that the root (or
separate boot) partition is sda2.
The following sections go over how to boot with BIOS and UEFI. The GRUB installations for BIOS, 64-bit UEFI, and 32-bit UEFI can coexist and share the same configuration. The images and data live at different locations, 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, this is something you can do as a blanket strategy.
If you're doing UEFI boot but have created the Grub BIOS partition, it may be a good idea to run the command for BIOS in case UEFI booting does not work as expected.
If you only need to install GRUB for one boot method, you don't have to run commands for both methods. You can just run the command for the boot method you need.
For booting with BIOS, make sure the boot partition is mounted
(if using a separate one) and the BIOS Boot partition exists.
After that, 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 MBR.
grub-install /dev/sda --target=i386-pc
For booting with UEFI, make sure the boot partition is mounted
(if using a separate one) and the EFI System Partition is mounted
at /boot/efi. After that, install
the GRUB files into /boot/grub and
the main GRUB image at /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 that it's 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 that you have 64-bit UEFI firmware. If
you want to make the system bootable on 32-bit UEFI firmware, run
the command with x86_64-efi replaced
by i386-efi.
The --removable option
makes grub-install
use the standard location, 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 result in the location in a EFI
variable be recorded, but LFS lacks the BLFS package
efibootmgr, which is needed to manipulate EFI variables.
Generate /boot/grub/grub.cfg:
cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5
insmod part_gpt
insmod ext2
set root=(hd0,2)
# For UEFI
insmod efi_gop
insmod efi_uga
set gfxpayload=1024x768x32
menuentry "GNU/Linux, Linux 6.19.8-lfs-r13.0-27-systemd" {
linux /boot/vmlinuz-6.19.8-lfs-r13.0-27-systemd root=/dev/sda2 ro
}
EOF
The insmod commands
load the GRUB modules named
part_gpt, ext2, efi_gop, and
efi_uga. Despite the naming,
ext2 actually supports ext2, ext3, and
ext4 filesystems. On UEFI systems,
efi_gop and efi_uga are for video support. GOP, or Graphics
Output Protocol, is the modern approach. UGA, or UGA Draw Protocol,
is a legacy way of handling it. In a typical configuration, these
four modules are already embedded in the main GRUB image by
grub-install, and
those four insmod
commands will do nothing; However, they do no harm anyway, and they
may be needed with some rare configurations.
The set gfxpayload=1024x768x32 command sets the 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. This line does nothing when the system is booted via UEFI, but it does no harm anyway.
From GRUB's perspective, the kernel files are relative to the partition used. If you used a separate /boot partition, remove /boot from the above linux line. You will also need to change the set root line to point to the boot partition.
The GRUB designator for a partition may change if you added or
removed some disks (including removable disks like USB thumb
devices). The change may cause boot failure because grub.cfg refers to some “old” designators. If
you wish to avoid such a problem, you may use the UUID of a
partition and the UUID of a filesystem instead of a GRUB
designator to specify a device. Run lsblk -o
UUID,PARTUUID,PATH,MOUNTPOINT to show the UUIDs
of your filesystems (in the UUID
column) and partitions (in the PARTUUID column). Then replace set root=(hdx,y) with search --set=root --fs-uuid , and replace <UUID of the filesystem where the kernel
is installed>root=/dev/sda2 with root=PARTUUID=.
<UUID of
the partition where LFS is built>
Note that the UUID of a partition is completely different from
the UUID of the filesystem in this partition. Some online
resources may instruct you to use root=UUID= instead of <filesystem
UUID>root=PARTUUID=,
but doing so will require an initramfs, which is beyond the scope
of LFS.
<partition UUID>
The name of the device node for a partition in /dev may also change (this is less likely than
a GRUB designator change). You can also replace paths to device
nodes like /dev/sda1 with
PARTUUID=, in
<partition UUID>/etc/fstab, to avoid a potential
boot failure in case the device node name has changed.
GRUB is an extremely powerful program and it provides a tremendous number of options for booting from a wide variety of devices, operating systems, and partition types. There are also many options for customization such as graphical splash screens, playing sounds, mouse input, etc. The details of these options are beyond the scope of this introduction.
There is a command, grub-mkconfig, that can write a configuration file automatically. It uses a set of scripts in /etc/grub.d/ and will destroy any customizations that you make. These scripts are designed primarily for non-source distributions and are not recommended for LFS. If you install a commercial Linux distribution, there is a good chance that this program will be run. Be sure to back up your grub.cfg file.