From f2733b7bf062d8f560bfdc8b4583d21680351f01 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sun, 12 Jun 2022 17:08:24 +0800
Subject: [PATCH] update rootfs & images build shell script, use rsync instead tar to install rootfs

---
 bsp/rootfs/build.sh |  328 ++++++++++++++++++++++--------------------------------
 1 files changed, 134 insertions(+), 194 deletions(-)

diff --git a/bsp/rootfs/build.sh b/bsp/rootfs/build.sh
index 33aeabd..33f7488 100755
--- a/bsp/rootfs/build.sh
+++ b/bsp/rootfs/build.sh
@@ -1,28 +1,15 @@
 #!/bin/bash
 
-PRJ_PATH=`pwd`
-PRJ_NAME=`basename ${PRJ_PATH}`
+source ../scripts/setup_env.sh
 
-# update by top build.sh
-BOARD=igkboard
-SYSTEM=buildroot
-DISTRO=2021.02
-SYSNAME=buildroot
+JSON_CONF=rootfs.json
 
-ROOTFS_DIR=rootfs_${SYSNAME}
-
-TAR_PATH=${PRJ_PATH}/../tarball
-
-# rootfs configuration
-DEBIAN_URL=http://ftp.cn.debian.org/debian/
-ARCH=armhf
-DEF_USER=lingyun
-DEF_PASSWD=12345
+DEF_USER=`jq -r ".SYS_CONF.DEF_USER" $JSON_CONF`
+DEF_PASSWD=`jq -r ".SYS_CONF.DEF_PASSWD" $JSON_CONF`
 DEF_HOSTNAME=${BOARD}
 
-APPS_CONF=extra_apps.json
+TARBALL_DIR=${PRJ_PATH}/../tarballs/
 
-set -u
 set -e
 
 trap 'exit_handler' EXIT
@@ -32,53 +19,14 @@
     umount ${ROOTFS_DIR}/{sys,proc,dev/pts,dev} 2>/dev/null || true
 }
 
-STAGE=0
-function msg_banner()
+function do_debian_fetch()
 {
-    STAGE=`expr $STAGE + 1`
-
-    echo ""
-    echo "+---------------------------------------------+"
-    printf " Stage $STAGE: $1\n"
-    echo "+---------------------------------------------+"
-    echo ""
-}
-
-function do_unpack()
-{
-    if [ -d ${ROOTFS_DIR} ] ; then
-        printf "\n\n -- ${ROOTFS_DIR} fetched already, skip it -- \n\n"
+    if [ $SYSTEM != debian -o -d $ROOTFS_DIR ] ; then
         return;
     fi
 
-    if [ -s ${TAR_PATH}/${ROOTFS_DIR}.tar.bz2 ]  ; then
-        printf "\n\n -- decompress ${ROOTFS_DIR}.tar.bz2 -- \n\n"
-        mkdir -p ${ROOTFS_DIR}
-        tar -xjf ${TAR_PATH}/${ROOTFS_DIR}.tar.bz2 -C ${ROOTFS_DIR}
-    fi
-
-    return;
-}
-
-
-# decompress rootfs packet or debootstrap fetch debian rootfs
-function do_fetch()
-{
-    msg_banner " ${ROOTFS_DIR} do fetch "
-
-    # try to decompress packet first.
-    do_unpack
-
-    # Yocto and buildroot rootfs decompress only
-    if [ -d ${ROOTFS_DIR} ] ; then
-        return ;
-    else
-        if [ $SYSTEM != "debian" ] ; then
-            echo " ERROR: miss ${SYSTEM}(${DISTRO}) rootfs, exit now. "
-            echo ""
-            exit;
-        fi
-    fi
+    debian_src=$1
+    ARCH=$2
 
     # debootstrap fetch debian rootfs
     export DEBIAN_FRONTEND=noninteractive
@@ -87,51 +35,39 @@
     export LANGUAGE=C
     export LANG=C
 
-    printf "\n\n -- debootstrap fetch start --\n\n"
-    echo "debootstrap --arch=${ARCH} --foreign ${DISTRO} ${ROOTFS_DIR} ${DEBIAN_URL}"
-    debootstrap --arch=${ARCH} --foreign ${DISTRO} ${ROOTFS_DIR} ${DEBIAN_URL}
+    pr_warn "debootstrap fetch start"
 
+    pr_info "debootstrap --arch=${ARCH} --foreign ${DISTRO} ${ROOTFS_DIR} ${debian_src}"
+    debootstrap --arch=${ARCH} --foreign ${DISTRO} ${ROOTFS_DIR} ${debian_src}
     chroot ${ROOTFS_DIR} debootstrap/debootstrap --second-stage
-    printf "\n\n -- debootstrap config start --\n\n"
 
+    pr_warn "debootstrap fetch done"
     chroot ${ROOTFS_DIR} dpkg --configure -a
-    printf "\n\n -- debootstrap fetch done -- \n\n"
-
-    # Clear the MD5 value to install extra apps
-    sed -i -e "s|.*md5apt.*|\t\"md5apt\":\"MD5_Auto_Generate_Here\"|g" $APPS_CONF
 
     cd ${ROOTFS_DIR}
     tar -cjf ../${ROOTFS_DIR}.orig.tar.bz2 *
     cd -
 }
 
