I tried to create some udev rules to mount and unmount my USB flash drives; the rules for the moment are very simple:
ACTION=="add",KERNEL=="sd[b-z]",RUN+="/root/scripts/plug_flash_drive.sh %k"
ACTION=="remove",KERNEL=="sd[b-z]",RUN+="/root/scripts/unplug_flash_drive.sh %k"
plug_flash_drive.sh is also very simple:
device_name=$1
mount_options="umask=000,utf8"
if [ ! -e "/media/$device_name" ]; then
mkdir "/media/$device_name"
fi
sleep 1
/usr/bin/mount "/dev/$device_name" "/media/$device_name" -o "$mount_options"
unplug_flash_drive.sh:
device_name=$1
umount "/dev/$device_name"
rmdir "/media/$device_name"
I have done some tests so I can ascertain that:
- When plugged in, my flash drive is detected; a file is created in /dev
- plug_flash_drive.sh is called by udev
- the mkdir part of the script works
- however, it seems that the "mount" part of the script is not executed, so my drive is not mounted
- when I call my scripts on the command line, they perfectly work
Does anybody know why mount is not executed when called by udev?
EDIT 28/08/14: I added "grep -q /proc/mounts && echo success || echo failure" at the end of my script to check in my debug log if the device is actually mounted before the script ends. It appears that the device is mounted at that point even when the script is called by udev. So the real problem is now "my block device is seemingly unmounted after the mount script end when called through udev" :s
mkdir "$mount_dir"
butrmdir "/media/$device_name"
? Where is$mount_dir
set?set -xv
andexec >> "$HOME"/mount.log 2>&1
into the.sh
files?