Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Linux - System: Boot Process

Back


Boot Process

Stage 1: BIOS/UEFI Initialization


Components Involved


Tasks


Configuration Files


Stage 2: Bootloader (GRUB2) Initialization


Components Involved


Tasks

  1. Load the Bootloader:
    • GRUB2 is loaded into memory by the BIOS/UEFI firmware.
  2. Display Boot Menu:
    • GRUB2 reads its configuration file (grub.cfg) and displays a menu with available boot options.
    • Users can select the desired option or let the default boot entry load automatically.
  3. Load the Kernel and Initramfs:
    • Loads the specified Linux kernel (vmlinuz-*) into memory.
    • Loads the initramfs (initramfs-*) into memory to initialize hardware and mount the root filesystem.
  4. Pass Boot Parameters:
    • GRUB2 passes kernel parameters (e.g., quiet, rhgb, or custom parameters like root=) defined in grub.cfg.
  5. Transfer Control to the Kernel:
    • GRUB2 hands over control to the Linux kernel to continue the boot process.

Congfiguration Files

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

GRUB_TIMEOUT: Time in seconds before the default entry boots. GRUB_DEFAULT: Default boot entry (0-based index or entry name). GRUB_CMDLINE_LINUX: Additional kernel parameters.


Common Commands

CMD DECS
grub2-install --version Displays the GRUB2 version installed on the system.
grub2-install /dev/path Installing GRUB2 Bootloader
grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg Generates or updates the grub.cfg file
grub2-editenv list View GRUB Environment Variables
grub2-set-default 1 Sets the default boot entry by index or name.
grub2-editenv - unset menu_auto_hide Disable GRUB menu auto-hide function

Stage 3: Kernel Initialization


Components Involved


Tasks

  1. Kernel Decompression and Initialization:
    • The compressed kernel image (vmlinuz) decompresses itself and initializes core subsystems, including:
      • CPU and memory management.
      • Interrupt handling.
      • Device discovery.
  2. Mounting initramfs:
    • The initramfs is mounted as a temporary root filesystem.
    • Drivers and tools within initramfs are used to locate and mount the actual root filesystem.
  3. Root Filesystem Mounting:
    • The kernel uses drivers and parameters from initramfs to mount the root filesystem specified by GRUB2 (root=/dev/sda1).
  4. Transfer of Control to init/systemd:
    • The kernel executes /sbin/init or /lib/systemd/systemd, transitioning control to user space.

Configuration Files


Stage 4: systemd Initialization


Components Involved


Tasks


Configuration Files


Targets


Configuration File and Command

CMD DESC
systemctl get-default Display the current target
systemctl set-default graphical.target Set default target
who -r Display the current run-level
systemctl list-dependencies graphical.target | grep target
# graphical.target
# ● └─multi-user.target
# ●   ├─basic.target
# ●   │ ├─paths.target
# ●   │ ├─slices.target
# ●   │ ├─sockets.target
# ●   │ ├─sysinit.target
# ●   │ │ ├─cryptsetup.target
# ●   │ │ ├─local-fs.target
# ●   │ │ └─swap.target
# ●   │ └─timers.target
# ●   ├─getty.target
# ●   ├─nfs-client.target
# ●   │ └─remote-fs-pre.target
# ●   └─remote-fs.target
# ●     └─nfs-client.target
# ●       └─remote-fs-pre.target
ls -al /lib/systemd/system/runlevel*
# lrwxrwxrwx. 1 root root 15 Nov  7 07:06 /lib/systemd/system/runlevel0.target -> poweroff.target
# lrwxrwxrwx. 1 root root 13 Nov  7 07:06 /lib/systemd/system/runlevel1.target -> rescue.target
# lrwxrwxrwx. 1 root root 17 Nov  7 07:06 /lib/systemd/system/runlevel2.target -> multi-user.target
# lrwxrwxrwx. 1 root root 17 Nov  7 07:06 /lib/systemd/system/runlevel3.target -> multi-user.target
# lrwxrwxrwx. 1 root root 17 Nov  7 07:06 /lib/systemd/system/runlevel4.target -> multi-user.target
# lrwxrwxrwx. 1 root root 16 Nov  7 07:06 /lib/systemd/system/runlevel5.target -> graphical.target
# lrwxrwxrwx. 1 root root 13 Nov  7 07:06 /lib/systemd/system/runlevel6.target -> reboot.target

Boot Log

CMD DESC
journalctl -b Shows logs from the last boot
journalctl -b -1 Shows logs from the previous boot
journalctl -b -2 Shows logs from the boot before the previous boot

Lab: Recover root password (RHEL 9)(using rd.break)

Overview


1. Interrupt the Boot Process


2. Modify the GRUB2 Boot Parameters


3. Access the Emergency Shell

# Remount the Root Filesystem as Read-Write
# need write access to edit the root user's password in the system's shadow file.
mount -o remount,rw /sysroot

# Switch to the sysroot Environment
# Change the root directory to /sysroot, enabling commands to work as if the system has booted normally.
chroot /sysroot

# Reset the Root Password
# updates the root password in /etc/shadow.
passwd

# Re-label the Filesystem (SELinux Contexts)
# Create an empty file to signal SELinux to relabel files during the next boot
# SELinux enforces security contexts, and any changes to the system require relabeling to ensure proper access controls.
touch /.autorelabel

# Exit and Reboot the System
# The system will boot normally, applying the SELinux relabeling and allowing access with the new root password.
exit
reboot

Lab: Recover root password (RHEL 9)(using init=/bin/sh)


Modify the GRUB2 Boot Parameters


Access the Emergency Shell

# Remount the file system as writeable
mount -o remount,rw /

# Reset the root password.
passwd

# To relabel all files on the next system boot
touch /.autorelabel

# Reboot the system
/usr/sbin/reboot -f

grub.cfg File

# change value
vi /etc/default/grub
# GRUB_TIMEOUT=10

# apply
grub2-mkconfig -o /boot/grub2/grub.cfg
# Generating grub configuration file ...
# Adding boot menu entry for UEFI Firmware Settings ...
# done
# on UEFI systems.
# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg


Booting into Specific Targets

emergency
rescue
1
s
single

Lab: Reset the root User Password

rd.break
# Make /sysroot appear as mounted on / using the chroot command:
chroot /sysroot

# Remount the root file system in read/write mode
mount -o remount,rw /
# change pwd
passwd

# Create a hidden file called .autorelabel to run SELinux relabeling on all files
touch /.autorelabel

# exit command to quit the chroot shell
exit

# restart the system and boot it to the default target.
reboot