Bootloaders: How Linux is Loaded
There are many bootloaders available, but this chapter focuses on the two most widely used: GRUB 2 and systemd-boot.
GRUB 2 - The Most Widely Used
GRUB 2 is the most popular bootloader for Linux, offering extensive functionality. It supports themes, multiboot setups, and various other features that make it highly versatile. For Windows/Linux multiboot systems, GRUB 2 is my recommended choice.
- GRUB Manual Table of Contents: https://links.thelinuxbook.com/grubtoc
Configuration
Before installing or updating GRUB, you need to create configuration files. One of the best resources for understanding GRUB is the https://wiki.gentoo.org/wiki/GRUB2.
Files Used for Configuration
File Location | Editable | Description |
---|---|---|
/boot/grub/grub.cfg | No | Generated by grub-mkconfig and will be overwritten. Do not edit directly. |
/etc/grub.d/* | Yes | Contains scripts processed in numerical order (e.g., 10-boot , 11-os ). |
/etc/default/grub | Yes | Primary configuration file for GRUB edits. Start here for modifications. |
Editing GRUB Configuration in /etc/default/grub
Below are common configuration entries in /etc/default/grub
:
GRUB_DEFAULT
: Specifies the default boot entry. Typically set to0
for the first entry. Use1
for the second entry,2
for the third, and so on.GRUB_TIMEOUT_STYLE
: Determines whether to display a menu (MENU
) or hide it (HIDDEN
). If hidden, theGRUB_HIDDEN_TIMEOUT
variable controls the splash screen display.GRUB_TIMEOUT
: Sets the menu display duration. If set to-1
, the menu will never timeout and will wait for user input.GRUB_CMDLINE_LINUX_DEFAULT
: Adds kernel options. Common examples:quiet splash
: Hides verbose text output during boot.nomodeset
: Bypasses modesetting, useful for booting NVIDIA systems to install drivers.single
: Boots into single-user mode, useful for system modifications or resetting user passwords.- Variable declarations: Add system-specific variables, such as
keymap
orvfio
for PCI passthrough.
GRUB_DISABLE_RECOVERY
: Disables recovery entries. Setting this totrue
is recommended if recovery entries are rarely needed. You can edit GRUB directly during boot by pressinge
when the menu appears.
For a complete list of GRUB configuration variables, refer to the https://links.thelinuxbook.com/grubconfig.
Sample GRUB Configuration (/etc/default/grub
)
Here’s an example configuration with no menu:
GRUB_DEFAULT="0"
GRUB_TIMEOUT_STYLE="hidden"
GRUB_TIMEOUT="0"
GRUB_DISTRIBUTOR="`lsb_release -i -s 2> /dev/null || echo Debian`"
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""
GRUB_DISABLE_RECOVERY="true"
GRUB_DISABLE_LINUX_RECOVERY="true"
Make Configuration files
Make the configuration files with grub-mkconfig
or update-grub
on debian-based systems.
grub-mkconfig -o /boot/grub/grub.cfg
Manual page for grub-mkconfig: https://www.gnu.org/software/grub/manual/grub/html_node/Simple-configuration.html#Simple-configuration
OR
update-grub
Manual page for update-grub: https://manpages.debian.org/buster/grub-legacy/update-grub.8.en.html
Installation
Once your configuration files are created, you can install GRUB onto your hard disk for booting. First, determine whether the installation is EFI or Legacy BIOS.
To check if you are using EFI:
ls /sys/firmware/efi
If files are listed with the ls command you are on EFI
EFI Installation
Efi installations consist of two partitions where Legacy BIOS can have one. This makes it unique, but can also cause a multiple problems when using manual partitioning. Prior to running these commands you need to make sure the partition is mounted!
Next, we need to install EFI to this partition with this command
grub-install --efi-directory=/boot/efi
BIOS Legacy Installation
Typically in a BIOS legacy setup you have one main partition mounted to / and you run the following command to install GRUB. Note: DO NOT USE Legacy where you can use EFI!
grub-install
You can also do a variation of this if you have multiple drives or are dual booting. Find the drive you are installing it on using the command lsblk
OR blkid
as these give more information about your drive setup
Example usage when specifying a drive:
grub-install /dev/sda
For the full usage of grub-install here is the manual page: https://www.gnu.org/software/grub/manual/grub/html_node/Installing-GRUB-using-grub_002dinstall.html
GRUB Videos
These are the videos I've done on GRUB via YouTube:
- How to Repair Linux With Boot Failure: https://links.thelinuxbook.com/repairgrubbootfail
- Fix Multi-Boot Systems running GRUB and Windows: https://links.thelinuxbook.com/fixmultiboot
- Using SuperGRUBDisk to repair GRUB: https://links.thelinuxbook.com/supergrub
- Custom Bootloader Themes Project: https://links.thelinuxbook.com/bootloaderthemes
- Using GRUB Rescue: https://links.thelinuxbook.com/grubrescue
Systemd Boot - Minimal Built-In boot
Many distributions are now starting to ship with systemd boot instead of GRUB (PopOS! is one example) and is an interesting boot manager when you don't want a menu or extra packages to boot into your Linux.
Note: systemd boot has the capabilities of menus and dual boot, but setting this up is much more cumbersome and why I recommend using GRUB instead of doing this
Systemd-boot is also an EFI ONLY bootloader. So if you aren't booted in using UEFI, you can NOT install systemd-boot. Verify that you are in efi mode using ls /sys/firmware/efi
Systemd has a dedicated site for documentation. Check it out @ https://systemd.io/ and look at the Booting Section for systemd-boot. Another good source for systemd-boot information is the Arch Wiki @ https://wiki.archlinux.org/title/systemd-boot
Configuraiton
Here is the directory breakdown for systemd-boot
- Main Directory: /boot/efi (Sometimes this can be /boot in rare instances and some systems it adds a EFI subdirectory, ex. /boot/efi/EFI)
- Configuration files: /boot/efi/loader (General configuration in loader.conf)
- OS Entries: /boot/efi/loader/entries (Each .conf is a boot entry)
General Configuration
The general configuration will be the loader.conf in the root of the /loader
directory. Here is the syntax:
- default – Default boot option and it can be a wildcard like arch-*.conf.
- timeout – menu timeout in seconds before the default entry is booted. If this is not set, the menu will only be shown on key press during boot.
- editor – yes is default, but this allows editing boot entries and can be used to reset password or get system level access. Set to no if security is a concern.
- auto-entries – shows automatic entries for Windows, EFI Shell, and Default Loader if set to 1 (default), 0 to hide;
- auto-firmware – shows entry for rebooting into UEFI firmware settings if set to 1 (default), 0 to hide;
- console-mode – changes UEFI console mode:
- 0 for 80x25
- 1 for 80x50
- 2 and above for non-standard modes provided by the device firmware
- auto picks a suitable mode automatically
- max for highest available mode
- keep (default) for the firmware selected mode.
boot/efi/loader/loader.conf example
default arch.conf
timeout 4
console-mode max
editor no
Adding Boot Entries
Every boot entry will be loaded from the /loader/entries
directory. Here is that syntax:
-
title – operating system name. Required.
-
version – kernel version. Optional.
-
machine-id – machine identifier from /etc/machine-id. Optional.
-
efi – EFI program to start. Either this parameter or linux is required.
-
options – space-separated command line options to pass to the EFI program or kernel parameters.
root="UUID=3255683f-53a2-4fdf-91cf" OR root="LABEL=OS Label"
is required for booting Linux. Here are common options:- rw = Read-Write needed for most installs
- root = UUID or Label used for identifying the root partition
- silent = do NOT display any console output
- splash = display OS Splash screen
- nomodeset = disable Kernel mode setting. Often needed if using NVIDIA cards and you do not have the drivers installed.
- S = run in single mode (often required for resetting root password or modifying root file system)
Q: How do you find the UUID? A: The command
sudo blkid
_Note: To identify specific drive labels I recommend
sudo lsblk
which gives a readout of all drives and where they are mounted. When used in conjunction withblkid
you will be able to tell what each drive is.
Check all kernel parameters @ https://links.thelinuxbook.com/kernelparameters
Example Arch Linux Entry /boot/efi/loader/entries/arch.conf
title Arch Linux
linux /vmlinuz-linux
initrd /intel-ucode.img
initrd /initramfs-linux.img
options root="LABEL=arch" rw
Example Windows 10 Entry /boot/efi/loader/entries/win10.conf
title Windows 10
efi /EFI/Microsoft/Boot/bootmgfw.efi
Automatic EFI Entry creation with the efibootmgr
package - You can check to see what efi options are installed to you with efibootmgr -v
.
efibootmgr --create --disk /dev/sda --part 1 --loader "\EFI\systemd\systemd-bootx64.efi" --label "Linux OS" --verbose
Note: This command is used if your Linux EFI system partition is located @ /dev/sda1 and the EFI file located @ /boot/efi/EFI/systemd/systemd-bootx64.efi
Installation
The default install is very easy once configuration is done. You simply type bootctl install
and you are done!
There are configurations with multiple boot partitions, but I do not recommend using systemd-boot for this.
In most instances this will auto update when boot options change, but you can force an update if you manually add more entries with bootctl update