In this blog, I will document what I learned from Exploring Linux Internals: Advanced Insights and Practical Applications by Sander van Vugt.

I will discuss here the boot process and do some Lab challenges at the end.

Will discuss about:

  1. Linux Boot Procedure
  2. BootLoader (Grub2)
  3. Initramfs
  4. Using emergency.target to troubleshoot the system (Please see the lab below)
  5. Grub Rescue(I didn't finished it but I will create another blog for this one)

 

Linux Boot Procedure

Process

  1. Firmware Initialization: The process starts with the firmware (BIOS or UEFI) identifying a bootable disk.

  2. Boot Loader: Depending on the disk type (MBR or GPT), the boot loader (GRUB) is located and loaded.

  3. Kernel and Initramfs: The Linux kernel and initramfs (containing necessary drivers) are loaded.

  4. Init Process: The init process, typically systemd, takes over to manage the early boot and system services.

  5. Shell Launch: Finally, systemd starts a shell (bash or GDM), completing the boot process.

Troubleshooting Techniques

  1. Rescue Disk: Use a rescue disk if anything goes wrong before GRUB is shown.

  2. Interrupting at Initramfs: Use the rd.brake option to stop the boot procedure at the end of loading the initramfs for early troubleshooting (Red Hat-specific).

  3. Bypassing Systemd: Use init=/bin/bash to bypass the loading of systemd for troubleshooting after the initramfs stage.

  4. Emergency Target: Use systemd.unit=emergency.target to load only the bare minimum, allowing for troubleshooting before the early boot completes.

  5. Rescue Target: Use systemd.unit=rescue.target to access a fully functional system without services, enabling troubleshooting after the early boot.

Firmware

  1. UEFI - capable of booting from disks bigger than 2 TB with GUID Partition Table (GPT). Can be configured as Secured Boot. It is signed by digital signature (TPM).
    1. Unlike BIOS Booting, there is no longer a need to scan bootable devices
    2. The OS registers itself in the firmware as the next thing to boot
    3. The EFI system partition is required to boot
  2. On Red Hat, grub2-install will write a bootable target to the UEFI firmware with the name grubx64.wfim abd this will cause system boot to fail, as the UEFI firmware should contain the shim.efi file
  3. To reinstall GRUB2 on UEFI systems, use dnf reinstall grub2-efi shim from a rescue system
  4. efi contains keys that are required for secured booting
  5. Optionally, also use grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg to restore the GRUB configuration file

Understanding shim.efi

  1. shim.efi is the default UEFI boot target for RHEL
  2. From shim.efi, the grubx64.efi bootloader is loaded
  3. shim.efi is signed with a keuy that is trusted by most UEFI firmware
  4. The efibootmgr tool allows users to manage the available UEFO boot targets

Using efibootmgr

  1. efibootmgr makes anaging UEFI boot targets possible
  2. Use efibootmgr to list all current boot targets
  3. Active voot are marked with a *
  4. Use efibootmgr -v for more details
  5. Force the system to boot into another entry the next time ut is started using -n option:
    1. efibootmgr -n 2c
  6. Alternatively, from the GRUB boot menu, access the UEFI firmware settings menu option

Executing lsblk, you will see the boot efi partition

Checking the content, you'll see that the partiton has no content. This is because if you're using EFI/UEFI boot, i the bootloader isn't in the MBR but in the EFI System Partition, typically mounted at /boot/efi.

root@grace-ubuntu:~# xxd -l 512 /dev/vda
00000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000120: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000130: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000170: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000190: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001c0: 0200 eeff ffff 0100 0000 ffff ff07 0000  ................
000001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.
root@grace-ubuntu:~# 

Checking the content of /boot/efi

You will also notice that its a vfat file system - this file system is readble for all operating system

root@grace-ubuntu:/boot/efi# mount | grep efi
efivarfs on /sys/firmware/efi/efivars type efivarfs (rw,nosuid,nodev,noexec,relatime)
/dev/vda1 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)

 

