blob: 628ac856242ad3b7828e2fbe48f659bb0cd1fe30 [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
Anthony Young40203cb2011-09-13 09:17:56 -070013# Shutdown any existing container
Anthony Young72d69632011-09-12 21:09:55 -070014lxc-stop -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -070015sleep 1
Anthony Young40203cb2011-09-13 09:17:56 -070016# This prevents zombie containers
Anthony Youngeeba8862011-09-13 03:02:38 -070017cgdelete -r cpu,net_cls:$CONTAINER
18sleep 1
Anthony Young40203cb2011-09-13 09:17:56 -070019# Destroy the old container
Anthony Young72d69632011-09-12 21:09:55 -070020lxc-destroy -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -070021sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070022
Anthony Young40203cb2011-09-13 09:17:56 -070023# Warm the base image on first run or when WARMCACHE=1
Anthony Young99003e72011-09-13 02:05:12 -070024CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40203cb2011-09-13 09:17:56 -070025if [ "$WARMCACHE" = "1" ] || [ ! -d $CACHEDIR ]; then
Anthony Young99003e72011-09-13 02:05:12 -070026 if [ -d $CACHEDIR ]; then
27 # Pre-cache files
28 chroot $CACHEDIR apt-get update
29 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
30 chroot $CACHEDIR pip install `cat pips/*`
31 fi
32fi
Anthony Young72d69632011-09-12 21:09:55 -070033
34# Create network configuration
Anthony Young40203cb2011-09-13 09:17:56 -070035LXC_CONF=/tmp/net.conf
36cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070037lxc.network.type = veth
38lxc.network.link = $BRIDGE
39lxc.network.flags = up
40lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070041# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070042lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070043EOF
44
45# Configure the network
Anthony Young40203cb2011-09-13 09:17:56 -070046lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Young99003e72011-09-13 02:05:12 -070047sleep 2
Anthony Youngb3c04542011-09-13 01:28:18 -070048
Anthony Young72d69632011-09-12 21:09:55 -070049# Where our container lives
50ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
51
Anthony Youngb3c04542011-09-13 01:28:18 -070052# Copy over your ssh keys and env if desired
53if [ "$COPYENV" = "1" ]; then
Anthony Young72d69632011-09-12 21:09:55 -070054 cp -pr ~/.ssh $ROOTFS/root/.ssh
Anthony Youngb3c04542011-09-13 01:28:18 -070055 cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys
Anthony Young72d69632011-09-12 21:09:55 -070056 cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig
57 cp -pr ~/.vimrc $ROOTFS/root/.vimrc
58 cp -pr ~/.bashrc $ROOTFS/root/.bashrc
59fi
60
61# Configure instance network
62INTERFACES=$ROOTFS/etc/network/interfaces
63cat > $INTERFACES <<EOF
64auto lo
65iface lo inet loopback
66
67auto eth0
68iface eth0 inet static
69 address $CONTAINER_IP
70 netmask $CONTAINER_NETMASK
71 gateway $CONTAINER_GATEWAY
72EOF
73
74# Configure the first run installer
75INSTALL_SH=$ROOTFS/root/install.sh
76cat > $INSTALL_SH <<EOF
77#!/bin/bash
Anthony Youngeeba8862011-09-13 03:02:38 -070078echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young72d69632011-09-12 21:09:55 -070079echo "nameserver $NAMESERVER" | resolvconf -a eth0
80sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070081# Create a stack user that is a member of the libvirtd group so that stack
82# is able to interact with libvirt.
83groupadd libvirtd
84useradd stack -s /bin/bash -d /opt -G libvirtd
85
86# a simple password - pass
87echo stack:pass | chpasswd
88
89# give stack ownership over /opt so it may do the work needed
90chown -R stack /opt
91
92# and has sudo ability (in the future this should be limited to only what
93# stack requires)
94
95echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
96
97# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070098apt-get update
99apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngbdbe6d92011-09-13 09:43:46 -0700100su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
101su -c "cd ~/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700102EOF
103
104chmod 700 $INSTALL_SH
105
106# Make installer run on boot
107RC_LOCAL=$ROOTFS/etc/rc.local
108cat > $RC_LOCAL <<EOF
109#!/bin/sh -e
110/root/install.sh
111EOF
112
Anthony Young72d69632011-09-12 21:09:55 -0700113# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700114mkdir -p /cgroup
115mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700116
117# Start our container
118lxc-start -d -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -0700119
120cat << EOF > /bin/remove_dead_cgroup.shecho
121"Removing dead cgroup .$CONTAINER." >> /var/log/cgroup
122rmdir /cgroup/$CONTAINER >> /var/log/cgroup 2>&1
123echo "return value was $?" >> /var/log/cgroup
124EOF
125chmod 755 /bin/remove_dead_cgroup.sh
126echo /bin/remove_dead_cgroup.sh > /cgroup/release_agent
127echo 1 > /cgroup/notify_on_release