-
-# Install extra apps defined in extra_apps.conf
-function do_extra_apt()
+# Install extra apps defined in configure file
+function do_debian_apt()
 {
-    if [ ! -f $APPS_CONF ] ; then
-        return ;
+    if [ $SYSTEM != debian ] ; then
+        return;
     fi
 
-    extra_apps=$(eval jq -r .extra_debian_apps[] $APPS_CONF )
-    md5_file=$(eval jq -r .md5apt $APPS_CONF)
-    md5_calc=$(eval echo $extra_apps | md5sum | awk '{print $1}')
-
-    if [ $md5_file == $md5_calc ] ; then
-        printf "\n\n -- debootstrap apt install already, skip it! --\n\n"
-        return ;
+    apt_install=$(eval echo `jq -r .SYS_DEBIAN.INSTALL_APT $JSON_CONF` | tr 'A-Z' 'a-z')
+    if [ $apt_install != yes ] ; then
+        pr_warn "skip debootstrap apt install extra apps"
+        return;
     fi
 
-    msg_banner " debootstrap apt install "
+    pr_warn "debootstrap apt install extra apps"
 
-    export DEBIAN_FRONTEND=noninteractive
-    export DEBCONF_NONINTERACTIVE_SEEN=true
-    export LC_ALL=C
-    export LANGUAGE=C
-    export LANG=C
-
+    extra_apps=`eval jq -r .SYS_DEBIAN.APT_APPS[] $JSON_CONF`
 
     cp -f /usr/bin/qemu-arm-static ${ROOTFS_DIR}/usr/bin/
+
     mount -o bind /proc ${ROOTFS_DIR}/proc
     mount -o bind /dev ${ROOTFS_DIR}/dev
     mount -o bind /dev/pts ${ROOTFS_DIR}/dev/pts
@@ -142,90 +78,32 @@
     chroot ${ROOTFS_DIR} apt autoremove
 
     umount ${ROOTFS_DIR}/{sys,proc,dev/pts,dev}
+    pr_warn "debootstrap apt install done"
+}
 
+function do_debian_conf()
+{
+    if [ $SYSTEM != debian ] ; then
+        return;
+    fi
 
-    sed -i -e "s|.*md5apt.*|\t\"md5apt\":\"$md5_calc\"|g" $APPS_CONF
-    printf "\n\n -- debootstrap apt install done --\n\n"
-
-    # modify root password
-    chroot ${ROOTFS_DIR} sh -c "echo root:${DEF_PASSWD} | chpasswd"
+    pr_warn "update debian system configuration"
 
     # add default user account
     set +e
     grep "$DEF_USER" ${ROOTFS_DIR}/etc/passwd > /dev/null 2>&1
     if [ $? != 0 ] ; then
-        printf "\n\n -- setup default user account --\n\n"
+        pr_info "setup default user account"
+        cp -f /usr/bin/qemu-arm-static ${ROOTFS_DIR}/usr/bin/
+        chroot ${ROOTFS_DIR} chmod 4755 /usr/bin/sudo
         chroot ${ROOTFS_DIR} sh -c "useradd -m -s /bin/bash ${DEF_USER}"
         chroot ${ROOTFS_DIR} sh -c "echo ${DEF_USER}:${DEF_PASSWD} | chpasswd"
         chroot ${ROOTFS_DIR} sh -c "usermod -aG video,audio,sudo ${DEF_USER}"
     fi
     set -e
-}
 
