chiark / gitweb /
apt-in-chroot: add schroot support
authorPeter Palfrader <peter@palfrader.org>
Wed, 8 Feb 2012 11:23:22 +0000 (12:23 +0100)
committerPeter Palfrader <peter@palfrader.org>
Wed, 8 Feb 2012 11:23:22 +0000 (12:23 +0100)
apt-in-chroot
debian/changelog

index 9082965e06192d672e3960cd5270a015bac46817..ee02a10ce1cefc4782c0a4e98f96eb769d356a45 100755 (executable)
@@ -3,6 +3,7 @@
 #  apt-in-chroot - runs apt in a specified chroot
 # 
 #  Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
 #  apt-in-chroot - runs apt in a specified chroot
 # 
 #  Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
+#  Copyright (C) 2012 Peter Palfrader <peter@palfrader.org>
 #
 #  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
 #
 #  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
 #  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 # 
 
 #  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/bin:/bin"
+export PATH
 
 usage() {
         bn="`basename "$0"`"
        echo "Usage: $bn <chroot> <apt sub-command> [<package>]"
        echo ""
 
 usage() {
         bn="`basename "$0"`"
        echo "Usage: $bn <chroot> <apt sub-command> [<package>]"
        echo ""
-       echo "  allowed chroots are ($VALID_CHROOTS_PARSE)"
-       echo ""
        echo "  allowed apt sub-commands are:"
        echo "          + install"
        echo "          + remove"
        echo "  allowed apt sub-commands are:"
        echo "          + install"
        echo "          + remove"
@@ -37,16 +36,48 @@ usage() {
 }
 
 
 }
 
 
-if [ "$#" -lt "3" ]; then
+if [ "$#" -lt "2" ]; then
        usage >&2
        exit 1
 fi
 
 CHROOT_DIR="/chroot"
        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)"
+elif [ -x /usr/bin/schroot ] ; then
+       chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' )
+       method=schroot
+       suffix="-dchroot"
+       suffix[${#suffix[*]}]="-dchroot"
+       suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-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
+fi
 
 case "$APTCMD" in
 install|remove|purge|build-dep|update)
 
 case "$APTCMD" in
 install|remove|purge|build-dep|update)
@@ -58,13 +89,14 @@ install|remove|purge|build-dep|update)
        ;;
 esac
 
        ;;
 esac
 
-if echo "$VALID_CHROOTS" | grep -qFx "$CHROOT"; 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'"
+       #/usr/sbin/chroot $CHROOT_DIR/"$requested_chroot" apt-get "$APTCMD" $PACKAGES
 else
 else
-       echo "$CHROOT not in $VALID_CHROOTS_PARSE"
+       echo >&2 "Invalid method."
        exit 1
 fi
        exit 1
 fi
-
-
index 88b1cdb1c6d25674d931d834dde3917c71ae9827..645581b89c41463ceb8252383c667133426d0169 100644 (file)
@@ -9,7 +9,10 @@ debian.org (46) Xnstable; urgency=low
   [ Cyril Brulebois ]
   * release.debian.org: Add git-email.
 
   [ Cyril Brulebois ]
   * release.debian.org: Add git-email.
 
- -- Peter Palfrader <weasel@debian.org>  Sun, 05 Feb 2012 22:26:37 +0100
+  [ Peter Palfrader ]
+  * apt-in-chroot: add schroot support.
+
+ -- Peter Palfrader <weasel@debian.org>  Wed, 08 Feb 2012 12:22:55 +0100
 
 debian.org (45) unstable; urgency=low
 
 
 debian.org (45) unstable; urgency=low