diff options
Diffstat (limited to 'mkarchroot.in')
-rw-r--r-- | mkarchroot.in | 96 |
1 files changed, 64 insertions, 32 deletions
diff --git a/mkarchroot.in b/mkarchroot.in index 454d789..8cf9474 100644 --- a/mkarchroot.in +++ b/mkarchroot.in @@ -15,6 +15,7 @@ CHROOT_VERSION='v2' FORCE='n' RUN='' NOCOPY='n' +NONETWORK='n' working_dir='' @@ -22,50 +23,87 @@ APPNAME=$(basename "${0}") # usage: usage <exitvalue> usage() { - echo "Usage: ${APPNAME} [options] working-dir [package-list | app]" + echo "Usage: ${APPNAME} [options] working-dir [action]" echo ' options:' - echo ' -r <app> Run "app" within the context of the chroot' - echo ' -u Update the chroot via pacman' echo ' -f Force overwrite of files in the working-dir' echo ' -C <file> Location of a pacman config file' echo ' -M <file> Location of a makepkg config file' echo ' -n Do not copy config files into the chroot' echo ' -c <dir> Set pacman cache' - echo ' -h This message' - exit 1 + echo ' -N Disable networking in the chroot' + echo ' actions:' + echo ' -i <pkg-list> Install "pkg-list" in the chroot.' + echo ' Creates the chroot if necessary, workding-dir must exist' + echo ' -r <cmd> Run "cmd" within the context of the chroot' + echo ' -u Update the chroot via pacman' + echo ' -h Print this message' + + exit ${1-1} } -while getopts 'r:ufnhC:M:c:' arg; do +################################################################################ + +while getopts 'fC:M:nc:Nr:uh' arg; do case "${arg}" in - r) RUN="$OPTARG" ;; - u) RUN='/bin/sh -c "pacman -Syu --noconfirm && (pacman -Qqu >/dev/null && pacman -Su --noconfirm || exit 0)"' ;; f) FORCE='y' ;; C) pac_conf="$OPTARG" ;; M) makepkg_conf="$OPTARG" ;; n) NOCOPY='y' ;; c) cache_dir="$OPTARG" ;; - h|?) usage ;; + N) NONETWORK='y' ;; + + r) action="-$arg"; action_arg="$OPTARG" ;; + u|h) action="-$arg" ;; + *) error "invalid argument '${arg}'"; usage ;; esac done -if (( $EUID != 0 )); then - die 'This script must be run as root.' -fi - shift $(($OPTIND - 1)) -if [[ -z $RUN ]] && (( $# < 2 )); then - die 'You must specify a directory and one or more packages.' -elif (( $# < 1 )); then - die 'You must specify a directory.' +if [[ -n $action ]]; then + case $# in + 0) error 'You must specify a directory.'; usage ;; + 1) + args=("$1" "$action") + [[ -n $action_arg ]] && args+=("$action_arg") + set -- "${args[@]}" + unset args action action_arg + ;; + *) error 'Extra arguments.'; usage ;; + esac +else + if (( $# < 2 )); then + error 'You must specify a directory and an action.' + usage + fi fi -working_dir="$(readlink -f ${1})" +working_dir="$(readlink -f "${1}")" shift 1 - [[ -z $working_dir ]] && die 'Please specify a working directory.' +action=$1 +shift 1 +case "$action" in + -i) PKGS=("$@") ;; + -r) RUN="$*" ;; + -u) + (( $# > 0 )) && { error 'Extra arguments.'; usage; } + RUN='/bin/sh -c "pacman -Syu --noconfirm && (pacman -Qqu >/dev/null && pacman -Su --noconfirm || exit 0)"' + ;; + -h) usage 0 ;; + -*) error "invalid argument '${action#-}'"; usage ;; + *) PKGS=("$action" "$@") ;; # for compatability with mkarchroot +esac +unset action + +################################################################################ + +if (( $EUID != 0 )); then + die 'This script must be run as root.' +fi + if [[ -z $cache_dir ]]; then cache_dirs=($(pacman -v $cache_conf 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g')) else @@ -112,23 +150,17 @@ copy_hostconf () { } chroot_lock () { - # Only reopen the FD if it wasn't handed to us - if [[ $(readlink -f /dev/fd/9) != "${working_dir}.lock" ]]; then - exec 9>"${working_dir}.lock" - fi - - # Lock the chroot. Take note of the FD number. - if ! flock -n 9; then - stat_busy "Locking chroot" - flock 9 - stat_done - fi + lock_open_write 9 "${working_dir}" "Locking chroot" } chroot_run() { local dir=$1 shift - eval systemd-nspawn -D "${dir}" "${mount_args[@]}" -- ${@} 2>/dev/null + local nspawn_args=(-D "$dir" "${mount_args[@]}") + if [[ $NONETWORK = y ]]; then + nspawn_args+=(--private-network) + fi + eval systemd-nspawn "${nspawn_args[@]}" -- "${@}" 2>/dev/null } # }}} @@ -174,7 +206,7 @@ else if [[ $FORCE = 'y' ]]; then pacargs+=("--force") fi - if ! pacstrap -GMcd "${working_dir}" "${pacargs[@]}" "$@"; then + if ! pacstrap -GMcd "${working_dir}" "${pacargs[@]}" "${PKGS[@]}"; then die 'Failed to install all packages' fi |