The number of inodes has reached the upper limit

inode description

inode view command

stat

Function: List file size, number of blocks occupied by the file, block size, major device number and minor device number, inode number, number of links, access rights, uid, gid, atime, mtime, ctime

ruanyang@ruanyang-HP-ProDesk-680-G2-MT:~$ stat go
  File: "go"
  Size: 3657 Blocks: 8 IO Blocks: 4096 Normal File
Device: 802h/2050d Inode: 13137033 Hard Link: 1
Permission: (0775/-rwxrwxr-x) Uid: ( 1000/ruanyang) Gid: ( 1000/ruanyang)
Last visit: 2016-07-27 19:12:39.792720377 + 0800
Last changed: 2016-07-27 19:11:35.608719535+0800
Last modified: 2016-07-27 19:11:35.632719536+0800
Created:-

df -l

Function: View the total number of i-nodes and the number of used hard disks. File system, total blocks, used blocks, available blocks, used percentage, mount point

ruanyang@ruanyang-HP-ProDesk-680-G2-MT:~$ df -l
Filesystem 1K-blocks used free % used mount point
udev 4016008 4 4016004 1% /dev
tmpfs 805348 1508 803840 1% /run
/dev/sda2 952649644 21641472 882593248 3% /
none 4 0 4 0% /sys/fs/cgroup
none 5120 0 5120 0% /run/lock
none 4026720 26084 4000636 1% /run/shm
none 102400 56 102344 1% /run/user
/dev/sda1 523248 3456 519792 1% /boot/efi

ls -i

Function: View the inode number of each file in the directory

ruanyang@ruanyang-HP-ProDesk-680-G2-MT:~$ ls -i
13140026 7.15-task 13107213 Public 13107214 Documentation
13107203 examples.desktop 13139539 Basic ticket processing.md 13121164 Untitled document 1
13137033 go 13107212 template 13107211 download
13140325 perl5 13107217 video 13107215 music
13116327 test 13118017 Add monitoring alarm.md 13107210 Desktop
13139985 YAML-Studing.md 13107216 Image

inode explanation

Inode is an important concept, which is the basis for understanding Unix/Linux file system and hard disk storage

What is an inode?

To understand inodes, we must start with file storage. Files are stored on the hard disk, and the smallest storage unit of the hard disk is called a “sector”. Each sector can store 512 bytes (equivalent to 0.5KB)

When the operating system reads the hard disk, it does not read sectors one by one, which is too inefficient. Instead, it reads multiple sectors continuously at one time, that is, reads one “block” (block) at one time. This “block” composed of multiple sectors is the smallest unit of file access. The size of the “block”, the most common is 4kb, that is, eight consecutive sectors form a block.

The file data is stored in the block, so obviously, we must also find a place to store the metadata of the file, such as the creator of the file, the date of creation of the file, the size of the file, and so on. This area that stores file meta information is called inode, and the Chinese translation is “index node”.

Each file has a corresponding inode, which contains some information related to the file

Inode content

The inode contains the meta information of the file, specifically the following:

  • the number of bytes in the file
  • file owner uid
  • The group gid of the file
  • The r, w, x permissions of the file
  • file timestamp
    1. ctime: the time when the inode of the file last changed
    2. mtime: The time when the content of the file was last changed
    3. atime: the time the file was last opened
  • number of hard links
  • The location of the file data block

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

Inode size

The 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; the other is the inode area (inode table), which stores the information contained in the inode.

The size of each inode node is generally 128 bytes or 256 bytes. The total number of inode nodes is given when formatting, generally an inode is set every 1kb or every 2kb. Assuming that on a 1GB hard disk, the size of each inode node is 128 bytes, and an inode is set for every 1kb, then the size of the inode table will reach 128MB, accounting for 12.8% of the entire hard disk.

  • To view the total number of inodes and the used number of each hard disk partition, you can use the command:
    df -i

  • To view the size of each inode node, you can use the command:
    sudo dumpe2fs -h /dev/hda | grep "Inode size"

Since each file must have an inode, it may happen that the inode has been used up, but the hard disk is not yet full. At this time, new files cannot be created on the hard disk.

inode number

Each inode has a number, and the operating system uses the inode number to identify the file. For the system, the file name is just an alias or nickname for the inode number for easy identification.

On the surface, the user opens the file by the file name. In fact, this process inside the system is divided into three steps:

  • The system finds the inode number corresponding to the file name
  • Obtain inode information by inode number
  • According to the inode information, find the block where the file data is located, and read the data

View the inode number corresponding to the file:
ls -i example.txt

Directory file

In the Unix/Linux system, a directory (directory) is also a kind of file. Opening a directory is actually opening a directory file.
The structure of the directory file is very simple, it is a list of a series of directory entries (dirent). Each directory entry consists of two parts: the file name of the contained file, and the inode number corresponding to the file name

  • List the entire directory file, i.e. filename and inode number:
ruanyang@ruanyang-HP-ProDesk-680-G2-MT:~$ ls -i perl5/
13238464 bin 13238465 lib 13502103 man
  • To view the detailed information of the file, you must visit the inode node and read the information according to the inode number. The ls -l command lists detailed information about a file.
 ruanyang@ruanyang-HP-ProDesk-680-G2-MT:~$ ls -l perl5/
  Total usage 12
  drwxrwxr-x 2 ruanyang ruanyang 4096 Jul 14 19:06 bin
  drwxrwxr-x 3 ruanyang ruanyang 4096 Jul 14 18:49 lib
  drwxrwxr-x 4 ruanyang ruanyang 4096 Jul 14 18:49 man

The special role of inode

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

  • The file name contains special characters and cannot be deleted normally. At this time, directly deleting the inode node can play the role of deleting the file
  • Move files or rename files, just change the file name without affecting the inode number
  • After opening a file, the system identifies the file with the inode number, regardless of the file name. Therefore, generally speaking, the system cannot know the file name from the inode number.

What is the appropriate number of inode nodes?

  1. The appropriate setting for the number of inodes depends on the size of your file system and your file storage needs. Generally speaking, the larger the number of Inodes, the less file information each Inode can contain, and you can store more files. However, if the number of Inodes is too large, the resources required to manage these Inodes will increase, resulting in a decrease in system performance.

  2. By default, the mkfs.ext4 command automatically calculates the number of Inodes based on the disk size and block size. However, you can use the -N option to override the default and set a custom number of inodes.

  3. If you want to set a larger number of Inodes, it is recommended that you use 1,000,000 as a benchmark to calculate the number of Inodes required per GB of disk capacity. If your disk capacity is 10GB, you can set the number of Inodes to 10,000,000.

  4. If you are not sure how many Inodes to set, it is recommended that you use the default value and adjust it as your file storage needs increase.

Operation steps:

Backup data: Please make sure your data is backed up before operation.

Adjusting the number of inodes will format the disk. Before executing, make sure that there is no important data on the disk or back up the data first.

Unmount filesystem: Unmount the filesystem using the following command:

Unmount the currently mounted file system (/dev/sdb1), in preparation for recreating the file system

umount /dev/xvdb1

Create a file system and specify the number of inode nodes

mkfs.ext4 /dev/xvdb1 -N 18276352

Modify the fstab file

vim /etc/fstab
/dev/sda6 /data0 ext4 defaults 1 2

Mount the file system

mount -a

View the modified inode parameters

dumpe2fs -h /dev/xvdb1 | grep node