I tried to do this on my computer and it's work :)
First I get a name for my device :
ls -l /proc/disk/by-id/
In my case it is /proc/disk/by-id/usb-09a6_8001
I added this line in /etc/fstab :
/dev/disk/by-id/usb-09a6_8001 /media/macle ext2 ro,users 0 2
And it's working, when I plug my usbkey, it's mounted ro and owned by my user.
The problem seems to be a catchall rule in /lib/udev/rules.d/69-libmtp.rules
:
# Autoprobe vendor-specific, communication and PTP devices
ENV{ID_MTP_DEVICE}!="1", ENV{MTP_NO_PROBE}!="1", ENV{COLOR_MEASUREMENT_DEVICE}!="1", ENV{libsane_matched}!="yes", ATTR{bDeviceClass}=="00|02|06|ef|ff", PROGRAM="/usr/lib/udev/mtp-probe /sys$env{DEVPATH} $attr{busnum} $attr{devnum}", RESULT=="1", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"
Commenting out that line, as shown below, fixed the problem:
# Autoprobe vendor-specific, communication and PTP devices
ENV{ID_MTP_DEVICE}!="1", ENV{MTP_NO_PROBE}!="1", ENV{COLOR_MEASUREMENT_DEVICE}!="1", ENV{libsane_matched}!="yes", ATTR{bDeviceClass}=="00|02|06|ef|ff", PROGRAM="/usr/lib/udev/mtp-probe /sys$env{DEVPATH} $attr{busnum} $attr{devnum}", RESULT=="1", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"
In my case, I had to reboot, but there's probably a service that could be restarted instead.
Best Answer
Udev manages devices via rules that determine what to do when a device is inserted (or removed). Udev itself doesn't handle mounting, but you can make it invoke an external program to do the mounting.
There are rules, stored in the various files under
/etc/udev/rules.d/
, that create entries in/dev/disk/by-label/
. We can use the same matching conditions to match a USB device which has a filesystem label and run a custom script.The custom script should create the mount point and perform the mounting. It should take a bit of care in case the directory already exists. The script I've written will do nothing if the mount point is already in use as a mount point, but will happily shadow a non-empty directory. Customize to your taste.
Don't forget to unmount the device before unplugging it, otherwise you may lose data.
Ubuntu - Automatically mount external drives to /media/LABEL on boot without a user logged in? shows the same technique with a different script.