Mount rootfs on Linux startup


1。 When Linux starts, after a series of initialization, the mount root file is requiredsystemTo prepare for the last init process, mount the root filesystemThere are several ways:

1) DocumentssystemIt already exists on a partition of the hard disk (or similar device). The kernel directly mounts it according to the command line parameters (root = / dev / xxx) started. Here is a problem in the root filesystemHow can the kernel find the corresponding device according to / dev / xxx when it does not exist? Note: root filesystemAnd other documentssystemThe mount method of is different. The kernel obtains the master and slave device numbers of the device by directly resolving the name of the device, and then can access the corresponding device driver. So / init.main There is a long string of root in C_ dev_ Names (such as HDA, hdab, SDA, SDB, NFS, ram, mtdblock…), through this table, you can get the device number according to the device name. Note that the boot parameter (root = / dev / xxx) set in the bootloader or kernel is only a code, which is the actual root filesystemThis device file does not necessarily exist in!

2) Load root files from slow devices such as floppy drivessystem, if the kernel supports ramdisk, load the root filesystemWhen the kernel judges that it needs to mount from the floppy disk (FDX) (root = / dev / fd0), it will automatically delete the filesystemThe image is copied to ramdisk, which generally corresponds to the device ram0, and then mount the root file on ram0system。 From the source code, if the kernel does not support ramdisk during compilation and the startup parameter is root = / dev / fd0,systemMount directly on the floppy disk. In addition to the slow speed, it is feasible in theory (I haven’t tried, I don’t know if it’s like this?)

3) Initrd is used to mount the root file at startupsystem。 Pay attention to understand the concepts of ramdisk and initrd. In fact, ramdisk is only a block device implemented on ram, similar to hard diskoperation, but with faster read and write speed, it cansystemUse at any time of operation, not just for startup; Initrd (boot loader initialized RAM disk) can be said to be a mechanism used in the startup process, and the specific implementation process also uses ramdisk technology. Before loading Linux, bootloader can put a relatively small root filesystemThe image of is loaded at a specified location in the memory. For the moment, this memory is called initrd (here is the memory occupied by initrd, not ramdisk, pay attention to the difference). Then bootloader tells the kernel the starting address and size of initrd by passing parameters (these parameters can also be compiled in the kernel). In the startup stage, initrd can be used to mount the root file temporarilysystem。 The initial purpose of initrd is to divide the startup of the kernel into two stages: keep the minimum and most basic startup code in the kernel, and then put the support for various hardware devices in initrd in a modular way, so that the root file of initrd mount can be used during the startup processsystemLoad the required modules in. One advantage of this is that different hardware can be flexibly supported by modifying the contents of initrd while keeping the kernel unchanged. In the final stage of startup completion, the root filesystemYou can remount to other devices, but you can also not remount (many embedded devices)systemThat’s it). The specific implementation process of initrd is as follows: bootloader puts the root filesystemLoad the image to the specified location in the memory and pass the relevant parameters to the kernel. When the kernel starts, copy the contents of initrd into ramdisk (ram0), release the memory occupied by initrd, and mount the root file on ram0system。 From this process, we can see that the kernel needs to support both ramdisk and initrd (these needs are programmed into the kernel and cannot be used as modules).

2。 EmbeddedsystemRoot filesystemAn implementation ofmethod: for kernel and root filessystemAre stored in flashsystemGenerally, you can use the initrd mechanism started by Linux. The specific process has been made clear earlier. Another point is to pass root = / dev / ram0 in the startup parameter, which makes the root file of Mount with initrdsystemNo more switching, because the actual device at this time is ram0. In addition, the starting address parameter of initrd is a virtual address, which needs to correspond to the physical address used in bootloader.