2012-06-18 04:44:55 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
shopt -s extglob
|
|
|
|
|
2012-06-18 17:07:37 +08:00
|
|
|
m4_include(common)
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2012-06-18 05:52:39 +08:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2012-09-09 03:29:18 +08:00
|
|
|
usage: ${0##*/} chroot-dir [command]
|
|
|
|
|
2012-11-13 10:00:09 +08:00
|
|
|
-h Print this help message
|
|
|
|
|
2012-09-09 03:29:18 +08:00
|
|
|
If 'command' is unspecified, ${0##*/} will launch /bin/sh.
|
2012-06-18 05:52:39 +08:00
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2012-06-18 04:44:55 +08:00
|
|
|
if [[ -z $1 || $1 = @(-h|--help) ]]; then
|
2012-06-18 05:52:39 +08:00
|
|
|
usage
|
2012-06-18 04:44:55 +08:00
|
|
|
exit $(( $# ? 0 : 1 ))
|
|
|
|
fi
|
|
|
|
|
2012-06-19 20:12:20 +08:00
|
|
|
(( EUID == 0 )) || die 'This script must be run with root privileges'
|
2012-06-20 22:25:18 +08:00
|
|
|
chrootdir=$1
|
2012-08-13 23:12:41 +08:00
|
|
|
shift
|
2012-06-18 05:52:39 +08:00
|
|
|
|
2012-08-13 23:12:41 +08:00
|
|
|
[[ -d $chrootdir ]] || die "Can't create chroot on non-directory %s" "$chrootdir"
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2014-12-17 12:15:17 +08:00
|
|
|
chroot_setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
|
2014-12-16 11:55:51 +08:00
|
|
|
chroot_add_mount /etc/resolv.conf "$chrootdir/etc/resolv.conf" --bind
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2014-12-12 10:40:07 +08:00
|
|
|
SHELL=/bin/sh unshare --fork --pid chroot "$chrootdir" "$@"
|