Skip to content

Instantly share code, notes, and snippets.

@maxwellb
Last active July 25, 2019 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwellb/c09ed2b4f7093e44e084d2abcdd7bfce to your computer and use it in GitHub Desktop.
Save maxwellb/c09ed2b4f7093e44e084d2abcdd7bfce to your computer and use it in GitHub Desktop.
Wrapper to debootstrap and chroot
#!/usr/bin/env bash
TARGET=${1:-/mnt}
TARGET=`readlink -f ${TARGET}`
DIST=${2:-buster}
MIRROR=${3:-http://cdn-fastly.deb.debian.org/debian}
if ! [ -d ${TARGET} ]; then
echo "Cannot locate TARGET=${TARGET}. Bailing." >&2
exit 1
fi
if [ `find ${TARGET} | grep -v 'lost+found' | wc -l` -gt 1 ]; then
echo "TARGET=${TARGET} is not empty. Skipping debootstrap step." >&2
else
debootstrap --components=main,contrib,non-free buster ${TARGET} ${MIRROR}
fi
if ! ( curl -Is ${MIRROR}/dists/${DIST}/InRelease >/dev/null ); then
echo "InRelease file for DIST=${DIST} is not found at MIRROR=${MIRROR}" >&2
exit 1
fi
echo "Entering a shell so that you can mount the appropriate filesystems inside TARGET=${TARGET}" >&2
echo "Exit the shell with a failure (exit 1) to halt this script" >&2
PS1='(debootstrap) $(whoami)@$(hostname):$(pwd)$([ $(whoami) = "root" ] && echo "#" || echo "\$") ' sh -CfV
if [ "$?" -gt 0 ]; then
echo "Received exit code. Bailing." >&2
exit 1
fi
mount | grep "on ${TARGET}/dev" || mount --verbose --rbind /dev ${TARGET}/dev
mount | grep "on ${TARGET}/sys" || mount --verbose -t sysfs none ${TARGET}/sys
mount | grep "on ${TARGET}/proc" || mount --verbose -t proc none ${TARGET}/proc
cp -vf /etc/resolv.conf ${TARGET}/etc/resolv.conf
/usr/bin/env -i LANG=C HOME=/root TERM=${TERM} PS1="(chroot ${TARGET}) \\u@\\h:\\w# " `which chroot` ${TARGET} /bin/bash --noprofile --login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment