3 # apt-in-chroot - runs apt in a specified chroot
5 # Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
6 # Copyright (C) 2012 Peter Palfrader <peter@palfrader.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 echo "Usage: $bn <chroot> <apt sub-command> [<package>]"
30 echo " allowed apt sub-commands are:"
37 echo " + dist-upgrade"
41 if [ "$#" -lt "2" ]; then
54 if [ -x /usr/bin/dchroot ] ; then
55 chroots=$(dchroot -l 2>&1 | awk -F": " '{print $2}' | tr ' ' '\n')
57 suffix[${#suffix[*]}]="_$(dpkg --print-architecture)"
59 elif [ -x /usr/bin/schroot ] ; then
60 chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' )
62 suffix[${#suffix[*]}]=""
63 suffix[${#suffix[*]}]="-dchroot"
64 suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-dchroot"
65 allowed_filter='-dchroot$'
67 echo >&2 "Cannot find chroot wrapper."
73 for (( i = 0 ; i < ${#suffix[*]} ; i++ )); do
74 if [ "$c" == "$CHROOT${suffix[$i]}" ]; then
80 if ! [ -n "$requested_chroot" ]; then
81 echo >&2 "$CHROOT is not a valid dchroot. Available chroots are:"
84 elif ! [[ "$requested_chroot" =~ $allowed_filter ]]; then # do not quote the regex
85 echo >&2 "$CHROOT is not a valid dchroot. While it exists, this script may not touch it. Chroot names must match $allowed_filter."
91 install|remove|purge|build-dep|update|upgrade|dist-upgrade)
92 # those are the allowed apt sub-commands
95 echo "$APTCMD is not a valid apt sub-command"
101 if [ "$method" = "dchroot" ]; then
102 echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$requested_chroot apt-get $APTCMD $PACKAGES'"
103 /usr/sbin/chroot $CHROOT_DIR/"$requested_chroot" apt-get "$APTCMD" $PACKAGES
104 elif [ "$method" = "schroot" ]; then
105 echo "Will run 'schroot -c $requested_chroot apt-get $APTCMD $PACKAGES'"
106 schroot -c "$requested_chroot" apt-get "$APTCMD" $PACKAGES
108 echo >&2 "Invalid method."