Introduction
In the realm of Linux systems, understanding and managing inodes play a crucial role in maintaining efficient file storage and system performance. Inodes are data structures that store metadata about files on a filesystem. This comprehensive guide will walk you through the process of How To Get Total Inodes and Increase Disk Inode Number in Linux, explaining their significance, and guiding you through increasing the number of inodes on a disk when needed.
Table of Contents
Understanding Inodes and Their Importance
In a Linux filesystem, each inode corresponds to a single file or directory. When you create a new file or directory, the system also creates an inode to store its metadata. In essence, inodes act as a reference point to the data stored on the disk. This metadata includes details like permissions, ownership, timestamps, and pointers to the actual data blocks on the disk.
Inodes play a vital role in maintaining file system integrity and organization. They allow the operating system to keep track of files, their attributes, and their physical location on the disk. However, the filesystem has a finite number of inodes available, and this number becomes determined during filesystem creation.
Calculating Total Inodes on a Disk
To calculate the total number of inodes available on a disk, use the following formula:
Total Inodes = Total Disk Blocks × Inodes per Block
Filesystem creation usually determines the inodes per block. Common values are 128, 256, or 1024. To check the total number of inodes on a Linux filesystem, you can use the df
command with the -i
option. Open a terminal and enter the following command:
df -i
How to Get Total Inodes of Root Partition
One possible way a filesystem can run out of space is by using up all the inodes. This can happen even when there is enough free space on disk; consumption of all inodes in the filesystem can block the creation of new files. Besides, it can result in a sudden stop of the system.
To get the number of inodes of files in a directory, for example, the root directory, open a terminal window and run the following ls command, where the -l option means long listing format, -a means all files and -i mean to print the index number of each file.
[samm@linux ~]$ ls -lai /
total 28
128 dr-xr-xr-x. 19 root root 247 Jul 25 22:23 .
128 dr-xr-xr-x. 19 root root 247 Jul 25 22:23 ..
100687509 dr-xr-xr-x. 2 root root 6 May 16 2022 afs
162206 lrwxrwxrwx. 1 root root 7 May 16 2022 bin -> usr/bin
128 dr-xr-xr-x. 5 root root 4096 Jul 1 05:22 boot
128 drwxr-xr-x. 3 minio minio 24 Jul 25 23:24 data
1 drwxr-xr-x. 21 root root 3340 Jul 25 22:37 dev
67155073 drwxr-xr-x. 115 root root 8192 Jul 30 16:27 etc
67222701 drwxr-xr-x. 5 root root 46 Jul 28 18:14 home
162212 lrwxrwxrwx. 1 root root 7 May 16 2022 lib -> usr/lib
162213 lrwxrwxrwx. 1 root root 9 May 16 2022 lib64 -> usr/lib64
100687514 drwxr-xr-x. 2 root root 6 May 16 2022 media
162214 drwxr-xr-x. 2 root root 6 May 16 2022 mnt
33675293 drwxr-xr-x. 6 root root 83 Jul 28 02:16 opt
1 dr-xr-xr-x. 209 root root 0 Jun 16 04:41 proc
33554562 dr-xr-x---. 4 root root 4096 Jul 28 18:38 root
1 drwxr-xr-x. 33 root root 980 Jul 28 16:38 run
162215 lrwxrwxrwx. 1 root root 8 May 16 2022 sbin -> usr/sbin
67222702 drwxr-xr-x. 2 root root 6 May 16 2022 srv
1 dr-xr-xr-x. 13 root root 0 Jun 16 04:41 sys
33554561 drwxrwxrwt. 9 root root 4096 Jul 30 16:27 tmp
140 drwxr-xr-x. 13 root root 155 Jul 28 19:59 usr
67155078 drwxr-xr-x. 21 root root 4096 Jul 21 15:01 var
To get the total number of inodes, run the following du command.
[samm@linux ~]$ sudo du --inode /
.....
14 /home/ansible
4 /home/sammy
21440 /home
1 /media
1 /mnt
2 /opt/app-sammlinux
3 /opt/remi/libzip/lib64
.......
9 /opt/remi/libmemcached-awesome/lib64
15 /opt/remi/libmemcached-awesome
25 /opt/remi
4 /opt/ansible-project
1 /opt/containerd/bin
1 /opt/containerd/lib
3 /opt/containerd
35 /opt
269643 /
List statistics about inode usage in the root partition, use the df commands as follows :
[samm@linux ~]$ sudo df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/rl-root 23615488 138973 23476515 1% /
[samm@linux ~]$ sudo df -ih /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/rl-root 23M 136K 23M 1% /
The -h flag allows for showing information in a human-readable format.
Increase Inode Number
To increase the disk inode number, you’ll need to create a new file system with a higher inode count and then move the existing files to the new file system. This process can be time-consuming and may require additional disk space, so it’s important to plan accordingly.
Creating a New File System
To create a new file system with a higher inode count, you can use the mkfs command. For example, to create a new ext4 file system with 10 million inodes, you can run following command
[samm@linux ~]$ sudo mkfs.ext4 -i 10000000 /dev/sdb1
In this example, the new file system will be created on the device /dev/sdb1. The -t option specifies the file system type, and the -N option sets the number of inodes.
You may also issue the following command from terminal using -T flag to configure how your filesystem handles large files.
[samm@linux ~]$ sudo mkfs.ext4 -T largefile /dev/sdb1
OR
[samm@linux ~]$ sudo mkfs.ext4 -T largefile4 /dev/sdb1
In the above code we use largefile and largefile4 settings that stores 1 inode per 1Mb and 4Mb respectively.
If you want to check the number of inodes available and used you can do so with the following command.
[samm@linux ~]$ df -iT
Filesystem Type Inodes IUsed IFree IUse% Mounted on
devtmpfs devtmpfs 463401 415 462986 1% /dev
tmpfs tmpfs 468702 1 468701 1% /dev/shm
tmpfs tmpfs 819200 660 818540 1% /run
/dev/mapper/rl-root xfs 23615488 138973 23476515 1% /
/dev/sda1 xfs 524288 365 523923 1% /boot
/dev/mapper/storage-data xfs 10483712 22 10483690 1% /data
tmpfs tmpfs 93740 21 93719 1% /run/user/1101
Mounting the New File System
Once you create the new file system, you can use the mount command to attach it to a directory. For example, to mount the new file system to the /mnt/data directory, you can run following command
[samm@linux ~]$ sudo mount /dev/sdb1 /mnt/data
Moving the Existing Files
With the new file system mounted, you can now move the existing files to the new location. One way to do this is to use the rsync command, which can copy files and directories while preserving ownership, permissions, and other attributes. For example, to copy all files and directories from the /data directory to the new location, you can run following command
[samm@linux ~]$ sudo rsync -a /data/ /mnt/data/
The -a option activates archive mode, which preserves all attributes of the files and directories being copied.
Updating the File System Table
After copying the files to the new file system, the next step involves updating the file system table. This action is necessary to guarantee the automatic mounting of the new file system during boot time. To do this, you can edit the /etc/fstab file and add an entry for the new file system. For example :
[samm@linux ~]$ sudo vi /etc/fstab
/dev/sdb1 /mnt/data ext4 defaults 0 2In this example, the first column specifies the device, the second column specifies the mount point, the third column specifies the file system type, the fourth column specifies the mount options (in this case, the defaults), and the last two columns specify the dump and fsck order.
Alternative Methods Increase Inode
In addition to creating a new file system with a higher inode count, you can also employ several alternative methods to increase the disk inode number in Linux. These include :
- Resize the existing file system − If unused space exists on the disk, you should consider resizing the existing file system to potentially increase the inode count. You can achieve this task by utilizing tools such as resize2fs or xfs_growfs. However, this method can be risky and may result in data loss or corruption if not done correctly.
- Use a file system with a higher default inode count − Some file systems, such as xfs, have a higher default inode count than others, such as ext4. Using a file system with a higher default inode count can help avoid the need to increase the inode count manually.
- Use a different file system type − Different file system types have different limits on the number of inodes they can support. For example, btrfs and ReiserFS have much higher inode limits than ext4. Using a different file system type may be a viable option if the current file system is limiting performance due to its inode count.
Considerations
When you increase the disk inode number, you should take several considerations into account. First, it’s important to ensure that the file system type supports the desired number of inodes. Some file systems, such as ext2 and ext3, have a fixed inode count that cannot be changed without reformatting the disk. In contrast, ext4 and xfs file systems allow for dynamic inode allocation, which makes it easier to increase the inode count without reformatting.
Secondly, increasing the inode count can impact disk space usage. Each inode carries overhead, and this can accumulate when generating a significant number of inodes. As a result, it becomes crucial to assess the quantity of files and directories that will reside on the disk.
Conclusion
Get Total Inodes and Increase Disk Inode Number in Linux can help prevent performance issues and ensure that the file system can accommodate a large number of files and directories. However, it’s important to carefully plan and execute the process, as it can be time-consuming and may require additional disk space. By following the steps outlined in this article, you can safely and effectively increase the disk inode number in Linux.
Also Read Our Other Guides :
- How To Create Multiboot USB with Ventoy in Linux
- How To Use Rsync to Sync Local and Remote Directories in Linux
- How To Create and Use Swap File on Linux System
- How To Install and Use Linux Screen with Commands
- The 40 Most-Used Linux Commands You Should Know
- 6 Methods To Search For Files By Name In Linux
- How To Install and Config Git on Ubuntu 22.04
Finally, now you have learned How To Get Total Inodes and Increase Disk Inode Number in Linux.