Skip to content

Mounting file systems

Practical 4.4 mounting-file-systems-annex.pdf · PDF

You plug a USB stick into your laptop and, a second later, a window opens with your files. Who did that? Something had to take the stick's file system and attach it somewhere you could reach it.

On deb-lab that something is missing. Your second disk has three partitions, each with a file system and a label — and no command of yours can reach them. cd /dev/vdb1 answers Not a directory. Today you open the doors, by hand first, and then for good, so they are still open after a reboot.

What you will learn

  • Explain mounting: attaching a file system to a directory of the one Linux tree
  • Mount and unmount by hand with mount and umount, and check with findmnt
  • Say why /etc/fstab identifies a file system by its UUID and not by /dev/vdb1
  • Write lines in /etc/fstab, test them with findmnt --verify and mount -a, and prove they survive a reboot
  • Tell apart df -h (how full is each file system) and du -sh (how big is this folder), and find what fills a disk
  • Why mounting copies nothing — and where files go when they "vanish" under a mount point

Three rooms with no door

lsblk -f

Three partitions, three file systems, three labels — and an empty MOUNTPOINTS column. The disk is connected. The file systems exist. Nobody can go in.

In the pantry of File systems, each room got shelves and an inventory. They still have no door onto the kitchen. Mounting cuts that door.

Mounting is attaching a file system to an ordinary, empty directory of the tree, called the mount point. From then on, behind that directory you see the shelves of that file system.

  • The room = the file system on /dev/vdb1.
  • The door = the mount point, for example /srv/tramuntana/dev.

Remember from the directory tree: Linux has one tree. There is no D:. Every file system hangs from a folder of that tree.

flowchart TB
    subgraph TREE ["The one tree of deb-lab (system disk vda)"]
        ROOT["<b>/</b>"] --> SRV["<b>/srv/tramuntana</b>"]
        ROOT --> MNT["<b>/mnt</b>"]
        SRV --> DEVD["<b>dev</b><br>mount point"]
        SRV --> DESD["<b>design</b><br>mount point"]
        MNT --> EXD["<b>exchange</b><br>mount point"]
    end
    subgraph DISK ["Second disk vdb"]
        V1["<b>vdb1</b> · ext4 · dev"]
        V2["<b>vdb2</b> · xfs · design"]
        V3["<b>vdb3</b> · exfat · EXCHANGE"]
    end
    V1 -->|"mounted on"| DEVD
    V2 -->|"mounted on"| DESD
    V3 -->|"mounted on"| EXD
Windows Linux
A new volume appears as A drive letter: D:, X: A directory you choose: /srv/tramuntana/dev
Who attaches it Windows, when it sees it You (mount), or the list in /etc/fstab

Mount and unmount by hand

Two commands, and nothing else:

sudo mount DEVICE DIRECTORY
sudo umount DIRECTORY

Note the spelling: umount, without the n.

Mount dev

Make the mount point, and leave a note inside it before mounting:

sudo mkdir -p /srv/tramuntana/dev
sudo touch /srv/tramuntana/dev/i-was-here.txt
ls /srv/tramuntana/dev

mkdir -p also creates the parent directories that are missing. Now mount, and look again:

student@deb-lab:~$ sudo mount /dev/vdb1 /srv/tramuntana/dev
student@deb-lab:~$ ls -a /srv/tramuntana/dev
.  ..  lost+found
student@deb-lab:~$ findmnt /srv/tramuntana/dev
TARGET              SOURCE    FSTYPE OPTIONS
/srv/tramuntana/dev /dev/vdb1 ext4   rw,relatime

Your note is gone from view, and there is a lost+found folder (mkfs.ext4 makes it: the disk checker puts there the pieces it recovers after a crash). findmnt shows what is mounted where, and df -h /srv/tramuntana/dev says the size: 2.0G, almost nothing used.

Unmount it

student@deb-lab:~$ cd /srv/tramuntana/dev
student@deb-lab:/srv/tramuntana/dev$ sudo umount /srv/tramuntana/dev
umount: /srv/tramuntana/dev: target is busy.
student@deb-lab:/srv/tramuntana/dev$ cd ~
student@deb-lab:~$ sudo umount /srv/tramuntana/dev
student@deb-lab:~$ ls /srv/tramuntana/dev
i-was-here.txt

The first umount failed because you were inside: you cannot remove a door while somebody stands in it. From your home it works in silence. And the note is back.

Careful: mounting copies nothing

