Removable Drives on Linux: Convenience Options

What methods are there to connect an external (USB) drive or memory stick to a Linux box — most conveniently? As criteria for convenience, let’s take:

  • minimal need for root permissions
  • minimal number of commands in day-to-day work
  • should work seamlessly with Linux native filesystems (ext4)
  • a stable, predictable mount point is highly desirable, at an arbitrary location
  • must work at a shell prompt under any window manager, and not depend on a specific file manager or desktop environment
  • automatic mounting when the device is plugged in is considered nice, but optional
  • should not be more complicated than running sudo mount ... directly

What are the options?

pmount

The pmount utility is an old standard. It allows any user to mount an external device without superuser privileges. It takes the name of a device (actually, a partition, such as /dev/sdc1) and creates a corresponding directory under /media/.

This tool works well when used only occasionally, but can be cumbersome for frequent use, mostly because it requires the device name, which is not stable, meaning that one has to first use lsblk or dmesg every time to find the name of the most recently added device.

Like the mount command itself, pmount preserves POSIX permissions on the mounted filesystem. This means that a POSIX-compliant filesystem (such as ext4) may not immediately writeable by the user: that depends on the ownership of the mounted filesystem. (We will come back to this issue at the very end of this post.) Permissions are not a concern when using filesystems such as NTFS or FAT that do not maintain permission information.

udisks

The udisks service is a daemon that “implements D-Bus interfaces used to query and manipulate storage devices”. It includes a command-line utility udiskctl, which can be used to mount devices, like so: udisksctl mount -b /dev/sdc1. As a command-line interface, this is clearly no better than running mount directly.

A number of wrappers for udisks exist that facilitate mounting, such as udiskie. The problem with udiskie for example is that it won’t allow an arbitrary mount point: devices are mounted under /run/media/<USER>/<DEVICE> or possibly /media/<USER>/<DEVICE>. This behavior is not easily configurable. It can be overridden using udev rules, but that clearly fails the “convenience” criterion.

udev

The udev service allows the registering of user-space handlers for system events. The service also manages the entries in the /dev directory, to represent the devices that are actually connected at any moment.

Event handlers are registered by creating “udev rules”, via configuration files that live under /etc/udev/rules.d. The system is clearly very powerful and flexible, but also fairly complicated, with a not-at-all straightforward syntax for the rules.

I don’t want to become a systemd expert; all I want to do is mount a disk.

automount

An option from a very different lineage is the Linux automounter, automount and autofs. It was not intended for removable drives, but to mount directories on other machines via NFS.

Because of its different heritage, the automounter behaves in a way that may seem counterintuitive. By default, it doesn’t do anything with a newly plugged in device: the /media directory (assuming this is where the mount point is supposed to be) will appear empty. Only when the drive is accessed does the service try to mount the device and create the mount point directory. Once the device has been idle for a while, it will automatically be unmounted, and the mount point removed. (This is quite inconvenient if one wants to write a file to the device using a FileSelector GUI widget: there is no directory to click on.)

Configuring autofs involves two files: first, one needs to add an entry /etc/auto.master, which tells the system where to find the configuration for each mount point; then one needs to create the config file for the actual mount point, typically under /etc/auto.master.d/.

fstab and mount

It turns out, the most basic way might well be the most convenient, using an fstab entry and the standard mount command. It goes like this:

  1. Assign the device (actually: the filesystem) a label when formatting it. (There are many tools to do that, such as gnome-disks, gparted, mintstick, or the bare mkfs. command-line tools.)

  2. Create an entry in /etc/fstab that mentions this label, and the desired mount point, together with some helpful options, like so:

    LABEL=TheDrive   /media/Drive   ext4   rw,noauto,user   0   0
    

    The option noauto means that this device will not be automatically mounted during the boot process (after all, it may not even be connected). The user option means that any user can mount this device, without special privileges.

  3. Create the mount point.

  4. Mount the device using mount -L TheDrive or mount /media/Drive, unmount using umount /media/Drive. (The umount command does not support the -L option.)

One problem is that it is easy to forget to properly unmount the device before disconnecting it!

Otherwise, this solution actually does meet my convenience criteria:

  • A single, non-root command to mount and unmount a device
  • Minimal set-up (one line in fstab, and creation of a mount point)
  • Entirely familiar commands and utilities

The one constraint is that the drive must be assigned a permanent label, and therefore will not work with arbitrary devices.

Ownership of mounted filesystem (ext2,3,4 only)

There is a possibly confusing issue when the external device is formatted using one of the Linux-native filesystems (such as ext4), which contain ownership and permission information. (This discussion does not apply to devices formatted using FAT or NTFS: filesystems that do not maintain this kind of ownership information.)

When a device that has been formatted as (say) ext4 is mounted, it retains its ownership information. Possibly surprisingly, the permissions associated with the mount point do not matter: once a device is mounted at that point, the device and its permissions hide the original mount point and its permissions. Once the device is unmounted, the mount point’s permissions on the host system will appear again.

If the device has been formatted as root (which is probably a common situation), then the filesystem on the device will also be owned by root. To make the filesystem writable by a user, its ownership needs to be changed to that user. This only needs to happen once, for instance when the device is mounted for the first time.

So, let’s say the device has been mounted on /media/Drive. Now execute sudo chown <user>:<group> /media/Drive (supplying the appropriate user and group name, of course). This only needs to be done once; subsequently, the device can be unmounted and remounted, and will be writable by the named user.