First, what is the inode?
To understand the inode, start with the file storage. The file is stored on the hard disk, and the minimum storage unit for the hard disk is called "Sector" (Sector). Each sector is stored 512 bytes (equivalent to 0.5KB). When the operating system reads the hard disk, it does not read each sector, so the efficiency is too low, but the one-time continuous reading of multiple sectors, that is, one time to read a block. This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block. File data is stored in "blocks", then obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of stored file meta information is called Inode, and the Chinese name is "Index node".
ii. contents of the Inode
The inode contains the meta information of the file, specifically the following: * Number of bytes of file * File owner's user ID * file's group ID * file's read, write, execute permissions * file timestamp, total three: CTime refers to the inode last change Time, mtime refers to the time the file content was last changed, atime refers to the time when the file was last opened. * Number of links, that is, how many file names point to this inode * file data block location you can use the Stat command to view the Inode information for a file: Stat Example.txt in short, all the file information except the filename is present in the inode. As for why there is no file name, the following is explained in detail.
third, the size of the Inode
The inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two zones. One is the data area, the file data is stored, and the other is the Inode area (inode table), which holds the information contained in the Inode. The size of each inode node is typically 128 bytes or 256 bytes. The total number of inode nodes, given at the time of formatting, is usually set to one inode per 1KB or 2KB each. Assuming that the size of each inode node in a 1GB hard disk is 128 bytes, and one inode is set per 1KB, the Inode table will be about 128MB in size, accounting for 12.8% of the entire drive.
You can use the DF command to view the total number of inode per hard disk partition and the quantity already used:
Df-i view the size of each inode node, you can use the following command: sudo dumpe2fs-h/dev/hda | grep "Inode size" because each file must have an inode, it is possible that the inode has been exhausted, but the hard disk is not yet full. At this point, you cannot create a new file on your hard disk.
iv. inode Number
Each inode has a number, and the operating system uses the Inode number to identify different files.
It is worth repeating that the Unix/linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify. On the surface, the user opens the file by file name. In fact, this process within the system is divided into three steps: First, the system finds the inode number corresponding to the file name, and secondly, obtains the inode information through the inode number, and finally, according to the Inode information, finds the block of the file data and reads the data.
With the Ls-i command, you can see the inode number corresponding to the file name: Ls-i example.txt
v. Catalogue files
In a unix/linux system, directory is also a file. Opening the directory is essentially opening the directory file. The structure of the catalog file is very simple, which is a list of a series of catalog items (dirent). Each directory entry consists of two parts: the file name of the included file, and the inode number that corresponds to the file name.
The LS command lists only all the file names in the directory file:
LS/ETC ls-i command lists the entire directory file, that is, the file name and inode number: ls-i/etc If you want to view the details of a file, you must access the Inode node and read the information based on the inode number. The ls-l command lists the details of the file. Ls-l/etc
Six, hard links
In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name. However, the Unix/linux system allows multiple filenames to point to the same inode number. This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. This is referred to as a "hard link". The ln command can create a hard link: The ln source file target file runs the above command, and the source file is the same as the inode number of the destination file, pointing to the same inode. One of the inode information is called the "number of links," which records the total number of filenames pointing to the inode, which increases by 1. Conversely, deleting a file name causes the "number of links" in the Inode node to be reduced by 1. When this value is reduced to 0, indicating that no file name points to the Inode, the system reclaims the inode number and its corresponding block region.
Here, by the way, the "number of links" to the catalog file. When you create a catalog, two catalog entries are generated by default: "." and ".". The inode number of the former is the inode number of the current directory, which is equivalent to the "hard link" of the current directory, and the inode number is the inode number of the parent directory of the current directory, which is equivalent to the "hard link" of the parent directory. Therefore, the total number of "hard links" in any directory is always equal to 2 plus the total number of subdirectories (including hidden directories), where 2 is the "hard link" to the parent directory and the ". Hard link" in the current directory.
Seven, soft link
In addition to hard links, there is a special case. The inode number for file A and file B is different, but the content of file A is the path to file B. When you read file A, the system automatically directs the visitor to file B. Therefore, regardless of which file you open, the final read is file B. At this point, file A is referred to as the "soft link" of File B (soft link) or "Symbolic link" (symbolic).
This means that file a depends on file B and if file B is deleted, opening file A will cause an error: "No such file or directory". This is the biggest difference between soft links and hard links: file a points to file B's file name, not file B's inode number, and file B's Inode "link count" does not change.
The ln-s command can create a soft link.
Ln-s source file or directory destination file or directory
viii. Experimental Soft and hard connection
[Email protected]]$ Touch F1 #创建一个测试文件f1
[Email protected]]$ LN F1 F2 #创建f1的一个硬连接文件f2
[Email protected]]$ ln-s F1 F3 #创建f1的一个符号连接文件f3
[[Email protected]]$ ls-li #-I parameter displays inode node information for a file
9797648-rw-r--r--2 Oracle Oinstall 0 APR 08:11 F1
9797648-rw-r--r--2 Oracle Oinstall 0 APR 08:11 F2
9797649 lrwxrwxrwx 1 Oracle oinstall 2 APR 08:11 f3-f1
As can be seen from the above results, the hard connection file F2 the same as the Inode node of the original file F1, all 9797648, however, the inode node of the symbolic connection file is different.
[Email protected]]$ echo "I am F1 file" >>f1
[Email protected]]$ Cat F1
I am F1 file
[Email protected]]$ cat F2
I am F1 file
[Email protected]]$ cat F3
I am F1 file
[Email protected]]$ rm-f F1
[Email protected]]$ cat F2
I am F1 file
[Email protected]]$ cat F3
Cat:f3:No such file or directory
As you can see from the above test, hard connection F2 is not affected when the original file F1 is deleted, but the symbolic connection F1 file is not valid
In this way you can do some related tests and get all the following conclusions:
ix. Special role of the Inode
1). Remove the symbolic connection F3, no effect on F1,F2;
2). Delete the hard connection F2, the F1,F3 also has no effect;
3). Delete the original file F1, the hard connection F2 has no effect, resulting in symbolic connection F3 invalid;
4). While deleting the original file f1, hard connect F2, the entire file will be really deleted.
Because the inode number is separated from the file name, this mechanism causes some unix/linux system-specific phenomena. 1. Sometimes the file name contains special characters and cannot be deleted properly. At this point, delete the Inode node directly, you can play the role of deleting files. 2. Move or rename the file, just change the file name without affecting the inode number. 3. After opening a file, the system will identify the file with the Inode number and no longer consider the filename. Therefore, it is generally not possible for the system to know the file name from the inode number. The 3rd makes software updates simple and can be updated without shutting down the software, without requiring a restart. Because the system through the Inode number, identify the running files, not through the file name. Update, the new version of the file with the same file name, the generation of an inode, will not affect the running files. The next time you run this software, the file name will automatically point to the new version of the file, the old file's inode is recycled.
X. Practical ISSUES
In a low-profile Linux server (memory, hard disk relatively small) in the/data partition to create files, the system prompted insufficient disk space, with the df-h command to see the disk usage, found that the/data partition only used 66%, and 12G of the remaining space, it is said that this problem does not occur. Later, using Df-i to view the index node (inode) of the/data partition, it was found to be full (iused=100%), which prevented the system from creating new directories and files.
There are a very large number of small-byte cache files in the/data/cache directory, with no more blocks, but a lot of inode.
1. Delete some files in the/data/cache directory and release part of the inode from the/data partition.
2. Use a soft connection to connect the Newcache directory in the free partition/opt to the/data/cache, using the inode of the/OPT partition to alleviate the problem of insufficient/data partition Inode:
Inode, soft connection, hard link