blob: 3ee1e2fb05eed5a56bb0a0c3be4c166a16c0060e [file] [log] [blame]
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07001#!/usr/bin/env bash
2
Jesse Andrews228f2462011-11-05 17:36:14 -07003# Make sure that we have the proper version of ubuntu (only works on natty/oneiric)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07004UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
5if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
6 if [ ! "natty" = "$UBUNTU_VERSION" ]; then
7 echo "This script only works with oneiric and natty"
8 exit 1
9 fi
10fi
11
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070012# Keep track of the current directory
13TOOLS_DIR=$(cd $(dirname "$0") && pwd)
14TOP_DIR=`cd $TOOLS_DIR/..; pwd`
15
Jesse Andrews53d75332011-11-06 07:54:11 -080016cd $TOP_DIR
17
18# Source params
19source ./stackrc
20
21# Ubuntu distro to install
22DIST_NAME=${DIST_NAME:-oneiric}
23
Jesse Andrews228f2462011-11-05 17:36:14 -070024# exit on error to stop unexpected errors
25set -o errexit
26set -o xtrace
27
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070028# Abort if localrc is not set
29if [ ! -e $TOP_DIR/localrc ]; then
30 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
31 echo "See stack.sh for required passwords."
32 exit 1
33fi
34
35# Install deps if needed
36dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
37
38# Where to store files and instances
39WORK_DIR=${WORK_DIR:-/opt/kvmstack}
40
41# Where to store images
Jesse Andrews228f2462011-11-05 17:36:14 -070042image_dir=$WORK_DIR/images/$DIST_NAME
43mkdir -p $image_dir
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070044
45# Original version of built image
Jesse Andrews228f2462011-11-05 17:36:14 -070046uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz
Jesse Andrews96dffbc2011-11-05 17:37:33 -070047tarball=$image_dir/$(basename $uec_url)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070048
49# download the base uec image if we haven't already
Jesse Andrews228f2462011-11-05 17:36:14 -070050if [ ! -f $tarball ]; then
51 curl $uec_url -o $tarball
Jesse Andrews3b768582011-11-05 17:40:20 -070052 (cd $image_dir && tar -Sxvzf $tarball)
Jesse Andrews9102d452011-11-05 23:49:08 -070053 resize-part-image $image_dir/*.img 10G $image_dir/disk
Jesse Andrews228f2462011-11-05 17:36:14 -070054 cp $image_dir/*-vmlinuz-virtual $image_dir/kernel
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070055fi
56
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070057
58# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
59ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
60
61# Name of our instance, used by libvirt
62GUEST_NAME=${GUEST_NAME:-devstack}
63
64# Mop up after previous runs
65virsh destroy $GUEST_NAME || true
66
67# Where this vm is stored
Jesse Andrews228f2462011-11-05 17:36:14 -070068vm_dir=$WORK_DIR/instances/$GUEST_NAME
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070069
70# Create vm dir and remove old disk
Jesse Andrews228f2462011-11-05 17:36:14 -070071mkdir -p $vm_dir
72rm -f $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070073
74# Create a copy of the base image
Jesse Andrewsf5a76912011-11-05 17:47:50 -070075qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070076
77# Back to devstack
78cd $TOP_DIR
79
80GUEST_NETWORK=${GUEST_NETWORK:-1}
81GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
82GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
83GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
84GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
85GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
86GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
87GUEST_RAM=${GUEST_RAM:-1524288}
88GUEST_CORES=${GUEST_CORES:-1}
89
90# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -070091NET_XML=$vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070092cat > $NET_XML <<EOF
93<network>
94 <name>devstack-$GUEST_NETWORK</name>
95 <bridge name="stackbr%d" />
96 <forward/>
Jesse Andrewsa6282622011-11-05 18:39:33 -070097 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK">
98 <dhcp>
Jesse Andrews02cc96c2011-11-06 10:29:10 -080099 <range start='192.168.$GUEST_NETWORK.2' end='192.168.$GUEST_NETWORK.127' />
Jesse Andrewsa6282622011-11-05 18:39:33 -0700100 </dhcp>
101 </ip>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700102</network>
103EOF
104
105if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
106 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrewsdca89002011-11-06 10:33:33 -0800107 # destroying the network isn't enough to delete the leases
108 rm -f /var/lib/libvirt/dnsmasq/devstack-$GUEST_NETWORK.leases
Jesse Andrews228f2462011-11-05 17:36:14 -0700109 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700110fi
111
112# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700113LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700114cat > $LIBVIRT_XML <<EOF
115<domain type='kvm'>
116 <name>$GUEST_NAME</name>
117 <memory>$GUEST_RAM</memory>
118 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700119 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700120 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700121 <cmdline>root=/dev/vda ro console=ttyS0 init=/usr/lib/cloud-init/uncloud-init ds=nocloud-net;s=http://192.168.$GUEST_NETWORK.1:4567/ ubuntu-pass=ubuntu</cmdline>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700122 </os>
123 <features>
124 <acpi/>
125 </features>
126 <clock offset='utc'/>
127 <vcpu>$GUEST_CORES</vcpu>
128 <devices>
129 <disk type='file'>
130 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700131 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700132 <target dev='vda' bus='virtio'/>
133 </disk>
134
135 <interface type='network'>
136 <source network='devstack-$GUEST_NETWORK'/>
137 </interface>
138
139 <!-- The order is significant here. File must be defined first -->
140 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700141 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700142 <target port='1'/>
143 </serial>
144
145 <console type='pty' tty='/dev/pts/2'>
146 <source path='/dev/pts/2'/>
147 <target port='0'/>
148 </console>
149
150 <serial type='pty'>
151 <source path='/dev/pts/2'/>
152 <target port='0'/>
153 </serial>
154
155 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
156 </devices>
157</domain>
158EOF
159
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700160
Jesse Andrewse49f7512011-11-05 22:34:45 -0700161rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700162cp -r $TOOLS_DIR/uec $vm_dir/uec
163
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700164# set metadata
165cat > $vm_dir/uec/meta-data<<EOF
166hostname: $GUEST_NAME
167instance-id: i-87018aed
168instance-type: m1.large
Jesse Andrews7306f3b2011-11-05 23:13:34 -0700169local-hostname: $GUEST_NAME.local
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700170EOF
171
Jesse Andrews63cb9232011-11-05 23:16:53 -0700172# set metadata
173cat > $vm_dir/uec/user-data<<EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700174#!/bin/bash
Jesse Andrewsb17c4f32011-11-06 09:25:55 -0800175# hostname needs to resolve for rabbit
Jesse Andrews6e3a4c52011-11-06 09:35:13 -0800176sed -i "s/127.0.0.1/127.0.0.1 \`hostname\`/" /etc/hosts
Jesse Andrews446a3302011-11-05 23:36:29 -0700177apt-get update
Jesse Andrewsc7f72ad2011-11-06 08:00:28 -0800178apt-get install git sudo -y
Jesse Andrews446a3302011-11-05 23:36:29 -0700179git clone https://github.com/cloudbuilders/devstack.git
180cd devstack
Jesse Andrews81881ec2011-11-06 00:22:41 -0700181git remote set-url origin `cd $TOP_DIR; git remote show origin | grep Fetch | awk '{print $3}'`
182git fetch
Jesse Andrews11416bf2011-11-06 00:32:21 -0700183git checkout `git rev-parse HEAD`
Jesse Andrews0c008952011-11-06 00:26:29 -0700184cat > localrc <<LOCAL_EOF
Jesse Andrewsc7f72ad2011-11-06 08:00:28 -0800185ROOTSLEEP=0
Jesse Andrews6b1c26e2011-11-06 00:13:30 -0700186`cat $TOP_DIR/localrc`
Jesse Andrews0c008952011-11-06 00:26:29 -0700187LOCAL_EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700188./stack.sh
Jesse Andrews63cb9232011-11-05 23:16:53 -0700189EOF
190
Jesse Andrewsee34f622011-11-05 22:41:57 -0700191# (re)start a metadata service
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700192(
193 pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1`
Jesse Andrews9645b0c2011-11-05 23:05:33 -0700194 [ -z "$pid" ] || kill -9 $pid
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700195)
Jesse Andrewsf504e282011-11-05 22:29:35 -0700196cd $vm_dir/uec
197python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700198
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700199# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700200virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700201
202# Tail the console log till we are done
203WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
204if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700205 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700206 # Done creating the container, let's tail the log
207 echo
208 echo "============================================================="
209 echo " -- YAY! --"
210 echo "============================================================="
211 echo
212 echo "We're done launching the vm, about to start tailing the"
213 echo "stack.sh log. It will take a second or two to start."
214 echo
215 echo "Just CTRL-C at any time to stop tailing."
216
Jesse Andrews228f2462011-11-05 17:36:14 -0700217 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700218 sleep 1
219 done
220
Jesse Andrews228f2462011-11-05 17:36:14 -0700221 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700222
223 TAIL_PID=$!
224
225 function kill_tail() {
226 kill $TAIL_PID
227 exit 1
228 }
229
230 # Let Ctrl-c kill tail and exit
231 trap kill_tail SIGINT
232
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700233 echo "Waiting stack.sh to finish..."
Jesse Andrewsd55a5152011-11-06 08:16:42 -0800234 while ! egrep -q '^stack.sh (completed|failed)' $vm_dir/console.log ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700235 sleep 1
236 done
237
238 set -o xtrace
239
240 kill $TAIL_PID
241
Jesse Andrews228f2462011-11-05 17:36:14 -0700242 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700243 exit 1
244 fi
245 echo ""
246 echo "Finished - Zip-a-dee Doo-dah!"
247fi