Low Orbit Flux Logo 2 F

GRUB and Boot Info

Partition types:

Partition schemes:

UEFI / BIOS with GPT EFI, BIOS Boot, Swap, Root Most flexible combination, supports almost everything
UEFI with GPT EFI, Swap, Root Most modern combination
BIOS with GPT BIOS Boot, Swap, Root Supports old hardware and VMs but with nice modern partitions
BIOS with MBR Swap, Root Potentially better dual booting support with windows
UEFI with MBR ???? CRAZY / pointless - no one does this

UEFI and BIOS support with GPT ( most flexible option ):

Mount point Partition Partition type Suggested size
/boot/efi /dev/sda1 EFI 512 MiB
na /dev/sda2 BIOS Boot 2Mib
[SWAP] /dev/sda3 Linux swap >= RAM (for hibernate)
/ /dev/sda4 root FS Remainder of the device

EFI System Partition (ESP)

BIOS Boot:

NOTE - mount order on boot can be an issue if you have both /boot and /boot/efi partitions

/boot doesn’t really need to be separate partition, allows you to use FAT32 for in case other FS gets features not supported
/boot/efi EFI System Partition (ESP)
/boot/efi/EFI// EFI applications installed here

UEFI booting:

My Ubuntu desktop has:

Partitioning tools:



efibootmgr          # list boot entries
efibootmgr -v       # verbose
efibootmgr --create --disk /dev/sda --part 1 --loader /EFI/ubuntu/grubx64.efi --label "Ubuntu"
efibootmgr --create --disk /dev/sdX --part Y --loader /EFI/GRUB/grubx64.efi --label "GRUB"
efibootmgr --create --disk /dev/sda --part 1 --loader /EFI/GRUB/grubx64.efi --label "My Arch"      # real example I used

   # /dev/sdX → The disk containing the EFI System Partition (ESP).
   # --part Y → The partition number of the ESP (usually 1).
   # --loader → Path to the bootloader relative to the ESP.
   # --label → Name for the boot entry.

efibootmgr --bootnext 0001                  # set default boot entry  ( only for next boot )
efibootmgr --bootorder 0002,0001,0003       # change order
efibootmgr --bootnum 0003 --delete-bootnum  # delete an entry

/boot/efi/EFI/BOOT/BOOTX64.EFI default EFI file location ??? - good for USB boot disks

Partitioning GPT:



DISK="/dev/sda"
mem=`free -m|grep -i Mem: | awk '{print $2}'`     # swap set to RAM size to support hibernate
START=1
ESP=$(( $START+512 ))
BIOS_BOOT=$(( $ESP+2 ))
SWAP=$(( $BIOS_BOOT+mem ))
ROOT=100%

wipefs -a $DISK
parted -s ${DISK} mklabel gpt

parted -s --align=optimal ${DISK} mkpart ESP fat32 ${START}MiB ${ESP}MiB
parted -s ${DISK} set 1 esp on
parted -s --align=optimal ${DISK} mkpart BIOS_BOOT fat32 ${ESP}MiB ${BIOS_BOOT}MiB
parted -s ${DISK} set 2 bios_grub on
parted -s --align=optimal ${DISK} mkpart linux-swap ${BIOS_BOOT}MiB ${SWAP}MiB
parted -s --align=optimal ${DISK} mkpart linux ${SWAP}MiB 100%

parted -s ${DISK} print


Check for UEFI:



test -d /sys/firmware/efi && echo efi || echo bios
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS

View EFI vars, some Boot* files look like boot loaders:



ls /sys/firmware/efi/efivars  #

GRUB



pacman -Sy grub os-prober
mkdir /boot/grub
grub-mkconfig -o /boot/grub/grub.cfg       # generate grub config, need to be booted or chrooted in
grub-install /dev/sda

update-grub   # wrapper for grub-mkconfig

Method 1:

Overwrite /boot/efi/EFI/BOOT/BOOTX64.EFI ( installs GRUB EFI application ). The rest of GRUB is installed here: /boot/grub ( grub modules ).



grub-install --target=x86_64-efi --removable

Method 2:

This method uses EFI variables which firmware tends to prefer. It will supposedly be more likely to be albe to select your disk without needing to invoke a boot menu ( unconfirmed ).

Creates boot entry in EFI variables and installs a grub efi application, will be listed in EFI variables: /boot/efi/EFI/[id]/grubx64.efi



grub-install --bootloader-id=LFS --recheck

MBR Grub Install



grub-install --target=i386-pc /dev/sdX     # BIOS mode  ( install either to MBR or BIOS Boot Partition )

GRUB Config



/etc/default/grub       # options to generate main config
/etc/grub.d/            # scripts to generate main config


/boot/grub/grub.cfg      # main config file, generated


/etc/grub.d/40_custom   # custom menu entries ( need to generate from this )
/boot/grub/custom.cfg   # custom menu entries  ( don't need to generate config )


Custom menu entry:



