| | |
| | | #!/bin/bash |
| | | |
| | | PRJ_PATH=`pwd` |
| | | PRJ_NAME=`basename ${PRJ_PATH}` |
| | | source ../scripts/setup_env.sh |
| | | |
| | | BOARD=imx6ull |
| | | JSON_CONF=rootfs.json |
| | | |
| | | DRV_PATH=${PRJ_PATH}/driver |
| | | TAR_PATH=${PRJ_PATH}/../tarball |
| | | |
| | | # tarballs download URL address |
| | | WEB_URL=http://192.168.2.193:9000/${BOARD} |
| | | |
| | | # rootfs should be buildroot/yocto or stretch/buster/bullseye for debian system |
| | | ROOTFS=buster |
| | | |
| | | ROOTFS_DIR=rootfs_${ROOTFS} |
| | | ARCH=arm64 |
| | | |
| | | # rootfs configuration |
| | | 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} |
| | | |
| | | APT_CONF=extra_apps.json |
| | | TARBALL_DIR=${PRJ_PATH}/../tarballs/ |
| | | |
| | | set -u |
| | | set -e |
| | | |
| | | trap 'exit_handler' EXIT |
| | |
| | | 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 |
| | | debian_src=$1 |
| | | ARCH=$2 |
| | | |
| | | return; |
| | | } |
| | | |
| | | |
| | | # debootstrap fetch debian rootfs |
| | | function do_fetch() |
| | | { |
| | | 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 |
| | | fi |
| | | |
| | | # debootstrap fetch debian rootfs |
| | | 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/ |
| | | 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" |
| | | |
| | | # set root password |
| | | cd ${ROOTFS_DIR} |
| | | tar -cjf ../${ROOTFS_DIR}.orig.tar.bz2 * |
| | | cd - |
| | | } |
| | | |
| | | # Install extra apps defined in configure file |
| | | function do_debian_apt() |
| | | { |
| | | if [ $SYSTEM != debian ] ; then |
| | | return; |
| | | fi |
| | | |
| | | 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 |
| | | |
| | | pr_warn "debootstrap apt install extra apps" |
| | | |
| | | 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 |
| | | mount -o bind /sys ${ROOTFS_DIR}/sys |
| | | |
| | | chroot ${ROOTFS_DIR} apt update |
| | | chroot ${ROOTFS_DIR} apt install -y ${extra_apps} |
| | | 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 |
| | | |
| | | 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 |
| | | 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 |
| | | |
| | | # modify root password |
| | | chroot ${ROOTFS_DIR} sh -c "echo root:${DEF_PASSWD} | chpasswd" |
| | | |
| | | # add extra user account |
| | | chroot ${ROOTFS_DIR} sh -c "useradd -m -G sudo -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 ${DEF_USER}" |
| | | # 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 |
| | | |
| | | # Clear the MD5 value to install extra apps |
| | | sed -i -e "s|.*md5val.*|\t\"md5val\":\"MD5_Auto_Generate_Here\"|g" $APT_CONF |
| | | # update sudo without passwd |
| | | sed -i "s|^%sudo.*|%sudo ALL=(ALL:ALL) NOPASSWD:ALL|g" ${ROOTFS_DIR}/etc/sudoers |
| | | |
| | | # 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 |
| | | } |
| | | |
| | | # Install extra apps defined in extra_apps.conf |
| | | function do_apt() |
| | | |
| | | function do_fetch() |
| | | { |
| | | export DEBIAN_FRONTEND=noninteractive |
| | | export DEBCONF_NONINTERACTIVE_SEEN=true |
| | | export LC_ALL=C |
| | | export LANGUAGE=C |
| | | export LANG=C |
| | | if [ -d $ROOTFS_DIR ] ; then |
| | | pr_warn "rootfs tree for $SYSTEM($DISTRO) fetch already" |
| | | return; |
| | | fi |
| | | |
| | | msg_banner " debootstrap apt install " |
| | | pr_warn "start fetch rootfs tree for $SYSTEM($DISTRO) " |
| | | |
| | | 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 [ -f $ROOTFS_DIR.tar.bz2 ] ; then |
| | | do_unpack $TARBALL_DIR/$ROOTFS_DIR.tar.bz2 $ROOTFS_DIR |
| | | return; |
| | | fi |
| | | |
| | | if [ $SYSTEM == "yocto" ] ; then |
| | | |
| | | if [ $md5_file == $md5_calc ] ; then |
| | | printf "\n\n -- debootstrap apt install already, skip it! --\n\n" |
| | | 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 |
| | | 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" |
| | | 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_modules() |
| | | function do_config() |
| | | { |
| | | if [ ! -d ${DRV_PATH}/lib/modules ] ; then |
| | | return ; |
| | | fi |
| | | |
| | | msg_banner " install kernel modules" |
| | | |
| | | rm -rf ${ROOTFS_DIR}/lib/modules/ |
| | | cp -af ${DRV_PATH}/lib/modules ${ROOTFS_DIR}/lib/ |
| | | |
| | | printf "\n\n -- install kernel modules done --\n\n" |
| | | } |
| | | |
| | | function do_modify() |
| | | { |
| | | msg_banner " modify rootfs environment" |
| | | pr_warn "start configure for $SYSTEM($DISTRO) rootfs" |
| | | |
| | | set +e |
| | | |
| | | # update hostnmae and 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 |
| | | echo "$BOARD GNU/Linux $ROOTFS \n \l, default password '$DEF_PASSWD'." > ${ROOTFS_DIR}/etc/issue |
| | | grep "${DEF_HOSTNAME}" ${ROOTFS_DIR}/etc/hosts > /dev/null 2>&1 |
| | | if [ $? != 0 ] ; then |
| | | echo "127.0.0.1 ${DEF_HOSTNAME}" >> ${ROOTFS_DIR}/etc/hosts |
| | | fi |
| | | |
| | | # update dns server |
| | | 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 |
| | | |
| | | # 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 |
| | | 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; |
| | | } |
| | |
| | | function do_rootfs() |
| | | { |
| | | do_fetch |
| | | |
| | | echo "do rootfs continue " |
| | | |
| | | # debian rootfs need apt install packet |
| | | if [ $ROOTFS != "buildroot" -a $ROOTFS != "yocto" ] ;then |
| | | do_apt |
| | | fi |
| | | |
| | | do_modules |
| | | do_modify |
| | | do_pack |
| | | do_install |
| | | do_config |
| | | } |
| | | |
| | | function do_root() |
| | |
| | | { |
| | | 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 "" |
| | |
| | | exit; |
| | | } |
| | | |
| | | while getopts "bch" OPTNAME |
| | | while getopts "bpch" OPTNAME |
| | | do |
| | | case "${OPTNAME}" in |
| | | "b") |
| | | break; |
| | | ;; |
| | | |
| | | "p") |
| | | do_pack |
| | | ;; |
| | | |
| | | "c") |
| | |
| | | do_distclean |
| | | ;; |
| | | |
| | | "*") |
| | | "h") |
| | | do_usage |
| | | ;; |
| | | esac |