首页 » 嵌入式笔记 » 正文

U盘自动挂载并升级应用程序

自动挂载U盘

现在Kernel中一般都有udev mdev.在U盘接入后会自动升成节点/dev/sd*[0,9] ,不过在udev配置文件中并没有挂载到目录,仅生成了设备节点. 这时可以自已写个脚本来实现

#!/bin/sh
if [ ! -f "/dev/sda1" ]; then
  mount /dev/sda1 /udisk -t vfat
fi
#按理说修改udev的配置文件才是最好的选择.在这里只挂载了指定设备,并只支持fat32

自用的开机检测脚本

#!/bin/sh
liveFile=liveRtspServer
decoderFile=decoder
filepath=/root/
updatapath=/udisk/

configFile=/root/config.json
# 判断文件是否存在
checkFile(){
    if [ -e $1 ]; then
        echo "$1 ready"
        return 0
    else 
        echo "$1 not ready"
        return 1
    fi
}
# 判断两个文件是否相同
diffFile(){ 
    diff $1 $2 > /dev/null
    if [ $? == 0 ]; then
        echo "Both file are same"
        return 1
    else
        echo "Both file are different"
        return 0
    fi
}

# 检测升级文件
audoUpdata(){
    # 检测U盘里的升级文件
    updataFile="${updatapath}${liveFile}"   
    checkFile $updataFile
    fileReady1=$?
    if [ 1 == $fileReady1 ]; then
        updataFile="${updatapath}${decoderFile}"
        checkFile $updataFile
        fileReady1=$?
        if [ ! 0 == $fileReady1 ]; then
            # echo "not find updata file exit.."
            return 0
        else
            localFile="${filepath}${decoderFile}"
        fi
    else
        localFile="${filepath}${liveFile}"
    fi  

    diffFile $updataFile $localFile
    if [ 0 == $? ]; then
        echo "upgrade file..."
        rm "${filepath}${decoderFile}"
        rm "${filepath}${liveFile}"
        cp $updataFile $filepath
    fi  
}

#读取json变量值 $1 文件名  $2变量名
getJsonval(){
    str=$(grep \"$2\" $1)  #找到变量所在的行 "xx": "ss:x,s",
    str=${str#*:}    #裁减掉第一个冒号前所有内容 "ss:x,s",
    str=${str#*\"}    #裁减掉第一个双引号前所有内容 ss:x,s",
    str=${str%,}     #如果最后一个逗号,则删除 ss:x,s"
    str=${str%\"*}    #裁减最后一个双引号后所有内容 ss:x,s
    echo $str
}
# 检查IP合法
checkip(){
    IP=$1
    VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
        if [ ${VALID_CHECK:-no} == "yes" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

################# 加载海思驱动MPP文件 ################
export LD_LIBRARY_PATH="/usr/local/lib:/usr/lib:/root/lib"
echo ======================= LOAD HISICON MPP ======================
cd /root/ko/
./load3520dv300 -i
cd /root/

################# 挂载U盘并检查升级文件 ################
echo ======================= CHECK U DISK ======================
if [ -e "/dev/sda1" ]; then
  mount /dev/sda1 /udisk -t vfat
  audoUpdata
fi
################# 检查IP MAC等配置 ###################
echo ======================= CHECK IP CONFIG ======================
dhcp=$(getJsonval $configFile ipmode)

if [ "true" == $dhcp ];then
    echo config eth0 to dhcp mode!!
    udhcpc -i eth0
else
    ip=$(getJsonval $configFile ip)
    mac=$(getJsonval $configFile mac)
    gateway=$(getJsonval $configFile gateway)
    checkip $ip
    if [ 1 == $? ];then
        echo IP address error! reset to 192.168.10.168
        ip="192.168.10.168"
    fi

    checkip $gateway
    if [ 1 == $? ];then
        echo gateway address error! reset to 192.168.10.1
        gateway="192.168.10.1"
    fi

    macok=$(echo $mac | grep "[A-Fa-f0-9][A-Fa-f0-9]:[A-Fa-f0-9][A-Fa-f0-9]:[A-Fa-f0-9][A-Fa-f0-9]:[A-Fa-f0-9][A-Fa-f0-9]:[A-Fa-f0-9][A-Fa-f0-9]:[A-Fa-f0-9][A-Fa-f0-9]")
    if [ ! $macok ]; then
        echo mac error! set to default!
        mac="52:54:00:69:ac:fd"
    fi
    echo configure eth0 ...
    ifconfig eth0 hw ether ${mac};
    ifconfig eth0 $ip netmask 255.255.255.0;
    route add default gw $gateway
fi
telnetd &

################# 启动程序 ###################
echo ======================= START APP ======================
checkFile ${filepath}${decoderFile}
if [ 0 == $? ]; then
    nohup ${filepath}${decoderFile} >/dev/null &
else
    nohup ${filepath}${liveFile} >/dev/null &
fi
echo ======================= DONE ======================

发表评论