Skip to content

104.3 Control mounting and unmounting of filesystems

Weight: 3

Candidates should be able to configure the mounting of a filesystem.

Objectives

  • Manually mount and unmount filesystems.
  • Configure filesystem mounting on bootup.
  • Configure user mountable removable filesystems.
  • Use of labels and UUIDs for identifying and mounting file systems.
  • Awareness of systemd mount units.

Terms

/etc/fstab, /media/, mount, umount, blkid, lsblk

Mounting and Unmounting

When we have a formatted partition and need to use it, we have to mount it somewhere in the Linux directory hierarchy. Unlike Windows, the new drive does not show up as a separate disk, but like a virtual subdirectory somewhere in your / tree.

   Windows                  Linux
   C:\                      /
   D:\  (second disk)       /media/mydisk   <- the second disk
                                               appears HERE, inside
                                               the one tree

Say we want to mount /dev/sda3 on /media/mydisk. The directory /media/mydisk should already exist, and then we just run:

sudo mount -t ext4 /dev/sda3 /media/mydisk

All files and folders in /dev/sda3 will be accessible from /media/mydisk.

Run mount with no parameter to see all mounted devices. To unmount, simply use umount on the drive or the directory. These two are equivalent:

sudo umount /dev/sda3
sudo umount /media/mydisk

Mounting and unmounting can happen on many different storage types, for example NFS storage, ISO files (with -o loop) and tmpfs.

Swap disks do not need mounting. You should use swapon and swapoff to use them.

mount -t ext4 /dev/sda1 /media

The -t switch indicates the type of the filesystem.

mount -o remount,ro /dev/sda1

The -o switch passes some options (say ro for readonly)

It is usually used as a one-line command:

mount -t ext4 -o remount,ro /dev/sda1 /media

The /media and /mnt directories are used to mount filesystems, even though you can use any directory for this purpose.

The general form is mount -t TYPE DEVICE MOUNTPOINT. An important detail about the mount point: it does not have to be empty, but it must exist. If it already holds files, those files become invisible while something is mounted over them. They are not deleted, just hidden until you unmount.

A real example, mounting a USB stick:

# mount -t exfat /dev/sdb1 ~/flash/
$ ls -lh ~/flash/
total 469M
-rwxrwxrwx 1 root root 454M jul 19 09:49 lineage-16.0-20190711-MOD-quark.zip
-rwxrwxrwx 1 root root 16M jul 19 09:44 twrp-3.2.3-mod_4-quark.img

Running mount alone gives a long list, since it includes many in-memory filesystems. -t filters it, and several types can be given at once separated by commas:

# mount -t ext4
/dev/sda1 on / type ext4 (rw,noatime,errors=remount-ro)

# mount -t ext4,fuseblk
/dev/sda1 on / type ext4 (rw,noatime,errors=remount-ro)
/dev/sdb1 on /home/carol/flash type fuseblk (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096) [DT_8GB]

Each line reads as SOURCE on TARGET type TYPE OPTIONS.

The other main mount options:

  • -a mount everything listed in /etc/fstab
  • -o or --options pass a comma separated list of mount options
  • -r or -ro mount read-only
  • -w or -rw mount writable

And for umount:

  • -a unmount everything listed in /etc/fstab
  • -f force the unmount, useful when a remote filesystem has become unreachable
  • -r if it cannot unmount, try to make it read-only instead

Here is the error you will actually hit in practice. A filesystem cannot be unmounted while a file on it is open:

# umount /dev/sdb1
umount: /media/carol/External_Drive: target is busy.
# lsof /dev/sdb1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
evince 3135 carol 16r REG 8,17 21881768 5195 /media/carol/External_Drive/Documents/E-Books/MagPi40.pdf

lsof means "list open files". Reading that output: the program evince, a PDF viewer, with PID 3135, has MagPi40.pdf open. Close that program and the unmount will work.

   umount fails: "target is busy"
        |
   lsof /dev/sdb1
        |
        +--> tells you WHICH program and WHICH file
        |
   close it, or kill the PID, then umount again

