views:

87

answers:

3

The linux kernel uses a kind of ram disk to access modules at an early boot stage. Out of curiosity I want to understand I have a question: The file containing the initramfs/initrd must be accessible for the kernel at this stage. This means the kernel must have support for the appropriate filesystem. So why couldn't the content of the initramfs just be in the very same filesystem as plain files?

+1  A: 

It can be. An initrd image is not needed to boot, but most use it. What this does, however is allows you to have different setups for your filesystems. For example, an initrd is usually required when using a non-ext2 filesystem (NFS? XFS?), when using RAID, or when hosting the root filesystem on an unusual device (USB drive? CDROM with unionfs?).

This can be avoided, of course, by compiling the proper filesystem or RAID drivers into your kernel, but this will make your kernel larger.

Dark Falcon
Concise and well-said! I learned something.
John Feminella
+5  A: 

The file containing the initramfs/initrd must be accessible for the kernel at this stage.

This seems to be the heart of your confusion: it's not the kernel that reads the initramfs/initrd, it's the bootloader! The kernel does not need to know how to access the initramfs/initrd. That's the whole point of the exercise!

The bootloader loads the initramfs/initrd into memory, and there is a well-defined communication protocol which allows the bootloader to tell the kernel at what memory address it loaded the initramfs/initrd.

In the case of initramfs, the initramfs image(s) may also be appended to the kernel image.

Jörg W Mittag
+1  A: 

First a bootloader (probably LILO or Grub) uses BIOS calls to read the kernel image from your boot drive. Then it passes control to the kernel it just placed in system RAM.

The kernel does not use BIOS calls to access disks. As the kernel is booting up, it scans the system for devices, and loads device driver modules for each device in the system, mounts the root filesystem and launches the first user process.

But wait: how did the kernel load the device driver module for the disk subsystem get loaded? There's a chicken-and-egg problem here, where you need to access the disk to get the device driver that allows you to access the disk.

The solution is to put all those drivers in the initial ramdisk (initrd); that ramdisk image is read by the bootloader and placed in RAM alongside the kernel, and that gives the kernel the set of drivers needed to get to the root filesystem.

Without this, you would need to have all of the drivers needed to access the root filesystem built in to the kernel.

Eric Seppanen