Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Configurable params |
| 3 | BRIDGE=${BRIDGE:-br0} |
| 4 | CONTAINER=${CONTAINER:-TESTER} |
| 5 | CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} |
| 6 | CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} |
| 7 | CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0} |
| 8 | CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1} |
| 9 | NAMESERVER=${NAMESERVER:-192.168.1.1} |
| 10 | COPYENV=${COPYENV:-1} |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 11 | WARMCACHE=${WARMCACHE:-0} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 12 | |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 13 | # Shutdown any existing container |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 14 | lxc-stop -n $CONTAINER |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 15 | |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 16 | # This prevents zombie containers |
Anthony Young | eeba886 | 2011-09-13 03:02:38 -0700 | [diff] [blame] | 17 | cgdelete -r cpu,net_cls:$CONTAINER |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 18 | |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 19 | # Destroy the old container |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 20 | lxc-destroy -n $CONTAINER |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 21 | |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 22 | # Warm the base image on first run or when WARMCACHE=1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 23 | CACHEDIR=/var/cache/lxc/natty/rootfs-amd64 |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 24 | if [ "$WARMCACHE" = "1" ] || [ ! -d $CACHEDIR ]; then |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 25 | 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 |
| 31 | fi |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 32 | |
| 33 | # Create network configuration |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 34 | LXC_CONF=/tmp/net.conf |
| 35 | cat > $LXC_CONF <<EOF |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 36 | lxc.network.type = veth |
| 37 | lxc.network.link = $BRIDGE |
| 38 | lxc.network.flags = up |
| 39 | lxc.network.ipv4 = $CONTAINER_CIDR |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 40 | # allow tap/tun devices |
Anthony Young | f49d7ee | 2011-09-13 03:29:52 -0700 | [diff] [blame] | 41 | lxc.cgroup.devices.allow = c 10:200 rwm |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 42 | EOF |
| 43 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 44 | # Create the container |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 45 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 46 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 47 | # Specify where our container lives |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 48 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 49 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 50 | # set root password to password |
| 51 | echo 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. |
| 55 | chroot $ROOTFS groupadd libvirtd |
| 56 | chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd |
| 57 | |
| 58 | # a simple password - pass |
| 59 | echo stack:pass | chroot $ROOTFS chpasswd |
| 60 | |
| 61 | # and has sudo ability (in the future this should be limited to only what |
| 62 | # stack requires) |
| 63 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 64 | |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 65 | # Copy over your ssh keys and env if desired |
| 66 | if [ "$COPYENV" = "1" ]; then |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 67 | cp -pr ~/.ssh $ROOTFS/root/.ssh |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 68 | cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 69 | cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig |
| 70 | cp -pr ~/.vimrc $ROOTFS/root/.vimrc |
| 71 | cp -pr ~/.bashrc $ROOTFS/root/.bashrc |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 72 | |
| 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 Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 78 | fi |
| 79 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 80 | # give stack ownership over /opt so it may do the work needed |
| 81 | chroot $ROOTFS chown -R stack /opt |
| 82 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 83 | # Configure instance network |
| 84 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 85 | cat > $INTERFACES <<EOF |
| 86 | auto lo |
| 87 | iface lo inet loopback |
| 88 | |
| 89 | auto eth0 |
| 90 | iface eth0 inet static |
| 91 | address $CONTAINER_IP |
| 92 | netmask $CONTAINER_NETMASK |
| 93 | gateway $CONTAINER_GATEWAY |
| 94 | EOF |
| 95 | |
| 96 | # Configure the first run installer |
| 97 | INSTALL_SH=$ROOTFS/root/install.sh |
| 98 | cat > $INSTALL_SH <<EOF |
| 99 | #!/bin/bash |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 100 | # Disable startup script |
Anthony Young | eeba886 | 2011-09-13 03:02:38 -0700 | [diff] [blame] | 101 | echo \#\!/bin/sh -e > /etc/rc.local |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame^] | 102 | # Make sure dns is set up |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 103 | echo "nameserver $NAMESERVER" | resolvconf -a eth0 |
| 104 | sleep 1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 105 | |
| 106 | # Install and run stack.sh |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame] | 107 | apt-get update |
| 108 | apt-get -y --force-yes install git-core vim-nox sudo |
Anthony Young | bdbe6d9 | 2011-09-13 09:43:46 -0700 | [diff] [blame] | 109 | su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack |
| 110 | su -c "cd ~/nfs-stack && ./stack.sh" stack |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 111 | EOF |
| 112 | |
| 113 | chmod 700 $INSTALL_SH |
| 114 | |
| 115 | # Make installer run on boot |
| 116 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 117 | cat > $RC_LOCAL <<EOF |
| 118 | #!/bin/sh -e |
| 119 | /root/install.sh |
| 120 | EOF |
| 121 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 122 | # Configure cgroup directory |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 123 | mkdir -p /cgroup |
| 124 | mount none -t cgroup /cgroup |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 125 | |
| 126 | # Start our container |
| 127 | lxc-start -d -n $CONTAINER |
Anthony Young | eeba886 | 2011-09-13 03:02:38 -0700 | [diff] [blame] | 128 | |
| 129 | cat << EOF > /bin/remove_dead_cgroup.shecho |
| 130 | "Removing dead cgroup .$CONTAINER." >> /var/log/cgroup |
| 131 | rmdir /cgroup/$CONTAINER >> /var/log/cgroup 2>&1 |
| 132 | echo "return value was $?" >> /var/log/cgroup |
| 133 | EOF |
| 134 | chmod 755 /bin/remove_dead_cgroup.sh |
| 135 | echo /bin/remove_dead_cgroup.sh > /cgroup/release_agent |
| 136 | echo 1 > /cgroup/notify_on_release |