0

I have a udev rule which automatically mounts usb device when connected

The rule is the following one :

# cat /etc/udev/rules.d/10-usbdetectd.rules 
KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,uid=1000,gid=1000,umask=022"
# Mount the device
ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/usb"
# Clean up after removal
ACTION=="remove", RUN+="/bin/umount -l /media/usb" 
# Exit
LABEL="media_by_label_auto_mount_end"

This rule is working well, however, I would like to mount the device with full permissions. To do so I have to set umask=000

When I'm trying mannually it works as expected

# ls -dl /media/usb/
drwxr-xr-x    2 root     root          4096 Jan 29  2019 /media/usb/

# mount -o relatime,utf8,uid=1000,gid=1000,umask=000 /dev/sda1 /media/usb/

# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root              6643016     86356   6199492   1% /
devtmpfs                223204         0    223204   0% /dev
tmpfs                   256484         0    256484   0% /dev/shm
tmpfs                   256484        48    256436   0% /tmp
tmpfs                   256484       132    256352   0% /run
/dev/mmcblk0p1           20185      2854     17331  14% /boot
/dev/sda1              1957600        96   1957504   0% /media/usb

# ls -dl /media/usb/
drwxrwxrwx    2 user    user        16384 Jan  1  1970 /media/usb/

However when the mount is done by udev, the mask is not applied

# ls -dl /media/usb/
drwxr-xr-x    2 root     root          4096 Jan 29  2019 /media/usb/

[USB drive plug]

# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root              6643016     86356   6199492   1% /
devtmpfs                223204         0    223204   0% /dev
tmpfs                   256484         0    256484   0% /dev/shm
tmpfs                   256484        52    256432   0% /tmp
tmpfs                   256484       132    256352   0% /run
/dev/mmcblk0p1           20185      2854     17331  14% /boot
/dev/sda1              1957600        96   1957504   0% /media/usb

# ls -dl /media/usb/
drwxr-xr-x    2 root     root         16384 Jan  1  1970 /media/usb/

It seems that when the mount is done by udev, none of the mount options are applied.

uid=1000 and gid=1000 corresponds to my user

# cat /etc/passwd 
[...]
user:x:1000:1000:- Standard user:/home/user:/bin/bash
[...]

1 Answer 1

0

It seems that the problem came from udev rule syntax.

After using an external script instead of directly calling /bin/mount and adding some logs I remarqueds that only relatime option was passed to the script.

After modifying udev rule to :

[...]
# Global mount options
ACTION=="add", ENV{mount_options}="relatime,utf8,uid=1000,gid=1000,dmask=0000,fmask=0000"
# Mount the device
ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/usb"
[...]

The udev rules works and mount the USB drive with expected permissions.

So it seems that it's not possible to override environment variable into udev rule once it has been defined (or I'm not using right syntax).

If someone know how I could keep the filesystem specific mount options I would be glad to see it.

Otherwise this workaround is sufficient for my case even if its not optimal.

You must log in to answer this question.

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