</>EmbeddedInterview

What is an initramfs and when is it needed?

MediumEmbedded Linux & Boot

Short answer: An initramfs is a small root filesystem packed as a cpio archive, which the kernel unpacks into a RAM-based filesystem (tmpfs/ramfs) and runs /init from before the real root filesystem is mounted. It is needed when mounting the real root needs work that the kernel alone cannot do: loading storage drivers built as modules, decrypting, assembling RAID/LVM, picking an A/B slot, or checking integrity.

How it works

  1. The bootloader loads the initramfs into RAM and passes its address (or it is built into the kernel image with CONFIG_INITRAMFS_SOURCE).
  2. The kernel unpacks it into rootfs (a tmpfs) early in boot.
  3. If /init exists, the kernel runs it as PID 1.
  4. /init does the setup, mounts the real root at e.g. /new_root, then calls switch_root (which deletes the initramfs contents to free RAM and execs the real /sbin/init).

Typical reasons to use one

  • Root is on a device whose driver is a module (common on generic distro kernels).
  • Encrypted root (LUKS/dm-crypt): unlock with a key from a TPM, OP-TEE or the user.
  • dm-verity for a verified read-only root.
  • A/B slot selection, factory reset, or a recovery shell.
  • Root over the network, iSCSI, or an overlay of a read-only image plus a writable partition.

initramfs vs the older initrd

initrd (old) initramfs
a block-device image (ext2) in a RAM disk a cpio archive unpacked into tmpfs
needs a filesystem driver and a fixed-size RAM disk no block layer, grows as needed
uses pivot_root uses switch_root

Embedded uses: some small products run entirely from an initramfs (the whole system in RAM, nothing writable on flash to get corrupted). Recovery/update images are often a kernel plus initramfs.

Cost: extra boot time to load and unpack it, and RAM usage. If you do not need it, booting straight to a root partition is faster.