chiark / gitweb /
Merge remote branch 'nthykier/master'
[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/bin:/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 }
39
40
41 if [ "$#" -lt "2" ]; then
42         usage >&2
43         exit 1
44 fi
45
46 CHROOT_DIR="/chroot"
47 CHROOT="$1"; shift
48 APTCMD="$1"; shift
49 PACKAGES="$*"
50
51 declare -a suffix
52 suffix[0]=""
53
54 if [ -x /usr/bin/dchroot ] ; then
55         chroots=$(dchroot -l 2>&1 | awk -F": " '{print $2}' | tr ' ' '\n')
56         method=dchroot
57         suffix[${#suffix[*]}]="_$(dpkg --print-architecture)"
58         allowed_filter='.'
59 elif [ -x /usr/bin/schroot ] ; then
60         chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' )
61         method=schroot
62         suffix[${#suffix[*]}]=""
63         suffix[${#suffix[*]}]="-dchroot"
64         suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-dchroot"
65         allowed_filter='-dchroot$'
66 else
67         echo >&2 "Cannot find chroot wrapper."
68         exit 1
69 fi
70
71 requested_chroot=""
72 while read c; do
73         for (( i = 0 ; i < ${#suffix[*]} ; i++ )); do
74                 if [ "$c" == "$CHROOT${suffix[$i]}" ]; then
75                         requested_chroot="$c"
76                 fi
77         done
78 done <<< "$chroots"
79
80 if ! [ -n "$requested_chroot" ]; then
81         echo >&2 "$CHROOT is not a valid dchroot.  Available chroots are:"
82         echo "$chroots"
83         exit 1
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."
86         exit 1
87 fi
88
89
90 case "$APTCMD" in
91 install|remove|purge|build-dep|update|upgrade|dist-upgrade)
92         # those are the allowed apt sub-commands
93         ;;
94 *)
95         echo "$APTCMD is not a valid apt sub-command"
96         exit 1
97         ;;
98 esac
99
100 # valid chroot
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
107 else
108         echo >&2 "Invalid method."
109         exit 1
110 fi