3

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[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...

Jelek21
  • 66
  • 7
  • 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
  • Well, this is for the little return problem (obviously a return don't work if it is not in a function) i'll look for the exit. but my problem is that the script fails at creating the directory when it is executed by Udev but succeed when it is executed by hand... i do not really understand why but when i plug the SDcard, the script terminate by a fail with exit code 1. – Jelek21 Nov 24 '17 at 14:37
  • @roaima No, but this directory is used by others scripts and those others scripts are able to create files in /media/sdcard/. I wrote this test to clear this folder in order to avoid problems when Udev mount my sdcard. – Jelek21 Nov 24 '17 at 14:41
  • @roaima And my Beagle is in a bigger system and is stopped brutally (just by cutting the power because it is a very high voltage system) so an SDcard could have been mounted before the system stopped, and removed while the system was down... in this case the directory still here and will probably be corrupted... that's why i need to delete it before recreating a fresh one. (I hope i'm clear...) – Jelek21 Nov 24 '17 at 14:44
  • @JeromeLEKIEFFRE as long as you understand and are happy with the potential side-effects that's absolutely fine :-) – roaima Nov 24 '17 at 14:51
  • well, that's because of other side-effects that i do that ^^ But you are absolutely right! for a normal system that is adding a large possibility of errors! :) (My system is just not normal ^^ nothing like a little computer to make NAS ^^) – Jelek21 Nov 24 '17 at 14:59

1 Answers1

2

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.

Example

# 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
n
p
1


w
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
roaima
  • 107,089
  • 14
  • 139
  • 261
  • 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](https://pastebin.com/pfGz5Gna) is a pastebin where i uploaded the lines . – Jelek21 Nov 27 '17 at 07: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 08:14
  • @Jerome I've just reread your Q. You say you run it with an explicit `bash -xe`. If you do that the top line #! is totally ignored; that line is only used when you treat your script as a program and just run it. If you're using `bash -xe /path/to/script` then that is where you need to remove the -e flag. – roaima Nov 28 '17 at 08:46
  • Well, I test the script by unplugging an SDScard and replugging it. It is then Udev that execute my script. Because i already know that the script works executed by hand... My problem is only there when it is Udev that execute the script... – Jelek21 Nov 28 '17 at 11:03