On where to mount, the convention: /mnt was traditionally for external devices, with fixed spots like /mnt/cdrom and /mnt/floppy. That has been replaced by /media for removable media, which modern desktops mount automatically at /media/USER/LABEL. So a stick labelled FlashDrive plugged in by user john appears at /media/john/FlashDrive/. When mounting something by hand, /mnt is the polite place to put it.

Real world use for -o loop: mounting an ISO file to look inside it without burning it to a disc. mount -o loop ubuntu.iso /mnt makes the image's contents readable as ordinary files.

UUID & Labels

As you already know, there is a problem when working with classical device names like /dev/vdb1: they change. The current /dev/sdb might be seen as /dev/sdd after you remove and reconnect it. To solve this, it is better to work with UUIDs (Universal Unique Identifiers). Check them with lsblk (-O will show all available columns, or specify with -o as below) and blkid.

# lsblk -o +UUID
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS                             UUID
sr0     11:0    1  1.8G  0 rom  /run/media/nagato/Fedora-WS-Live-35_B-1-2 2021-09-22-21-47-34-00
zram0  251:0    0  1.9G  0 disk [SWAP]  
vda    252:0    0   20G  0 disk  
├─vda1 252:1    0  600M  0 part /boot/efi                               E13A-EF36
├─vda2 252:2    0    1G  0 part /boot                                   19ed96a1-3b36-4202-81bb-349f7adfb8b1
└─vda3 252:3    0 18.4G  0 part /home                                   076766a5-8864-4e35-a632-464b03396f7a
                                /  
vdb    252:16   0    2G  0 disk  
└─vdb1 252:17   0    2G  0 part /tmp/lkj                                4c1a51e6-47bf-4a34-84a2-87027c91e14a

# blkid
/dev/vdb1: UUID="4c1a51e6-47bf-4a34-84a2-87027c91e14a" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5415e516-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-09-22-21-47-34-00" LABEL="Fedora-WS-Live-35_B-1-2" TYPE="iso9660"
/dev/zram0: LABEL="zram0" UUID="e459f522-1675-40d2-b318-51d9bd16d7bb" TYPE="swap"
/dev/vda2: UUID="19ed96a1-3b36-4202-81bb-349f7adfb8b1" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5f4ee154-3ade-4af6-8809-6d90d5827d39"
/dev/vda3: LABEL="fedora_localhost-live" UUID="076766a5-8864-4e35-a632-464b03396f7a" UUID_SUB="a4340a29-6d9b-4c28-a7c8-b4aab5d08893" BLOCK_SIZE="4096" TYPE="btrfs" PARTUUID="a46e64aa-65ef-4a62-9bf8-96fd19aee353"
/dev/vda1: UUID="E13A-EF36" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="a7a2b260-0302-45bc-a4db-42bd2e0ee7f2"

# blkid /dev/vdb1
/dev/vdb1: UUID="4c1a51e6-47bf-4a34-84a2-87027c91e14a" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="5415e516-01"

# mount UUID="4c1a51e6-47bf-4a34-84a2-87027c91e14a" /media/mydisk/

Several things in that lsblk output are worth reading carefully.

  • The tree drawing with ├─ and └─ shows partitions belonging to their disk. vda is a whole disk, vda1 to vda3 are its partitions.
  • vda3 shows two mount points, /home and /. That is possible because it is btrfs with subvolumes, from 104.1.
  • zram0 shows [SWAP] instead of a directory, since swap is never mounted anywhere.
  • RM is the removable flag. sr0, the optical drive, has 1.
  • vda1 has a short UUID, E13A-EF36, because it is vfat. vfat UUIDs are shorter than ext4 ones.

lsblk -f shows the filesystem columns without you having to ask for them:

$ lsblk -f /dev/sda1
NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sda1 ext4 6e2c12e3-472d-4bac-a257-c49ac07f3761 64,9G 33% /

