Kahibaro
Discord Login Register

Understanding GRUB2

Role of GRUB2 in the Linux Boot Chain

GRUB2 (GRand Unified Bootloader, version 2) is the stage between your firmware/bootloader and the kernel. In a typical modern Linux system:

  1. Firmware (BIOS/UEFI) runs and finds the boot device.
  2. GRUB2 is loaded from disk (or from the EFI System Partition on UEFI).
  3. GRUB2 presents a menu, loads the kernel and initramfs, and passes control to the kernel with given parameters.

In this chapter we focus on how GRUB2 is organized, how it’s configured, and how you can interact with it and fix common issues.

GRUB2 Architecture and Components

GRUB2 is modular and has several parts; their exact layout depends on BIOS vs UEFI, but the concepts are similar.

Main Components

GRUB2 Configuration Files and Layout

While grub.cfg is GRUB’s main runtime configuration, it is generally generated from templates and scripts.

grub.cfg: The Generated Configuration

You treat grub.cfg as read-only and control it via higher-level configuration.

/etc/default/grub: Primary Tuning Point

This file is used by grub-mkconfig to build grub.cfg. Typical contents:

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=Ubuntu
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

Common keys:

To apply changes:

    sudo update-grub
  sudo grub2-mkconfig -o /boot/grub2/grub.cfg
  sudo grub2-mkconfig -o /boot/efi/EFI/<distro>/grub.cfg

/etc/grub.d/ Scripts

On many distributions, /etc/grub.d/ contains scripts that assemble the final grub.cfg. Examples:

These scripts output GRUB configuration syntax to stdout; grub-mkconfig runs them in order and concatenates the results.

For most users:

GRUB2 Language Basics

GRUB2’s configuration language is shell-like but simplified. Some core constructs:

set, terminal, and environment

  set default=0
  set timeout=5
  set root='hd0,msdos1'

if, else, fi (Basic Logic)

GRUB2 supports if statements:

if [ "$grub_platform" = "efi" ]; then
    set timeout=3
else
    set timeout=5
fi

Tests use GRUB’s built‑in [ ] command, not the shell’s; syntax is similar but not identical.

menuentry Blocks

Each boot option is defined by a menuentry block:

menuentry 'My Linux' --class gnu-linux --class gnu --class os {
    insmod gzio
    insmod part_gpt
    insmod ext2
    set root='hd0,gpt2'
    linux   /vmlinuz-6.1.0 root=UUID=1234-5678 ro quiet
    initrd  /initrd.img-6.1.0
}

Within the block:

Device and Partition Naming in GRUB2

GRUB2 uses its own naming scheme for devices and partitions.

Disk and Partition Syntax

Format:

Examples:

A full root specification often looks like:

set root='hd0,gpt2'

You can also refer by UUID or label using GRUB commands, but this is usually handled automatically when grub.cfg is generated.

Interacting with the GRUB2 Menu

When the system starts and GRUB loads, you typically see:

Key interactions (may vary slightly by distro):

These changes are temporary and lost on next boot unless saved via persistent configuration.

Temporarily Editing Boot Parameters (On-Demand Debugging)

Modifying parameters from the GRUB menu is a common way to debug boot problems without permanently changing configuration.

Editing a Menu Entry

  1. Select your kernel entry with the arrow keys.
  2. Press e to edit.
  3. You’ll see a small editor with lines like:
   linux   /boot/vmlinuz-... root=UUID=... ro quiet splash
   initrd  /boot/initrd.img-...
  1. Use arrow keys to navigate, Home/End to jump to line starts/ends (keys vary by system).
  2. Add or remove kernel parameters on the linux line, for example:
    • Remove quiet and splash to see verbose boot messages.
    • Add systemd.unit=rescue.target to boot into rescue mode.
    • Add nomodeset to work around graphics-driver issues.
  3. Press Ctrl+x or F10 to boot with the edited entry.

