CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 2 of 7 · 2 min read

Mounting

Think of a building in which new rooms are attached to the corridor by doors. Each room has its own plan and furniture, and from the corridor you only see the door. A room put in front of an old cupboard hides the cupboard until the room is removed. The analogy stops in one place: a door joins rooms of the same kind, and a mount can join file systems of quite different kinds.

In plain words: Unix does not give each disk a drive letter. It has one directory tree, which starts at the root. Mounting attaches the root of another file system to an existing, usually empty directory, called the mount point. After that, a path that goes through the mount point enters the other file system. The old contents of the mount point directory are hidden while the mount lasts, and they come back after the unmount. The kernel keeps a mount table of what is attached where. Each file system has its own set of inode numbers, so a file is identified by the pair of a device number and an inode number.

A worked example. On the test machine an ext4 image was mounted on /tmp/l8/mnt and a tmpfs on /tmp/l8/mnt/t. Before the second mount the directory t held a file hidden.txt. The results were:

(before)       ls mnt/t        -> hidden.txt
(after mount)  ls mnt/t | wc   -> 0 files
(after mount)  ls mnt/t/hidden.txt -> No such file or directory
(after umount) ls mnt/t        -> hidden.txt

The file never moved or changed. It is on the ext4 image and was simply not reachable, as the path now led into the tmpfs.

The key lines. The mount command takes a source (a device, an image or a name), a target directory, a type and some options. The call behind it is mount(2), and umount fails with "target is busy" if a process still uses a file or the working directory inside the mount.

Mount

Mounting is the operation that attaches a file system to a directory of the existing tree, called the mount point. Paths that pass through the mount point then lead into the attached file system, and whatever the mount point directory held before is hidden until the file system is unmounted.

Example: mounting a tmpfs on /tmp/l8/mnt/t hides the file hidden.txt that was in that directory.

Check yourself: 1. What happens to the files that were in a directory when another file system is mounted on it? 2. Why is a file identified by a device number and an inode number together? 3. Why can umount fail with "target is busy"?