Modern GRUBing

Wiki.TerraBase.info
Jump to navigation Jump to search

Notes;

  • LVM on NVMe is the issue


Rocky Linux 9 does not use the /etc/default/grub and /boot/grub2/grub.cfg Files any longer. Instead the BLS (Boot Loader Specification) 'method' is used. If one reads the documentation from RedHat, it seems like thing got really messy (and confusing).

Quick D for Cloning, err, "Recreating" a drive from a BackUp (IE, the closest thing to Acronis that Linux seems to have)

  • Create a duplicate or functionally similar Partition layout to the original
    • CFDISK and "Logical Volume Commands"
    • Use Blivet from a Rocky 8 Live Boot Device

Rough D

  • Start from a 'Clean Slate': wipefs -a /dev/sdX (make dang sure the proper 'sdX' is chosen!)
  • Set the Partition Type (MBR or GPT): cfdisk /dev/sdX, (select DOS or GPT, save it (write) )
  • Create Partitions: cfdisk
  • LVM Stuff using Commands or BLIVET (sadly only Rocky Linux 8)
    • pvcreate /dev/sdXy
    • vgcreate VG.NVMe.ROOT /dev/sdXy
      • vgrename WhatEverOldName WhatEverNewName
    • lvcreate -L 64G -n LV.ROOT VG.NVMe.ROOT
  • Format Partitions
    • mkfs.vfat -F32 /dev/sdXy
    • mkfs.ext4 -v -L ROOT.EXT4 /dev/sdXy ( -v = Verbose, -L = Label )
    • mkswap -L SWAP /dev/sdaXy
  • MBR Stuff if desired
  • UEFI Stuff ("make" the grubx64.efi File) --- Best done from a UEFI system!
    • grub2-mkstandalone is a similar command
    • Make sure UEFI Stuff is installed (if cloning from an MBR / BIOS based system);
      • dnf install grub2-efi-x64 grub2-tools grub2-tools-efi grub2-tools-extra grub2-efi-x64-modules efibootmgr shim-x64
    • mkdir /mnt/efi
    • mount /dev/nvme0n1p1 /mnt/efi
    • grub2-install --target=x86_64-efi --efi-directory=/mnt/efi --bootloader-id=rocky --recheck --force --verbose --modules="part_gpt ext2 fat normal search search_fs_uuid search_fs_file configfile all_video gfxterm gzio" **--no-nvram** **--removable**
      • --force = Do it on a supposedly 'non-secure' system
      • --no-nvram = Do it even if a target drive is being prepared as a UEFI Booted system even if the current OS is an MBR / BIOS system
      • --removable is optional and installs the 'generic' BOOTX64.EFI File. )
      • --modules because apparently absolutely no modules are installed by default, even ext2!
        • ...and if you've got loads of space, then load up grubx64.efi with everything: --modules="$(ls /boot/grub2/x86_64-efi/ | sed 's/\.mod//g' | tr '\n' ' ')"
    • Command to show information on how a system was booted (and can boot): bootctl status
    • Command for EFIBootMgr: efibootmgr -v
  • GRUB Menu stuff
    • Edit: /etc/default/grub
    • Make the grub.cfg file: grub2-mkconfig -o /boot/grub2/grub.cfg
    • Seemingly useless command (as a text editor would work, no matter the 'threats' in the file): grub2-editenv
      • Wait, spoke too soon. If this error is encountered: grub2-editenv: error: environment block too small. when using kernel-install it indicates the grubenv file isn't 1024 bytes. Seems like the grub2-editenv tool pads the file with ###### symbols to make it exactly 1024 Bytes (and we know how important it is to have a file equal to exactly 1024 Bytes!)
    • grub2-probe: General information, simple
  • Kernel Stuff (initramfs, vmlinuz, bootloader entries, etc.): kernel-install
    • to 'redo' VMLinuz Stuff: dracut
  • Edit / Register the entry in the Hardwares UEFI Storage space
    • efibootmgr --create --disk /dev/nvme0n1 --part 1 --loader '\EFI\rocky\grubx64.efi' --label "Custom GRUB" --bootorder 0001
  • Edit the BLS (Boot Loader System) Entries, etc.
    • GRUBBY
  • Restore BOOT and ROOT File Systems
    • f
    • f
  • Modify Entries in;
    • /boot/loader/entries (wherever mounted)
    • /etc/fstab (wherever mounted)
  • ...and the tribulations continue as the below command won't work from an MBR / BIOS system or theoretically from any system that isn't the one being booted, run from etc.;
    • Entry for Hardware Firmware (AKA BIOS Settings): efibootmgr --create --disk /dev/sdb --part 1 --loader /EFI/rocky/grubx64.efi --label "Rocky Linux" --verbose
    • Work Around:

