Skip to main content

initramfs: The Initial RAM Filesystem Explained

Learn how initramfs enables Linux boot by loading essential drivers before the root filesystem mounts. Explore early userspace initialization.

Why initramfs Matters

A Linux kernel may need a storage or filesystem driver before it can read the root filesystem. If that driver is a module stored on the same filesystem, boot has a circular dependency. initramfs, the initial RAM filesystem, supplies an early userspace environment with the modules and tools needed to reach the installed system. A kernel with all required drivers built in can boot a simple root filesystem without it.

Understanding initramfs is essential for anyone who has ever struggled with a system that drops to a rescue shell during boot, configured disk encryption, set up network-mounted root filesystems, or simply wanted to know what happens in the seconds between pressing the power button and seeing a login prompt.

The Chicken-and-Egg Problem

To appreciate initramfs, consider what a Linux kernel actually is at the moment it starts running. The kernel is a single binary loaded into memory by the bootloader. It contains core subsystems -- process scheduling, memory management, device infrastructure -- but it does not contain every possible driver for every possible piece of hardware. That would make it enormous and unmaintainable.

Instead, Linux uses loadable kernel modules: small driver files with the .ko extension that live on the filesystem and get loaded on demand. Here is where the paradox appears:

  1. The kernel needs a filesystem driver (ext4, XFS, Btrfs, or similar) to read files from your root partition.
  2. That filesystem driver is a module stored as a file on the root partition.
  3. The kernel cannot load the module because it cannot yet read the partition.

General-purpose distributions use initramfs to support many hardware and storage configurations with one kernel. The root partition might be on a SATA drive requiring the ahci module, on a software RAID array needing md, encrypted with LUKS requiring dm-crypt, or sitting on a network share requiring an entire TCP/IP stack and NFS client.

The Solution: A Filesystem in RAM

initramfs breaks the deadlock by providing a temporary miniature filesystem that the bootloader places directly into RAM alongside the kernel. The archive contains the pieces needed to bridge the gap; its size depends on the included drivers, tools, firmware, and compression:

  • The specific storage drivers your hardware needs
  • Tools to assemble RAID arrays, unlock encrypted volumes, or activate LVM
  • A small init script that orchestrates the whole process
  • Basic utilities (often provided by BusyBox, a single binary that acts as dozens of common commands)

Because this filesystem lives entirely in RAM, the kernel needs no disk drivers to access it. The bootloader can read the archive using its own filesystem and device support before Linux takes control. The kernel then unpacks the files into its initial rootfs.

The Boot Sequence as a Story

Follow the root filesystem and PID 1 through each handoff. Select a stage or press Play; Archive contents shows the supporting files, and vs initrd explains how the archive differs from a RAM disk.

From initramfs to the real root

How does Linux reach a root filesystem whose drivers and tools are stored on that same filesystem?

Step 1 / 7

The kernel can run, but the real root is still locked away

Linux has the core kernel in RAM, but the storage, crypto, or filesystem module it needs may live on the root filesystem it cannot mount yet.

  1. Firmware / GRUB

    finds boot files

    Waiting
  2. RAM payloads

    vmlinuz + archive

    Waiting
  3. Kernel

    vmlinuz running

    Current
  4. initramfs rootfs

    temporary /

    Waiting
  5. /init

    early PID 1

    Waiting
  6. Device stack

    udev + modules

    Blocked
  7. Real root

    /newroot -> /

    Locked
Current root
empty rootfs
PID 1
kernel only
Devices
required driver unavailable
Real root
locked
Command / event · illustrative
# kernel: blocked: device mapper, disk, or filesystem support is missing
mount real root (kernel operation)

This is the bootstrap loop: to read root, the kernel may need code stored on root.

Boot log · illustrative
Boot events · not a recorded trace
1. Kernel is in memory, but the root filesystem is not readable yet.
Authored illustration of a Linux boot with a LUKS root on NVMe. Commands mix kernel operations and userspace examples; they are not a complete boot script. Playback timing and log entries are illustrative, not measured. The Problem step shows the dependency that initramfs avoids.

The example follows a LUKS-encrypted root on NVMe. Other storage layouts change the discovery and assembly work, while retaining the same early-userspace handoff.

Act 1: Firmware and Bootloader

Firmware initializes the machine and starts a bootloader such as GRUB. On a typical UEFI installation, the bootloader is an EFI executable on the EFI System Partition. Legacy BIOS uses a different boot path; it does not rely on UEFI filesystem support.

GRUB then reads its configuration, presents a menu if needed, and loads two critical items into RAM: the kernel image vmlinuz and the initramfs archive initramfs-*.img. It tells the kernel where to find both and hands over control.

Act 2: Kernel Unpacks initramfs

The kernel decompresses and starts executing. One of its earliest actions is to unpack the initramfs archive into rootfs, a RAM-backed filesystem using ramfs or tmpfs depending on kernel configuration. This becomes the initial root filesystem -- the very first root directory the kernel sees.

The archive is a compressed cpio archive, not a disk image. This distinction matters: unlike the older initrd approach, the kernel does not need a filesystem driver to read cpio. The unpacking logic is built directly into the kernel, keeping the process simple and dependency-free.

Act 3: Early Userspace

The kernel executes /init from the unpacked initramfs. This script (or, on systemd-based distributions, a small systemd instance) becomes PID 1 -- the first process. It carries out the critical bridging work:

  • Mounting virtual filesystems like /proc, /sys, and /dev that the kernel uses to communicate with userspace
  • Hardware discovery through udev, which detects attached devices and loads the appropriate kernel modules
  • Storage assembly, which might involve unlocking a LUKS-encrypted volume, scanning for LVM logical volumes, assembling a software RAID array, or all three in sequence
  • Root filesystem mounting, where the real root partition is finally mounted at a temporary location

