--- /dev/null
+#!/bin/bash
+#
+# Utility script to debootstrap a minbase filesystem, with additions
+# of kernel, busybox and an init script. Then make an initrd.gz from
+# that and prepare a boot image file with syslinux from that.
+#
+if [ $(id -u) -ne 0 ] ; then
+ exec sudo env http_proxy=$http_proxy $0 $*
+ echo "** Abort: must be root" >&2
+ exit 1
+fi
+set -e -x
+
+ARCH="${1-$(dpkg-architecture -q DEB_HOST_ARCH)}"
+DIST=${2-ceres}
+IMG=${3-boot-$DIST-$ARCH.img}
+FS=${4-FS}
+
+# Prepare or keep FS
+DEBSTRAP=true
+USRMERGE=true
+if [ -d "$FS" ] ; then
+ select x in "keep existing $FS and skip debootstrap" \
+ "remove existing $FS and debootstrap anew" \
+ "abort" ; do
+ [ "$x" = abort ] && exit 1
+ [ -z "$x" ] || break
+ done
+ echo "selection: $REPLY"
+ [ "$REPLY" = 1 ] && DEBSTRAP=false
+fi
+
+if $DEBSTRAP ; then
+ ## Optionally add the stupid-links
+ read -n 1 -p "prime $FS with stupid-links? [Yn]" x
+ [ "$x" = "n" ] && USRMERGE=false
+
+ rm -rf "$FS" # remove if existin
+
+ if $USRMERGE ; then
+ mkdir -p "$FS"/usr
+ for d in bin sbin lib ; do
+ mkdir "$FS"/usr/$d
+ ln -s usr/$d "$FS"/$d
+ done
+ fi
+
+ ## bootstrap a filesystem, with exclusions
+ echo "http_proxy=$http_proxy debootstrap ..."
+ debootstrap --exclude=logrotate,cron,cron-daemon-common \
+ --arch=$ARCH $DIST "$FS" http://deb.devuan.org/merged
+ chroot "$FS" apt-get install -y logrotate cron
+
+ ## Select and add a kernel
+ KERNELS=( $(chroot "$FS" apt-cache search linux-image-\* | \
+ sed 's/\s.*//' | sort -h ) )
+ echo "** Please select kernel **"
+ select KERNEL in "${KERNELS[@]}" ; do [ -n "$KERNEL" ] && break ; done
+ chroot "$FS" apt-get install -y $KERNEL
+ chroot "$FS" depmod -a ${KERNEL#linux-image-}
+
+ chroot "$FS" apt-get install -y busybox-static debootstrap
+ touch "$FS"/usr/bin{linuxrc,init} # block these
+ chroot "$FS" /usr/bin/busybox --install -s /usr/bin
+
+fi # End of $DEBSTRAP actions
+
+## (re)install /init
+echo "** Please select /init template"
+select INIT in none $(echo init*.template) ; do [ -n "$INIT" ] && break ; done
+if [ "$INIT" != none ] ; then
+ cp $INIT "$FS"/init
+ cp message.txt "$FS"/
+ chmod a+x "$FS"/init
+fi
+
+## Pick boot kernel, if there are many
+LINUXES=( $(cd "$FS"/boot ; ls vmlinuz* 2>/dev/null) )
+if [ ${#LINUXES[@]} -gt 1 ] ; then
+ echo "** Please select boot kernel"
+ select LINUX in $LINUXES ; do [ -n "$LINUX" ] && break ; done
+elif [ -n "$LINUXES" ] ; then
+ LINUX="$FS"/boot/${LINUXES[0]}
+else
+ echo "** Oh No! There is no $FS/boot/vmlinux-* ... bailing out!" >&2
+ exit 1
+fi
+
+#============================================================
+# Prepare a temporary directory tree with a boot kernel and $FS packed
+# up into an initrd.gz
+TMP=$(mktemp -d XXXX)
+trap "rm -r $TMP" 0 2 15
+
+( cd "$FS" && find -printf '%P\n' | cpio -o -H newc ) | \
+ gzip > $TMP/initrd.gz
+cp $LINUX $TMP/vmlinuz
+
+#============================================================
+# Add syslinux boot equipment for bios boot under $TMP/syslinux
+# (also includes the syslinux image mastering further below)
+echo "Please select boot console options"
+
+CONOPTS=( none ttyS0,115200 )
+select CON in "${CONOPTS[@]}" ; do [ -n "$CON" ] && break ; done
+case "$CON" in none) CON= ;; *) CON="console=$CON" ;; esac
+
+mkdir -p $TMP/syslinux
+cp -t $TMP/syslinux /usr/lib/syslinux/modules/bios/*
+cat <<EOF > $TMP/syslinux/syslinux.cfg
+default menu.c32
+label linux
+ kernel /vmlinuz
+ append initrd=/initrd.gz init=/init console=ttyS0,115200
+EOF
+
+#============================================================
+[ -e "$IMG" ] && read -p "** Will overwrite $IMG (or ^C here and now)" x
+rm -f "$IMG"
+
+# Estimate the required disk image size in Mib
+DUM=$(( $(du -sB1 $TMP | sed 's/\s.*//') / 1048576 + 3 ))
+
+# Create the image, with the partition marked as EFI parition and
+# bootable; acutal format is FAT{12,16,32}
+dd if=/dev/zero of="$IMG" bs=${DUM}M count=0 seek=1 status=none
+cat <<EOF | sfdisk -q "$IMG"
+label: dos
+
+- - 0xef *
+EOF
+# First sector is 2048 by default, which is byte address 1048576
+mkfs.fat --offset 2048 "$IMG" >/dev/null
+mcopy -i "$IMG@@1048576" -s $TMP/* ::/
+[ -z "$SUDO_USER" ] || chown $SUDO_USER: "$IMG"
+
+#============================================================
+# Final syslinux bios boot image mastering
+dd if=/usr/lib/SYSLINUX/mbr.bin of="$IMG" bs=440 conv=notrunc status=none
+syslinux -i -d syslinux -t 1048576 "$IMG"