chiark / gitweb /
add apt-in-chroot
authorMartin Zobel-Helas <zobel@debian.org>
Thu, 18 Mar 2010 21:35:40 +0000 (22:35 +0100)
committerMartin Zobel-Helas <zobel@debian.org>
Thu, 18 Mar 2010 21:35:40 +0000 (22:35 +0100)
apt-in-chroot [new file with mode: 0755]

diff --git a/apt-in-chroot b/apt-in-chroot
new file mode 100755 (executable)
index 0000000..279b95f
--- /dev/null
@@ -0,0 +1,72 @@
+#!/bin/sh
+# 
+#  apt-in-chroot - runs apt in a specified chroot
+# 
+#  Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2, or (at your option)
+#  any later version.
+# 
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+# 
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
+# 
+
+VALID_CHROOTS="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /usr/bin/tr ' ' '\n')"
+VALID_CHROOTS_PARSE="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /bin/sed -e 's/ /|/g')"
+
+usage() {
+        bn="`basename "$0"`"
+       echo "Usage: $bn <chroot> <apt sub-command> <package>"
+       echo ""
+       echo "  allowed chroots are ($VALID_CHROOTS_PARSE)"
+       echo ""
+       echo "  allowed apt sub-commands are:"
+       echo "          + install"
+       echo "          + remove"
+       echo "          + purge"
+       echo "          + build-dep"
+}
+
+
+if [ "$#" -lt "3" ]; then
+       usage >&2
+       exit 1
+fi
+
+CHROOT_DIR="/chroot"
+CHROOT=$1; shift
+APTCMD=$1; shift
+PACKAGES=$*
+
+
+case "$APTCMD" in
+install|remove|purge|build-dep)
+       # those are the allowed apt sub-commands
+       ;;
+*)
+       echo "$APTCMD is not a valid apt sub-command"
+       ;;
+esac
+
+grep -qFx $CHROOT <<< "$VALID_CHROOTS"
+
+RET=$?
+
+if [ "$RET" == "0" ]; then
+       # valid chroot
+       echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES'"
+       /usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES
+else
+       echo "$CHROOT not in $VALID_CHROOTS_PARSE"
+       exit 1
+fi
+
+