blob: 6bab526ce97f0c0dbdc2ad03e571fc06261bfdd9 [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 Andrewse3c47a32011-11-07 10:44:43 -08004if ! egrep -q "oneiric|natty" /etc/lsb-release; then
5 echo "This script only works with ubuntu oneiric and natty"
6 exit 1
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07007fi
8
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07009# Keep track of the current directory
10TOOLS_DIR=$(cd $(dirname "$0") && pwd)
11TOP_DIR=`cd $TOOLS_DIR/..; pwd`
12
Jesse Andrews53d75332011-11-06 07:54:11 -080013cd $TOP_DIR
14
15# Source params
16source ./stackrc
17
18# Ubuntu distro to install
19DIST_NAME=${DIST_NAME:-oneiric}
20
Jesse Andrewse3c47a32011-11-07 10:44:43 -080021# Configure how large the VM should be
22GUEST_SIZE=${GUEST_SIZE:-10G}
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
Jesse Andrewse3c47a32011-11-07 10:44:43 -080036DEPS="kvm libvirt-bin kpartx"
37dpkg -l $DEPS || apt-get install -y --force-yes $DEPS
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070038
39# Where to store files and instances
40WORK_DIR=${WORK_DIR:-/opt/kvmstack}
41
42# Where to store images
Jesse Andrews228f2462011-11-05 17:36:14 -070043image_dir=$WORK_DIR/images/$DIST_NAME
44mkdir -p $image_dir
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070045
46# Original version of built image
Jesse Andrews228f2462011-11-05 17:36:14 -070047uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz
Jesse Andrews96dffbc2011-11-05 17:37:33 -070048tarball=$image_dir/$(basename $uec_url)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070049
50# download the base uec image if we haven't already
Jesse Andrews228f2462011-11-05 17:36:14 -070051if [ ! -f $tarball ]; then
52 curl $uec_url -o $tarball
Jesse Andrews3b768582011-11-05 17:40:20 -070053 (cd $image_dir && tar -Sxvzf $tarball)
Jesse Andrewse3c47a32011-11-07 10:44:43 -080054 resize-part-image $image_dir/*.img $GUEST_SIZE $image_dir/disk
Jesse Andrews228f2462011-11-05 17:36:14 -070055 cp $image_dir/*-vmlinuz-virtual $image_dir/kernel
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070056fi
57
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070058
59# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
60ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
61
62# Name of our instance, used by libvirt
63GUEST_NAME=${GUEST_NAME:-devstack}
64
65# Mop up after previous runs
66virsh destroy $GUEST_NAME || true
67
68# Where this vm is stored
Jesse Andrews228f2462011-11-05 17:36:14 -070069vm_dir=$WORK_DIR/instances/$GUEST_NAME
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070070
71# Create vm dir and remove old disk
Jesse Andrews228f2462011-11-05 17:36:14 -070072mkdir -p $vm_dir
73rm -f $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070074
75# Create a copy of the base image
Jesse Andrewsf5a76912011-11-05 17:47:50 -070076qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070077
78# Back to devstack
79cd $TOP_DIR
80
81GUEST_NETWORK=${GUEST_NETWORK:-1}
82GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
83GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
84GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
85GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
86GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
87GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
88GUEST_RAM=${GUEST_RAM:-1524288}
89GUEST_CORES=${GUEST_CORES:-1}
90
91# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -070092NET_XML=$vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070093cat > $NET_XML <<EOF
94<network>
95 <name>devstack-$GUEST_NETWORK</name>
96 <bridge name="stackbr%d" />
97 <forward/>
Jesse Andrewsa6282622011-11-05 18:39:33 -070098 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK">
99 <dhcp>
Jesse Andrews02cc96c2011-11-06 10:29:10 -0800100 <range start='192.168.$GUEST_NETWORK.2' end='192.168.$GUEST_NETWORK.127' />
Jesse Andrewsa6282622011-11-05 18:39:33 -0700101 </dhcp>
102 </ip>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700103</network>
104EOF
105
106if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
107 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrewsdca89002011-11-06 10:33:33 -0800108 # destroying the network isn't enough to delete the leases
109 rm -f /var/lib/libvirt/dnsmasq/devstack-$GUEST_NETWORK.leases
Jesse Andrews228f2462011-11-05 17:36:14 -0700110 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700111fi
112
113# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700114LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700115cat > $LIBVIRT_XML <<EOF
116<domain type='kvm'>
117 <name>$GUEST_NAME</name>
118 <memory>$GUEST_RAM</memory>
119 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700120 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700121 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700122 <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 -0700123 </os>
124 <features>
125 <acpi/>
126 </features>
127 <clock offset='utc'/>
128 <vcpu>$GUEST_CORES</vcpu>
129 <devices>
130 <disk type='file'>
131 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700132 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700133 <target dev='vda' bus='virtio'/>
134 </disk>
135
136 <interface type='network'>
137 <source network='devstack-$GUEST_NETWORK'/>
138 </interface>
139
140 <!-- The order is significant here. File must be defined first -->
141 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700142 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700143 <target port='1'/>
144 </serial>
145
146 <console type='pty' tty='/dev/pts/2'>
147 <source path='/dev/pts/2'/>
148 <target port='0'/>
149 </console>
150
151 <serial type='pty'>
152 <source path='/dev/pts/2'/>
153 <target port='0'/>
154 </serial>
155
156 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
157 </devices>
158</domain>
159EOF
160
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700161
Jesse Andrewse49f7512011-11-05 22:34:45 -0700162rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700163cp -r $TOOLS_DIR/uec $vm_dir/uec
164
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700165# set metadata
166cat > $vm_dir/uec/meta-data<<EOF
167hostname: $GUEST_NAME
Jesse Andrewse3c47a32011-11-07 10:44:43 -0800168instance-id: i-hop
169instance-type: m1.ignore
Jesse Andrews7306f3b2011-11-05 23:13:34 -0700170local-hostname: $GUEST_NAME.local
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700171EOF
172
Jesse Andrews63cb9232011-11-05 23:16:53 -0700173# set metadata
174cat > $vm_dir/uec/user-data<<EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700175#!/bin/bash
Jesse Andrewsb17c4f32011-11-06 09:25:55 -0800176# hostname needs to resolve for rabbit
Jesse Andrews6e3a4c52011-11-06 09:35:13 -0800177sed -i "s/127.0.0.1/127.0.0.1 \`hostname\`/" /etc/hosts
Jesse Andrews446a3302011-11-05 23:36:29 -0700178apt-get update
Jesse Andrewsc7f72ad2011-11-06 08:00:28 -0800179apt-get install git sudo -y
Jesse Andrews446a3302011-11-05 23:36:29 -0700180git clone https://github.com/cloudbuilders/devstack.git
181cd devstack
Jesse Andrews81881ec2011-11-06 00:22:41 -0700182git remote set-url origin `cd $TOP_DIR; git remote show origin | grep Fetch | awk '{print $3}'`
183git fetch
Jesse Andrews11416bf2011-11-06 00:32:21 -0700184git checkout `git rev-parse HEAD`
Jesse Andrews0c008952011-11-06 00:26:29 -0700185cat > localrc <<LOCAL_EOF
Jesse Andrewsc7f72ad2011-11-06 08:00:28 -0800186ROOTSLEEP=0
Jesse Andrews6b1c26e2011-11-06 00:13:30 -0700187`cat $TOP_DIR/localrc`
Jesse Andrews0c008952011-11-06 00:26:29 -0700188LOCAL_EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700189./stack.sh
Jesse Andrews63cb9232011-11-05 23:16:53 -0700190EOF
191
Jesse Andrewsee34f622011-11-05 22:41:57 -0700192# (re)start a metadata service
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700193(
194 pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1`
Jesse Andrews9645b0c2011-11-05 23:05:33 -0700195 [ -z "$pid" ] || kill -9 $pid
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700196)
Jesse Andrewsf504e282011-11-05 22:29:35 -0700197cd $vm_dir/uec
198python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700199
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700200# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700201virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700202
203# Tail the console log till we are done
204WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
205if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700206 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700207 # Done creating the container, let's tail the log
208 echo
209 echo "============================================================="
210 echo " -- YAY! --"
211 echo "============================================================="
212 echo
213 echo "We're done launching the vm, about to start tailing the"
214 echo "stack.sh log. It will take a second or two to start."
215 echo
216 echo "Just CTRL-C at any time to stop tailing."
217
Jesse Andrews228f2462011-11-05 17:36:14 -0700218 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700219 sleep 1
220 done
221
Jesse Andrews228f2462011-11-05 17:36:14 -0700222 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700223
224 TAIL_PID=$!
225
226 function kill_tail() {
227 kill $TAIL_PID
228 exit 1
229 }
230
231 # Let Ctrl-c kill tail and exit
232 trap kill_tail SIGINT
233
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700234 echo "Waiting stack.sh to finish..."
Jesse Andrewsd55a5152011-11-06 08:16:42 -0800235 while ! egrep -q '^stack.sh (completed|failed)' $vm_dir/console.log ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700236 sleep 1
237 done
238
239 set -o xtrace
240
241 kill $TAIL_PID
242
Jesse Andrews228f2462011-11-05 17:36:14 -0700243 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700244 exit 1
245 fi
246 echo ""
247 echo "Finished - Zip-a-dee Doo-dah!"
248fi