What is linux rootfs

藏色散人
Release: 2023-04-19 10:11:25
Original
2998 people have browsed it

linux rootfs refers to the root file system in Linux; the root file system is the first file system mounted when the kernel starts. The kernel code image file is stored in the root file system, and the system boot program After the root file system is mounted, some basic initialization scripts and services will be loaded into memory for running.

What is linux rootfs

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is linux rootfs?

Root file system in Linux (principle and introduction of rootfs)

1. What is File system

The file system is the method and data structure used by the operating system to identify files on storage devices (commonly disks, but also solid-state drives based on NAND Flash) or partitions; that is, on storage devices Ways to organize files. The software organization responsible for managing and storing file information in the operating system is called a file management system, or file system for short. The file system consists of three parts: the file system interface, the software collection for object operation and management, and the objects and attributes. From a system perspective, the file system is a system that organizes and allocates the space of file storage devices, is responsible for file storage, and protects and retrieves stored files. Specifically, it is responsible for creating files for users, storing, reading, modifying, and dumping files, controlling file access, and revoking files when users no longer use them.

Although the kernel is the core of Linux, files are the main tool used by users to interact with the operating system. This is especially true for Linux, which, in the UNIX tradition, uses file I/O mechanisms to manage hardware devices and data files.

2. What is the root file system

The root file system is the first file system mounted when the kernel starts up, the kernel code The image file is stored in the root file system, and the system boot program will load some basic initialization scripts and services into the memory for running after the root file system is mounted.

3. Why is the root file system so important?

The reason why the root file system is preceded by a "root" indicates that it is the "root" for loading other file systems. , then if there is no such root, other file systems will not be able to be loaded.

The root file system contains directories and critical files necessary for system startup, as well as files necessary to enable other file systems to be mounted. For example:

The application of the init process must run on the root file system;
The root file system provides the root directory "/";
The information that Linux relies on when mounting a partition is stored in the root In the file system /etc/fstab;
The shell command program must run on the root file system, such as ls, cd and other commands;
In short: a Linux system, only the kernel itself cannot work, it must It requires the cooperation of rootfs (configuration files in the etc directory, shell commands in /bin /sbin and other directories, and library files in the /lib directory, etc...) to work.

When Linux starts, the first thing that must be mounted is the root file system; if the system cannot mount the root file system from the specified device, the system will make an error and exit the startup. After success, other file systems can be mounted automatically or manually. Therefore, different file systems can exist in a system at the same time. The process of associating a file system with a storage device in Linux is called mounting. Use the mount command to attach a file system to the current file system hierarchy (root). When performing a mount, provide the file system type, file system, and a mount point. After the root file system is mounted on "/" under the root directory, there are various directories of the root file system under the root directory, files: /bin /sbin /mnt, etc., and then mount other partitions to the /mnt directory. , there are various directories and files of this partition in the /mnt directory.

4. How to mount the root file system in the kernel

init/main.c->

start_kernel()->vfs_caches_init(totalram_pages)–>

mnt_init()–>

/* sysfs用来记录和展示linux驱动模型,sysfs先于rootfs挂载是为全面展示linux驱动模型做好准备 /
     / mnt_init()调用sysfs_init()注册并挂载sysfs文件系统,然后调用kobject_create_and_add()创建fs目录 */
     sysfs_init();

/* init_rootfs()注册rootfs,然后调用init_mount_tree()挂载rootfs */
     init_rootfs();

init_mount_tree();

1、sysfs文件系统目前还没有挂载到rootfs的某个挂载点上,后续init程序会把sysfs挂载到rootfs的sys挂载点上;

2、rootfs是基于内存的文件系统,所有操作都在内存中完成;也没有实际的存储设备,所以不需要设备驱动程序的参与。基于以上原因,linux在启动阶段使用rootfs文件系统,当磁盘驱动程序和磁盘文件系统成功加载后,linux系统会将系统根目录从rootfs切换到磁盘文件系统。

start_kernel
  vfs_caches_init
    mnt_init
      init_rootfs注册rootfs文件系统
      init_mount_tree 挂载rootfs文件系统
        vfs_kern_mount
          mount_fs
            type->mount其实是rootfs_mount
              mount_nodev
                fill_super 其实是ramfs_fill_super
                  inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
                  sb->s_root = d_make_root(inode);
                    static const struct qstr name = QSTR_INIT("/", 1);[1*]
                    __d_alloc(root_inode->i_sb, &name);
          …
          mnt->mnt.mnt_root = root;[2*]
          mnt->mnt.mnt_sb = root->d_sb;[3*]
          mnt->mnt_mountpoint = mnt->mnt.mnt_root;[4*]
          mnt->mnt_parent = mnt;[5*]
