2013年4月16日 星期二

ext4 filesystem VS stat command.

這篇文章我不知道要如何命名比較恰當 , 主要會使用 mkfs.ext4 和 stat 這兩個utility 所以就簡單命名了 !!

在 embedded system 中 nand flash space 是很寶貴的 , 不能太浪費 !!
需求來了, 我需要計算要放入的資料大小(包含一堆目錄)然後建立一個 剛剛好大小的 image file , 這個 image file 需要包含 ext4 filesystem ,量產時只要將這個 image file 直接燒錄到eMMC 對應的partition 中即可.

問題是.... 現在我每次都計算部準確 .... 常常 空間不足夠 , 又不能浪費一堆空間 (這個是純資料 , Device 會使用 mount -o r ..... read-only type , 所以浪費的也用不到 !! )

先說 stat 吧 !!

stat 可以將檔案 , 資料夾所需要的 block & size都列印出來 , 指令如下:

  STAT="/bin/stat"
   size=`${STAT} -c%s $1`
   block=`${STAT} -c%b $1
`


也可以利用 stat 來知道是檔案還是資料夾

${STAT} ${dir_list} | grep -q "directory"
    if [ $? != 0 ];then
##        echo "###### FileStatus ${dir_list}"
        FileStatus ${dir_list}
    else
##        echo "###### DirectStatus ${dir_list}"
        DirectStatus ${dir_list}
        if [ DIRECT_COUNT != 0 ];then
            let DIRECT_COUNT=DIRECT_COUNT-1
        fi
    fi


然後用 shell file 計算所有資料夾需要的 size 和 block count .

這邊要注意 , ext2/ext4 的 block size 有 1024/2048 & 4096 區分 .
一樣會有內部斷裂問題 (小於 block size會使用一個 block 去儲存) .

stat -c%b 回應的值是 disk 的最小block 單位 目前都是 512 bytes.
或者可以用 stat -c%B 去讀取 確定的大小值 .
我寫了一個 xstat.sh shell scrip 去計算所有資料的大小 , 如下:

example :

[jeff@instant ec910-sdk]$ ./build/xstat.sh src/
file count:145
folder count:7
file size:2055196
folder size:94208
total need block:603
[jeff@instant ec910-sdk]$



這個 shell 統計出 需要 603 個 block 也就是需要 603 * 4096 bytes.


ext4 filesystem structure:



shell script 中的 getopt & getopts

在 linux shell 中我們常常用到 傳入一些參數 . 之前我都使用 getopts 的方式, 使用上都好好的 , example file 如下(片段):


while getopts ":O:h" opt; do
        echo "opt:${opt}"
        case ${opt} in
        ## ---- output size setting.
            O )
                OUT_BLOCK_SZ=${OPTARG}
            ;;

        ## ---- help menu.
            \? | h )
                ${MKMATE} color -fred "## Bad command ${OPTARG}...."
                Usage
                return 1
            ;;

        esac

    done


今天想要輸入 一堆其它的值 例如: <$> foo.sh -O 1234 dir_1 dir_2 dir_3. or
<$> foo.sh dir_1 dir_2 dir_3 ..... -O 1234
使用之前就的方式卻出問題, 這些 dir 訊息沒有辦法取出(maybe I got some mistake in getopts ),所以就換成使用 getopt 了, example 如下:


    args=`getopt O:h $@ `
    if [ $? != 0 ];then
        ${MKMATE} color -fred "## Bad command ${OPTARG}...."
        return 1
    fi

    set -- ${args}

    while [ -n "$1" ]
    do
        case $1 in

        ## ---- output size setting.
            -O )
                shift
                OUT_BLOCK_SZ=$1
            ;;

            -h | - )
                Usage
                return 1
            ;;

            \? )
                ${MKMATE} color -fred "## Bad command ${OPTARG}...."
                Usage
                return 1
            ;;

            -- )
                DIR_LIST=
            ;;

            * )
                if [ -z ${DIR_LIST} ];then
                    DIR_LIST="$1"
                else
                    DIR_LIST=${DIR_LIST}:$1
                fi

            ;;
        esac
        shift
    done


不管輸入方式是 <$>foo.sh -O 1234 dir_1 ..... or <$>foo.sh dir_1 ..... -O 1234
最後 srgs 都會 -O 1234 -- dir_1 dir_2 .....
接著用 set -- ${args} 就可以重新設定 $@ 參數 .
在利用 while 和 shift 就可以逐一取出想要的參數 , 利用 -- 當區隔就可以將 dir_1 dir_2 取入 .

我利用 : 將這些 dir 資料並起來, 有利於我的處理需求 !!



Reference :

http://changyy.pixnet.net/blog/post/25660234-%5Bbash%5D-%E5%B0%87%E6%AA%94%E6%A1%88%E5%85%A7%E7%9A%84%E5%AD%97%E4%B8%B2%E9%80%B2%E8%A1%8C%E5%8F%96%E4%BB%A3