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