3

In Debian Wheezy I had a special rule for my ntfs usb hdd. When it is inserted it is mounted in /media under a specific sub-folder.

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", SYMLINK+="mx%n"
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", RUN+="/bin/mount <options>", OPTIONS="last_rule"
ACTION=="remove", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", RUN+="/bin/umount <options>"

Ufter I updated to Jessie it stopped working. I found out that after changes in udev you cannot use mount and it is recommended to use either udisks2 or some self-written systemd unit. I chose udisks2 and rewrote rule as follows

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", SYMLINK+="mx%n"
ACTION=="add", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", RUN+="/bin/su storage_user -c '/usr/bin/udisksctl mount --block-device /dev/mx1 --filesystem-type ntfs --options locale=en_IE.UTF-8,fmask=0113,dmask=0002 --no-user-interaction'", OPTIONS="last_rule"
ACTION=="remove", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", RUN+="/usr/bin/udisksctl unmount --block-device /dev/mx1 --no-user-interaction"

It doesn't work. In syslog I see:

Error looking up object for device /dev/mx1

But if I run this command from cli it works fine. I believe because of the asynchronous nature of systemd services /dev/mx1 is not ready when udisk2 is trying to mount usb hdd.

  1. What rule should I write instead?
  2. Is there any good guide on Internet how to write custom automounting rules especially for ntfs file systems?
1
  • I was able to reproduce the error with udisksctl mount -b /foo/bar. Probably the special rule is triggerd too early
    – darw
    Dec 5, 2021 at 17:18

1 Answer 1

1

Unfortunately I wasn't able to find the reason why udev and udisks2 didn't work together. But I found a solution for my problem here. Below is a simple example how to implement automount of a ntfs usb hdd. First is a script mount.sh to mount a drive

#!/bin/bash
mkdir -p /media/usbhdd
mount -t ntfs-3g -o locale=en_IE.UTF-8,fmask=0113,dmask=0002,uid=storage-user,gid=storage-group /dev/mx1 /media/usbhdd

Then we create a systemd unit in /etc/systemd/system/mount-hdd.service

[Unit]
Description=mount usb hdd
[Service]
Type=forking
ExecStart=/usr/local/scripts/storage/mount.sh
[Install]
WantedBy=multi-user.target

And finally udev rule

ACTION=="add", SUBSYSTEMS=="usb", KERNEL=="sd*", ATTRS{serial}=="<serial_number>", SYMLINK+="mx%n"
ACTION=="add", SUBSYSTEMS=="usb", KERNEL=="sd*1", ATTRS{serial}=="<serial_number>", RUN+="/bin/systemctl start mount-hdd"
ACTION=="remove", SUBSYSTEMS=="usb", ATTRS{serial}=="<serial_number>", RUN+="/bin/umount /media/usbhdd", RUN+="/bin/rmdir /media/usbhdd"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .