Kahibaro
Discord Login Register

initramfs

Role of initramfs in the Boot Process

initramfs (initial RAM filesystem) is a small, temporary root filesystem that the kernel loads into memory very early in the boot process. It provides just enough userspace environment and tools to:

Modern Linux systems normally use an initramfs image instead of the older initrd mechanism.

Key characteristics:

Once the real root filesystem is available and mounted, the initramfs hands off control and can be discarded (its memory is freed).

initramfs vs initrd

Although often used interchangeably, they are technically different mechanisms.

From a user/admin perspective, you mostly deal with “initramfs images” such as /boot/initramfs-<version>.img or /boot/initrd.img-<version>, even if the filename still says initrd—internally, it is typically an initramfs.

When initramfs Is Required

Some simple setups can boot without an initramfs (e.g., kernel has all required drivers built-in and root is a simple disk partition). However, an initramfs is effectively required when:

Most generic distro kernels assume an initramfs is present.

Typical initramfs Contents and Structure

An initramfs image contains:

Structure can differ by distro and tooling (dracut, initramfs-tools, mkinitcpio, etc.), but the existence of an /init entrypoint and minimal toolkit is common.

Lifecycle: What initramfs Actually Does

After the bootloader loads the kernel (and the initramfs image) and hands control to the kernel:

  1. Kernel initializes hardware basics
    • Sets up paging, basic drivers, and RAM disk support.
    • Decompresses the initramfs cpio archive into a tmpfs mounted at /.
  2. Kernel executes /init in the initramfs
    • This script or binary becomes PID 1 for the early stage.
    • It sets up /proc, /sys, /dev, and sometimes /run.
  3. Module loading and device discovery
    • Loads required kernel modules (e.g., storage, filesystem).
    • Starts udev or equivalent to populate /dev.
    • Waits for root device to appear if necessary.
  4. Prepare the real root filesystem
    • Assemble RAID arrays.
    • Activate LVM volume groups and logical volumes.
    • Unlock encrypted volumes (LUKS) with cryptsetup.
    • Optionally check or repair filesystem (basic checks).
  5. Mount the real root
    • Mounts the root filesystem (e.g., on /new_root or /root).
    • Optionally mounts other essential filesystems needed early.
  6. Switch root
    • Uses one of:
      • switch_root /new_root /sbin/init
      • pivot_root + exec /sbin/init
    • Control passes to the real system init (often systemd).
    • The initramfs filesystem is typically unmounted and freed.

From this point, normal system boot continues from the real root.

initramfs Generation Tools by Distribution

Distributions use different tools to generate their initramfs images, but the concepts are the same: scan the system, include needed modules and tools, then compress into an image in /boot.

Common tools:

The image is usually named something like:

Regenerating initramfs

Regeneration is needed when:

Examples (exact commands can differ with distro; check your distro’s chapter for details):

  dracut --force /boot/initramfs-$(uname -r).img $(uname -r)
  update-initramfs -u -k all
  # or just the current kernel:
  update-initramfs -u -k $(uname -r)
  mkinitcpio -P  # rebuild for all kernels defined in /etc/mkinitcpio.d
  # or rebuild for one preset:
  mkinitcpio -p linux

Hooks and Modules in initramfs

Most initramfs builders are driven by configuration files and hooks (or “modules” in the sense of logical units, not only kernel modules).

Typical configurable aspects:

These are usually controlled by:

Changing these configurations and running the generator again updates the initramfs image.

Inspecting and Debugging initramfs

When boot problems occur before the real root is mounted, the issue is often in the initramfs stage. Being able to inspect and debug it is crucial.

Inspecting the initramfs image

You can unpack an initramfs image for inspection (make a copy first):

  1. Identify compression (often gzip, xz, or lz4).
  2. Run something like:
   mkdir /tmp/initramfs-inspect
   cd /tmp/initramfs-inspect
   zcat /boot/initramfs-$(uname -r).img | cpio -idmv

Adjust zcat to xzcat, lz4 -d, etc., depending on compression.

You can then browse the extracted tree to see:

Enabling an early shell for debugging

Many initramfs systems support a debug shell if mounting root fails, or via boot parameters.

Typical kernel command-line options (varies by tooling and distro):

You can usually add these in the bootloader’s kernel parameters (e.g., in GRUB’s edit menu). Once in a shell provided by initramfs:

Common initramfs-related Problems

Typical classes of issues:

In all such cases, inspecting the initramfs contents and using a debug shell can help isolate the problem.

Customizing initramfs for Advanced Use Cases

Once you understand the basics, you can extend initramfs for more complex scenarios:

These use cases typically rely on custom hooks or a custom /init, and a carefully minimized set of binaries and libraries to keep the image small and boot time fast.

Performance and Size Considerations

Because initramfs is loaded into RAM and decompressed at every boot:

Most initramfs generators allow you to:

Choosing the right trade-off depends on whether the image must boot multiple hardware profiles or a single known machine.

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!