Today, the editor will share with you the relevant knowledge points about what the linux file system is. After reading this article, everyone has gained something, let's take a look at it together.
Linux files are directories (files). In order to facilitate the management of files and directories, the Linux system organizes them into an inverted tree structure starting with the root directory /. Directories in Linux are similar to folders in Windows systems, the difference is that directories in Linux systems are also treated as files.
Linux files are directories (files).
In order to facilitate the management of files and directories, the Linux system organizes them into an inverted tree structure starting with the root directory /. The directory in Linux is similar to the folder in Windows system, the difference is that the directory in Linux system is also treated as a file.
In the Linux operating system, all files and directories are organized into an inverted tree structure starting with a root node /, as shown in Figure 1.
Figure 1 Linux system file and directory organization diagram
Among them, the directory is equivalent to the folder in Windows, and the files stored in the directory can also be are other subdirectories, and the real information is stored in the file.
The topmost layer of the file system starts from the root directory. The system uses / to represent the root directory. Include (sub)directories or files. So repeated can form a huge file system.
In fact, the main purpose of using this tree-like and hierarchical file structure is to facilitate the management and maintenance of the file system. Imagine that if all files are placed in one directory, the management of the file system And maintenance will turn into a nightmare.
There are many similar examples in reality. For example, in the entire administrative system, villagers are equivalent to files. They live in a village, and the village is the directory for storing villagers. Many villages form a township, which is equivalent to storing the directory of villages, and so on, and finally builds a huge administrative area management structure map.
Note that directory names or file names are case-sensitive, such as dog, DOG and Dog are 3 different directories or files. A complete directory or file path is composed of a series of directory names, each of which is separated by /. For example, the full path of cat is /home/cat.
In the file system, there are two special directories, one is the working directory where the user is located, that is, the current directory, which can be represented by a dot; the other is the upper directory of the current directory, also called The parent directory is represented by two dots...
If a directory or file name starts with a dot, it means that the directory or file is a hidden directory or file. That is, when searching in the default mode (the search command will be discussed later), the directory or file will not be displayed.
In order to facilitate management and maintenance, the Linux system adopts the file system hierarchy standard, also known as the FHS standard, which specifies what types of files (or subdirectories) should exist in each directory under the root directory, for example , the executable files should be stored in the /bin and /sbin directories
The physical composition of the hard disk
First of all, briefly understand the physical structure of the hard disk. Generally speaking, the hard disk structure includes: disk, magnetic head, disk spindle, control motor, head controller, data converter, interface, Cache and other parts. All the platters (generally there are multiple platters in a hard disk, and the platters are parallel to each other) are fixed on a spindle. There is a magnetic head on the storage surface of each disk, and the distance between the magnetic head and the disk is very small (so it is easily damaged by severe vibration), and the magnetic head is connected to a magnetic head controller to uniformly control the movement of each magnetic head. The magnetic head moves along the radial direction of the disk, and the disk rotates at a high speed in a specified direction, so that the magnetic head can reach any position on the disk.
A disk is composed of multiple rings, these rings are called tracks, and a track is divided into multiple sectors (sector), each sector is 512Byte, the circle of the same position of all disks of the hard disk The rings form a magnetic column (Cylinder). The hard disk capacity is: 512Byte * number of sectors * number of magnetic cylinders * number of heads Calculate the storage capacity of a circle, and a head reads a circle, so multiplying by the number of heads is equivalent to multiplying the area of the circle by the number of circles.
The smallest unit of disk division is the magnetic cylinder (Cylinder)
Disk division is actually to record a division The start column and end column of the partition, and the recorded information is stored in the main boot sector (Master Boot Recorder, MBR). In fact, MBR is on the zeroth track of a hard disk, which is also the first area that must be read when the computer is turned on to use the hard disk.
Think about a questionQuestion: Is the size of the MBR fixed?
We think so, the storage order of data is: MBR + other data. If the size of the MBR is not fixed, for example, the information of 3 partitions is stored in the original MBR, and now we want to add a new partition, what will happen? The consequence is that all other data must be sequentially moved backward by a distance of partition information. Did you think of the addition and deletion of the array? So we want to fix the MBR, and the fact is that the MBR is fixed to only store the information of 4 partitions. This seems much better, but there are only four partitions, is it enough? Of course, the designer also thought of this problem, so the partition is divided into two types: Primary and Extended (E can only have one at most). Among them, P can be used directly, but E cannot be used directly. E is equivalent to a pointer, pointing to the location information storage place of the extended partition.
filesystem is the file system, each oartition can have its own filesystem, such as fat32, ntfs, etc
< p> Although different partuitions have different filesystems, they are all used to store data. Earlier we introduced that the smallest storage unit of a hard disk is a sector (sector, generally 512Byte), but the smallest storage unit of a filesystem is not a sector but a block. The block is a power multiple of 2 of the sector, and the magnetic head reads the data of one block at a time. Therefore, if the block is too small, the magnetic head needs to read a large number of blocks when reading a file, which is very inefficient, but the block cannot be too large, because only one file can exist in a block, for example, the block size is 100M , then if there is a 100.1M file, it needs to occupy two blocks, wasting a lot of space.
The first block in each filesystem is called superblock. The role of the superblock is to store the size of the filesystem, empty and filled blocks and other general books and such information. That is to say, if you want to use a filesystem, the first thing you have to go through is the superblock block. If the superblock is broken, the partition is probably useless.
Linux EXT2 file system
Since we are learning linux, we naturally have to learn the filessystem of linux. We will use the most standard EXT2 of linux as an illustration.
The filesystem in EXT2 is divided into inode area and block area, in which the inode stores the relevant attributes of the file and other information, and the block area stores the content of the file. Each inode acts as a pointer, which can describe The relevant attributes of the file and point to the location of the block where the file is located. The number of blocks and inodes is fixed at the beginning of the format.
Schematic diagram of the entire filesystem:
SuperBlock: As mentioned earlier, Superblock is the place to record the relevant information of the entire filesystem, there is no Superblock , there is no such filesystem. The information he records mainly includes:
The total amount of blocks and inodes;
Unused and used The number of inodes / blocks;
The size of a block and an inode;
The mount time of the filesystem, the latest Information about the file system such as the time of writing data, the time of the latest disk check (fsck);
A valid bit value, if the file system has been mounted, Then the valid bit is 0, if not mounted, the valid bit is 1.
Group Description: record where this block starts to record;
Block bitmap: here Record whether the block has been used;
Inode bitmap: here to record whether the inode has been used;
Inode table: for each inode data storage area;
Data Blocks: for each block data storage area.
The operation of the Linux file system
We know that the access speed of the hard disk is very slow compared to the memory , in order to improve the overall speed, linux adopts asynchronous processing.
What is asynchronous? For example: "When the system reads a certain file, the block data where the file is located will be loaded into the memory, so the disk block will be placed in the buffer cache of the main memory, if these When the block data is changed, only the block data in the main memory will be changed at the beginning, and the block data in the buffer will be marked as "Dirty". At this time, the disk entity block has not been corrected! Therefore, it means that the data of these "Dirty" blocks must be written back to the disk, so as to maintain the consistency of the data on the disk physical block and the block data in the main memory. 』
So pay attention to the shutdown of linux, otherwise it may cause the loss of files or even the damage of the disk! ! !
So pay attention to the shutdown of linux, otherwise it may cause file loss or even disk damage! ! !
What we mentioned above is all about the file system (filesystem), but if we want to be able to use our Linux, we have to "mount the Load (mount)" on our Linux system will do! We just mentioned above that the directory can record information about file names and inodes. In addition, the directory is also the entry point that allows us to generate a corresponding entry point with the filesystem. Therefore, we call that entry point directory "mount point (mount point)"
Copyright Description：No reproduction without permission。
Knowledge sharing community for developers。
Let more developers benefit from it。
Help developers share knowledge through the Internet。