Home Linux What is Inode in Linux? What if I run out of Inode?

What is Inode in Linux? What if I run out of Inode?

An Inode or Index Node is a data structure in Unix, which is used to denote a file or a directory. The Inode will have different pieces of information about a particular file such as its location, access mode, etc.

What is Inode in Linux

The inode is a very important topic that one should have the knowledge about if he/she wants to move any further in the field of Unix and Linux. So, in this article, we are going to learn more about Inode and what all can you do with it.

What is a File System in Linux?

Before learning about the Inode, one needs to know what a file system is. A file system is like a drawer in which one stores different files and folders.

It sets boundaries for files, if you remove the file system then your system will no longer be aware of the starting and ending point of the file.

A file system has metadata. This metadata stores different information about a file such as its location, name, permission, etc.

A file system manages the files/directories and hence determines how the data will be stored? And how the data will be fetched?

When it comes to Linux, ext4files, the inode, and directory structure accumulatively work in order to create a framework that allows the users to store every file’s metadata

File System Structure:-

File System in Linux has a tree-like structure. One can easily equate this tree to a family tree, with directories as offsprings as each directory has a parent. However, the only exception is the root directory. The root directory has no parent and is represented by a forward slash “/

Root Directory is the first directory(top-most directory) in our directory tree. Root Directories contain files and sub-directories which contain more files and subdirectories, and so on.

Absolute Vs Relative paths:-

Absolute Path: An Absolute Path starts with the root directory and the directory trees branch by branch until the desired directory or path is reached.

For example, if you need to reach a document named “phone.txt”, located inside class” placed under “home” then the absolute path would be “ /home/class/phone.txt”

Relative Path: The relative path starts from the current working directory and then move backward. 

For example, if your current working directory is home and you need to access the text file “phone.txt”, then the relative path would be “/class/phone.txt”.

Inode Number:-

Inode Number is a unique number associated with a Linux file, this number is assigned by the Linux and not by the user. 

The inode number will never contain the file’s name and content rather it stores 

  • the size of the file
  • Permission
  • Owner’s name
  • Group owner’s name
  • Last updated time
  • ACL setting
  • link count 
  • pointer to the location where the file is stored.

Use of the “i” Command:-

The “i” command is assigned to the Inode number in Linux and it can be used to do lots of things. The “i” stands for Index and it combined with the df command is going to be used in this article.

The file system on the first partition on the first hard drive:-

To check the file system on the first partition on the first hard drive we are going to use the “df -i /dev /sda1” command.

What is Inode in Linux

Now let’s talk about the output for a bit.

  • udev, is the name of the file system on which the command runs.
  • Inodes:488839, represent the total number of inodes available in the file system.
  • Iused:560 represents the number of inodes that the user/s is/are using.
  • Ifree:487829 represents the number of inodes that the user/s is/are not using i.e; free.
  • Iused%:1% represents the percentage of inodes that the user/s is/are using.
  • Mounted on /dev is this file system’s mount point.

As you can see in this example, I have used only 1% of the total inode, that a file system contains. 

Can one run out of Inodes?

Theoretically speaking, yes you can run out of Inodes. But the fact of the matter is Inode’s scarcity is a very extreme condition. You need to be storing a huge quantity of tiny files to run out of inodes. 

You can analyze the abundance of Inodes through the fact that in Ext3 or Ext4, the users are provided with 1 inode per 16 kibibytes of disk space. Which is a lot, if you think about it. 1kibibytes is equal to 1024 bytes.

Therefore, the first thing that you should be concerned about is not your Inode count rather it’s your disk space. As the chances of you running out of space before having to face Inode scarcity is very high.

Now, let’s prove this statement with an example.

First of all, we are going to check the disk block’s size available on your file system. To do that, we are going to use the “sudo blockdev –getbsz /dev/sda” command.

What is Inode in Linux? What if I run out of Inode?

As you can see in the ScreenShot that in my system, the disk block’s size available on your file system is 4096. 

Now we are going to use this block size to get the check the disk usage. To do that, we are going to use “df -B 4096 /dev/sda1”.

What is Inode in Linux? What if I run out of Inode?

As you can see in the image above, my disk usage is 0% which is comparable to inode usage. 

