I'm currently trying to mount any usb drive connected to my computer automaticaly. My goal is to mount usb devices either with the label if they have one or with the uuid if they don't.
To do it I write a udev rule in /etc/udev/rules.d/10-usb-detect.rules :
ACTION=="add", KERNEL=="sd?[0-9]", SUBSYSTEM=="block", RUN+="/usr/local/bin/add.sh"
The script is called everytime an add event append on the block subsystem.
The udev rule works fine however when I try to mount the file system from the script it doesn't work. What is strange is that the mount command from the script always return $?=0, so logically the file system should be mounted but it's not.
Here is my script :
#!/bin/bash
LOG_FILE=<path_to_file>
echo "New usb device detected at $DEVNAME" >> $LOG_FILE
echo "mount $DEVNAME /media/usb/test" >> $LOG_FILE
mount $DEVNAME /media/usb/test &>> $LOG_FILE
ret=$?
echo "$ret" >> $LOG_FILE
if [ $ret == "0" ]; then
echo "$DEVNAME mounted at /media/usb/test" >> $LOG_FILE
else
echo "Failed to mount $DEVNAME at /media/usb/test" >> $LOG_FILE
fi
echo "" >> $LOG_FILE
I tried with /media/usb/test not existing and I have the expected error from the mount command. But when the folder exists he mount command returns 0 even if the filesystem is not mounted.
Here is an output of the log file :
New usb device detected at /dev/sdc1
mount /dev/sdc1 /media/usb/test
mount: mount point /media/usb/test does not exist
32
Failed to mount /dev/sdc1 at /media/usb/test
New usb device detected at /dev/sdc1
mount /dev/sdc1 /media/usb/test
0
/dev/sdc1 mounted at /media/usb/test
/dev/sdc1 is not mounted even if mount returns 0.
I precise that when I mount the file system from command line, there is absolutely no problem and the file system is mounted as it should.
Does someone has a clue about how I could debug this?
I think the problem is because the script is called from udev because if I call it from command line it also works.