10.4. Using GRUB to Set Up the Boot Process

10.4.1. GRUB Naming Conventions

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 number starts from zero, but the partition number starts from one for normal partitions and 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).

10.4.2. Setting Up the Configuration

[Note]

Note

Ensure the boot partition and the EFI partition are mounted correctly before continuing. You can use findmnt command to verify.

We've already explained how GRUB works on our target machine (LoongArch) in Section 6.23, “Making the Temporary System Bootable”. Now, Install GRUB EFI executable and GRUB modules:

grub-mkimage --prefix '(,gpt2)/grub'                       \
             --output /boot/efi/EFI/BOOT/BOOTLOONGARCH.EFI \
             --format loongarch64-efi                      \
             ext2 part_gpt

rm -rf /boot/grub/loongarch64-efi

cp -av /usr/lib/grub/loongarch64-efi \
       /boot/grub/loongarch64-efi

10.4.3. Creating the GRUB Configuration File

Generate /boot/grub/grub.cfg:

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,2)

menuentry "GNU/Linux, Linux 5.15.0-rc5-lfs-cross-la-r11.0-228-systemd" {
        linux   /vmlinux-5.15.0-rc5-lfs-cross-la-r11.0-228-systemd root=/dev/sda3 ro
        initrd  /acpi-initrd
        boot
}
EOF

If you want to continue to use serial console for connecting to your LFS system, append console=ttyS0,115200 or something suitable for your serial device at the end of the linux command.

[Note]

Note

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.

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.

[Caution]

Caution

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.