rustymanito
rustymanito

Reputation: 41

BASH script doesn't mount my device when invoked by UDEV

SOLVED IN UNIX & LINUX

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/key-mount.log"

sudo mount /dev/masterkey /mnt/masterkey > /tmp/key-mount.log

MOUNT_POINT="$(findmnt -n -o TARGET --source '/dev/masterkey')"

echo "$MOUNT_POINT" >> "$LOG_FILE"

if [ -n $MOUNT_POINT ]; then
    echo "Device with UUID: $UUID mounted at $MOUNT_POINT" >> "$LOG_FILE"
else 
    echo "Error: Device with UUID: $UUID not mounted" >> "$LOG_FILE"
fi

In order to run the mount command strait forward as a root without waiting for the password, i did edit the /etc/sudoers file with visudo and added both lines with the NOPASSWD field for mount for the vagrant and root user. like this :

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

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. That file looks like this :


Device with UUID:  mounted at 

I edited the script and instead i ran it without sudo, but got the same output of above.

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 (of corse i did #!/bin/sh) and in none of that ways it worked. So it seems it doesn't have any problem invoking the script, 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 it's work propperly.

And before editing the sudoers file to not requiring a password, i did try many times the script with the same methods presented, but got the same results of above.


SYSTEM

Vagrant (archlinux/archlinux)

Kernel 6.10.10-arch1-1

Upvotes: 3

Views: 111

Answers (0)

Related Questions