Linux initrd (linuxrc, init)

Source: Internet
Author: User
Linux initrd (linuxrc, init)

From Linux Oracle Baidu Space

Linux's initrd technology is a very common mechanism. The file format of initrd in the Linux kernel is changed from the original file system image file to the cpio format, and the changes are not only reflected in the file format, the Linux kernel processes initrd in these two formats differently. This article first introduces what is initrd technology, and then introduces the initrd processing process of the linux2.4 kernel and the 2.6 kernel respectively. Finally, through the analysis of some codes processed by initrd in linux2.6 kernel, the reader can
The initrd technology has a comprehensive understanding. To better read this article, you must have a preliminary understanding of Linux VFS and initrd.

 

1. What is initrd?

The English meaning of initrd is Boot Loader initialized RAM disk, which is the memory disk initialized by boot loader. Before the Linux kernel starts, Boot Loader loads the initrd file in the storage medium to the memory. When the kernel starts, it accesses the initrd File System in the memory before accessing the real root file system. When boot loader is configured with initrd, the kernel startup is divided into two phases. In the first phase, the system executes "a file" in the initrd File System to load the driver module and other tasks, in the second stage
/Sbin/INIT process. The "file" mentioned here, the Linux kernel is different from the previous kernel version, so the "file" is used for the time being, which will be detailed later. The purpose of Phase 1 startup is to clear all obstacles for Phase 2 startup. The most important thing is to load the driver module of the root file system storage medium. We know that the root file system can be stored on a variety of media, including IDE, SCSI, and USB. If the drivers of these devices are compiled into the kernel, we can imagine how huge and bloated the kernel will be.

Initrd has the following functions:

1. essential components of the Linux release

The Linux release must adapt to different hardware architectures. It is unrealistic to compile all drivers into the kernel. The initrd technology is the key technology to solve this problem. The Linux release only compiles the basic hardware driver in the kernel. During the installation process, it detects the system hardware and generates the initrd that includes the system hardware driver, it is nothing more than a flexible solution.

2. essential components of livecd

Compared with the Linux release, livecd may face more complex hardware environments, so initrd must also be used.

3. initrd must be used to create a Linux USB boot disk.

A USB device is a device that is relatively slow to start. It takes several seconds to load the driver to make the device available. If the USB driver is compiled into the kernel, the kernel usually cannot successfully access the file system on the USB device. Because the USB device is not initialized when the kernel accesses the USB device. Therefore, the general practice is to load the USB driver in initrd, then sleep for a few seconds, wait until the USB device is initialized and then mount the file system in the USB device.

4. You can easily enable personalized bootsplash in the linuxrc script.


2. Process of initrd processed by linux2.4 Kernel

To give readers a clear understanding of the changes in the initrd mechanism of the linux2.6 kernel, before focusing on the initrd of the linux2.6 kernel, let's give a brief introduction to the initrd of the linux2.4 kernel. The initrd format of the linux2.4 kernel is a file system image file. This document calls it image-initrd to distinguish the cpio format of the Linux kernel. The initrd processing process of the linux2.4 kernel is as follows:

1. Boot Loader loads the kernel and/dev/initrd content to the memory./dev/initrd is a device initialized by boot loader and stores initrd.

2. During kernel initialization, the kernel decompress the/dev/initrd device and copy it to the/dev/ram0 device.

3. the kernel can read and write the/dev/ram0 device as the original root file system.

4. If/dev/ram0 is specified as the real root file system, the kernel will be started normally until the last step.

5. Run the/linuxrc file on initrd. linuxrc is usually a script file that loads the drivers required to access the root file system by the kernel and loads the root file system.

6./after executing linuxrc, the real root file system is mounted.

7. If the real root file system has the/initrd directory,/dev/ram0 will be moved from/to/initrd. Otherwise, if the/initrd directory does not exist,/dev/ram0 will be uninstalled.

8. Run/sbin/init during normal startup on the real root file system. The initrd execution of the linux2.4 kernel is an intermediate stage of kernel startup. That is to say, after the initrd/linuxrc is executed, the kernel will continue to execute the initialization code, we will see that this is a significant difference between the initrd processing process of the linux2.4 kernel and the 2.6 kernel.





3. The initrd processing process of linux2.6 Kernel

