Service to automount USB devices with udisks2

Udev is problematic when trying to mount USB devices – it works first time, but fails for re-insertions (Debian 9 Stretch, running in KVM, with ntfs-3g USB drive passed through).  Instead create a script to monitor:

apt-get install udisks2

#nano /usr/local/bin/myautomounter

pathtoname() { udevadm info -p /sys/”$1″ | awk -v FS== ‘/DEVNAME/#39;
stdbuf -oL — udevadm monitor –udev -s block | while read -r — _ _ event devpath _; do
        if [ “$event” = add ]; then
            devname=$(pathtoname “$devpath”)
            udisksctl mount –block-device “$devname” –no-user-interaction


Next, create a systemd service to run at startup:

#nano /etc/systemd/system/myautomounter.service

# Uses udevadm to monitor for drives to mount

Description=My Auto Mount


WantedBy =

Reload systemd configs and start the service:

systemctl reload
service myautomounter start