blob: 2ef5c087327cfbb7ceceb9a49d8aa39c7a737d40 [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 Youngeeba8862011-09-13 03:02:38 -070015sleep 1
16cgdelete -r cpu,net_cls:$CONTAINER
17sleep 1
Anthony Young72d69632011-09-12 21:09:55 -070018lxc-destroy -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -070019sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070020
21CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
22if [ "$WARMCACHE" = "1" ]; then
23 if [ -d $CACHEDIR ]; then
24 # Pre-cache files
25 chroot $CACHEDIR apt-get update
26 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
27 chroot $CACHEDIR pip install `cat pips/*`
28 fi
29fi
Anthony Young72d69632011-09-12 21:09:55 -070030
31# Create network configuration
32NET_CONF=/tmp/net.conf
33cat > $NET_CONF <<EOF
34lxc.network.type = veth
35lxc.network.link = $BRIDGE
36lxc.network.flags = up
37lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Youngf49d7ee2011-09-13 03:29:52 -070038lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070039EOF
40
41# Configure the network
42lxc-create -n $CONTAINER -t natty -f $NET_CONF
Anthony Young99003e72011-09-13 02:05:12 -070043sleep 2
Anthony Youngb3c04542011-09-13 01:28:18 -070044
Anthony Young72d69632011-09-12 21:09:55 -070045# Where our container lives
46ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
47
Anthony Youngb3c04542011-09-13 01:28:18 -070048# Copy over your ssh keys and env if desired
49if [ "$COPYENV" = "1" ]; then
Anthony Young72d69632011-09-12 21:09:55 -070050 cp -pr ~/.ssh $ROOTFS/root/.ssh
Anthony Youngb3c04542011-09-13 01:28:18 -070051 cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys
Anthony Young72d69632011-09-12 21:09:55 -070052 cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig
53 cp -pr ~/.vimrc $ROOTFS/root/.vimrc
54 cp -pr ~/.bashrc $ROOTFS/root/.bashrc
55fi
56
57# Configure instance network
58INTERFACES=$ROOTFS/etc/network/interfaces
59cat > $INTERFACES <<EOF
60auto lo
61iface lo inet loopback
62
63auto eth0
64iface eth0 inet static
65 address $CONTAINER_IP
66 netmask $CONTAINER_NETMASK
67 gateway $CONTAINER_GATEWAY
68EOF
69
70# Configure the first run installer
71INSTALL_SH=$ROOTFS/root/install.sh
72cat > $INSTALL_SH <<EOF
73#!/bin/bash
Anthony Youngeeba8862011-09-13 03:02:38 -070074echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young72d69632011-09-12 21:09:55 -070075echo "nameserver $NAMESERVER" | resolvconf -a eth0
76sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070077# Create a stack user that is a member of the libvirtd group so that stack
78# is able to interact with libvirt.
79groupadd libvirtd
80useradd stack -s /bin/bash -d /opt -G libvirtd
81
82# a simple password - pass
83echo stack:pass | chpasswd
84
85# give stack ownership over /opt so it may do the work needed
86chown -R stack /opt
87
88# and has sudo ability (in the future this should be limited to only what
89# stack requires)
90
91echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
92
93# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070094apt-get update
95apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young99003e72011-09-13 02:05:12 -070096su -c "git clone git://github.com/cloudbuilders/nfs-stack.git /opt/nfs-stack" stack
97su -c "cd /opt/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -070098EOF
99
100chmod 700 $INSTALL_SH
101
102# Make installer run on boot
103RC_LOCAL=$ROOTFS/etc/rc.local
104cat > $RC_LOCAL <<EOF
105#!/bin/sh -e
106/root/install.sh
107EOF
108
Anthony Young72d69632011-09-12 21:09:55 -0700109# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700110mkdir -p /cgroup
111mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700112
113# Start our container
114lxc-start -d -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -0700115
116cat << EOF > /bin/remove_dead_cgroup.shecho
117"Removing dead cgroup .$CONTAINER." >> /var/log/cgroup
118rmdir /cgroup/$CONTAINER >> /var/log/cgroup 2>&1
119echo "return value was $?" >> /var/log/cgroup
120EOF
121chmod 755 /bin/remove_dead_cgroup.sh
122echo /bin/remove_dead_cgroup.sh > /cgroup/release_agent
123echo 1 > /cgroup/notify_on_release