Mounting
Chapter One Hundred Two
Syllabus topic Module 2, "File System Interface - File-System Mounting"
Pages 412 to 415 of 452
In one line
Mounting attaches the root of a file system on a device to a chosen directory in the tree already there, so that one tree spans many devices.
What a mount is
A file system must be mounted before any file in it can be used, and mounting means naming the place in the existing tree where its root is to appear. That place is the mount point.
| The operating system is given | Example |
|---|---|
| the device holding the file system | a partition such as the vda1 of Chapter ninety three |
| the mount point: a directory in the tree already mounted | /home, or /mnt/backup |
What it does before it agrees
| Step | What happens |
|---|---|
| 1 | ask the device driver to read the device's directory structure, the superblock of Chapter one hundred five |
| 2 | check that it is a file system of a format the system understands, and that it is consistent |
| 3 | note in the kernel's own mount table that a file system is mounted at that directory |
| 4 | from then on, any path that reaches that directory continues into the mounted file system |
Step 2 is the reason a mount can fail. A device holding no file system, or one left inconsistent by a crash, is refused. That refusal is a feature: it stops the system reading rubbish as though it were directories.
What happens to what was already there
The question every examiner asks, and there are two answers because systems differ.
| Policy | What happens |
|---|---|
| hide the old contents | the directory's own files become inaccessible for as long as the file system is mounted, and reappear when it is unmounted. Nothing is lost |
| refuse a non empty mount point | the mount fails unless the directory is empty, which prevents the surprise |
The first is what UNIX does. A file at /mnt/notes.txt is invisible while a file system is mounted at /mnt, and nothing has happened to it: the path now resolves into the other file system.
The same reasoning covers unmounting. A file system that is busy, because some process has a file open in it or has its current directory inside it, cannot be unmounted: the system refuses rather than pull the tree out from under a running program.
Who mounts, and when
| Policy | How it works |
|---|---|
| at boot, automatically | a table on disk lists devices and their mount points, and the system mounts each one as it starts |
| on demand | the system mounts a file system the first time a path into it is used, which is how a network file system is often arranged |
| by hand | a command, usually restricted to the administrator, because mounting affects every user |
Mounting
Mounting is a privileged operation for a simple reason: whoever mounts a file system chooses the owners and permissions recorded in it, so an ordinary user who could mount could bring a file owned by anybody into the tree.
One tree or many
The design difference worth knowing, because it changes what a path looks like.
| System | How several devices appear |
|---|---|
| UNIX and Linux | one tree. Every file system is mounted somewhere inside it, and a path never says which device it is on |
| Windows | one namespace per volume, named by a drive letter: C: and D: are separate trees, and a path begins with the device |
| macOS | one tree, with removable volumes mounted automatically under a standard directory |
The UNIX arrangement means a program never knows or cares which device its file is on, and an administrator can move a directory to a new disk by mounting it in the same place. The Windows arrangement makes the device visible in every path, which is simpler to explain and harder to change.
The lab machine's own tree
Four different file systems, four different types, in one tree, and one of them is not a disk at all.
$ stat -f -c '%n is a file system of type %T' / /proc /sys /dev/shm
/ is a file system of type overlayfs
/proc is a file system of type proc
/sys is a file system of type sysfs
/dev/shm is a file system of type tmpfs
$ awk '$2 == "/proc" || $2 == "/sys" {print $2, "is mounted, and holds a", $3, "file system"}' /proc/mounts
/proc is mounted, and holds a proc file system
/sys is mounted, and holds a sysfs file system
$ test "$(stat -c %d /)" != "$(stat -c %d /proc)" && echo "the root and /proc are on different devices, in one tree"
the root and /proc are on different devices, in one treeRead the last line first. /proc is on a different device from /, and a path walks from one to the other without saying so. That is mounting doing its work: /proc/self/status, which Chapter eighty read, is a path through two file systems.
| The mounted file system | What is really behind it |
|---|---|
overlayfs at / | two directories on the host, layered so that writes go to one of them |
proc at /proc | no storage at all: the kernel makes up the files as they are read |
sysfs at /sys | the kernel's own objects, which is how Chapter ninety three read the disk's block count |
tmpfs at /dev/shm | memory, not disk: files in it disappear when the machine stops |
Three of the four store nothing on a disk. Mounting is not a disk operation; it is a namespace operation, and a file system is anything that can answer the file system interface of Chapter one hundred four. That is worth one sentence in an answer about mounting.
Mounting
Distinctions that carry marks
| Mounting | Formatting | |
|---|---|---|
| What it does | attaches an existing file system to the tree | creates a file system on a device |
| How often | every boot | once |
| Destroys data | no | yes |
| Mount point | Root of the mounted file system | |
|---|---|---|
| Belongs to | the existing tree | the new file system |
| After mounting | its own contents are hidden | it is what the path now reaches |
| UNIX | Windows | |
|---|---|---|
| Several devices | one tree | one namespace per volume |
| A path says which device | no | yes, the drive letter |
| Moving a directory to another disk | mount it in the same place | the paths change |
What it does not mean
Mounting does not copy anything. It records where a file system's root is to appear.
A hidden mount point directory is not deleted. Its contents come back when the file system is unmounted.
A file system is not always a disk. On the lab machine three of four are not.
Unmounting is not always possible. A busy file system is refused.
Mounting is not something an ordinary user does. It is privileged, because the mounted file system carries its own owners and permissions.
Quick revision
- Mounting attaches a file system on a device to a mount point in the tree already there;
nothing in it can be used before that.
- Before agreeing, the system reads the superblock, checks the format and the
consistency, and records the mount in its mount table.
- Files already in the mount point directory are hidden while the mount lasts, or the mount
is refused if the directory is not empty. Nothing is lost either way.
- A busy file system, one with an open file or a process's current directory inside it,
cannot be unmounted.
- Mounting happens at boot from a table, on demand, or by hand, and it is
privileged because the mounted file system carries its own owners and permissions.
- UNIX has one tree; Windows has a namespace per volume, named by a drive letter.
- Measured on the lab machine:
/is overlayfs,/procis proc,/sysis sysfs,
/dev/shm is tmpfs, and / and /proc are on different devices in one tree. Three of the four store nothing on a disk.
Test yourself
- What does mounting do? It attaches the root of a file system on a device to a directory,
the mount point, in the tree that is already mounted, so that paths reaching that directory continue into the new file system.
Mounting
- What does the operating system check before it mounts? That the device holds a file system
of a format it understands and that it is consistent, by reading its superblock through the device driver.
- What happens to files already in the mount point directory? They become inaccessible while
the file system is mounted and reappear when it is unmounted; some systems instead refuse to mount onto a non empty directory.
- Why can a file system be impossible to unmount? Because it is busy: a process has a file
open in it or has its current directory inside it.
- Give three policies for when a file system is mounted. Automatically at boot from a table
of devices and mount points; on demand when a path into it is first used; or by hand with a privileged command.
- Why is mounting privileged? Because a mounted file system carries its own record of owners
and permissions, so anyone who could mount could introduce files owned by anybody.
- How do UNIX and Windows differ in handling several devices? UNIX mounts every file system
into one tree, so a path never names a device; Windows gives each volume its own namespace under a drive letter, so every path begins with the device. 8. The lab machine has four file systems of four types and three store nothing on disk. What does that show about mounting? That it is an operation on the namespace, not on a disk: anything that can answer the file system interface can be mounted, including the kernel's own made up files and a file system in memory.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.