The principle of file system, root file system, VFS

2.18.1. Overview of the root file system
2.18.1.1. Why do you need a root file system

(1) The application of the init process is on the root file system                                                                                          

Explanation: The kernel startup process will eventually call a function to turn the kernel state process 0 into the application state process 1, which is the main init process.

(2) The root file system provides the root directory /

Explanation: All directories start from /. For a tree structure like linux, it is equivalent to the root of the tree, and there will be everything that follows. We all know that after the operating system starts, we all operate under the file system.

(3) The application layer configuration (etc directory) after the kernel is started is on the root file system.

Explanation: After the operating system is started, a series of user application layer configurations must be done. For example, our vi defaults to display line numbers and other configurations. We all need to configure them in the xxx.conf file. These configuration files are the so-called user application layer configurations. These configuration files will be called after the operating system starts to configure the system. These configurations are all on the root file system, which can almost be considered as: distribution = kernel (only the kernel itself, regardless of the application layer, there is no root file at all System related things) + rootfs (application layer related things are all provided by the root file system) rootfs = root file system + some useful and fun applications running on it.

(4) The shell command program is on the root file system. Commands such as ls, cd, etc. (also applications)
Summary: For a linux system, only the kernel itself cannot work, it must be rootfs (configuration files in the etc directory on the top, shell commands in directories such as /bin /sbin, and library files in the /lib directory, etc. ··) work together.


2.18.1.2 What is the essence of the root file system

(1) The root file system is a special-purpose file system.

Explanation: The root file system is also a file system, with the addition of a root word. The difference between it and a normal file system is that it can provide a root directory, but it is also a type of file system.

(2) The root file system must also belong to a certain file system format. rootfstype= (kernel startup parameters, which is the file system format provided by uboot to the kernel)

Explanation: The principles of different formats are different, the security level, the size limit, and the applicable storage devices are different.

(3) What is the file system used for? ZnFAT
Explanation: First of all, storage devices (block devices, such as hard disks, flash, etc.) are divided into blocks (sectors). Physically, the bottom layer can only access the storage device according to the block number (sector number). This is very troublesome.
        Secondly, the file system is some code and a set of software. The function of this software is to manage the sectors of the storage device, turning the access of these sectors into the access of directories and file names. When we access a file in the upper layer according to a specific directory and file name, the file system will convert the directory + file name into access to the sector number.
        Finally, the difference between different file systems lies in the management strategies and methods of these sectors, such as bad block management and fragmentation management.




2.18.2. Format of root file system 2.18.2.1.
Format of image file
(1) An image file that can be burned by using special tool software
(2) The image contains all files in the root file system

(3) Burning this image is similar to formatting the corresponding partition.

Analysis: Each image has its own format, that is, the above-mentioned algorithm principle requirements are different. In the final identification and reading, the image should also be identified and parsed according to the corresponding format (file management table). After our U disk is formatted, the file management table is formatted, but the files in it are still stored, but these files Unable to read because the read format is formatted in another format.

(4) The image file system has a certain format. The format is internalized and has nothing to do with the file name suffix.

Explanation: The internalization of the format refers to its own algorithmic principles, such as ext, jiffs2, etc. For example, we made two formats of rootfs.ext3 and rootfs.jiffs2, even if we change the suffix of rootfs.jiffs2 to ext3, the internal If it is not damaged, it can still be parsed in jiffs format, but you have cheated others. I know, all the parameters will be parsed in ext3 format, and finally the error.

2.18.2.2. Folder form
(1) The root file system is actually a folder containing specific content
(2) The root file system can be formed by adding necessary files to any empty folder
(3) The prototype of the root file system is
2.18.2.3 in the form of a folder constructed in the development host , summary
(1) The main purpose of the root file system in the form of an image file is to burn it to a block device, and mount it after the kernel on the device is started. The root file system in the form of an image file is made from the root file system in the form of a folder using a dedicated image making tool.
(2) Initially, an empty folder was created by mkdir in the development host, and then some necessary files were added to it (including the runtime configuration file in the etc directory, the executable program in the /bin directory, and the /lib directory). library files, etc...), a rootfs in the form of a folder is formed. Then the rootfs in the form of a folder can be remotely mounted and used by the kernel through nfs, but it cannot be used to burn block devices. In order to burn this rootfs into a block device, we use some special software tools to make it into a root file system image of a certain format that can be burned.
(3) The rootfs in the form of a folder has no format. After it is made into an image, there is a certain rootfs format. The format is determined by our image production process and production tools. The usage of the image maker for each format is different.




2.18.3. Make your own root file system in ext3 format
2.18.3.1, mke2fs introduction
(1) mke2fs is an application, which is installed by default in ubuntu. This application is used to make root file systems in ext2, ext3, ext4 and other formats.
(2) The names of the applications generally used to make rootfs in different formats are very similar, similar to mkfs.xxx (for example, the tool used to make rootfs in ext2 format is called mkfs.ext2, and the rootfs used to make rootfs in jffs2 format is called mkfs.ext2. The tool is called mkfs.jffs2)
(3) mkfs.ext2 in ubuntu14.04 are all symbolic links of mke2fs.


