Stage 3 · Build
Boot & Init Systems
initramfs Early Userspace
Dracut, initramfs-tools, root device discovery, cryptsetup, and emergency shell recovery.
initramfs Overview
The initramfs (initial RAM filesystem) is a temporary root filesystem loaded into memory by the boot loader. It contains the minimum tools needed to mount the real root filesystem. Without it, the kernel cannot access LVM, RAID, or encrypted partitions.
# List initramfs contents
lsinitramfs /boot/initrd.img-$(uname -r) | head -20
# bin/
# conf/
# etc/
# lib/
# run/
# sbin/
# usr/
# var/
# init <- the first script executed by the kernel
# Extract initramfs
mkdir /tmp/initrd && cd /tmp/initrd
unmkinitramfs /boot/initrd.img-$(uname -r) .
# Check what's inside
ls early/
ls main/
# main/modules/ contains kernel modules
# main/run/ contains runtime scriptsinitramfs is a cpio archive that the kernel unpacks into RAM. The /init script runs first, discovers the root device, and pivots to the real root.
Dracut
# Dracut is the initramfs generator for Fedora/RHEL/CentOS
# Regenerate initramfs
sudo dracut --force
# Include specific modules
sudo dracut --force --add "crypt lvm" /boot/initramfs-custom.img
# List available modules
dracut --list-modules
# View module contents
ls /usr/lib/dracut/modules.d/
# 00-systemd 01-discover 02-lvm
# 03-crypt 04-network 05-btrfs
# Create minimal initramfs
sudo dracut --force --hostonly
# Test in a VM
sudo dracut --force --test-imageDracut dynamically generates initramfs based on your system configuration. Use --hostonly for smaller, faster boot images.
initramfs-tools
# Regenerate initramfs
sudo update-initramfs -u
# Generate new initramfs
sudo update-initramfs -c -k $(uname -r)
# List installed kernels
ls /boot/initrd.img-*
# View hooks
ls /etc/initramfs-tools/hooks/
# cryptroot lvm2 plymouth
# View scripts
ls /etc/initramfs-tools/scripts/
# init-bottom init-top local-bottom local-premount
# Update initramfs configuration
cat /etc/initramfs-tools/initramfs.conf
# MODULES=most
# BUSYBOX=auto
# COMPRESS=gzipinitramfs-tools uses hooks to include drivers and scripts. Hooks determine what gets included in the initramfs.
Root Device Discovery
# The initramfs /init script discovers the root device:
# 1. Parse kernel command line for root=
cat /proc/cmdline | grep -o "root=[^ ]*"
# root=/dev/mapper/vg0-root
# 2. Load required modules
# LVM, RAID, crypto, filesystem drivers
# 3. Assemble RAID arrays
mdadm --assemble --scan
# 4. Unlock encrypted devices
cryptsetup luksOpen /dev/sda2 encrypted_vol
# 5. Activate LVM volumes
vgchange -ay
# 6. Mount root filesystem
mount /dev/mapper/vg0-root /new_root
# 7. Pivot to real root
exec switch_root /new_root /sbin/initThe root device discovery process loads drivers, assembles storage, and mounts the root filesystem. This is why initramfs must contain the right modules.
cryptsetup in initramfs
# Configure cryptsetup for initramfs (Debian/Ubuntu)
# /etc/cryptsetup-initramfs/conf-hook
# DEVICE=
# CIPHER=
# KEY_SIZE=
# HASH=
# USE_LUKS=
# LUKS_OPTIONS=
# Add key to initramfs
sudo cryptsetup luksAddKey /dev/sda2 /etc/keys/root.key
# Configure initramfs to use the key
echo "KEYFILE=/etc/keys/root.key" | sudo tee -a /etc/cryptsetup-initramfs/conf-hook
# Regenerate initramfs
sudo update-initramfs -u
# For Dracut (Fedora/RHEL)
# /etc/dracut.conf.d/crypt.conf
# add_dracutmodules+=" crypt "
# install_items+=" /etc/keys/root.key "initramfs must contain the encryption key or prompt for the passphrase at boot. Store keys securely and restrict permissions.
Emergency Shell Recovery
# Boot to emergency shell
# In GRUB, add to kernel line:
# init=/bin/bash
# or
# systemd.unit=emergency.target
# After boot, filesystem is read-only
mount -o remount,rw /
# Check filesystem
fsck -y /dev/sda2
# Fix initramfs
update-initramfs -u
# Reinstall GRUB
grub-install /dev/sda
update-grub
# If root password is lost
passwd root
# Reboot
reboot -f
# If initramfs is missing or corrupt
# Boot from live USB
sudo mount /dev/sda2 /mnt
sudo mount /dev/sda1 /mnt/boot
for i in /dev /proc /sys /run; do
sudo mount --bind $i /mnt$i
done
sudo chroot /mnt
update-initramfs -u
exitThe emergency shell provides root access for repairs. Always have a recovery plan for critical systems.
Before deploying initramfs changes to production, test in a VM or staging system. A broken initramfs means the system will not boot.
Each kernel version requires its own initramfs. After kernel updates, always regenerate the initramfs with update-initramfs -u or dracut --force.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.