我正在编写一个由 Udev 执行的脚本,用于在 Debian Jessie 下的我的 Beaglebone Black 上插入新的 SD 卡。
我希望我的脚本做的第一件事是创建/media/sdcard
目录。这应该很容易,但由于某些不明原因它不起作用......
这是我写的脚本:
#!/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'
以下是我插入 SD 卡时生成的日志行:
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.
所以我的理解是:
- Udev 在我的脚本中输入 OK
- Udev 成功检查文件是否存在
- Udev 执行 mkdir 失败
执行此脚本后,我尝试:
root@undefined:~/ServolineScripts# cd /media
root@undefined:/media# ls -l
total 0
没有意见...
最后,我尝试删除行来检查 /media/sdcard 是否已存在(所有第一个 if 和 elif),在这种情况下,进程在 mkdir 执行时失败。
这种症状是在我插入 µSdcard 时出现的,但如果我通过键入来运行脚本bash -xe /root/ServolineScripts/PrepareSDCard
,则会得到以下结果:
+ 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'
(显然它成功执行了 mkdir 并且 /media/sdcard/ 已创建......)
* 如果您对问题出在哪里有任何想法...或者任何可以帮助我调试这个问题的想法...*
(我已经 4 天了……我什至尝试用新图像恢复 Beagle……:-( )
PS:我已经看到我写的不同的返回不是一个好的命令...如果你知道如何在返回值时打破脚本,我会接受!但这是第二个主题......
答案1
由于您已启用,您的脚本一旦收到错误就会退出bash -e
。
失败的命令是rm -rf
,因为在您的脚本运行时安装了一个文件系统/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
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