-function install_file()
-{
-    if [ $# != 1 ] ; then
-        return ;
-    fi
-
-    # parser the source file and destination install path
-    src=`echo $1 | cut -d: -f1`
-    dst=${ROOTFS_DIR}/`echo $1 | cut -d: -f2`
-
-    echo "install $src => $dst"
-
-    mkdir -p ${dst}
-
-    # parser to get .tar.gz .tar.bz2 .tar.xz
-    fname=`basename ${src}`
-    suffix=`echo "${fname#*.}"`
-
-    case $suffix in
-        tar.gz)
-            tar -xzf ${src} -C ${dst}
-            ;;
-
-        tar.bz2)
-            tar -xjf ${src} -C ${dst}
-            ;;
-
-        tar.xz)
-            tar -xJf ${src} -C ${dst}
-            ;;
-
-        *)
-            rm -rf ${dst}/${fname}
-            cp -rf ${src} ${dst}
-            ;;
-
-        esac
-}
-
-function do_install()
-{
-    # apt install extra packet for debian rootfs
-    if [ $SYSTEM == "debian" ] ; then
-        do_extra_apt
-    fi
-
-    # install common files for all the system
-    for row in $( jq -r '.extra_common_files | keys[] as $k | "\($k):\(.[$k])"' $APPS_CONF ) ; do
-        install_file $row
-    done
-
-    # install extra files for custom system
-    jq_args=".extra_${SYSTEM}_files | keys[] as \$k | \"\(\$k):\(.[\$k])\""
-    for row in $( jq -r "${jq_args}" $APPS_CONF ) ; do
-        install_file $row
-    done
-}
-
-
-function update_debian()
-{
-    if [ $SYSTEM != debian ] ; then
-        return;
-    fi
+    # modify root password
+    chroot ${ROOTFS_DIR} sh -c "echo root:${DEF_PASSWD} | chpasswd"
 
     # update PATH environment in /etc/profile
     sed -i 's|PATH=.*|PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"|g' ${ROOTFS_DIR}/etc/profile
@@ -235,20 +113,95 @@
 
     # enable ping command for all user
     chmod 4755 ${ROOTFS_DIR}/usr/bin/ping*
+
+    # enable root login on serial port
+    MOD_FILE=${ROOTFS_DIR}/etc/securetty
+    set +e
+    grep ttyLP1 ${MOD_FILE} > /dev/null 2>&1
+    if [ $? != 0 ] ; then
+        echo "" >> ${MOD_FILE}
+        echo "# i.MX8ULP serial console" >> ${MOD_FILE}
+        echo "ttyLP1" >> ${MOD_FILE}
+        echo "ttyLP2" >> ${MOD_FILE}
+        echo "ttyLP3" >> ${MOD_FILE}
+    fi
+    set -e
 }
 
