程序员人生 网站导航

[未完成]优酷路由宝的一些脚本分析

栏目:综合技术时间:2015-04-15 08:44:38

首先,分析的是/etc/youku/rc.main这个文件,它负责优酷相干服务的初始化、启动和结束。

这个脚本1开始先export YoukuHome=”/tmp/youku”,设置1个路径。

接下来是几个无关紧要的函数

ylog() { #format: /etc/youku/rc.main ylog "logstring" [ -n "$@" ] && echo `date +%Y-%m-%d-%H-%M-%m`" $@" | tee -ai $YoukuHome/log/boot.log } #add by David _log() { _DEBUG=0; if [ "${_DEBUG}" -eq 1 ]; then echo $1 >> $YoukuHome/log/rc.log fi } help() { cat <<EOF Syntax: $action Available commands: start Start all youku services stop Stop all youku services ikuacc start|stop tf mount|umount dectect TF storage card usb mount|umount dectect USB storage disk help EOF }

start函数

在start函数中调用了6个函数履行不同的任务。bootup目录创建等1些准备工作,tf挂载tf卡,usb挂载u盘,setup设置,checkYoukuSN检查sn,还有versionupdate和youku_luci接下来会讲。

start() { ylog "rc.main start ..." bootup tf mount usb mount setup checkYoukuSN versionupdate # start youkucmc with procd. # ikuacc start youku_luci ylog "rc.main start finished" return 0 }

bootup函数

bootup() { [ -d "$YoukuHome" ] && return 0; #这段代码先测试了$YoukuHome是不是存在,存在就不用再履行下去了,下面是不存在$YoukuHome的情况。 # ensure youku directory structure and env var. #use /tmp/youku/* to point the latest youku app, web, and script. # use system tmpfs or ourselves. the later is easy to evaluation memory used. mkdir -p $YoukuHome #建立$YoukuHome目录 mount -t tmpfs tmpfs $YoukuHome -o size=4m #mkdir -p $YoukuHome/log echo `date +%Y-%m-%d-%H-%M-%S`" rc.main bootup ..." > $YoukuHome/log/boot.log mkdir -p $YoukuHome/mnt #used for tf storage card and usb storage mkdir -p $YoukuHome/sys mkdir -p $YoukuHome/stat mkdir -p $YoukuHome/cfg mkdir -p $YoukuHome/bin mkdir -p $YoukuHome/pub #used for link shared resources which can be seen out of router. mkdir -p $YoukuHome/ver #used for save version info of firmware and packages. mkdir -p $YoukuHome/data #used for storage big size of data. visit if from $YoukuHome/var mount -t tmpfs tmpfs $YoukuHome/data -o size=4m #give 4m as default, ln -sfn $YoukuHome/data $YoukuHome/pkg #redirect $YoukuHome/pkg to tf or usb storage if possible. #ln -sfn $YoukuHome/data $YoukuHome/var #redirect $YoukuHome/var to tf or usb storage if possible. ln -sfn $YoukuHome/data $YoukuHome/tmp #redirect $YoukuHome/tmp to tf or usb storage if possible. ln -sfn $YoukuHome/data $YoukuHome/log mkdir -p $YoukuHome/var mkdir -p $YoukuHome/var/ikucmc mkdir -p $YoukuHome/var/ikuacc #mkdir -p $YoukuHome/var/ikuacc/meta #used for ikuacc meta mkdir -p $YoukuHome/var/ikuacc/data #used for ikuacc data, youkudisk. #FIXME just for debug. yang,2014-09-05,let it to be seen in web, remove it when publish #ylog "rc.main bootup ln -sfn / $YoukuHome/pub/root" #ln -sfn / $YoukuHome/pub/root ylog "rc.main bootup finished." return 0 }

第11行把tmpfs挂载到$YoukuHome。第17~26行建立了1些目录,
第27行又在$YoukuHome/data挂载tmpfs,第29~32行建立了1些软连接,第34~38行建立了ikuacc的1些目录。

挂载和卸载tf卡

tf() { ylog "rc.main tf ....[$@]" index=0; ################################################### #partitions,add by David partition_sd RetTmp=$? if [ "$RetTmp" -eq 1 ]; then { _log "RetTmp=$RetTmp, return" return } fi #do mount operation if [ "-$1" = "-mount" ]; then { [ `find /dev/ -type b -name mmcblk* | wc -l` -gt 0 ] || { ylog "rc.main tf: no tf device found, exit 1" return 1 } for devfile in `ls /dev/ | grep mmcblk | grep p`; do { _log "mount start" mkdir $YoukuHome/mnt/tf$index mount /dev/$devfile $YoukuHome/mnt/tf$index -o errors=continue RETVAL=$? if [ $RETVAL = 0 ]; then ylog "rc.main tf mounted: /dev/$devfile ==> $YoukuHome/mnt/tf$index" index=$(($index+1)) #prepare to mount next else ylog "rc.main tf mounted failure: /dev/$devfile ==> $YoukuHome/mnt/tf$index" rmdir $YoukuHome/mnt/tf$index fi } done # last=$(($index-1)) for i in `seq 0 $last`; do storage_mounted "tf" "$YoukuHome/mnt/tf$i" "$index" "$i" done } fi #################################################### #do unmount operation if [ "-$1" = "-umount" ]; then { for tfdir in `ls $YoukuHome/mnt/ | grep tf`; do { umount $YoukuHome/mnt/$tfdir rmdir $YoukuHome/mnt/$tfdir rm $YoukuHome/pub/$tfdir index=$(($index+1)) ylog "rc.main tf umounted ==> $YoukuHome/mnt/$tfdir" } done } fi #setup bin links again if something changed. [ $index -gt 0 ] && setup return 0 }

第8~15行,调用了partition_sd,接下来会讲,检查partition_sd的返回值,没甚么实际作用。
如果参数是mount,履行第17~49行挂载sd;如果参数是umount,履行第53~66行卸载sd卡。
第21~24行,看看装备中有无块装备叫做mmcblk*的,这类不出意外就是sd卡了,否则就返回1。
第26~39就是枚举并挂载sd卡了。其中,第29行创建挂载的目录,第30行挂载sd卡到该目录。
第43~46行调用storage_mounted,这个函数里面创建了1些软连接,后面会讲。
第53~66行就是umount部份,卸载并删除挂载目录和软连接。特别注意还有个第69行,履行setup,后面会讲。

挂载和卸载USB存储器

usb() { ylog "rc.main usb ....[$@]" index=0; ################################################### #do mount operation if [ "-$1" = "-mount" ]; then { [ `find /dev/ -type b -name sd* | wc -l` -gt 0 ] || { ylog "rc.main tf: no usb storage device found, exit 1" return 1 } for devfile in `ls /dev/ | grep -e sda -e sdb -e sdc`; do { mkdir $YoukuHome/mnt/usb$index mount /dev/$devfile $YoukuHome/mnt/usb$index RETVAL=$? if [ $RETVAL = 0 ]; then ylog "rc.main usb mounted: /dev/$devfile ==> $YoukuHome/mnt/usb$index" index=$(($index+1)) #prepare to mount next else ylog "rc.main usb mounted failure: /dev/$devfile ==> $YoukuHome/mnt/usb$index" rmdir $YoukuHome/mnt/usb$index fi } done # last=$(($index-1)) for i in `seq 0 $last`; do storage_mounted "usb" "$YoukuHome/mnt/usb$i" "$index" "$i" done } fi #################################################### #do unmount operation if [ "-$1" = "-umount" ]; then { for usbdir in `ls $YoukuHome/mnt/ | grep usb`; do { umount $YoukuHome/mnt/$usbdir rmdir $YoukuHome/mnt/$usbdir rm $YoukuHome/pub/$usbdir index=$(($index+1)) ylog "rc.main usb umounted ==> $YoukuHome/mnt/$usbdir" } done } fi #setup bin links again if something changed. [ $index -gt 0 ] && setup return 0; }

进程跟挂载和卸载tf卡差不多。

setup函数

setup() { ylog "rc.main setup ..." #set default .or when some disk umounted [ -L $YoukuHome/bin/youkucmc -a -f "`readlink -f $YoukuHome/bin/youkucmc`" ] || ln -sfn /usr/sbin/guard $YoukuHome/bin/youkucmc [ -L $YoukuHome//bin/ikuacc -a -f "`readlink -f $YoukuHome/bin/ikuacc`" ] || ln -sfn /usr/sbin/ikuacc $YoukuHome/bin/ikuacc [ -L $YoukuHome/bin/sn_youku -a -f "`readlink -f $YoukuHome/bin/sn_youku`" ] || ln -sfn /usr/sbin/sn_youku $YoukuHome/bin/sn_youku [ -L $YoukuHome/www -a -d "`readlink -f $YoukuHome/www`" ] || ln -sfn /www/ $YoukuHome/www #let publish resouces can be seen from web [ -L $YoukuHome/www/pub -o -d $YoukuHome/www/pub ] || ln -sfn $YoukuHome/pub $YoukuHome/www/pub ylog "rc.main setup: cur www path=>[`readlink -f $YoukuHome/www`]" ##################################################################### #select space for save data,suck as ikuacc virtual disk and so on. #[ -L $YoukuHome/var -a -d "`readlink -f $YoukuHome/var`" ] || ln -sfn $YoukuHome/data $YoukuHome/var ylog "rc.main setup: cur var path=>[`readlink -f $YoukuHome/var`]" flag=0 minsize=0 maxsize=0 mindisk=0 maxdisk=0 cd $YoukuHome/mnt #get smallest and biggest start partition for p in `ls $YoukuHome/mnt | grep -e tf -e usb | sort`; do { newsize=`df -m | grep $YoukuHome/mnt/$p | awk '{print $2 }'` [ -z $newsize ] && newsize=0 [ ${flag} -eq 0 ] && { flag=1 minsize=${newsize} maxsize=${newsize} mindisk=${YoukuHome}/mnt/$p maxdisk=${YoukuHome}/mnt/$p } [ ${newsize} -lt ${minsize} ] && { mindisk=${YoukuHome}/mnt/$p minsize=${newsize} } [ ${newsize} -gt ${maxsize} ] && { maxdisk=${YoukuHome}/mnt/$p maxsize=${newsize} } }; done _log "minsize=${minsize}, mindisk=${mindisk}" _log "maxsize=${maxsize}, maxdisk=${maxdisk}" [ ${minsize} -gt 100 ] && { if [ "${mindisk}/meta" != "`readlink -f ${YoukuHome}/var/ikuacc/meta`" ]; then { [ -d ${YoukuHome}/var/ikuacc/meta ] && cp -rfp ${YoukuHome}/var/ikuacc/meta/* ${mindisk}/youku/var/ikuacc/meta #only copy meta to new space. mkdir -p ${mindisk}/meta #mkdir -p $YoukuHome/var/ikuacc/meta ln -sfn ${mindisk}/meta ${YoukuHome}/var/ikuacc/meta } fi if [ "`readlink -f $YoukuHome/pkg`" != "${mindisk}/youku/bin" ]; then { mkdir -p ${mindisk}/youku/bin ln -sfn ${mindisk}/youku/bin $YoukuHome/pkg mkdir -p ${mindisk}/youku/tmp ln -sfn ${mindisk}/youku/tmp $YoukuHome/tmp mkdir -p ${mindisk}/youku/log ln -sfn ${mindisk}/youku/log $YoukuHome/log ylog "rc.main setup: set pkg path=>[size=$newsize Mb,${mindisk}/youku/bin" ylog "rc.main setup: set tmp path=>[size=$newsize Mb,${mindisk}/youku/tmp" } fi } #mount data0~data3 cnt=0 for p in `ls $YoukuHome/mnt | grep -e tf | sort`; do { partsize=`df -m | grep $YoukuHome/mnt/$p | awk '{print $2 }'` [ ${partsize} -gt 500 ] && { _log "mount data, partsize=${partsize}" #mkdir -p $YoukuHome/var/ikuacc/data/data${cnt} ln -sfn $YoukuHome/mnt/$p $YoukuHome/var/ikuacc/data/data${cnt} cnt=$((${cnt}+1)) } }; done #### add by xiongying for 1.5 branch patch [ -f /usr/sbin/patch.sh ] && /bin/sh /usr/sbin/patch.sh ##################################################################### #go to default work directory cd $YoukuHome/tmp ylog "rc.main setup finished." return 1 }
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