Article Title: proficient in building initramfs stepbystep (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I. What is init ramfs?
The Linux kernel version 2.6 contains a compressed cpio package file. When the kernel starts, the file will be exported from the package file to the rootfs File System of the kernel, and then the kernel checks whether the rootfs contains the init file. If yes, execute it, as the first process of PID 1. This init process will be responsible for subsequent work of starting the system, including locating and attaching "real" root file system devices (if any ). If the kernel does not find the init file in rootfs, the kernel locates and mounts the root partition in the previous version, and then runs the/sbin/init program to complete subsequent system initialization.
The compressed cpio package file is init ramfs. When compiling the 2.6 Linux kernel, the compiling system always creates the init ramfs and connects it with the compiled kernel. The usr directory in the kernel source code tree is used to build the init ramfs in the kernel. The ini tramfs_data.cpio.gz file is init ramfs. By default, init ramfs is empty, and the file size in X86 architecture is 134 bytes.
2. Construct the first init ramfs: hello world
Since the C language, the first program to learn computer programming languages is almost all hello world. Therefore, we also build the simplest hello world init ramfs, to illustrate the basic construction method of init ramfs.
The soul of init ramfs is the init file (or program, because it will be first executed by the kernel). Let's first write a simple init program, it prints the classic hello world Information in the kernel console.
Hello. c:
# Include
# Include
Int main (int argc, char argv [])
{
Printf ("hello world, from init ramfs. \ n ");
Sleep (9999999 );
Return 0;
}
The sleep () function statement is used to avoid panic information quickly generated by the kernel during execution. It is not a functional requirement.
Then compile hello. c into a static Connection Program:
Gcc-o hello_static-static-s hello. c
The-s parameter in the command line indicates that the compiled program does not contain debugging positioning information, so as to reduce the size of the compiled program file.
Create another init ramfs build source file directory image, and put the hello_static program into this directory and rename it init.
In the image directory, create a dev/console device file. No init program cannot output information in the kernel console:
Mknod-m 600 dev/console c 5 1
Note: You must have the root permission to execute this command.
Now you can set the Kernel configuration parameters to build the init ramfs:
Enter the path name of the image under the init ramfs sources configuration item under the general setup configuration directory. For example, the path of my path is/home/wyk/init ramfs-test/image. Because our init program is in the ELF format, the kernel needs to support the ELF executable file, otherwise it will fail to start this init program. In the Executable file formats configuration directory of the kernel, select kernel support for ELF binaries to enable the kernel to support Executable files in ELF format. Other Kernel configuration parameters can be set according to actual needs. However, to reduce the kernel Compilation Time, refer.
After the Kernel configuration parameters are set, compile according to the general kernel compilation method. The init ramfs will automatically connect to the compiled kernel image file.
Iii. Test Environment Construction
Testing init ramfs requires frequent system restart, so using the CPU simulator is a good choice. We can use qemu to directly start the Linux kernel without installing the OS in the simulator. For ease of use, we use qemu launcher to set qemu parameters.
In the linux Configuration tab of qemu launcher, check to start linux directly, and fill in the path name of the compiled kernel image file in the text box below. Because qemu still needs to set the hard disk image file to run, we also need to create a new hard disk image file in the configuration tab on the left, but we do not actually use the hard disk.
After configuring the qemu parameters, click the launcher button to run the kernel in qemu. After the kernel outputs a bunch of kernel running information
Hello world, from init ramfs.
Haha, The init ramfs we have built can work normally!