EFIbootmgr - to check boot targets

root@grace-ubuntu:/boot/efi# efibootmgr
BootCurrent: 0005
Timeout: 3 seconds
BootOrder: 0005,0003,0000,0002,0004,0001
Boot0000* UiApp	FvVol(64074afe-340a-4be6-94ba-91b5b4d0f71e)/FvFile(462caa21-7614-4503-836e-8ab6f4662331)
Boot0001* UEFI PXEv4 (MAC:62D67B4C7A44)	PciRoot(0x0)/Pci(0x1,0x0)/MAC(62d67b4c7a44,1)/IPv4(0.0.0.00.0.0.0,0,0){auto_created_boot_option}
Boot0002* UEFI Misc Device	VenHw(93e34c7e-b50e-11df-9223-2443dfd72085,00){auto_created_boot_option}
Boot0003* UEFI Misc Device 2	PciRoot(0x0)/Pci(0x6,0x0){auto_created_boot_option}
Boot0004* EFI Internal Shell	FvVol(64074afe-340a-4be6-94ba-91b5b4d0f71e)/FvFile(7c04a583-9e3e-4f1c-ad65-e05268d0b4d1)
Boot0005* Ubuntu	HD(1,GPT,6d96b0b1-0d3a-4b03-ac93-db5bf954a9c1,0x800,0x219800)/File(\EFI\ubuntu\shimaa64.efi)

You can change the boot priority using efibootmgr -n 0005. You can see that there is boot current and there is boot next

 

BootLoader

  1. Through the firmmware, the bootloader is activated.
  2. The purpose of bootloader is to load Linux Kernel and related initramfs
  3. Also, boot menu can be presented to specify boot options
  4. Grub2 is the common bootloader in Linux
  5. Uboot is used on embedded Linux.
  6. Press e edit the grub config

Grub2

  • it is the unix/linux bootloader. When the UEFI boots, it boots grubx64.efi bootloader in the EFI system partition of GPT.

  1. GRUB loads two files from the file system into RAM:
    • kernel(vmlinuz) - At boot, the kernel is loaded into memory, and GRUB passes the kernel parameters (command line) to it. The kernel then mounts the initrd as the initial root filesystem.
      • vmlinuz is the current kernel image
      • root = UID*** ois the UID where the root fs is located
      • ro -initially during the boot process to ensure system stability and allow for potential repair actions. It allows for repair actions without having any files open. This helps in maintaining the integrity of the file system and prevents further damage during the boot process.
    • initrd (or initramfs) – This provides a minimal root filesystem in RAM that enables essential services such as loading drivers and preparing to mount the real root filesystem. It loads necessary drivers and kernel modules (e.g., for disk, filesystem, LVM, encryption). Once complete, control is passed to the real root filesystem using switch_root or pivot_root

      After mounting the real root filesystem (usually located at /), the kernel starts the first userspace process: /sbin/init, which in most modern systems is a symbolic link to systemd.

      The systemd is responsible for starting up the system services such as daemons, like web server, database, etc.

      Note that these are user space services and not features of kernel. The user space first run the service then use its glibc function to interface with kernel via system calls.  The kernel in turn manages low level task and interactions with memory, I/O operations and communication with hardware/drivers.

Example, I will modify the grub menu to use mem=1064 and boot the systemd in rescu.target mode

Using free -m, I checked that 1 GB of RAM was specified, but only 672 MB is shown because the video driver, which was loaded by GRUB, uses some of the memory for video emulation. This is why I see less available memory.

 

Example of checking the modules un lsinitramfs

Checking whether the xfs filesystem driver is included in your initramfs image

  • lsinitramfs: Lists the contents of the initrd (initial RAM disk) image.
  • /boot/initrd.img-$(uname -r): Refers to the initrd file for your current running kernel version.
