blob: 1a2cea3e4879590fd2adac19bb91d1e15058dffd [file] [log] [blame]
Anthony Young72d69632011-09-12 21:09:55 -07001#!/bin/bash
2# Configurable params
3BRIDGE=${BRIDGE:-br0}
4CONTAINER=${CONTAINER:-TESTER}
5CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
6CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
7CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
8CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
9NAMESERVER=${NAMESERVER:-192.168.1.1}
10COPYENV=${COPYENV:-1}
Anthony Youngb3c04542011-09-13 01:28:18 -070011WARMCACHE=${WARMCACHE:-0}
Anthony Young72d69632011-09-12 21:09:55 -070012
13# Destroy any existing container
14lxc-stop -n $CONTAINER
Anthony Youngb3c04542011-09-13 01:28:18 -070015sleep 1
Anthony Young72d69632011-09-12 21:09:55 -070016lxc-destroy -n $CONTAINER
Anthony Youngb3c04542011-09-13 01:28:18 -070017sleep 1
Anthony Young72d69632011-09-12 21:09:55 -070018
19# Create network configuration
20NET_CONF=/tmp/net.conf
21cat > $NET_CONF <<EOF
22lxc.network.type = veth
23lxc.network.link = $BRIDGE
24lxc.network.flags = up
25lxc.network.ipv4 = $CONTAINER_CIDR
26EOF
27
28# Configure the network
29lxc-create -n $CONTAINER -t natty -f $NET_CONF
30
Anthony Youngb3c04542011-09-13 01:28:18 -070031if [ "$WARMCACHE" = "1" ]; then
32 # Pre-cache files
33 BASECACHE=/var/cache/lxc/natty/rootfs-amd64
34 chroot $BASECACHE apt-get update
35 chroot $BASECACHE apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
36 chroot $BASECACHE pip install `cat pips/*`
37fi
38
Anthony Young72d69632011-09-12 21:09:55 -070039# Where our container lives
40ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
41
Anthony Youngb3c04542011-09-13 01:28:18 -070042# Copy over your ssh keys and env if desired
43if [ "$COPYENV" = "1" ]; then
Anthony Young72d69632011-09-12 21:09:55 -070044 cp -pr ~/.ssh $ROOTFS/root/.ssh
Anthony Youngb3c04542011-09-13 01:28:18 -070045 cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys
Anthony Young72d69632011-09-12 21:09:55 -070046 cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig
47 cp -pr ~/.vimrc $ROOTFS/root/.vimrc
48 cp -pr ~/.bashrc $ROOTFS/root/.bashrc
49fi
50
51# Configure instance network
52INTERFACES=$ROOTFS/etc/network/interfaces
53cat > $INTERFACES <<EOF
54auto lo
55iface lo inet loopback
56
57auto eth0
58iface eth0 inet static
59 address $CONTAINER_IP
60 netmask $CONTAINER_NETMASK
61 gateway $CONTAINER_GATEWAY
62EOF
63
64# Configure the first run installer
65INSTALL_SH=$ROOTFS/root/install.sh
66cat > $INSTALL_SH <<EOF
67#!/bin/bash
68echo "nameserver $NAMESERVER" | resolvconf -a eth0
69sleep 1
Anthony Younge2c3a372011-09-12 23:25:37 -070070apt-get update
71apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young72d69632011-09-12 21:09:55 -070072git clone git://github.com/cloudbuilders/nfs-stack.git /root/nfs-stack
73EOF
74
75chmod 700 $INSTALL_SH
76
77# Make installer run on boot
78RC_LOCAL=$ROOTFS/etc/rc.local
79cat > $RC_LOCAL <<EOF
80#!/bin/sh -e
81/root/install.sh
82EOF
83
Anthony Young5f6c93b2011-09-13 00:04:57 -070084# Setup cache
Anthony Younge2c3a372011-09-12 23:25:37 -070085# FIXME - use proper fstab mount
86CWD=`pwd`
Anthony Young5f6c93b2011-09-13 00:04:57 -070087CACHEDIR=$CWD/cache/
88mkdir -p $CACHEDIR/apt
89mkdir -p $CACHEDIR/pip
90cp -pr $CACHEDIR/apt/* $ROOTFS/var/cache/apt/
91cp -pr $CACHEDIR/pip/* $ROOTFS/var/cache/pip/
Anthony Younge2c3a372011-09-12 23:25:37 -070092
Anthony Young72d69632011-09-12 21:09:55 -070093# Configure cgroup directory
Anthony Youngb3c04542011-09-13 01:28:18 -070094if [ ! -d /cgroup ] ; then
95 mkdir -p /cgroup
96 mount none -t cgroup /cgroup
97fi
Anthony Young72d69632011-09-12 21:09:55 -070098
99# Start our container
100lxc-start -d -n $CONTAINER