blob: 5d93da3a2ab17858f40d1dcda7343ece4f6cfc4a [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/>
96 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
97</network>
98EOF
99
100if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
101 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrews228f2462011-11-05 17:36:14 -0700102 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700103fi
104
105# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700106LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700107cat > $LIBVIRT_XML <<EOF
108<domain type='kvm'>
109 <name>$GUEST_NAME</name>
110 <memory>$GUEST_RAM</memory>
111 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700112 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700113 <kernel>$image_dir/kernel</kernel>
114 <cmdline>root=/dev/vda ro console=ttyS0 init=/usr/lib/cloud-init/uncloud-init ds=nocloud ubuntu-pass=ubuntu</cmdline>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700115 </os>
116 <features>
117 <acpi/>
118 </features>
119 <clock offset='utc'/>
120 <vcpu>$GUEST_CORES</vcpu>
121 <devices>
122 <disk type='file'>
123 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700124 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700125 <target dev='vda' bus='virtio'/>
126 </disk>
127
128 <interface type='network'>
129 <source network='devstack-$GUEST_NETWORK'/>
130 </interface>
131
132 <!-- The order is significant here. File must be defined first -->
133 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700134 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700135 <target port='1'/>
136 </serial>
137
138 <console type='pty' tty='/dev/pts/2'>
139 <source path='/dev/pts/2'/>
140 <target port='0'/>
141 </console>
142
143 <serial type='pty'>
144 <source path='/dev/pts/2'/>
145 <target port='0'/>
146 </serial>
147
148 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
149 </devices>
150</domain>
151EOF
152
153# Create the instance
Jesse Andrews228f2462011-11-05 17:36:14 -0700154cd $vm_dir && virsh create libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700155
156# Tail the console log till we are done
157WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
158if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700159 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700160 # Done creating the container, let's tail the log
161 echo
162 echo "============================================================="
163 echo " -- YAY! --"
164 echo "============================================================="
165 echo
166 echo "We're done launching the vm, about to start tailing the"
167 echo "stack.sh log. It will take a second or two to start."
168 echo
169 echo "Just CTRL-C at any time to stop tailing."
170
Jesse Andrews228f2462011-11-05 17:36:14 -0700171 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700172 sleep 1
173 done
174
Jesse Andrews228f2462011-11-05 17:36:14 -0700175 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700176
177 TAIL_PID=$!
178
179 function kill_tail() {
180 kill $TAIL_PID
181 exit 1
182 }
183
184 # Let Ctrl-c kill tail and exit
185 trap kill_tail SIGINT
186
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700187 echo "Waiting stack.sh to finish..."
Jesse Andrews228f2462011-11-05 17:36:14 -0700188 while ! cat $vm_dir/console.log | grep -q 'All done' ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700189 sleep 1
190 done
191
192 set -o xtrace
193
194 kill $TAIL_PID
195
Jesse Andrews228f2462011-11-05 17:36:14 -0700196 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700197 exit 1
198 fi
199 echo ""
200 echo "Finished - Zip-a-dee Doo-dah!"
201fi