BASH script doesn't mount my device when invoked by UDEV
2
votes
1
answer
113
views
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).
Asked by rustymanito
(35 rep)
Feb 14, 2025, 12:58 AM
Last activity: Feb 14, 2025, 09:36 PM
Last activity: Feb 14, 2025, 09:36 PM