chiark / gitweb /
Install dose-debcheck on buildd.d.o
[dsa-metapackages.git] / apt-in-chroot
1 #!/bin/bash
2
3 #  apt-in-chroot - runs apt in a specified chroot
4
5 #  Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
6 #  Copyright (C) 2012 Peter Palfrader <peter@palfrader.org>
7 #
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)
11 #  any later version.
12
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.
17
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.
21
22
23 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
24 export PATH
25
26 usage() {
27         bn="`basename "$0"`"
28         echo "Usage: $bn <chroot> <apt sub-command> [<package>]"
29         echo ""
30         echo "  allowed apt sub-commands are:"
31         echo "          + install"
32         echo "          + remove"
33         echo "          + purge"
34         echo "          + build-dep"
35         echo "          + update"
36         echo "          + upgrade"
37         echo "          + dist-upgrade"
38         echo "          + clean"
39 }
40
41
42 if [ "$#" -lt "2" ]; then
43         usage >&2
44         exit 1
45 fi
46
47 CHROOT_DIR="/chroot"
48 CHROOT="$1"; shift
49 APTCMD="$1"; shift
50 PACKAGES="$*"
51
52 declare -a suffix
53 suffix[0]=""
54
55 if [ -x /usr/bin/dchroot ] ; then
56         chroots=$(dchroot -l 2>&1 | awk -F": " '{print $2}' | tr ' ' '\n')
57         method=dchroot
58         suffix[${#suffix[*]}]="_$(dpkg --print-architecture)"
59         allowed_filter='.'
60 elif [ -x /usr/bin/schroot ] ; then
61         chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' )
62         method=schroot
63         suffix[${#suffix[*]}]=""
64         suffix[${#suffix[*]}]="-dchroot"
65         suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-dchroot"
66         allowed_filter='-dchroot$'
67 else
68         echo >&2 "Cannot find chroot wrapper."
69         exit 1
70 fi
71
72 requested_chroot=""
73 while read c; do
74         for (( i = 0 ; i < ${#suffix[*]} ; i++ )); do
75                 if [ "$c" == "$CHROOT${suffix[$i]}" ]; then
76                         requested_chroot="$c"
77                 fi
78         done
79 done <<< "$chroots"
80
81 if ! [ -n "$requested_chroot" ]; then
82         echo >&2 "$CHROOT is not a valid dchroot.  Available chroots are:"
83         echo "$chroots"
84         exit 1
85 elif ! [[ "$requested_chroot" =~ $allowed_filter ]]; then  # do not quote the regex
86         echo >&2 "$CHROOT is not a valid dchroot.  While it exists, this script may not touch it.  Chroot names must match $allowed_filter."
87         exit 1
88 fi
89
90
91 case "$APTCMD" in
92 install|remove|purge|build-dep|update|upgrade|dist-upgrade|clean)
93         # those are the allowed apt sub-commands
94         ;;
95 *)
96         echo "$APTCMD is not a valid apt sub-command"
97         exit 1
98         ;;
99 esac
100
101 # valid chroot
102 if [ "$method" = "dchroot" ]; then
103         echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$requested_chroot apt-get $APTCMD $PACKAGES'"
104         /usr/sbin/chroot $CHROOT_DIR/"$requested_chroot" apt-get "$APTCMD" $PACKAGES
105 elif [ "$method" = "schroot" ]; then
106         echo "Will run 'schroot -c $requested_chroot apt-get $APTCMD $PACKAGES'"
107         schroot -c "$requested_chroot" apt-get "$APTCMD" $PACKAGES
108 else
109         echo >&2 "Invalid method."
110         exit 1
111 fi