11.4. Removable and On-Demand Storage

Boot-time fstab entries serve filesystems that are always present. Removable media and rarely-needed volumes are different: they appear and disappear, and a mount strategy built on the assumption of presence either blocks boots or leaves dead mount points. Debian has two answers - the desktop's session-driven automounting, and systemd's path-triggered automount units - and the right choice depends on what kind of system is holding the disk. Figure 11.2 maps the decision.

Choosing a mount policy

Figure 11.2. Conceptual decision paths from an existing filesystem to a mount policy. The temporary mount of Section 11.3.2 tests; the fstab entries of Section 11.3.3 commit at boot; automount units and the desktop stack mount on demand. One filesystem gets exactly one policy.

11.4.1. Desktop Automounting and Server Mount Policy

A Debian desktop does not ask its user to run mount. The udisks2 daemon watches for block-device events over D-Bus and offers mounts to the desktop environment; the file manager calls it when a user clicks a device, and polkit - the authorization framework that decides who may do what - approves the request because a logged-in local user is an "active" one. The filesystem appears under /media/<user>/<label>, owned by that user, and the manager's eject icon unmounts it again. Chapter 4 walked this from the desktop side in Section 4.3.3, including the mount-and-unmount sequence for removable media; this section looks at the same machinery from below, with the udisks2 command-line client, which is also the tool of choice when no file manager is present.

On the example system the service is running:

# systemctl status udisks2 --no-pager
● udisks2.service - Disk Manager
     Loaded: loaded (/usr/lib/systemd/system/udisks2.service; enabled; preset: enabled)
     Active: active (running) since Fri 2026-11-20 02:29:34 UTC; 25s ago
 Invocation: 1648cd818fff4de9b5d6a65092af2a14
       Docs: man:udisks(8)
   Main PID: 557 (udisksd)
      Tasks: 6 (limit: 1107)
     Memory: 11.5M (peak: 11.8M)
        CPU: 89ms
     CGroup: /system.slice/udisks2.service
             └─557 /usr/libexec/udisks2/udisksd

Nov 20 02:29:33 storage systemd[1]: Starting udisks2.service - Disk Manager...
Nov 20 02:29:33 storage udisksd[557]: udisks daemon version 2.10.1 starting
Nov 20 02:29:34 storage systemd[1]: Started udisks2.service - Disk Manager.
Nov 20 02:29:34 storage udisksd[557]: Acquired the name org.freedesktop.UDisks2 on the system message bus

udisksctl is its client. Used with a block device, info prints what udisks knows about it - selected fields for /dev/vdb2, whose handbook-opt filesystem Section 11.3.1 created:

# udisksctl info --block-device /dev/vdb2
/org/freedesktop/UDisks2/block_devices/vdb2:
  org.freedesktop.UDisks2.Block:
    Device:                     /dev/vdb2
    HintSystem:                 true
    IdLabel:                    handbook-opt
    IdType:                     ext4
    IdUUID:                     bcd65ade-67cd-4438-9b2c-3f4319d69194
    IdUsage:                    filesystem
    Size:                       536870912
    Symlinks:                   /dev/disk/by-diskseq/2-part2
                                /dev/disk/by-partlabel/optional
                                /dev/disk/by-partuuid/31ea70f4-3964-4c43-bbb6-76aa7b5562ca
                                /dev/disk/by-uuid/bcd65ade-67cd-4438-9b2c-3f4319d69194
    UserspaceMountOptions:      
  org.freedesktop.UDisks2.Filesystem:
    MountPoints:        
    Size:               536870912
  org.freedesktop.UDisks2.Partition:
    Name:               optional
    Number:             2
    Table:              '/org/freedesktop/UDisks2/block_devices/vdb'
    Type:               0fc63daf-8483-4772-8e79-3d69d8477de4

Selected output; the full record also lists device numbers, the parent drive, udisks configuration and its remaining hints, the RAID and crypto backings (empty here), and every symlink udev maintains for the device.

