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

#!/bin/sh
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
        fi
done

Reference: https://wiki.archlinux.org/index.php/Udisks#Udisks

Next, create a systemd service to run at startup:

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

# Uses udevadm to monitor for drives to mount

[Unit]
Type=simple
Description=My Auto Mount

[Service]
ExecStart=/usr/local/bin/myautomnt

[Install]
WantedBy = multi-user.target

Reload systemd configs and start the service:

systemctl reload
service myautomounter start