[Ubuntu][Shell]make_ubuntu_sd.sh注释解读

#!/bin/bash
#
#   =======================================================================
#
# Copyright (C) 2018, Hisilicon Technologies Co., Ltd. All Rights Reserved.
#
# 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 names of the copyright holders nor the names of the
#   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.
#   =======================================================================

# ************************Variable*********************************************
ScriptPath="$( cd "$(dirname "$0")" ; pwd -P )""/"
DEV_NAME=$1

#********************** UPDATE TO FIX ISSUE REGARDING /dev/mmc* MOUNTED SD CARD **********

if [[ $DEV_NAME == /dev/mmc* ]]
then
    echo "Partion naming with p-prefix"    
    p1="p1"
    p2="p2"
    p3="p3"          
else
    echo "Partition naming without p-prefix"    
    p1="1"
    p2="2"
    p3="3"
fi

#####################################################################################


ISO_FILE_DIR=$2
ISO_FILE=$3
RUN_MINI=$4
NETWORK_CARD_DEFAULT_IP=$5
USB_CARD_DEFAULT_IP=$6

LogPath=${ScriptPath}"sd_card_making_log/"
TMPDIR_SD_MOUNT=${LogPath}"sd_mount_dir"
TMPDIR_SD2_MOUNT=${LogPath}"sd_mount_dir2"
TMPDIR_SD3_MOUNT=${LogPath}"sd_mount_dir3"
TMPDIR_DATE=${LogPath}"no_touch_make_sd_dir"

USER_NAME="HwHiAiUser"
USER_PWD="HwHiAiUser:\$6\$klSpdQ1K\$4Gm/7HxehX.YSum4Wf3IDFZ3v5L.clybUpGNGaw9zAh3rqzqB4mWbxvSTFcvhbjY/6.tlgHhWsbtbAVNR9TSI/:17795:0:99999:7:::"
ROOT_PWD="root:\$6\$klSpdQ1K\$4Gm/7HxehX.YSum4Wf3IDFZ3v5L.clybUpGNGaw9zAh3rqzqB4mWbxvSTFcvhbjY/6.tlgHhWsbtbAVNR9TSI/:17795:0:99999:7:::"

MINIRC_LOGROTATE_DIR="/etc/crob.minirc/"
SYSLOG_MAXSIZE="1000M"
SYSLOG_ROTATE="4"
KERNLOG_MAXSIZE="1000M"
KERNLOG_ROTATE="4"
# end

# ************************Cleanup*********************************************
# Description:  files cleanup
# ******************************************************************************
function filesClean()
{
    #卸载所有的分区
    df -h | grep "${TMPDIR_DATE}"
    if [ $? -eq 0 ];then
        umount ${TMPDIR_DATE}
    fi
    # 删除文件夹${TMPDIR_DATE}
    rm -rf ${TMPDIR_DATE}
    # 卸载${LogPath}squashfs-root/cdtmp
    df -h | grep "${LogPath}squashfs-root/cdtmp"
    if [ $? -eq 0 ];then
        umount ${LogPath}squashfs-root/cdtmp
    fi
    # 删除文件夹${LogPath}squashfs-root
    rm -rf ${LogPath}squashfs-root
    # 删除文件夹${LogPath}filesystem.squashfs
    rm -rf ${LogPath}filesystem.squashfs
    #卸载分区1
    df -h | grep "${TMPDIR_SD_MOUNT}"
    if [ $? -eq 0 ];then
        umount ${TMPDIR_SD_MOUNT}
    fi
    #删除Ubuntu分区1对应的文件夹
    rm -rf ${TMPDIR_SD_MOUNT}
    # 卸载分区2
    df -h | grep "${TMPDIR_SD2_MOUNT}"
    if [ $? -eq 0 ];then
        umount ${TMPDIR_SD2_MOUNT}
    fi
    #删除Ubuntu分区2对应的文件夹
    rm -rf ${TMPDIR_SD2_MOUNT}
    #卸载分区3
    df -h | grep "${TMPDIR_SD3_MOUNT}"
    if [ $? -eq 0 ];then
        umount ${TMPDIR_SD3_MOUNT}
    fi
    #删除Ubuntu分区3对应的文件夹
    rm -rf ${TMPDIR_SD3_MOUNT}
    #删除mini_developerkit
    rm -rf ${LogPath}mini_developerkit
}
#end
# ************************check ip****************************************
# Description:  check ip valid or not
# $1: ip
# ******************************************************************************
function checkIpAddr()
{
    ip_addr=$1
    echo ${ip_addr} | grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null
    if [ $? -ne 0 ]
    then
        return 1
    fi

    for num in `echo ${ip_addr} | sed "s/./ /g"`
    do
        if [ $num -gt 255 ] || [ $num -lt 0 ]
        then
            return 1
        fi
   done
   return 0
}

