diff options
Diffstat (limited to 'mkrootfs')
-rwxr-xr-x | mkrootfs | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/mkrootfs b/mkrootfs new file mode 100755 index 0000000..e8941b0 --- /dev/null +++ b/mkrootfs @@ -0,0 +1,136 @@ +#!/bin/bash +set -e + +# Directory in which the file system will be built +# +ROOTFS=$(pwd)/cb5-rootfs + +# Debian release +# +RELEASE=stretch + +# Host name to be used by machine +# +HOSTNAME=cb5 + +# SSH to log in as root +# +SSH_KEY="$HOME/.ssh/id_rsa.pub" + +print_usage() { + cat 1>&2 <<EOF +Usage: $0 [options] rootfs-directory +Create a debian root filesystem for the cubieboard5. +The password for the root account will be set to 'guest', unless an ssh key is +specified, in which case the account will be locked. + +Options: + --hostname=<$HOSTNAME> Use a given hostname for the device. + --ssh-key=<$SSH_KEY> Add the given SSH key to root's authorized_keys file. When this option is provided, root's account will be locked. + --release=<$RELEASE> Create a root filesystem for the given debian release. +EOF +} + +# Process options +# +while [ $# -gt 1 ] +do + case "$1" in + --help|-h) + print_usage + exit 0 + ;; + --hostname=*) + HOSTNAME="${1#*=}" + ;; + --release=*) + RELEASE="${1#*=}" + ;; + --ssh-key=*) + SSH_KEY="${1#*=}" + ;; + *) + echo "Unknown option '$1'" 1>&2 + exit 1 + esac + shift +done + +# Process last argument, the root file system location +if [ -z "$1" ] || [ "$1" = -h ] || [ "$1" = --help ]; then + print_usage + exit 1 +fi +if [ "$EUID" -ne 0 ]; then + echo "This must be run as root." 1>&2 + exit 1 +fi +export ROOTFS="$1" + +export RELEASE +export HOSTNAME +export SSH_KEY + +# Run a command in the root file system +# +chroot_exec() { + # Exec command in chroot + LANG=C LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot ${ROOTFS} /bin/sh -c "$*" +} +export -f chroot_exec + +# Unmount and remove any temporary files +# +cleanup() { + set +e + + # Identify and kill all processes still using files + echo "Killing processes using mount point" 1>&2 + fuser -k "${ROOTFS}" + sleep 3 + fuser -9 -k -v "${ROOTFS}" + + # Clean up all temporary mount points + echo "Removing temporary mount points" 1>&2 + umount -l "${ROOTFS}/proc" 2> /dev/null + umount -l "${ROOTFS}/sys" 2> /dev/null + umount -l "${ROOTFS}/dev/pts" 2> /dev/null + umount -l "${ROOTFS}/dev" 2> /dev/null + trap - 0 1 2 3 6 +} +trap cleanup 0 1 2 3 6 + +# Bootstrap initial file system +# +echo "Creating rootfs" 1>&2 +# proxy is set to use apt-cacher-ng +http_proxy="localhost:3142" qemu-debootstrap \ + --verbose \ + --include=linux-image-armmp-lpae,locales,flash-kernel,sunxi-tools,firmware-linux-free,u-boot-tools \ + --arch=armhf \ + "$RELEASE" \ + "$ROOTFS" \ + http://httpredir.debian.org/debian + +# Prepare filesystem to be chrooted +# +echo "Creating temporary system mount points in rootfs" 1>&2 +mount -t proc chproc "$ROOTFS/proc" +mount -t sysfs chsys "$ROOTFS/sys" +mount -t devtmpfs chdev "$ROOTFS/dev" +mount -t devpts chpts "$ROOTFS/dev/pts" +echo "Configuring rootfs" 1>&2 +chroot_exec dpkg --configure -a + +# Run setup scripts +# +echo "Running setup scripts" 1>&2 +dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +for script in "$dir"/mkrootfs.d/*.sh; do + echo "Running $script" 1>&2 + bash "$script" +done +echo "Completed setup scripts" 1>&2 + +cleanup +echo "Successfully created rootfs" 1>&2 |