Linux file system initialization process (6)---execute the init program

First, the purpose

    After the kernel loads the initrd file, the necessary preparations are made for mounting the disk file system, including mounting the sysfs and proc file systems, and loading the disk driver driver. Next, the kernel jumps to the init program in the user space, and init completes the creation of disk device files, loading the disk file system, and switching from rootfs to the disk root file system.

    Because in different Linux distributions, the implementation of init is very different, and it is impossible to analyze all the distributions, so this article selects the ubuntu12.04 distribution to describe how to switch from rootfs to the disk root file system.

 

Second, create a disk device file

    The init program uses the udev tool to dynamically create disk device files. The working principle of udev is to create corresponding device files in the /dev directory according to the device information in sysfs, so the sysfs file system needs to be prepared in advance.

    First, create the necessary mount point directories /dev, /root, /sys, /proc, etc.; then, mount the sysfs in the VFS to the /sys directory of the rootfs, and mount the tmpfs to the /dev directory (/ The file system type of dev is tmpfs); finally, in order to output the printing information, two special device files of /dev/console and /dev/null are created.

    After the necessary information is prepared, the udev background process can be started, and the disk device file can be dynamically created by udev according to sysfs. The Udev startup code is in scripts/init-top/udev.

 

 

 

Three, mount the disk file system

    There are generally two ways to mount a disk file system: local way and network way. According to the value of the BOOT variable, init chooses to perform local loading or network loading, if it is local loading, it executes the /scripts/local script; if it is network loading, it executes the /scripts/nfs script. Personal PCs are generally loaded locally, and servers in data centers are generally loaded by nfs.

    Finally, the /scripts/local script is called by the init program to mount the disk file system.

 

 

Fourth, switch the root file system

    After successfully mounting the disk file system, you need to migrate important directories such as /sys, /proc, and /dev under rootfs to the disk file system.

    Finally, the root file system of the kernel is switched from rootfs to the root directory of the disk file system by calling the /sbin/run-init program.

 

 

Five, the final VFS view

    So far, the kernel file system initialization process has been completed. The final VFS view is given below (because the file system is too large, only the key topological structure is given):

 

Six, summary

    The main job of the init program is to load the disk file system, migrate important directories under rootfs to the disk file system, and finally switch the kernel root directory from rootfs to the root directory of the disk file system.

 

Guess you like

Origin blog.csdn.net/daocaokafei/article/details/114873134