From fd1207b324081b72794fd026ecb50d20ae3f7971 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Fri, 10 Jan 2020 20:04:55 +0100 Subject: [PATCH 01/13] gbmake: quoting and formatting of echo strings --- scripts/gbmake | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/scripts/gbmake b/scripts/gbmake index 9a7cbd7..3a356f7 100755 --- a/scripts/gbmake +++ b/scripts/gbmake @@ -51,10 +51,10 @@ gnubee_model() { case `grep machine /proc/cpuinfo` in *GB-PC1* ) prefix=gbpc1;; *GB-PC2* ) prefix=gbpc2;; - * ) echo UNKNOWN; return 1 + * ) echo "UNKNOWN"; return 1 esac if [ ! -c /dev/mtd0 ]; then - echo UNKNOWN; return 1 + echo "UNKNOWN"; return 1 fi if [ ! -f Makefile ]; then echo "No-version"; return 1 @@ -72,9 +72,9 @@ gnubee_defconfig() { case $mach in "" | defconfig ) # guess if mach=`gnubee_model`; then - echo Using config $mach + echo "I: Using config $mach" else - echo >&2 Cannot guess machine type + echo >&2 "W: Cannot guess machine type" mach= fi esac @@ -83,8 +83,8 @@ gnubee_defconfig() { cp "$GNUBEE_CONFIG_DIR/$mach" "$GNUBEE_KERNEL_OBJECTS/.config" make O="$GNUBEE_KERNEL_OBJECTS" oldconfig else - [ -n "$mach" ] && echo >&2 "Config file '$mach' does not exist" - echo >&2 "Please choose one of:" + [ -n "$mach" ] && echo >&2 "E: Config file '$mach' does not exist" + echo >&2 "E: Please choose one of:" ls -C "$GNUBEE_CONFIG_DIR" | sed 's/^/ /' >&2 exit 1 fi @@ -115,14 +115,14 @@ gnubee_chroot() { sed -n -e 's,^.*\(http://ftp.debian.org/[^"]*\)".*,\1,p'` case $URL in http:*_all.deb ) ;; - * ) echo >&2 ERROR cannot find deb file for debootstrap, sorry. + * ) echo >&2 "E: cannot find deb file for debootstrap" exit 1 esac rm -f $deb/* wget -O $deb/debootstrap.deb "$URL" - (cd $deb; deb_data debootstrap.deb ) + (cd $deb; deb_data debootstrap.deb) if [ ! -f $deb/usr/sbin/debootstrap ]; then - echo >&2 ERROR failed to download debootstrap package + echo >&2 "E: failed to download debootstrap package" exit 1 fi fi @@ -144,9 +144,9 @@ gnubee_chroot() { if [ ! -e $deb/chroot/debootstrap/debpaths ]; then return 1 fi - for i in $(echo $EXTRA | tr , ' '); do + for i in $(echo "$EXTRA" | tr , ' '); do (cd $deb/chroot - echo I: Extracting $i... + echo "I: Extracting $i..." p=$(awk -v p=$i '$1 == p {print $2}' debootstrap/debpaths) deb_data ./$p ) @@ -158,18 +158,18 @@ gnubee_initramfs() { i=${GNUBEE_INITRAMFS_TREE} mkdir -p "$i" case `realpath "$i"` in - / ) echo >&2 Cannot use root as initramfs tree.; exit 1 + / ) echo >&2 "E: Cannot use root as initramfs tree"; exit 1 esac rm -rf "$i"; mkdir "$i" mkdir -p "$GNUBEE_KERNEL_OBJECTS" $gbtools/scripts/mkinitramfs "$i" "$GNUBEE_KERNEL_OBJECTS/initramfs-files.txt" - echo "initramfs: $(du -sh "$i")" + echo "I: initramfs: $(du -sh "$i")" } gnubee_modules() { case `realpath $GNUBEE_INITRAMFS_TREE/lib` in - /lib ) echo >&2 Cannot use root as initramfs tree.; exit 1 + /lib ) echo >&2 "E: Cannot use root as initramfs tree"; exit 1 esac rm -rf "$GNUBEE_INITRAMFS_TREE"/lib/modules/[1-9]* make O="$GNUBEE_KERNEL_OBJECTS" \ @@ -185,7 +185,7 @@ gnubee_modules() { gnubee_headers() { case `realpath $GNUBEE_BUILD_DIR/` in - /usr | / ) echo >&2 Cannot build to /usr or /.; exit 1 + /usr | / ) echo >&2 "E: Cannot build to /usr or /"; exit 1 esac rm -rf "$GNUBEE_BUILD_DIR/include" make O="$GNUBEE_KERNEL_OBJECTS" INSTALL_HDR_PATH="$GNUBEE_BUILD_DIR" \ @@ -220,7 +220,7 @@ gnubee_extern() { gnubee_bin() { if [ ! -f "$GNUBEE_INITRAMFS_TREE/init" ]; then - echo >&2 "Please run 'gbmake initramfs' first" + echo >&2 "E: Please run 'gbmake initramfs' first" exit 1 fi make O="$GNUBEE_KERNEL_OBJECTS" uImage.bin || exit 1 @@ -282,7 +282,7 @@ case $1 in * ) if [ ! -f "$GNUBEE_KERNEL_OBJECTS/.config" ]; then - echo >&2 Please run gbmake defconfig_XX first + echo >&2 "E: Please run gbmake defconfig_XX first" exit 1 fi make O="$GNUBEE_KERNEL_OBJECTS" ${1+"$@"} From d37574b5f7fdf7129f86523faffd01af8432ea3e Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Fri, 10 Jan 2020 20:50:54 +0100 Subject: [PATCH 02/13] gbmake: quote variables where possible --- scripts/gbmake | 134 ++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/scripts/gbmake b/scripts/gbmake index 3a356f7..a75c37b 100755 --- a/scripts/gbmake +++ b/scripts/gbmake @@ -8,10 +8,10 @@ # It should be run in the root of a Linux kernel source tree. -prog=$0 -if [ -L "$prog" ]; then prog=`readlink $prog`; fi -gbtools=`realpath $(dirname $prog)/..` -[ -f $gbtools/config ] && . $gbtools/config +prog="$0" +if [ -L "$prog" ]; then prog="`readlink $prog`"; fi +gbtools="`realpath "$(dirname "$prog")/.."`" +[ -f "$gbtools/config" ] && . "$gbtools/config" : kernel build object are placed in ${GNUBEE_KERNEL_OBJECTS:=O} : initramfs is placed in ${GNUBEE_INITRAMFS_TREE:=initramfs} @@ -19,13 +19,13 @@ gbtools=`realpath $(dirname $prog)/..` : headers are installed into ${GNUBEE_BUILD_DIR:=$gbtools/build} : cross compuler used is ${CROSS_COMPILE} -mkdir -p ${GNUBEE_KERNEL_OBJECTS} +mkdir -p "${GNUBEE_KERNEL_OBJECTS}" -export ARCH=mips CROSS_COMPILE +export CROSS_COMPILE ARCH="mips" export GNUBEE_KERNEL_OBJECTS GNUBEE_INITRAMFS_TREE GNUBEE_BUILD_DIR -export GNUBEE_INITRAMFS_PATH=`realpath $GNUBEE_INITRAMFS_TREE` -touch $GNUBEE_KERNEL_OBJECTS/initramfs-files.txt -export GNUBEE_INITRAMFS_FILES=`realpath $GNUBEE_KERNEL_OBJECTS/initramfs-files.txt` +export GNUBEE_INITRAMFS_PATH="`realpath "$GNUBEE_INITRAMFS_TREE"`" +touch "$GNUBEE_KERNEL_OBJECTS/initramfs-files.txt" +export GNUBEE_INITRAMFS_FILES="`realpath "$GNUBEE_KERNEL_OBJECTS/initramfs-files.txt"`" export GNUBEE_SSH_HOST help() { cat << END @@ -48,30 +48,30 @@ END } gnubee_model() { - case `grep machine /proc/cpuinfo` in - *GB-PC1* ) prefix=gbpc1;; - *GB-PC2* ) prefix=gbpc2;; + case "`grep machine /proc/cpuinfo`" in + *GB-PC1* ) prefix="gbpc1";; + *GB-PC2* ) prefix="gbpc2";; * ) echo "UNKNOWN"; return 1 esac - if [ ! -c /dev/mtd0 ]; then + if [ ! -c "/dev/mtd0" ]; then echo "UNKNOWN"; return 1 fi - if [ ! -f Makefile ]; then + if [ ! -f "Makefile" ]; then echo "No-version"; return 1 fi - vers=`awk '$1=="VERSION" {v=$3} $1=="PATCHLEVEL" {p=$3} END {printf "%d.%d\n",v,p}' Makefile` + vers="`awk '$1=="VERSION" {v=$3} $1=="PATCHLEVEL" {p=$3} END {printf "%d.%d\n",v,p}' Makefile`" echo "$prefix-$vers" return 0 } gnubee_defconfig() { - mach=${1#defconfig_} - mach=${mach#defconfig-} - mach=${mach%-defconfig} - mach=${mach%_defconfig} - case $mach in + mach="${1#defconfig_}" + mach="${mach#defconfig-}" + mach="${mach%-defconfig}" + mach="${mach%_defconfig}" + case "$mach" in "" | defconfig ) # guess - if mach=`gnubee_model`; then + if mach="`gnubee_model`"; then echo "I: Using config $mach" else echo >&2 "W: Cannot guess machine type" @@ -83,7 +83,7 @@ gnubee_defconfig() { cp "$GNUBEE_CONFIG_DIR/$mach" "$GNUBEE_KERNEL_OBJECTS/.config" make O="$GNUBEE_KERNEL_OBJECTS" oldconfig else - [ -n "$mach" ] && echo >&2 "E: Config file '$mach' does not exist" + [ -n "$mach" ] && echo >&2 "E: Config file \"$mach\" does not exist" echo >&2 "E: Please choose one of:" ls -C "$GNUBEE_CONFIG_DIR" | sed 's/^/ /' >&2 exit 1 @@ -94,12 +94,12 @@ gnubee_defconfig() { } deb_data() { - case `ar t $1` in + case "`ar t $1`" in *data.tar.gz* ) - ar p $1 data.tar.gz | tar xzf - + ar p "$1" data.tar.gz | tar xzf - ;; *data.tar.xz* ) - ar p $1 data.tar.xz | tar xJf - + ar p "$1" data.tar.xz | tar xJf - ;; esac } @@ -107,73 +107,73 @@ deb_data() { gnubee_chroot() { # use debootstrap to make a chroot with mipsel debian # so we can copy some files into our initramfs - deb=$GNUBEE_BUILD_DIR/deb - mkdir -p $deb - if [ ! -f $deb/usr/sbin/debootstrap ]; then + deb="$GNUBEE_BUILD_DIR/deb" + mkdir -p "$deb" + if [ ! -f "$deb/usr/sbin/debootstrap" ]; then # need to get debootstrap - URL=`wget 2> /dev/null -O - https://packages.debian.org/stable/all/debootstrap/download | - sed -n -e 's,^.*\(http://ftp.debian.org/[^"]*\)".*,\1,p'` - case $URL in + URL="`wget 2> /dev/null -O - https://packages.debian.org/stable/all/debootstrap/download |\ + sed -n -e 's,^.*\(http://ftp.debian.org/[^"]*\)".*,\1,p'`" + case "$URL" in http:*_all.deb ) ;; * ) echo >&2 "E: cannot find deb file for debootstrap" exit 1 esac - rm -f $deb/* - wget -O $deb/debootstrap.deb "$URL" - (cd $deb; deb_data debootstrap.deb) - if [ ! -f $deb/usr/sbin/debootstrap ]; then + rm -f "$deb/"* + wget -O "$deb/debootstrap.deb" "$URL" + (cd "$deb"; deb_data debootstrap.deb) + if [ ! -f "$deb/usr/sbin/debootstrap" ]; then echo >&2 "E: failed to download debootstrap package" exit 1 fi fi - rm -rf $deb/chroot $deb/bin - mkdir $deb/chroot - mkdir -p $deb/bin - echo "echo 0" > $deb/bin/id - ln -s /bin/true $deb/bin/mknod - chmod +x $deb/bin/id - EXTRA=debootstrap,ntpdate,wget,busybox,mdadm,dropbear-bin,cryptsetup-bin,mtd-utils,u-boot-tools,xfsprogs,btrfs-progs,lvm2,evtest,perl - EXTRA=$EXTRA,libdevmapper-event1.02.1,libdevmapper1.02.1,libpcre2-8-0,libreadline5 - EXTRA=$EXTRA,libtomcrypt1,libcryptsetup12,libssl1.1,libpopt0,libtommath1,libaio1,libpsl5,libtinfo6,libunistring2,libgmp10 - EXTRA=$EXTRA,libargon2-1,liblzma5,libp11-kit0,libjson-c3,liblz4-1,libtasn1-6,libhogweed4,libffi6,liblzo2-2 - PATH=$deb/bin:$PATH DEBOOTSTRAP_DIR=$deb/usr/share/debootstrap/ \ - $deb/usr/sbin/debootstrap --keep-debootstrap-dir --arch=mipsel --foreign \ - --include=$EXTRA \ - stable $deb/chroot/ http://deb.debian.org/debian/ - if [ ! -e $deb/chroot/debootstrap/debpaths ]; then + rm -rf "$deb/chroot" "$deb/bin" + mkdir "$deb/chroot" + mkdir -p "$deb/bin" + echo "echo 0" > "$deb/bin/id" + ln -s /bin/true "$deb/bin/mknod" + chmod +x "$deb/bin/id" + EXTRA="debootstrap,ntpdate,wget,busybox,mdadm,dropbear-bin,cryptsetup-bin,mtd-utils,u-boot-tools,xfsprogs,btrfs-progs,lvm2,evtest,perl" + EXTRA="$EXTRA,libdevmapper-event1.02.1,libdevmapper1.02.1,libpcre2-8-0,libreadline5" + EXTRA="$EXTRA,libtomcrypt1,libcryptsetup12,libssl1.1,libpopt0,libtommath1,libaio1,libpsl5,libtinfo6,libunistring2,libgmp10" + EXTRA="$EXTRA,libargon2-1,liblzma5,libp11-kit0,libjson-c3,liblz4-1,libtasn1-6,libhogweed4,libffi6,liblzo2-2" + PATH="$deb/bin:$PATH" DEBOOTSTRAP_DIR="$deb/usr/share/debootstrap/" \ + "$deb/usr/sbin/debootstrap" --keep-debootstrap-dir --arch=mipsel --foreign \ + --include="$EXTRA" \ + stable "$deb/chroot/" http://deb.debian.org/debian/ + if [ ! -e "$deb/chroot/debootstrap/debpaths" ]; then return 1 fi for i in $(echo "$EXTRA" | tr , ' '); do - (cd $deb/chroot + (cd "$deb/chroot" echo "I: Extracting $i..." - p=$(awk -v p=$i '$1 == p {print $2}' debootstrap/debpaths) - deb_data ./$p + p="$(awk -v p="$i" '$1 == p {print $2}' debootstrap/debpaths)" + deb_data "./$p" ) done return 0 } gnubee_initramfs() { - i=${GNUBEE_INITRAMFS_TREE} + i="${GNUBEE_INITRAMFS_TREE}" mkdir -p "$i" - case `realpath "$i"` in + case "`realpath "$i"`" in / ) echo >&2 "E: Cannot use root as initramfs tree"; exit 1 esac rm -rf "$i"; mkdir "$i" mkdir -p "$GNUBEE_KERNEL_OBJECTS" - $gbtools/scripts/mkinitramfs "$i" "$GNUBEE_KERNEL_OBJECTS/initramfs-files.txt" + "$gbtools/scripts/mkinitramfs" "$i" "$GNUBEE_KERNEL_OBJECTS/initramfs-files.txt" echo "I: initramfs: $(du -sh "$i")" } gnubee_modules() { - case `realpath $GNUBEE_INITRAMFS_TREE/lib` in + case "`realpath "$GNUBEE_INITRAMFS_TREE/lib"`" in /lib ) echo >&2 "E: Cannot use root as initramfs tree"; exit 1 esac rm -rf "$GNUBEE_INITRAMFS_TREE"/lib/modules/[1-9]* make O="$GNUBEE_KERNEL_OBJECTS" \ - INSTALL_MOD_PATH="`realpath $GNUBEE_INITRAMFS_TREE`" \ + INSTALL_MOD_PATH="`realpath "$GNUBEE_INITRAMFS_TREE"`" \ INSTALL_MOD_STRIP=1 \ modules modules_install rm -f "$GNUBEE_INITRAMFS_TREE"/lib/modules/[1-9]*/{build,source} @@ -184,7 +184,7 @@ gnubee_modules() { } gnubee_headers() { - case `realpath $GNUBEE_BUILD_DIR/` in + case "`realpath "$GNUBEE_BUILD_DIR/"`" in /usr | / ) echo >&2 "E: Cannot build to /usr or /"; exit 1 esac rm -rf "$GNUBEE_BUILD_DIR/include" @@ -234,16 +234,16 @@ gnubee_bin() { then ls -lh /srv/tftpboot/GB-PCx_uboot.bin fi - V=`sed -n 's/^VERSION = //p' Makefile` - P=`sed -n 's/^PATCHLEVEL = //p' Makefile` - S=`sed -n 's/^SUBLEVEL = //p' Makefile` - X=`sed -n 's/^EXTRAVERSION = //p' Makefile` + V="`sed -n 's/^VERSION = //p' Makefile`" + P="`sed -n 's/^PATCHLEVEL = //p' Makefile`" + S="`sed -n 's/^SUBLEVEL = //p' Makefile`" + X="`sed -n 's/^EXTRAVERSION = //p' Makefile`" if [ " $S" = " " -o " $S" = " 0" ]; then vers="$V.$P$X" else vers="$V.$P.$S$X" fi - if grep "^CONFIG_DTB_GNUBEE2=y" $GNUBEE_KERNEL_OBJECTS/.config > /dev/null; then + if grep "^CONFIG_DTB_GNUBEE2=y" "$GNUBEE_KERNEL_OBJECTS/.config" > /dev/null; then suffix="${vers}-gbpc2" else if grep "^CONFIG_DTB_GNUBEE1_UBOOT=y" $GNUBEE_KERNEL_OBJECTS/.config > /dev/null; then @@ -269,15 +269,15 @@ gnubee_firmware() { } -case $1 in - defconfig* | *defconfig ) gnubee_defconfig $1;; +case "$1" in + defconfig* | *defconfig ) gnubee_defconfig "$1";; chroot ) gnubee_chroot ;; initramfs ) gnubee_initramfs;; modules ) gnubee_modules ;; headers ) gnubee_headers ;; extern ) gnubee_extern ;; gnubee.bin ) gnubee_bin ;; - firmware ) gnubee_firmware $2 $3;; + firmware ) gnubee_firmware "$2" "$3";; help | -h ) help ;; * ) From 61c54ea7cb221d200b39c9ded9ff724513818e24 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Fri, 10 Jan 2020 22:06:32 +0100 Subject: [PATCH 03/13] gbmake: make debian suite and mirror configurable --- config.sample | 38 +++++++++++++++++++++++++++----------- scripts/gbmake | 12 +++++++----- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/config.sample b/config.sample index 99381da..23309ca 100644 --- a/config.sample +++ b/config.sample @@ -1,19 +1,35 @@ # SPDX-License-Identifier: GPL-2.0 # -# This is check code sourced by gbmake - -# Directory for kernel build objects, usually a relative path -# GNUBEE_KERNEL_OBJECTS=O -# Directory for initramfs, can be relative -# GNUBEE_INITRAMFS_TREE:=initramfs -# Directory containing kernel config files - 'kern_config' in parent of script by default -# GNUBEE_CONFIG_DIR= -# Directory where user-space tools are built - usually ../build from script dir -# GNUBEE_BUILD_DIR= -# Cross compiler - full path such as /opt/cross/bin/mipsel-unknown-linux-gnu- +# This file is sourced by gbmake +# +# $gbtools can be used as the path to the root of the gnubee-tools repository +# all paths can be relative to the directory the script is executed in + +# Debian suite to use for debootstrap, will be used to build initramfs +# May be a release code name (e.g. stretch, buster, sid) or a symbolic name (eg, unstable, testing, stable, oldstable) +# GNUBEE_DEBIAN_SUITE="stable" + +# Debian mirror to use for debootstrap, will be used to build initramfs +# GNUBEE_DEBIAN_MIRROR="http://deb.debian.org/debian/" + +# Directory for kernel build objects +# GNUBEE_KERNEL_OBJECTS="O" + +# Directory for initramfs +# GNUBEE_INITRAMFS_TREE="initramfs" + +# Directory containing kernel config files +# GNUBEE_CONFIG_DIR="$gbtools/kern_config" + +# Directory where user-space tools are built +# GNUBEE_BUILD_DIR="$gbtools/build" + +# Path to cross compiler - must be in $PATH or full path such as /opt/cross/bin/mipsel-unknown-linux-gnu- # CROSS_COMPILE= + # GnuBee running Debian from which various programs can be copied. # GNUBEE_SSH_HOST=root@192.168.1.111 + # To include extenal modules in the image, list them here. Just the name # of the directory may suffice if the Makefile there follows normal conventions. # Otherwise provide a script that will build the module and install it. diff --git a/scripts/gbmake b/scripts/gbmake index a75c37b..485e4ce 100755 --- a/scripts/gbmake +++ b/scripts/gbmake @@ -13,10 +13,12 @@ if [ -L "$prog" ]; then prog="`readlink $prog`"; fi gbtools="`realpath "$(dirname "$prog")/.."`" [ -f "$gbtools/config" ] && . "$gbtools/config" -: kernel build object are placed in ${GNUBEE_KERNEL_OBJECTS:=O} -: initramfs is placed in ${GNUBEE_INITRAMFS_TREE:=initramfs} -: default kernel config files are found in ${GNUBEE_CONFIG_DIR:=$gbtools/kern_config} -: headers are installed into ${GNUBEE_BUILD_DIR:=$gbtools/build} +: debian suite is ${GNUBEE_DEBIAN_SUITE:="stable"} +: debian mirror is ${GNUBEE_DEBIAN_MIRROR:="http://deb.debian.org/debian/"} +: kernel build object are placed in ${GNUBEE_KERNEL_OBJECTS:="O"} +: initramfs is placed in ${GNUBEE_INITRAMFS_TREE:="initramfs"} +: default kernel config files are found in ${GNUBEE_CONFIG_DIR:="$gbtools/kern_config"} +: headers are installed into ${GNUBEE_BUILD_DIR:="$gbtools/build"} : cross compuler used is ${CROSS_COMPILE} mkdir -p "${GNUBEE_KERNEL_OBJECTS}" @@ -140,7 +142,7 @@ gnubee_chroot() { PATH="$deb/bin:$PATH" DEBOOTSTRAP_DIR="$deb/usr/share/debootstrap/" \ "$deb/usr/sbin/debootstrap" --keep-debootstrap-dir --arch=mipsel --foreign \ --include="$EXTRA" \ - stable "$deb/chroot/" http://deb.debian.org/debian/ + "$GNUBEE_DEBIAN_SUITE" "$deb/chroot/" "$GNUBEE_DEBIAN_MIRROR" if [ ! -e "$deb/chroot/debootstrap/debpaths" ]; then return 1 fi From b522ea3c454db2932d1370a96dc3f6020db4cfcb Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Fri, 10 Jan 2020 22:42:21 +0100 Subject: [PATCH 04/13] mkinitramfs: quote variables where possible --- scripts/mkinitramfs | 122 ++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/scripts/mkinitramfs b/scripts/mkinitramfs index 2da7fd8..4a7938f 100755 --- a/scripts/mkinitramfs +++ b/scripts/mkinitramfs @@ -15,7 +15,7 @@ usage() { } case $# in - 2 ) dir=$1 devlist=$2 + 2 ) dir="$1" devlist="$2" if [ ! -d "$dir" ]; then echo >&2 "$dir does not exist". usage @@ -24,16 +24,16 @@ case $# in * ) usage esac -case `uname -m` in +case "`uname -m`" in mips ) remote=eval ;; * ) if [ -d "$GNUBEE_BUILD_DIR/deb/chroot" ]; then remote=from_chroot elif [ -n "$GNUBEE_SSH_HOST" ]; then remote="ssh -x $GNUBEE_SSH_HOST" - case `$remote uname -m` in + case "`$remote uname -m`" in mips ) : success ;; - * ) echo >&2 $GNUBEE_SSH_HOST no accessible or not mips; exit 1 + * ) echo >&2 "E: $GNUBEE_SSH_HOST not accessible or not mips"; exit 1 esac else echo 'Please run on a gnubee, or provide GNUBEE_SSH_HOST, or "gbmake chroot"' @@ -42,27 +42,27 @@ case `uname -m` in esac from_chroot() { - ch=$GNUBEE_BUILD_DIR/deb/chroot + ch="$GNUBEE_BUILD_DIR/deb/chroot" if [ "$2" = "which" ]; then - for p in /sbin /usr/sbin `echo $PATH | tr : ' '`; do - if [ -f $ch/$p/$3 ]; then - echo $p/$3 + for p in /sbin /usr/sbin `echo "$PATH" | tr : ' '`; do + if [ -f "$ch/$p/$3" ]; then + echo "$p/$3" return fi done return fi - if [ "$1" = cat ]; then + if [ "$1" = "cat" ]; then if [ -e "$ch$2" ]; then - cat $ch$2 + cat "$ch$2" else - cat $ch/usr$2 + cat "$ch/usr$2" fi return fi - if [ "$1" = ldd ]; then - strings - $ch$2 | grep -a '\.so\.[0-9.]*$' | while read b; do + if [ "$1" = "ldd" ]; then + strings - "$ch$2" | grep -a '\.so\.[0-9.]*$' | while read b; do for d in /lib/mipsel-linux-gnu /usr/lib/mipsel-linux-gnu ; do if [ -f "$ch$d/$b" ]; then echo " stuff $d/$b (mess)"; fi done @@ -73,8 +73,8 @@ from_chroot() { # do this after boot return fi - if [ "$1" = dropbearkey ]; then - a=`which 2> /dev/null dropbearkey` + if [ "$1" = "dropbearkey" ]; then + a="`which 2> /dev/null dropbearkey`" if [ -n "$a" ]; then eval "$*" fi @@ -82,26 +82,26 @@ from_chroot() { fi case "$*" in *debootstrap* ) - (cd $ch ; tar 2> /dev/null cf - usr/sbin/debootstrap usr/share/debootstrap usr/share/keyrings/debian-archive-keyring.gpg ) + (cd "$ch" ; tar 2> /dev/null cf - usr/sbin/debootstrap usr/share/debootstrap usr/share/keyrings/debian-archive-keyring.gpg ) ;; *lib/terminfo*echo* ) - ( cd $ch; [ -d lib/terminfo ] && echo lib || echo usr/lib ) + ( cd "$ch"; [ -d lib/terminfo ] && echo "lib" || echo "usr/lib" ) ;; - * ) echo >&2 ERROR cannot run command with chroot: "$*" + * ) echo >&2 "E: cannot run command with chroot: $*" exit 1 esac } -prog=$0 -if [ -L "$prog" ]; then prog=`readlink $prog`; fi -gbtools=`realpath $(dirname $prog)/..` +prog="$0" +if [ -L "$prog" ]; then prog="`readlink $prog`"; fi +gbtools="`realpath "$(dirname "$prog")/.."`" if [ ! -d "$gbtools/scripts" ]; then - echo >&2 Cannot find scripts in $gbtools + echo >&2 "Cannot find $gbtools/scripts" exit 1 fi -cat > $devlist <<- END +cat > "$devlist" <<- END dir /dev 755 0 0 nod /dev/console 600 0 0 c 5 1 nod /dev/null 666 0 0 c 1 3 @@ -125,12 +125,12 @@ if grep CONFIG_DTB_GNUBEE._UBOOT=y "$GNUBEE_KERNEL_OBJECTS/.config" > /dev/null add_optional=no fi -builddir=`realpath "$GNUBEE_BUILD_DIR"` -cd $dir +builddir="`realpath "$GNUBEE_BUILD_DIR"`" +cd "$dir" -case `realpath .` in +case "`realpath .`" in /*/*/* ) : ok ;; - * ) echo >&@ Refusing to create initramfs at such a high-level directory: `realpath .` + * ) echo >&@ "Refusing to create initramfs at such a high-level directory: `realpath .`" exit 2; esac @@ -143,12 +143,12 @@ ln -s ../lib usr/lib ln -s /proc/mounts etc/mtab # Debian moved some things from /lib to /usr/lib... makes life awkward. -# terminfo is each to check.. -LIB=$($remote 'cd /; [ -d lib/terminfo ] && echo lib || echo usr/lib') +# terminfo is easy to check.. +LIB="$($remote 'cd /; [ -d lib/terminfo ] && echo lib || echo usr/lib')" # cfdisk need terminfo - provide one. mkdir -p etc/terminfo/l -$remote cat /$LIB/terminfo/l/linux > etc/terminfo/l/linux +$remote cat "/$LIB/terminfo/l/linux" > etc/terminfo/l/linux # lvm need to know not to use udev # cribbed from https://wiki.gentoo.org/wiki/Custom_Initramfs#LVM @@ -184,14 +184,14 @@ fi # install essential binaries for b in busybox findfs; do - path=`$remote PATH=/bin:/sbin:/usr/sbin:$PATH which $b 2> /dev/null` + path="`$remote "PATH=/bin:/sbin:/usr/sbin:$PATH" which "$b" 2> /dev/null`" if [ -z "$path" ]; then echo "Please install $b" exit 1; fi echo "## Install $path" - b=`basename $path` - $remote cat "$path" > bin/$b ; chmod 755 bin/$b + b="`basename "$path"`" + $remote cat "$path" > "bin/$b" ; chmod 755 "bin/$b" libs="$libs `$remote ldd "$path" |sed -n -e 's,^.*[ ]\(/[^ ]*\) (.*$,\1,p'`" done @@ -212,10 +212,10 @@ if [ -f bin/debootstrap ]; then debootstrap_bins="$debootstrap_bins perl" else echo "## building pkgdetails" - T=/tmp/.gb-built-$$ - (cd $gbtools/pkgdetails; tar cf - . ) | - $remote "rm -rf $T ; mkdir -p $T; cd $T; tar xf -; cc -Wno-implicit-function-declaration -o pkgdetails pkgdetails.c" - $remote cat $T/pkgdetails > usr/share/debootstrap/pkgdetails + T="/tmp/.gb-built-$$" + (cd "$gbtools/pkgdetails"; tar cf - . ) | + $remote "rm -rf "$T" ; mkdir -p "$T"; cd "$T"; tar xf -; cc -Wno-implicit-function-declaration -o pkgdetails pkgdetails.c" + $remote cat "$T/pkgdetails" > usr/share/debootstrap/pkgdetails chmod 755 usr/share/debootstrap/pkgdetails fi else @@ -225,13 +225,13 @@ fi for b in blkid cryptsetup mdadm dropbear dbclient fsck.ext4 xfs_repair lvm \ btrfs \ evtest ntpdate passwd $debootstrap_bins ; do - path=`$remote PATH=/sbin:/usr/sbin:$PATH which $b 2> /dev/null` + path="`$remote "PATH=/sbin:/usr/sbin:$PATH" which "$b" 2> /dev/null`" if [ -z "$path" ]; then echo $"$b not found, not installing" else echo "## Install $path" - b=`basename $path` - $remote cat "$path" > bin/$b ; chmod 755 bin/$b + b="`basename $path`" + $remote cat "$path" > "bin/$b" ; chmod 755 "bin/$b" libs="$libs `$remote ldd "$path" |sed -n -e 's,^.*[ ]\(/[^ ]*\) (.*$,\1,p'`" fi done @@ -240,16 +240,16 @@ if [ -f bin/dropbear ]; then # dropbear needs some extra configuration echo '## configuring dropbear' mkdir etc/dropbear - if [ ! -f $builddir/dropbear/dropbear_ecdsa_host_key ]; then - mkdir -p $builddir/dropbear + if [ ! -f "$builddir/dropbear/dropbear_ecdsa_host_key" ]; then + mkdir -p "$builddir/dropbear" $remote "dropbearkey -t ecdsa -f /tmp/drop$$.key >&2; - cat /tmp/drop$$.key" > $builddir/dropbear/dropbear_ecdsa_host_key + cat /tmp/drop$$.key" > "$builddir/dropbear/dropbear_ecdsa_host_key" fi - cp $builddir/dropbear/* etc/dropbear - echo passwd: files >> etc/nsswitch.conf - echo group: files >> etc/nsswitch.conf - echo > etc/passwd root:x:0:0:root:/:/bin/sh - echo > etc/group root:x:0: + cp "$builddir/dropbear/*" etc/dropbear + echo "passwd: files" >> etc/nsswitch.conf + echo "group: files" >> etc/nsswitch.conf + echo "root:x:0:0:root:/:/bin/sh" > etc/passwd + echo "root:x:0:" > etc/group echo 'PATH=.:/bin; export PATH' > etc/profile # root password is GnuBee echo > etc/shadow 'root:$6$l80E2NNrh32z0N$HX1nhaV6TcdM3OcMm8PVyzQZAl6c9Q2GYugdS9MuDcG6NkxW3eLCvLbw05tMKkbyRlieU1QeZ4tngZpaS9Fnf0::0:99999:7:::' @@ -257,26 +257,26 @@ if [ -f bin/dropbear ]; then if [ -f bin/passwd ]; then # need pam modules too echo 'passwd password required pam_unix.so sha512' > etc/pam.conf - mkdir -p $LIB/mipsel-linux-gnu/security - path=$LIB/mipsel-linux-gnu/security/pam_unix.so - $remote cat /$path > $path + mkdir -p "$LIB/mipsel-linux-gnu/security" + path="$LIB/mipsel-linux-gnu/security/pam_unix.so" + $remote cat "/$path" > "$path" libs="$libs `$remote ldd "/$path" |sed -n -e 's,^.*[ ]\(/[^ ]*\) (.*$,\1,p'`" fi fi # ntpdate need to find the ntp service -echo ntp 123/tcp >> etc/services -echo ntp 123/udp >> etc/services +echo "ntp 123/tcp" >> etc/services +echo "ntp 123/udp" >> etc/services fi # Now make sure we have all the libraries we need. for lib in $libs; do - if [ ! -f .$lib ]; then - echo '## Adding' $lib - mkdir -p `dirname .$lib` - $remote cat $lib > .$lib ; chmod +x .$lib + if [ ! -f ".$lib" ]; then + echo "## Adding $lib" + mkdir -p "`dirname ".$lib"`" + $remote cat "$lib" > ".$lib" ; chmod +x ".$lib" fi done @@ -284,12 +284,12 @@ done $remote busybox --list | while read a; do - [ -e bin/$a ] || ln -s busybox bin/$a + [ -e "bin/$a" ] || ln -s busybox "bin/$a" done # copy in some scripts -for i in $gbtools/initramfs/* +for i in "$gbtools"/initramfs/* do - echo "## Install script `basename $i`" - cp -r $i . + echo "## Install script `basename "$i"`" + cp -r "$i" . done From 0212baa23e7c56c84ded0d6784a46624412f5572 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sat, 11 Jan 2020 15:15:21 +0100 Subject: [PATCH 05/13] mkinitrams: make dropbearkey work in chroot --- scripts/mkinitramfs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/mkinitramfs b/scripts/mkinitramfs index 4a7938f..d96786f 100755 --- a/scripts/mkinitramfs +++ b/scripts/mkinitramfs @@ -73,13 +73,6 @@ from_chroot() { # do this after boot return fi - if [ "$1" = "dropbearkey" ]; then - a="`which 2> /dev/null dropbearkey`" - if [ -n "$a" ]; then - eval "$*" - fi - return - fi case "$*" in *debootstrap* ) (cd "$ch" ; tar 2> /dev/null cf - usr/sbin/debootstrap usr/share/debootstrap usr/share/keyrings/debian-archive-keyring.gpg ) @@ -87,6 +80,13 @@ from_chroot() { *lib/terminfo*echo* ) ( cd "$ch"; [ -d lib/terminfo ] && echo "lib" || echo "usr/lib" ) ;; + dropbearkey* ) + a="`which 2> /dev/null dropbearkey`" + if [ -n "$a" ]; then + eval "$*" + fi + return + ;; * ) echo >&2 "E: cannot run command with chroot: $*" exit 1 esac From 0b60b88d9a5c6b7fdf700ae6308550bed79dd3a3 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sat, 11 Jan 2020 17:14:49 +0100 Subject: [PATCH 06/13] init: add ability to set led trigger --- initramfs/init | 3 +++ 1 file changed, 3 insertions(+) diff --git a/initramfs/init b/initramfs/init index 447203d..8c6ee54 100755 --- a/initramfs/init +++ b/initramfs/init @@ -14,6 +14,9 @@ set_led() { echo $2 > delay_on echo $3 > delay_off ;; + 2 ) + echo $2 > trigger + ;; * ) echo none > trigger esac ) From f00a71da03200b363a49507a04241afeacfb360e Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sat, 11 Jan 2020 17:32:43 +0100 Subject: [PATCH 07/13] init: set status led to fast blink before switch_root --- initramfs/init | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initramfs/init b/initramfs/init index 8c6ee54..65645cf 100755 --- a/initramfs/init +++ b/initramfs/init @@ -108,7 +108,7 @@ gnubee_switch_root(){ done set_led system - set_led status + set_led status 100 100 umount -l /proc /sys /dev/pts /dev exec switch_root /mnt/root /sbin/init From ad2b231792e9d88c5d6df3ecb1d0bdc21568834f Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sun, 12 Jan 2020 21:31:08 +0100 Subject: [PATCH 08/13] initramfs/config: add udev rule for LEDs to target --- initramfs/config | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/initramfs/config b/initramfs/config index 78d652d..a6ad645 100755 --- a/initramfs/config +++ b/initramfs/config @@ -11,6 +11,7 @@ # hostname # ssh PermitRootLogin # label filesystem +# add leds udev rule mp=/tmp/newroot @@ -199,6 +200,13 @@ fixup_modules() { cp -a /lib/modules/. $mp/lib/modules } +fixup_leds() { + cat > "$mp/lib/udev/rules.d/70-gnubee-leds.rules" <<- "EOF" + ACTION=="add", SUBSYSTEM=="leds", KERNEL=="gb-pc1:green:status", ATTR{trigger}="default-on" + ACTION=="add", SUBSYSTEM=="leds", KERNEL=="gb-pc1:green:system", ATTR{trigger}="activity" + EOF +} + fixup() { hostname > $mp/etc/hostname root=`grep '^root:' /etc/shadow` @@ -211,6 +219,7 @@ fixup() { esac fixup_net fixup_modules + fixup_leds return 0 } From 846a1ce32c38a9e9bc8249d651883705d508c9bb Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sun, 12 Jan 2020 21:40:32 +0100 Subject: [PATCH 09/13] initramfs/config: ask about which Debian suite and mirror to use --- initramfs/config | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/initramfs/config b/initramfs/config index a6ad645..a24dbd5 100755 --- a/initramfs/config +++ b/initramfs/config @@ -14,6 +14,8 @@ # add leds udev rule mp=/tmp/newroot +default_debian_suite=buster +default_debian_mirror="http://httpredir.debian.org/debian" set_host() { case `hostname` in @@ -32,6 +34,33 @@ set_host() { return 1 } +set_debian() { + while [ -z "$debian_suite" ]; do + read -p "Which Debian suite do you want to install? (jessie | stretch | buster | bullseye | sid) [buster] " debian_suite + case "$debian_suite" in + "" ) debian_suite="$default_debian_suite" ;; + jessie | stretch | buster | bullseye | sid ) ;; + * ) echo "Unknown debian suite. Please enter one of the above choices." + esac + done + while [ -z "$debian_mirror" ]; do + read -p "Which Debian mirror do you want to use? [$default_debian_mirror]" debian_mirror + debian_mirror="${debian_mirror%/}" + case "$debian_mirror" in + "" ) debian_mirror="$default_debian_mirror" ;; + * ) + echo -n "Validating mirror URL ... " + if wget -q --spider "$debian_mirror/debian/dists/$debian_suite/Release.gpg"; then + echo "OK." + else + echo "ERROR, please enter a valid Debian mirror URL." + debian_mirror= + fi + esac + done + +} + check_net() { net_dev=`ip route show match 0/0 | awk '$4 == "dev" {print $5 }'` if [ -z "$net_dev" ]; then @@ -146,7 +175,7 @@ bootstrap() { fi PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH export PATH - debootstrap --arch=mipsel --include=vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,libnl-3-200,libnl-genl-3-200 stretch $mp http://httpredir.debian.org/debian || exit 1 + debootstrap --arch=mipsel --include=vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,libnl-3-200,libnl-genl-3-200 "$debian_version" "$mp" "$debian_mirror" || exit 1 return 1 } @@ -223,7 +252,7 @@ fixup() { return 0 } -until set_host && set_passwd && set_net && set_time && create_filesystem && bootstrap && fixup; do +until set_host && set_debian && set_passwd && set_net && set_time && create_filesystem && bootstrap && fixup; do : done From ac15d86c616f7967b139929eca92ea4e86bd283d Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sun, 12 Jan 2020 21:44:22 +0100 Subject: [PATCH 10/13] initramfs/config: move debootstrap include packages to variable --- initramfs/config | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/initramfs/config b/initramfs/config index a24dbd5..ce10e8e 100755 --- a/initramfs/config +++ b/initramfs/config @@ -16,6 +16,7 @@ mp=/tmp/newroot default_debian_suite=buster default_debian_mirror="http://httpredir.debian.org/debian" +include_packages="vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,libnl-3-200,libnl-genl-3-200" set_host() { case `hostname` in @@ -175,7 +176,7 @@ bootstrap() { fi PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH export PATH - debootstrap --arch=mipsel --include=vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,libnl-3-200,libnl-genl-3-200 "$debian_version" "$mp" "$debian_mirror" || exit 1 + debootstrap --arch=mipsel --include="$include_packages" "$debian_version" "$mp" "$debian_mirror" || exit 1 return 1 } From ded9bb0226bdfa7d3e0771193650dfd2622adbd1 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sun, 12 Jan 2020 21:45:10 +0100 Subject: [PATCH 11/13] initramfs/config: remove unneeded libnl packages from debootstrap --- initramfs/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initramfs/config b/initramfs/config index ce10e8e..1c4581c 100755 --- a/initramfs/config +++ b/initramfs/config @@ -16,7 +16,7 @@ mp=/tmp/newroot default_debian_suite=buster default_debian_mirror="http://httpredir.debian.org/debian" -include_packages="vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,libnl-3-200,libnl-genl-3-200" +include_packages="vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan" set_host() { case `hostname` in From 1f6c0cb69c608dd356bfb1922d33311241aaf8c4 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Sun, 12 Jan 2020 21:47:35 +0100 Subject: [PATCH 12/13] initramfs/config: add some packages to include in target - dbus: fixes timedatectl bug - man-db, manpages, less, bash-completion: useful --- initramfs/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initramfs/config b/initramfs/config index 1c4581c..696eec3 100755 --- a/initramfs/config +++ b/initramfs/config @@ -16,7 +16,7 @@ mp=/tmp/newroot default_debian_suite=buster default_debian_mirror="http://httpredir.debian.org/debian" -include_packages="vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan" +include_packages="vim,openssh-server,ntpdate,cron,locales,udev,fake-hwclock,mtd-utils,ca-certificates,apt-transport-https,vlan,bash-completion,less,man-db,manpages,dbus" set_host() { case `hostname` in From b11648d51632d299504fad7ae7c2488252f2db63 Mon Sep 17 00:00:00 2001 From: Jernej Jakob Date: Thu, 18 Feb 2021 00:58:17 +0100 Subject: [PATCH 13/13] initramfs/init: replace dropbearkey with dropbear -R The initramfs didn't contain dropbearkey so host keys weren't generated. Use -R instead. Should be in all dropbear versions from jessie onward. --- initramfs/init | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/initramfs/init b/initramfs/init index 65645cf..b73329b 100755 --- a/initramfs/init +++ b/initramfs/init @@ -181,11 +181,9 @@ gnubee_boot(){ udhcpd_config ethblack 192.168.10 udhcpd /udhcpd.conf fi - if [ ! -s /etc/dropbear/dropbear_ecdsa_host_key ]; then - # build host didn't have dropbear, so make a new key on each boot - dropbearkey -t ecdsa -f /etc/dropbear/dropbear_ecdsa_host_key - fi - dropbear + + # -R to generate new host keys on each boot + dropbear -R run_shell /dev/ttyS0 fi