diff --git a/arch-chroot.in b/arch-chroot.in index e854b21..9f704e2 100644 --- a/arch-chroot.in +++ b/arch-chroot.in @@ -8,7 +8,8 @@ usage() { cat <[: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[@]}"