Β§39.17–39.18Making and Mounting a File System β€” and a Recap

Part III OSTEP pp. 488–490 Β· ~6 min read

  • mount point

We’ve toured files, directories, and links. One task remains: assembling a full directory tree out of many separate file systems. That takes two steps β€” making file systems, then mounting them.

39.17 Making And Mounting A File System

To create a file system, you use a tool usually called mkfs (β€œmake fs”). Give it a device (a disk partition like /dev/sda1) and a file-system type (e.g. ext3), and it writes an empty file system β€” starting with a root directory β€” onto that partition.

But a fresh file system sitting on a partition isn’t reachable from the tree yet. The mount program (via the mount() system call) fixes that: it takes an existing directory as a mount point and pastes the new file system’s root onto the tree there. Step through an example β€” mounting an ext3 file system (with directories a and b, each holding a foo) at /home/users:

mkfs writes an empty file system onto a device; mount() grafts it onto an existing directory (the mount point), so it becomes part of one tree.
/homeusersempty mount point/dev/sda1 β€” ext3 (not yet mounted)rootabfoofoomount -t ext3 /dev/sda1 /home/users

1Before: two separate things

The running system has a tree with an empty directory /home/users. Separately, device /dev/sda1 holds a freshly-made ext3 file system (root with directories a and b, each containing a file foo) β€” but it is not reachable from the tree yet.

step 1 / 2

The payoff is a single, uniform namespace. On a real machine, running mount reveals a whole mix of file systems β€” on-disk, in-memory, pseudo, distributed β€” all glued onto one tree:

Running `mount` shows many independent file systems glued onto one directory tree.
backing storemounted attype
root disk/dev/sda1/ext3 (on-disk file system)
procproc/procproc
sysfssysfs/syssysfs
tmp disk/dev/sda5/tmpext3
shared memorytmpfs/dev/shmtmpfs
AFSAFS/afsafs (distributed file system)
Dotted-underlined cells have explanations β€” click one.

That uniformity is the quiet triumph of the file-system interface: whether data lives on a local disk, in memory, or on a server across the world, you name and reach it the same way β€” through one directory tree.

39.18 Summary

The UNIX file-system interface looks rudimentary, but there’s real depth to master. Here is the whole chapter distilled β€” the mental model to carry into the implementation chapters that follow:

The UNIX file-system interface at a glance β€” the whole chapter in one table.
conceptwhat it is
file / directoryfile / directorya byte array with a low-level name (inode number); a directory maps names β†’ inode numbers, nesting into one tree
open + descriptoropen() β†’ fdask the OS for access; get a per-process file descriptor (a capability) for later read/write
open file table + offsetopen file tableeach entry tracks the inode, R/W flags, a refcount, and the current offset (where I/O lands)
lseek / fsynclseek() / fsync()lseek repositions the offset (random access, no disk I/O); fsync forces buffered data durably to disk
linkshard / soft linksmultiple names for one file: a hard link shares the inode (link count); a soft link stores a pathname (can dangle). unlink() removes one name, freeing the file at count 0
sharing controlspermissions / ACLspermission bits (owner/group/other rwx) and richer access control lists decide who may access a file
mountmkfs / mountmake an empty file system on a device, then graft it onto a mount point to build one unified tree
Dotted-underlined cells have explanations β€” click one.

Nothing cements this like using it: trace real programs with strace, write a tiny ls or stat, and watch the calls fly. Next, we stop using the file system and start building one β€” turning raw disk blocks into these files, directories, inodes, and links.

Check yourself: making and mounting file systems

1.What does mkfs do?

2.What does mount do?

3.After `mount -t ext3 /dev/sda1 /home/users`, where the mounted file system has directories a and b each holding foo, how is the foo inside a named?

4.What is the big benefit of mounting many file systems?

5.Which pairing from the chapter recap is correct?

5 questions