Mounting

Introduction

This article provides:

  • an illustration of disk mounting and partitions, with explanations
  • a series of commands to depict how mounting and unmounting really works

All files in Linux are arranged in a tree hierarchy, rooted at "/". Parts of this file hierarchy can be spread across separate devices/partitions. Mounting is attaching a disk partition at a particular point in this file hierarchy.


Illustration, with explanations

Mounting

  • Attach a disk partition (and its filesystem) at a specified directory (mount point)
  • Allows read/write of files stored on the partition from that mount point.
  • e.g. mount   /dev/sda3   /boot/efi will mount the /dev/sda4 partition at the mountpoint "/boot/efi". The files on that partition can now be accessed.
  • Many people commonly refer to a disk partition by its device file (e.g. /dev/sda4), such as "please mount /dev/sda4" or "/dev/sda4 is your root partition".

Root partition

  • The fourth partition of this disk, in this case.
  • Mounted at "/" (root directory). Root directory is the root (base) of the OS, and contains all other directories and files.

EFI system partition (ESP) (/dev/sda3)

  • The third partition of this disk, in this case.
  • Mounted at "/boot/efi" in the above diagram. Common alternative mount points for the ESP include "/efi" and "/boot".
  • The ESP stores a bootloader required for the boot process. It can be mounted so that you can access its files or update it. See my article on the UEFI boot process for more information on EFI system partitions.

Disk partition vs Mount point vs Device file

  • Take a moment to note the distinction between the three.
  • A disk partition is a section (partition) of the disk. Partitioning allows neat dividing of the disk into segments, with each partition having its own filesystem and metadata.
  • A mount point is a directory that you have chosen to attach a disk partition to. After attaching the ESP to "/boot/efi", you can view the directories and files on the ESP, and create directories and write files on the ESP. If you mount the ESP at "/efi" instead, you will access the ESP from "/efi" instead.
  • A device file is used for communication between the kernel/OS and device drivers. Device files are usually automatically created and handled by the kernel/OS. Do not worry about them.

Let me explain mounting and unmounting with a series of commands

  • Below, I will mount and unmount the ESP multiple times, creating files at its mountpoint directory at certain junctions, to illustrate how everything works.
  • At any point, if you need a visual model of the filesystem hierarchy, partitions, or mounting, look at the first image on this page above.
  • Let's start...

lsblk -f is used to display block devices and filesystems. We can see that the ESP is mounted at "/efi" currently. tree -L 2 /efi is used to display data, with directory depth of 2.

umount /efi or umount /dev/sda1 is used to unmount the ESP. We see that the partition is no longer mounted. We can no longer access data stored on the ESP.

mkdir and touch are used to create some random directories and files in /efi. Because the ESP (/dev/sda3) is no longer mounted, they will be created on the root partition (/dev/sda4), i.e. we can no longer read or write from the ESP partition.

mount now mounts the ESP (/dev/sda3) back at /efi. We use tree to inspect the contents of the directory "/efi". We see that the ESP's files are now back at "/efi"! When we access the directory "/efi", or any of the files under "/efi", the system realizes that the ESP (/dev/sda3) is mounted at "/efi", and redirects us to read and write from the ESP. The root of the ESP filesystem is attached to the directory "/efi".

Using umount /efi, we unmount the ESP again!!! Lo and behold, using tree, we find that the random directories and files we created earlier are still there! In essence, they were masked when we mounted the ESP at "/efi" over them. They could not be accessed as any reference to files/directories in "/efi" would instead access from the ESP.

Now let's clean up the random directories and files we created earlier, and remount the ESP so everything is back to normal :)

A Parting Illustration...

Before mounting the ESP

After mounting the ESP

Conclusion

Mounting really is just attaching a disk partition to a particular directory in the filesystem hierarchy.

You can mount your Windows or other Linux partitions to read and write files from them. You can mask files under a directory by mounting a partition at the same directory. You can boot into a live USB and mount your Linux OS for boot repair, and downgrade kernels or other packages.


Additional Info

Filesystems?

  • organizes data (files, directories, etc.) stored on a device partition
  • allows data to be interpreted in a logical manner, rather than just looking like random 0s and 1s
  • the different filesystem types are different ways of organizing your data

Chroot? (change root)

  • changes the apparent root directory, or the directory that the system thinks is the root directory
  • mount /dev/sdXn /mnt followed by chroot /mnt are often used to mount, then chroot into a different partition, /dev/sdXn (e.g. /dev/sdb3)
  • once you enter the chroot of /mnt, you can visualize it as if you are standing at the root of the newly mounted partition
  • modifies pathname lookups for absolute pathnames (e.g. "/" => "/mnt", and "/efi/EFI/ubuntu" => "/mnt/efi/EFI/ubuntu")
  • Let's pretend you mount and chroot into a separate partition containing a Linux OS and filesystem hierarchy. When you execute commands, it will execute binaries (/bin) and use libraries (/lib) located on the newly chrooted partition, i.e. (/bin/ls is really referencing /mnt/bin/ls).

References

  1. ArchWiki - Filesystem (Mounting)
  2. Filesystem Hierarchy Standard - Root filesystem
  3. ArchWiki - EFI System Partition (ESP)
  4. ArchWiki - chroot
  5. LWN - "What chroot() is really for" by Jake Edge

Final Notes

These guides are targeted mostly at newcomers to Linux. The extensive use of illustrations is something I find most other guides lack. I would appreciate any feedback, and corrections if I have made any mistakes. Apologies in advance if I have!

Email:
sky100aw@gmail.com