And it distinguishes UUID from PARTUUID, both of which appear in the blkid output above:

   UUID      belongs to the FILESYSTEM
             created by mkfs, changes if you reformat

   PARTUUID  belongs to the PARTITION ENTRY
             created by fdisk/gdisk, survives a reformat

Both a UUID and a label can be used anywhere a device name can, in mount and in /etc/fstab:

$ mount -t ntfs UUID=56C11DCC5D2E1334 /mnt/external

fstab

For automatic mounting, Linux uses the /etc/fstab file. It is like a table which shows what file system should be mounted where during the boot. This is the /etc/fstab of my Fedora:

# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Oct 20 13:16:38 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=076766a5-8864-4e35-a632-464b03396f7a /                       btrfs   subvol=root,compress=zstd:1 0 0
UUID=19ed96a1-3b36-4202-81bb-349f7adfb8b1 /boot                   ext4    defaults        1 2
UUID=E13A-EF36          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
UUID=076766a5-8864-4e35-a632-464b03396f7a /home                   btrfs   subvol=home,compress=zstd:1 0 0

These are the columns:

  • file system: Label, UUID, device
  • mount point: swap or none for swap
  • type: can be ext4, xfs, nfs or other types of filesystem
  • options: defaults, rw / ro, noauto, user, exec / noexec, noatime, umask
  • dump: does the dump command back this up? mostly 0
  • pass: Non-zero values of pass specify the order of checking filesystems at boot time

note:

  • User-mounted filesystems default to noexec unless exec is specified after user.
  • noatime will disable recording of access times. Not using access times may improve performance.

The six columns lined up against a real line from that file:

UUID=19ed96a1-...  /boot   ext4   defaults   1   2
        |            |       |        |      |   |
        |            |       |        |      |   +-- PASS  check order at boot
        |            |       |        |      +------ DUMP  backup flag
        |            |       |        +------------- OPTIONS
        |            |       +---------------------- TYPE
        |            +------------------------------ MOUNTPOINT
        +------------------------------------------- FILESYSTEM

Look at the two btrfs lines in that file. Both use the same UUID but mount at / and /home with different subvol= options. That is one btrfs filesystem serving two mount points through subvolumes.

The PASS column is the same field fsck -A reads, from 104.2. Root normally gets 1, other filesystems get 2, and 0 means never check at boot.

The comment in the file is worth noticing too: after editing /etc/fstab, run systemctl daemon-reload, because systemd turns each line into a generated mount unit.

The full list of generic mount options:

  • atime and noatime whether to record when a file was last read. noatime speeds up disk I/O. This is not the same as modification time, which is always recorded.
  • auto and noauto whether mount -a picks it up
  • defaults a shorthand for rw, suid, dev, exec, auto, nouser and async
  • dev and nodev whether device files on it are honoured
  • exec and noexec whether programs on it may be run
  • user and nouser whether an ordinary user may mount it
  • group a user may mount it if they belong to the group owning the device
  • owner a user may mount it if they own the device
  • suid and nosuid whether SETUID and SETGID bits take effect
  • ro and rw read-only or writable
  • remount re-apply options to something already mounted. This is only used with mount -o, never in /etc/fstab
  • sync and async whether writes happen immediately or are buffered. async is the usual default, and sync on a flash drive can shorten its life

Real world use for noexec,nosuid,nodev: mounting a USB stick or a /tmp directory that untrusted people can write to. Together these three stop anyone running a program from it, stop SETUID tricks, and stop fake device files, which closes off a common way of gaining root.

Real world use for remount: fixing a system that booted read-only. mount -o remount,rw / makes the root filesystem writable again without a reboot. Note that when remounting you only give the device or the mount point, not the type.

Systemd mount units

When using systemd, a unit configuration file whose name ends in ".mount" encodes information about a file system mount point controlled and supervised by systemd.

A mount unit is a text file in /etc/systemd/system/ with this shape:

[Unit]
Description=

[Mount]
What=
Where=
Type=
Options=

[Install]
WantedBy=

