Understanding of the Inode of Linux

Source: Internet
Author: User
Tags disk usage

First, the question:
  When creating a file in the/data partition of a Linux server, the system prompts for insufficient disk space, uses the df-h command to view the disk usage, finds that the/data partition uses only 66%, and has 12G of space left.
Second, the analysis of the problem:
  Using Df-i to see the index node (inode) of the/data partition, the discovery is full (iused=100%), which prevents the system from creating new directories and files.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/45/AF/wKiom1PppjbiKYNvAAAIM2ONW70279.gif "title=" Inode.gif "alt=" Wkiom1pppjbikynvaaaim2onw70279.gif "/>

Inode into Chinese is the index node, each storage device (such as a hard disk) or storage device partition is formatted as a file system, there should be two parts, part of the inode, and the other part of the Block,block is used to store data. The inode is the information that is used to store this data, including file size, owner, attribution user group, read-write permission, and so on. The Inode indexes the information for each file, so there is a value for the inode. According to the instructions, the operating system can find the corresponding file by the Inode value.
The server's block, while remaining, but the inode is full, so when you create a new directory or file, the system prompts for insufficient disk space.

Third, Reason:
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.
Four, the solution:
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:
Ln-s/opt/newcache/data/cache
3, increase the space

I. Inode

the minimum storage unit for a 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 from one sector to the next, so it is too inefficient to read a "block" at a time, and each "block" consists of eight contiguous sector. This "block", composed of multiple sectors, is the smallest unit of file access. The size of the "block", the most common is 4KB,file data is stored in the "block", you 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". each file has a corresponding inode, which contains some information about the file.

Ii. contents of the Inode

The inode contains the meta-information for the file, with the following content:

* Number of bytes in the file

* User ID of the owner of the file

* The group ID of the file

* file read, write, execute permissions

* File timestamp, total three: CTime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, atime refers to the time when the file was last opened.

* Number of links, that is, how many filenames point to this inode

* Location of File data block

You can use the Stat command to view inode information for a file:

[[Email protected]_server home]# stat zabbix/file: ' zabbix/' size:4096 blocks:8 IO block:4096 Direct orydevice:803h/2051dinode:130647 links:4access: (0700/drwx------) Uid: (500/zabbix) Gid: (500/zabbix) Ac cess:2014-08-12 10:46:02.810999643 +0800modify:2014-07-23 07:19:01.146989703 +0800change:2014-07-23 07:19:01.146989703 +0800[[email Protected]_server home]#

All file information except the filename exists in the inode. (Why no file name, continue to see below)

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.

Use Df-i to see the total number of inode and the number of uses for each hard disk partition

[[email protected]_server ~]# df -ifilesystem       inodes  iused   ifree iuse% mounted on/ dev/sda3      1166880 103467 1063413    9% / tmpfs           128806       4  128802    1% /dev/shm/dev/sda1         51200     38   51162    1% /boot/ dev/sr0             0       0       0     - /media/rhel_6.5  i386 disc 1[[email protected]_server ~]# 

To view the size of each inode node

[Email protected]_server ~]# dumpe2fs-h/dev/sda3 | Grep-i "inode size" dumpe2fs 1.41.12 (17-may-2010) inode size:256[[email protected]_server ~]#

DUMPE2FS is not joined to path by default and needs to be added manually

[Email protected]_server ~]# export PATH=/SBIN/DUMPE2FS: $PATH [[Email Protected]_server ~]# echo $PATH/sbin/dumpe2fs:/ Home/script:/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/bin:/usr/sbin:/usr/bin:/home:/root/bin[[email Protected]_server ~]#

Since 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 the hard disk, and the instance scenario above occurs.

Iv. inode Number

each inode has a number . , 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 inside 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:

[Email protected]_server zabbix]# ls-i indoe.txt 130698 indoe.txt[[email protected]_server zabbix]#

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. the ls-i command lists the entire directory file, the file name and inode number:

[Email protected]_server home]# ls./zabbix/indoe.txt[[email protected]_server home]# ls./zabbix/-i130698 indoe.txt[[ Email Protected]_server home]#

If you want to view the details of a file, you must access the Inode node based on the inode number and read the information. The ls-l command lists the details of the file.

[Email protected]_server home]# LL./zabbix/total 0-rw-r--r--1 root root 0 in 15:02 indoe.txt[[email protected]_serv ER home]#

In summary, the Read permission (r) and Write permission (W) for the directory file are for the directory file itself. Because only the file name and inode number are in the directory file, if you have Read permission, you can only get the file name and cannot get additional information because the other information is stored in the Inode node, and the information inside the Inode node requires execute permission (x) of the directory file.

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 destination file

[[email protected]_server ~]# cat /home/zabbix/indoe.txt hard link[[email  protected]_server ~]# ll -i ! $ll  -i /home/zabbix/indoe.txt130698 -rw-r--r--  1 root root 10 Aug 13 15:21 /home/zabbix/indoe.txt[[email  protected]_server ~]# ln !$ indoehl.txtln /home/zabbix/indoe.txt indoehl.txt[[ Email protected]_server ~]# cat indoehl.txt hard link[[email protected]_ server ~]# ll -i indoehl.txt 130698 -rw-r--r-- 2 root root  10 aug 13 15:21 indoehl.txt[[email protected]_server ~]# ll /home/ zabbix/indoe.txt -rw-r--r-- 2 root root 10 aug 13 15:21 /home/ zabbix/indoe.txt[[email protected]_server ~]# ll -i /home/zabbix/indoe.txt  130698 -rw-r--r-- 2 Root root 10 aug 13 15:21 /home/zabbix/indoe.txt[[email protected]_server  ~]# rm -f indoehl.txt [[email protected]_server ~]# ll -i / home/zabbix/indoe.txt 130698 -rw-r--r-- 1 root root 10 aug 13  15:21 /home/zabbix/indoe.txt[[email protected]_server ~]#

The hard-link 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.

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 (with hidden directories).

Seven, soft link

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.

Ln-s command to create a soft link: ln-s source file or directory destination file or directory

[[email protected]_server ~]# cat /home/zabbix/indoe.txt soft link[[email  protected]_server ~]# ll -i ! $ll  -i /home/zabbix/indoe.txt130698 -rw-r--r--  1 root root 10 Aug 13 15:35 /home/zabbix/indoe.txt[[email  Protected]_server ~]# ln -s !$ indoesl.txtln -s /home/zabbix/indoe.txt  indoesl.txt[[email protected]_server ~]# ll -i /home/zabbix/indoe.txt130698  -rw-r--r-- 1 root root 10 aug 13 15:35 /home/zabbix/indoe.txt[[ Email protected]_server ~]# ll -i indoesl.txt 922388 lrwxrwxrwx 1  root root 22 aug 13 15:36 indoesl.txt -> /home/zabbix/ indoe.txt[[email protected]_server ~]# cat indoesl.txt soft link[[email  Protected]_server ~]# rm -f /home/zabbix/indoe.txt [[email protected]_server ~]# cat indoesl.txt cat:  indoesl.txt: no such file or directory[[email protected]_server ~]#

Viii. the special role of the Inode

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.

This article is from "I am not a rookie" blog, please be sure to keep this source http://justinpeng.blog.51cto.com/7662323/1539481

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.