The boot process of a Linux system involves a number of stages. These include the BIOS initialization, reading the MBR, the bootloader, kernel initialization and the init process. The initrd (initial ramdisk) plays a very significant role in booting up the system. This article discusses this initrd ramdisk and how we can create it if it needs to be recreated.
GRUB bootloader and initrd.
A bootloader is the first software program that runs during the startup process. It is responsible for booting the operating system. Bootloader loads the kernel into the memory and the kernel mounts the root partition so that it can start the first process, i.e. init process. Let us have a look at the commands that the bootloader executes for these tasks:
kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00
The first command "root (hd0,0)" tells the bootloader that kernel image is present on first partition of the hard disk hd0 (i.e. hd0,0). This is the location of the kernel image.
The second command is the kernel image itself. In this case, it is "/vmlinuz-2.6.18-238.el5". The arguments passed to this command are ro and root. ro specifies the read only mode and root tells the partition on which root filesystem resides. The kernel mounts this root partition in read-only mode.
The third command is the location of initrd. Now to understand what initrd is, let’s look at an issue that the kernel faces while mounting the root partition.
The Chicken Egg Module Problem.
We just saw that the kernel has to mount the root partition in read-only mode. But the root filesystem can be on a partition with one or more of following capabilities:
• Logical Volume Management (as in our case).
• Software RAID.
• Encrypted partition.
• SCSI controller support.
The kernel needs modules for these devices as they are not compiled into kernel itself. So, the kernel will load modules for these devices at runtime. These modules are present in “/lib/modules/” directory. This directory is present on the root filesystem itself and the root partition is not mounted yet (that is what this all is about). So, how can kernel access the modules for mounting the root partition residing on the root partition itself (without mounting it)?
Here initrd comes to rescue. The initrd ramdisk contains the modules required for mounting the root partition. This initrd resides on the same partition on which kernel image is present. So the kernel loads the initrd in memory, accesses the modules and mounts the root partition in read-only mode.
When the filesystem hardware or software changes, then we need to recreate the initrd. initrd can be created with “mkinitrd” command. The location of initrd is /boot directory. The kernel version for which the initrd image is being created needs to be passed as an argument to the mkinitrd command. The current kernel version can be checked with "uname -r" command:
[[email protected] ~]# uname -r
We can use this command to pass the required argument with command substitution:
[[email protected] ~]# mkinitrd /boot/initrd-latest.img $(uname -r)
The $(uname -r) will substitute the output of the command "uname -r" in its place.
The initrd image is a compressed image. You can check this with file command:
[[email protected] ~]# file /boot/initrd-latest.img
/boot/initrd-latest.img: gzip compressed data, from Unix, last modified: Fri Aug 3 10:47:47 2012, max compression