rootfsimg.sh 4.3 KB
Newer Older
1 2
#!/bin/bash
#
3 4
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
#    conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
#    of conditions and the following disclaimer in the documentation and/or other materials
#    provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
#    to endorse or promote products derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -e

system=$(uname -s)
ROOTFS_DIR=$1
FSTYPE=$2
36
ROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img"
37 38
JFFS2_TOOL=mkfs.jffs2 #linux 下 制作 jffs2镜像文件的工具
WIN_JFFS2_TOOL=mkfs.jffs2.exe #windows 下 制作 jffs2镜像文件的工具
39 40
YAFFS2_TOOL=mkyaffs2image100
VFAT_TOOL=mkfs.vfat
41
ROMFS_TOOL=genromfs
42
MCOPY_TOOL=mcopy
43

44 45 46 47 48 49 50 51 52
tool_check() {
local ret='0'
command -v "$1" >/dev/null 2>&1 || { local ret='1'; }
if [ "$ret" -ne 0 ]; then
    echo "$1 tool is not exit, please install it" >&2
fi
return 0
}

53
chmod -R 755 ${ROOTFS_DIR}  
54
if [ -f "${ROOTFS_DIR}/bin/init" ]; then
55
    chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null #标准错误输出重定向到空设备文件
56 57
fi
if [ -f "${ROOTFS_DIR}/bin/shell" ]; then
58 59
    chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
fi
60
# http://manpages.ubuntu.com/manpages/trusty/man1/mkfs.jffs2.1.html
61 62
if [ "${FSTYPE}" = "jffs2" ]; then
    if [ "${system}" != "Linux" ] ; then
63
        tool_check ${WIN_JFFS2_TOOL}
64
        ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
65
    else #Linux + jffs2 执行这里
66
        tool_check ${JFFS2_TOOL}
67 68
        ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
    fi
69
elif [ "${FSTYPE}" = "yaffs2" ]; then
70 71 72 73 74
    tool_check ${YAFFS2_TOOL}
    ${YAFFS2_TOOL}  ${ROOTFS_DIR} ${ROOTFS_IMG} 2k 24bit
elif [ "${FSTYPE}" = "romfs" ]; then
    tool_check ${ROMFS_TOOL}
    ${ROMFS_TOOL}  -d ${ROOTFS_DIR} -f ${ROOTFS_IMG}
75 76
elif [ "${FSTYPE}" = "vfat" ]; then
    if [ "${system}" != "Linux" ] ; then
77
        echo "Unsupported fs type!" >&2
78
    else
79 80
        tool_check ${VFAT_TOOL}
        tool_check ${MCOPY_TOOL}
81 82 83 84 85 86 87 88 89 90
        BLK_SIZE=512
        CLT_SIZE=2048
        FAT_TAB_NUM=2
        CLT_CNT=$(( ${CLT_SIZE} / ${BLK_SIZE} ))
        if [ $# -eq 3 ]; then
            IMG_SIZE=$3
        else
            FAT32_ITEM_SIZE=4
            RESV_CNT=38
            IMG_MIN_SIZE=1048576
91 92 93
            DU_DIR_SIZE=$(( $(echo $(du -s ${ROOTFS_DIR} | awk '{print $1}')) * 1024 ))
            DIR_NUM=$(( $(echo $(ls -lR ${ROOTFS_DIR} | grep "^d" | wc -l | awk '{print $1}')) + 1 ))
            DIR_SIZE=$(( ${DU_DIR_SIZE} + ${DIR_NUM} * 4096 ))
94 95 96 97 98 99 100 101
            IMG_SIZE=$(( ${DIR_SIZE} / (1 - ${FAT_TAB_NUM} * ${FAT32_ITEM_SIZE} / ${CLT_SIZE}) + ${RESV_CNT} * ${BLK_SIZE}))
            if [ ${IMG_SIZE} -le ${IMG_MIN_SIZE} ]; then
                IMG_SIZE=${IMG_MIN_SIZE}
            fi
        fi
        IMG_CNT=$(( (${IMG_SIZE} + ${BLK_SIZE} - 1) / ${BLK_SIZE} ))
        echo mtools_skip_check=1 >> ~/.mtoolsrc
        dd if=/dev/zero of=${ROOTFS_IMG} count=${IMG_CNT} bs=${BLK_SIZE}
102 103
        ${VFAT_TOOL} ${ROOTFS_IMG} -s ${CLT_CNT} -f ${FAT_TAB_NUM} -S ${BLK_SIZE} > /dev/null
        ${MCOPY_TOOL} -i ${ROOTFS_IMG} ${ROOTFS_DIR}/* -/ ::/
104 105
    fi
else
106
    echo "Unsupported fs type!" >&2
107
fi