Collection
zero Useful+1
zero

Root file system

file system
The root file system is the first file system mounted when the kernel starts. The kernel code image file is saved in the root file system, and the system boot program will load some basic initialization scripts and services into memory to run after the root file system is mounted.
Chinese name
Root file system
Category 1
/Bin directory
Category 2
/Sbin directory
Category 3
/Dev directory

brief introduction

Announce
edit
The first thing to understand is "what is a file system". A file system is a mechanism for organizing data and metadata on a storage device. This mechanism is conducive to the interaction between users and operating systems. In an oracle technical article, I saw the sentence "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, because in the UNIX tradition, it uses the file I/O mechanism to manage hardware devices and data files.", If there is no file system in Linux, the interaction between the user and the operating system will be disconnected. For example, the interactive shell we use most, including some other user programs, cannot run. You can see the importance of the file system relative to the Linux operating system here. Here is Linux Filesystem Component architecture.
User space contains applications (for example, users of file systems) and GNU C libraries (glibc) that provide user interfaces for file system calls (open, read, write, and close). The system call interface acts like a switch, sending system calls from user space to the appropriate endpoints in kernel space.
VFS is the main interface of the underlying file system. This component exports a set of interfaces, and then abstracts them to each file system. The behavior of each file system may vary greatly. There are two caches for file system objects (inode and dentry). They cache recently used file system objects.
Each file system implementation (such as ext2, JFS, etc.) exports a set of common interfaces for VFS to use. The buffer cache caches requests between the file system and the related block device. For example, read and write requests to the underlying device driver are passed through the buffer cache. This allows you to cache requests in it, reduce the number of times you access physical devices, and speed up access. The buffer cache is managed as a recently used (LRU) list. Note that you can use the sync command to send the request in the buffer cache to the storage media (force all unwritten data to be sent to the device driver, and then to the storage device).
When we mentioned the file system under Windows, what was your first reaction? Is it the file system type of Fat32, NTFS, etc. under Windows. In Linux, you may think of Ext2 and Ext3, but you must also have a concept of root file system. The root file system is first a file system. This file system not only has the function of storing data files in the ordinary file system, but is different from the ordinary file system in that it is the first file system mounted when the kernel starts. The image file of the kernel code is saved in the root file system, After the root file system is mounted, the system boot startup program will load some initialization scripts (such as rcS, inittab) and services into memory to run. We should understand that the file system and the kernel are completely independent. When the kernel transplanted in embedded system is downloaded to the development board, there is no way to really start the Linux operating system, and the error of failing to load the file system will occur.
When is the root file system mounted during system startup? Mount/dev/ram0 first, and then execute/linuxrc. Switch the root directory, and then mount the specific root file system. After the root file system is executed, that is, at the end of the Start_kernel() function, the init process is executed, that is, the first user process. Perform various initialization operations on the system. If you want to understand the process here, you should take a good look Linux kernel Source code. It shows the hierarchy of VFS, kernel and file system:
The reason why the root file system is preceded by a "root" indicates that it is the "root" for loading other file systems. If it is a root, other file systems cannot be loaded without this root. It contains files necessary for system boot and mounting other file systems. The root file system includes directories and key files necessary for Linux startup. For example, when Linux starts, it needs to have related files in the init directory. When Linux mounts partitions, Linux will find the/etc/fstab mount file. The root file system also includes many application bin directories, Any file including those necessary for Linux system startup can become the root file system.
When Linux starts, the first file system 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 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 mount. Use the mount command to attach a file system to the current file system hierarchy (root). When mounting, provide the file system type, file system, and a mount point. After the root file system is mounted to "/" under the root directory, there will be various directories of the root file system under the root directory, such as files:/bin/sbin/mnt, etc. Then attach other partitions to the/mnt directory, and there will be various directories and files of this partition under the/mnt directory.
The Linux root file system generally has the following directories:
1./bin directory
The commands in this directory can be used by root and general accounts. Because these commands can be used before attaching other file systems, the/bin directory must be in the same partition as the root file system.
/Common commands in the bin directory are: cat chgrp、chmod、cp、ls、sh、kill、mount、umount、mkdir、 [, test, etc. The "[" command is the test command. When we use Busybox to create the root file system, we can see some executable files in the generated bin directory, that is, some available commands.
2./sbin directory
This directory stores system commands, which are only available to system administrators (commonly known as the root with the highest authority). System commands can also be stored in the/usr/sbin,/usr/local/sbin directory. The/sbin directory stores basic system commands, which are used to start the system and repair the system. Similar to the/bin directory,/sbin can be used before attaching other file systems, Therefore, the/sbin directory must be in the same partition as the root file system.
/The commonly used commands in the sbin directory are: shutdown, reboot, fdisk, fsck, init, etc. The system commands installed by local users are placed in the/usr/local/sbin directory.
3. /dev directory
This directory stores the files of devices and device interfaces. Device files are unique file types in Linux. Under Linux system, various devices are accessed in the form of files, that is, a specific hardware is operated by reading or writing a device file. For example, the serial port 0 can be operated through the "dev/ttySAC0" file, and the second partition of the MTD device can be accessed through the "/dev/mtdblock1" file. Important files include/dev/null,/dev/zero,/dev/tty,/dev/lp *, etc.
4./etc directory
This directory stores the main configuration files of the system, such as the account and password files of personnel, and the actual files of various services. Generally speaking, the file attributes of this directory can be viewed by ordinary users, but only root has permission to modify them. For Linux systems on PCs, there are many files and directories in the/etc directory. These directory files are optional. They depend on the applications in the system and whether these programs need configuration files. In embedded system, these contents can be greatly reduced.
5./lib directory
This directory stores shared libraries and loadable (drivers), which are used to start the system. Run the executable program in the root file system, such as the program in the/bin/sbin directory.
6./home directory
The default user folder of the system is optional. For each ordinary user, there is a subdirectory named after the user name in the/home directory, which stores user related configuration files.
7./root directory
The home folder of the system administrator (root) is the directory of the root user. Correspondingly, the directory of an ordinary user is a subdirectory under/home.
8./usr directory
/The contents of the usr directory can be stored in another partition, and then attached to the/usr directory in the root file system after the system starts. It stores shared and read-only programs and data, which indicates that the contents of the/usr directory can be shared among multiple hosts, which mainly conform to the FHS standard/ The files in the usr should be read-only. Other host related and variable files should be saved in other directories, such as/var/ The usr directory can be reduced in embedded systems.
9./var directory
In contrast to the/usr directory, the/var directory stores variable data, such as the spool directory (mail, news), Log file , temporary file.
10./proc directory
This is an empty directory, which is often used as the attachment point of the proc file system. The proc file system is a virtual file system, which has no actual storage device. The directories and files inside are all from the kernel
It is generated temporarily to indicate the running status of the system, and can also operate the file control system.
11./mnt directory
The mount point used to temporarily mount a file system is usually an empty directory. You can also create an empty subdirectory in it, such as/mnt/cdram/mnt/hda1. It is used to temporarily mount CDs, removable storage devices, etc.
12./tmp directory
It 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 be accessible.
Then we use Busybox to create the root file system, which is to create these directories and various files under these directories.
The root file system of an embedded Linux system may not be as complex as the one listed above. For example, embedded systems are not usually targeted at multiple users, so the directory/home may be rarely used in general embedded Linux, The/boot directory depends on whether the BootLoader you use can regain the kernel image from your root file system before the kernel starts. Generally speaking, only/bin,/dev,/etc,/lib,/proc,/var,/usr are required, while others are optional.
The root file system has always been an important part of all Unix like operating systems. It can also be considered as an important feature of embedded Linux system that distinguishes it from other traditional embedded operating systems. It has brought many powerful and flexible functions to Linux, as well as some complexity. We need to clearly understand the basic structure of the root file system, carefully select the required system libraries, kernel modules and applications, configure various initialization script files, and select the appropriate file system type and place it in the appropriate location of the actual storage device. The following are some commonly used file systems.

file system

Announce
edit
(1)jffs2
JFFS embedded system file system was first developed by Axis Communications of Sweden for embedded systems based on the Linux 2.0 kernel. JFFS2 is RedHat
JFFS based Flash File System JFFS2 is an embedded file system originally developed for the embedded product eCos of RedHat, so JFFS2 can also be used in Linux and uCLinux.
Jffs2: Journaling Flash FileSystem v2
It is mainly used for NOR flash memory and is based on MTD driver layer. It is characterized by: readable and writeable, data compression supporting, hash table based log file system, crash/power down security protection, "write balance" support, etc. The disadvantage is that when the file system is full or nearly full, the garbage collection jffs2 The running speed of. At present, jffs3 is under development. For detailed documentation on the use of the jffs series file system, refer to mtd-jffs-HOWTO.txt in the MTD patch package.
Jffsx is not suitable for NAND flash memory The main reason is that the capacity of NAND flash memory is generally large, which leads to the rapid increase of the memory space occupied by jffs to maintain log nodes. In addition, when the jffsx file system is mounted, it needs to scan the entire FLASH content to find out all log nodes and establish the file structure, which will consume a lot of time for large capacity NAND flash memory.
(2)yaffs:Yet Another Flash File System
Yaffs/yaffs2 is a log file system designed for embedded systems to use NAND flash memory. Compared with jffs2, it reduces some functions (for example, it does not support data compression), so it is faster, has a short mount time, and uses less memory. In addition, it is also a cross platform file system. In addition to Linux and eCos, it also supports WinCE, pSOS and ThreadX.
Yaffs/yaffs2 comes with NAND chip driver, and provides embedded systems with Direct access to the file system The user can directly operate on the file system without using MTD and VFS in Linux. Of course, yaffs can also be used with MTD drivers.
The main difference between yaffs and yaffs2 is that the former only supports small pages (512 Bytes) NAND flash memory The latter can support large page (2KB) NAND flash memory. At the same time, yaffs2 has greatly improved in terms of memory space occupation, garbage collection speed, read/write speed, etc.
(3) Cramfs:Compressed ROM File System
Cramfs is a read-only compressed file system developed by Linus Torvalds, the founder of Linux. It is also based on the MTD driver. In the cramfs file system, each page (4KB) is compressed separately and can be accessed randomly. The compression ratio is up to 2:1, which saves a lot of Flash storage space for the embedded system and enables the system to store the same files through lower capacity FLASH, thus reducing the system cost.
Cramfs file system is stored in compression mode and decompressed at runtime, so it does not support applications running in XIP mode. All applications are required to be copied to RAM for running, but this
Does not represent the demand of Ramfs RAM space It should be larger, because Cramfs stores files by means of paging compression. When reading files, it will not consume too much memory space at once,
The memory is allocated only for the part actually read at present, and the memory space is not allocated for the part not yet read. When the file we read is not in memory, the Cramfs file system automatically calculates the location of the compressed data, and then decompresses it to RAM immediately. In addition, it is fast and efficient, and its read-only feature is conducive to protecting the file system from damage and improving the reliability of the system.
Because of the above characteristics, Cramfs is widely used in embedded systems. However, its read-only attribute is also a major defect, making it impossible for users to expand its content. Cramfs images are usually placed in Flash, but they can also be placed in other file systems. The loopback device can install them in other file systems.
(4) Network File System
NFS is a technology developed by Sun to share files between different machines and operating systems through the network. In the development and debugging phase of embedded Linux system, this technology can be used to establish the root file system based on NFS on the host, mount it to the embedded device, and easily modify the content of the root file system. All of the above are memory based file systems, which can be used as the root file system of Linux. In fact, Linux also supports logical or pseudo file system, such as procfs (proc file system), which is used to obtain system information, and devfs (device file system) and sysfs, which are used to maintain device files.