Hard link and soft link?

I would like to give some examples to describe practical meaning of hard links,soft links and cache in memory in a linux system. I will start creating two ramdisk partitions:

# mkdir /ramdisk1
[root@tux ~]# mkdir /ramdisk2
[root@tux ~]# mount none -t tmpfs -o size=256m /ramdisk1
[root@tux ~]# mount none -t tmpfs -o size=256m /ramdisk2
# df -h | grep ramdisk
none 256M 0 256M 0% /ramdisk1
none 256M 0 256M 0% /ramdisk2

Now lets look at the free memory usage of my PC;

[root@tux ramdisk1]# free -m
             total       used       free     shared    buffers     cached
Mem:          3018       1893       1125          0        108        658
-/+ buffers/cache:       1126       1892
Swap:         5055          0       5055

According to this output, my cache data is 658MB. As you might know, cached amount isn’t actually used by processes actively but kernel keeps them in memory as they might be referenced. Instead of keeping them in the secondary storage (e.g hard disk), as long as memory is available, kernel caches them in the memory. Now we will increase this number sharply.

Above we have created two ram disk partitions each of which has 256M capacity and mounted. Lets create a 200MB file on /ramdisk1

# dd if=/dev/zero of=orig_file1 bs=1M count=200

When we check the memory usage once again;

[root@tux ramdisk1]# free -m
             total       used       free     shared    buffers     cached
Mem:          3018       2094        924          0        108        858
-/+ buffers/cache:       1127       1891
Swap:         5055          0       5055

Can you see that we incremented cached data by 200MB ? Now lets skip to hard & soft link concept:

[root@tux ramdisk1]# ls -lhi
total 201M
201439 -rw-r--r-- 1 root root 200M Mar 12 22:18 orig_file1

The number 201439 is the inode number and orig_file1 name is pointing to this inode. We can create a soft link to this file as below;

# ln -s orig_file1 orig_file1-soft
[root@tux ramdisk1]# ls -lhi
total 201M
201439 -rw-r--r-- 1 root root 200M Mar 12 22:18 orig_file1
206670 lrwxrwxrwx 1 root root 10 Mar 12 22:24 orig_file1-soft -> orig_file1

We can see that the new soft link has a different inode number. Let’s see what happens if we create a hard link to this orig_file1 ?

# ln orig_file1 orig_file1-hard
[root@tux ramdisk1]# ls -lhi
total 401M
201439 -rw-r--r-- 2 root root 200M Mar 12 22:18 orig_file1
201439 -rw-r--r-- 2 root root 200M Mar 12 22:18 orig_file1-hard
206670 lrwxrwxrwx 1 root root 10 Mar 12 22:24 orig_file1-soft -> orig_file1

The new hard link has the same inode like the original file (orig_file1). As soon as you create the hard link, link counter is increment by 1 and you see that link count as 2 in the “ls” output but hang on a second! /ramdisk1 file system has the 256M capacity how can I have 400M in this file system. This isn’t the reality indeed. Both file names orig_file1 and orig_file1-hard referring to the same inode which is 201439 therefore there is no duplication.
If you delete orig_file1;

# ls -lhi
total 201M
201439 -rw-r--r-- 1 root root 200M Mar 12 22:18 orig_file1-hard
206670 lrwxrwxrwx 1 root root 10 Mar 12 22:24 orig_file1-soft -> orig_file1

“rm” command will only decrease the link counter by 1 and nothing will happen except our symbolic link will reference a non-existent file because it doesn’t point to the location of the file (inode) but the removed file itself.
If you run “rm” command on orig_file1 once more, link count will drop to 0 and linux will delete the data by releasing its resources.

About: rtoodtoo

Worked for more than 10 years as a Network/Support Engineer and also interested in Python, Linux, Security and SD-WAN // JNCIE-SEC #223 / RHCE / PCNSE


You have a feedback?

Discover more from RtoDto.net

Subscribe now to keep reading and get access to the full archive.

Continue reading