This does not modify grub.cfg; it only affects the current boot.

Kernel Parameters From GRUB2

GRUB is responsible for supplying the kernel’s command-line. It’s common to add or adjust parameters via /etc/default/grub.

Examples:

  GRUB_CMDLINE_LINUX_DEFAULT="verbose"
  GRUB_CMDLINE_LINUX_DEFAULT="quiet loglevel=7"

After editing, regenerate grub.cfg and reboot.

GRUB2 on BIOS vs UEFI Systems

While GRUB2 itself behaves similarly, installation and locations differ.

BIOS (Legacy) Setup

Installation example (conceptual):

sudo grub-install /dev/sda
sudo grub-mkconfig -o /boot/grub/grub.cfg

grub-install writes GRUB to the MBR and related sectors, not to a partition like /dev/sda1.

UEFI Setup

Installation example (conceptual):

sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi \
                  --bootloader-id=GRUB
sudo grub-mkconfig -o /boot/efi/EFI/GRUB/grub.cfg

Details vary by distro, but the pattern is:

Persistent Defaults: GRUB_DEFAULT and Saved Entries

GRUB2 can either boot a fixed default entry or remember the last used entry.

Fixed Default by Index or Name

In /etc/default/grub:

  GRUB_DEFAULT=0   # first entry in grub.cfg
  GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 6.1.0-26"

The title must match exactly (including spaces).

Using Saved Default

To always boot the last selected entry:

  1. Set in /etc/default/grub:
   GRUB_DEFAULT=saved
   GRUB_SAVEDEFAULT=true
  1. Regenerate grub.cfg.

Now:

Alternatively, from the OS:

<ENTRY> can be an index or title, similar to GRUB_DEFAULT.

GRUB Command Line and Manual Recovery

The GRUB command-line is useful to investigate or manually boot when the menu is broken.

Entering the GRUB Command Line

You’ll see a prompt like:

grub>

Useful commands include:

  grub> ls
  (hd0) (hd0,gpt1) (hd0,gpt2) (hd1) (hd1,gpt1)

Manually Booting a System

A typical manual boot sequence:

  1. Identify the root partition:
   grub> ls
   grub> ls (hd0,gpt2)/
  1. Set root and prefix:
   grub> set root=(hd0,gpt2)
   grub> set prefix=(hd0,gpt2)/boot/grub
  1. (Optional) Load normal mode and config:
   grub> insmod normal
   grub> normal

This often brings you back to a menu.

  1. Or load kernel and initrd manually, for example:
   grub> linux /boot/vmlinuz-6.1.0 root=UUID=1234-5678 ro
   grub> initrd /boot/initrd.img-6.1.0
   grub> boot

This doesn’t fix the configuration permanently but can rescue a boot so you can repair from the running system.

Common Adjustments and Tweaks

Changing the Menu Timeout and Visibility

In /etc/default/grub:

  GRUB_TIMEOUT_STYLE=hidden
  GRUB_TIMEOUT=5
  GRUB_TIMEOUT_STYLE=menu
  GRUB_TIMEOUT=10

Regenerate grub.cfg after editing.

Changing Resolution and Appearance

Basic options in /etc/default/grub:

GRUB_GFXMODE=1024x768
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_THEME=/boot/grub/themes/mytheme/theme.txt

After changes, regenerate grub.cfg.

GRUB2 and Encrypted/Complex Storage

GRUB2 supports:

Basic behavior:

The exact configuration is typically generated by the distro’s tools when you install with encryption, so you rarely need to edit this part directly, but it explains why GRUB may ask for a passphrase before the kernel even starts.

GRUB2 and Multiple Operating Systems

When multiple OSes are installed:

Common scenarios:

If you do not see other OSes:

Safety Considerations

Understanding GRUB2’s layout, configuration model, and interactive tools makes it far easier to customize the boot process and recover from boot-related problems.

Views: 22

Comments

Please login to add a comment.

Don't have an account? Register now!