The linux2.6 kernel supports two formats of initrd. One is the traditional format file system image-initrd of the linux2.4 kernel described in section 3rd, it is created in the same way as the initrd of the linux2.4 kernel. Its core file is/linuxrc. In another format, initrd is in cpio format. initrd is introduced from linux2.5 and is generated using the cpio tool. Its core file is no longer/linuxrc, but/init, this document describes
Initrd is called cpio-initrd. Although the linux2.6 kernel supports both cpio-initrd and image-initrd formats, there are significant differences in the processing process, the following describes how the Linux kernel processes these two initrd types.

Cpio-initrd processing process

1. Boot Loader loads the kernel and initrd file to a specific location in the memory.

2. the kernel determines the initrd file format, for example, cpio format.

3. Release initrd content to rootfs.

4. Execute the/init file in initrd. After this is done, all kernel work is completed and handed over to the/init file for processing.

Process of image-initrd

1. Boot Loader loads the kernel and initrd file to a specific location in the memory.

2. the kernel determines the initrd file format. If it is not in cpio format, it will be processed as image-initrd.

3. the kernel stores initrd content in the/initrd. Image File under rootfs.

4. the kernel reads/initrd. image into the/dev/ram0 device, that is, a memory disk.

5. Then, the kernel can read and write the/dev/ram0 device as the original root file system.

6 .. If/dev/ram0 is specified as the real root file system, the kernel will jump to the last step to start normally.

7. Run the/linuxrc file on initrd. linuxrc is usually a script file that loads the drivers required to access the root file system by the kernel and loads the root file system.

8./after linuxrc is executed, the general root file system is mounted.

9. If the regular root file system has the/initrd directory,/dev/ram0 will move from/to/initrd. Otherwise, if the/initrd directory does not exist,/dev/ram0 will be uninstalled.

10. Run/sbin/init during normal startup on the regular root file system.

The process described above shows that the image-initrd processing process of the linux2.6 kernel has not changed significantly compared with that of the linux2.4 kernel, cpio-initrd processes differ greatly from image-initrd processes. The process is very simple. In the source code analysis, readers can better understand the simplicity of the process.

4. Differences and advantages between cpio-initrd and image-initrd

I have not found a formal document comparing cpio-initrd with image-initrd. Based on the author's experience and kernel code analysis, I have summarized the following differences, these differences are also the advantages of cpio-initrd:

Making cpio-initrd easier

Cpio-initrd is easy to create. the entire production process can be completed using two commands.

# Assume that the current directory is in the root directory of the prepared initrd File System bash # Find. | cpio-c-o> ../initrd. imgbash # gzip ../initrd. img


The traditional initrd production process is cumbersome and requires the following six steps:

# Assume that the current directory is in the root directory of the prepared initrd File System bash # dd If =/dev/Zero of = .. /initrd. img bs = 512 k count = 5 bash # mkfs. ext2-F-M0 .. /initrd. imgbash # Mount-T ext2-o loop .. /initrd. IMG/mntbash # cp-R */mntbash # umount/mntbash # gzip-9 .. /initrd. IMG


This article does not give a detailed explanation of the meaning of the above commands, because this article mainly introduces how the Linux kernel processes initrd. Readers who do not understand the above commands can refer to the relevant documentation.

Cpio-initrd kernel processing process is simpler

Through the introduction of the initrd processing process above, the cpio-initrd processing process is extremely simple. Through comparison, we can see that the cpio-initrd processing process is simplified in the following two aspects:

1. cpio-initrd does not use an additional ramdisk, but inputs its content into rootfs. In fact, rootfs itself is also a memory-based file system. In this way, steps such as attaching and detaching ramdisk are eliminated.

2. cpio-initrd after the/INIT process is started, the kernel task is finished, and the rest of the work is completely handed over to/init. for image-initrd, after the kernel finishes the/linuxrc process, some final work should also be done, and the/sbin/init of the real root file system should be executed. Figure 1 shows the differences in the processing process:


Figure 1 kernel processes cpio-initrd and image-initrd

Cpio-initrd is more important

1. cpio-initrd is no longer an intermediate step for Linux kernel startup as image-initrd, but an end point for Kernel startup, after the kernel has handed the control permission to the/init file of cpio-initrd, the kernel task is finished. Therefore, we can do more work in the/init file, it is not worried about the title connection problem with the subsequent processing of the kernel. Of course, the content of the cpio-initrd/init file in Linux has not changed in essence, but I believe that the increase in initrd's responsibilities must be a trend.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.