Take you to re-understand the inode of the Linux system

Inode introduction
In the Linux operating system, the file system is divided into 2 parts, one is the data area, and the other is the metadata area. The metadata here is the inode, which accesses the file’s creator, the file’s creation date, and the modification date. Date of access, file size and other information.

Obtaining the content of the inode node
Just create a file under a certain file system and use the stat command to get the content of the inode node.

[mysql@mysql test]$ stat test.log
  文件:"test.log"
  大小:9               块:8          IO 块:4096   普通文件
设备:fd02h/64770d      Inode:8393435     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 2001/   mysql)   Gid:( 4001/   mysql)
最近访问:2020-09-28 15:34:05.452763004 +0800
最近更改:2020-09-28 15:34:05.452763004 +0800
最近改动:2020-09-28 15:34:05.517763243 +0800


How does Linux read the contents of the file when you execute the cat test.log command.

In fact, in the Linux system, the file name is not used. What is really used is the inode node number. First, the system will find the corresponding inode node number by the file name, and then get the inode node information through the inode node number, and finally, by obtaining The inode node information, read the data content required.

Take you to re-understand the inode of the Linux system

The file system inode calculation method
uses the xfs type file system as an example here.


[mysql@mysql test]$ df -Th|grep -i home
文件系统                类型        1K-块     已用     可用 已用% 挂载点
/dev/mapper/centos-home xfs        10G  324M  9.7G    4% /home

You can see that the file system type of /home is xfs, so how do you know the number of inode nodes of the /home file system?


[mysql@mysql test]$ df -i|grep -i home
文件系统                   Inode 已用(I)  可用(I) 已用(I)% 挂载点
/dev/mapper/centos-home  5242880     354  5242526       1% /home

From the above results, you can see that the total number of inode nodes in the /home file system is 5242880. Do you want to know how 5242880 is calculated? Here you can use the xfs_info command to get more detailed information.


[mysql@mysql test]$ xfs_info /home
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Here you can get isize=512, which means that an inode occupies 0.5k bytes, and maxpct=25 means that it occupies 25% of the total size of the entire file system.

So the total number of inodes can be calculated like this: 2621440 4 0.25 / 0.5 = 5242880

In a file system, inode node resources are limited. If there are too many small files, the number of inode nodes will be used up. Even if there is free disk space in the file system at this time, new files cannot be created. Do not delete them at this time. Useless files, or expand the total number of inode nodes.


xfs_growfs -m 30 /home

Using inode to delete files
is here to expand a knowledge point. When you create a file with special symbols in the file system, you can't delete it directly with the file name. Let's test it here.

[mysql@mysql test]$ ls -l
总用量 0
-rw-rw-r-- 1 mysql mysql 0 9月  28 16:13 -test.jldfa
[mysql@mysql test]$ rm -test.jldfa
rm:无效选项 -- t
Try 'rm ./-test.jldfa' to remove the file "-test.jldfa".
Try 'rm --help' for more information.
[mysql@mysql test]$
[mysql@mysql test]$ rm "-test.jldfa"
rm:无效选项 -- t
Try 'rm ./-test.jldfa' to remove the file "-test.jldfa".
Try 'rm --help' for more information.

What to do, here you can delete the
first one in two ways , use rm ./-test.jldfa according to the system prompt to delete the
second one, use the inode number to delete


[mysql@mysql test]$ ls -li
总用量 0
8393433 -rw-rw-r-- 1 mysql mysql 0 9月  28 16:13 -test.jldfa
[mysql@mysql test]$ pwd
/home/mysql/test
[mysql@mysql test]$ find /home/mysql/test -inum 8393433
/home/mysql/test/-test.jldfa
[mysql@mysql test]$ find /home/mysql/test -inum 8393433 -delete
[mysql@mysql test]$ ll

总用量 0

You can see that the file has been deleted.

Guess you like

Origin blog.51cto.com/15061930/2642096