首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[Ubuntu][Shell]make_ubuntu_sd.sh注释解读

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

作者头像
云未归来
发布2025-07-18 13:58:40
发布2025-07-18 13:58:40
950
举报

#!/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="

#********************** 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:\4Gm/7HxehX.YSum4Wf3IDFZ3v5L.clybUpGNGaw9zAh3rqzqB4mWbxvSTFcvhbjY/6.tlgHhWsbtbAVNR9TSI/:17795:0:99999:7:::" ROOT_PWD="root:\

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

1     echo {ip_addr} | grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}

{ip_addr} | sed "s/./ /g"`     do         if [

# **************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 "

{TMPDIR_DATE}     mount -o loop {ISO_FILE_DIR}/{ISO_FILE} {LogPath}     cp {TMPDIR_DATE}/install/filesystem.squashfs {LogPath}     if [[

    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 "\

{MINIRC_LOGROTATE_DIR}minirc_logrotate.conf EOF # 将脚本内容写入minirc_logrotate.conf文件     cat >

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

{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/cdtmp     # 挂载目录     mount -o bind {TMPDIR_DATE} {LogPath}squashfs-root/cdtmp     #将下面脚本内容写入chroot_install.sh     echo " #!/bin/bash RUN_MINI=\

# 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\\\\\

# 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

{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 {USER_NAME} '"

    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 "^(fdisk -l 2>/dev/null | grep "^

        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

? -ne 0 ];then         return 1     fi     echo "y     " | mkfs.ext3 -L ubuntu_fs ? -ne 0 ];then         return 1     fi     echo "y     " | mkfs.ext3 -L ubuntu_fs

? -ne 0 ];then         return 1     fi     echo "y     " | mkfs.ext3 -L ubuntu_fs

{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 [[ {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 {LogPath}     cp {LogPath}mini_developerkit/extend_rootfs/perf {LogPath}squashfs-root/usr/bin/perf     if [[

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

{LogPath}mini_pkg_install/opt/mini     cp {ISO_FILE_DIR}/{RUN_MINI}  {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 {LogPath}mini_pkg_install/opt     # 复制mini_pkg_install里面所有的文件到squashfs-root文件夹     cp -rf {LogPath}mini_pkg_install/* {LogPath}squashfs-root/     if [[ {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}/var/log/所有文件到分区2     cp -rf {TMPDIR_SD_MOUNT}/var/log/* {TMPDIR_SD2_MOUNT}/     #rm -rf

# ************************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

{TMPDIR_SD_MOUNT}     mount {DEV_NAME}p1 {TMPDIR_SD2_MOUNT}     mount {DEV_NAME}p2 {TMPDIR_SD3_MOUNT}     mount {DEV_NAME}p3

    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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档