udisks reads the same facts as the command-line tools - label, type, UUID, partition table and type GUID - through the same udev data, and reports which of its interfaces apply: this device has a Filesystem interface with no current MountPoints, and its partition type GUID is the 0fc63daf-… Linux-filesystem code from Section 11.2.2. One field is udisks' own contribution: HintSystem: true is its judgment that the device looks like internal storage rather than removable media, and the desktop's mount authorization distinguishes the two - a judgment worth knowing about when a file manager offers to mount one disk automatically but not another.

The mount request itself is one command:

# udisksctl mount --block-device /dev/vdb2
Mounted /dev/vdb2 at /media/root/handbook-opt
# udisksctl unmount --block-device /dev/vdb2
Unmounted /dev/vdb2.
udisksctl subcommand Description
info -b DEVICE Print udisks' properties for a block device (-b is short for --block-device).
mount -b DEVICE Mount the device's filesystem under /media/, report the path.
unmount -b DEVICE Unmount it again.
power-off -b DEVICE After unmounting, cut power to the enclosing drive - the USB "safe removal" step (Chapter 4, Section 4.3.3).

These two commands ran as root because this system has no desktop session, and udisks' polkit policy authorizes the session path - an ordinary user clicking a device in a file manager, or running udisksctl mount from one - only from an active local session. Chapter 4 demonstrated the boundary from the other side in Section 4.3.3: a bare shell without a polkit agent gets the same mount request refused, and the desktop session is what supplies the agent. The mount path also shows the naming rule: the filesystem landed under /media/root/ because root requested it, with the filesystem's label as the directory name.

Server policy is the mirror image. A machine with no local desktop session gains nothing from udisks2, and a server's mounts should be deliberate: declared in fstab if needed at boot (with nofail if optional), automounted by systemd if needed on demand (the next section), or mounted by hand for a one-off copy. Never mind elegance - the reason is failure analysis: a mount that exists for a reason written in a file can be explained months later; a mount that happened because a device was plugged in and some daemon was running cannot.

11.4.2. Systemd Automounts and Optional Devices

The systemd automount is the server-side answer to "mount it when someone needs it". An .automount unit installs an autofs trigger at the mount point: the path exists and looks empty until something accesses it, at which moment systemd mounts the real filesystem underneath and the access proceeds. Nothing waits at boot; the device is contacted only when first used.

Automounts are usually declared in fstab - the same entry as a boot-time mount, plus x-systemd.automount. The entry for the second practice filesystem:

UUID=bcd65ade-67cd-4438-9b2c-3f4319d69194 /srv/handbook/optional ext4 noatime,nofail,x-systemd.automount,x-systemd.device-timeout=5s 0 0

Every piece is load-bearing: the UUID names the device, x-systemd.automount makes the mount on-demand instead of at-boot, x-systemd.device-timeout=5s bounds how long an access waits for the device to appear, and nofail keeps the whole thing best-effort. Append it and regenerate:

# printf '%s\n' 'UUID=bcd65ade-67cd-4438-9b2c-3f4319d69194 /srv/handbook/optional ext4 noatime,nofail,x-systemd.automount,x-systemd.device-timeout=5s 0 0' >> /etc/fstab; systemctl daemon-reload

Silent. The generator produced an .automount unit alongside the mount unit, named by the same path rule:

# systemctl start srv-handbook-optional.automount; systemctl is-active srv-handbook-optional.automount
active

(Starting it by hand because the system is already booted; at boot it would be started automatically.) Before anything touches the path, the mount point is an autofs trigger, not a mounted filesystem:

# findmnt -no FSTYPE /srv/handbook/optional
autofs

An access is all it takes. As john, list the directory:

$ ls /srv/handbook/optional
lost+found

