Lesson 5 | Inodes and superblocks |
Objective | Describe how inodes and filenames interact. |
Inodes, Superblocks and File Interaction
Describe how inodes and filenames interact on a Unix System.
Are you a Unix System user who's curious about how inodes and filenames interact on your system? In this article, we'll dive deep into the workings of Unix file systems and explore the relationship between inodes and filenames.
Unix is a powerful operating system used in many industries worldwide. One of its most important features is its file system, which stores and manages all of the data on the system. Every file and directory on a Unix system is represented by an inode, a data structure that contains information about the file, such as its size, permissions, and location on the disk. But how do inodes and filenames interact with each other on a Unix system? Let's explore this question in more detail.
Every file or directory on a Unix system has both a filename and an inode number. The filename is the human-readable name of the file or directory, such as "example.txt" or "myfolder." The inode number, on the other hand, is a unique identifier that the system uses to locate the file or directory on the disk.
When you create a new file or directory on a Unix system, the system assigns it a new inode number and a filename. The inode number is stored in the inode table, which is a data structure that contains information about all of the inodes on the system. The filename is stored in the directory entry, which is a data structure that contains information about all of the files and directories in a particular directory.
When you access a file or directory on a Unix system, the system uses the filename to locate the appropriate directory entry. The directory entry contains the inode number for the file or directory, which the system then uses to locate the inode in the inode table. Once the system has located the inode, it can access all of the information about the file or directory, such as its size, permissions, and location on the disk.
Updating Inodes and Filenames
Inodes and filenames can be updated in a variety of ways on a Unix system. For example, if you rename a file, the system will update the directory entry for the file with the new filename. The inode number for the file remains the same, but the filename associated with the inode has changed.
Similarly, if you move a file from one directory to another, the system will update the directory entries for both directories. The directory entry for the original directory will no longer reference the inode number for the file, while the directory entry for the new directory will reference the inode number for the file.
It is also possible to update the metadata associated with an inode, such as its size or permissions. This can be done using the "chmod" or "chown" commands on a Unix system. When you update the metadata for an inode, the system updates the inode table to reflect the changes.
Inode Limitations
While inodes are a powerful feature of Unix file systems, they do have some limitations. One limitation is that the number of inodes available on a file system is fixed at the time the file system is created. This means that if you run out of inodes on a file system, you won't be able to create any more files or directories, even if there is still space available on the disk.
Another limitation is that inodes can become fragmented over time, which can slow down access to files and directories on the system. This happens when files are deleted or moved, leaving gaps in the inode table. Over time, these gaps can become scattered throughout the inode table, making it more difficult for the system to locate inodes quickly.
The administrator needs to know two key concepts when working with UNIX filesystems. The first concept is the idea of a superblock, and the second is the idea of an inode. The common UNIX filesystems are all organized around superblocks and inodes.
Superblock
The superblock contains high-level information about the disk partition, such as its label. It also contains tables of information laying out available space on the partition. Every filesystem has one active superblock, but the information there is so critical that the commonly used UNIX filesystems replicate the superblock data in other locations on the disk. Using this
replicated data, the system can sometimes recover a disk partition with a damaged superblock.
Inode
An inode is the physical representation of a file on the disk. Each inode, and therefore each file, has a number that uniquely identifies it. These inode numbers can be viewed with the –i
option to the ls
command. For example:
ls –iF .
864531 Mailbox 864354 dead.out 256671
public_html/
The numbers you see here are the inode numbers associated with each of these files (or directories).
When you create a new file on a disk, a new inode is allocated to that file. With one exception, all the attributes of that new file are really attributes of the inode. For example, the owner, group, and permission bits for the file are stored in the inode, as are the creation and modification times. The only file attribute that is not stored in the inode is the filename. This rather surprising
fact needs more explanation. In a UNIX filesystem, directory files are actually tables that relate pathnames to inode numbers. When a file with name "file" is created in the directory /home/jeremy, what actually happens
is:
- A new inode is allocated.
- The owner, group, permissions, and modification time of the new file are assigned to that inode.
Suppose that the new inode number is 1231. An entry is made in the directory /home/jeremy that says the following: file=inode 1231
. The directory entry created in this example is called
a link or a hard link. The inode 1231 is linked to the directory /home/jeremy.
To create a hard link, use the ln
command, just as you did to create symbolic links.
Part of system administration is creating viable backup solutions in case of a disaster. You can learn more about backup strategies in the next course in this series, Linux/UNIX System Administration II, but you can also use hard links to create redundant file entries and create a simple backup.
In the simulation below, you will learn how to do just that.
1) Initial Prompt
2) cd /etc
3) ls -ia
4) ls -i
5) ls -i passwd
6) 5708 passwd
7) mkdir links
8) ln passwd /etc/links
9) ls -i /etc/links/passwd
❮
❯
- One logical place to begin creating redundant hard links is the /etc/passwd file. You are already logged in as a superuser. Change to the /etc directory
- Change directory
- cd /etc
- Type cd /etc to change to the /etc directory.
- You are now in the /etc directory. It contains many configuration files, including inetd.conf, rc.local, and of course the passwd file.
Enter ls -ia.
- List,
- ls -ia
- The command you have entered is incorrect. Please check the instructions and try again.
- The ls -ia command lists the inode numbers of all files, including the current directory. This list can be quite long. For the purposes of this simulation, we're only showing you a few lines from the list. Note that the inode for the current directory (.) is 5617, and that the inode for the parent directory (..) is 2. Use ls -i to list the inode numbers of only the files in your current directory (/etc).
- List
- ls -i
- Type ls -i to list the inode numbers of the files in the /etc directory.
- Again, this listing can be quite long, so we're only showing you select lines. List the inode number for just the passwd file.
- List
- ls -i passwd
- Type ls -i passwd to list the inode number for just the passwd file.
Inodes and hard links
Here are the steps you followed in this simulation:
- One logical place to begin creating redundant hard links is the /etc/passwd file. You are already logged in as a superuser. Change to the /etc directory.
- You are now in the /etc directory. It contains many configuration files, including inetd.conf, rc.local, and of course the passwd file.
Type ls -ia
.
- The
ls -ia
command lists the inode numbers of all files, including the current directory. Note that the inode for the current directory (.) is 5617, and that the inode for the parent directory (..) is 2. Use ls -i
to list the inode numbers of only the files in your current directory (/etc).
- List the inode number for just the passwd file.
- Notice that the passwd file has the inode of 5708. Imagine the consequences if this file were accidentally deleted. To protect against this, you will create a new directory, then create a hard link to this new directory. First, create a new directory named links.
- Type
ln passwd /etc/links
to link the passwd file to a new passwd file in the /etc/links directory.
- Use the
ls -i
command to view the inode of the /etc/passwd file.
- Use the
ls -i
command to view the inode of the /etc/links/passwd file.
- The inode is the same for both files.