Check the health and availability of your Linux servers for optimal performance with Site24x7's Linux monitoring tool.
Inodes keep track of all the files on a Linux system. Except for the file name and the actual content of the file, inodes save everything else. It’s like a file-based data structure that holds metadata about all of the files in the system.
Inodes, or “index nodes,” were first introduced in the Unix operating system in the 1970s. In the ’90s, Linux introduced the same concept that continues to be in use. To a large extent, the entire operating system depends on inodes for proper functioning. This post will explain why.
On a Linux system, files are stored in the form of blocks of a certain size. If a file is larger than this predetermined size, it’s broken down into chunks and stored in empty blocks wherever they’re available on the disk. With an increase in the number of files, this can easily get confusing. Inodes help the system organize data.
Even though inodes don't store the actual content of the file, they hold the locations of the various chunks for every file, along with other metadata, including:
As inodes don't store a file name, it doesn't matter if we cut and paste a file to a different location on the virtual disk or rename it—the file will still be physically located at the same location. This requires no change to the inode data, which is one of the features that makes them so flexible.
While inodes can help manage files, they also come with certain limitations. Just as files are stored in units of a given size, inodes are also stored in blocks, making it possible for a file as large as 4 MB to single-handedly overrun an inode that is 32 bytes.
When this happens, an indirect pointer in the inode points to another inode. A file that is hundreds of MBs can cause a chain of inodes to be used just for one file, and this will quickly deplete the number of available inodes.
Even with small files, if there are enough of them in a system, all the available inodes can be exhausted because there are only a limited number of inodes available in a system. While it’s possible to increase this number, it's not without risk. Changing the number of inodes requires a complete disk rebuild, which could lead to complete data loss. This is because the command mkfs has the option i, which can be used to set the bytes-per-inode parameter while formatting a disk. A smaller number here would ensure more inodes are created for a given disk.
There are a few indicators that a system has run out of inodes. Since inodes are necessary to store the location of a file, when a new file is created, it just won’t show up on the computer. This will happen even if there’s enough storage available on the disk.
And there can be other issues as well, such as applications crashing, the operating system restarting or scheduled tasks not running. If these problems occur frequently, it’s a sign of exhausted inodes.
Every Linux system has its own inode limit based on the system hardware. We can use the "disk filesystem" (command df) to get these numbers. For example, the screenshot in figure 1 shows the inode information from a MacBook Pro laptop with a 1 TB storage disk.
Fig. 1: df -i outputThe command used here is:
df -i
This output shows multiple columns with the following meanings:
Filesystem
: All of the disks mounted on the systemSize
: Size of the disksUsed
: Used storage on the file system, presented in GiB (gibibytes)Capacity
: The percentage of storage usediused
: The number of inodes usedifree
: The number of inodes free%iused
: The percentage of inodes usedMounted on
: The location where the disk is mountedThis command makes it easy to check the number of inodes and the amount of storage available. The total number of inodes available for a given disk can be calculated by adding the values in the iused
and the ifree
columns. Though high, for modern computers, these numbers are still limited. Theoretically, the maximum number of inodes a system can have is 2^32, or 4.3 billion inodes. But practically, the number is close to 1:16 KB of the system capacity.
As noted above, even if there's enough storage space available on the disk to create new files, inodes could still be exhausted. For example, in figure 1, there's 480 MiB of storage space available and 4,910,520 inodes free in the filesystem /dev/disk1s2
.
That’s a lot of inodes. But if we create 4,910,520 files of just 1 byte each on that disk, it'll take up all the available inodes while consuming only 5 MB of storage space. So, even though there’s more than 400 MiB of space available on the disk, no new files can be created. This is the problem with running out of inodes on a Linux system.
You could argue that nobody is going to create 4 million small files, and that may be true. But some applications might. For example, an application might save each email as a file on the disk, and over time, lead to many small files.
Once the warning signs mentioned earlier start to appear, the first thing to do is to use the df -i
command to check the number of inodes available. This command will provide the actual number of inodes, making it easy to see if the inodes are completely exhausted or are just running low.
Once you’ve established that the issue is related to the inodes, the next step is to delete all unnecessary files on the disk. This requires identifying such files at their source. Using the email application example, it would be helpful to configure the application to save all emails in a particular location. You can then delete those small files from that location or directly from the application itself.
Another way to get some inodes back is to clear the cache and temporary files. Depending on the operating system or Linux distribution of choice, the location for storing temporary files changes. This is true for cached files as well.
The next place to look is the directories where temporary files are stored—and are often forgotten. An example is the downloads directory, which can be cleared of unnecessary files.
Finally, sorting all files in a given location or directory by file size makes it easy to identify small files you might not need anymore and can therefore delete. The command to list files in ascending order by file size is:
ls -laShr <path_to_directory>
Figure 2 below shows a screenshot of this command’s output, listing all files and directories at the given location on aMacBook Pro laptop.
Fig. 2: ls -laShr outputThe delete file command can be piped with this command to automatically delete unneeded small files. However, deleting files this way can be dangerous, especially if the user doesn’t know what files are being targeted. Naturally, deleting files that are required for the proper functioning of an application is going to cause issues.
If deleting files isn’t an option, the only other way to increase the number of inodes is to repartition the complete disk, which, if not done properly, could result in data loss or other issues.
Though it’s rare, inode issues are not unheard of. Understanding inode usage helps diagnose the problem correctly. Using a few of the most commonly used Linux commands, we can easily confirm whether inodes are the source of the problem or not.
There are also a variety of Linux commands to easily identify small and unnecessary files for deletion. This ensures that there are enough inodes available to create new files to ensure optimal system performance.
Write for Site24x7 is a special writing program that supports writers who create content for Site24x7 “Learn” portal. Get paid for your writing.
Apply Now