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 else logger -t SDCardscripts '/media/sdcard does not exist creating a new one' fi mkdir -p /media/sdcard if [ $? -eq 0 ];then logger -t SDCardscripts '/media/sdcard directory successfully created'; else logger -t SDCardscripts 'Failed to create /media/sdcard directory' exit 1; fi 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: Process '/root/ServolineScripts/PrepareSDCard.sh' failed with exit code 1. Nov 24 14:06:35 undefined systemd-udevd: 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
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...