| | |
| | | #!/bin/bash |
| | | |
| | | PRJ_PATH=`pwd` |
| | | PRJ_NAME=`basename ${PRJ_PATH}` |
| | | |
| | | BOARD=imx6ull |
| | | |
| | | TAR_PATH=/home/guowenxue/imx6ull/bsp/tarball |
| | | DRV_PATH=${PRJ_PATH}/driver |
| | | TAR_PATH=${PRJ_PATH}/../tarball |
| | | |
| | | # rootfs should be buildroot or stretch/buster/bullseye for debian system |
| | | # rootfs should be buildroot/yocto or buster/bullseye for debian system |
| | | ROOTFS=buildroot |
| | | |
| | | ROOTFS_DIR=rootfs_${ROOTFS} |
| | | ARCH=armhf |
| | | |
| | | # rootfs configuration |
| | | DEF_PASSWD=123456 |
| | | DEF_USER=lingyun |
| | | DEF_PASSWD=12345 |
| | | DEF_HOSTNAME=${BOARD} |
| | | |
| | | APT_UPDATE=0 |
| | | |
| | | EXTRA_APPS="vim net-tools network-manager tree file parted locales lsb-release tzdata wireless-tools openssh-server" |
| | | APT_CONF=extra_apps.json |
| | | |
| | | set -u |
| | | set -e |
| | | |
| | | trap 'ctrlC_handler' INT |
| | | |
| | | function ctrlC_handler() |
| | | trap 'exit_handler' EXIT |
| | | function exit_handler() |
| | | { |
| | | echo "Ctrl+C is captured, do some clean work" |
| | | rm -f ${ROOTFS_DIR}/usr/bin/qemu-arm-static |
| | | umount ${ROOTFS_DIR}/{sys,proc,dev/pts,dev} 2>/dev/null || true |
| | | } |
| | | |
| | | STAGE=0 |
| | | |
| | | function msg_banner() |
| | | { |
| | | { |
| | | STAGE=`expr $STAGE + 1` |
| | | |
| | | |
| | | echo "" |
| | | echo "+---------------------------------------------+" |
| | | printf " Stage $STAGE: $1\n" |
| | | echo "+---------------------------------------------+" |
| | | printf " Stage $STAGE: $1\n" |
| | | echo "+---------------------------------------------+" |
| | | echo "" |
| | | } |
| | | |
| | | function do_systools() |
| | | function do_unpack() |
| | | { |
| | | msg_banner " install system tools" |
| | | |
| | | debootstrap --version > /dev/null 2>&1 |
| | | if [ $? == 0 ] ; then |
| | | echo "system tools already installed, skip it" |
| | | return ; |
| | | if [ -d ${ROOTFS_DIR} ] ; then |
| | | printf "\n\n -- ${ROOTFS_DIR} fetched already, skip it -- \n\n" |
| | | return; |
| | | fi |
| | | |
| | | apt install binfmt-support qemu qemu-user-static debootstrap debian-archive-keyring |
| | | 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; |
| | | } |
| | | |
| | | |
| | | # debootstrap fetch debian rootfs |
| | | function do_fetch() |
| | | { |
| | | if [ ! -d ${ROOTFS_DIR} ] ; then |
| | | if [ -s ${TAR_PATH}/${ROOTFS_DIR}.tar.bz2 ] ; then |
| | | msg_banner " decompress ${ROOTFS_DIR}.tar.bz2 " |
| | | mkdir -p ${ROOTFS_DIR} |
| | | tar -xjf ${TAR_PATH}/${ROOTFS_DIR}.tar.bz2 -C ${ROOTFS_DIR} |
| | | APT_UPDATE=1 |
| | | else |
| | | msg_banner " debootstrap fetch " |
| | | debootstrap --arch=${ARCH} --foreign ${ROOTFS} ${ROOTFS_DIR} http://ftp.cn.debian.org/debian/ |
| | | DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} debootstrap/debootstrap --second-stage |
| | | APT_UPDATE=1 |
| | | msg_banner " ${ROOTFS_DIR} do fetch " |
| | | |
| | | # 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. " |
| | | echo "" |
| | | exit; |
| | | else |
| | | return 0; |
| | | fi |
| | | else |
| | | msg_banner "debain rootfs already fetch, skip it" |
| | | fi |
| | | |
| | | if [ $APT_UPDATE == 0 ] ; then |
| | | export DEBIAN_FRONTEND=noninteractive |
| | | export DEBCONF_NONINTERACTIVE_SEEN=true |
| | | export LC_ALL=C |
| | | export LANGUAGE=C |
| | | export LANG=C |
| | | |
| | | printf "\n\n -- debootstrap fetch start --\n\n" |
| | | debootstrap --arch=${ARCH} --foreign ${ROOTFS} ${ROOTFS_DIR} http://ftp.cn.debian.org/debian/ |
| | | |
| | | chroot ${ROOTFS_DIR} debootstrap/debootstrap --second-stage |
| | | printf "\n\n -- debootstrap config start --\n\n" |
| | | |
| | | 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|.*md5val.*|\t\"md5val\":\"MD5_Auto_Generate_Here\"|g" $APT_CONF |
| | | } |
| | | |
| | | # Install extra apps defined in extra_apps.conf |
| | | function do_apt() |
| | | { |
| | | export DEBIAN_FRONTEND=noninteractive |
| | | export DEBCONF_NONINTERACTIVE_SEEN=true |
| | | export LC_ALL=C |
| | | export LANGUAGE=C |
| | | export LANG=C |
| | | |
| | | msg_banner " debootstrap apt install " |
| | | |
| | | if [ -f $APT_CONF ] ; then |
| | | extra_apps=$(eval jq -r .extra_apps[] $APT_CONF ) |
| | | md5_file=$(eval jq -r .md5val extra_apps.json) |
| | | 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" |
| | | else |
| | | printf "\n\n -- debootstrap apt install start --\n\n" |
| | | |
| | | 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 |
| | | mount -o bind /sys ${ROOTFS_DIR}/sys |
| | | |
| | | chroot ${ROOTFS_DIR} apt update |
| | | chroot ${ROOTFS_DIR} apt install -y ${extra_apps} |
| | | |
| | | umount ${ROOTFS_DIR}/{sys,proc,dev/pts,dev} |
| | | |
| | | sed -i -e "s|.*md5val.*|\t\"md5val\":\"$md5_calc\"|g" $APT_CONF |
| | | printf "\n\n -- debootstrap apt install done --\n\n" |
| | | |
| | | # modify root password |
| | | chroot ${ROOTFS_DIR} sh -c "echo root:${DEF_PASSWD} | chpasswd" |
| | | |
| | | # add extra 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" |
| | | 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 -G video,audio,sudo avnet" |
| | | |
| | | echo "${DEF_USER} ALL=(ALL:ALL) NOPASSWD:ALL" >> ${ROOTFS_DIR}/etc/sudoers |
| | | fi |
| | | set -e |
| | | fi |
| | | fi |
| | | } |
| | | |
| | | function do_modules() |
| | | { |
| | | if [ ! -d ${DRV_PATH}/lib/modules ] ; then |
| | | return ; |
| | | fi |
| | | |
| | | msg_banner " debootstrap config " |
| | | 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} dpkg --configure -a |
| | | DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C chroot ${ROOTFS_DIR} apt update |
| | | msg_banner " install kernel modules" |
| | | |
| | | msg_banner " debootstrap apt install " |
| | | 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 -rf ${ROOTFS_DIR}/lib/modules/ |
| | | cp -af ${DRV_PATH}/lib/modules ${ROOTFS_DIR}/lib/ |
| | | |
| | | rm -f ${ROOTFS_DIR}/usr/bin/qemu-arm-static |
| | | if [ ! -d ${ROOTFS_DIR}/lib/firmware -a -s firmware.tar.bz2 ] ; then |
| | | tar -xjf firmware.tar.bz2 -C ${ROOTFS_DIR}/lib/ |
| | | fi |
| | | |
| | | printf "\n\n -- install kernel modules done --\n\n" |
| | | } |
| | | |
| | | function do_modify() |
| | | { |
| | | msg_banner " modify rootfs environment" |
| | | |
| | | set +e |
| | | |
| | | # update hostnmae and issue |
| | | echo "GNU/Linux $ROOTFS \n \l, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue |
| | | echo $DEF_HOSTNAME > ${ROOTFS_DIR}/etc/hostname |
| | | echo "Debian GNU/Linux $ROOTFS \n \l, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue |
| | | grep "$BOARD" ${ROOTFS_DIR}/etc/hosts > /dev/null 2>&1 |
| | | if [ $? != 0 ] ; then |
| | | echo "127.0.0.1 ${BOARD}" >> ${ROOTFS_DIR}/etc/hosts |
| | | fi |
| | | |
| | | # update dns server |
| | | echo "nameserver 114.114.114.114" > ${ROOTFS_DIR}/etc/resolv.conf |
| | |
| | | fi |
| | | |
| | | # permit root ssh login |
| | | 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 |
| | | 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 |
| | | |
| | | set -e |
| | | |
| | | printf "\n\n -- modify rootfs done --\n\n" |
| | | } |
| | | |
| | | function do_pack() |
| | | { |
| | | |
| | | if [ -f ${ROOTFS_DIR}.tar.bz2 ] ; then |
| | | msg_banner " rootfs already generate, skip it" |
| | | return ; |
| | |
| | | tar -cjf ../${ROOTFS_DIR}.tar.bz2 * |
| | | |
| | | cd ${PRJ_PATH} |
| | | |
| | | printf "\n\n -- generate rootfs packet done --\n\n" |
| | | } |
| | | |
| | | if [ $ROOTFS == buildroot ] ;then |
| | | echo "INFO: buildroot no need build." |
| | | exit 0; |
| | | fi |
| | | function do_distclean() |
| | | { |
| | | printf "\n\n -- do distclean in $PRJ_NAME --\n\n" |
| | | |
| | | if [ `id -u` != 0 ] ; then |
| | | echo "ERRROR: This shell script must excuted as root privilege." |
| | | exit 0; |
| | | fi |
| | | rm -rf driver |
| | | rm -rf $ROOTFS_DIR |
| | | |
| | | do_systools |
| | | exit; |
| | | } |
| | | |
| | | do_fetch |
| | | function do_rootfs() |
| | | { |
| | | do_fetch |
| | | |
| | | set +e |
| | | do_modify |
| | | echo "do rootfs continue " |
| | | |
| | | do_pack |
| | | # debian rootfs need apt install packet |
| | | if [ $ROOTFS != "buildroot" -a $ROOTFS != "yocto" ] ;then |
| | | do_apt |
| | | fi |
| | | |
| | | do_modules |
| | | do_modify |
| | | do_pack |
| | | } |
| | | |
| | | function do_root() |
| | | { |
| | | echo "" |
| | | if [[ $1 == "yes" ]] && [ `id -u` != 0 ] ; then |
| | | echo "ERROR: This action must run as root!" |
| | | echo "" |
| | | exit; |
| | | elif [[ $1 != "yes" ]] && [ `id -u` == 0 ] ; then |
| | | echo "ERROR: This action cannot run as root!" |
| | | echo "" |
| | | exit; |
| | | fi |
| | | } |
| | | |
| | | function do_usage() |
| | | { |
| | | echo "" |
| | | echo "Usage:" |
| | | echo " $0 [-b] [-c] [-h]" |
| | | echo " -b: download and build $PRJ_NAME" |
| | | echo " -c: clean all the source code" |
| | | echo " -h: show this help message" |
| | | echo "" |
| | | echo " WARNNING: This shell script must run as sudo" |
| | | echo "" |
| | | exit; |
| | | } |
| | | |
| | | while getopts "bch" OPTNAME |
| | | do |
| | | case "${OPTNAME}" in |
| | | "b") |
| | | break; |
| | | ;; |
| | | |
| | | "c") |
| | | do_root "yes" |
| | | do_distclean |
| | | ;; |
| | | |
| | | "*") |
| | | do_usage |
| | | ;; |
| | | esac |
| | | done |
| | | |
| | | do_root "yes" |
| | | do_rootfs |
| | | |