Taking inspiration from this Ask Ubuntu answer, and after lot of tinkering, here's what I settled on: a udev rule that spawns a systemd (user) unit file to mount the volume using udisksctl
.
Create this file under your own user account, first making intermediate directories as necessary (mkdir -p ~/.config/systemd/user
):
# ~/.config/systemd/user/mount-backup-volume.service
[Unit]
Description=Mount backup volume on external USB drive
[Service]
Type=oneshot
ExecStart=/usr/bin/udisksctl mount -b /dev/disk/by-id/usb-MFR_MODEL_SERIAL-0:0-partN
You can get the actual /dev/disk/by-id/usb-MFR_MODEL_SERIAL-0:0-partN
for your own device and partition from the output of udiskctl dump
. You don't want to use something like /dev/sdb1
, because that could change next reboot, depending on what other devices you have plugged in, in what order.
As root, using sudo vi
or whatever is your preference:
# /etc/udev/rules.d/98-automount-backup.rules
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="THE-FILE-SYSTEMS-UUID", ENV{SYSTEMD_USER_WANTS}="mount-backup-volume.service"
Get THE-FILE-SYSTEMS-UUID
from the output of blkid /dev/sdXY
when your device is mounted (e.g., sudo blkid /dev/sdb1
), or search for IdUUID:
in the output of udisksctl dump
, associated with the desired device and partition.
The names mount-backup-volume.service
and 98-automount-backup.rules
are up to you; just make sure ENV{SYSTEMD_USER_WANTS}
matches whatever you named the unit file. I didn't find that there was any need to "enable" the systemd unit file, or udevadm control --reload
, as you might see suggested in some places; udev picks up on the changes as soon as you save the file in /etc/udev/rules.d
.
Caveats
- relies on systemd; probably somewhat Debian/Ubuntu specific in where the config files go exactly
- is tedious if you want to do this for multiple volumes, or multiple partitions on a single device
You may want to look into using a mount helper like udiskie or udevil if you're doing this more than a couple times. That seems to be how the minimalist / tiling window manager crowd gets by.
Details
This rigmarole seems to be necessary because udev rules alone don't run with the permissions of the user. You might think that it'd be possible with su -c
or something like that, but that was the very first thing I tried and didn't have any luck. I also didn't want to mess with /etc/fstab
; this isn't a server. I'm the only user of the machine, and I want the device to be mounted on hotplug, not at boot-time.
Too bad this isn't simpler—more elementary-like. The only issue I'm aware of where this was proposed as a feature for Pantheon Files was eventually closed as WONTFIX without much out-in-the-open discussion.
I wanted something near to how Time Machine works on macOS, in that a backup would start soon after the device with the backups on it was plugged in. Back In Time got me pretty close, and it even has a "When drive get [sic] connected (udev)" option, but that doesn't actually mount the device for you. It assumes the desktop environment mounts it automatically, I guess. And as you know, elementary OS doesn't.
This is probably OK, because other operating systems' tendencies to mount everything mountable when you plug a device in—or nag you about it every time—is less preferable than elementary OS's default behavior.