# **************check network card and usb card ip******************************
# Description:  check network card and usb card ip
# ******************************************************************************
function checkIps()
{
    # NETWORK_CARD_DEFAULT_IP如果是空的就赋值为192.168.0.2
    if [[ ${NETWORK_CARD_DEFAULT_IP}"X" == "X" ]];then
        NETWORK_CARD_DEFAULT_IP="192.168.0.2"
    fi

    checkIpAddr ${NETWORK_CARD_DEFAULT_IP}
    if [ $? -ne 0 ];then
        echo "Failed: Invalid network card ip."#提示这个错误就要自己的板子ip 网口地址
        return 1
    fi
    NETWORK_CARD_GATEWAY=`echo ${NETWORK_CARD_DEFAULT_IP} | sed -r 's/([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+/\1.1/g'`

    # USB_CARD_DEFAULT_IP是空的就赋值为192.168.1.2
    if [[ ${USB_CARD_DEFAULT_IP}"X" == "X" ]];then
        USB_CARD_DEFAULT_IP="192.168.1.2"
    fi

    checkIpAddr ${USB_CARD_DEFAULT_IP}
    if [ $? -ne 0 ];then
        echo "Failed: Invalid usb card ip." #提示这个错误就要自己的板子ip usb地址
        return 1
    fi
    return 0
}


# ************************umount SD Card****************************************
# Description:  check sd card mount, if mounted, umount it
# ******************************************************************************
function checkSDCard()
{
    #检查SD卡是否挂载,挂载了就卸载掉
    paths=`df -h | grep "$DEV_NAME" | awk -F ' ' '{print $6}'`
    for path in $paths
    do
        echo "umount $path"
        umount $path
        if [ $? -ne 0 ];then
            echo "Failed: umount $path failed!"
            return 1
        fi
    done
    return 0
}
#end

# ************************Extract ubuntufs from iso*****************************
# Description:  mount iso file , extract root filesystem from squashfs, after
# execute function it will create squashfs-root/ in "./"
# ******************************************************************************
function ubuntufsExtract()
{
    # 创建文件夹${TMPDIR_DATE}
    mkdir ${TMPDIR_DATE}
    # 镜像挂载${TMPDIR_DATE}
    mount -o loop ${ISO_FILE_DIR}/${ISO_FILE} ${TMPDIR_DATE}
    # 复制文件filesystem.squashfs到${LogPath}
    cp ${TMPDIR_DATE}/install/filesystem.squashfs ${LogPath}
    if [[ $? -ne 0 ]];then
        echo "Failed: Copy 'filesystem.squashfs' fail!"
        return 1;
    fi
    #切换目录
    cd ${LogPath}
    # 解压文件
    unsquashfs filesystem.squashfs

    if [[ $? -ne 0 ]];then
        echo "Failed: Unsquashfs 'filesystem.squashfs' fail!"
        return 1;
    fi
    # 返回到脚本路径
    #Return to the bin directory
    cd ${ScriptPath}
    return 0
}
# end


