+}
+
+usage() {
+ cat <<-EOF
+ Usage: ${0##*/} [options] [program to run]
+
+ Sets up common mount points and then chroots in and runs a program.
+ If no program is specified, then launch a login shell.
+
+ Options:
+ -u Unmount all paths in the chroot
+ -m <path> Add path to mount list
+ -d <dir> Use <dir> as chroot (defaults to ${0%/*})
+ -h This help screen
+ EOF
+ exit
+}
+
+main() {
+ bootstrap "$@"
+
+ local mounts=(
+ proc sys tmp dev dev/shm run
+ usr/portage usr/portage/distfiles
+ var/db/repos/gentoo var/cache/distfiles
+ usr/local/src
+ )
+
+ local chroot=${0%/*}
+ case ${chroot} in
+ .) chroot=${PWD} ;;
+ ./*) chroot=${PWD}/${chroot#./} ;;
+ esac
+
+ local cmd
+ while [[ -n $1 ]] ; do
+ case $1 in
+ -u) cmd='umount' ;;
+ -m) mounts+=( "$2" ); shift ;;
+ -d) chroot=$(realpath "$2"); shift ;;
+ --help|-h) usage ;;
+ -*) echo "${0##*/}: unknown option $1"; exit 1 ;;
+ *) break ;;
+ esac
+ shift
+ done
+ cd "${chroot}"
+
+ case ${cmd} in
+ umount) exec "${0%/*}/umount-tree" -y "${chroot}" ;;
+ esac
+
+ local m
+ for m in "${mounts[@]}" ; do
+ maybe_mount "${m}"
+ done
+ # Handle special mounts that we don't want to just bind mount.
+ if ! is_mounted "${chroot}/dev/pts" ; then
+ # Option order matters: on older kernels that don't recognize newinstance,
+ # parsing stops as soon as it hits that.
+ mount -t devpts devpts "${chroot}/dev/pts" \
+ -o nosuid,noexec,mode=0620,gid=5,ptmxmode=0666,newinstance
+ fi
+
+ init_chroot
+
+ local setarch
+ if type -P setarch &>/dev/null ; then
+ local bin_dst=$(get_type bin/bash)
+ if [[ -n ${bin_dst} ]] ; then
+ setarch="setarch ${bin_dst}"
+ fi
+ fi
+
+ # Doubtful these settings we want to leak into the chroot.
+ unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
+ unset LS_COLORS # format changes over time
+ [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
+ exec \
+ ${setarch} \
+ chroot "${chroot}" \
+ "$@"
+}
+
+main "$@"