Now, let’s understand the entity of the image.

  • udev, is the name of the file system on which the command runs.
  • 4K-block:488389 is the amount of 4KB blocks available in your file system.
  • Used:0, this states the number of 4KB blocks used by me is 0.
  • Available:488389 is the amount of 4KB blocks available in my file system.
  • Used%:0% is the percentage of used blocks.
  • Mounted on /dev is this file system’s mount point.

Now, as mentioned earlier, I am not under the risk of running out of Inodes. Even though my disk usage is less than the inode usage still the risk is not there because of the fact that the difference is so low.

Note: Even if in your case the difference exceeds still you shouldn’t panic if the inode used is less than the disk usage.

What if I run out of Inode?

First of all, you need to know that it’s almost impossible for someone to run out of inode. But in the worst possible scenario if you run out of Inodes you need to increase the capacity of the disk.

The reason why your computer ran out of Inode is the formation of tiny files. And if your system’s Inode entry is filled, which means, your computer probably has zillions of tiny files.

But if you want to delete the files separately then it would take your entire life to find out those files and delete them. Therefore, the best option to delete these files would be to reformat the disk and to do that use the “mkfs.ext4 -i” command.

Use the “i” command to list Inode information:-

After the creation of a new file by the user, a file name and an inode number is assigned to it. To get the inode information of each files one needs to use the command “df -hi”. 

What is Inode in Linux? What if I run out of Inode?

As you can see in the image above the information about all the inodes is listed with the help of this command.

You can even use the command “df -i” but the result won’t be in a human-readable format. Therefore the command “h” is used to get the result in a human-readable format.

Inodes and Links:-

Inodes allow users to create symbolic links. These links tends to create shortcut to a particular file. Withthe help of which one can easily be redirected to a particular file/directory in the file system.

First of all we are going to use the “ls -li” command to check the directory.

What is Inode in Linux? What if I run out of Inode?

Soft Link:-

Now let’s create the soft-link of the file star.sh present in the directory.

To do that, we are going to use “ls -s script.sh tech.sh” command, and the we are going to use the “ls -li *.sh” command to check the links.

As you can see in the screenshot below.

What is Inode in Linux? What if I run out of Inode?

Now let’s check, what will happen to your content after you have deleted the original file. To do that we are going to write “rm star.sh”, which was the original file. Now, use “cat tech.sh”, which is the link.

What is Inode in Linux? What if I run out of Inode?

Now, as you can see that we can’t access the tech.sh because we have not copied the content there instead the tech.sh was a shortcut to star.sh.

Hard Link:-

Hard Link is used when you need to give the newly created link the same permission as you have given to the original file.

To do that we are going to use, two text files.

The command is “ln unverse.txt star.txt”.

What is Inode in Linux? What if I run out of Inode?

As you can see in the image above that the link is created.

Now, let’s look at newly created link “star.txt”, with the help of “stat star.txt”

What is Inode in Linux

As you can see in the image above, the star file has two links. Which means the number of hard-links pointing towards this file=2.

Directory Inodes:-

Inode command i.e; “I” command can also be used to directory’s inode number.

To do that, we are going to use the “ls -lid star” command.

You can use any directory in the place or star.

As you can see in the image above, that the inode number of this file is 7874038.

One thing about Inode that you need to know is the fact that the Inode number of the directory and all the content are related. They depend on the dots(.). Before discussing any thing about them let’s look at the Inode of the content of the directory.

To do that, we are going to use “ls -lia star”. The “a” command means all, which means the directory as a monolith.

What is Inode in Linux

As you can see the image above, the one with one dot, as the same Inode value as the directory, on the other hand, the Inode number of the entity with two dots is the same to that of the home directory.

Summary:-

Let’s wrap-up the article with some key points from the article.

  • An Inode or Index Node is a data structure in Unix.
  • A file system has metadata. This metadata stores different information about a file such as its location, name, permission, etc.
  • File System in Linux has a tree-like structure. 
  • Root Directory is the first directory(top-most directory) in our directory tree. Root Directories contain files and sub-directories which contain more files and subdirectories, and so on.
  • Use the “df -i /dev /sda1” command to check the first partition on the first hard-drive.
  • Use the “sudo blockdev –getbsz /dev/sda” command to check the disk’s block size.
  • Use the “df -B 4096(block size) /dev/sda1” command to check the disk usage with the help of block size.
  •  To reformat the disk and to do that use the “mkfs.ext4 -i” command.
  • Use “df -hi” to get Inode information in a human-readable format.