2.18.3.2. Make the root file system in ext3 format by hand
(1) Create the rootfs.ext2 file and mount it to a directory for easy access to it
. Reference: http://blog.csdn.net/zhengmeifu/article/ details/24174513"
dd if=/dev/zero of=rootfs.ext2 bs=1024 count=2048
losetup /dev/loop1 rootfs.ext2
mke2fs -m 0 /dev/loop1 2048
mount -t ext2 /dev/loop1 ./rootfs /
(2) We write a normal file linuxrc to the image. This file will become /linuxrc in the image we made. After the kernel has mounted the image, it will try to execute /linuxrc. Then it is bound to fail when executed. The phenomenon we will see in future experiments should be: the mount is successful, and the execution of /linuxrc fails.
(3) When making a useful rootfs in the future, it is necessary to add a real executable linuxrc program in this step, and then add other library files in the /lib directory, configuration files in the /etc directory, and so on.
(4) Uninstall it, and then the mirror is ready.
umount /dev/loop1
losetup -d /dev/loop1




2.18.4.nfs mode to start rootfs in the form of self-made simple folders
2.18.4.1, what is nfs
(1) nfs is a network communication protocol consisting of a server and a client.
(2) The role of nfs. Many direct applications can be made using the nfs protocol. We use nfs here mainly for rootfs mounts. Run the kernel on the development board as an nfs client, and build an nfs server on the host ubuntu. Export the rootfs directory in the form of a folder we made in the nfs server of the host ubuntu, then you can mount the rootfs in the form of this folder in the client to start the system.

(3) Build an nfs server.


2.18.4.2. Configure the kernel to support nfs as rootfs
(1) Set the bootargs of the nfs startup method
(2) Configure the nfs startup method in the menuconfig


2.18.4.3. Summary
(1) The nfs method startup is equivalent to the kernel remote on the development board The rootfs mounted on the host
(2) The nfs method starts without making a rootfs image
(3) The nfs method is not suitable for real products. Generally,




2.18.5 is used for debugging in the product development stage. What is linuxrc
2.18.5.1, /linuxrc is An executable application
(1)/linuxrc is at the application layer and has nothing to do with the kernel source code
(2)/linuxrc is executable under the current kernel system of the development board. Therefore, under the linux system of the ARM SoC, the application is compiled and linked with arm-linux-gcc; if it is under the linux system of the PC, then the program is compiled and linked with gcc.
(3) If /linuxrc is statically compiled and linked, it can be run directly; if it is dynamically compiled and linked, then we must provide it with the necessary library files to run. However, because our /linuxrc program is directly invoked and executed by the kernel, the user has no chance to export the path of the library file, so in fact this /linuxrc cannot be dynamically linked, and is generally statically linked.


2.18.5.2. The user interface is brought out when /linuxrc is executed
(1) After the operating system is started, after a series of self-running configurations, it will finally give the user an operation interface (maybe cmdline, maybe GUI), this user operation interface is created by /linuxrc brought out.
(2) Many things such as the user interface are not responsible for the /linuxrc program. The user interface has its own special application program, but the application program of the user interface is directly or indirectly executed by the /linuxrc call. User interface programs and other applications are processes 2, 3, 4... This is what we call process 1 (the init process, which is /linuxrc) is the ancestor process of all other application processes.


2.18.5.3, /linuxrc is responsible for the configuration after the system is started
(1) Just like a house cannot be lived directly after it is built, it needs to be decorated; the operating system cannot be used directly after it is started, so it needs to be configured.

(2) The configuration of the application layer after the operating system is started (generally called runtime configuration, English abbreviation etc) is to make our operating system more convenient to use and more suitable for my personal hobby or practicality.

Explanation: QQ boot auto-start configuration under windows, default line number of vi under Linux, these configuration files are directly or indirectly called by /linuxrc.



2.18.5.4, /linuxrc is usually busybox in embedded linux
(1) busybox is a project written in C language, which contains many .c files and .h files. This project can be configured and compiled into applications that can run on various platforms. If we compile busybox with arm-linux-gcc, we will get an application that can run on the linux kernel of our development board.
(2) The busybox program is developed to build rootfs in an embedded environment, which means that it is a specially developed init process application.
(3) busybox provides a complete set of shell command assemblies for the current system. For example vi, cd, mkdir, ls, etc. In desktop linux distributions (such as ubuntu, redhat, centOS, etc.) vi, cd, ls, etc. are all separate applications. But in embedded linux, in order to save trouble, we put together all the commonly used shell commands such as vi and cd to form a shell command package, named busybox.






2.18.6. What else should be in rootfs

2.18.6.2. Device files in the dev directory.
(1) In linux, everything is a file, so a hardware device is also virtualized into a device file to access. In the linux system, /dev/xxx represents a hardware device. When we want to operate the hardware, open the device. file, then read/write/ioctl to operate the device, and finally close to close the device.