# *****************configure syslog and kernlog**************************************
# Description:  configure syslog and kernlog
# ******************************************************************************
function configure_syslog_and_kernlog()
{
    # 创建文件夹${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}
    if [ ! -d ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR} ];then
        mkdir -p ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}
    fi
    # minirc_logrotate和minirc_logrotate.conf内容清空
    echo "" > ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}minirc_logrotate
    echo "" > ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}minirc_logrotate.conf
    # 将脚本内容写入minirc_logrotate文件
    cat > ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}minirc_logrotate << EOF
#!/bin/bash

#Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "\${logfile}" ] && echo "\"\${logfile}\" \${date}"
done >> status.clean

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate ${MINIRC_LOGROTATE_DIR}minirc_logrotate.conf
EOF
# 将脚本内容写入minirc_logrotate.conf文件
    cat > ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}minirc_logrotate.conf << EOF
# see "main logrotate" for details

# use the syslog group by default, since this is the owing group
# of /var/log/syslog.
su root syslog

# create new (empty) log files after rotating old ones
create
/var/log/syslog
{
        rotate ${SYSLOG_ROTATE}
        weekly
        maxsize ${SYSLOG_MAXSIZE}
        missingok
        notifempty
        compress
        postrotate
                invoke-rc.d rsyslog rotate > /dev/null
        endscript
}
/var/log/kern.log
{
        rotate ${SYSLOG_ROTATE}
        weekly
        maxsize ${SYSLOG_MAXSIZE}
        missingok
        notifempty
        compress
}
EOF
    # 赋予权限
    chmod 755 ${LogPath}squashfs-root/${MINIRC_LOGROTATE_DIR}minirc_logrotate
    # 将代码追加到crontab文件
    echo "*/30 *   * * *   root     cd / && run-parts --report ${MINIRC_LOGROTATE_DIR}" >> ${LogPath}squashfs-root/etc/crontab
    # 替换字符串
    if [ -f ${LogPath}squashfs-root/etc/rsyslog.d/50-default.conf ];then
        sed -i 's/*.*;auth,authpriv.none/*.*;auth,authpriv,kern.none/g' ${LogPath}squashfs-root/etc/rsyslog.d/50-default.conf
    fi
    # 追加文件到对应文件
    echo 'LogLevel=emerg' >> /etc/systemd/system.conf
    echo 'MaxLevelStore=emerg' >> /etc/systemd/journald.conf
    echo 'MaxLevelSyslog=emerg' >> /etc/systemd/journald.conf
    echo 'MaxLevelKMsg=emerg' >> /etc/systemd/journald.conf
    echo 'MaxLevelConsole=emerg' >> /etc/systemd/journald.conf
    echo 'MaxLevelWall=emerg' >> /etc/systemd/journald.conf
}