root@grace-ubuntu:~# lsinitramfs /boot/initrd.img-$(uname -r) | grep xfs
usr/lib/modules/6.8.0-47-generic/kernel/fs/xfs
usr/lib/modules/6.8.0-47-generic/kernel/fs/xfs/xfs.ko.zst
  • usr/lib/modules/.../fs/xfs: This is the directory inside the initramfs that holds the XFS kernel module.
  • xfs.ko.zst: This is the kernel object module for XFS, compressed using Zstandard (.zst) format.

Service Managers

  • The service manager handles loading everything after the kernel and initrd have been loaded, and the root filesystem has been mounted.

  • It initializes remaining hardware devices not handled by initramfs, mounts filesystems, and starts services.

  • Once these tasks are completed, it starts a shell.

  • Systemd is the dominant service manager on most leading Linux distributions.

  • Alternatives like init and upstart are still used on some distributions but are less common.

  • Minimized Linux distributions typically do not use Systemd.

Accessing an Early Boot Shell

These are some of the commands that can be used. You can edit the grub to add these as a startup parameter to eneter a systemd troubleshooting mode

  • systemd.unit = emergency.target - very early troubleshoot with minimal functionality

  • systemd.unit = rescue.target - stops the system before loading of the services.

  • rd.break or init=/bin/bash  - to enter a troubleshooting environment before systemd is started

  • Enable debug-shell.service and access through /dev/tty9 while booting. Can press ctrl + alt+f9+ function key 9 while booting

Laboratory:

A. Will make the system fail to boot by adding invalid mount in /etc/fstab

  1. echo '/dev/sdx1 /mnt/foo ext4 defaults 0 2' >> /etc/fstab

    echo '/dev/sdx1 /mnt/foo ext4 defaults 0 2' >> /etc/fstab
  2. Reboot the machine.
  3. In ubuntu, it automatically directed me to emergency mode

  4. For other Linux machines, you can edit the grub to boot in emergency target

  5. In emergency mode, we can check the journalctl logs. You can then find out that the /mnt/foo failed to mount.

  6. We can then check the /etc/fstab for bad mounts
  7. Commenting it out

  8. Then reboot normally

B. Recovering from Lost GPT bootloader

  1. Delete the shimx64.efi, grub.cfg and grubx64.efi bootlader using the commands below

  2. Reboot the server

    Choose Try or Install Ubuntu Server

  3. In the CHoose Language Screen, Click Help and Enter SHell

  4. You are now in the live shell enviroment provided by Ubuntu Live ISO. Run lsblk to display all disk partitions. Check for lvm, where the system’s root filesystem is likely stored. You will mount this later as part of the recovery process.

  5. Execute lvdisplay to know the name of the volume group. Then, since this is live iso, there is no auto mount, and /etc/fstab, we need to mount the logical volume to /mnt directory.

    However as seen below, we have trouble mounting the logical volume. I checked if the volume group was activated, 

  6. Since this is a live shell Ubuntu OS and the bootloader was not initialized, we need to manually activate the volume group and then mount it in /mnt

7. Mount the /dev/nvme0n1p1 (vfat partition as checked with lsblk -f) to /mnt/boot/efi

8. Inside /mnt, you may see directories like /boot/efi, /dev, /proc, and /sys. To simulate a real root filesystem environment and prepare for GRUB recovery, you need to manually bind these virtual filesystems and mount the EFI partition (e.g., /dev/nvme0n1p1) to /mnt/boot/efi. This allows you to chroot into the installed system as if it were running normally and reinstall GRUB

9. Now change the root to /mnt directory and we can now install and recover the grub

10. I have problem with grub-install because it cannoyt locate my /dev/mapper, I just used this one for arm64

11. Unmount the directory we bind then reboot.

12. Now after here, it loads in grub. I still need to troubleshoot it as I'm using arm64 and EFI. Will create another blog for this one.