What the boot loader does
Video 065 of 140 in Virtualization Foundations, in the unit “The boot sequence, end to end”. 3:56, with English and Hindi audio.
Once firmware has started a boot program, that program is the boot loader. On Linux it is usually GRUB. Firmware is too simple to read a Linux filesystem, but GRUB can read the disk where the kernel lives. It loads the kernel and the initramfs, then passes the kernel a line of options. This article explains why a second loader is needed, what GRUB does in order, and where to look on an Ubuntu machine.
What you will learn
- The boot loader is the program that firmware starts from the disk.
- GRUB reads its settings, then loads the kernel and the initramfs into memory.
- GRUB passes the kernel a command line, and one option names the root disk.
- GRUB reads its generated config at boot, so rebuild that config after you change the settings.
- Without a root option, the kernel does not know which disk holds the system.
Why a second loader is needed
Firmware is simple. It reads one small sector, or a small FAT partition, and it cannot make sense of a Linux filesystem. A filesystem is the way Linux keeps files on a disk.
The Linux kernel, however, is a file in the boot folder, and that file sits in a Linux filesystem. So the boot loader must understand the filesystem where the kernel lives, and load the kernel from there. On Linux, that boot loader is usually GRUB.
What GRUB does, in order
- Reads its settings: the GRUB config file lists the kernels it knows about, and the options to start each one with.
- Shows a menu: if the menu is shown, you can choose a kernel from it.
- Loads the kernel: a compressed file that unpacks itself when it starts.
- Loads the initramfs: a small starter filesystem with the drivers the kernel needs to reach the real disk.
- Passes the options: a command line that the kernel reads when it starts.
Once the kernel is running, it reads the root option and goes looking for that disk.
The kernel command line
The kernel command line is a line of words given to the kernel at start. Each word is an option that changes how the kernel starts.
One option names the root disk, the disk that holds the system's files. Without that option, the kernel does not know which disk holds the system.
Note: GRUB reads its generated config at boot. A change to the settings does nothing until you rebuild that config.
Seeing GRUB's work on an Ubuntu machine
On an Ubuntu machine, you can look at the files GRUB uses, and at the options the running kernel started with. The steps below run four commands in order. The first three only read. The last one rebuilds GRUB's config, so run it only after you edit the settings.
Try it yourself
You need a shell on an Ubuntu machine. The first three commands only read files. The last command rebuilds GRUB's config from the settings file, so it needs administrator rights.
List the boot folder. It holds the kernel files and the initramfs files for the installed versions.
ls /bootWhat you should see: The kernel files and the initramfs files for the installed versions.
Show the options that the running kernel started with. Look for the option that names the root disk.
cat /proc/cmdlineWhat you should see: The options the running kernel was started with.
Read the wait time and the default options from the GRUB settings file.
grep -E 'GRUB_(TIMEOUT|CMDLINE)' /etc/default/grubWhat you should see: The wait time and the default options, from the settings file.
After you edit the settings, rebuild GRUB's config so the change takes effect.
sudo update-grubWhat you should see: The GRUB config is rebuilt from the settings, so the next boot uses them.
Terms
- Boot loader
- The program that firmware starts from the disk. On Linux it is usually GRUB.
- GRUB
- The boot loader most often used on Linux. It reads the disk, loads the kernel and passes it options.
- Filesystem
- The way Linux keeps files on a disk, so that programs can find them by name.
- Kernel command line
- The list of options the boot loader gives the kernel when it starts, such as which disk is the root.
Check yourself
Why can firmware not load the Linux kernel itself?
Firmware reads only one small sector or a small FAT partition. It cannot understand the Linux filesystem where the kernel is stored.
What is the kernel command line?
The list of options the boot loader gives the kernel at start. One of them names the root disk.
Why does a change to GRUB's settings have no effect yet?
GRUB reads its generated config at boot, so the config must be rebuilt first.
Previous: What firmware does at power-on. Next: The kernel and the initramfs.