blob: 24422af353a57a0600865542a74bba1895b556d9 [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
42tarball=$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
47 tar -Sxvzf $tarball $image_dir
48 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 Andrews228f2462011-11-05 17:36:14 -070074# qemu-img create -f qcow2 -b ${BASE_IMAGE} $vm_dir/disk
75cp $image_dir/disk $vm_dir/disk
76cp $image_dir/kernel $vm_dir/kernel
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/>
98 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
99</network>
100EOF
101
102if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
103 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrews228f2462011-11-05 17:36:14 -0700104 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700105fi
106
107# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700108LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700109cat > $LIBVIRT_XML <<EOF
110<domain type='kvm'>
111 <name>$GUEST_NAME</name>
112 <memory>$GUEST_RAM</memory>
113 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700114 <type>hvm</type>
115 <kernel>$vm_dir/kernel</kernel>
116 <cmdline>root=/dev/vda console=ttyS0 init=/usr/lib/cloud-init/uncloud-init ds=nocloud ubuntu-pass=ubuntu</cmdline>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700117 </os>
118 <features>
119 <acpi/>
120 </features>
121 <clock offset='utc'/>
122 <vcpu>$GUEST_CORES</vcpu>
123 <devices>
124 <disk type='file'>
125 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700126 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700127 <target dev='vda' bus='virtio'/>
128 </disk>
129
130 <interface type='network'>
131 <source network='devstack-$GUEST_NETWORK'/>
132 </interface>
133
134 <!-- The order is significant here. File must be defined first -->
135 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700136 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700137 <target port='1'/>
138 </serial>
139
140 <console type='pty' tty='/dev/pts/2'>
141 <source path='/dev/pts/2'/>
142 <target port='0'/>
143 </console>
144
145 <serial type='pty'>
146 <source path='/dev/pts/2'/>
147 <target port='0'/>
148 </serial>
149
150 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
151 </devices>
152</domain>
153EOF
154
155# Create the instance
Jesse Andrews228f2462011-11-05 17:36:14 -0700156cd $vm_dir && virsh create libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700157
158# Tail the console log till we are done
159WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
160if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700161 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700162 # Done creating the container, let's tail the log
163 echo
164 echo "============================================================="
165 echo " -- YAY! --"
166 echo "============================================================="
167 echo
168 echo "We're done launching the vm, about to start tailing the"
169 echo "stack.sh log. It will take a second or two to start."
170 echo
171 echo "Just CTRL-C at any time to stop tailing."
172
Jesse Andrews228f2462011-11-05 17:36:14 -0700173 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700174 sleep 1
175 done
176
Jesse Andrews228f2462011-11-05 17:36:14 -0700177 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700178
179 TAIL_PID=$!
180
181 function kill_tail() {
182 kill $TAIL_PID
183 exit 1
184 }
185
186 # Let Ctrl-c kill tail and exit
187 trap kill_tail SIGINT
188
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700189 echo "Waiting stack.sh to finish..."
Jesse Andrews228f2462011-11-05 17:36:14 -0700190 while ! cat $vm_dir/console.log | grep -q 'All done' ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700191 sleep 1
192 done
193
194 set -o xtrace
195
196 kill $TAIL_PID
197
Jesse Andrews228f2462011-11-05 17:36:14 -0700198 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700199 exit 1
200 fi
201 echo ""
202 echo "Finished - Zip-a-dee Doo-dah!"
203fi