From 98ec0b8ada5b4e477798b51906e22ee9f268bab6 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 31 Jan 2024 15:27:54 +0800
Subject: [PATCH] Build:Debian:IGKBoard-All:Add debian rootfs build shell script

---
 debian/build.sh |  193 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 193 insertions(+), 0 deletions(-)

diff --git a/debian/build.sh b/debian/build.sh
new file mode 100755
index 0000000..f04ab7d
--- /dev/null
+++ b/debian/build.sh
@@ -0,0 +1,193 @@
+#!/bin/bash
+# This shell script used to fetch debian root file system
+
+# this project absolute path
+PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
+
+# top project absolute path
+TOP_PATH=$(realpath $PRJ_PATH/..)
+
+# config file path
+CONF_FILE=$TOP_PATH/config.json
+
+# rootfs configuration
+DEF_PASSWD=12345
+
+# shell script will exit once get command error
+set -e
+
+#+-------------------------+
+#| Shell script functions  |
+#+-------------------------+
+
+function pr_error() {
+    echo -e "\033[40;31m $1 \033[0m"
+}
+
+function pr_warn() {
+    echo -e "\033[40;33m $1 \033[0m"
+}
+
+function pr_info() {
+    echo -e "\033[40;32m $1 \033[0m"
+}
+
+# parser configure file and export environment variable
+function export_env()
+{
+    export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export DIS_TYPE=`jq -r ".system.distro" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export DIS_VER=`jq -r ".system.version" $CONF_FILE | tr 'A-Z' 'a-z'`
+
+    if [ $DIS_TYPE != debian ] ; then
+        pr_error "ERROR: Distro type in configure is not debian"
+        exit ;
+    fi
+
+    if [[ $BOARD =~ 6ull ]]  ; then
+        export ARCH=armhf
+    else
+        export ARCH=arm64
+    fi
+
+    export ROOTFS_DIR=rootfs-$DIS_TYPE-$DIS_VER
+
+    echo ""
+    pr_info "INFO: Start debootstrap fetch debian $DIS_VER root filesystem now."
+}
+
+# Debian 12 depends on the new keyring
+# https://packages.debian.org/sid/all/debian-archive-keyring/download
+function update_keyring()
+{
+    debname=debian-archive-keyring
+    debversion=2023.3
+
+    set +e
+    dpkg -l $debname > /dev/null 2>&1
+    if [ $? == 0 ] ; then
+        dpkg -s debian-archive-keyring | grep Version: | grep $debversion > /dev/null 2>&1
+        if [ $? != 0 ] ; then
+            pr_info "updating $debname"
+            apt purge -y $debname
+        else
+            pr_info "$debname is latest"
+            return;
+        fi
+    fi
+    set -e
+
+    pr_info "install $debname now"
+    wget http://ftp.us.debian.org/debian/pool/main/d/$debname/${debname}_${debversion}_all.deb > /dev/null
+    dpkg -i ${debname}_${debversion}_all.deb
+    rm -f ${debname}_${debversion}_all.deb
+}
+
+function do_install()
+{
+    pr_warn "\ninstall system tools"
+
+    update_keyring
+
+    debootstrap --version > /dev/null 2>&1
+    if [ $? == 0 ] ; then
+        pr_info "system tools installed already"
+        return ;
+    fi
+
+    apt install binfmt-support qemu qemu-user-static debootstrap
+}
+
+function do_fetch()
+{
+    pr_warn "\ndebootstrap fetch"
+
+    if [ -f ${ROOTFS_DIR}/bin/bash ] ; then
+        pr_info "debain rootfs fetched already"
+        return ;
+    fi
+    debootstrap --arch=${ARCH} --foreign ${DIS_VER} ${ROOTFS_DIR} http://ftp.debian.org/debian/
+
+    cp -f /usr/bin/qemu-arm-static ${ROOTFS_DIR}/usr/bin/
+    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} debootstrap/debootstrap --second-stage
+    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} dpkg --configure -a
+    rm -f ${ROOTFS_DIR}/usr/bin/qemu-arm-static
+}
+
+function modify_rootfs()
+{
+    pr_warn "\nmodify rootfs environment"
+
+    # update hostnmae and issue
+    BOARD=igkboard
+    echo $BOARD > ${ROOTFS_DIR}/etc/hostname
+    echo "Debian GNU/Linux $VERSION \n \l, default password '$DEF_PASSWD'" > ${ROOTFS_DIR}/etc/issue
+
+    # update dns server
+    echo "nameserver 223.5.5.5" > ${ROOTFS_DIR}/etc/resolv.conf
+    echo "nameserver 114.114.114.114" >> ${ROOTFS_DIR}/etc/resolv.conf
+
+    # update NTP server
+    sed -i "s|^#NTP=.*|NTP=pool.ntp.org|g" ${ROOTFS_DIR}/etc/systemd/timesyncd.conf
+
+    set +e
+    # add ls alias for display with color
+    grep "color=auto" ${ROOTFS_DIR}/etc/profile > /dev/null 2>&1
+    if [ $? != 0 ] ; then
+        echo "alias ls='ls --color=auto'" >> ${ROOTFS_DIR}/etc/profile
+    fi
+
+    grep "^PermitRootLogin" ${ROOTFS_DIR}/etc/ssh/sshd_config > /dev/null 2>&1
+    if [ $? != 0 ] ; then
+        echo "PermitRootLogin yes" >> ${ROOTFS_DIR}/etc/ssh/sshd_config
+    fi
+    set -e
+}
+
+function do_conf()
+{
+    EXTRA_APPS="vim net-tools network-manager tree file parted locales lsb-release tzdata wireless-tools openssh-server systemd-timesyncd"
+
+    pr_warn "\ndebootstrap configure"
+
+    cp -f /usr/bin/qemu-arm-static ${ROOTFS_DIR}/usr/bin/
+    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} apt update
+    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} apt install -y ${EXTRA_APPS}
+    DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} sh -c "echo root:${DEF_PASSWD} | chpasswd"
+    rm -f ${ROOTFS_DIR}/usr/bin/qemu-arm-static
+
+    modify_rootfs
+}
+
+function do_pack()
+{
+    pr_warn "\npackage $ROOTFS_DIR"
+
+    cd $PRJ_PATH/$ROOTFS_DIR
+    tar -cJf ../$ROOTFS_DIR.tar.xz *
+}
+
+#+-------------------------+
+#| Shell script body start |
+#+-------------------------+
+
+if [ `id -u` != 0 ] ; then
+    pr_error "ERRROR: This shell script must excuted as root privilege."
+    exit 0;
+fi
+
+if [[ $# == 1 && $1 == -c ]] ;then
+    pr_warn "start clean debian rootfs"
+    rm -rf rootfs-debian-*
+    exit;
+fi
+
+export_env
+
+do_install
+
+do_fetch
+
+do_conf
+
+do_pack

--
Gitblit v1.9.1