-function do_modify()
+
+function do_fetch()
 {
-    msg_banner " modify rootfs environment"
+    if [ -d $ROOTFS_DIR ] ; then
+        pr_warn "rootfs tree for $SYSTEM($DISTRO) fetch already"
+        return;
+    fi
+
+    pr_warn "start fetch rootfs tree for $SYSTEM($DISTRO) "
+
+    if [ -f $ROOTFS_DIR.tar.bz2 ] ; then
+        do_unpack $TARBALL_DIR/$ROOTFS_DIR.tar.bz2 $ROOTFS_DIR
+        return;
+    fi
+
+    if [ $SYSTEM == "yocto" ] ; then
+
+        mkdir -p $ROOTFS_DIR
+        do_fetch_json  $JSON_CONF SYS_YOCTO $ROOTFS_DIR
+        do_unpack $TARBALL_DIR/$ROOTFS_DIR.tar.bz2 $ROOTFS_DIR
+
+    elif [ $SYSTEM == "buildroot" ] ; then
+
+        mkdir -p $ROOTFS_DIR
+        do_fetch_json  $JSON_CONF SYS_BUILDROOT $ROOTFS_DIR
+        do_unpack $TARBALL_DIR/$ROOTFS_DIR.tar.bz2 $ROOTFS_DIR
+
+    elif [ $SYSTEM == "debian" ] ; then
+        protocal=`jq -r ".SYS_DEBIAN.PROTOCAL" $JSON_CONF`
+        if [ $protocal != "debootstrap" ] ; then
+            mkdir -p $ROOTFS_DIR
+            do_fetch_json $JSON_CONF SYS_DEBIAN $ROOTFS_DIR
+            do_unpack $TARBALL_DIR/$ROOTFS_DIR.tar.bz2 $ROOTFS_DIR
+
+        else
+            debian_src=`jq -r ".SYS_DEBIAN.URL" $JSON_CONF`
+            arch=`jq -r ".SYS_DEBIAN.ARCH" $JSON_CONF`
+            do_debian_fetch $debian_src $arch
+        fi
+
+    fi
+    rm -f *.tar.*
+}
+
+
+function do_install()
+{
+    # Remove lib/modules first
+    rm -rf $ROOTFS_DIR/lib/modules/*
+
+    # apt install extra packet for debian rootfs
+    if [ $SYSTEM == "debian" ] ; then
+        do_debian_apt
+        do_debian_conf
+        do_install_json $JSON_CONF SYS_DEBIAN.INSTALL_FILES
+    elif [ $SYSTEM == "yocto" ] ; then
+        do_install_json $JSON_CONF SYS_YOCTO.INSTALL_FILES
+    elif [ $SYSTEM == "buildroot" ] ; then
+        do_install_json $JSON_CONF SYS_BUILDROOT.INSTALL_FILES
+    fi
+}
+
+function do_config()
+{
+    pr_warn "start configure for $SYSTEM($DISTRO) rootfs"
 
     set +e
 
     # update hostnmae and issue
-    echo "Welcome to LingYun IoT Gateway Kit Board GNU/Linux ${SYSTEM}(${DISTRO}) system, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue
+    echo "Welcome to ${BOARD} Board GNU/Linux ${SYSTEM}(${DISTRO}) system, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue
     echo $DEF_HOSTNAME > ${ROOTFS_DIR}/etc/hostname
-    grep "$BOARD" ${ROOTFS_DIR}/etc/hosts > /dev/null 2>&1
+    grep "${DEF_HOSTNAME}" ${ROOTFS_DIR}/etc/hosts > /dev/null 2>&1
     if [ $? != 0 ] ; then
-       echo "127.0.0.1       ${BOARD}" >> ${ROOTFS_DIR}/etc/hosts
+       echo "127.0.0.1       ${DEF_HOSTNAME}" >> ${ROOTFS_DIR}/etc/hosts
     fi
 
     # update dns server
@@ -266,48 +219,31 @@
     fi
 
     # permit root ssh login
-    sed -i "s|^#PermitRootLogin.*|PermitRootLogin yes|g" ${ROOTFS_DIR}/etc/ssh/sshd_config
-    sed -i "s|^#PasswordAuthentication.*|PasswordAuthentication yes|g" ${ROOTFS_DIR}/etc/ssh/sshd_config
-
-    update_debian
-
-    # add vim alias for buildroot and yocto
-    if [ $SYSTEM != "debian" ] ; then
-        grep "alias vim=" ${ROOTFS_DIR}/etc/profile > /dev/null 2>&1
-        if [ $? != 0 ] ; then
-            echo "alias vim='vi'" >> ${ROOTFS_DIR}/etc/profile
-        fi
+    if [ -f ${ROOTFS_DIR}/etc/ssh/sshd_config ] ; then
+        sed -i "s|^#PermitRootLogin.*|PermitRootLogin yes|g" ${ROOTFS_DIR}/etc/ssh/sshd_config
+        sed -i "s|^#PasswordAuthentication.*|PasswordAuthentication yes|g" ${ROOTFS_DIR}/etc/ssh/sshd_config
     fi
 
     set -e
-
-    printf "\n\n -- modify rootfs done --\n\n"
+    pr_warn "configure for $SYSTEM($DISTRO) rootfs done"
 }
 
 function do_pack()
 {
-    if [ -f ${ROOTFS_DIR}.tar.bz2 ] ; then
-       msg_banner " rootfs already generate, skip it"
-       return ;
-   else
-       msg_banner " generate rootfs packet "
-    fi
+    pr_warn "start generate $SYSTEM($DISTRO) rootfs packet"
 
     cd ${ROOTFS_DIR}
-
     tar -cjf ../${ROOTFS_DIR}.tar.bz2 *
-
     cd ${PRJ_PATH}
 
-    printf "\n\n -- generate rootfs packet done --\n\n"
+    pr_warn "generate $SYSTEM($DISTRO) rootfs packet done"
 }
 
 function do_distclean()
 {
-    printf "\n\n -- do distclean in $PRJ_NAME --\n\n"
+    pr_warn "clean rootfs source tree"
 
-    rm -rf driver
-    rm -rf $ROOTFS_DIR
+    rm -rf $ROOTFS_DIR* rootfs_*
 
     exit;
 }
@@ -316,8 +252,7 @@
 {
     do_fetch
     do_install
-    do_modify
-    do_pack
+    do_config
 }
 
 function do_root()
@@ -338,8 +273,9 @@
 {
     echo ""
     echo "Usage:"
-    echo "   $0 [-b] [-c] [-h]"
+    echo "   $0 [-b] [p] [-c] [-h]"
     echo "       -b: download and build $PRJ_NAME"
+    echo "       -p: packet the rootfs tree source"
     echo "       -c: clean all the source code"
     echo "       -h: show this help message"
     echo ""
@@ -348,11 +284,15 @@
     exit;
 }
 
-while getopts "bch" OPTNAME
+while getopts "bpch" OPTNAME
 do
     case "${OPTNAME}" in
         "b")
             break;
+            ;;
+
+        "p")
+            do_pack
             ;;
 
         "c")
@@ -360,7 +300,7 @@
             do_distclean
             ;;
 
-        "*")
+        "h")
             do_usage
             ;;
     esac

--
Gitblit v1.9.1