I'm working on a script executed by Udev on insertion of a new sdcard on my Beaglebone Black under Debian Jessie.

The first thing I would like my script to do is to create the /media/sdcard directory. It should be easy, but for some obscure reason it is not working...

Here is the script I wrote:

#!/bin/bash -xe
#This script creates the /media/sdcard folder in order to mount the sdcard in it
#Error codes:
# - 2 : /media/sdcard already exist but is not a directory
# - 1 : Failed to create /media/sdcard/

logger -t SDCardscripts 'Entering /root/ServolineScripts/PrepareSDCard.sh script'

if [ -d /media/sdcard ]; then
logger '/media/sdcard is already present. Deleting it.' && rm -rf /media/sdcard;
elif [ -f /media/sdcard ]; then
logger -t SDCardscripts 'A file /media/sdcard already exist! Stopping process, Please look in the /media directory and take a decision with it content'
exit 2
logger -t SDCardscripts '/media/sdcard does not exist creating a new one'

mkdir -p /media/sdcard

if [ $? -eq 0 ];then
logger -t SDCardscripts '/media/sdcard directory successfully created';
logger -t SDCardscripts 'Failed to create /media/sdcard directory'
exit 1;

logger -t SDCardscripts 'Leaving /root/ServolineScripts/PrepareSDCard.sh script'

Here are the logg lines generated when i plug in an SD card :

Nov 24 14:06:34 undefined kernel: [ 5546.379851] mmc0: host does not support reading read-only switch, assuming write-enable
Nov 24 14:06:34 undefined kernel: [ 5546.383558] mmc0: new high speed SDHC card at address 0007
Nov 24 14:06:34 undefined kernel: [ 5546.401181] mmcblk0: mmc0:0007 SD08G 7.42 GiB 
Nov 24 14:06:34 undefined rsyslogd-2007: action 'action 17' suspended, next retry is Fri Nov 24 14:07:04 2017 [try http://www.rsyslog.com/e/2007 ]
Nov 24 14:06:34 undefined kernel: [ 5546.413258]  mmcblk0: p1
Nov 24 14:06:34 undefined SDCardscripts: Entering /root/ServolineScripts/PrepareSDCard.sh script
Nov 24 14:06:35 undefined logger: /media/sdcard is already present. Deleting it.
Nov 24 14:06:35 undefined systemd-udevd[2003]: Process '/root/ServolineScripts/PrepareSDCard.sh' failed with exit code 1.
Nov 24 14:06:35 undefined systemd-udevd[2003]: Process '/bin/mount /dev/%K /media/sdcard' failed with exit code 32.

So what i understand is:

  • Udev enter in my script OK
  • Udev succesfully checked if a file exist
  • Udev fails at executing mkdir

After executing this script, i tried :

root@undefined:~/ServolineScripts# cd /media
root@undefined:/media# ls -l
total 0

No comment...

Finally, i tried to delete the lines to check if /media/sdcard already exist (all the first if and elif) and in this case, the process fails on the mkdir execution.

This symptoms are when i plugin the µSdcard, but if i run the script by typing bash -xe /root/ServolineScripts/PrepareSDCard, here is what i got:

+ logger -t SDCardscripts 'Entering /root/ServolineScripts/PrepareSDCard.sh script'
+ '[' -d /media/sdcard ']'
+ logger '/media/sdcard is already present. Deleting it.'
+ rm -rf /media/sdcard
+ mkdir -p /media/sdcard
+ '[' 0 -eq 0 ']'
+ logger -t SDCardscripts '/media/sdcard directory successfully created'
+ logger -t SDCardscripts 'Leaving /root/ServolineScripts/PrepareSDCard.sh script'

(And obviously it succeed at executing mkdir and the /media/sdcard/ has been created...)

* If you have any ideas on where the problem is... or anything that could help me debug this...*

(it's been 4 days i'm on it... i even tried to restore the Beagle with a new image... :-( )

P.S.: I already saw that the different return I wrote are not the good command... if you know how to break a script while returning a value i take it! but this is a second subject...

  • I don't know if it's your actual issue, but you need a semicolon (or line break) before the then in elif [ -f /media/sdcard ] then – steeldriver Nov 24 '17 at 13:57
  • You're right! hum... ok i made the corrections – Jelek21 Nov 24 '17 at 14:05
  • The subject is corrected! @steeldriver you where right on this, the first part of the problem is solved! thanks buuuuuuut... the biggest problem is still here... – Jelek21 Nov 24 '17 at 14:14
  • I still don't really understand what you're asking - but I suspect you need to exit rather than return – steeldriver Nov 24 '17 at 14:23
  • rm -rf /media/sdcard and mkdir -p /media/sdcard seems a strange combination. Can you ever have the situation where something else is mounted on /media/sdcard when you run this script? – roaima Nov 24 '17 at 14:33

Your script exits as soon as it receives an error because you have bash -e enabled.

The failing command is rm -rf, because at the point your script is running there is a filesystem mounted on /media/sdcard.


# Prepare disk as image
dd if=/dev/zero bs=1M count=100 >100M.img
lo=$(losetup --find --show 100M.img | tee /dev/stderr)
fdisk $lo <<X

losetup -d $lo
lo=$(losetup --find --show --partscan 100M.img | tee /dev/stderr)

# Create and mount new filesystem
mkfs -t ext4 ${lo}p1
mkdir -p /mnt/dsk
mount ${lo}p1 /mnt/dsk

# Demonstrate trying to remove the directory tree (and failing)
rm -rf /mnt/dsk
echo SS=$?

# All done
umount /mnt/dsk
losetup -d $lo
rm 100M.img
  • OK i understand,but somthing still strange to me... in the Udev rule, Udev starts by executing the script and then try to mount the SDcard, so this directory should not have anything mounted on it... and the other thing is, even if the directory does not exist, i have the exact same problem... (the test works and it goes in the else and it sontinue failing, i suppose on mkdir because it does not create any new folder...) (am I clear ? :/ ) – Jelek21 Nov 24 '17 at 15:14
  • @JeromeLEKIEFFRE do you get anything exciting if you put mount | logger -t SDCardscripts somewhere near the top of your script? – roaima Nov 24 '17 at 16:07
  • Hi @roaima! Sorry I've been out of internet for the week-end... :/ If you still wana try to help me, mount | logger -t SDCardscripts has nothing related to any /media folder... if you wan't to hav a look on the logg, here is a pastebin where i uploaded the lines . – Jelek21 Nov 27 '17 at 7:48
  • @Jerome please remove -e flag from top of script (ie #!/bin/bash -x) and add as next line exec 1>/tmp/log.txt 2>&1 then after triggering your script, examine that log file for error messages – roaima Nov 27 '17 at 21:04
  • It don't logg anything. It does even not create /tmp/log.txt – Jelek21 Nov 28 '17 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.