The note was never deleted. It lives in the directory on the system disk, hidden behind the door. Mount, and you see the other file system; unmount, and the folder shows its old content again. The trap: files saved in /srv/tramuntana/dev while nothing is mounted land on the system disk — and vanish from view the day the room is mounted.

The exchange stick

EXCHANGE is the room every system can read. Use it like a USB stick: mount, copy, unmount.

sudo mkdir /mnt/exchange
sudo mount /dev/vdb3 /mnt/exchange
df -hT /mnt/exchange
sudo cp ~/tramuntana-share/admin/staff.csv /mnt/exchange/
ls -l /mnt/exchange
sudo umount /mnt/exchange
ls /mnt/exchange

df -hT shows type exfat, about 1021M. The last ls shows nothing: the stick is "safely removed". That is exactly what Safely remove does on a desktop: unmount before you pull the stick out, so every pending write reaches it.

Why staff.csv is owned by root on exFAT

exFAT keeps no owners and no permissions at all — it was made for sticks that travel between systems. Linux shows every file on it as owned by one user — here root, because root mounted it. Permissions are Unit 5.

Message Cause
mount point does not exist You skipped the mkdir
wrong fs type, bad option, bad superblock You typed the disk (vdb) instead of the partition (vdb1), or it has no file system yet
target is busy Some terminal is still inside the mount point: cd ~
unmount: command not found It is umount

Make it permanent: /etc/fstab

Restart deb-lab and your mounts are gone: mount lasts until the machine is switched off. The system needs a written list of what to mount at every start.

/etc/fstab (file systems table) is the text file that lists which file systems to mount at boot, where, and how.

Why a UUID, not /dev/vdb1

The names vda, vdb, vdc are handed out at every boot, in the order the kernel finds the disks. Add or remove a disk — in LVM and RAID, later in this unit, you add four — and vdb can become another disk.

The UUID you saw in File systems is written inside the file system by mkfs. It travels with it. In the pantry: /dev/vdb1 is "the second room on the left" — rebuild the corridor and it is another room. The UUID is the number engraved on its inventory.

The six fields

cat /etc/fstab shows the lines the installer wrote. Here is one for dev:

UUID=4b481e94-b109-4014-a26b-be4bdb6cd5ca  /srv/tramuntana/dev  ext4  defaults,nofail  0  2
# Example Meaning
1 UUID=4b48... What to mount
2 /srv/tramuntana/dev Where: the mount point. It must exist.
3 ext4 Which file system it holds (xfs for design)
4 defaults,nofail Options. nofail: if this disk is missing, keep booting.
5 0 Used by the old dump backup program. Always 0 for us.
6 2 Order of the check at boot: 1 for /, 2 for the others, 0 for never

Edit it safely

Get the UUIDs with blkid, and copy the UUID, not the PARTUUID (that one belongs to the partition table):

sudo blkid /dev/vdb1 /dev/vdb2

Then make the second mount point, keep a copy of the file, and open it in nano, a simple text editor that runs inside the terminal:

sudo mkdir /srv/tramuntana/design
sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab

In nano: arrows to move, Ctrl+Shift+V to paste, Ctrl+O then Enter to save, Ctrl+X to leave. Do not copy 36 characters by hand: select the UUID with the mouse and use Ctrl+Shift+C. Add two lines at the end, one for dev (ext4) and one for design (xfs), each with its own UUID.

One wrong line can stop the boot

A wrong line for a disk the system requires stops the boot. Three safety nets, always:

  1. Copy the file first (/etc/fstab.bak).
  2. Write nofail on every extra disk.
  3. Run sudo findmnt --verify before any reboot.

Test before you reboot

sudo findmnt --verify
sudo systemctl daemon-reload
sudo findmnt --verify
sudo mount -a
findmnt /srv/tramuntana/dev
findmnt /srv/tramuntana/design
  • The first findmnt --verify warns that your fstab has been modified, but systemd still uses the old version. systemctl daemon-reload makes systemd re-read it.
  • The second one should end with Success, no errors or warnings detected.
  • mount -a mounts everything in fstab that is not mounted yet. It prints nothing when all is well.

The two checks catch different mistakes:

Mistake in the line findmnt --verify mount -a
One wrong character in the UUID [E] unreachable on boot required source Says nothing (because of nofail) and mounts nothing
ext5 instead of ext4 [W] ext5 seems unsupported by the current kernel unknown filesystem type 'ext5'
A mount point that does not exist [E] unreachable on boot required target mount point does not exist.

