We're updating our servers from a very out-of-date distro to a modern Debian Jessie based system, including lightdm / xfce, and of course systemd (and udisks2). One sticking point is automounting USB drives. We used to accomplish this with some udev rules. The old rules almost still work – the mount point gets created and the drive is mounted fine, but after a few seconds systemd is doing something that breaks the mount, so subsequent access attempts result in "Transport endpoint is not connected" errors.
Manually mounting the drive via the command line works fine. So does letting a file manager (thunar and thunar-volman, which in turn uses udisks2). But those are not viable options – these systems mostly run headless, so thunar isn't normally running. We need to be able to plug in disk drives for unattended cron-based backups.
I thought that modifying the udev script to spawn a detached job which waits a few seconds before performing the mount might do the trick, but systemd seems to go out of its way to prevent this – it somehow still waits for the detached job to finish before continuing.
Perhaps having the udev script tickle udisks2 somehow is the right approach? I'm at a lose, so any advice greatly appreciated.
Best Answer
After several false starts I figured this out. The key is to add a systemd unit service between udev and a mounting script.
(For the record, I was not able to get this working using udisks2 (via something like
udisksctl mount -b /dev/sdb1
) called either directly from a udev rule or from a systemd unit file. There seems to be a race condition and the device node isn't quite ready, resulting inError looking up object for device /dev/sdb1
. Unfortunate, since udisks2 could take care of all the mount point messyness...)The heavy lifting is done by a shell script, which takes care of creating and removing mount points, and mounting and unmounting the drives.
/usr/local/bin/usb-mount.sh
The script, in turn, is called by a systemd unit file. We use the "@" filename syntax so we can pass the device name as an argument.
/etc/systemd/system/usb-mount@.service
Finally, some udev rules start and stop the systemd unit service on hotplug/unplug:
/etc/udev/rules.d/99-local.rules
This seems to do the trick! A couple of useful commands for debugging stuff like this:
udevadm control -l debug
turns on verbose logging to/var/log/syslog
so you can see what's happening.udevadm control --reload-rules
after you modify files in the rules.d dir (may not be necessary, but can't hurt...).systemctl daemon-reload
after you modify systemd unit files.