blob: a0c2788200f05f60f80b81b7f85c181f70cc9275 [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 Andrews63fa7ab2011-11-05 18:49:36 -070099 <range start='192.168.$GUEST_NETWORK.100' end='192.168.$GUEST_NETWORK.120' />
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 Andrews228f2462011-11-05 17:36:14 -0700107 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700108fi
109
110# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700111LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700112cat > $LIBVIRT_XML <<EOF
113<domain type='kvm'>
114 <name>$GUEST_NAME</name>
115 <memory>$GUEST_RAM</memory>
116 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700117 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700118 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700119 <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 -0700120 </os>
121 <features>
122 <acpi/>
123 </features>
124 <clock offset='utc'/>
125 <vcpu>$GUEST_CORES</vcpu>
126 <devices>
127 <disk type='file'>
128 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700129 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700130 <target dev='vda' bus='virtio'/>
131 </disk>
132
133 <interface type='network'>
134 <source network='devstack-$GUEST_NETWORK'/>
135 </interface>
136
137 <!-- The order is significant here. File must be defined first -->
138 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700139 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700140 <target port='1'/>
141 </serial>
142
143 <console type='pty' tty='/dev/pts/2'>
144 <source path='/dev/pts/2'/>
145 <target port='0'/>
146 </console>
147
148 <serial type='pty'>
149 <source path='/dev/pts/2'/>
150 <target port='0'/>
151 </serial>
152
153 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
154 </devices>
155</domain>
156EOF
157
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700158
Jesse Andrewse49f7512011-11-05 22:34:45 -0700159rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700160cp -r $TOOLS_DIR/uec $vm_dir/uec
161
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700162# set metadata
163cat > $vm_dir/uec/meta-data<<EOF
164hostname: $GUEST_NAME
165instance-id: i-87018aed
166instance-type: m1.large
Jesse Andrews7306f3b2011-11-05 23:13:34 -0700167local-hostname: $GUEST_NAME.local
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700168EOF
169
Jesse Andrews63cb9232011-11-05 23:16:53 -0700170# set metadata
171cat > $vm_dir/uec/user-data<<EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700172#!/bin/bash
173apt-get update
174apt-get install git -y
175git clone https://github.com/cloudbuilders/devstack.git
176cd devstack
Jesse Andrews81881ec2011-11-06 00:22:41 -0700177git remote set-url origin `cd $TOP_DIR; git remote show origin | grep Fetch | awk '{print $3}'`
178git fetch
Jesse Andrews11416bf2011-11-06 00:32:21 -0700179git checkout `git rev-parse HEAD`
Jesse Andrews0c008952011-11-06 00:26:29 -0700180cat > localrc <<LOCAL_EOF
Jesse Andrews6b1c26e2011-11-06 00:13:30 -0700181`cat $TOP_DIR/localrc`
Jesse Andrews0c008952011-11-06 00:26:29 -0700182LOCAL_EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700183./stack.sh
Jesse Andrews63cb9232011-11-05 23:16:53 -0700184EOF
185
Jesse Andrewsee34f622011-11-05 22:41:57 -0700186# (re)start a metadata service
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700187(
188 pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1`
Jesse Andrews9645b0c2011-11-05 23:05:33 -0700189 [ -z "$pid" ] || kill -9 $pid
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700190)
Jesse Andrewsf504e282011-11-05 22:29:35 -0700191cd $vm_dir/uec
192python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700193
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700194# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700195virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700196
197# Tail the console log till we are done
198WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
199if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700200 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700201 # Done creating the container, let's tail the log
202 echo
203 echo "============================================================="
204 echo " -- YAY! --"
205 echo "============================================================="
206 echo
207 echo "We're done launching the vm, about to start tailing the"
208 echo "stack.sh log. It will take a second or two to start."
209 echo
210 echo "Just CTRL-C at any time to stop tailing."
211
Jesse Andrews228f2462011-11-05 17:36:14 -0700212 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700213 sleep 1
214 done
215
Jesse Andrews228f2462011-11-05 17:36:14 -0700216 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700217
218 TAIL_PID=$!
219
220 function kill_tail() {
221 kill $TAIL_PID
222 exit 1
223 }
224
225 # Let Ctrl-c kill tail and exit
226 trap kill_tail SIGINT
227
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700228 echo "Waiting stack.sh to finish..."
Jesse Andrews228f2462011-11-05 17:36:14 -0700229 while ! cat $vm_dir/console.log | grep -q 'All done' ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700230 sleep 1
231 done
232
233 set -o xtrace
234
235 kill $TAIL_PID
236
Jesse Andrews228f2462011-11-05 17:36:14 -0700237 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700238 exit 1
239 fi
240 echo ""
241 echo "Finished - Zip-a-dee Doo-dah!"
242fi