The kernel and the initramfs

Video 066 of 140 in Virtualization Foundations, in the unit “The boot sequence, end to end”. 4:06, with English and Hindi audio.

The boot loader has handed control to the Linux kernel. The kernel faces a catch: to read a disk, it needs a driver for the disk controller, and that driver is a file stored on the same disk. The initramfs solves this problem. It is a small temporary filesystem in memory that carries the drivers needed to reach the real disk. This article explains what the kernel does in order, and how to look at the initramfs on Ubuntu.

What you will learn

The catch: the driver is on the disk

Linux needs a driver for the controller that talks to its disk. Without that driver, the kernel cannot read anything on the disk.

The problem is that drivers are files, and they are stored on that same disk. The kernel needs a driver to reach the disk, and it needs the disk to reach the driver.

The initramfs: a temporary root in memory

The way out is the initramfs. It is a small filesystem held in memory, and it carries only the drivers and tools needed to find the real disk. The kernel works through these steps in order.

Two roots, one after the other

Picture the boot as two roots. The temporary root comes first, and it gives the kernel the drivers it needs.

Once the real disk is found and mounted, the machine switches to the real root. The temporary one is no longer needed. The real root holds your programs, settings and data, and everything after this point lives there.

The kernel then starts systemd, the service manager, from the real root disk. From here, systemd takes charge of the rest of the boot.

Looking at the kernel and initramfs on Ubuntu

On Ubuntu, the boot folder keeps a kernel file and an initramfs for each installed version. The commands below show the running version, the files in the boot folder, and what is packed inside the initramfs. The last command rebuilds the initramfs image.

Try it yourself

You need a shell on an Ubuntu machine. The first three commands only read information. The last one rebuilds the initramfs image, so run it with care.

  1. Show the kernel version that is running now.

    uname -r

    What you should see: The kernel version that is running now.

  2. List the boot folder. It keeps a kernel file and an initramfs for each installed version.

    ls /boot

    What you should see: A kernel file and an initramfs for each installed version.

  3. List the first lines of what is packed inside the initramfs for the running kernel.

    lsinitramfs /boot/initrd.img-$(uname -r) | head -20

    What you should see: The files packed into the initramfs: drivers, small scripts and tools.

  4. Rebuild the initramfs image.

    sudo update-initramfs -u

    What you should see: The initramfs image is rebuilt.

Terms

Kernel
The core of Linux. It starts the hardware, loads the drivers and mounts the root disk.
Driver
A program that lets the kernel talk to a piece of hardware, such as a disk controller.
Initramfs
A small temporary filesystem in memory that carries the drivers and tools needed to find the real disk.
UUID
A unique ID stored on a filesystem, which the kernel uses to find that disk at boot.

Check yourself

Why can the kernel not read the driver it needs from the disk itself?

The driver is a file on that disk, and the kernel needs the driver before it can read the disk.

How does the kernel find the root disk?

It finds the disk by its UUID, a unique ID stored on the filesystem, and then mounts it.

What is the initramfs for?

It is a small temporary filesystem in memory that carries the drivers and tools needed to reach the real disk.

Previous: What the boot loader does. Next: What systemd does at boot.