[Reprint] Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

Gregg said that the original operation and maintenance 2019-07-17 00:03:00
https://www.toutiao.com/i6713116869781357069/

 

Outline

Linux, everything is a file, and each file has an inode, in the inode associated with the file system will be identified by the inode number of the file, not the file name. And first find the inode when accessing files, locate the file location by block inode recorded.


1, a hard link

Although each file has an inode, but there is a possibility: multiple files of the same inode, inode number that is also metadata, block position are the same, which is what kind of situation? Can imagine the same inode inode is a record with these files, it is the same file, data block these files in the directory inode pointer to the destination is the same on behalf of, except that each corresponding file pointer name different from each other only. This same file inode is known as a "hard link" in Linux.

inode hard linked files are the same, each file has a "number of hard links" of the property, the second column is the number of links are hard to use ls -l, which indicated that the document has several hard links.

Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

 

For example, files in the directory dir1 name1 and hard link dir2 / name2 described under FIG right respectively, and their inode datablock. Here also noted that the only difference is that they are different records in the directory hard links between files. Note that the figure has a number of attributes is Link Count tag hard links.

Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

 

Each create a hard link of a file is essentially one more point to the inode inode pointer record, and the number of hard links plus 1.

The essence of deleted files is to delete the file directory where the corresponding data block in the inode pointer, it is also reducing the number of hard links, because the block pointers are stored in the inode, so not really delete data, if there are other pointers to the inode, then the block pointer of the file is still available. When a hard link is the number 1 is true then delete the file to delete the file, this time recorded in block pointer inode it will also be deleted.

Create hard links can not span partitions, because inode numbers are different file system may be the same, if a hard link allows you to create, copy to the inode number inode conflict another partition and the partition may have been used.

Method of creating hard link: ln file_target link_name.


2, soft links

Soft link is character link, the link file refers to the default character link files (note not a character), use the "l" represents its type.

Hard links can not cross file system creation, otherwise the inode number may conflict. So to achieve a soft link in order to establish a link across file systems. Since it is a cross-file system, soft links have to have their own inode number.

Shortcuts Windows system, soft links and functionally equivalent, it points to the original file, the original file is corrupted or lost, the soft link file is damaged. Content can be considered soft links inode pointer record is a string target path.

Creating ways: ln -s source_file softlink_name, remember that it is source_file <- pointing relationship link_name of (anti-arrow).

View softlink value: readlink softlink_name

Set in soft link time, source_file Although not required absolute path, but it is recommended to absolute path.


3, inode size and divided

inode size is a multiple of 128 bytes, a minimum of 128 bytes. It has a default value of size, its default value is specified by /etc/mke2fs.conf file. Different file system defaults may be different.

# cat /etc/mke2fs.conf
Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

 

同样观察到这个文件中还记录了blocksize的默认值和inode分配比率inode_ratio。inode_ratio=16384表示每16384个字节即16KB就分配一个inode号,由于默认blocksize=4KB,所以每4个block就分配一个inode号。当然分配的这些inode号只是预分配,并不真的代表会全部使用,毕竟每个文件才会分配一个inode号。但是分配的inode自身会占用block,而且其自身大小256字节还不算小,所以inode号的浪费代表着空间的浪费。

既然知道了inode分配比率,就能计算出每个块组分配多少个inode号,也就能计算出inode table占用多少个block。

如果文件系统中大量存储电影等大文件,inode号就浪费很多,inode占用的空间也浪费很多。但是没办法,文件系统又不知道你这个文件系统是用来存什么样的数据,多大的数据,多少数据。


4、ext文件系统预留的inode号

Ext预留了一些inode做特殊特性使用,如下:某些可能并非总是准确,具体的inode号对应什么文件可以使用"find / -inum NUM"查看。

  • Ext4的特殊inode
  • Inode号 用途
  • 0 不存在0号inode
  • 1 虚拟文件系统,如/proc和/sys
  • 2 根目录
  • 3 ACL索引
  • 4 ACL数据
  • 5 Boot loader
  • 6 未删除的目录
  • 7 预留的块组描述符inode
  • 8 日志inode
  • 11 第一个非预留的inode,通常是lost+found目录

所以在ext4文件系统的dumpe2fs信息中,能观察到fisrt inode号可能为11也可能为12。

并且注意到"/"的inode号为2,这个特性在文件访问时会用上。

需要注意的是,每个文件系统都会分配自己的inode号,不同文件系统之间是可能会出现使用相同inode号文件的。例如:

# find / -ignore_readdir_race -inum 2 -ls
Detailed Linux system inode principle - hard links, soft links, innodb size and delineation

 

Seen from the results, in addition to the root Inode number 2, there are several file inode number is 2, they all belong to a separate file system, some of the virtual file system, such as / proc and / sys.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11621354.html