Many administrative tasks in Linux require us to access the underlying filesystem on a very basic level. In general, Linux does a great job of abstracting the hardware layer from the users so that we only see a list of directories and subdirectories. The reality however, can be far more complex. In Windows, different disks and partitions are separated under unique alphabets like C: and D: . In Linux, the philosophy is that “everything is a file” and there are no such abstract separations to mirror the underlying reality.
In Linux, we can have any directory mapped to any partition or hard drive. So you might well have a folder where different subdirectories have different underlying hardware or volumes. You never know! The reference point for which partitions and volumes attach to which directories is located in a file called “/etc/fstab”. But regular users will never need to access this file, or make any changes.
The Need for Unmounting/Mounting Partitions
When a disk or partition is mapped to a certain directory that’s accessible via the regular bash commands, it’s said to be “mounted”. When you “unmount” a partition, it means that it’s not mapped to any directory. What’s the purpose of unmounting a filesystem in this way?
The reason is that sometimes you have to do some low level work directly with the disks and this can’t be done when it is mounted and is linked to a particular directory. The classic example is formatting and partitioning a volume. Operations like this deal with the physical partitioning of the hard drive on a sector by sector basis and thus end up destroying all data on it. If the disk is mounted, it’s possible that some existing process is using a file on that disk which will be destroyed, and thereby lead to the process itself failing or getting corrupted. Many processes in Linux are critical and this might lead to the system itself being irretrievably compromised.
For this reason, certain operations are impossible unless the filesystem is unmounted. So how do we go about unmounting a file system in Linux?
Unmounting a Filesystem – The Challenges
The first step is to find out which disks and volumes are mapped to which directories. In my basic test Linux installation, there is just one disk with one partition that’s mapped to the entire base directory. We can get this information by typing the following command:
This provides the following output:
I have a device called “/dev/vda” which has one main partition of 20G mapped to the based directory “/”. Obviously my entire system depends on it – every running process is using files currently on the disk.
So it’s no surprise that when I try and unmount the root directory, I get a big fat error warning. The command to unmount a device or directory is called “umount”.
Here is the output:
My system is essentially telling me not to be an idiot since there are existing processes. I can try and use the “-l” parameter where “l” stands for “lazy”. It means that the disk is unmounted, but all existing references to files by existing processes are left in tact until they become free. Then they too are cleared up.
But this can be dangerous as shown here. If I unmount my base directory, even basic commands stop working:
This makes sense. Unmounting the base directory is possible, but very very complicated. However, I add a fresh new volume to my Linux server using DigitalOcean’s flexibility:
This shows that the device /dev/sda points to the following folder “/mnt/volume-nyc1-01”. Since I just added it, there are no processes that are currently using it. Unmounting it is as simple as:
And this command works as you can see when I run the lsblk command immediately after:
Now “/dev/sda” doesn’t point to anything at all. It’s “unmounted”. I can now partition it and format it without fear of data corruption!