root.mnt = mnt;
        root.dentry = mnt->mnt_root;
        mnt->mnt_flags |= MNT_LOCKED;
        set_fs_pwd(current->fs, &root);
        set_fs_root(current->fs, &root);
  …
  rest_init
  kernel_thread(kernel_init, NULL, CLONE_FS);
Copy after login

Before executing kernel_init, the roofs file system will be established.

1. The name of the root directory is set to "/";
2. The root directory in vfsmount is set at;
3. The root directory in vfsmount is set at super block;
4. The file mount point in vfsmount is set to point to itself;
5. The vfsmount of the parent file system in vfsmount is set to itself;

five , Introduction to various common directories of the root file system

Normally speaking, the root file system includes at least the following directories:

/etc/:存储重要的配置文件。
/bin/:存储常用且开机时必须用到的执行文件。
/sbin/:存储着开机过程中所需的系统执行文件。
/lib/:存储/bin/及/sbin/的执行文件所需的链接库,以及Linux的内核模块。
/dev/:存储设备文件。
Copy after login

6. Common directories

Linux There are generally the following directories in the file system:

/bin directory
This directory stores basic commands that can be used by all users. These commands can be used before mounting other file systems, so the /bin directory must be the same as the root The file system is in the same partition.
Commonly used commands in the /bin directory include: cat, chgrp, chmod, cp, ls, sh, kill, mount, umount, mkdir, mknod, test, etc. When we use Busybox to create the root file system, we generate In the bin directory, you can see some executable files, which are some available commands.

/sbin directory
This directory stores system commands, that is, commands that only administrators can use. System commands can also be stored in /usr/sbin, /usr/local/ Under the sbin directory, the /sbin directory stores basic system commands. They are used to start the system, repair the system, etc. Similar to the /bin directory, /sbin can be used before mounting other file systems, so the /sbin directory must In the same partition as the root file system.
Commonly used commands in the /sbin directory include: shutdown, reboot, fdisk, fsck, etc. System commands installed by local users are placed in the /usr/local/sbin directory.

/dev directory
This directory stores device files. Device files are a unique file type in Linux. Under Linux systems, various devices are accessed in the form of files. , that is, operating a specific hardware by reading and writing a device file. For example, serial port 0 can be operated through the "dev/ttySAC0" file, and the second partition of the MTD device can be accessed through "/dev/mtdblock1".

/etc directory
Various configuration files are stored in this directory. For Linux systems on PCs, there are many files and directories in the /etc directory. These directory files are Optionally, they depend on the applications you have on the system and whether they require configuration files. In embedded systems, these can be greatly reduced.

/lib directory
This directory stores shared libraries and loadable (drivers), and shared libraries are used to start the system. Run executable programs in the root file system, such as programs in the /bin /sbin directory.

/home directory
User directory, it is optional. For every ordinary user, there is a subdirectory named after the user name in the /home directory. Store user-related configuration files.

/root directory
The directory of the root user. Correspondingly, the directory of ordinary users is a subdirectory under /home.

/usr directory
The contents of the /usr directory can be stored in another partition, and then mounted to the /usr directory in the root file system after the system is started. It stores shared, read-only programs and data, which shows that the contents of the /usr directory can be shared among multiple hosts, and these mainly comply with FHS standards. Files in /usr should be read-only, related to other hosts, and variable files should be saved in other directories, such as /var. The /usr directory can be reduced in embedded systems.

/var directory
Contrary to the /usr directory, the /var directory stores variable data, such as the spool directory (mail, news), log files, and temporary files.

/proc directory
This is an empty directory, often used as the mounting point of the proc file system. The proc file system is a virtual file system. It has no actual storage devices. The directories and files are temporarily generated by the kernel to represent the running status of the system and can also operate the file control system.

/mnt directory
is a mount point used to temporarily mount a file system. It is usually an empty directory. You can also create an empty subdirectory in it, such as / mnt/cdram /mnt/hda1. Used to temporarily mount CDs and hard drives.

/tmp directory
is used to store temporary files, usually an empty directory. Some programs that need to generate temporary files use the /tmp directory, so the /tmp directory must exist and accessible.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is linux rootfs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!