Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the content of inode in linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

What is the content of inode in linux? in view of this question, this article introduces in detail the corresponding analysis and answers, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

What is inode?

Files are stored on the hard disk, and the smallest storage unit of the hard disk is called the "Sector". Each sector stores 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it will not read it sector by sector, which is too inefficient, but read multiple sectors in succession at one time, that is, read a "block" at one time. This "block", which consists of multiple sectors, is the smallest unit of file access. The most common size of a "block" is 4KB, that is, eight consecutive sector form a block.

File data is stored in "blocks", so 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. The area where the meta-information of the file is stored is called inode, which is translated into Chinese as "index node".

II. The content of inode

Inode contains the meta-information of the file, specifically, the following:

* number of bytes in the file

* User ID of the file owner

* Group ID of the file

* permissions to read, write and execute files

* there are three timestamps for a file: ctime refers to the time when the inode was last changed, mtime refers to the last time the file content changed, and atime refers to the last time the file was opened.

* number of links, that is, how many file names point to this inode

* location of file data block

You can use the stat command to view the inode information of a file:

Stat example.txt

In short, all the file information except the file name is stored in the inode. As to why there is no file name, it will be explained in detail below.

3. The size of inode

Inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two areas. One is the data area, which stores file data, and the other is the inode area (inode table), which stores the information contained in inode.

The size of each inode node is typically 128byte or 256byte. The total number of inode nodes, which is given when formatted, usually sets one inode per 1KB or per 2KB. Suppose that in a 1GB hard disk, the size of each inode node is 128bytes, and one inode is set for each 1KB, then the size of the inode table will reach 128MB, accounting for 12.8% of the whole hard disk.

To see the total number of inode per hard disk partition and how many have been used, you can use the df command.

Df-I

To see the size of each inode node, you can use the following command:

Sudo dumpe2fs-h / dev/hda | grep "Inode size"

Since each file must have an inode, it is possible that the inode has been used up but the hard drive is not full. At this point, a new file cannot be created on the hard drive.

4. Inode number

Each inode has a number, and the operating system uses inode numbers to identify different files.

It's worth repeating that instead of using file names inside the Unix/ Linux system, inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to identify. On the surface, the user opens the file by the file name. In fact, the internal process of the system is divided into three steps: first, the system finds the inode number corresponding to the file name; secondly, it obtains the inode information through the inode number; finally, according to the inode information, it finds the block where the file data is located and reads the data.

Using the ls-I command, you can see the inode number corresponding to the file name:

Ls-I example.txt

V. Catalog files

In Unix/Linux systems, a directory is also a file. To open a directory is actually to open a directory file.

The structure of a 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 corresponding to the file name.

The ls command lists only all file names in the directory file:

Ls / etc

The 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 the 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

VI. Hard links

In general, there is a "one-to-one correspondence" relationship between file name and inode number, and each inode number corresponds to a file name. However, the Unix/Linux system allows multiple file names to point to the same inode number. This means that the same content can be accessed with different file names; changes to the file contents affect all file names; however, deleting one file name does not affect access to another file name. This situation is called "hard link".

The ln command creates hard links:

Ln source file target file

After running the above command, the source file and the target file have the same inode number and both point to the same inode. There is an item in the inode information called "number of links", which records the total number of file names pointing to the inode, which increases by 1. In turn, deleting a file name reduces the number of links in the inode node 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 area.

By the way, the "number of links" of the directory file. When you create a directory, two directory 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; the inode number of the latter 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 its subdirectories (including hidden directories), where 2 is the "hard links" of the parent directory and the "hard links" under the current directory.

7. Soft links

In addition to hard links, there is a special case. The inode numbers of file An and file B are different, but the content of file An is the path of file B. When you read file A, the system automatically directs visitors to file B. Therefore, no matter which file is opened, file B will be read in the end. At this point, file An is called the "soft link" or "symbolic link" of file B.

This means that file A depends on file B. if file B is deleted, open file A will report an error: "No such file or directory". This is the biggest difference between soft links and hard links: file A points to the file name of file B, not the inode number of file B, and the number of inode "links" of file B will not change.

The ln-s command creates soft links.

Ln-s source text file or directory target file or directory

8. The special function of inode

Because the inode number is separated from the file name, this mechanism leads to some phenomena unique to the Unix/Linux system.

1. Sometimes the file name contains special characters that cannot be deleted normally. At this point, directly delete the inode node, you can play the role of deleting files.

two。 Move the file or rename the file, just change the file name, do not affect the inode number.

3. After opening a file, the system recognizes the file by the inode number, regardless of the file name. Therefore, in general, the system cannot know the file name from the inode number.

The third point makes it easy to update the software without shutting down the software without restarting it. Because the system identifies the running file by the inode number, not by the file name. During the update, the new version of the file generates a new inode with the same file name, which does not affect the running file. The next time you run the software, the file name automatically points to the new version of the file, and the inode of the old version is recycled.

9 practical problems

When creating files in the / data partition of a low-configured Linux server (with relatively small memory and hard disk), the system prompts you that there is insufficient disk space and uses the df-h command to check the disk usage. It is found that the / data partition only uses 66%, and there is still 12 GB of space left. In theory, this problem will not occur. Later, I looked at the index node (inode) of the / data partition with df-I and found that it was full (IUsed=100%), preventing the system from creating new directories and files.

Find the reason:

There are a very large number of small-byte cache files in the / data/cache directory, which do not take up much Block, but take up a lot of inode.

Solution:

1. Delete some files in the / data/cache directory and release part of the inode of the / data partition.

2. Connect the newcache directory in the free partition / opt to / data/cache with a soft connection, and use the inode of the / opt partition to alleviate the problem of insufficient inode in the / data partition:

This is the answer to ln-s / opt/newcache / data/cache 's question about what inode in linux contains. I hope the above content can be of some help to you. If you still have a lot of questions to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report