# ************************Configure ubuntu**************************************
# Description:  install ssh, configure user/ip and so on
# ******************************************************************************
function configUbuntu()
{
    # 1. configure image sources
    # 创建目录${LogPath}squashfs-root/cdtmp
    mkdir -p ${LogPath}squashfs-root/cdtmp
    # 挂载目录
    mount -o bind ${TMPDIR_DATE} ${LogPath}squashfs-root/cdtmp
    #将下面脚本内容写入chroot_install.sh
    echo "
#!/bin/bash
RUN_MINI=\$1
username=\$2
password=\$3
root_pwd=\$4

# 1. apt install deb
mv /etc/apt/sources.list /etc/apt/sources.list.bak
touch /etc/apt/sources.list
echo \"deb file:/cdtmp xenial main restrict\" > /etc/apt/sources.list

locale-gen zh_CN.UTF-8 en_US.UTF-8 en_HK.UTF-8
apt-get update
apt-get install openssh-server -y
apt-get install unzip -y
apt-get install vim -y
apt-get install gcc -y
apt-get install zlib -y
apt-get install python2.7 -y
apt-get install python3 -y
apt-get install pciutils -y
apt-get install strace -y
apt-get install nfs-common -y
apt-get install sysstat -y
apt-get install libelf1 -y
apt-get install libpython2.7 -y
apt-get install libnuma1 -y
apt-get install dmidecode -y
apt-get install rsync -y

mv /etc/apt/sources.list.bak /etc/apt/sources.list

# 2. set username
useradd -m \${username} -d /home/\${username} -s /bin/bash
sed -i \"/^\${username}:/c\\\\\${password}\" /etc/shadow
sed -i \"/^root:/c\\\\\${root_pwd}\" /etc/shadow

# 3. config host
echo 'davinci-mini' > /etc/hostname
echo '127.0.0.1        localhost' > /etc/hosts
echo '127.0.1.1        davinci-mini' >> /etc/hosts

# 4. config ip
echo \"source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address ${NETWORK_CARD_DEFAULT_IP}
netmask 255.255.255.0
gateway ${NETWORK_CARD_GATEWAY}

auto usb0
iface usb0 inet static
address ${USB_CARD_DEFAULT_IP}
netmask 255.255.255.0
\" > /etc/network/interfaces

# 5. auto-run minirc_cp.sh and minirc_sys_init.sh when start ubuntu
echo \"#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will \"exit 0\" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
cd /var/

/bin/bash /var/minirc_boot.sh /opt/mini/\${RUN_MINI}

exit 0
\" > /etc/rc.local

echo \"RuntimeMaxUse=50M\" >> /etc/systemd/journald.conf
echo \"SystemMaxUse=50M\" >> /etc/systemd/journald.conf

exit
# end" > ${LogPath}squashfs-root/chroot_install.sh
    # 赋予chroot_install.sh权限
    chmod 750 ${LogPath}squashfs-root/chroot_install.sh
    # 2. add user and install software
    # execute in ./chroot_install.sh
    # 执行chroot_install.sh这个脚本
    chroot ${LogPath}squashfs-root /bin/bash -c "./chroot_install.sh ${RUN_MINI} ${USER_NAME} '"${USER_PWD}"' '"${ROOT_PWD}"'"

    if [[ $? -ne 0 ]];then
        echo "Failed: qemu is broken or the version of qemu is not compatible!"
        return 1;
    fi
    # 配置日志信息
    #configure syslog and kern log
    configure_syslog_and_kernlog
    # 卸载${LogPath}squashfs-root/cdtmp
    umount ${LogPath}squashfs-root/cdtmp
    # 删除${LogPath}squashfs-root/cdtmp
    rm -rf ${LogPath}squashfs-root/cdtmp
    # 删除脚本
    rm ${LogPath}squashfs-root/chroot_install.sh
    return 0
}
# end

# ************************Format SDcard*****************************************
# 格式化为ext3系统并分3个区
# ******************************************************************************
function formatSDcard()
{
    if [[ $(fdisk -l 2>/dev/null | grep "^${DEV_NAME}" | wc -l) -gt 1 ]];then
    for i in $(fdisk -l 2>/dev/null | grep "^${DEV_NAME}" | awk -F ' ' '{print $1}'); do
            echo "d

        w" | fdisk ${DEV_NAME}
    done
    else
    echo "d

    w" | fdisk ${DEV_NAME}
    fi
    umount ${DEV_NAME} 2>/dev/null

    echo "n

+5G
n

+1G
n


    w" | fdisk ${DEV_NAME}

    partprobe

    fdisk -l

    sleep 5

    checkSDCard
    if [ $? -ne 0 ];then
        return 1
    fi
    echo "y
    " | mkfs.ext3 -L ubuntu_fs ${DEV_NAME}$p1 # updated by aman
    if [[ $? -ne 0 ]];then
     echo "Failed: Format SDcard1 failed!"
     return 1;
    fi
    # 检查SD卡
    checkSDCard
    if [ $? -ne 0 ];then
        return 1
    fi
    echo "y
    " | mkfs.ext3 -L ubuntu_fs ${DEV_NAME}$p2 # updated by aman
    if [[ $? -ne 0 ]];then
     echo "Failed: Format SDcard2 failed!"
     return 1;
    fi

    checkSDCard
    if [ $? -ne 0 ];then
        return 1
    fi
    echo "y
    " | mkfs.ext3 -L ubuntu_fs ${DEV_NAME}$p3 # updated by aman
    if [[ $? -ne 0 ]];then
     echo "Failed: Format SDcard3 failed!"
     return 1;
    fi
    return 0
}
#end

