Build an Initramfs by Hand and Boot It in QEMU on Linux

Build a minimal initramfs with a static BusyBox, cpio and gzip, then boot it with a kernel in QEMU to see how early userspace starts.

Embedded Published 4 min read

Contents

An initramfs is a compressed cpio archive that the kernel unpacks into memory and uses as its first root filesystem. This guide builds a tiny one by hand, with a static BusyBox and a short /init script, and boots it in QEMU so you can watch early userspace start.

How the kernel uses an initramfs

According to the kernel documentation, the kernel extracts the archive into rootfs and then looks for a file named init. If it finds one, it runs it as PID 1. If there is no /init, the kernel falls back to older code that finds and mounts a real root partition.

The kernel documentation describes the initramfs as a gzipped cpio archive, and its packing recipe uses cpio -H newc. The same page says init is not expected to return to the kernel, so our script will power the virtual machine off instead.

Install the tools

This guide is tested on Debian and Ubuntu. You need a kernel to boot, a statically linked BusyBox (so no shared libraries have to be copied in), cpio, file, and QEMU. Installing the distribution kernel package places a kernel image in /boot.

✓ tested · debian
apt-get update
apt-get install -y linux-image-amd64 busybox-static cpio file qemu-system-x86
✓ tested · ubuntu
apt-get update
apt-get install -y linux-image-virtual busybox-static cpio file qemu-system-x86

On other distributions the package names differ, and the kernel image may live elsewhere, so adjust the paths in the later steps. If you target a different CPU architecture, the ARM cross-compile guide shows one way to build binaries for another machine.

Create the root directory tree

The initramfs needs only a few directories and one binary. BusyBox bundles many tools into one executable, so we copy it in and create symlinks for the commands the init script uses.

✓ tested · debian,ubuntu
mkdir -p /tmp/initramfs/root
cd /tmp/initramfs/root
mkdir -p bin proc sys dev
cp /bin/busybox bin/busybox
for tool in sh mount echo cat uname poweroff; do
  ln -s busybox bin/$tool
done
file bin/busybox

The file output should say the binary is statically linked.

Write the init script

/init is an ordinary script run by the BusyBox shell. It mounts the virtual filesystems that most programs expect, prints a few lines so you can see it ran, and powers off. To see which filesystems a running system has mounted, the lsblk, blkid and findmnt guide covers findmnt.

✓ tested · debian,ubuntu
cat > /tmp/initramfs/root/init <<'EOF'
#!/bin/busybox sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
echo "Hello from the initramfs"
uname -r
cat /proc/cmdline
poweroff -f
EOF
chmod +x /tmp/initramfs/root/init

Pack it with cpio and gzip

The kernel documentation gives the packing recipe: list files with find, feed them to cpio -H newc, and compress with gzip. It warns against find -depth, because the kernel extractor cannot create a file in a directory that does not exist yet.

✓ tested · debian,ubuntu
cd /tmp/initramfs/root
find . | cpio -o -H newc | gzip > /tmp/initramfs/initramfs.cpio.gz
ls -l /tmp/initramfs/initramfs.cpio.gz
zcat /tmp/initramfs/initramfs.cpio.gz | cpio -it

The last command lists the archive contents, which is a quick way to confirm init sits at the top level. For more on archive and compression tools, see the tar guide.

Boot it in QEMU

QEMU can load a kernel directly with -kernel, an initial RAM disk with -initrd, and a kernel command line with -append. With -nographic, QEMU redirects the emulated serial port to the console, and the command line below sets console=ttyS0. -no-reboot makes QEMU exit instead of rebooting.

✓ tested · debian,ubuntu
KERNEL=$(ls /boot/vmlinuz-* | sort | tail -n 1)
timeout 240 qemu-system-x86_64 -m 512 -nographic -no-reboot \
  -kernel "$KERNEL" \
  -initrd /tmp/initramfs/initramfs.cpio.gz \
  -append "console=ttyS0 panic=-1" < /dev/null > /tmp/initramfs/boot.log 2>&1
grep -a "Hello from the initramfs" /tmp/initramfs/boot.log

QEMU also has an -enable-kvm option for KVM full virtualization, available only if KVM support was compiled in. You should see the usual kernel messages, then the lines from the script:

Hello from the initramfs
<kernel version string>
console=ttyS0 panic=-1

The exact kernel version depends on the package you installed. To get a shell instead of a poweroff, change the last line of init to exec sh, or boot with rdinit=/bin/sh on the command line, which the kernel documentation mentions as the initramfs counterpart of init=/bin/sh. That is an interactive session, so it is not part of the tests.

qemu-system-x86_64 -m 512 -nographic -no-reboot \
  -kernel /boot/vmlinuz-* \
  -initrd /tmp/initramfs/initramfs.cpio.gz \
  -append "console=ttyS0 rdinit=/bin/sh"

Where to go next

A real initramfs ends by handing off to another root. The kernel documentation says /init can do this by overmounting / with a new root device and executing another init program. If you want a full embedded system rather than a hand-built one, the Buildroot guide builds a kernel and root filesystem for QEMU.

Sources

  1. Ramfs, rootfs and initramfs (Linux kernel documentation)
  2. QEMU System Emulator invocation documentation

#Initramfs #Qemu #Cpio #Busybox #Embedded

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.