脚本自动运行时,设备的blkid不会输出信息

时间:2019-03-07 08:14:08

标签: linux bash kernel udev

脚本在Qnap存储上运行。我创建了udev规则:

ACTION=="add", KERNEL=="sd*", RUN+="/bin/sh /share/MD0_DATA/Images/scripts/usb.sh /dev/%k"

我知道它目前处于脏状态,但是它可以运行。

usb.sh是:

# CONFIGURATION
DEBUG=1 # set to 1 for debugging output
DEST="/share/MD0_DATA/Images/" # destination for files
BEEP=0 # do we beep?

DEVICE="$1" # the device name
NAME="$2"
#echo "$DEVICE $NAME" >> /share/MD0_DATA/Images/scripts/test.log

#LOGFACILITY="kernel.info" # for debugging output
LOGFILE="/share/MD0_DATA/Images/scripts/action.log"
#sleep 4

# won't work currently, TODO: fix
#LABEL=$(blkid $DEVICE -s LABEL | awk -F '"' '{print $2}')
#LABEL=$(echo -e "${LABEL}" | tr -d '[:space:]')

DATE=$(date '+%Y-%m-%d-%H.%M.%S')
PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')

if [ $BEEP == 1 ]; then /sbin/hal_app --se_buzzer enc_id=0,mode=101; fi
echo "$PRINTDATE Starting USB script for $DEVICE $LABEL" >> $LOGFILE

sleep 5

mount | grep "$DEVICE"
FOO="$?"

if [ $FOO == 0 ];
then
    PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
    echo "$PRINTDATE $DEVICE is mounted, trying to unmount..." >> $LOGFILE
    umount $DEVICE
fi

# won't work
USB_NAME=$(blkid | grep -oP '(?<=LABEL=").+?(?=")')
echo "blkid $DEVICE -s LABEL -> $USB_NAME" >> /share/MD0_DATA/Images/scripts/test.log

LABEL=$(echo -e "${LABEL}" | tr -d '[:space:]')
FILE="$DEST$DATE-$LABEL.img"

PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
echo "$PRINTDATE Writing image of $DEVICE to $FILE" >> $LOGFILE
dd if=$DEVICE of=$FILE
DD_EXIT_STATUS=$?
#echo $DD_EXIT_STATUS >> $LOGFILE

# check if overall dd operation worked
if [ $DD_EXIT_STATUS gt 0 ]; 
then
    PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
    echo "$PRINTDATE ERROR: creating image for $DEVICE failed!" >> $LOGFILE
else
    PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
    echo "$PRINTDATE SUCCESS: image for $DEVICE created." >> $LOGFILE
fi

MD5_DEVICE=$(dd if=$DEVICE status=none | md5sum)
MD5_IMAGE=$(dd if=$FILE status=none | md5sum)

# check if md5sum of created image and device match
if [ $MD5_DEVICE != $MD5_IMAGE ]; 
then
    if [ $BEEP == 1 ];
    then
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
    fi
    PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
    echo "$PRINTDATE ERROR: md5 hashes of $DEVICE and $FILE don't match!" >> $LOGFILE
else
    if [ $BEEP == 1 ];
        then
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
        /sbin/hal_app --se_buzzer enc_id=0,mode=101
    fi
    PRINTDATE=$(date '+%d.%m.%Y %H:%M:%S')
    echo "$PRINTDATE SUCCESS: md5 hashes of $DEVICE and $FILE match" >> $LOGFILE
fi

我的错误是blklist不会给我任何内容。 当我手动运行脚本时,blklist会提供一个值。

问题:为什么当脚本由udev规则运行时,我没有任何blklist值?

0 个答案:

没有答案