Part 4 of 7 · 3 min read
Boot loader and kernel
Think of a moving van that carries the first few boxes so that the movers can get into the house and bring in the rest. The boot loader and the initial RAM file system carry the first files that the kernel needs. The analogy stops in one place: the van leaves after the move, and the initial RAM file system is dropped once the real root file system is mounted.
In plain words: The boot loader's task is to find the kernel file on disk and start it. On Linux, GRUB is a common boot loader. It reads a configuration file, can show a menu, and lets the user choose among installed kernels or change kernel settings at boot time.
The key lines.
The settings that reach the kernel appear in a file on a running system. Reading /proc/cmdline shows them. A typical line looks like this (made up to show the idea):
BOOT_IMAGE=/boot/vmlinuz-5.15.0-91-generic root=UUID=5f2e2232-4e47-4fe8-ae94-45ea749a5c92 ro quiet
BOOT_IMAGE is the kernel file that was loaded. root names the disk area that holds the root file system, the top of the file tree. ro mounts it read-only at first, and quiet cuts down on messages.
Linux kernel images are compressed to save space and speed up loading. After the loader places the image in memory, it unpacks itself. A puzzle follows. The kernel needs drivers to read the real root file system. But the drivers are files, and they live on that file system. The way out is an initial RAM file system (initramfs), a small temporary file system that the loader puts in memory beside the kernel. It holds the drivers and modules needed to reach the real disk. Once those are loaded, the kernel switches its root from the temporary file system to the real one on disk.
After that, the kernel starts its first program, which is covered in the next section.
Android differs a little. It uses a Linux-based kernel and the same compressed image and initial RAM file system. It does not use GRUB, and leaves boot loaders to device makers, with a common choice called LK (little kernel). It also keeps the initial RAM file system as the root file system, where Linux discards it.
A worked example.
A worked sequence (made up to show the idea). The kernel image is 12 MB compressed. After unpacking, it is 40 MB in memory. The initramfs is 30 MB. Memory used before the real disk is reachable: 40 + 30 = 70 MB. The initramfs is freed after the switch, giving back 30 MB.
Initial RAM file system
An initial RAM file system (initramfs) is a small temporary root file system that the boot loader loads into memory together with the kernel. It holds the drivers and tools the kernel needs to find and mount the real root file system, and it is discarded once that is done.
Example: it contains the disk driver, so the kernel can mount the real root file system from a disk it could not otherwise read.
Check yourself:
1. What does the root= setting tell the kernel?
2. Why is an initial RAM file system needed?
3. Which part reads the compressed kernel image and puts it in memory?