X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=dsa-metapackages.git;a=blobdiff_plain;f=apt-in-chroot;h=7126202770fc3cda2771196e4708aa57a31cb4c6;hp=4e0028a6748bf5d09bd90f3f2ae1bea4269e34eb;hb=27d6b5b7495afdd6a897fa8223285cf82b01c1bc;hpb=6e621a197aee70969e9732c9afa178bebaee6b4c diff --git a/apt-in-chroot b/apt-in-chroot index 4e0028a..7126202 100755 --- a/apt-in-chroot +++ b/apt-in-chroot @@ -3,6 +3,7 @@ # apt-in-chroot - runs apt in a specified chroot # # Copyright (C) 2010 Martin Zobel-Helas +# Copyright (C) 2012 Peter Palfrader # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,36 +20,76 @@ # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. # -VALID_CHROOTS="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /usr/bin/tr ' ' '\n')" -VALID_CHROOTS_PARSE="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /bin/sed -e 's/ /|/g')" +PATH="/usr/sbin:/usr/bin:/sbin:/bin" +export PATH usage() { bn="`basename "$0"`" - echo "Usage: $bn " - echo "" - echo " allowed chroots are ($VALID_CHROOTS_PARSE)" + echo "Usage: $bn []" echo "" echo " allowed apt sub-commands are:" echo " + install" echo " + remove" echo " + purge" echo " + build-dep" + echo " + update" + echo " + upgrade" + echo " + dist-upgrade" + echo " + clean" } -if [ "$#" -lt "3" ]; then +if [ "$#" -lt "2" ]; then usage >&2 exit 1 fi CHROOT_DIR="/chroot" -CHROOT=$1; shift -APTCMD=$1; shift -PACKAGES=$* +CHROOT="$1"; shift +APTCMD="$1"; shift +PACKAGES="$*" + +declare -a suffix +suffix[0]="" + +if [ -x /usr/bin/dchroot ] ; then + chroots=$(dchroot -l 2>&1 | awk -F": " '{print $2}' | tr ' ' '\n') + method=dchroot + suffix[${#suffix[*]}]="_$(dpkg --print-architecture)" + allowed_filter='.' +elif [ -x /usr/bin/schroot ] ; then + chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' ) + method=schroot + suffix[${#suffix[*]}]="" + suffix[${#suffix[*]}]="-dchroot" + suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-dchroot" + allowed_filter='-dchroot$' +else + echo >&2 "Cannot find chroot wrapper." + exit 1 +fi + +requested_chroot="" +while read c; do + for (( i = 0 ; i < ${#suffix[*]} ; i++ )); do + if [ "$c" == "$CHROOT${suffix[$i]}" ]; then + requested_chroot="$c" + fi + done +done <<< "$chroots" + +if ! [ -n "$requested_chroot" ]; then + echo >&2 "$CHROOT is not a valid dchroot. Available chroots are:" + echo "$chroots" + exit 1 +elif ! [[ "$requested_chroot" =~ $allowed_filter ]]; then # do not quote the regex + echo >&2 "$CHROOT is not a valid dchroot. While it exists, this script may not touch it. Chroot names must match $allowed_filter." + exit 1 +fi case "$APTCMD" in -install|remove|purge|build-dep) +install|remove|purge|build-dep|update|upgrade|dist-upgrade|clean) # those are the allowed apt sub-commands ;; *) @@ -57,17 +98,14 @@ install|remove|purge|build-dep) ;; esac -grep -qFx $CHROOT <<< "$VALID_CHROOTS" - -RET=$? - -if [ "$RET" == "0" ]; then - # valid chroot - echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES'" - /usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES +# valid chroot +if [ "$method" = "dchroot" ]; then + echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$requested_chroot apt-get $APTCMD $PACKAGES'" + /usr/sbin/chroot $CHROOT_DIR/"$requested_chroot" apt-get "$APTCMD" $PACKAGES +elif [ "$method" = "schroot" ]; then + echo "Will run 'schroot -c $requested_chroot apt-get $APTCMD $PACKAGES'" + schroot -c "$requested_chroot" apt-get "$APTCMD" $PACKAGES else - echo "$CHROOT not in $VALID_CHROOTS_PARSE" + echo >&2 "Invalid method." exit 1 fi - -