The fields:

  • Description= a short description, like "Mounts the backup disk"
  • What= the volume, given as /dev/disk/by-uuid/VOL_UUID
  • Where= the full path of the mount point
  • Type= the filesystem type
  • Options= the same options used with mount or in /etc/fstab
  • WantedBy= usually multi-user.target, meaning it mounts on a normal boot

A complete example:

[Unit]
Description=External data disk

[Mount]
What=/dev/disk/by-uuid/56C11DCC5D2E1334
Where=/mnt/external
Type=ntfs
Options=defaults

[Install]
WantedBy=multi-user.target

The filename is not free. It has to match the mount point, with slashes turned into hyphens. Mounting at /mnt/external means the file must be called mnt-external.mount:

   /mnt/external   ->   mnt-external.mount
   /srv/backup     ->   srv-backup.mount
   /home           ->   home.mount

   the leading slash is dropped, the rest become hyphens

Then reload systemd and start it, exactly as in 101.3:

# systemctl daemon-reload
# systemctl start mnt-external.mount

Checking it:

# systemctl status mnt-external.mount
● mnt-external.mount - External data disk
  Loaded: loaded (/etc/systemd/system/mnt-external.mount; disabled; vendor pres
  Active: active (mounted) since Mon 2019-08-19 22:27:02 -03; 14s ago
  Where: /mnt/external
  What: /dev/sdb1
  Tasks: 0 (limit: 4915)
  Memory: 128.0K
  CGroup: /system.slice/mnt-external.mount
ago 19 22:27:02 pop-os systemd[1]: Mounting External data disk...
ago 19 22:27:02 pop-os systemd[1]: Mounted External data disk.

Note disabled on the Loaded line and active (mounted) on the Active line. start only applies to this session. systemctl enable mnt-external.mount makes it happen on every boot, which is the same start-versus-enable distinction from 101.3.

There is also an automount unit, which mounts the disk only when somebody actually looks in the directory:

[Unit]
Description=Automount for the external data disk

[Automount]
Where=/mnt/external

[Install]
WantedBy=multi-user.target

It is saved as mnt-external.automount, alongside the .mount file, and started the same way:

# systemctl daemon-reload
# systemctl start mnt-external.automount

Real world use: a network share that is slow or often unreachable. With a plain mount unit, boot waits for it. With an automount unit, boot is fast and the share is only contacted the first time someone opens the folder.

Summary

I have a Linux system where a disk is useless until it is attached to the single directory tree, and that attaching is called mounting. There are no drive letters. Instead I pick a directory, the mount point, and everything on the disk appears underneath it. The command is mount -t TYPE DEVICE MOUNTPOINT, and umount reverses it, taking either the device or the mount point. The mount point must exist, and anything already inside it becomes hidden while something is mounted over it. Swap is the exception to all of this, since it is never mounted, only switched on with swapon.

When umount refuses with "target is busy", it means a file on that filesystem is still open. lsof followed by the device tells me which program and which file, so I can close it and try again. For a filesystem I cannot reach at all, umount -f forces it. By convention removable media lands under /media, usually at /media/USER/LABEL, while /mnt is where I mount something by hand.

Device names like /dev/sdb1 move around when disks are added or ports change, so I identify filesystems by UUID or label instead. blkid and lsblk -f show both, and either can be used in place of a device name in mount and in /etc/fstab. The UUID belongs to the filesystem and changes if I reformat, while the PARTUUID belongs to the partition entry and survives a reformat.

Automatic mounting at boot is driven by /etc/fstab, six columns per line: the filesystem, the mount point, the type, the options, the dump flag and the pass number that decides check order at boot. The options I use most are defaults, ro, noatime to skip recording read times, noauto to keep something out of mount -a, user to let an ordinary user mount it, and the trio noexec,nosuid,nodev for anything untrusted. remount is the odd one out, used only with mount -o and never in the file, and mount -o remount,rw / is how I recover a system that came up read-only. On systemd machines the same job can also be done with a .mount unit in /etc/systemd/system/, whose filename has to match the mount point with the slashes turned into hyphens, plus an optional .automount unit that delays the mount until someone actually opens the directory.