blob: 5e07b1ac232789bb7616ab3b4f3b4ec09881d359 [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 Young7c3e5ed2011-09-13 09:57:31 -070015
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
Anthony Young7c3e5ed2011-09-13 09:57:31 -070018
Anthony Young40203cb2011-09-13 09:17:56 -070019# Destroy the old container
Anthony Young72d69632011-09-12 21:09:55 -070020lxc-destroy -n $CONTAINER
Anthony Young99003e72011-09-13 02:05:12 -070021
Anthony Young40203cb2011-09-13 09:17:56 -070022# Warm the base image on first run or when WARMCACHE=1
Anthony Young99003e72011-09-13 02:05:12 -070023CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40203cb2011-09-13 09:17:56 -070024if [ "$WARMCACHE" = "1" ] || [ ! -d $CACHEDIR ]; then
Anthony Young99003e72011-09-13 02:05:12 -070025 if [ -d $CACHEDIR ]; then
26 # Pre-cache files
27 chroot $CACHEDIR apt-get update
28 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
29 chroot $CACHEDIR pip install `cat pips/*`
30 fi
31fi
Anthony Young72d69632011-09-12 21:09:55 -070032
33# Create network configuration
Anthony Young40203cb2011-09-13 09:17:56 -070034LXC_CONF=/tmp/net.conf
35cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070036lxc.network.type = veth
37lxc.network.link = $BRIDGE
38lxc.network.flags = up
39lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070040# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070041lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070042EOF
43
Anthony Young7c3e5ed2011-09-13 09:57:31 -070044# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070045lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070046
Anthony Young7c3e5ed2011-09-13 09:57:31 -070047# Specify where our container lives
Anthony Young72d69632011-09-12 21:09:55 -070048ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
49
Anthony Young7c3e5ed2011-09-13 09:57:31 -070050# set root password to password
51echo root:pass | chroot $ROOTFS chpasswd
52
53# Create a stack user that is a member of the libvirtd group so that stack
54# is able to interact with libvirt.
55chroot $ROOTFS groupadd libvirtd
56chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
57
58# a simple password - pass
59echo stack:pass | chroot $ROOTFS chpasswd
60
61# and has sudo ability (in the future this should be limited to only what
62# stack requires)
63echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
64
Anthony Youngb3c04542011-09-13 01:28:18 -070065# Copy over your ssh keys and env if desired
66if [ "$COPYENV" = "1" ]; then
Anthony Young72d69632011-09-12 21:09:55 -070067 cp -pr ~/.ssh $ROOTFS/root/.ssh
Anthony Youngb3c04542011-09-13 01:28:18 -070068 cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys
Anthony Young72d69632011-09-12 21:09:55 -070069 cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig
70 cp -pr ~/.vimrc $ROOTFS/root/.vimrc
71 cp -pr ~/.bashrc $ROOTFS/root/.bashrc
Anthony Young7c3e5ed2011-09-13 09:57:31 -070072
73 cp -pr ~/.ssh $ROOTFS/opt/.ssh
74 cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
75 cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
76 cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
77 cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070078fi
79
Anthony Young10039522011-09-13 10:05:07 -070080# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070081chroot $ROOTFS chown -R stack /opt
82
Anthony Young72d69632011-09-12 21:09:55 -070083# Configure instance network
84INTERFACES=$ROOTFS/etc/network/interfaces
85cat > $INTERFACES <<EOF
86auto lo
87iface lo inet loopback
88
89auto eth0
90iface eth0 inet static
91 address $CONTAINER_IP
92 netmask $CONTAINER_NETMASK
93 gateway $CONTAINER_GATEWAY
94EOF
95
96# Configure the first run installer
97INSTALL_SH=$ROOTFS/root/install.sh
98cat > $INSTALL_SH <<EOF
99#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700100# Disable startup script
Anthony Youngeeba8862011-09-13 03:02:38 -0700101echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700102# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -0700103echo "nameserver $NAMESERVER" | resolvconf -a eth0
104sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700105
106# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700107apt-get update
108apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngbdbe6d92011-09-13 09:43:46 -0700109su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
110su -c "cd ~/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700111EOF
112
113chmod 700 $INSTALL_SH
114
115# Make installer run on boot
116RC_LOCAL=$ROOTFS/etc/rc.local
117cat > $RC_LOCAL <<EOF
118#!/bin/sh -e
119/root/install.sh
120EOF
121
Anthony Young72d69632011-09-12 21:09:55 -0700122# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700123mkdir -p /cgroup
124mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700125
126# Start our container
127lxc-start -d -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -0700128
129cat << EOF > /bin/remove_dead_cgroup.shecho
130"Removing dead cgroup .$CONTAINER." >> /var/log/cgroup
131rmdir /cgroup/$CONTAINER >> /var/log/cgroup 2>&1
132echo "return value was $?" >> /var/log/cgroup
133EOF
134chmod 755 /bin/remove_dead_cgroup.sh
135echo /bin/remove_dead_cgroup.sh > /cgroup/release_agent
136echo 1 > /cgroup/notify_on_release