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} |
| 11 | |
| 12 | # Destroy any existing container |
| 13 | lxc-stop -n $CONTAINER |
| 14 | lxc-destroy -n $CONTAINER |
| 15 | |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame^] | 16 | FSTAB=/tmp/fstab |
| 17 | cat > $FSTAB <<EOF |
| 18 | none /var/lib/lxc/$CONTAINER/dev/pts devpts defaults 0 0 |
| 19 | none /var/lib/lxc/$CONTAINER/proc proc defaults 0 0 |
| 20 | none /var/lib/lxc/$CONTAINER/sys sysfs defaults 0 0 |
| 21 | none /var/lib/lxc/$CONTAINER/var/lock tmpfs defaults 0 0 |
| 22 | none /var/lib/lxc/$CONTAINER/var/run tmpfs defaults 0 0 |
| 23 | /var/cache/apt/ $APTCACHEDIR none bind 0 0 |
| 24 | EOF |
| 25 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 26 | # Create network configuration |
| 27 | NET_CONF=/tmp/net.conf |
| 28 | cat > $NET_CONF <<EOF |
| 29 | lxc.network.type = veth |
| 30 | lxc.network.link = $BRIDGE |
| 31 | lxc.network.flags = up |
| 32 | lxc.network.ipv4 = $CONTAINER_CIDR |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame^] | 33 | lxc.mount = $FSTAB |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 34 | EOF |
| 35 | |
| 36 | # Configure the network |
| 37 | lxc-create -n $CONTAINER -t natty -f $NET_CONF |
| 38 | |
| 39 | # Where our container lives |
| 40 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 41 | |
| 42 | # Copy over your ssh keys if desired |
| 43 | if [ $COPYENV ]; then |
| 44 | cp -pr ~/.ssh $ROOTFS/root/.ssh |
| 45 | cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig |
| 46 | cp -pr ~/.vimrc $ROOTFS/root/.vimrc |
| 47 | cp -pr ~/.bashrc $ROOTFS/root/.bashrc |
| 48 | fi |
| 49 | |
| 50 | # Configure instance network |
| 51 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 52 | cat > $INTERFACES <<EOF |
| 53 | auto lo |
| 54 | iface lo inet loopback |
| 55 | |
| 56 | auto eth0 |
| 57 | iface eth0 inet static |
| 58 | address $CONTAINER_IP |
| 59 | netmask $CONTAINER_NETMASK |
| 60 | gateway $CONTAINER_GATEWAY |
| 61 | EOF |
| 62 | |
| 63 | # Configure the first run installer |
| 64 | INSTALL_SH=$ROOTFS/root/install.sh |
| 65 | cat > $INSTALL_SH <<EOF |
| 66 | #!/bin/bash |
| 67 | echo "nameserver $NAMESERVER" | resolvconf -a eth0 |
| 68 | sleep 1 |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame^] | 69 | apt-get update |
| 70 | apt-get -y --force-yes install git-core vim-nox sudo |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 71 | git clone git://github.com/cloudbuilders/nfs-stack.git /root/nfs-stack |
| 72 | EOF |
| 73 | |
| 74 | chmod 700 $INSTALL_SH |
| 75 | |
| 76 | # Make installer run on boot |
| 77 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 78 | cat > $RC_LOCAL <<EOF |
| 79 | #!/bin/sh -e |
| 80 | /root/install.sh |
| 81 | EOF |
| 82 | |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame^] | 83 | # Setup apt cache |
| 84 | # FIXME - use proper fstab mount |
| 85 | CWD=`pwd` |
| 86 | APTCACHEDIR=$CWD/cache/apt |
| 87 | mkdir -p $APTCACHEDIR |
| 88 | cp -pr $APTCACHEDIR/* $ROOTFS/var/cache/apt/ |
| 89 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 90 | # Configure cgroup directory |
| 91 | mkdir -p /cgroup |
| 92 | mount none -t cgroup /cgroup |
| 93 | |
| 94 | # Start our container |
| 95 | lxc-start -d -n $CONTAINER |