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]
|
|
|
|
|
2016-04-18 19:05:15 +08:00
|
|
|
-h Print this help message
|
|
|
|
-u <user>[:group] Specify non-root user and optional group to use
|
2012-11-13 10:00:09 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-01-27 07:12:33 +08:00
|
|
|
chroot_add_resolv_conf() {
|
|
|
|
local chrootdir=$1 resolv_conf=$1/etc/resolv.conf
|
|
|
|
|
|
|
|
# Handle resolv.conf as a symlink to somewhere else.
|
|
|
|
if [[ -L $chrootdir/etc/resolv.conf ]]; then
|
|
|
|
# readlink(1) should always give us *something* since we know at this point
|
|
|
|
# it's a symlink. For simplicity, ignore the case of nested symlinks.
|
|
|
|
resolv_conf=$(readlink "$chrootdir/etc/resolv.conf")
|
|
|
|
if [[ $resolv_conf = /* ]]; then
|
|
|
|
resolv_conf=$chrootdir$resolv_conf
|
|
|
|
else
|
|
|
|
resolv_conf=$chrootdir/etc/$resolv_conf
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ensure file exists to bind mount over
|
|
|
|
if [[ ! -f $resolv_conf ]]; then
|
|
|
|
install -Dm644 /dev/null "$resolv_conf" || return 1
|
|
|
|
fi
|
2015-07-05 04:26:49 +08:00
|
|
|
elif [[ ! -e $chrootdir/etc/resolv.conf ]]; then
|
|
|
|
# The chroot might not have a resolv.conf.
|
|
|
|
return 0
|
2015-01-27 07:12:33 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
chroot_add_mount /etc/resolv.conf "$resolv_conf" --bind
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:05:15 +08:00
|
|
|
while getopts ':hu:' flag; do
|
|
|
|
case $flag in
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
userspec=$OPTARG
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( OPTIND - 1 ))
|
2012-06-18 04:44:55 +08:00
|
|
|
|
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
|
|
|
|
2016-06-11 03:36:12 +08:00
|
|
|
(( $# )) || die 'No chroot directory specified'
|
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"
|
2015-01-27 07:12:33 +08:00
|
|
|
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
|
2012-06-18 04:44:55 +08:00
|
|
|
|
2016-04-18 19:05:15 +08:00
|
|
|
chroot_args=()
|
|
|
|
[[ $userspec ]] && chroot_args+=(--userspec "$userspec")
|
|
|
|
chroot_args+=("$chrootdir" "$@")
|
|
|
|
|
|
|
|
SHELL=/bin/bash unshare --fork --pid chroot "${chroot_args[@]}"
|