Act 4: The Handoff

Once the real root filesystem is mounted and verified, early userspace performs the root handoff, commonly through switch_root. It makes the mounted filesystem the new root, removes the old initramfs contents, and executes the real system's /sbin/init. Replacing the process with exec preserves PID 1. From this point forward, systemd (or whatever init system is installed) takes over and the system boots normally.

initramfs vs initrd: Why the Old Way Was Replaced

The predecessor to initramfs was initrd (initial RAM disk), and understanding the difference illustrates why initramfs is a better design.

initrd and initramfs

Format

initrd
Compressed block-device image, commonly ext2.
initramfs
cpio archive unpacked directly into the RAM rootfs.

Kernel requirement

initrd
Needs a filesystem driver before the image can be read.
initramfs
Needs only built-in cpio unpacking.

Memory behavior

initrd
Fixed image size can waste RAM and duplicate cache paths.
initramfs
Grows with extracted files and uses page-cache semantics.

Modification

initrd
Rebuild the disk image.
initramfs
Append or regenerate archive entries.

Modern use

initrd
Legacy design retained mostly for historical context.
initramfs
Default early userspace mechanism on modern Linux systems.

The key insight is that initrd merely moved the chicken-and-egg problem one level down: the kernel still needed a built-in driver for the filesystem used by the initrd image, often ext2. initramfs eliminated that dependency entirely by using cpio, which the kernel handles natively.

What Lives Inside initramfs

The contents of an initramfs archive are a stripped-down Linux root filesystem. A typical one contains:

  • /init -- The entry-point script or binary. Everything starts here.
  • /bin and /sbin -- Essential utilities, often provided by BusyBox (a single binary that implements sh, mount, ls, cat, and dozens of other commands).
  • /lib/modules -- Kernel modules and their dependency metadata. Host-only images select modules for the current machine; generic or fallback images include a broader set. Autodetection can reduce image size but must retain the drivers needed at the next boot.
  • /etc -- Minimal configuration: crypttab entries for encrypted volumes, LVM configuration, or network settings for PXE boot.
  • /usr/lib/systemd -- On systemd-based distributions, a small systemd instance replaces the shell script, bringing parallel initialization and proper dependency management to early boot.

Distributions provide tools to generate initramfs images automatically -- dracut on Fedora/RHEL, mkinitcpio on Arch Linux, and update-initramfs on Debian/Ubuntu. These tools inspect your running system, determine which modules and tools are necessary, and package them into the archive.

When initramfs Is Essential

While simple configurations (a single unencrypted ext4 partition with all drivers compiled into the kernel) can technically boot without initramfs, several common scenarios make it indispensable:

Encrypted Root Filesystems

Full-disk encryption with LUKS is increasingly standard. The kernel implements the block encryption through dm-crypt, while userspace configures the mapping and obtains a key. A common setup needs cryptsetup and a way to prompt for a passphrase. initramfs supplies the early userspace tools and any required modules.

Logical Volume Management and RAID

LVM and software RAID require userspace tools such as lvm and mdadm to assemble volumes before they can be mounted. Without initramfs, these tools would need to be on a partition that itself requires assembly -- another chicken-and-egg loop.

Network Boot

Diskless workstations and cloud instances often mount their root filesystem over the network via NFS or iSCSI. This requires loading network drivers, acquiring an IP address via DHCP, and establishing the network mount -- all before the "real" system exists.

CPU Microcode Updates

An initramfs image can include a special early section containing CPU microcode patches from Intel or AMD. These are applied before almost anything else runs, fixing hardware bugs at the earliest possible moment.

Troubleshooting Boot Failures

When initramfs cannot complete its job -- perhaps because a required driver is missing, the root device UUID has changed, or an encrypted volume cannot be unlocked -- the system typically drops to an initramfs emergency shell. This is the early userspace environment, often BusyBox or a distribution-specific dracut/systemd rescue shell. Its available commands depend on what the image contains.

From this shell, you can investigate what went wrong: check which devices the kernel detected, examine kernel messages for errors, verify that the expected root device exists, and attempt a manual mount. The most common fixes involve:

  • Missing drivers: Regenerating the initramfs image to include the correct storage or filesystem modules.
  • Changed device names: Switching from device paths like /dev/sda2 to stable identifiers like UUID=... or LABEL=... in the bootloader configuration, since device names can shift when hardware changes.
  • Corrupted images: Booting from a live USB and regenerating the initramfs from a working environment.

Performance Considerations

The time spent in initramfs directly adds to boot time, so distributions optimize this phase:

  • Compression algorithm choice trades image size against decompression speed. LZ4, Zstandard, and XZ make different tradeoffs. The effect on boot time depends on the image contents, compression settings, CPU, and storage bandwidth; compare measurements for the target machine.
  • Module pruning via autodetect hooks keeps the image small by including only drivers for hardware that is actually present, rather than a generic set covering all possible hardware.
  • Parallel initialization with systemd in initramfs allows independent tasks (loading modules, starting udev, configuring network) to proceed simultaneously rather than sequentially.

For embedded systems or appliances with known, fixed hardware, it is possible to compile all necessary drivers directly into the kernel and skip initramfs entirely. This eliminates the decompression and early-userspace phase but sacrifices flexibility.

References

If you found this explanation helpful, consider sharing it with others.

Mastodon