Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to mount rootfs when linux starts

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to mount rootfs when linux starts". In daily operation, I believe many people have doubts about how to mount rootfs when linux starts. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to mount rootfs when linux starts". Next, please follow the editor to study!

one. When linux starts, after a series of initialization, you need to mount the root file system to prepare for the final run of the init process. There are several ways for the mount root file system to run:

1) the file system already exists on a partition of the hard disk (or similar device), and kernel mount directly according to the startup command line argument (root=/dev/xxx). Here is the question: how can kernel find the corresponding device based on / dev/xxx when the root file system itself does not exist? Note: the mount mode of the root file system is different from that of other file systems. Kernel obtains the master and slave device numbers of the device by directly parsing the device name, and then you can access the corresponding device driver. So there is a long list of root_dev_names (such as hda,hdab,sda,sdb,nfs,ram,mtdblock) in init/main.c. Through this table, you can get the device number based on the device name Note that the boot parameter (root=/dev/xxx) set in the bootloader or kernel is only a code name, and this device file does not necessarily exist in the actual root file system!

2) load the root file system from the floppy drive and other slow devices. If kernel supports ramdisk, when loading the root file system, the kernel determines that it needs to mount (root=/dev/fd0) from the floppy disk (fdx), it will automatically copy the file system image to ramdisk, which generally corresponds to the device ram0, and then mount the root file system on the ram0. From the source code point of view, if kernel does not support ramdisk when compiling, and the startup parameter is root=/dev/fd0, the system will mount directly on the floppy disk, except that the speed is relatively slow, it is theoretically feasible (I have not tried, I do not know if this is the case? )

3) initrd is used to mount the root file system when starting. Pay attention to understand the two concepts of ramdisk and initrd, in fact, ramdisk is just a block device implemented on ram, similar to hard disk operation, but with faster read and write speed, it can be used at any time when the system is running, 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. Just before loading linux, bootloader can load the image of a relatively small root file system in a specified location in memory, and call this memory initrd (here is the memory occupied by initrd, not ramdisk, note the difference), and then bootloader tells the kernel the starting address and size of initrd by passing parameters (you can also compile these parameters in the kernel) During the startup phase, you can temporarily use initrd to mount the root file system. The original purpose of initrd is to divide the startup of kernel into two stages: keep the minimum and most basic startup code in kernel, and then put the support for various hardware devices in initrd as modules, so that the required modules can be loaded from the root file system of initrd mount during startup. One advantage of this is that while keeping the kernel unchanged, you can flexibly support different hardware by modifying the contents of the initrd. In the final stages of boot completion, the root file system can be re-mount to other devices, but it can no longer be re-mount (as is the case with many embedded systems). The specific implementation process of initrd is as follows: bootloader loads the root file system image to the specified location of memory, passes the relevant parameters to the kernel, when the kernel starts, copies the contents of initrd to ramdisk (ram0), releases the memory occupied by initrd, and mount the root file system on ram0. As you can see from this process, the kernel needs support for both ramdisk and initrd (this need is programmed into the kernel and cannot be used as a module).

two. A method to realize the root file system of embedded system: for the system where both kernel and root file system are stored in flash, we can generally make use of the mechanism of initrd started by linux. The specific process is clear before, and another thing is to pass root=/dev/ram0 in the startup parameters, so that the root file system of mount with initrd is no longer switched, because at this time the actual device 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.

At this point, the study on "how to mount rootfs when linux starts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report