Skip to content
Browse files

various tweaks

  • Loading branch information...
1 parent dbf1728 commit d5befd0b47ee2cd3d357d1cf27a5d3b518b7ed35 @infertux committed Jun 30, 2012
Showing with 88 additions and 22 deletions.
  1. +18 −14 lxc-create-debian-custom.sh
  2. +3 −1 lxc-network.sh
  3. +5 −1 lxc-start-all.sh
  4. +3 −1 lxc-stop-all.sh
  5. +59 −5 lxc-upgrade-all.sh
View
32 lxc-create-debian-custom.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# LXC tools
-# Copyright (C) 2011 Infertux <infertux@infertux.com>
+# Copyright (C) 2012 Infertux <infertux@infertux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -20,22 +20,26 @@
# You may probably ajust the following constants before using this script.
-NETWORK=10.1.2
-GATEWAY=192.168.1.1
-INTERFACE=eth0
-BRIDGE=br0
+NETWORK=10.1.2 # local network without the machine part of the address
+NETMASK=255.255.255.0
+GATEWAY=192.168.1.1 # can be on a different network
+INTERFACE=eth0 # network interface in containers
+BRIDGE=br0 # host bridge for containers
ARCH=amd64
-VERSION=squeeze
+VERSION=wheezy
PACKAGES=ifupdown,netbase,net-tools
# Other useful packages: dialog, iproute
PASSWORD=ChangeMe
+###
+
+set -eu
usage()
{
- cat <<EOF
+ cat >&2 <<EOF
$(basename $0) -h|--help -n|--name=<name> -i|--ip=<last-byte>
Example: $(basename $0) -n test -i 42
EOF
@@ -59,17 +63,17 @@ do
-h|--help) usage $0 && exit 0;;
-n|--name) name=$2; shift 2;;
-i|--ip) ip=$2; shift 2;;
- --) shift 1; break ;;
+ --) shift; break ;;
*) break ;;
esac
done
-type debootstrap || fail "'debootstrap' command is missing"
+command -v debootstrap >/dev/null || fail "You need to install 'debootstrap'."
-[ "$name" ] || fail "'name' parameter is required"
-[ "$ip" ] || fail "'ip' parameter is required"
+[ "$name" ] || fail "'name' parameter is required."
+[ "$ip" ] || fail "'ip' parameter is required."
-[ $UID -ne 0 ] && fail "This script should be run as 'root'"
+[ $UID -ne 0 ] && fail "This script must be run as root."
path="/var/lib/lxc/$name"
rootfs="$path/rootfs"
@@ -111,7 +115,7 @@ iface lo inet loopback
auto $INTERFACE
iface $INTERFACE inet static
address $NETWORK.$ip
- netmask 255.255.255.0
+ netmask $NETMASK
up route add -host $GATEWAY dev $INTERFACE
up route add default gw $GATEWAY dev $INTERFACE
EOF
@@ -184,5 +188,5 @@ lxc.network.ipv4 = $NETWORK.$ip/24
EOF
echo
-echo "Container ready, start it with: lxc-start -n $name."
+echo "Container ready, you can start it as a daemon with: lxc-start -dn $name."
View
4 lxc-network.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# LXC tools
-# Copyright (C) 2011 Infertux <infertux@infertux.com>
+# Copyright (C) 2012 Infertux <infertux@infertux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -18,6 +18,8 @@
# Print IP addresses of all containers.
+set -eu
+
LXC=/var/lib/lxc
grep ip $LXC/*/config | sed "s|$LXC/\(.*\)/.*network\.\(.*\)$|\1 \t\2|"
View
6 lxc-start-all.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# LXC tools
-# Copyright (C) 2011 Infertux <infertux@infertux.com>
+# Copyright (C) 2012 Infertux <infertux@infertux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -20,6 +20,10 @@
# List of containers you want to start at boot
CONTAINERS="web mail test"
+###
+
+set -eu
+
for container in $CONTAINERS; do
if [ "$(lxc-info -n $container)" != "'$container' is STOPPED" ]; then
echo "'$container' is already running, skipping."
View
4 lxc-stop-all.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# LXC tools
-# Copyright (C) 2011 Infertux <infertux@infertux.com>
+# Copyright (C) 2012 Infertux <infertux@infertux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -19,6 +19,8 @@
# Invoke halt command on every running container.
# Especially useful to shutdown properly all containers before rebooting the LXC host.
+set -eu
+
LXC=/var/lib/lxc
ROOTFS=rootfs
RUNNING_CONTAINERS="$(netstat -xa | grep $LXC | sed -e 's#.*'"$LXC/"'\(.*\)/command#\1#')"
View
64 lxc-upgrade-all.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# LXC tools
-# Copyright (C) 2011 Infertux <infertux@infertux.com>
+# Copyright (C) 2012 Infertux <infertux@infertux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -17,22 +17,76 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Check for updates on Debian-based containers.
-# Pass --interactive flag (or anything ;)) to give you the possibility to upgrade.
+# Pass -i flag to give you the possibility to upgrade.
+
+set -eu
LXC=/var/lib/lxc
ROOTFS=rootfs
UPGRADE_CMD="apt-get dist-upgrade"
RUNNING_CONTAINERS="$(netstat -xa | grep $LXC | sed -e 's#.*'"$LXC/"'\(.*\)/command#\1#')"
-INTERACTIVE="$1"
+INTERACTIVE=0
+
+usage()
+{
+ cat >&2 <<EOF
+$(basename $0) -h|--help -i|--interactive
+Example: $(basename $0) -i
+EOF
+ exit 0
+}
+
+options=$(getopt -o hi -l help,interactive -- "$@")
+[ $# -gt 1 -o $? -ne 0 ] && usage
+
+eval set -- "$options"
+
+while true
+do
+ case "$1" in
+ -h|--help) usage $0 && exit 0;;
+ -i|--interactive) INTERACTIVE=1; shift;;
+ --) shift; break ;;
+ *) break ;;
+ esac
+done
+
+
+trap quit INT TERM
+
+mount_special_fs() {
+ mount -t proc proc $1/proc/
+ mount -t sysfs sys $1/sys/
+ mount -t devpts dev/pts $1/dev/pts
+}
+
+umount_special_fs() {
+ umount $1/proc/
+ umount $1/sys/
+ umount $1/dev/pts
+}
+
+quit() {
+ echo "Oops"
+}
for container in $RUNNING_CONTAINERS; do
chroot $LXC/$container/$ROOTFS apt-get update -qq
- updates="$(chroot $LXC/$container/$ROOTFS $UPGRADE_CMD -qs | grep '^ ')"
+ updates="$(chroot $LXC/$container/$ROOTFS $UPGRADE_CMD -qs | grep '^ ' || true)"
if [ "$updates" ]; then
- [ "$INTERACTIVE" ] && chroot $LXC/$container/$ROOTFS $UPGRADE_CMD || echo -e "Updates for $container:\n$updates"
+ if [ $INTERACTIVE -eq 1 ]; then
+ rootfs=$LXC/$container/$ROOTFS
+ mount_special_fs $rootfs
+ chroot $rootfs $UPGRADE_CMD || true
+ umount_special_fs $rootfs
+ else
+ echo -e "Updates for $container:\n$updates"
+ fi
else
echo "$container is up-to-date."
fi
done
+echo "Done."
+

0 comments on commit d5befd0

Please sign in to comment.
Something went wrong with that request. Please try again.