blob: b4052f2209dfe0e9daaeaa498c5ab270d6f00c3e [file] [log] [blame]
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07001#!/usr/bin/env bash
2
Jesse Andrews228f2462011-11-05 17:36:14 -07003# Ubuntu distro to install
4DIST_NAME=${DIST_NAME:-oneiric}
5
6# Make sure that we have the proper version of ubuntu (only works on natty/oneiric)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07007UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
8if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
9 if [ ! "natty" = "$UBUNTU_VERSION" ]; then
10 echo "This script only works with oneiric and natty"
11 exit 1
12 fi
13fi
14
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070015# Keep track of the current directory
16TOOLS_DIR=$(cd $(dirname "$0") && pwd)
17TOP_DIR=`cd $TOOLS_DIR/..; pwd`
18
Jesse Andrews228f2462011-11-05 17:36:14 -070019# exit on error to stop unexpected errors
20set -o errexit
21set -o xtrace
22
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070023# Abort if localrc is not set
24if [ ! -e $TOP_DIR/localrc ]; then
25 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
26 echo "See stack.sh for required passwords."
27 exit 1
28fi
29
30# Install deps if needed
31dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
32
33# Where to store files and instances
34WORK_DIR=${WORK_DIR:-/opt/kvmstack}
35
36# Where to store images
Jesse Andrews228f2462011-11-05 17:36:14 -070037image_dir=$WORK_DIR/images/$DIST_NAME
38mkdir -p $image_dir
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070039
40# Original version of built image
Jesse Andrews228f2462011-11-05 17:36:14 -070041uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz
Jesse Andrews96dffbc2011-11-05 17:37:33 -070042tarball=$image_dir/$(basename $uec_url)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070043
44# download the base uec image if we haven't already
Jesse Andrews228f2462011-11-05 17:36:14 -070045if [ ! -f $tarball ]; then
46 curl $uec_url -o $tarball
Jesse Andrews3b768582011-11-05 17:40:20 -070047 (cd $image_dir && tar -Sxvzf $tarball)
Jesse Andrews228f2462011-11-05 17:36:14 -070048 cp $image_dir/*.img $image_dir/disk
49 cp $image_dir/*-vmlinuz-virtual $image_dir/kernel
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070050fi
51
52cd $TOP_DIR
53
54# Source params
55source ./stackrc
56
57# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
58ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
59
60# Name of our instance, used by libvirt
61GUEST_NAME=${GUEST_NAME:-devstack}
62
63# Mop up after previous runs
64virsh destroy $GUEST_NAME || true
65
66# Where this vm is stored
Jesse Andrews228f2462011-11-05 17:36:14 -070067vm_dir=$WORK_DIR/instances/$GUEST_NAME
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070068
69# Create vm dir and remove old disk
Jesse Andrews228f2462011-11-05 17:36:14 -070070mkdir -p $vm_dir
71rm -f $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070072
73# Create a copy of the base image
Jesse Andrewsf5a76912011-11-05 17:47:50 -070074qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070075
76# Back to devstack
77cd $TOP_DIR
78
79GUEST_NETWORK=${GUEST_NETWORK:-1}
80GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
81GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
82GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
83GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
84GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
85GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
86GUEST_RAM=${GUEST_RAM:-1524288}
87GUEST_CORES=${GUEST_CORES:-1}
88
89# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -070090NET_XML=$vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070091cat > $NET_XML <<EOF
92<network>
93 <name>devstack-$GUEST_NETWORK</name>
94 <bridge name="stackbr%d" />
95 <forward/>
Jesse Andrewsa6282622011-11-05 18:39:33 -070096 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK">
97 <dhcp>
Jesse Andrews63fa7ab2011-11-05 18:49:36 -070098 <range start='192.168.$GUEST_NETWORK.100' end='192.168.$GUEST_NETWORK.120' />
Jesse Andrewsa6282622011-11-05 18:39:33 -070099 </dhcp>
100 </ip>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700101</network>
102EOF
103
104if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
105 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrews228f2462011-11-05 17:36:14 -0700106 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700107fi
108
109# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700110LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700111cat > $LIBVIRT_XML <<EOF
112<domain type='kvm'>
113 <name>$GUEST_NAME</name>
114 <memory>$GUEST_RAM</memory>
115 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700116 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700117 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700118 <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 -0700119 </os>
120 <features>
121 <acpi/>
122 </features>
123 <clock offset='utc'/>
124 <vcpu>$GUEST_CORES</vcpu>
125 <devices>
126 <disk type='file'>
127 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700128 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700129 <target dev='vda' bus='virtio'/>
130 </disk>
131
132 <interface type='network'>
133 <source network='devstack-$GUEST_NETWORK'/>
134 </interface>
135
136 <!-- The order is significant here. File must be defined first -->
137 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700138 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700139 <target port='1'/>
140 </serial>
141
142 <console type='pty' tty='/dev/pts/2'>
143 <source path='/dev/pts/2'/>
144 <target port='0'/>
145 </console>
146
147 <serial type='pty'>
148 <source path='/dev/pts/2'/>
149 <target port='0'/>
150 </serial>
151
152 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
153 </devices>
154</domain>
155EOF
156
Jesse Andrewse49f7512011-11-05 22:34:45 -0700157rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700158cp -r $TOOLS_DIR/uec $vm_dir/uec
159
Jesse Andrewsee34f622011-11-05 22:41:57 -0700160# (re)start a metadata service
161`lsof -i -n | grep 192.168.$GUEST_NETWORK.1:4567 | awk '{print $2}' | xargs -n1 kill -9`
Jesse Andrewsf504e282011-11-05 22:29:35 -0700162cd $vm_dir/uec
163python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700164
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700165# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700166virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700167
168# Tail the console log till we are done
169WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
170if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700171 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700172 # Done creating the container, let's tail the log
173 echo
174 echo "============================================================="
175 echo " -- YAY! --"
176 echo "============================================================="
177 echo
178 echo "We're done launching the vm, about to start tailing the"
179 echo "stack.sh log. It will take a second or two to start."
180 echo
181 echo "Just CTRL-C at any time to stop tailing."
182
Jesse Andrews228f2462011-11-05 17:36:14 -0700183 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700184 sleep 1
185 done
186
Jesse Andrews228f2462011-11-05 17:36:14 -0700187 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700188
189 TAIL_PID=$!
190
191 function kill_tail() {
192 kill $TAIL_PID
193 exit 1
194 }
195
196 # Let Ctrl-c kill tail and exit
197 trap kill_tail SIGINT
198
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700199 echo "Waiting stack.sh to finish..."
Jesse Andrews228f2462011-11-05 17:36:14 -0700200 while ! cat $vm_dir/console.log | grep -q 'All done' ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700201 sleep 1
202 done
203
204 set -o xtrace
205
206 kill $TAIL_PID
207
Jesse Andrews228f2462011-11-05 17:36:14 -0700208 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700209 exit 1
210 fi
211 echo ""
212 echo "Finished - Zip-a-dee Doo-dah!"
213fi