Understanding Linux initrd – Initial RAM Disk
initrd (initial RAM disk) is a temporary root file system that is mounted during system boot to support the two-state boot process. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The initrd contains various executables and drivers that permit the real root file system to be mounted, after which the initrd RAM disk is unmounted and its memory freed. In many embedded Linux systems, the initrd is the final root file system.
The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel.
How initrd Works:
initrd provides the capability to load a RAM disk by the boot loader. This RAM disk can then be mounted as the root file system and programs can be run from it. Afterwards, a new root file system can be mounted from a different device. The previous root (from initrd) is then moved to a directory and can be subsequently unmounted.
When using initrd, the system typically boots as follows:
1) The boot loader loads the kernel and the initial RAM disk
2) The kernel converts initrd into a “normal” RAM disk and frees the memory used by initrd
3) initrd is mounted read-write as root
4) /linuxrc is executed (this can be any valid executable, including shell scripts; it is run with uid 0 and can do basically everything init can do)
5) linuxrc mounts the “real” root file system
6) linuxrc places the root file system at the root directory using the pivot_root system call
7) The usual boot sequence (e.g. invocation of /sbin/init) is performed on the root file system
8) The initrd file system is removed
The main motivation for implementing initrd was to allow for modular kernel configuration at system installation. The procedure would work as follows:
1) system boots from floppy or other media with a minimal kernel (e.g. support for RAM disks, initrd, a.out, and the ext2 FS) and loads initrd
2) /linuxrc determines what is needed to (a) mount the “real” root FS (i.e. device type, device drivers, file system) and (b) the distribution media (e.g. CD-ROM, network, tape, …). This can be done by asking the user, by auto-probing, or by using a hybrid approach.
3) /linuxrc loads the necessary kernel modules
4) /linuxrc creates and populates the root file system (this doesn’t have to be a very usable system yet)
5) /linuxrc invokes pivot_root to change the root file system and execs – via chroot – a program that continues the installation
6) the boot loader is installed
7) the boot loader is configured to load an initrd with the set of modules that was used to bring up the system (e.g. /initrd can be modified, then unmounted, and finally, the image is written from /dev/ram0 or /dev/rd/0 to a file)
8) now the system is bootable and additional installation tasks can be performed