5.4 Boot Loaders
At the start of the boot process, before the kernel and init start, a boot loader program starts the kernel. The boot loader’s job sounds simple: it loads the kernel into memory from somewhere on a disk and then starts the kernel with a set of kernel parameters. However, this job is more complicated than it appears. To understand why, consider the questions that the boot loader must answer:
- Where is the kernel?
- What kernel parameters should be passed to the kernel when it starts?
The answers are (typically) that the kernel and its parameters are usually somewhere on the root filesystem. It may sound like the kernel parameters should be easy to find, but remember that the kernel itself is not yet running, and it’s the kernel that usually traverses a filesystem to find the necessary files. Worse, the kernel device drivers normally used to access the disk are also unavailable. Think of this as a kind of “chicken or egg” problem. It can get even more complicated than this, but for now, let’s see how a boot loader overcomes the obstacles of the drivers and the filesystem.
A boot loader does need a driver to access the disk, but it’s not the same one that the kernel uses. On PCs, boot loaders use the traditional Basic Input/Output System (BIOS) or the newer Unified Extensible Firmware Interface (UEFI) to access disks. (Extensible Firmware Interface, or EFI, and UEFI will be discussed in more detail in Section 5.8.2.) Contemporary disk hardware includes p. 122 firmware allowing the BIOS or UEFI to access attached storage hardware via Logical Block Addressing (LBA). LBA is a universal, simple way to access data from any disk, but its performance is poor. This isn’t a problem, though, because boot loaders are often the only programs that must use this mode for disk access; after starting, the kernel has access to its own high-performance drivers.
To determine if your system uses a BIOS or UEFI, run efibootmgr. If you get a list of boot targets, your system has UEFI. If instead you’re told that EFI variables aren’t supported, your system uses a BIOS. Alternatively, you can check to see that /sys/firmware/efi exists; if so, your system uses UEFI.
Once access to the disk’s raw data has been resolved, the boot loader must do the work of locating the desired data on the filesystem. Most common boot loaders can read partition tables and have built-in support for read-only access to filesystems. Thus, they can find and read the files that they need to get the kernel into memory. This capability makes it far easier to dynamically configure and enhance the boot loader. Linux boot loaders have not always had this capability; without it, configuring the boot loader was more difficult.
In general, there’s been a pattern of the kernel adding new features (especially in storage technology), followed by boot loaders adding separate, simplified versions of those features to compensate.
5.4.1 Boot Loader Tasks
A Linux boot loader’s core functionality includes the ability to do the following:
- Select from multiple kernels.
- Switch between sets of kernel parameters.
- Allow the user to manually override and edit kernel image names and parameters (for example, to enter single-user mode).
- Provide support for booting other operating systems.
Boot loaders have become considerably more advanced since the inception of the Linux kernel, with features such as command-line history and menu systems, but a basic need has always been flexibility in kernel image and parameter selection. (One surprising phenomenon is that some needs have actually diminished. For example, because you can perform an emergency or recovery boot from a USB storage device, you rarely have to worry about manually entering kernel parameters or going into single-user mode.) Current boot loaders offer more power than ever, which can be particularly handy if you’re building custom kernels or just want to tweak parameters.
5.4.2 Boot Loader Overview
Here are the main boot loaders that you may encounter:
GRUB A near-universal standard on Linux systems, with BIOS/MBR and UEFI versions.
LILO One of the first Linux boot loaders. ELILO is a UEFI version.
SYSLINUX Can be configured to run from many different kinds of filesystems.
LOADLIN Boots a kernel from MS-DOS.
systemd-boot A simple UEFI boot manager.
coreboot (formerly LinuxBIOS) A high-performance replacement for the PC BIOS that can include a kernel.
Linux Kernel EFISTUB A kernel plug-in for loading the kernel directly from a EFI/UEFI System Partition (ESP).
efilinux A UEFI boot loader intended to serve as a model and reference for other UEFI boot loaders.
This book deals almost exclusively with GRUB. The rationale behind using other boot loaders is that they’re simpler to configure than GRUB, they’re faster, or they provide some other special-purpose functionality.
You can learn a lot about a boot loader by getting to a boot prompt where you can enter a kernel name and parameters. To do this, you need to know how to get to a boot prompt or menu. Unfortunately, this can sometimes be difficult to figure out because Linux distributions heavily customize boot loader behavior and appearance. It’s usually impossible to tell just by watching the boot process which boot loader the distribution uses.
The next sections tell you how to get to a boot prompt in order to enter a kernel name and parameters. Once you’re comfortable with that, you’ll see how to configure and install a boot loader.