# ************************Copy files to SD**************************************
# Description:  copy rar and root filesystem to SDcard
# ******************************************************************************
function copyFilesToSDcard()
{
    # 复制第三方的文件到系统
    mkdir -p ${LogPath}squashfs-root/opt/mini #创建一个文件夹
    chmod 755 ${LogPath}squashfs-root/opt/mini # 赋予权限
    # 把minirc_install_phase1.sh解压到${LogPath}
    unzip ${ISO_FILE_DIR}/${RUN_MINI} mini_developerkit/scripts/minirc_install_phase1.sh -d ${LogPath}
    # 把minirc_install_phase1.sh放到mini文件夹下面
    cp ${LogPath}mini_developerkit/scripts/minirc_install_phase1.sh ${LogPath}squashfs-root/opt/mini/
    if [[ $? -ne 0 ]];then
        # 出现这个错误说明没复制成功者个文件,为什么呢,很可能就是你的mini_developerkit包文件有问题
        echo "Failed: Copy minirc_install_phase1.sh to filesystem failed!"
        return 1
    fi
    # 赋予可执行权限
    chmod +x ${LogPath}/mini_developerkit/scripts/minirc_install_phase1.sh
    # 解压minirc_boot.sh并把这个文件复制到squashfs-root/var/目录
    unzip ${ISO_FILE_DIR}/${RUN_MINI} mini_developerkit/scripts/minirc_boot.sh -d ${LogPath}
    cp ${LogPath}mini_developerkit/scripts/minirc_boot.sh ${LogPath}squashfs-root/var/
    if [[ $? -ne 0 ]];then
        # 出现这个错误说明没复制成功者个文件,为什么呢,很可能就是你的mini_developerkit包文件有问题
        echo "Failed: Copy minirc_boot.sh to filesystem failed!"
        return 1
    fi
    # 复制perf文件
    unzip ${ISO_FILE_DIR}/${RUN_MINI} mini_developerkit/extend_rootfs/perf -d ${LogPath}
    cp ${LogPath}mini_developerkit/extend_rootfs/perf ${LogPath}squashfs-root/usr/bin/perf
    if [[ $? -ne 0 ]];then
        echo "Failed: Copy perf.sh to filesystem failed!"
        return 1
    fi
    chmod +x ${LogPath}squashfs-root/usr/bin/perf

    # 是x86系统就删除qemu-aarch64-static
    if [[ ${arch} =~ "x86" ]];then
        rm ${LogPath}squashfs-root/usr/bin/qemu-aarch64-static
    fi

    #复制${RUN_MINI}到mini_pkg_install/opt/mini/
    mkdir -p ${LogPath}mini_pkg_install/opt/mini
    cp ${ISO_FILE_DIR}/${RUN_MINI}  ${LogPath}mini_pkg_install/opt/mini/
    chmod +x ${LogPath}squashfs-root/opt/mini/minirc_install_phase1.sh
    ${LogPath}mini_developerkit/scripts/minirc_install_phase1.sh ${LogPath}mini_pkg_install
    res=$(echo $?)
    if [[ ${res} != "0" ]];then
        echo "Install ${RUN_MINI} fail, error code:${res}"
        echo "Failed: Install ${RUN_MINI} failed!"
        return 1
    fi
    # 删除mini_pkg_install/opt
    rm -rf ${LogPath}mini_pkg_install/opt
    # 复制mini_pkg_install里面所有的文件到squashfs-root文件夹
    cp -rf ${LogPath}mini_pkg_install/* ${LogPath}squashfs-root/
    if [[ $? -ne 0 ]];then
        echo "Failed: Copy mini_pkg_install to filesystem failed!"
        return 1
    fi
    # 删除mini_pkg_install
    rm -rf ${LogPath}mini_pkg_install
    # 复制squashfs-root所有文件到分区1
    cp -a ${LogPath}squashfs-root/* ${TMPDIR_SD_MOUNT}
    if [[ $? -ne 0 ]];then
        echo "Failed: Copy root filesystem to SDcard failed!"
        return 1
    fi
    # 复制/home所有文件到分区3
    cp -rf ${TMPDIR_SD_MOUNT}/home/* ${TMPDIR_SD3_MOUNT}/
    #rm -rf ${TMPDIR_SD_MOUNT}/home/*
    # 复制${TMPDIR_SD_MOUNT}/var/log/所有文件到分区2
    cp -rf ${TMPDIR_SD_MOUNT}/var/log/* ${TMPDIR_SD2_MOUNT}/
    #rm -rf ${TMPDIR_SD_MOUNT}/var/log/*
    return 0
}
# end

# ************************Make sysroot**************************************
# Description:  copy aarch64 gnu libs
# ******************************************************************************
function make_sysroot()
{
    # 创建文件夹/usr/aarch64-linux-gnu/
    if [ ! -d /usr/aarch64-linux-gnu/ ]; then
        mkdir -p /usr/aarch64-linux-gnu/
    fi
    # 创建文件夹/usr/lib/aarch64-linux-gnu/
    if [ ! -d /usr/lib/aarch64-linux-gnu/ ]; then
        mkdir -p /usr/lib/aarch64-linux-gnu/
    fi
    # 复制文件夹
    cp -rdp ${LogPath}squashfs-root/usr/include /usr/aarch64-linux-gnu/
    cp -rdp ${LogPath}squashfs-root/usr/lib/aarch64-linux-gnu/* /usr/lib/aarch64-linux-gnu/
    cp -rdp ${LogPath}squashfs-root/lib/aarch64-linux-gnu /lib/
    if [ ! -f /usr/lib/aarch64-linux-gnu/libz.so ]; then
        ln -s /lib/aarch64-linux-gnu/libz.so.1 /usr/lib/aarch64-linux-gnu/libz.so
    fi
    # 创建软链接
    ln -s /usr/aarch64-linux-gnu/include/sys /usr/include/sys
    ln -s /usr/aarch64-linux-gnu/include/bits /usr/include/bits
    ln -s /usr/aarch64-linux-gnu/include/gnu /usr/include/gnu
}

# ########################Begin Executing######################################
# ************************Check args*******************************************
function main()
{
    # 检查输入的参数合不合法,4个就是对的不是4个就是错的
    if [[ $# -lt 4 ]];then
        echo "Failed: Number of parameter illegal! Usage: $0 <dev fullname> <img path> <iso fullname> <mini filename>"
        return 1;
    fi
    # 检查网卡IP和USB IP地址是不是有效的
    # ***************check network and usb card ip**********************************
    checkIps
    if [ $? -ne 0 ];then
        return 1
    fi
    # 检查SD卡
    # ************************umount dev_name***************************************
    checkSDCard
    if [ $? -ne 0 ];then
        return 1
    fi

    # ************************Extract ubuntufs**************************************
    # output:squashfs-root/
    #提取Ubunutu镜像里面的系统
    ubuntufsExtract
    if [ $? -ne 0 ];then
        return 1
    fi
    # end
    # 将/usr/bin/qemu-aarch64-static复制到/usr/bin/qemu-aarch64-static目录
    # ************************Check architecture************************************
    arch=$(uname -m)
    if [[ ${arch} =~ "x86" ]];then
         cp /usr/bin/qemu-aarch64-static ${LogPath}squashfs-root/usr/bin/
         if [ $? -ne 0 ];then
             echo "Failed: qemu-user-static or binfmt-support not found!"
             return 1;
         fi
         chmod 755 ${LogPath}squashfs-root/usr/bin/qemu-aarch64-static
    fi
    # end
    # 配置Ubuntu系统
    # ************************Configure ubuntu**************************************
    echo "Process: 1/4(Configure ubuntu filesystem)"
    configUbuntu
    if [ $? -ne 0 ];then
        return 1
    fi
    # end
    #格式化SD卡
    # ************************Format SDcard*****************************************
    echo "Process: 2/4(Format SDcard)"
    formatSDcard
    if [ $? -ne 0 ];then
        return 1
    fi
    # end

    # ************************Copy files to SD**************************************
    # 卸载TMPDIR_SD_MOUNT分区并删除里面的所有文件
    if [[ -d "${TMPDIR_SD_MOUNT}" ]];then
        umount ${TMPDIR_SD_MOUNT} 2>/dev/null
        rm -rf ${TMPDIR_SD_MOUNT}
    fi
    #创建TMPDIR_SD_MOUNT目录并挂载
    mkdir ${TMPDIR_SD_MOUNT}
    mount ${DEV_NAME}$p1 ${TMPDIR_SD_MOUNT} 2>/dev/null  # updated by aman
    # 卸载分区2,并删除分区2里面所有的文件
    if [[ -d "${TMPDIR_SD2_MOUNT}" ]];then
        umount ${TMPDIR_SD2_MOUNT} 2>/dev/null
        rm -rf ${TMPDIR_SD2_MOUNT}
    fi
    #创建TMPDIR_SD_MOUNT2目录并挂载
    mkdir ${TMPDIR_SD2_MOUNT}
    mount ${DEV_NAME}$p2 ${TMPDIR_SD2_MOUNT} 2>/dev/null  # updated by aman
    # 卸载分区3,并删除分区3里面所有的文件
    if [[ -d "${TMPDIR_SD3_MOUNT}" ]];then
        umount ${TMPDIR_SD3_MOUNT} 2>/dev/null
        rm -rf ${TMPDIR_SD3_MOUNT}
    fi
    #创建TMPDIR_SD_MOUNT3目录并挂载
    mkdir ${TMPDIR_SD3_MOUNT}
    mount ${DEV_NAME}$p3 ${TMPDIR_SD3_MOUNT} 2>/dev/null  # updated by aman

    echo "Process: 3/4(Copy filesystem and mini package to SDcard)"
    # 复制文件到SD卡里面去,具体怎么复制请看这个函数
    copyFilesToSDcard
    if [ $? -ne 0 ];then
        return 1
    fi
    # end

    echo "Process: 4/4(Make sysroot)"
    make_sysroot
    umount ${TMPDIR_SD_MOUNT} 2>/dev/null
    if [[ $? -ne 0 ]];then
        echo "Failed: Umount ${TMPDIR_SD_MOUNT} to SDcard failed!"
        return 1
    fi
    
    

    umount ${TMPDIR_SD2_MOUNT} 2>/dev/null
    if [[ $? -ne 0 ]];then
        echo "Failed: Umount ${TMPDIR_SD2_MOUNT} to SDcard failed!"
        return 1
    fi

    umount ${TMPDIR_SD3_MOUNT} 2>/dev/null
    if [[ $? -ne 0 ]];then
        echo "Failed: Umount ${TMPDIR_SD3_MOUNT} to SDcard failed!"
        return 1
    fi
    echo "Finished!"
    return 0
}
#进入脚本我们应该从这里看代码,上面全部是封装的函数,用来调用的
main $* #执行main函数
ret=$? #获取执行的结果
#clean files
filesClean #清理缓存的文件

#失败就向make_ubuntu_sd.result写个字符串Failed
if [[ ret -ne 0 ]];then
    echo "Failed" > ${LogPath}/make_ubuntu_sd.result
    exit 1
fi
#成功就向make_ubuntu_sd.sh写个字符串Success
echo "Success" > ${LogPath}/make_ubuntu_sd.result
exit 0
# end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农张三疯

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值