chiark / gitweb /
Also accept empty/no suffix in chroot name
[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 elif [ -x /usr/bin/schroot ] ; then
59         chroots=$(schroot -l | awk -F":" '{print $2}' | grep -- '-dchroot$' )
60         method=schroot
61         suffix[${#suffix[*]}]=""
62         suffix[${#suffix[*]}]="-dchroot"
63         suffix[${#suffix[*]}]="-$(dpkg --print-architecture)-dchroot"
64 else
65         echo >&2 "Cannot find chroot wrapper."
66         exit 1
67 fi
68
69 requested_chroot=""
70 while read c; do
71         for (( i = 0 ; i < ${#suffix[*]} ; i++ )); do
72                 if [ "$c" == "$CHROOT${suffix[$i]}" ]; then
73                         requested_chroot="$c"
74                 fi
75         done
76 done <<< "$chroots"
77
78 if ! [ -n "$requested_chroot" ]; then
79         echo >&2 "$CHROOT is not a valid dchroot.  Available chroots are:"
80         echo "$chroots"
81         exit 1
82 fi
83
84 case "$APTCMD" in
85 install|remove|purge|build-dep|update|upgrade|dist-upgrade)
86         # those are the allowed apt sub-commands
87         ;;
88 *)
89         echo "$APTCMD is not a valid apt sub-command"
90         exit 1
91         ;;
92 esac
93
94 # valid chroot
95 if [ "$method" = "dchroot" ]; then
96         echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$requested_chroot apt-get $APTCMD $PACKAGES'"
97         /usr/sbin/chroot $CHROOT_DIR/"$requested_chroot" apt-get "$APTCMD" $PACKAGES
98 elif [ "$method" = "schroot" ]; then
99         echo "Will run 'schroot -c $requested_chroot apt-get $APTCMD $PACKAGES'"
100         schroot -c "$requested_chroot" apt-get "$APTCMD" $PACKAGES
101 else
102         echo >&2 "Invalid method."
103         exit 1
104 fi