When dealing with various faults in the Linux system, the symptoms of the fault are the easiest to find, and the cause of this fault is the key to the final troubleshooting
Familiar with the common log files in the Linux system, understanding the analysis and solutions of general failures, will help the administrator to quickly locate the failure point, "prescribe the right medicine", and solve various system problems in time
Due to the uncertainty of the failure phenomenon, it is best to make a backup in advance before performing some simulated failure operations
One, inode and block
In the new operating system, in addition to the actual content, files usually contain many attributes, such as the file permissions of the Linux operating system (rwx, r stands for readable and accessible; w stands for writable and modifiable; x stands for executable ) And file attributes (owner, group, time parameters, etc.)
The file system usually stores these two parts in inode and block respectively
1 Overview
Files are stored on the hard disk, the smallest storage unit of the hard disk is "sector", each sector stores 512 bytes
1.1 block
Generally, eight consecutive sectors form a "block", a block is 4K in size, which is the smallest unit of file access
When the operating system reads the hard disk, it will not read one sector one sector at a time, but read multiple sectors at a time, that is, read one block at a time
1.2 Meta information
File data includes actual data and meta information (similar to file attributes)
Meta information is information about information, used to describe the structure, semantics, purpose, and usage of the information, such as the creator, creation date, file size, and file permissions of the file.
1.3 inode
File data is stored in "blocks", you also need to find a place to store file meta-information
This area is called inode, the Chinese translation is "index node". I-node
In summary, a file must occupy one inode and at least one block
Store content
storage location
Meta information
inode
data
block
2. Inode content
Inode contains a lot of file meta information, but does not include the file name
There are two ways to view the inode information corresponding to the file name
ls -i file name (only file name and size are displayed)
Linux system files have three main time attributes (see the picture above)
Recently accessed (Access): the last time the file or directory was accessed
Recently changed (Modify): the last time a file or directory (content) was modified
Recent changes (Change): the last time a file or directory (attribute) was changed
3. The structure of the directory file
As mentioned above, indo does not contain the file name; the file name is stored in the directory, and in the Linx system, everything is a file, so the directory is also a kind of file
The directory file structure is as shown below
File name 1
inode number 1
File name 2
inode number 2
…
…
Each inode has a number, and the operating system uses the inode number to identify different files
The Linux system does not use the file name, but uses the inode number to identify the file
For users, the file name is just an "alias" for easy identification of the inode number
File name and inode number are in a corresponding relationship, each inode number corresponds to a file name
4.inode number
(Example) The user uses the file name to open the file, but there will be three steps in the system:
The system will first find the inode number corresponding to the file name
Then get the inode information through the inode number to check whether the user has the permission to access the file
If so, it will point to the block where the corresponding file data is located according to the inode information, and read the data and display it to the user (if not, it will return Permission denied, that is, no permission)
Structure after hard disk partition
file name
→
Directory item
/
Directory block
Meta information
→
inode
/
inode table block
data
→
block
/
block data area
4. The size of the inode
Inode also consumes hard disk space, each inode size is generally 128 bytes or 256 bytes
When formatting, the system will automatically divide the hard disk into two areas
One is the data area, storing file data
The other is the inode area, which stores the information contained in the inode
Normally, you do not need to pay attention to the size of a single inode, but the total number of inodes; the total number of inodes is given when formatting
Execute "df -i" to view the total number of inodes corresponding to each hard disk partition and the number of inodes used
Since the inode number is separated from the file name, this mechanism causes the following unique phenomena in the Linux system
If the file name contains special characters, it may not be deleted normally. In this case, you need to delete the inode directly to delete the file.
Moving files or renaming files will only change the file name and will not affect the inode number
After opening a file, the system will identify the file by its inode number instead of considering the file name
After the file data is modified and saved, a new inode number will be generated
Under this mechanism, the software can be updated without shutting down and does not need to be restarted, because the system uses the inode number to identify the running file instead of the file name . In this way, when updating (just like you are entering The process of editing with vim editor), the new version of the file will generate a new inode with the same file name, without affecting the running file. When the software is run again (wq), the file name will point to The new version of the file, and the inode number of the old version of the file is recovered at the same time (update the inode number)
Two, hard link and soft link
There are two kinds of link files under Linux
A file similar to the shortcut function of windows, which can be quickly connected to the target file or directory. This is called a soft link
The other is to generate the file name through the inode link of the file system instead of generating a new file. This is called a hard link
1. Hard link
Generally, the file name and the inode number are in a corresponding relationship, and each inode number corresponds to a file name
But the Linux system allows multiple file names to point to the same inode number, which means that different file names can be used to access the same content
The "ln" command can create a hard link, the command format is:
ln 源文件 目标
After running this command, the source file and the target file have the same inode number, and both point to one inode. The "number of links" in the inode information will increase by 1 at this time.
When a file has multiple hard links, and then modify the file content, it will affect all file names, but deleting a file name will not affect other file names, deleting a file name will only make the inode number "Number of links" minus one
Hard links are almost never applied in the actual environment, and hard links to directories cannot be made
Example:
Create a new file, check it, you can see the inode number and the number of links
Now create a hard link, you can see that the inode number is the same, and the number of links increases by one, it becomes 2.
Deleted the source file "xcf" and found that the hard links were not affected, but the number of links decreased by one and changed back to one
2. Soft link
A soft link is to create a separate file, and this file will let the data read point to the file name of the file it is connected to
For example: Although the inode numbers of file A and file B are different, the content of file A is the path of file B. When reading file A, the system will automatically direct the visitor to file B. At this time, file A is called file B's "Soft link" or "symbolic link"
This means that file A depends on file B and exists. If file B is deleted, file A will start reporting errors. This is the biggest difference between soft link and hard link: file A points to the file name of file B, not the inode Number, the inode "number of links" of file B will not change accordingly
Format for creating soft links:
ln -s 源文件或目录 目标文件或目录
Example
Create a new file and create a soft link for it. At this time, it is found that the inode number has changed, and it is obvious that the soft link file is "xcf2->xcf1" pointing to the source file
Delete the source file xcf1 and find that xcf2 starts to report errors.
3. Summary
/
Soft link
Hard link
inode
Soft link source files & link files have different inode numbers, which are two different files
Hard link source file & link file share an inode number like a file with multiple file names, indicating that they are the same file
File attributes
The soft link clearly points out that it is a linked file
The hard link is not written out, and is essentially equal to the source file
Cross file system establishment
stand by
not support
Number of links
The number of soft links will not increase, and the file size will be different (it can be understood as the relationship between the shortcut and the corresponding source file)
Hard link shows that the file size is the same as the source file