blob: fc281d3199a18a5fdd77c88be55e9fb0e9a24760 [file] [log] [blame]
Jesse Andrewse97a2e72011-10-30 18:37:49 -07001#!/usr/bin/env bash
2
3# exit on error to stop unexpected errors
4set -o errexit
5
6# Make sure that we have the proper version of ubuntu
7UBUNTU_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
Dean Troyer55c02732011-11-01 17:44:03 -050015# Clean up any resources that may be in use
16cleanup() {
17 set +o errexit
18 unmount_images
19
20 if [ -n "$ROOTFS" ]; then
21 umount $ROOTFS/dev
22 umount $ROOTFS
23 fi
24
25 # Release NBD devices
26 if [ -n "$NBD" ]; then
27 qemu-nbd -d $NBD
28 fi
29
30 # Kill ourselves to signal any calling process
31 trap 2; kill -2 $$
32}
33
34trap cleanup SIGHUP SIGINT SIGTERM
35
Jesse Andrewse97a2e72011-10-30 18:37:49 -070036# Echo commands
37set -o xtrace
38
39# Keep track of the current directory
40TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Jesse Andrews31989742011-10-30 18:56:05 -070041TOP_DIR=`cd $TOOLS_DIR/..; pwd`
Jesse Andrewse97a2e72011-10-30 18:37:49 -070042
43# Where to store files and instances
Jesse Andrewsb0559b22011-10-30 19:46:54 -070044WORK_DIR=${WORK_DIR:-/opt/kvmstack}
Jesse Andrewse97a2e72011-10-30 18:37:49 -070045
46# Where to store images
47IMAGES_DIR=$WORK_DIR/images
48
49# Create images dir
50mkdir -p $IMAGES_DIR
51
52# Abort if localrc is not set
53if [ ! -e $TOP_DIR/localrc ]; then
54 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
55 echo "See stack.sh for required passwords."
56 exit 1
57fi
58
Jesse Andrews7fa56132011-10-30 18:43:54 -070059cd $TOP_DIR
60
Jesse Andrewse97a2e72011-10-30 18:37:49 -070061# Source params
62source ./stackrc
63
64# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
65ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
66
Jesse Andrewse97a2e72011-10-30 18:37:49 -070067# Base image (natty by default)
68DIST_NAME=${DIST_NAME:-natty}
69IMAGE_FNAME=$DIST_NAME.raw
70
71# Name of our instance, used by libvirt
72GUEST_NAME=${GUEST_NAME:-devstack}
73
74# Original version of built image
Jesse Andrews2b7d2212011-10-30 19:37:56 -070075BASE_IMAGE=$IMAGES_DIR/$DIST_NAME.raw
Jesse Andrewse97a2e72011-10-30 18:37:49 -070076
77# Copy of base image, which we pre-install with tasty treats
78VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
79
80# Mop up after previous runs
81virsh destroy $GUEST_NAME || true
82
83# Where this vm is stored
84VM_DIR=$WORK_DIR/instances/$GUEST_NAME
85
86# Create vm dir
87mkdir -p $VM_DIR
88
89# Mount point into copied base image
90COPY_DIR=$VM_DIR/copy
91mkdir -p $COPY_DIR
92
Jesse Andrews2b7d2212011-10-30 19:37:56 -070093# Get the base image if it does not yet exist
94if [ ! -e $BASE_IMAGE ]; then
95 $TOOLS_DIR/get_uec_image.sh -f raw -r 5000 $DIST_NAME $BASE_IMAGE
Jesse Andrewse97a2e72011-10-30 18:37:49 -070096fi
97
98# Create a copy of the base image
99if [ ! -e $VM_IMAGE ]; then
100 cp -p $BASE_IMAGE $VM_IMAGE
101fi
102
103# Unmount the copied base image
104function unmount_images() {
105 # unmount the filesystem
106 while df | grep -q $COPY_DIR; do
107 umount $COPY_DIR || echo 'ok'
108 sleep 1
109 done
110}
111
112# Unmount from failed runs
113unmount_images
114
115# Ctrl-c catcher
116function kill_unmount() {
117 unmount_images
118 exit 1
119}
120
121# Install deps if needed
122dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
123
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700124# Where Openstack code will live in image
125DEST=${DEST:-/opt/stack}
126
127# Mount the file system
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700128# For some reason, UEC-based images want 255 heads * 63 sectors * 512 byte sectors = 8225280
129mount -t ext4 -o loop,offset=8225280 $VM_IMAGE $COPY_DIR
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700130
131# git clone only if directory doesn't exist already. Since ``DEST`` might not
132# be owned by the installation user, we create the directory and change the
133# ownership to the proper user.
134function git_clone {
135 if [ ! -d $2 ]; then
136 sudo mkdir $2
137 sudo chown `whoami` $2
138 git clone $1 $2
139 cd $2
140 # This checkout syntax works for both branches and tags
141 git checkout $3
142 fi
143}
144
145# Make sure that base requirements are installed
146cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
147chroot $COPY_DIR apt-get update
148chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
149chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
150chroot $COPY_DIR pip install `cat files/pips/*`
151
152# Clean out code repos if directed to do so
153if [ "$CLEAN" = "1" ]; then
154 rm -rf $COPY_DIR/$DEST
155fi
156
157# Cache openstack code
158mkdir -p $COPY_DIR/$DEST
159git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
160git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
161git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
162git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
163git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
164git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
165git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
166git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
167git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
168
169# Back to devstack
170cd $TOP_DIR
171
172# Unmount the filesystems
173unmount_images
174
175# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700176GUEST_NETWORK=${GUEST_NETWORK:-1}
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700177GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700178
179GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
180GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
181GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
182GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
183GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
184GUEST_RAM=${GUEST_RAM:-1524288}
185GUEST_CORES=${GUEST_CORES:-1}
186
187# libvirt.xml configuration
188NET_XML=$VM_DIR/net.xml
189cat > $NET_XML <<EOF
190<network>
191 <name>devstack-$GUEST_NETWORK</name>
192 <bridge name="stackbr%d" />
193 <forward/>
194 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
195</network>
196EOF
197
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700198if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
199 virsh net-destroy devstack-$GUEST_NETWORK || true
200 virsh net-create $VM_DIR/net.xml
201fi
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700202
203# libvirt.xml configuration
204LIBVIRT_XML=$VM_DIR/libvirt.xml
205cat > $LIBVIRT_XML <<EOF
206<domain type='kvm'>
207 <name>$GUEST_NAME</name>
208 <memory>$GUEST_RAM</memory>
209 <os>
210 <type>hvm</type>
211 <bootmenu enable='yes'/>
212 </os>
213 <features>
214 <acpi/>
215 </features>
216 <vcpu>$GUEST_CORES</vcpu>
217 <devices>
218 <disk type='file'>
219 <driver type='qcow2'/>
220 <source file='$VM_DIR/disk'/>
221 <target dev='vda' bus='virtio'/>
222 </disk>
223
224 <interface type='network'>
225 <source network='devstack-$GUEST_NETWORK'/>
226 </interface>
227
228 <!-- The order is significant here. File must be defined first -->
229 <serial type="file">
230 <source path='$VM_DIR/console.log'/>
231 <target port='1'/>
232 </serial>
233
234 <console type='pty' tty='/dev/pts/2'>
235 <source path='/dev/pts/2'/>
236 <target port='0'/>
237 </console>
238
239 <serial type='pty'>
240 <source path='/dev/pts/2'/>
241 <target port='0'/>
242 </serial>
243
244 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
245 </devices>
246</domain>
247EOF
248
249# Mount point for instance fs
250ROOTFS=$VM_DIR/root
251mkdir -p $ROOTFS
252
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700253# Clean up from previous runs
254umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700255
256# Clean up old runs
257cd $VM_DIR
258rm -f $VM_DIR/disk
259
260# Create our instance fs
261qemu-img create -f qcow2 -b $VM_IMAGE disk
262
Dean Troyerdccd6b92011-11-01 15:46:14 -0500263# Finds the next available NBD device
264# Exits script if error connecting or none free
265# map_nbd image
266# returns full nbd device path
267function map_nbd {
268 for i in `seq 0 15`; do
269 if [ ! -e /sys/block/nbd$i/pid ]; then
270 NBD=/dev/nbd$i
271 # Connect to nbd and wait till it is ready
272 qemu-nbd -c $NBD $1
273 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
274 echo "Couldn't connect $NBD"
275 exit 1
276 fi
277 break
278 fi
279 done
280 if [ -z "$NBD" ]; then
281 echo "No free NBD slots"
282 exit 1
283 fi
284 echo $NBD
285}
286
Dean Troyer7a569732011-10-31 17:34:29 -0500287# Make sure we have nbd-ness
288modprobe nbd max_part=63
289
290# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500291NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500292NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700293
294# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700295mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700296
297# Configure instance network
298INTERFACES=$ROOTFS/etc/network/interfaces
299cat > $INTERFACES <<EOF
300auto lo
301iface lo inet loopback
302
303auto eth0
304iface eth0 inet static
305 address $GUEST_IP
306 netmask $GUEST_NETMASK
307 gateway $GUEST_GATEWAY
308EOF
309
310# User configuration for the instance
311chroot $ROOTFS groupadd libvirtd || true
312chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700313cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700314echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
315echo "stack:pass" | chroot $ROOTFS chpasswd
316echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
317
318# Gracefully cp only if source file/dir exists
319function cp_it {
320 if [ -e $1 ] || [ -d $1 ]; then
321 cp -pRL $1 $2
322 fi
323}
324
325# Copy over your ssh keys and env if desired
326COPYENV=${COPYENV:-1}
327if [ "$COPYENV" = "1" ]; then
328 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
329 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
330 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
331 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
332 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
333fi
334
335# pre-cache uec images
336for image_url in ${IMAGE_URLS//,/ }; do
337 IMAGE_FNAME=`basename "$image_url"`
338 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
339 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
340 fi
341 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
342done
343
344# Configure the runner
345RUN_SH=$ROOTFS/$DEST/run.sh
346cat > $RUN_SH <<EOF
347#!/usr/bin/env bash
348
349# Kill any existing screens
350killall screen
351
352# Install and run stack.sh
353sudo apt-get update
354sudo apt-get -y --force-yes install git-core vim-nox sudo
355if [ ! -d "$DEST/devstack" ]; then
356 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
357fi
358cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
359echo >> /$DEST/run.sh.log
360echo >> /$DEST/run.sh.log
361echo "All done! Time to start clicking." >> /$DEST/run.sh.log
362cat $DEST/run.sh.log
363EOF
364chmod 755 $RUN_SH
365
366# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700367RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700368cat > $RC_LOCAL <<EOF
369#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700370# cloud-init overwrites the hostname with ubuntuhost
371echo $GUEST_NAME > /etc/hostname
372hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700373su -c "$DEST/run.sh" stack
374EOF
375chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700376chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700377
378# Make our ip address hostnames look nice at the command prompt
379echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
380echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
381
382# Give stack ownership over $DEST so it may do the work needed
383chroot $ROOTFS chown -R stack $DEST
384
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700385# Set the hostname
386echo $GUEST_NAME > $ROOTFS/etc/hostname
387
388# We need the hostname to resolve for rabbit to launch
389if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
390 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
391fi
392
Dean Troyer6fe687b2011-10-31 20:30:04 -0500393# GRUB 2 wants to see /dev
394mount -o bind /dev $ROOTFS/dev
395
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700396# Change boot params so that we get a console log
397G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
398sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700399sed -e "s,GRUB_CMDLINE_LINUX_DEFAULT=.*$,GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS0 console=tty0 ds=nocloud ubuntu-pass=pass\",g" -i $ROOTFS/etc/default/grub
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700400sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
401echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
402echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
403echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
404
405chroot $ROOTFS update-grub
406umount $ROOTFS/dev
407
408# Pre-generate ssh host keys and allow password login
409chroot $ROOTFS dpkg-reconfigure openssh-server
410sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
411
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700412# Unmount
413umount $ROOTFS || echo 'ok'
Dean Troyer55c02732011-11-01 17:44:03 -0500414ROOTFS=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700415qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500416NBD=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700417
418# Create the instance
419cd $VM_DIR && virsh create libvirt.xml
420
421# Tail the console log till we are done
422WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
423if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
424 # Done creating the container, let's tail the log
425 echo
426 echo "============================================================="
427 echo " -- YAY! --"
428 echo "============================================================="
429 echo
430 echo "We're done launching the vm, about to start tailing the"
431 echo "stack.sh log. It will take a second or two to start."
432 echo
433 echo "Just CTRL-C at any time to stop tailing."
434
435 while [ ! -e "$VM_DIR/console.log" ]; do
436 sleep 1
437 done
438
Jesse Andrews16341962011-10-31 00:21:56 -0700439 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700440
441 TAIL_PID=$!
442
443 function kill_tail() {
444 kill $TAIL_PID
445 exit 1
446 }
447
448 # Let Ctrl-c kill tail and exit
449 trap kill_tail SIGINT
450
Jesse Andrews16341962011-10-31 00:21:56 -0700451 set +o xtrace
452
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700453 echo "Waiting stack.sh to finish..."
454 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700455 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700456 done
457
Jesse Andrews16341962011-10-31 00:21:56 -0700458 set -o xtrace
459
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700460 kill $TAIL_PID
461
Jesse Andrewsa06ac1c2011-10-31 22:29:23 -0700462 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700463 exit 1
464 fi
465 echo ""
466 echo "Finished - Zip-a-dee Doo-dah!"
467fi