#!/bin/bash set -Eeuo pipefail # config WORKDIR="/tmp/menhera" ROOTFS="https://images.linuxcontainers.org/images/debian/stretch/amd64/default/20190409_05:24/rootfs.squashfs" # internal global variables OLDROOT="/" NEWROOT="" # https://stackoverflow.com/a/3232082/2646069 confirm() { # call with a prompt string or use a default read -r -p "${1:-Are you sure? [y/N]} " response case "$response" in [yY][eE][sS]|[yY]) true ;; *) false ;; esac } sync_filesystem() { echo "Syncing..." sync sync } prepare_environment() { echo "Loading kernel modules..." modprobe overlay modprobe squashfs echo "Creating workspace in '${WORKDIR}'..." # workspace mkdir -p "${WORKDIR}" mount -t tmpfs tmpfs "${WORKDIR}" # new rootfs mkdir -p "${WORKDIR}/newroot" # readonly part of new rootfs mkdir -p "${WORKDIR}/newrootro" # writeable part of new rootfs mkdir -p "${WORKDIR}/newrootrw" # overlayfs workdir mkdir -p "${WORKDIR}/overlayfs_workdir" echo "Downloading temporary rootfs..." wget -c "${ROOTFS}" -O "${WORKDIR}/rootfs.squashfs" } mount_new_rootfs() { echo "Mounting temporary rootfs..." mount -t squashfs "${WORKDIR}/rootfs.squashfs" "${WORKDIR}/newrootro" mount -t overlay overlay -o rw,lowerdir="${WORKDIR}/newrootro",upperdir="${WORKDIR}/newrootrw",workdir="${WORKDIR}/overlayfs_workdir" "${WORKDIR}/newroot" NEWROOT="${WORKDIR}/newroot" } install_software() { echo "Installing OpenSSH Server into new rootfs..." DEBIAN_FRONTEND=noninteractive chroot "${NEWROOT}" apt-get update -y DEBIAN_FRONTEND=noninteractive chroot "${NEWROOT}" apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install -y ssh } copy_config() { echo "Copying important config into new rootfs..." ! cp -ax "${OLDROOT}/etc/resolv.conf" "${NEWROOT}/etc" ! cp -axr "${OLDROOT}/etc/ssh" "${NEWROOT}/etc" ! cp -ax "${OLDROOT}/etc/"{passwd,shadow} "${NEWROOT}/etc" ! cp -axr "${OLDROOT}/root/.ssh" "${NEWROOT}/root" chroot "${NEWROOT}" chsh -s /bin/bash root cat > "${NEWROOT}/etc/motd" <