The listing appeared - and behind it, the trigger fired, systemd mounted /dev/vdb2, and the ls read the real filesystem. (lost+found is ext4's recovery directory, present on every ext4 filesystem; orphaned files from crashes are reconnected there, and Chapter 12 has the details.) The mount table now shows both layers:

# findmnt -no TARGET,SOURCE,FSTYPE /srv/handbook/optional
/srv/handbook/optional systemd-1 autofs
/srv/handbook/optional /dev/vdb2 ext4

The autofs trigger sits on top; the ext4 filesystem is mounted at the same path beneath it. From here the filesystem behaves like any mount - until the automount unit is stopped or, with x-systemd.idle-timeout= set (a documented option this example does not use), the mount is dropped again after a period of disuse.

Stopping the automount removes the trigger and the mount together:

# systemctl stop srv-handbook-optional.automount; systemctl stop srv-handbook-optional.mount

Both silent. (The second stop is belt and braces: stopping the automount unit ends the trigger, and explicitly stopping the mount unit ends any mount the trigger created.) The /srv/handbook/optional directory is back to being an ordinary empty directory, and /dev/vdb2 is dismounted - the state Section 11.4.1's udisks exercise also left it in.

When to choose an automount over a plain fstab entry: devices that are sometimes absent (an external backup disk, a spare), volumes whose service is intermittent, and filesystems where the mount's own startup cost should never be paid by the boot. The cost is a small one: every first access after a trigger pays the mount instead, and an access to a path whose device is gone waits up to the device timeout before failing. With nofail and a short timeout, that trade is usually right for optional storage.

11.4.3. Unmounting Busy Filesystems and Safe Removal

Unmounting is the operation that reveals who is still using a filesystem. The kernel refuses to detach a filesystem that any process holds - an open file, a shell sitting in the directory, a running program's executable or working directory - and reports target is busy. The refusal is protective: detaching under an active user would leave that process writing to a filesystem that is no longer there.

Mount the third practice filesystem to produce a busy one, in the root shell, and give it to john:

# mount /dev/vdb3 /srv/handbook/scratch; chown john:john /srv/handbook/scratch

Silent. Now, as john, in a second session, hold the mount point: change into /srv/handbook/scratch and leave a process running there - sleep 300 started in the background is enough, because a process's working directory keeps that directory (and its filesystem) in use. Back in the root shell, try to unmount:

# umount /srv/handbook/scratch
umount: /srv/handbook/scratch: target is busy.

Exit status 32, and the reason stated plainly. Two tools name the holder. fuser -vm lists processes using the mount point, with the kernel's own mount entry included:

# fuser -vm /srv/handbook/scratch
                     USER        PID ACCESS COMMAND
/srv/handbook/scratch:
                     root      mount /srv/handbook/scratch
                     john      ..c.. sleep
kernel  1154

The table (printed on standard error) shows root holding the mount itself - every mount has this kernel entry - and john's sleep in the directory. The ..c.. access markers mean current directory: that is the hold. The last line is fuser's identifier list on standard output: the kernel holder plus PID 1154, the sleep.

lsof +D walks the directory tree instead and reports open or directory-held files, one line per holder:

# lsof +D /srv/handbook/scratch
COMMAND  PID USER  FD   TYPE DEVICE SIZE/OFF NODE NAME
sleep   1154 john cwd    DIR 254,19     4096    2 /srv/handbook/scratch

Same finding, more detail: the holder is sleep, PID 1154, owned by john, holding the directory as its cwd (working directory). On a large filesystem lsof +D is the slower of the two - it walks every file - so fuser -vm is usually the first probe and lsof the confirmation.

End the holder and unmount again. The argument to kill is the PID the two tools just reported - 1154 on the example system, a different number on any other run:

# kill 1154
# umount /srv/handbook/scratch

Both silent. Confirm the filesystem is gone:

# findmnt /srv/handbook/scratch

No output, and findmnt exited 1 - its "not a mount point" answer. The pattern generalizes: umount says busy → fuser/lsof names the holder → end the holder (or let its owner finish) → umount succeeds. When the holder cannot be ended - an unreachable NFS server, a process that must not be killed - umount -l (lazy) detaches the filesystem immediately and cleans up when the last user goes, documented in umount(8) as the escape hatch; the cost is that the detached filesystem's data is not guaranteed flushed at that moment, so it is a last resort, not a routine step.

Safe removal of physical media is unmounting plus one more step: USB drives and card readers should be unmounted (every partition of them), then powered off with udisksctl power-off -b - the same sequence Chapter 4's Section 4.3.3 documents for a real USB disk. The file manager's eject icon performs it. The point of the power-off step is the enclosure, not the data: unmount flushes and releases the filesystem, but only cutting the link parks some hardware cleanly.