What is a bootable RAM disk?

Bootable RAM disk (Initrd) is the image of the root filesystem that is mounted before the real root filesystem will be available. Initrd associated with the kernel and loaded as part of the kernel at boot time. The kernel mounts the initrd image which contains the necessary modules to mount the root fs and has the further transition to the root as a major.

Initrd literally means “boot loader initialized RAM disk”, in other words, this is a special RAM disk, before loading the Linux kernel, to be initialized by the boot loader, concrete actions from a particular storage device is loaded initrd into RAM (from the boot parameter “initrd=” specify the image of the physical or logical location), then linux kernel is loaded and executed, it priority placed initrd memory space, and this basically a file system space, init and other programs often include, it can be used to mount some of the special driver, for example, SCSI, after the completion of milestones, kernel will mount the real root file system and execute /sbin/init program.

Anyway, why we need such a roundabout way to start it?

The reason is, root file system (by the boot parameter “root=” is specified, hereinafter referred to as rootfs) where the storage devices is likely to be extremely difficult to find, for example, SCSI device requires complex and time-consuming process, if the system is using RAID need to see a configuration may be, the same problem also occurs in the USB storage, because it would take much longer to wait for kernel and configuration time, or that the remote mount rootfs, not only have to deal with network devices, or even have consider the relevant server authentication, communication round-trip time and other issues. More importantly, we can put some special programs initrd, mount rootfs as a preparation for, say, the hardware initialization, decryption, decompression, etc., and secondly, prompt the user or system administrator is currently state, which for consumer electronics products, have great significance. Overall, if we can increase the power of flexibility (for example, with a simple shell script to achieve USB / SCSI initialization, if implemented through the kernel code to do), but also moderately reduce the kernel image of the design complexity and space usage, take initrd is a very good way, so almost every Linux distribution, are provided with initrd, to address the different hardware and different boot device, technical issues, but also CD-ROM/DVD ensure a variety of personal computer system can be loaded, but also supports [ bootsplash ] A class boot animation program.

initrd provides a “two-stage boot” program. First of all, everything is still in kernel mode, completed by the kernel and hardware-related initialization, and then, at the appropriate time point, when the kernel to read and where to mount initrd memory after the file system, kernel for the first time cut into the kernel space from user space, stored in RAM disk to perform the init program, of course, this requires a complete execution environment (for example, C runtime or necessary program loader, etc.), also have to determine the kernel of the rootfs can be found and properly mounted. When the first phase of the initrd into the end, then back to kernel mode, initrd memory space where appropriate will also be released (based on the configuration may be), this second phase, which is in the implementation of the real rootfs The init program. In the Linux kernel 2.4, initrd general processing flow is as follows: (square brackets indicate that the implementation of the main unit).

[Boot loader] Boot loader based on predetermined conditions, the kernel and initrd image is loaded into the two RAM
[Boot loader -> kernel] to complete the necessary action, ready to hand over the implementation of Linux kernel
[Kernel] a series of initialization, initrd memory is where the kernel corresponds to /dev/initrd device equipment, through the kernel inside the decompressor (gzip decompression) to unlock the content and copy / dev/ram0 device equipment on
[Kernel] Linux to R / W (write) mode /dev/ram0 mount rootfs for the temporary
[Kernel-space -> user-space] kernel for execution /dev/ram0 on /linuxrc program, and switch the implementation process
[User space] /linuxrc and related programs to deal with specific operations, for example, says it is prepared to mount rootfs, etc.
[User-space -> kernel-space] /linuxrc is about to complete the implementation, enforcement powers transferred to the kernel
[Kernel] Linux mounts the real rootfs and run /sbin/init program
[User space] based Linux distribution standard process, the implementation of various systems and applications.

It is worth mentioning that the above “two-stage power” is made of elastic initrd boot process, in real applications, it may never really need to mount rootfs, in other words, just as all the systems operate in the RAM disk or never performed in the guided initrd /linuxrc program execution (Note: kernel always retained PID = 1 as the init process to identify, and /linuxrc PID implementation will not be 1), in many devices such as smart phones, are was for several years, but this does not affect our subsequent discussion.

Linux Kernel development culture is willing to give up doing both real and bold new approach (in line with international specifications, under the premise), Linux 2.6 the reason for making initramfs, initrd is to amend the various technical problems. Does the problem lie? First, the review process just discussed, initrd RAM disk is on the kernel itself is a real block device, in order to construct one of the files stored (at least have a /linuxrc), usually a class we need to ext2 file system (recommended). So, to construct such a initrd image point of view, usually through mkfs.ext2 and losetup (function: “set up and control loop devices”) and other tools to create loopback device and editing, it is natural to face the following problems:

initrd a file system implementation must be bound to do, such as ext2, but the majority of cases, we do not need at this stage to do a complete implementation.
/dev/initrd block device that has limited space when building, maintaining cumbersome
Stage of the operation in the initrd, file operations are actually constantly /dev/initrd (corresponding to a certain memory) can access the file system corresponding to the memory address, do the unnecessary consumption of resources.

Kernel documentation (Documentation /filesystems/ramfs-rootfs-initramfs.txt) further pointed out that:

Advertisements