The fourth stage of improvement
Time: August 29, 2023
Participants: All members of the class
content:
In-depth analysis of file system principles
Table of Contents
1. Overview of Inode and Block
(1) View the inode information of the file: stat
(2) Detailed explanation of Atime, Mtime and Ctime:
(3) Operation examples:
2. Structure of directory files
3. Inode number
(2) Check the inode number of the file
(3) File storage location comparison
(4) Delete the file corresponding to the specified inode number
(5) Check the inode and block information of the file system
(6) Specify the number of inodes and block size of the file system when formatting
(7) Solving disk failures caused by inode exhaustion
4. Principles of soft and hard links in Linux file system
(1) Hard link
(2) Soft link
(3) Comparison of hard links and soft links
1. Overview of Inode and Block
Files are stored on the hard disk. The smallest storage unit of the hard disk is called a “sector”, and each sector has 512 bytes.
When the operating system reads data from the hard disk, it does not read it in sectors, which is too inefficient. Instead, it reads multiple sectors continuously at one time, that is, one “block” is read at one time. , consisting of multiple sectors of space).
This “block” consisting of multiple sectors is the smallest unit of file access. The most common size of “block” is 4KB, that is, eight consecutive sectors form a Block. Block stores file data.
File data is stored in “blocks”, so you must find a place to store the meta-information of the file, such as the user to whom the file belongs, the group to which the file belongs, the type of file, the permissions of the file, the creation time of the file, the modification time of the file, the Access time, Block information used by the file, number of hard links to the file, file size and other attribute information.
This area that stores file metainformation is called Inode. The Chinese translation is “index node”, also called i node.
Inode and Block are the basic core concepts of the file system< /strong>, the file system is formed when the partition is formatted, and the file system is responsible for organizing the storage of files on the partition.
Note:
Inode does not contain file name. The file name is stored in the directory entry of the directory.
A file must occupy an inode and at least one block.
(1) View the inode information of the file inode information: stat
[root@localhost ~]# stat 1.txt
(2) Atime, Mtime , Ctime detailed explanation:
English |
nickname |
Chinese translation |
When to modify |
View command |
Access |
Time |
interview time |
read |
ls -lu |
Modify |
Mtime |
Change the time |
write, modify |
ls -l |
Change/Create |
Ctime |
change time/creation time |
Modify file name, write, modify, change permissions, and make links |
ls -lc |
(3) Operation example:
1. Read the file content and check the changes in Atime:
[root@localhost ~]# echo haha > a.txt
[root@localhost ~]# stat a.txt
[root@localhost ~]# cat a.txt
[root@localhost ~]# stat a.txt
2. Write the file content and check the changes of Mtime/Ctime
[root@localhost ~]# stat a.txt
[root@localhost ~]# echo hehe > a.txt
[root@localhost ~]# stat a.txt
3. Modify file permissions and view changes in Ctime
[root@localhost ~]# stat a.txt
[root@localhost ~]# chmod 777 a.txt
[root@localhost ~]# stat a.txt
4. Create a hard link and view the modification of Ctime
[root@localhost ~]# stat a.txt
[root@localhost ~]# ln /root/a.txt /tmp/a.txt
[root@localhost ~]# stat a.txt
5. Modify the file name and check the changes in Ctime
[root@localhost ~]# stat a.txt
[root@localhost ~]# mv a.txt aa.txt
[root@localhost ~]# stat aa.txt
6. Touch update time, modify all three times
[root@localhost ~]# stat aa.txt
[root@localhost ~]# touch aa.txt
[root@localhost ~]# stat aa.txt
2. Structure of directory files
A directory is also a type of file
Each inode has a number, and the operating system uses the inode number to identify different files.
The file system does not use file names internally to refer to files, but uses inode numbers to identify files. For the file system, the file name is just another name for the inode number for easy identification, and the file name is the data of the directory.
3. Inode number
(1) Steps for file system access to files:
1. The user sees the name of the file to be accessed in the directory
2. Find the inode number corresponding to this file name through the directory data
3. Obtain inode information (metainformation of the file) through the inode number
4. According to the inode information, find the block where the file data is located and read the data.
Inodes are generally 128 bytes or 256 bytes. Each inode records the block number used by the file. Each piece of information recording the block number occupies 4 bytes.
The record of block numbers in the inode contains a total of 12 direct, 1 indirect, 1 double indirect and 1 triple indirect.
12 direct connections occupy a total of 48 bytes of disk space and contain 12 information directly pointing to the block number. If the default block size of this file system is 4KB, it can only point to file content of 12*4=48KB.
However, if our file is larger, we need to use more blocks, definitely more than 12 blocks, and then there are indirect, double indirect and triple indirect.
Indirect: Refers to the 4-byte content recorded in the inode, which points to a block. What is stored in this block is not the real file content, but the real file. For the block number information stored in the location, if each block size is 4KB, then 1024 block number information can be stored, and the size of a file that can be stored indirectly is 1024*4=4096KB.
Double indirection: You can store larger file contents, that is, indirection is performed on the basis of indirection. If the block size is the default 4KB, then 1 double The file content size that can be stored indirectly is: 1024*1024*4=4096MB
Triple indirection: That is, indirection is performed on the basis of double indirection. If the block size is the default 4KB at this time, the file content size that can be stored in one triple indirection is :1024*1024*1024*4=4096GB
That is to say, in a file system with a default block size of 4KB, the maximum storage of a file can reach 48KB + 4096KB + 4096MB + 4096GB, which is approximately 4100GB.
(2) View the inode number of the file
ls -i filename
stat file name
Example:
[root@localhost ~]# ls -i aa.txt
[root@localhost ~]# stat aa.txt
(3) File storage location comparison strong>
SuperBlock stores all Inode and block related information of the file system.
When a user tries to access a file in the Linux system, the system will first search its inode based on the file name to see if the user has the permission to access the file. If yes, point to the corresponding data block; if not, return Permission denied (access denied)
(4) Delete the specified The file corresponding to the inode number
Format: find ./ -inum inode number -exec rm -i {} \;
Example:When the file name contains special symbols, we can delete the inode number to delete the file.
[root@localhost ~]# touch ‘a b`_c de ! 4|’.txt
[root@localhost ~]# ls -li
[root@localhost ~]# find ./ -inum 34097384 -exec rm -i {} \;
rm: Do you want to delete the ordinary empty file “./a b`_c de ! 4|.txt”? y
[root@localhost ~]# ls
Look for files modified within 3 days from the /root/ directory and copy them to /tmp
[root@localhost ~]# ls /tmp/
[root@agent ~]# find /root/ -mtime -3 -a -type f -exec cp {} /tmp \;
(5) View File system inode and block information
df -i device name (query when the file system is mounted, query the total number of inodes and the number used)
dumpe2fs -h device name (the file system does not need to be mounted)
tune2fs -l device name (the file system does not need to be mounted)
Example:
[root@localhost ~]# df -i /data
[root@localhost ~]# umount /data
[root@localhost ~]# dumpe2fs -h /dev/sdb1
[root@localhost ~]# tune2fs -l /dev/sdb1
(6) Specify the number of inodes and block size of the file system when formatting
mkfs.ext4 -N inode number -b block size (in bytes) device name
Example:The number of inodes is set to 60,000, and the block size is 1KB
[root@localhost ~]# mkfs.ext4 -N 60000 -b 1024 /dev/sdb1
[root@localhost ~]# dumpe2fs -h /dev/sdb1 | grep -i “inode count”
[root@localhost ~]# dumpe2fs -h /dev/sdb1 | grep -i “block size”
(7) Solve the inode Disk failure due to exhaustion
1. Delete unused files and configure disk quotas
[root@localhost ~]# find /data -mtime -1 -exec rm -rf {} \;
[root@localhost ~]# rm -rf `find /data -mtime -1` “ = $()
[root@localhost ~]# find /data -mtime -1 |xargs rm -rf
2. Back up the file, reformat the file system, and specify a larger number of inodes
[root@localhost ~]# df -i /test
[root@localhost ~]# touch /test/{1..28213}.txt
[root@localhost ~]# touch /test/hello.txt
Use the second method:
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mv /test/* /backup/
[root@localhost ~]# umount /test
[root@localhost ~]# mkfs.ext4 -N 50000 /dev/sdb2 &> /dev/null
[root@localhost ~]# mount /dev/sdb2 /test
[root@localhost ~]# mv /backup/* /test
mv: overwrite `/test/lost + found’? y
[root@localhost ~]# touch /test/hello.txt
4. Principles of soft and hard links in Linux file system
(1) Hard link (hard link)
Method: ln source file target file
Features:
The inode number of the file pointed to by the hard link. The inode number of the newly generated hard link file is the same as the inode number of the source file. Hard links cannot be made to directories and must be in the same file system. Deleting one file name does not affect access to the other.
Example:
[root@localhost ~]# df -h /data [root@localhost ~]# cd /data [root@localhost data]# ln a.txt hardlink-a.txt [root@localhost data]# ls -li
Delete the source file, hard links will not be affected
[root@localhost data]# echo “abc” > a.txt
[root@localhost data]# rm -rf a.txt
[root@localhost data]# cat hardlink-a.txt
(2) Soft link (soft link)
Method: ln -s source file or directory target file or directory
Features:
Soft links are also called symbolic links
The file name pointed to by the soft link. The inode number of the newly generated soft link file is different from the source file. The directory can also generate soft links. The soft link file and the source file do not need to be in the same file system. The content of the soft link file belongs to the source file. Path, when reading, the system will automatically direct to the source file path and find the file content based on the source file. However, when the source file is moved or renamed, the soft link will report an error.
Example:
[root@localhost data]# echo "soft test" > a.txt [root@localhost data]# ln -s a.txt softlink-a.txt [root@localhost data]# ll -i
Rename the source file, the hard link will not be affected, but the soft link will become invalid
[root@huyang1 data]# ls -li [root@huyang1 data]# ln a.txt hardlink.txt [root@huyang1 data]# ls -li [root@localhost data]# mv a.txt b.txt [root@localhost data]# ls -li [root@huyang1 data]# cat b.txt [root@huyang1 data]# cat hardlink.txt [root@localhost data]# cat softlink-a.txt
(3) Hard links and Comparison of soft links
hard link |
soft link |
|
direction |
inode number |
file name |
Are the inode numbers the same? |
same |
different |
Is it possible to target the directory |
Cannot target directories |
Can target directories |
Can it cross file systems? |
Cannot cross file systems |
Can span file systems |
Delete, rename, move source files |
not affected |
Invalid |
Create command |
ln source file target file |
ln -s source file or directory target file or directory |