Modern GRUBing: Difference between revisions

Created page with "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). ==== BLS (Boot Loader Specification) ==== /etc/fstab grub2-mkconfig -o /tmp/boot/grub2/grub.cfg --root-directory=/tmp/LV.ROOT ==== GRUB "Installation" for MBR / BIOS ==== Installs the Stage 1 Boot Code (program) in..."
 
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
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).
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<br />
====Rough D====
*Start from a 'Clean Slate': <code>wipefs -a /dev/sdX</code> (make dang sure the proper 'sdX' is chosen!)
*Set the Partition Type (MBR or GPT): <code>cfdisk /dev/sdX</code>, (select DOS or GPT, save it (write) )
*Create Partitions: cfdisk
**https://en.wikipedia.org/wiki/Partition_type
**Linux EXT4: $83 (that's Commodore Speaky for 83h)
**Linux LVM (and LVM2): $8E
*LVM Stuff using Commands or BLIVET (sadly only Rocky Linux 8)
**<code>pvcreate /dev/sdXy</code>
**<code>vgcreate VG.NVMe.ROOT /dev/sdXy</code>
***<code>vgrename WhatEverOldName WhatEverNewName</code>
**<code>lvcreate -L 64G -n LV.ROOT VG.NVMe.ROOT</code>
*Format Partitions
**mkfs.vfat -F32 /dev/sdXy
**<code>mkfs.ext4 -v -L ROOT.EXT4 /dev/sdXy ( -v = Verbose, -L = Label )</code>
**<code>mkswap -L SWAP /dev/sdaXy</code>
*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
**<code>mkdir /mnt/efi</code>
**<code>mount /dev/nvme0n1p1 /mnt/efi</code>
**<code>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**</code>
***--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): <code>bootctl status</code>
**Command for EFIBootMgr: <code>efibootmgr -v</code>
*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) ====
====BLS (Boot Loader Specification)====


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


/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


grub2-mkconfig -o /tmp/boot/grub2/grub.cfg --root-directory=/tmp/LV.ROOT
...see below for key information on /etc/default/grub and BLS


==== GRUB "Installation" for MBR / BIOS ====
====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
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


Line 19: Line 85:
Use DD to clone the Boot Partition
Use DD to clone the Boot Partition


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


Line 26: Line 92:
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
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 ====
====ChatGPT's Opinion====


