Skip to content

Instantly share code, notes, and snippets.

@vishalbiswas
Last active November 22, 2016 06:30
Show Gist options
  • Save vishalbiswas/09c76fa44dcea2205f3d6a935d2794ee to your computer and use it in GitHub Desktop.
Save vishalbiswas/09c76fa44dcea2205f3d6a935d2794ee to your computer and use it in GitHub Desktop.
script for easy chrooting. adapted from an answer on stackoverflow which I can't find anymore
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 CHROOT_PATH [PROGRAM]"
exit 1
fi
CHROOT_PATH="$1"
PROGRAM="$2"
BIND_DIRS="/proc /dev /dev/pts /dev/shm"
start()
{
for DIR in $BIND_DIRS; do
echo "Binding $DIR to ${CHROOT_PATH}${DIR}"
mkdir -p "${CHROOT_PATH}${DIR}"
mount --bind "$DIR" "${CHROOT_PATH}${DIR}"
done
}
stop()
{
# Unmount in reverse order
for DIR in `echo "$BIND_DIRS" | sed -e 's/ /\n/g' | tac`; do
echo "Ubinding ${CHROOT_PATH}${DIR}"
umount "${CHROOT_PATH}${DIR}"
done
}
start
echo "Chroot..."
export LC_ALL=C
me=`whoami`
chroot --userspec=$me:$me "$CHROOT_PATH" "$PROGRAM"
stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment