Ubuntu – mount options for udev-rule mounted ext3 fs on14.04.3

14.04ext3mountudev

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.

Best Answer

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