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).
1 Answer 1
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.
$PATH
. Use the absolute path forfindmnt
(type -p findmnt
).RUN+="/usr/bin/mount /dev/masterkey /mnt/masterkey'"
work?ls /dev | grep masterkey
the symlink does appear in redmount ... > "$LOG_FILE"
tomount ... > "$LOG_FILE" 2>&1
and see if there is any error message.