/etc/grub.d/40_custom
...
...
menuentry "Other Linux Distro!!!" {
        insmod part_gpt
        insmod fat
        set root='hd0,gpt1'
        linux /vmlinuz-linux root=UUID=338c1900-a4db-425a-9cf0-d4a42a8ae254 rw loglevel=3 quiet
        initrd /intel-ucode.img
}

OS Prober

Enable or disable using os-prober to detect other bootable partitions for generated config:



GRUB_DISABLE_OS_PROBER=false



/etc/default/grub
GRUB_CMDLINE_LINUX              # variables for kernel
GRUB_CMDLINE_LINUX_DEFAULT      # variables for kernel ( skipped for recovery boot entry )
GRUB_TIMEOUT=45
GRUB_TIMEOUT=600


GRUB_TOP_LEVEL="/boot/vmlinuz-linux"    # specify kernel for top level entry

Super simple GRUB config from LFS emergency boot disk creation:



cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod ext2
set root=(hd0,2)

menuentry "GNU/Linux, Linux 6.1.11-lfs-11.3" {
        linux   /boot/vmlinuz-6.1.11-lfs-11.3 root=/dev/sda2 ro
}
EOF

Another GRUB config from LFS install:



cat > /boot/grub/grub.cfg << EOF
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod part_gpt
insmod ext2
##set root=(hd0,2)
search --set=root --fs-uuid 1a900e89-0aba-40f3-b07b-abe8cc248be9

if loadfont /grub/fonts/unicode.pf2; then
  set gfxmode=auto
  insmod all_video
  terminal_output gfxterm
fi

menuentry "GNU/Linux, Linux 6.1.11-lfs-11.3"  {
  #linux   /vmlinuz-6.1.11-lfs-11.3 root=/dev/sda2 ro
  # linux   /vmlinuz-6.1.11-lfs-11.3 root=PARTUUID=2017147d-221e-1245-a2dd-725696bc30d5 ro
   linux   /vmlinuz-6.1.11-lfs-11.3 root=PARTUUID=322ae130-327b-2c43-9d46-6407b1e8ef82 ro

}

menuentry "Firmware Setup" {
  fwsetup
}
EOF

GRUB Commands

From menu:



grub>               # small problem, ex: can't find menu but can find disks
grub rescue>        # big problem, ex: can't find disks


From rescue mode, get normal grub shell:

grub rescue> set prefix=(hdX,Y)/boot/grub
grub rescue> insmod (hdX,Y)/boot/grub/i386-pc/normal.mod
rescue:grub> normal

set pager=1    # enable pager for better output

ctrl-l   # refresh menu

c    # enter commands
e    # edit menu entry
esc  # cancel changes, return to main menu
Ctrl-x # boot current edited menu entry

More GRUB rescue commands:



cat (hd0,1)/etc/issue



set root=(hd0,1)         # first disk first partition
chainloader +1           # jump to first sector of what is stored on root
boot


set root=hd0             # first disk
chainloader +1           # jump to first sector of what is stored on root
boot


insmod fat                                                # insert fat module to access FAT partition
set root=(hd0,gpt4)
chainloader (${root})/EFI/Microsoft/Boot/bootmgfw.efi     # specify EFI file path
boot

Recommended to specify msdos vs gpt to avoid ambiguity especially in scripts. I often see people just leave both of these out. Ubuntu doesn’t use either, it just searches based on UUID.



(hd0,msdos5)    # MBR partition
(hd0,gpt5)      # GPT partition
(hd0,5)         # let GRUB figure it out

(hd0,msdos5)       # extended partitions start counting from 5
(hd1,msdos1,bsd1)  # second HD, first PC slice, BSD partition 'a'

set root=(        # use tab completionfor list of drives

Inserting modules:



grub> insmod part_gpt
grub> set root=(hd0,gpt3)

grub> insmod part_bsd
grub> set root=(hd0,netbsd1)


insmod linux

insmod ntfs
insmod gzio
insmod part_gpt
insmod ext2
insmod xfs

specify kernel, options, and initrd:



linux   /boot/vmlinuz-6.5.0-17-generic root=UUID=cafb9a4e-0cb6-44e4-955b-f0b498c457e5 ro  quiet splash $vt_handoff
initrd  /boot/initrd.img-6.5.0-17-generic

recovery mode:



linux   /boot/vmlinuz-6.5.0-17-generic root=UUID=cafb9a4e-0cb6-44e4-955b-f0b498c457e5 ro recovery nomodeset dis_ucode_ldr
initrd  /boot/initrd.img-6.5.0-17-generic

An example:



grub rescue> set prefix=(hd0,1)/boot/grub
grub rescue> set root=(hd0,1)
grub rescue> insmod normal
grub rescue> normal
grub rescue> insmod linux
grub rescue> linux /boot/vmlinuz-3.13.0-29-generic root=/dev/sda1
grub rescue> initrd /boot/initrd.img-3.13.0-29-generic
grub rescue> boot

Boot Process

MBR - first 512 byts of bootable disk, has stage 1 bootloader, partition table, boot signature

BIOS / MBR

UEFI / GPT

BIOS / GPT