blob: aae4fb8edf547e23d35262b6adadede4a50439ab [file] [log] [blame]
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07001#!/usr/bin/env bash
2
3# Make sure that we have the proper version of ubuntu
4UBUNTU_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
12# exit on error to stop unexpected errors
13set -o errexit
14
15# Keep track of the current directory
16TOOLS_DIR=$(cd $(dirname "$0") && pwd)
17TOP_DIR=`cd $TOOLS_DIR/..; pwd`
18
19# Abort if localrc is not set
20if [ ! -e $TOP_DIR/localrc ]; then
21 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
22 echo "See stack.sh for required passwords."
23 exit 1
24fi
25
26# Install deps if needed
27dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
28
29# Where to store files and instances
30WORK_DIR=${WORK_DIR:-/opt/kvmstack}
31
32# Where to store images
33IMAGES_DIR=$WORK_DIR/images
34
35# Original version of built image
36DIST_NAME=${DIST_NAME:oneiric}
37UEC_NAME=$DIST_NAME-server-cloudimg-amd64
38UEC_URL=http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME-disk1.img
39BASE_IMAGE=$IMAGES_DIR/$DIST_NAME.raw
40
41# download the base uec image if we haven't already
42if [ ! -e $BASE_IMAGE ]; then
43 mkdir -p $IMAGES_DIR
44 curl $UEC_URL -O $BASE_IMAGE
45fi
46
47cd $TOP_DIR
48
49# Source params
50source ./stackrc
51
52# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
53ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
54
55# Name of our instance, used by libvirt
56GUEST_NAME=${GUEST_NAME:-devstack}
57
58# Mop up after previous runs
59virsh destroy $GUEST_NAME || true
60
61# Where this vm is stored
62VM_DIR=$WORK_DIR/instances/$GUEST_NAME
63
64# Create vm dir and remove old disk
65mkdir -p $VM_DIR
66rm -f $VM_DIR/disk.img
67
68# Create a copy of the base image
69qemu-img create -f qcow2 -b ${BASE_IMAGE} $VM_DIR/disk.img
70
71# Back to devstack
72cd $TOP_DIR
73
74GUEST_NETWORK=${GUEST_NETWORK:-1}
75GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
76GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
77GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
78GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
79GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
80GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
81GUEST_RAM=${GUEST_RAM:-1524288}
82GUEST_CORES=${GUEST_CORES:-1}
83
84# libvirt.xml configuration
85NET_XML=$VM_DIR/net.xml
86cat > $NET_XML <<EOF
87<network>
88 <name>devstack-$GUEST_NETWORK</name>
89 <bridge name="stackbr%d" />
90 <forward/>
91 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
92</network>
93EOF
94
95if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
96 virsh net-destroy devstack-$GUEST_NETWORK || true
97 virsh net-create $VM_DIR/net.xml
98fi
99
100# libvirt.xml configuration
101LIBVIRT_XML=$VM_DIR/libvirt.xml
102cat > $LIBVIRT_XML <<EOF
103<domain type='kvm'>
104 <name>$GUEST_NAME</name>
105 <memory>$GUEST_RAM</memory>
106 <os>
107 <type arch='i686' machine='pc'>hvm</type>
108 <boot dev='hd'/>
109 <kernel>$VM_DIR/kernel</kernel>
110 <cmdline>root=/dev/vda ro init=/usr/lib/cloud-init/uncloud-init ds=nocloud ubuntu-pass=ubuntu</cmdline>
111 </os>
112 <features>
113 <acpi/>
114 </features>
115 <clock offset='utc'/>
116 <vcpu>$GUEST_CORES</vcpu>
117 <devices>
118 <disk type='file'>
119 <driver type='qcow2'/>
120 <source file='$VM_DIR/disk.img'/>
121 <target dev='vda' bus='virtio'/>
122 </disk>
123
124 <interface type='network'>
125 <source network='devstack-$GUEST_NETWORK'/>
126 </interface>
127
128 <!-- The order is significant here. File must be defined first -->
129 <serial type="file">
130 <source path='$VM_DIR/console.log'/>
131 <target port='1'/>
132 </serial>
133
134 <console type='pty' tty='/dev/pts/2'>
135 <source path='/dev/pts/2'/>
136 <target port='0'/>
137 </console>
138
139 <serial type='pty'>
140 <source path='/dev/pts/2'/>
141 <target port='0'/>
142 </serial>
143
144 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
145 </devices>
146</domain>
147EOF
148
149# Create the instance
150cd $VM_DIR && virsh create libvirt.xml
151
152# Tail the console log till we are done
153WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
154if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
155 # Done creating the container, let's tail the log
156 echo
157 echo "============================================================="
158 echo " -- YAY! --"
159 echo "============================================================="
160 echo
161 echo "We're done launching the vm, about to start tailing the"
162 echo "stack.sh log. It will take a second or two to start."
163 echo
164 echo "Just CTRL-C at any time to stop tailing."
165
166 while [ ! -e "$VM_DIR/console.log" ]; do
167 sleep 1
168 done
169
170 tail -F $VM_DIR/console.log &
171
172 TAIL_PID=$!
173
174 function kill_tail() {
175 kill $TAIL_PID
176 exit 1
177 }
178
179 # Let Ctrl-c kill tail and exit
180 trap kill_tail SIGINT
181
182 set +o xtrace
183
184 echo "Waiting stack.sh to finish..."
185 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
186 sleep 1
187 done
188
189 set -o xtrace
190
191 kill $TAIL_PID
192
193 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
194 exit 1
195 fi
196 echo ""
197 echo "Finished - Zip-a-dee Doo-dah!"
198fi