Hi everyone! Let's today investigate deep abyss of storing information on UNIX systems.
I know, exist so many sources information regarding this topic, but when I tried to investigate this issue from "A" to "Z" at the first time, I had look for information on different books and sites. So, in this post, I will put all my records for you, my dear reader.
I have built a big scheme but don't be scared, because on this post we will investigate it by smallest parts. And I promise that I will not skip any details.
Schema and definitions
At first, let's understand what is the filesystem. Filesystem
is the structure of data which gives the possibility to collect files on partition disk and track them by operation system.
The UNIX operating system has a personal type of file system and exactly this part we will investigate today.
Everything in Unix is considered to be a file. If we are talking about folders, or some devices (USB, Network Devices etc.), still system access to it getting from the generated file and is indicated like a file inside the OS. Below is represented detailed scheme according which one we will build this part of the topic.
All hard drives on UNIX systems are drawn up from volumes (during installation of OS, the system is doing this automatically, but additional working with space should be manual).
After mounting of the hard drive, going step of the creation of physical volumes, on which stands out spaces for volume groups.
Volume groups created for more comfortable access to logical volumes. Logical disks called partitions.
I know it scheme can makes crazy, but all these actions give elastic work with space and for administrating it's an important point.
Every partition is divided by default from three parts (in case if this primary partition, exist bootable block). Partition elements :
- Super block - layout of the disk. Consist information about size of disk and number of inodes and data blocks
- Inodes table - list of indodes
- Data block - structured data
From all this information, now we can make definition what is this "file" in the UNIX system.
- is only link on inode, where stored information. So, all permission, owners, and other data, we are giving not on file, but on inodes.
- is also file, which has inode number, data block, but inside consists additional information about parent and current directory.
Also, we should understand definitions "link". On UNIX systems exist two type of links, hard and symbol link.
- is a file. It's also referenced on the inode and from this, we have, that one inode can have few references on itself.
- is reference on file (hard link). Is using for quick and comfortable access to file.
NOTICE : If soft (symbol) link has been removed, the file will be in save, but if all hard links will be removed to file, data will be destroyed and soft links will be wrong (because the file will not exist anymore).
And now we came to the part about inodes.
- is a data structure on a filesystem, where stored all the information about a file, except the name of the file and actual data.
One inode = 128 bytes
Inode consists from the next parts :
Data block pointer
- Mode - permissions on file
- Link count - count of links on inode
- UID - owner of file
- GID - group owner of file
- Size - size of data
- Access date - last accessed date
- Modify date - last modifying date
- Inode number - number of inode
- Pointers - object, whose value the reference on data
- Block count - count of blocks of data
- is a direction to data. Data block pointers have a level of indirection to data. And for the present time present 4 levels of pointers.
Levels of indirection :
- 0 - the direct pointer on data
- 1 (Ind1) - the single indirect on 256 pointers. Each pointer is reference to the single data block
- 2 (Ind2) - the single indirect on 256 another singles indirect and then each of them on 256 pointers
- 3 (Ind3) - the single indirect on 256 another indirect, then each of them indirect on another 256 and in the end to a single data block
- is reference on block with information
- information (row of bits)
Like a separate part of the topic, I wanted to mark "permissions" on file.
Let's review bits of access :
- Owner permissions - access for owner of the file (3 bits)
- Group permissions - access for group owner of the file (3 bits)
- Other permissions - access to the file for other users (3 bits)
- Sticky bit - is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file (1 bit)
- SUID - is a special type of file permissions, which is giving permissions to a user to run a file (program) with permissions of owner (1 bit)
- GUID - is a special type of file permissions, which is giving permissions to the group to run a file (program) with permissions of owner (1 bit)
So now we can move to practice.
Working with files
We will start from command "ls" because it's a basic command for administrators. So, let's create test file via "touch" command and run "ls".
# touch test.file
# ls -la ./test.file
-rw-r--r--. 1 root root 0 Oct 14 18:02 ./test.file
- is permissions on file. First character is type of file. First character "-" it's mean that this is file, "d" is directory and "l" is link. And next row of accesses bits is permissions to owner, group and other, which are described before.
There are these permissions :
- r - read permissions (4 in numerical equivalent)
- w - write permissions (2 in numerical equivalent)
- x - execute permissions (1 in numerical equivalent)
If we want to add few permissions, we should just calculate the numerical equivalent of access and set it.
For example, if we want to give permission on file read, write and execute for owner, read and execute for group, and for other only read, we should give permissions
in numerical equivalent it will be for owner 4+2+1, for group 4+0+1 and for other 4+0+0 (754).
Give these permissions we can via "chmod" command.
# chmod 754 ./test.file
# ls -la ./test.file
-rwxr-xr--. 1 root root 0 Oct 14 18:02 ./test.file
Also via this command, we can operate with additional bits of access.
Sticky bit :
# chmod +t ./test.file
# ls -la ./test.file
-rwxr-xr-T. 1 root root 0 Oct 14 18:02 ./test.fil
For the understanding of the importance of this bit, have a look on folder "tmp". This bit give possibility to remove files only by owner (or superuser) and thanks for this, all temporary files of each process are protected.
The same with other additional bits. (u - for SUID, g - GUID)
Regarding the changing of owners and groups, use "chown" command.
For example, let's change owner to user "test" and group "testg".
# chown test:testg ./test.file
# ls -la ./test.file
-rwxr-xr-T. 1 test testg 0 Oct 14 18:02 ./test.file
Also, want to show one more useful option of "ls" command. Thanks to this command, we can know the number of inode of the specific file.
# ls -i ./test.file
4374062 - is number of inode
On this point, I finished this post and I really hope that this information was useful for you my dear friend.
Thanks for the attention,