title: Auto mount usb drive from udev rules and shell script
body: <p>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.</p>
  
  <p>To do it I write a udev rule in /etc/udev/rules.d/10-usb-detect.rules :</p>
  
  <pre><code>ACTION=="add", KERNEL=="sd?[0-9]", SUBSYSTEM=="block", RUN+="/usr/local/bin/add.sh"
  </code></pre>
  
  <p>The script is called everytime an add event append on the block subsystem.</p>
  
  <p>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.</p>
  
  <p>Here is my script :</p>
  
  <pre><code>#!/bin/bash
  
  LOG_FILE=&lt;path_to_file&gt;
  
  echo "New usb device detected at $DEVNAME" &gt;&gt; $LOG_FILE
  
  echo "mount $DEVNAME /media/usb/test" &gt;&gt; $LOG_FILE
  
  mount $DEVNAME /media/usb/test &amp;&gt;&gt; $LOG_FILE
  
  ret=$?
  echo "$ret" &gt;&gt; $LOG_FILE
  
  if [ $ret == "0" ]; then
      echo "$DEVNAME mounted at /media/usb/test"  &gt;&gt; $LOG_FILE
  else
      echo "Failed to mount $DEVNAME at /media/usb/test"  &gt;&gt; $LOG_FILE
  fi
  
  echo "" &gt;&gt; $LOG_FILE
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is an output of the log file :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>/dev/sdc1 is not mounted even if mount returns 0.</p>
  
  <p>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.</p>
  
  <p>Does someone has a clue about how I could debug this? </p>
  
  <p>I think the problem is because the script is called from udev because if I call it from command line it also works.</p>