(2) The /dev directory is also indispensable in the minimum rootfs, and there are one or two device files that are necessary for the rootfs.


2.18.6.3, sys and proc directories.

(1) It is also indispensable in the minimum rootfs, but these two only need to create an empty folder, there is nothing in it, and there is no need for anything. These two directories are also related to drivers. It belongs to the virtual file system VFS in linux.

解释:这两个目录不是我们人为mkdir创建的,而是Linux系统运行时用软件虚拟出来的,主要是为了让我们通过目录以及目录里的文件看到Linux内核运行时的一些状态。


2.18.6.4、usr目录

是系统的用户所有的一些文件的存放地,这个东西将来busybox安装时会自动生成。


2.18.6.5、etc目录

(1)是很关键很重要的一个,目录中的所有文件全部都是运行时配置文件。/etc目录下的所有配置文件会直接或者间接的被/linuxrc所调用执行,完成操作系统的运行时配置。etc目录是制作rootfs的关键,所以后面下一个课程专门讲这个etc目录。

etc扩展:

(1)rcS文件介绍1:

1、/etc/init.d/rcS文件是linux的运行时配置文件中最重要的一个,其他的一些配置都是由这个文件引出来的。这个文件可以很复杂也可以很简单,里面可以有很多的配置项。比如Windows下QQ开机自启动配置,Linux下VI默认显示行号等

(2)rcS文件介绍2:

1、mdev

(1)mdev是udev的嵌入式简化版本,udev/mdev是用来配合linux驱动工作的一个应用层的软件,udev/mdev的工作就是配合linux驱动生成相应的/dev目录下的设备文件。

(2)因为这个问题涉及到驱动,因此详细讲解要等到驱动部分。这里我们只是通过一些直观的现象来初步理解udev/mdev的工作效果。

(3)在rcS文件中没有启动mdev的时候,/dev目录下启动后是空的;在rcS文件中添加上mdev有关的2行配置项后,再次启动系统后发现/dev目录下生成了很多的设备驱动文件。

(4)/dev目录下的设备驱动文件就是mdev生成的,这就是mdev的效果和意义。

2、hostname

(1)hostname是linux中的一个shell命令。命令(hostname xxx)执行后可以用来设置当前系统的主机名为xxx,直接hostname不加参数可以显示当前系统的主机名。

(2)/bin/hostname -F /etc/sysconfig/HOSTNAME -F来指定了一个主机名配置文件(这个文件一般文件名叫hostname或者HOSTNAME)

3、ifconfig

(1)有时候我们希望开机后进入命令行时ip地址就是一个指定的ip地址(譬如192.168.1.30),这时候就可以在rcS文件中ifconfig eth0 192.168.1.30



2.18.6.6、lib目录

(1)也是rootfs中很关键的一个,不能省略的一个。lib目录下放的是当前操作系统中的动态和静态链接库文件。我们主要是为了其中的动态链接库。


2.18.7.VFS简介
2.18.7.1、什么是VFS
(1)VFS是linux内核的一种设计理念、设计机制。VFS就是vitrual file system,叫虚拟文件系统。
(2)具体的一些文件系统如FAT、NTFS、ext2、ext3、jffs2、yaffs2、ubi等主要 设计目的是为了管理块设备(硬盘、Nand···)
(3) VFS是借鉴了文件系统的设计理念(通过文件系统将底层难以管理的物理磁盘扇区式访问,转换成目录+文件名的方式来访问),将硬件设备的访问也虚拟化成了对目录+文件的访问。所以有了VFS后我们可以通过设备文件(目录+文件名,譬如/dev/mmcblk0p2)的方式来访问系统中的硬件设备。
(4)以上可以初步看出VFS的一些厉害之处,但是VFS不止于此。


2.18.7.2、VFS的意义
(1)上面说过的对硬件设备的访问,将对硬件设备的访问和对普通文件的访问给接口统一化了 (linux中一切皆是文件)
(2)将操作系统上层(应用层)对下层不同文件系统类型的访问细节给屏蔽掉了。因此如果没有VFS那我们写cp命令(其他命令也一样)的时候就不得不去考虑你cp的这个文件在什么文件系统类型下。所以cp命令非常复杂,因此要考虑具体的文件系统类型。有了VFS后情况就不同了。VFS成了一个隔离层,隔离了下层的不同文件系统的差异性,对上层应用提供一个统一的接口。
(3)VFS将不同文件系统和下层硬件设备(块设备)驱动之间的细节也给屏蔽了。不同类型的文件系统在本身设计时是不用考虑各种不同的硬件设备的具体操作差异的,这里有一个类似于VFS的设计理念。


2.18.7.3、VFS和我们学习的关系
(1)VFS机制和rootfs挂载,和其他文件系统的挂载都是有关联的。
(2)内核中有一些sys proc这种虚拟文件系统,这东西也是和VFS机制有关。
(3)/dev/目录下的设备文件都和VFS有关,所以学习驱动绕不开VFS。




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858122&siteId=291194637