Auto mount usb drive from udev rules and shell script

Viewed 15780

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.

3 Answers

I wanted my Raspberrypi Pico microcontroller board to get automatically mounted and unmounted, much the same sort of problem as the initial question as the RPi Pico is recognized as usb storage when the bootsel button is pressed as you plug it in. So here is what I put in my /lib/udev/rules.d/10-usb-storage.rules (the exact path may be distribution dependent)

root@darkstar:/lib/udev/rules.d# cat 10-usb-storage.rules
ACTION=="add", KERNEL=="sd?[0-9]", SUBSYSTEM=="block", RUN+="usb_automount"
ACTION=="remove", KERNEL=="sd?[0-9]", SUBSYSTEM=="block", RUN+="usb_automount"
root@darkstar:/lib/udev/rules.d#

and this is the helper script called:

root@darkstar:/lib/udev/rules.d# cat /lib/udev/usb_automount
#!/bin/bash 
#this will automatically mount and umount th RPi Pico on /mnt/pico
Name=$(basename $0)
Logger="/usr/bin/logger -p local3.info -t $Name "
#logger -p local3.info -t aoutomount -- testing
Message="$* $DEVNAME $ACTION $ID_FS_LABEL"
$Logger <<< $Message

pico_add ()
{ Message="automounting  $DEVNAME $ID_FS_LABEL"
  $Logger <<< $Message
  /sbin/mount $DEVNAME /mnt/pico && $Logger <<< "mounted" || $Logger <<< "failed"
}

pico_remove ()
{ Message="umounting  $DEVNAME $ID_FS_LABEL"
  $Logger <<< $Message
  /sbin/umount -f /mnt/pico && $Logger <<< "umounted" || $LOGGER "failed"
}

case $ID_FS_LABEL in
  RPI-RP2) pico_$ACTION ;;
  *) $Logger <<< "$ID_FS_LABEL is not configured for any automatic action"
esac
root@darkstar:/lib/udev/rules.d#

To reload the udev rules: https://unix.stackexchange.com/questions/39370/how-to-reload-udev-rules-without-reboot/39371

udevadm control --reload-rules && udevadm trigger

BTW: udev seems to ignore the content of /etc/fstab

Related