chiark / gitweb /
9082965e06192d672e3960cd5270a015bac46817
[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 #
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software Foundation,
19 #  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
20
21
22 VALID_CHROOTS="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /usr/bin/tr ' ' '\n')"
23 VALID_CHROOTS_PARSE="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /bin/sed -e 's/ /|/g')"
24
25 usage() {
26         bn="`basename "$0"`"
27         echo "Usage: $bn <chroot> <apt sub-command> [<package>]"
28         echo ""
29         echo "  allowed chroots are ($VALID_CHROOTS_PARSE)"
30         echo ""
31         echo "  allowed apt sub-commands are:"
32         echo "          + install"
33         echo "          + remove"
34         echo "          + purge"
35         echo "          + build-dep"
36         echo "          + update"
37 }
38
39
40 if [ "$#" -lt "3" ]; then
41         usage >&2
42         exit 1
43 fi
44
45 CHROOT_DIR="/chroot"
46 CHROOT=$1; shift
47 APTCMD=$1; shift
48 PACKAGES=$*
49
50
51 case "$APTCMD" in
52 install|remove|purge|build-dep|update)
53         # those are the allowed apt sub-commands
54         ;;
55 *)
56         echo "$APTCMD is not a valid apt sub-command"
57         exit 1
58         ;;
59 esac
60
61 if echo "$VALID_CHROOTS" | grep -qFx "$CHROOT"; then
62         # valid chroot
63         echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES'"
64         /usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES
65 else
66         echo "$CHROOT not in $VALID_CHROOTS_PARSE"
67         exit 1
68 fi
69
70