===== GRUB "Installation" ('''What Does <code>grub2-install</code> Do?)''' =====
=====GRUB "Installation" ('''What Does <code>grub2-install</code> Do?)'''=====
The <code>grub2-install</code> 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.
The <code>grub2-install</code> 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 <code>grub2-install</code> Works''' ======
======'''How <code>grub2-install</code> Works'''======
When executed, <code>grub2-install</code> performs the following actions:
When executed, <code>grub2-install</code> performs the following actions:


'''1. Determines the Installation Target'''
'''1. Determines the Installation Target'''


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


'''2. Copies GRUB Core to the Correct Location'''
'''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 '''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 '''GPT systems using BIOS (BIOS-GPT)''', it places GRUB in the '''BIOS Boot Partition'''.
* On '''UEFI systems''', it installs the <code>grubx64.efi</code> file into the '''EFI System Partition (ESP)'''.
*On '''UEFI systems''', it installs the <code>grubx64.efi</code> file into the '''EFI System Partition (ESP)'''.


'''3. Selects the Correct GRUB Modules'''
'''3. Selects the Correct GRUB Modules'''


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


'''4. Writes GRUB Boot Code'''
'''4. Writes GRUB Boot Code'''


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


'''5. Registers the Bootloader (If UEFI)'''
'''5. Registers the Bootloader (If UEFI)'''


* On '''UEFI systems''', <code>grub2-install</code> registers <code>grubx64.efi</code> with the UEFI boot manager using <code>efibootmgr</code>.
*On '''UEFI systems''', <code>grub2-install</code> registers <code>grubx64.efi</code> with the UEFI boot manager using <code>efibootmgr</code>.
 
===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 <code>grub2-mkconfig</code>.'''
*'''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'''==
{| class="wikitable"
!'''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 <code>/boot/grub2</code>.
|'''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 <code>grubx64.efi</code>.
|'''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 (<code>vfat</code>)'''.
 
Example: If the target drive is <code>/dev/sdb</code>, and the EFI partition is <code>/dev/sdb1</code>, 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'''===
 
*<code>--target=x86_64-efi</code> → Specifies '''UEFI mode'''.
*<code>--efi-directory=/boot/efi</code> → Ensures GRUB is installed in the EFI partition.
*<code>--bootloader-id=rocky</code> → Labels the boot entry as '''"Rocky"'''.
*<code>--recheck</code> → 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 <code>/boot</code> partition:
 
==='''B. Chroot into the Target System'''===
 
==='''C. Install Required BIOS GRUB Packages'''===
 
==='''D. Install GRUB to the MBR of the Target Drive'''===
 
*<code>--target=i386-pc</code> → '''Required for Legacy BIOS (MBR) booting'''.
*<code>/dev/sdb</code> → '''Must be the whole disk (not a partition like <code>/dev/sdb1</code>)'''.
*<code>--recheck</code> → Forces a disk layout rescan.
 
==='''E. Generate the GRUB Configuration'''===
⚠ '''But due to BLS, <code>grub.cfg</code> 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 <code>grub2-mkconfig</code>'''.
 
==='''How to Manually Manage BLS Entries'''===
 
#'''List Existing Boot Entries:'''
#'''Manually Create a BLS Entry:'''
#'''Example BLS Entry (<code>rocky.conf</code>)'''
#'''Enable BLS Support in GRUB (if needed)'''
 
==='''Why BLS Makes <code>grub2-mkconfig</code> Less Relevant'''===
 
*'''Traditional GRUB entries are no longer used''' in Rocky Linux.
*The '''bootloader reads BLS entries instead'''.
*'''<code>grub.cfg</code> 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'''==
{| class="wikitable"
!'''Boot Mode'''
!'''Target Option for GRUB'''
!'''Install Command'''
!'''Config Location'''
|-
|'''UEFI'''
|<code>x86_64-efi</code>
|<code>grub2-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=rocky</code>
|<code>/boot/efi/EFI/rocky/grub.cfg</code>
|-
|'''BIOS (MBR)'''
|<code>i386-pc</code>
|<code>grub2-install --target=i386-pc /dev/sdb</code>
|<code>/boot/grub2/grub.cfg</code>
|-
|'''BLS (Rocky Linux Default)'''
|<code>systemd-boot controlled</code>
|<code>bootctl list</code>
|<code>/boot/loader/entries/</code>
|}
<br />
<br />
=='''1. The Presence (or Absence) of <code>/boot/grub2/grub.cfg</code>'''==
*If '''<code>grub.cfg</code> exists''', GRUB will parse it.
*If '''<code>grub.cfg</code> does NOT exist''', GRUB will attempt to '''read the BLS entries directly''' instead.
This means:
✔ '''Deleting <code>grub.cfg</code> forces GRUB to rely entirely on BLS.'''
✔ '''Regenerating <code>grub.cfg</code> (<code>grub2-mkconfig</code>) 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 (<code>grub2-install</code>)'''==
The actual bootloader installed to the disk controls whether it defaults to '''BLS mode or <code>grub.cfg</code> mode'''.
When GRUB is installed with:
(''where <code>/dev/sdX</code> is your boot disk, like <code>/dev/sda</code> or <code>/dev/nvme0n1</code>''), it does the following:
*Sets up '''GRUB’s core image''' (<code>/boot/grub2/i386-pc/core.img</code> for BIOS systems or <code>/boot/efi/EFI/rocky/grubx64.efi</code> 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 <code>grub.cfg</code> and go directly to BLS entries'''.
This means: ✔ If GRUB was installed '''with BLS enabled''', even if <code>grub.cfg</code> exists, it may not be used.
✔ If GRUB was installed '''without BLS support''', it will fall back to <code>grub.cfg</code>.
----
=='''How to Explicitly Switch Between BLS and <code>grub.cfg</code>'''==
==='''To Force BLS Mode (and Ignore <code>grub.cfg</code> Completely)'''===
#'''Ensure BLS entries exist''':  If this folder is empty, something is broken.
#'''Delete <code>grub.cfg</code>''' (this ensures GRUB won’t use it):
#'''Ensure GRUB is installed with BLS support''':  ''(For UEFI systems; adjust for BIOS if needed.)''
#'''Reboot''' and verify that <code>grub.cfg</code> is no longer in use.
----
==='''To Force Legacy <code>grub.cfg</code> Mode (and Ignore BLS)'''===
#'''Ensure <code>grub.cfg</code> is present''':
#'''Reinstall GRUB to disable BLS mode''':  ''(The <code>--no-bootloader-spec</code> flag explicitly prevents GRUB from using BLS.)''
#'''Reboot''' and verify that <code>grub.cfg</code> is being used.
----
==='''Final Answer'''===
✔ '''The real switch between BLS and <code>grub.cfg</code> is NOT in <code>/etc/default/grub</code>—it's controlled by how GRUB is installed and configured.'''
✔ '''Deleting <code>grub.cfg</code> forces BLS mode, provided GRUB was installed with BLS support.'''
✔ '''Reinstalling GRUB with <code>--no-bootloader-spec</code> forces it to use <code>grub.cfg</code> instead of BLS.'''
✔ '''To change modes, you must either delete <code>grub.cfg</code> (to force BLS) or reinstall GRUB (to force legacy mode).'''