Ora

How Does GRUB Bootloader Work?

Published in Bootloader 5 mins read

GRUB (GRand Unified Bootloader) is a powerful and flexible bootloader that manages the initial startup process of most Linux-based systems, enabling users to choose which operating system or kernel to load. It acts as an intermediary between your computer's firmware (BIOS or UEFI) and the operating system, taking control after the firmware initializes hardware and before the operating system kernel starts.

Understanding the Role of GRUB

When you power on your computer, the firmware performs a Power-On Self-Test (POST) and then searches for a bootable device. Once a boot device is identified, the firmware loads the bootloader, which is typically GRUB, into memory. GRUB's primary job is to locate the operating system kernel, load it into RAM, and then pass control to it, allowing the operating system to fully initialize.

GRUB's design is modular and consists of several stages, each performing a specific part of the boot process.

The Multi-Stage Boot Process of GRUB

GRUB operates in a series of stages, each building upon the previous one to gradually load the operating system.

1. Stage 1 (boot.img)

  • Location: Resides in the Master Boot Record (MBR) of a hard drive or the boot sector of a partition.
  • Function: Due to the limited space in the MBR (512 bytes), Stage 1's primary task is simply to point to and load the next stage of GRUB. It knows just enough about the disk layout to find Stage 1.5.

2. Stage 1.5 (core.img)

  • Location: Typically resides immediately after the MBR, in the "gap" between the MBR and the first partition, or in a dedicated BIOS Boot Partition (for GPT partitioned disks).
  • Function: This stage is larger than Stage 1 and contains basic file system drivers (e.g., for FAT, ext2/3/4) and other modules. Its main purpose is to locate and load Stage 2 of GRUB from a known file system location, usually within the /boot/grub directory.

3. Stage 2 (normal.mod and other modules)

  • Location: Stored as files within the /boot/grub directory on the root filesystem partition.
  • Function: This is the most feature-rich stage. It loads all necessary modules, reads the GRUB configuration file (typically grub.cfg), and presents the boot menu to the user. From this stage, GRUB can:
    • Identify and access various file systems.
    • Parse the configuration file for available boot entries.
    • Load the specified operating system kernel and initial RAM disk (initramfs/initrd) into memory.
    • Pass boot parameters to the kernel.
    • Finally, transfer control to the loaded kernel, beginning the operating system's startup.

GRUB Configuration and User Interaction

GRUB is highly configurable, offering both a graphical menu and a powerful command-line interface.

The GRUB Menu Interface

GRUB implements a menu interface that presents users with a list of bootable operating systems or kernel versions. These boot options are predefined in a configuration file. In older GRUB Legacy versions, this file was named menu.lst, while modern GRUB 2 systems use grub.cfg.

Key Features of the Menu Interface:

  • Boot Entries: Each entry in the menu corresponds to a specific operating system or kernel, along with its associated boot parameters.
  • Default Selection: A default operating system is usually pre-selected, and GRUB will automatically boot into it after a short timeout if no user input is received.
  • Editor: Users can typically press a key (e.g., e for edit) to temporarily modify the boot parameters for a selected entry before booting. This is useful for troubleshooting, such as adding nomodeset to resolve display issues.

The GRUB Command-Line Interface (CLI)

Beyond the menu, GRUB also has a robust command-line interface that is accessible from the GUI menu interface (often by pressing c). This CLI is an invaluable tool for advanced users and system administrators to perform various boot functions, including modifying default boot behavior or recovering a system.

Common CLI Commands:

Command Description Example Usage
ls Lists devices and partitions. ls (hd0,gpt1)/
set root= Sets the root device for the current boot session. set root=(hd0,gpt2)
linux Loads the Linux kernel from a specified path. linux /boot/vmlinuz-5.15.0-78-generic root=/dev/sda2 ro
initrd Loads the initial RAM disk (initramfs). initrd /boot/initrd.img-5.15.0-78-generic
boot Boots the loaded kernel and initramfs. boot
configfile Loads an alternative configuration file. configfile (hd0,gpt1)/grub/grub.cfg

Practical Insights with the CLI:

  • Recovery: If your grub.cfg file becomes corrupted or an OS update breaks GRUB, you can manually issue commands from the CLI to boot your system.
  • Testing: You can experiment with different kernel parameters without modifying the permanent configuration file.
  • Dual Boot Management: The CLI is essential for manually discovering and booting operating systems if GRUB fails to detect them automatically.

Configuring GRUB

The primary configuration file for GRUB 2 is /boot/grub/grub.cfg. However, this file should generally not be edited directly. Instead, it is automatically generated by the update-grub command (which executes grub-mkconfig) based on settings found in:

  • /etc/default/grub: Main settings file for GRUB behavior (e.g., default boot entry, timeout, visual theme).
  • /etc/grub.d/: Directory containing scripts that generate boot entries for various operating systems and kernels.

Example of /etc/default/grub entries:

GRUB_DEFAULT=0            # Boots the first entry in the menu by default
GRUB_TIMEOUT=10           # Waits 10 seconds before booting the default
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" # Default kernel parameters
GRUB_DISABLE_OS_PROBER=false # Enable detection of other OSes

After making changes to /etc/default/grub or adding custom boot entries in /etc/grub.d/, you must run sudo update-grub to regenerate /boot/grub/grub.cfg and apply the changes.

For more detailed information, refer to the GNU GRUB Manual or Ubuntu's GRUB2 documentation.