6

I want to go beyond this excellent solution by @A.B. which permits to auto-mount a given removable ext3 FS using a udev rule. Specifically I'd like to specify several mount options: "nodev,noexec,x-gvfs-show", preferably within the specific udev rule used to mount it, since it deals with one very specific physical medium.

With no option specified, the volume mounts so:

$ cat /proc/mounts | grep -e MYLABEL
/dev/mmcblk0p1 /mnt/MYLABEL ext3 rw,relatime,data=ordered 0 0

To specify mount options I tried to expand the udev rule from the previous solution so:

KERNEL=="mmc*", ENV{ID_FS_UUID}=="______", RUN+="/usr/local/sbin/mount_by.sh '%E{ID_FS_LABEL}' '%E{ID_FS_UUID}' 'nodev,noexec,x-gvfs-show'"

where /usr/local/sbin/mount_by.sh includes:

#!/bin/sh
/bin/mount "/dev/disk/by-uuid/$2" "/mnt/$1" -o "$3"

The above breaks quietly as root defined mount options seem to be rejected and the volume (an SD card) is silently mounted at /media/MYUSER/MYLABEL.

What's wrong ?


What I tried to do:

The many posts I have seen dealing with mount-option(s) specification difficulties when using udev (e.g. 1,2,...) remain unanswered.

One deals with the GVFS option x-gvfs-show used in conjunction with udev rules, when the mounted volume must appear under Devices on the Nautilus GUI and the non-root user must be able to unmount it. To at least get that last aspect covered, I reverted to NOT specifying mount option in my udev rules but added an /etc/fstab entry, a pretty ugly hack given the fact I keep a functioning udev rule for the same uuid volume in parallel. Still, I added :

UUID=_________ /mnt/MYLABEL ext3 nodev,noexec,x-gvfs-show 0 2

The FS is mounted but although it appears as intended in Nautilus, cat /proc/mounts | grep -e MYLABEL yields the same result as before... i.e. options nodev,noexec appear to be blithely ignored.

Not too surprisingly I also get the error message on screen:

Unable to mount MYLABEL. Device /dev/mmcblk0p1 is already mounted at `/mnt/MYLABEL'.

Any thought anyone ?

BOUNTY Please try to provide a canonical answer that can serve this OP and the whole lot of people interested in specifying mount-options via a udev rule. If not possible, please explain why and provide a viable workaround. Cheers.

Cbhihe
  • 2,801
  • 3
  • 27
  • 47

3 Answers3

2

The script

#!/bin/sh
export mount_point="/mnt/$1"
existing_device=$(awk '$2 == ENVIRON["mount_point"] {print $1; exit}' < /proc/mounts)
if [ -n "$existing_device" ]; then
  exit 1
fi
mkdir -p "$mount_point"
sleep 1 # Perhaps not necessary, but in the test with the OP it was necessary
mount "/dev/disk/by-uuid/$2" "$mount_point" -o "$3"
exit 0

should work perfect with a udev-rule like this (you have to replace the UUID)

KERNEL=="mmc*", ENV{ID_FS_UUID}=="13ededb9-41e9-4674-b9dc-40ce178af91d", RUN+="/usr/local/bin/mount_by '%E{ID_PART_ENTRY_NAME}' '%E{ID_FS_UUID}' nodev,noexec"

Because the udev-rules runs as root, the -o will be used.

The bad thing, definitely in my system, x-gvfs-show doesn't work in the option list for -o

A.B.
  • 92,125
2

You shouldn't be using a udev rule to do this. For one, it is conflicting with udisks: they are both trying to mount the drive in different places, and which one wins is anyone's guess. Also when you are done with the drive, you want to be able to right click on it in the gui and eject it, but you can't do that if the udev rule mounted it.

If you want it mounted with custom options or a custom location, you just need to add an entry to /etc/fstab for it, and make sure you include the "user" option, which allows non root users to auto mount it with udisks, and then they can later unmount it.

psusi
  • 38,031
1

To complete A.B.'s answer and for the record:

Mount options can be specified inside udev rules ... under certain conditions. As I ran tests I noticed that some options are:

  • accepted and enforced: ro, rw, nodev, noexec
  • accepted and ignored: comment=x-gvfs-show,
  • not accepted, in that they break the udev rule: mode=0XYZ, x-gvfs-show

In the latter case, breaking the udev rule means udisk auto-mounts the medium with default options.

The udev tag MODE="0550", you could think equivalent to invoking the read-only (ro) mount-option, is ignored. Note that specifying ro or rw does not show in Nautilus as you'd expect. Oddly permissions displayed by Nautilus remain unchanged, whereas trying to write if ro is set will result in a warning message and write permission denial. Bug ?

Last but not least, do not arrange for an /etc/fstab entry at the same time you specify a udev mount rule for a given device. That will results in a conflict and an error message as udisk will compete with your udev rule to mount the device. No surprise in that. The device will still be mounted though (in my case, by the udev rule - please don't ask why...) and can even be accessed.

HTH a bit.

Cbhihe
  • 2,801
  • 3
  • 27
  • 47