BLS (Boot Loader Specification)

Nope, not anymore (sort of): /etc/fstab

Nope, not anymore (sort of): grub2-mkconfig -o /tmp/boot/grub2/grub.cfg --root-directory=/tmp/LV.ROOT

Look in /boot/loader/entries and / or use GRUBBY

...see below for key information on /etc/default/grub and BLS

GRUB "Installation" for MBR / BIOS

Installs the Stage 1 Boot Code (program) in the MBR and Stage 1.5 Boot Code (program) in the "MBR Gap": grub2-install --target=i386-pc /dev/sdd

...and don't forget to set the Boot Partition active using CFDISK or something else

OR

Use DD to clone the Boot Partition

Sources

https://www.baeldung.com/linux/grub-menu-management

https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/ > https://uapi-group.org/specifications/specs/boot_loader_specification/

https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/managing_monitoring_and_updating_the_kernel/configuring-kernel-command-line-parameters_managing-monitoring-and-updating-the-kernel

ChatGPT's Opinion

GRUB "Installation" (What Does grub2-install Do?)

The grub2-install command is used to install the GRUB bootloader onto a disk or partition. It places the necessary GRUB boot code onto the target disk and copies GRUB modules required for booting.

How grub2-install Works

When executed, grub2-install performs the following actions:

1. Determines the Installation Target

  • If given a disk (e.g., /dev/sda), it installs GRUB to the MBR or GPT boot partition.
  • If given a partition (e.g., /dev/sda1), it may fail or install to the partition's boot sector (not recommended).
  • If on a BIOS system, it installs the i386-pc GRUB bootloader.
  • If on a UEFI system, it installs x86_64-efi and places EFI files in the EFI system partition.

2. Copies GRUB Core to the Correct Location

  • On BIOS/MBR systems, GRUB places its boot code in the MBR and additional code in the post-MBR gap (sectors after the MBR).
  • On GPT systems using BIOS (BIOS-GPT), it places GRUB in the BIOS Boot Partition.
  • On UEFI systems, it installs the grubx64.efi file into the EFI System Partition (ESP).

3. Selects the Correct GRUB Modules

  • grub2-install determines which GRUB modules are needed based on the filesystem, partition layout, and system firmware.
  • It installs these modules into /boot/grub2/ (BIOS) or /boot/efi/EFI/ (UEFI).

4. Writes GRUB Boot Code

  • BIOS (MBR) Installation:
    • The Stage 1 boot code is written into the MBR.
    • The Stage 1.5 boot code is stored in the post-MBR gap (if available).
    • The Stage 2 boot code (GRUB core modules) is stored in /boot/grub2/.
  • UEFI Installation:
    • The grubx64.efi file is copied to the EFI System Partition.
    • GRUB is registered as a boot entry with the system's UEFI firmware.

5. Registers the Bootloader (If UEFI)

  • On UEFI systems, grub2-install registers grubx64.efi with the UEFI boot manager using efibootmgr.

More from ChatGPT on "GRUBing"

Comprehensive Guide to Installing GRUB on Rocky Linux (UEFI & BIOS)

This guide covers:

  • UEFI bootloader installation on a target drive.
  • Legacy BIOS (MBR) bootloader installation.
  • BLS (Boot Loader Specification) and its effect on grub2-mkconfig.
  • GRUB Boot Stages Explanation (Stage 1, Stage 1.5, Stage 2).
  • How to properly configure bootloader entries and verify installation.

1. Understanding GRUB Boot Stages

GRUB Stage Description Applies To
Stage 1 MBR code (first 512 bytes of disk) loads Stage 1.5 or Stage 2. Legacy BIOS (MBR) Only
Stage 1.5 Located in the first 30KB after MBR, it helps find /boot/grub2. Legacy BIOS (MBR) Only
Stage 2 Loads the GRUB menu, kernel, and initrd. Both UEFI and BIOS
UEFI Firmware Boot Uses the EFI System Partition (ESP) instead of MBR. Loads grubx64.efi. UEFI Only

2. Installing GRUB for UEFI Booting

A. Identify the Target Drive

List available drives and partitions:

Look for the EFI System Partition (ESP), usually formatted as FAT32 (vfat).

Example: If the target drive is /dev/sdb, and the EFI partition is /dev/sdb1, mount it:

B. Bind System Directories (for Chroot)

If configuring an external system:

C. Install Required UEFI GRUB Packages

