support coreutils chroot's --userspec

This commit is contained in:
Dave Reisner 2016-04-18 07:05:15 -04:00
parent 1b8f4b542c
commit 633d192029

View File

@ -8,7 +8,8 @@ usage() {
cat <<EOF
usage: ${0##*/} chroot-dir [command]
-h Print this help message
-h Print this help message
-u <user>[:group] Specify non-root user and optional group to use
If 'command' is unspecified, ${0##*/} will launch /bin/sh.
@ -41,10 +42,24 @@ chroot_add_resolv_conf() {
chroot_add_mount /etc/resolv.conf "$resolv_conf" --bind
}
if [[ -z $1 || $1 = @(-h|--help) ]]; then
usage
exit $(( $# ? 0 : 1 ))
fi
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 ))
(( EUID == 0 )) || die 'This script must be run with root privileges'
chrootdir=$1
@ -55,4 +70,8 @@ shift
chroot_setup "$chrootdir" || die "failed to setup chroot %s" "$chrootdir"
chroot_add_resolv_conf "$chrootdir" || die "failed to setup resolv.conf"
SHELL=/bin/bash unshare --fork --pid chroot "$chrootdir" "$@"
chroot_args=()
[[ $userspec ]] && chroot_args+=(--userspec "$userspec")
chroot_args+=("$chrootdir" "$@")
SHELL=/bin/bash unshare --fork --pid chroot "${chroot_args[@]}"