2

Im trying to create a udev rule that mounts my device by his uuid, so i can after read into its contents.

My udev file is localized in the /etc/udev/rules.d/99-local.rules, and this are it's contents :

ACTION=="add", ENV{ID_FS_UUID}=="12CB-F616", SYMLINK+="masterkey", RUN+="/usr/bin/mountkey.sh" 

The script ran by the udev rule consists of this :

LOG_FILE="/tmp/mount-key.log" mount /dev/masterkey /mnt/masterkey > "$LOG_FILE" MOUNT_POINT="$(findmnt -n -o TARGET --source '/dev/masterkey')" echo "Device mounted at $MOUNT_POINT" >> "$LOG_FILE" 

The problem is that when udev invokes the command mount apparently it doesn't work and nor the command produces any output as well as the MOUNT_POINT env variable in the log file. even though udev actually creates the masterkey symlink.

The log file looks like this :

 Device mounted at 

I comprobed that it did not indeed mount the partition after running lsblk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 1 3.8G 0 disk └─sda1 8:1 1 3.8G 0 part ... 

I edited the script and instead, i ran it with sudo, but got the same output of above. So i decided to run the mount command as a root without waiting for a password (thinking it was the problem), i edited the /etc/sudoers file with visudo and added the line with the NOPASSWD field for mount for root user, like this :

root ALL=(ALL:ALL) ALL root ALL=NOPASSWD:/usr/bin/mount 

As well i tried to write to run it in this different ways :

ACTION=="add", ENV{ID_FS_UUID}=="12CB-F616", SYMLINK+="masterkey", RUN+="/bin/bash -c '/usr/bin/mountkey.sh'" ACTION=="add", ENV{ID_FS_UUID}=="12CB-F616", SYMLINK+="masterkey", RUN+="/bin/bash -c 'sudo /usr/bin/mount /dev/masterkey /mnt/masterkey'" ACTION=="add", ENV{ID_FS_UUID}=="12CB-F616", SYMLINK+="masterkey", RUN+="/bin/bash -c '/usr/bin/mount /dev/masterkey /mnt/masterkey'" 

Too i did all of this with sh (with #!/bin/sh) and in none of that ways it worked. So it seems it doesn't have any problem invoking the script, and creating the symlink, but running the mount binary in whatever way it is presented.

And nor its my mount binary , because when i run :

sudo mount /dev/sdnx /mnt/masterkey 

it does work propperly.

After this many tries without seeing a hint of which could be the problem i changed the udev rule to this :

ACTION=="add", ENV{ID_FS_UUID}=="12CB-F616", RUN+="/bin/bash -c '/usr/local/bin/mountkey.sh $ID_FS_UUID'" 

And my bash script :

#!/bin/bash UUID="$1" DEVICE=$(blkid -o device -t UUID="$UUID") echo "$UUID\n$DEVICE" > "/tmp/mountkey.log" mount $DEVICE /media/masterkey 

But ohh! surprise, nothing did change.


VM SYSTEM

Vagrant (archlinux/archlinux)

Kernel 6.10.10-arch1-1

REAL SYSTEM

I tried to run the udev rule inside my real machine, which is :

Arch linux / 6.13.2-arch1-1

But i still got the same result (No partition mounted in the system).

So by doing this i pruebed the problem is within one of the files (the udev rule or the bash script).

4
  • The runtime contexts (desktop vs udev rule) differ, especially $PATH. Use the absolute path for findmnt (type -p findmnt).CommentedFeb 14 at 2:10
  • Does RUN+="/usr/bin/mount /dev/masterkey /mnt/masterkey'" work?CommentedFeb 14 at 2:30
  • @HaukeLaging no it does not, it seems like udev actually creates the symlink "masterkey", but it does not mount it, when i run ls /dev | grep masterkey the symlink does appear in redCommentedFeb 14 at 2:55
  • Change mount … > "$LOG_FILE" to mount … > "$LOG_FILE" 2>&1 and see if there is any error message.CommentedFeb 14 at 5:15

1 Answer 1

2

You can't use mount from udev rules.  udev runs with a separate mount namespace, so running mount doesn't work; you need to use systemd-mount instead.  Like this :

#!/bin/bash /usr/bin/systemd-mount --no-block --automount=yes /dev/masterkey /media/masterkey 

--no-block says to systemd-mount to not wait for a response from mounting the device, because udev rules when executing commands have a tiny time before they time out.

--automount=yes – you should use automount always according to udev rules, when you have a removable device, like in this case a USB.  What this does is that each time a user or application accesses the mount point, systemd-mount will automatically mount the drive in that directory so you can access it.

0

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.