D. Install GRUB to the Target Drive

  • --target=x86_64-efi → Specifies UEFI mode.
  • --efi-directory=/boot/efi → Ensures GRUB is installed in the EFI partition.
  • --bootloader-id=rocky → Labels the boot entry as "Rocky".
  • --recheck → Forces a re-scan of the drive layout.

E. Generate the GRUB Configuration

However, Rocky Linux follows BLS, which makes this step less critical (explained in Section 4).


3. Installing GRUB for Legacy BIOS (MBR Boot)

A. Mount the Target Drive's Root Partition

If the system has a separate /boot partition:

B. Chroot into the Target System

C. Install Required BIOS GRUB Packages

D. Install GRUB to the MBR of the Target Drive

  • --target=i386-pcRequired for Legacy BIOS (MBR) booting.
  • /dev/sdbMust be the whole disk (not a partition like /dev/sdb1).
  • --recheck → Forces a disk layout rescan.

E. Generate the GRUB Configuration

But due to BLS, grub.cfg is no longer the main configuration source.


4. Understanding BLS (Boot Loader Specification)

Rocky Linux follows BLS (Boot Loader Specification), meaning boot entries are controlled by systemd-boot entries, not grub2-mkconfig.

How to Manually Manage BLS Entries

  1. List Existing Boot Entries:
  2. Manually Create a BLS Entry:
  3. Example BLS Entry (rocky.conf)
  4. Enable BLS Support in GRUB (if needed)

Why BLS Makes grub2-mkconfig Less Relevant

  • Traditional GRUB entries are no longer used in Rocky Linux.
  • The bootloader reads BLS entries instead.
  • grub.cfg is still used for bootloader settings, but kernel entries are controlled by BLS.

5. Verify Installation

Check EFI Boot Entries (For UEFI Only)

Check GRUB Installation

Check BLS Entries

Reboot and Test


6. Summary

Boot Mode Target Option for GRUB Install Command Config Location
UEFI x86_64-efi grub2-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=rocky /boot/efi/EFI/rocky/grub.cfg
BIOS (MBR) i386-pc grub2-install --target=i386-pc /dev/sdb /boot/grub2/grub.cfg
BLS (Rocky Linux Default) systemd-boot controlled bootctl list /boot/loader/entries/


1. The Presence (or Absence) of /boot/grub2/grub.cfg

  • If grub.cfg exists, GRUB will parse it.
  • If grub.cfg does NOT exist, GRUB will attempt to read the BLS entries directly instead.

This means:

Deleting grub.cfg forces GRUB to rely entirely on BLS.

Regenerating grub.cfg (grub2-mkconfig) puts GRUB back into a "hybrid mode" where it may still reference BLS but also maintain compatibility with legacy boot setups.


2. GRUB’s Bootloader Installation (grub2-install)

The actual bootloader installed to the disk controls whether it defaults to BLS mode or grub.cfg mode.

When GRUB is installed with:

(where /dev/sdX is your boot disk, like /dev/sda or /dev/nvme0n1), it does the following:

  • Sets up GRUB’s core image (/boot/grub2/i386-pc/core.img for BIOS systems or /boot/efi/EFI/rocky/grubx64.efi for UEFI systems).
  • Embeds the logic for how GRUB will find and load boot entries.
  • If BLS is active, it instructs GRUB to skip parsing grub.cfg and go directly to BLS entries.

This means: ✔ If GRUB was installed with BLS enabled, even if grub.cfg exists, it may not be used.

✔ If GRUB was installed without BLS support, it will fall back to grub.cfg.


How to Explicitly Switch Between BLS and grub.cfg

To Force BLS Mode (and Ignore grub.cfg Completely)

  1. Ensure BLS entries exist: If this folder is empty, something is broken.
  2. Delete grub.cfg (this ensures GRUB won’t use it):
  3. Ensure GRUB is installed with BLS support: (For UEFI systems; adjust for BIOS if needed.)
  4. Reboot and verify that grub.cfg is no longer in use.

To Force Legacy grub.cfg Mode (and Ignore BLS)

  1. Ensure grub.cfg is present:
  2. Reinstall GRUB to disable BLS mode: (The --no-bootloader-spec flag explicitly prevents GRUB from using BLS.)
  3. Reboot and verify that grub.cfg is being used.

Final Answer

The real switch between BLS and grub.cfg is NOT in /etc/default/grub—it's controlled by how GRUB is installed and configured.

Deleting grub.cfg forces BLS mode, provided GRUB was installed with BLS support.

Reinstalling GRUB with --no-bootloader-spec forces it to use grub.cfg instead of BLS.

To change modes, you must either delete grub.cfg (to force BLS) or reinstall GRUB (to force legacy mode).