2012-06-17 13:44:55 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
shopt -s extglob
|
|
|
|
|
2012-06-18 02:07:37 -07:00
|
|
|
m4_include(common)
|
2012-06-17 13:44:55 -07:00
|
|
|
|
2012-06-17 14:52:39 -07:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2012-09-08 12:29:18 -07:00
|
|
|
usage: ${0##*/} chroot-dir [command]
|
|
|
|
|
2012-11-12 18:00:09 -08:00
|
|
|
-h Print this help message
|
|
|
|
|
2012-09-08 12:29:18 -07:00
|
|
|
If 'command' is unspecified, ${0##*/} will launch /bin/sh.
|
2012-06-17 14:52:39 -07:00
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2012-06-17 13:44:55 -07:00
|
|
|
if [[ -z $1 || $1 = @(-h|--help) ]]; then
|
2012-06-17 14:52:39 -07:00
|
|
|
usage
|
2012-06-17 13:44:55 -07:00
|
|
|
exit $(( $# ? 0 : 1 ))
|
|
|
|
fi
|
|
|
|
|
2012-06-19 05:12:20 -07:00
|
|
|
(( EUID == 0 )) || die 'This script must be run with root privileges'
|
2012-06-20 07:25:18 -07:00
|
|
|
chrootdir=$1
|
2012-08-13 08:12:41 -07:00
|
|
|
shift
|
2012-06-17 14:52:39 -07:00
|
|
|
|
2012-08-13 08:12:41 -07:00
|
|
|
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
|
2012-06-17 13:44:55 -07:00
|
|
|
|
2012-07-15 07:09:49 -07:00
|
|
|
trap '{ api_fs_umount "$chrootdir"; umount "$chrootdir/etc/resolv.conf"; } 2>/dev/null' EXIT
|
2012-06-18 13:56:13 -07:00
|
|
|
|
2012-08-13 08:12:41 -07:00
|
|
|
api_fs_mount "$chrootdir" || die "failed to setup API filesystems in chroot %s" "$chrootdir"
|
2012-06-20 07:25:18 -07:00
|
|
|
mount -B /etc/resolv.conf "$chrootdir/etc/resolv.conf"
|
2012-06-17 13:44:55 -07:00
|
|
|
|
2012-08-13 08:12:41 -07:00
|
|
|
SHELL=/bin/sh chroot "$chrootdir" "$@"
|
2012-06-18 13:56:13 -07:00
|
|
|
|
2012-06-20 07:25:18 -07:00
|
|
|
umount "$chrootdir/etc/resolv.conf"
|