So: both checks, then findmnt to see the mount with your own eyes. And then the real test, sudo reboot: after logging in, both findmnt must show the mounts without you typing mount. A mount that was not tested after a reboot is not finished.

df asks the disk, du asks the folder

The designers say the disk is full. Two questions: how full? and who did it? Two different commands.

df -hT
du -sh /srv/tramuntana/design
df -h (disk free) du -sh FOLDER (disk usage)
Asks Each mounted file system One folder: adds up the files inside
Answers Size, Used, Avail, Use%, Mounted on One number
Use it for "Is the disk full?" "Who filled it?"

-h means human: 2.0G instead of a long number of blocks. -T adds the type, -s gives one total. Skip the lines that start with tmpfs and udev: they live in RAM, not on a disk. In the pantry: df reads the sign on the door (how many shelves are free); du goes in and weighs one box.

Find the hog

The system disk fills up one day. Where do you look? Measure one level of a folder and sort by size:

sudo du -h --max-depth=1 /usr | sort -h

sort -h understands human sizes like 12M and 1.3G, and puts the biggest at the bottom. Go down into the biggest folder and repeat until you find the culprit.

In Practical 4.4 you fill the rooms with the work of the dev and design teams and a fake 300 MB video, made with head -c 300M /dev/urandom > campaign.mp4 (300 MiB of random bytes), and then catch it with df and du.

Key takeaways

  • Mounting attaches a file system to a directory of the one tree, the mount point. Nothing is copied: what was in that directory is only hidden.
  • sudo mount DEVICE DIRECTORY and sudo umount DIRECTORY (no n). findmnt shows what is mounted where. You cannot unmount from inside.
  • /etc/fstab makes mounts permanent: six fields, UUID first, because device names like vdb can change at boot.
  • Always: back up the file, add nofail to extra disks, run findmnt --verify and mount -a, then reboot to prove it.
  • df -h tells how full each file system is; du -sh tells how big one folder is. du ... | sort -h finds the hog.

Quick check

1. What does mounting a file system do?
  • Copies its files into a folder of the system disk
  • Attaches it to a directory of the tree, so you can reach it
  • Creates a new, empty file system on the partition
  • Gives the disk a drive letter like D:

Mounting cuts a door: the mount point becomes the entrance to the other file system. Nothing is copied, and Linux uses directories, not letters. Creating a file system is mkfs.

2. Why does /etc/fstab use UUID=... instead of /dev/vdb1?
  • It is shorter to type
  • It makes the disk faster
  • It stays the same when disks are added or removed
  • It encrypts the disk

Device names are handed out at every boot in the order disks are found, so vdb can become another disk. The UUID is written inside the file system by mkfs and travels with it.

3. What does the option nofail do in an /etc/fstab line?
  • If the disk is missing, the system still boots
  • The disk can never fail
  • Errors on the disk are repaired at boot
  • The disk is mounted read-only

Without nofail, a missing disk listed in fstab can stop the boot. With it, the system skips that line and keeps going — which also means mount -a stays silent when the disk is not found, so check with findmnt.

4. Which command tells you how big one folder is?
  • df -h
  • lsblk -f
  • findmnt
  • du -sh

du (disk usage) adds up the files inside one folder. df answers per file system: how full each mounted one is.

5. You type sudo umount /srv/tramuntana/dev and get target is busy. What is the most likely reason?
  • Your terminal is inside that directory
  • The partition has no file system
  • The mount point does not exist
  • The command should be spelled unmount

You cannot remove the door while you stand in it. cd ~ and try again. A misspelled unmount would give command not found instead.

6. Joan saved photos in /srv/tramuntana/design while the partition was not mounted. Then it is mounted there. Where are the photos?
  • Deleted for ever
  • On the system disk, hidden under the mount point
  • Copied into the new partition
  • Inside the partition's lost+found folder

Before mounting, /srv/tramuntana/design was an ordinary directory of the system disk, so the photos went there. Mounting only covers them. Unmount, and they are back.

7. You just added a line to /etc/fstab. Select the two steps that belong before any reboot.
  • Run sudo findmnt --verify and read its last line
  • Run sudo mount -a and check the result with findmnt
  • Run mkfs again on the partition to refresh its UUID
  • Delete /etc/fstab.bak so the system reads the new file

findmnt --verify catches wrong UUIDs, types and mount points; mount -a mounts what the file lists. Running mkfs again would create a new UUID and break the line you just wrote, and the backup is your way back.

You partitioned, formatted and mounted a disk on Linux. Seen from Windows, the same ideas have other names. Next: Windows Disk Management.