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.

0
2
+50

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

4
  • Did you try listing that option as comment=x-gvfs-show ? (Do you run 14.04 or earlier ?) -- Now trying the udev rule without the leading KERNEL=="mmc*", as in my case... – Cbhihe Oct 26 '15 at 18:28
  • I have tried this without KERNEL=="mmc*" and I have tried nodev,noexec,x-gvfs-show and nodev,noexec,comment=x-gvfs-show, no drive in Nautilus (Files). It works only with an fstab entry and than with nodev,noexec,x-gvfs-show. Sorry =\ – A.B. Oct 26 '15 at 18:33
  • Please do modify your answer when you have time (within the bounty grace period !) ;-D ----- Yay ! prbm solved ! – Cbhihe Oct 26 '15 at 19:16
  • Right I found a satisfactory solution, thanks to your insight. Can you modify your answer so I can accept it ? -- the udev rule should read: 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" -- It does not work without the KERNEL tag !0_o! . Also note that the last argument does not accept single quotes. It was what broke the udev rule all the time. The x-gvfs-show option appears to break the udev rule too, while comment=x-gvfs-show is just ignored. I can live with that. Thanks @A.B. – Cbhihe Oct 26 '15 at 19:22
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.

6
  • Yes, there obviously is a conflict. If you looked at the previous solution by @A.B. you will see that the udev rule is necessary to mount the SD card on /mnt/MYLABEL by root only, upon insertion in the SD slot. I cannot allow users to mount, unmount that medium. The fact that specifying an entry in fstab (as reported) with the x-gvfs-show option allows any user to umnount the card from nautilus was just a stop-gap while waiting for help. Ideally I'd like to get RID of the fstab entry, while specifying options in my udev rule. Do you confirm that it is NOT possible ? – Cbhihe Oct 26 '15 at 18:22
  • @Cbhihe, right.. it is not possible since udisks will try to mount it. Also if you don't let the user unmount it, then they will just yank it out and cause all kinds of problems. Let udisks do its job and take care of mounting and unmounting. – psusi Oct 26 '15 at 18:32
  • Sorry, I have strict constraints, and I cannot let user(s) mount or unmount,mount must not be on /media(MYUSER/MYLABEL but on /mnt/MYLABEL, options must be set and mount must occur automatically upon insertion of SD card. Actually my question is best summarized as "how does one specify mount OPTIONS inside a udev rule ?" Your answer does not address that. – Cbhihe Oct 26 '15 at 19:20
  • @Cbhihe, so you want your user to insert the cd card... use it.. then pull it out without unmounting it? This makes no sense. – psusi Oct 27 '15 at 1:16
  • You're certainly right. But in this case, the partition had to be mounted with their name. Therefore udev. – A.B. Oct 27 '15 at 5:40
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.