#!/bin/sh -e # xchroot DIR [CMD...] - chroot into a Void (or other Linux) installation fail() { printf '%s\n' "$1" >&2 exit 1 } if [ "$(id -u)" -ne 0 ]; then fail 'xchroot needs to run as root' fi CHROOT=$1; shift [ -d "$CHROOT" ] || fail 'not a directory' [ -d "$CHROOT/dev" ] || fail 'no /dev in chroot' [ -d "$CHROOT/proc" ] || fail 'no /proc in chroot' [ -d "$CHROOT/sys" ] || fail 'no /sys in chroot' for _fs in dev proc sys; do mount --rbind "/$_fs" "$CHROOT/$_fs" mount --make-rslave "$CHROOT/$_fs" done touch "$CHROOT/etc/resolv.conf" mount --bind /etc/resolv.conf "$CHROOT/etc/resolv.conf" cleanup() { umount -R "$CHROOT/dev" "$CHROOT/proc" "$CHROOT/sys" "$CHROOT/etc/resolv.conf" } trap cleanup EXIT INT if [ -x "$CHROOT/$SHELL" ]; then INNER_SHELL="$SHELL" elif [ -x "$CHROOT/bin/bash" ]; then INNER_SHELL="/bin/bash" else INNER_SHELL="/bin/sh" fi printf "\033[1m=> Entering chroot $CHROOT\033[m\n" export PS1="[xchroot $CHROOT] $PS1" chroot "$CHROOT" ${@:-$INNER_SHELL} STATUS=$? if [ $STATUS -ne 0 ]; then printf "\033[1m=> Exited chroot $CHROOT\033[m\n" else printf "\033[1m=> Exited chroot $CHROOT with status $STATUS\033[m\n" fi exit $STATUS