From 2161762ebc50914fe51a7923945c64e6ce4ee25f Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Tue, 28 Dec 2021 21:46:06 +0800
Subject: [PATCH] update build shell script, add system and distro

---
 bsp/rootfs/build.sh |  100 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 77 insertions(+), 23 deletions(-)

diff --git a/bsp/rootfs/build.sh b/bsp/rootfs/build.sh
index 3adcd33..11d2a5c 100755
--- a/bsp/rootfs/build.sh
+++ b/bsp/rootfs/build.sh
@@ -5,21 +5,32 @@
 
 BOARD=igkboard
 
-DRV_PATH=${PRJ_PATH}/driver
+# $SYSTEM should be buildroot, yocto or debian
+# Buildroot distro should be: buildroot-2021.02
+# Yocto distro should be    : hardknott
+# Debian distro should be   : buster or bullseye
+SYSTEM=buildroot
+DISTRO=v2021.02
+
+SYSTYPE=`echo $SYSTEM | tr 'A-Z' 'a-z'`
+if [ $SYSTYPE == "debian" ] ; then
+   SYSNAME=${DISTRO}
+else
+   SYSNAME=${SYSTYPE}
+fi
+ROOTFS_DIR=rootfs_${SYSNAME}
+
 TAR_PATH=${PRJ_PATH}/../tarball
-
-# rootfs should be buildroot/yocto or buster/bullseye for debian system
-ROOTFS=buildroot
-
-ROOTFS_DIR=rootfs_${ROOTFS}
-ARCH=armhf
+DRV_PATH=${PRJ_PATH}/driver
 
 # rootfs configuration
+ARCH=armhf
 DEF_USER=lingyun
 DEF_PASSWD=12345
 DEF_HOSTNAME=${BOARD}
 
 APT_CONF=extra_apps.json
+PATCH_PATH=${PRJ_PATH}/files
 
 set -u
 set -e
@@ -68,13 +79,13 @@
     # try to decompress packet first.
     do_unpack
 
-    if [ $ROOTFS == "buildroot" -o $ROOTFS == "yocto" ] ; then
-        if [ ! -d ${ROOTFS_DIR} ] ; then
-            echo " ERROR: miss rootfs ${ROOTFS}, exit now. "
+    if [ -d ${ROOTFS_DIR} ] ; then
+        return ;
+    else
+        if [ $SYSTYPE != "debian" ] ; then
+		echo " ERROR: miss ${SYSTEM}(${DISTRO}) rootfs, exit now. "
             echo ""
             exit;
-        else
-            return 0;
         fi
     fi
 
@@ -85,7 +96,7 @@
     export LANG=C
 
     printf "\n\n -- debootstrap fetch start --\n\n"
-    debootstrap --arch=${ARCH} --foreign ${ROOTFS} ${ROOTFS_DIR} http://ftp.cn.debian.org/debian/
+    debootstrap --arch=${ARCH} --foreign ${DISTRO} ${ROOTFS_DIR} http://ftp.cn.debian.org/debian/
 
     chroot ${ROOTFS_DIR} debootstrap/debootstrap --second-stage
     printf "\n\n -- debootstrap config start --\n\n"
@@ -154,20 +165,52 @@
 
 function do_modules()
 {
-    if [ ! -d ${DRV_PATH}/lib/modules ] ; then
+    msg_banner " install linux firmware"
+    if [ -s ${TAR_PATH}/firmware.tar.bz2 ] ; then
+        tar -xjf ${TAR_PATH}/firmware.tar.bz2 -C ${ROOTFS_DIR}/lib/
+    fi
+
+    if [ ! -d ${DRV_PATH}/lib/ ] ; then
         return ;
     fi
 
-    msg_banner " install kernel modules"
+    if [ -d ${DRV_PATH}/lib/modules ] ; then
+        msg_banner " install kernel modules"
+        rm -rf ${ROOTFS_DIR}/lib/modules/
+        cp -af ${DRV_PATH}/lib/modules  ${ROOTFS_DIR}/lib/
+    fi
+}
 
-    rm -rf ${ROOTFS_DIR}/lib/modules/
-    cp -af ${DRV_PATH}/lib/modules  ${ROOTFS_DIR}/lib/
+function do_common()
+{
+    if [ -s ${PATCH_PATH}/expand_rootfs ] ; then
+        cp ${PATCH_PATH}/expand_rootfs ${ROOTFS_DIR}/usr/sbin/
+        chmod a+x ${ROOTFS_DIR}/usr/sbin/expand_rootfs
+    fi
+}
 
-    if [ ! -d ${ROOTFS_DIR}/lib/firmware -a -s firmware.tar.bz2 ] ; then
-        tar -xjf firmware.tar.bz2 -C ${ROOTFS_DIR}/lib/
+function do_buildroot()
+{
+    if [ $SYSTYPE != "buildroot" ] ; then
+        return ;
     fi
 
-    printf "\n\n -- install kernel modules done --\n\n"
+    # add vim alias for vim
+    grep "alias vim=" ${ROOTFS_DIR}/etc/profile > /dev/null 2>&1
+    if [ $? != 0 ] ; then
+       echo "alias vim='vi'" >> ${ROOTFS_DIR}/etc/profile
+    fi
+
+    if [ -s ${PATCH_PATH}/S30WpaSupplicant ] ; then
+        cp ${PATCH_PATH}/S30WpaSupplicant ${ROOTFS_DIR}/etc/init.d/
+        chmod a+x ${ROOTFS_DIR}/etc/init.d/S30WpaSupplicant
+    fi
+
+    if [ -s ${PATCH_PATH}/wpa_supplicant.conf ] ; then
+        cp ${PATCH_PATH}/wpa_supplicant.conf ${ROOTFS_DIR}/etc/
+    fi
+
+    printf "\n\n -- modify buildroot rootfs done --\n\n"
 }
 
 function do_modify()
@@ -176,8 +219,14 @@
 
     set +e
 
+    # modify for buildroot rootfs
+    do_buildroot
+
+    # modify for all rootfs
+    do_common
+
     # update hostnmae and issue
-    echo "GNU/Linux $ROOTFS \n \l, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue
+    echo "Welcome to LingYun IoT Gateway Kit 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
     if [ $? != 0 ] ; then
@@ -188,8 +237,13 @@
     echo "nameserver 114.114.114.114" > ${ROOTFS_DIR}/etc/resolv.conf
     echo "nameserver 223.5.5.5" >> ${ROOTFS_DIR}/etc/resolv.conf
 
+    # update profile
+    sed -i "s|PS1='# '|PS1='\\\u@\\\h:\\\w# '|g" ${ROOTFS_DIR}/etc/profile
+    sed -i "s|PS1='$ '|PS1='\\\u@\\\h:\\\w$ '|g" ${ROOTFS_DIR}/etc/profile
+
+
     # add ls alias for display with color
-    grep "color=auto" ${ROOTFS_DIR}/etc/profile > /dev/null 2>&1
+    grep "^alias ls=" ${ROOTFS_DIR}/etc/profile > /dev/null 2>&1
     if [ $? != 0 ] ; then
        echo "alias ls='ls --color=auto'" >> ${ROOTFS_DIR}/etc/profile
     fi
@@ -238,7 +292,7 @@
     echo "do rootfs continue "
 
     # debian rootfs need apt install packet
-    if [ $ROOTFS != "buildroot" -a $ROOTFS != "yocto"  ]  ;then
+    if [ $SYSTYPE == "debian" ]  ;then
         do_apt
     fi
 

--
Gitblit v1.9.1