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 mount point An existing directory onto which another file system is grafted with mount(), so its root appears at that path and its contents become reachable in the unified tree. Mounting is how many separate file systems (ext3, proc, tmpfs, AFSβ¦) are glued into one directory hierarchy; mkfs first writes an empty file system onto a device.
defined in ch. 39 β open in glossary
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:
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.
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:
| backing store | mounted at | type | |
|---|---|---|---|
| root disk | /dev/sda1 | / | ext3 (on-disk file system) |
| proc | proc | /proc | proc |
| sysfs | sysfs | /sys | sysfs |
| tmp disk | /dev/sda5 | /tmp | ext3 |
| shared memory | tmpfs | /dev/shm | tmpfs |
| AFS | AFS | /afs | afs (distributed file system) |
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:
| concept | what it is | |
|---|---|---|
| file / directory | file / directory | a byte array with a low-level name (inode number); a directory maps names β inode numbers, nesting into one tree |
| open + descriptor | open() β fd | ask the OS for access; get a per-process file descriptor (a capability) for later read/write |
| open file table + offset | open file table | each entry tracks the inode, R/W flags, a refcount, and the current offset (where I/O lands) |
| lseek / fsync | lseek() / fsync() | lseek repositions the offset (random access, no disk I/O); fsync forces buffered data durably to disk |
| links | hard / soft links | multiple 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 controls | permissions / ACLs | permission bits (owner/group/other rwx) and richer access control lists decide who may access a file |
| mount | mkfs / mount | make an empty file system on a device, then graft it onto a mount point to build one unified tree |
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?