blob: 7efabbbad49599db9ecb2fde2e649b1bb3668a5d [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
Jesse Andrews14681332011-11-03 15:52:37 -0500148chroot $COPY_DIR apt-get install -y --download-only `cat files/apts/* | cut -d\# -f1`
149chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/general`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700150
151# Clean out code repos if directed to do so
152if [ "$CLEAN" = "1" ]; then
153 rm -rf $COPY_DIR/$DEST
154fi
155
156# Cache openstack code
157mkdir -p $COPY_DIR/$DEST
158git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
159git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
160git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
161git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
162git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
163git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
164git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
165git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
166git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
167
168# Back to devstack
169cd $TOP_DIR
170
171# Unmount the filesystems
172unmount_images
173
174# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700175GUEST_NETWORK=${GUEST_NETWORK:-1}
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700176GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700177
178GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
179GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
180GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
181GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
182GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
183GUEST_RAM=${GUEST_RAM:-1524288}
184GUEST_CORES=${GUEST_CORES:-1}
185
186# libvirt.xml configuration
187NET_XML=$VM_DIR/net.xml
188cat > $NET_XML <<EOF
189<network>
190 <name>devstack-$GUEST_NETWORK</name>
191 <bridge name="stackbr%d" />
192 <forward/>
193 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
194</network>
195EOF
196
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700197if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
198 virsh net-destroy devstack-$GUEST_NETWORK || true
199 virsh net-create $VM_DIR/net.xml
200fi
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700201
202# libvirt.xml configuration
203LIBVIRT_XML=$VM_DIR/libvirt.xml
204cat > $LIBVIRT_XML <<EOF
205<domain type='kvm'>
206 <name>$GUEST_NAME</name>
207 <memory>$GUEST_RAM</memory>
208 <os>
209 <type>hvm</type>
210 <bootmenu enable='yes'/>
211 </os>
212 <features>
213 <acpi/>
214 </features>
215 <vcpu>$GUEST_CORES</vcpu>
216 <devices>
217 <disk type='file'>
218 <driver type='qcow2'/>
219 <source file='$VM_DIR/disk'/>
220 <target dev='vda' bus='virtio'/>
221 </disk>
222
223 <interface type='network'>
224 <source network='devstack-$GUEST_NETWORK'/>
225 </interface>
226
227 <!-- The order is significant here. File must be defined first -->
228 <serial type="file">
229 <source path='$VM_DIR/console.log'/>
230 <target port='1'/>
231 </serial>
232
233 <console type='pty' tty='/dev/pts/2'>
234 <source path='/dev/pts/2'/>
235 <target port='0'/>
236 </console>
237
238 <serial type='pty'>
239 <source path='/dev/pts/2'/>
240 <target port='0'/>
241 </serial>
242
243 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
244 </devices>
245</domain>
246EOF
247
248# Mount point for instance fs
249ROOTFS=$VM_DIR/root
250mkdir -p $ROOTFS
251
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700252# Clean up from previous runs
253umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700254
255# Clean up old runs
256cd $VM_DIR
257rm -f $VM_DIR/disk
258
259# Create our instance fs
260qemu-img create -f qcow2 -b $VM_IMAGE disk
261
Dean Troyerdccd6b92011-11-01 15:46:14 -0500262# Finds the next available NBD device
263# Exits script if error connecting or none free
264# map_nbd image
265# returns full nbd device path
266function map_nbd {
267 for i in `seq 0 15`; do
268 if [ ! -e /sys/block/nbd$i/pid ]; then
269 NBD=/dev/nbd$i
270 # Connect to nbd and wait till it is ready
271 qemu-nbd -c $NBD $1
272 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
273 echo "Couldn't connect $NBD"
274 exit 1
275 fi
276 break
277 fi
278 done
279 if [ -z "$NBD" ]; then
280 echo "No free NBD slots"
281 exit 1
282 fi
283 echo $NBD
284}
285
Dean Troyer7a569732011-10-31 17:34:29 -0500286# Make sure we have nbd-ness
287modprobe nbd max_part=63
288
289# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500290NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500291NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700292
293# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700294mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700295
296# Configure instance network
297INTERFACES=$ROOTFS/etc/network/interfaces
298cat > $INTERFACES <<EOF
299auto lo
300iface lo inet loopback
301
302auto eth0
303iface eth0 inet static
304 address $GUEST_IP
305 netmask $GUEST_NETMASK
306 gateway $GUEST_GATEWAY
307EOF
308
309# User configuration for the instance
310chroot $ROOTFS groupadd libvirtd || true
311chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700312cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700313echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
314echo "stack:pass" | chroot $ROOTFS chpasswd
315echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
316
317# Gracefully cp only if source file/dir exists
318function cp_it {
319 if [ -e $1 ] || [ -d $1 ]; then
320 cp -pRL $1 $2
321 fi
322}
323
324# Copy over your ssh keys and env if desired
325COPYENV=${COPYENV:-1}
326if [ "$COPYENV" = "1" ]; then
327 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
328 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
329 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
330 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
331 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
332fi
333
334# pre-cache uec images
335for image_url in ${IMAGE_URLS//,/ }; do
336 IMAGE_FNAME=`basename "$image_url"`
337 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
338 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
339 fi
340 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
341done
342
343# Configure the runner
344RUN_SH=$ROOTFS/$DEST/run.sh
345cat > $RUN_SH <<EOF
346#!/usr/bin/env bash
347
348# Kill any existing screens
349killall screen
350
351# Install and run stack.sh
352sudo apt-get update
353sudo apt-get -y --force-yes install git-core vim-nox sudo
354if [ ! -d "$DEST/devstack" ]; then
355 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
356fi
357cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
358echo >> /$DEST/run.sh.log
359echo >> /$DEST/run.sh.log
360echo "All done! Time to start clicking." >> /$DEST/run.sh.log
361cat $DEST/run.sh.log
362EOF
363chmod 755 $RUN_SH
364
365# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700366RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700367cat > $RC_LOCAL <<EOF
368#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700369# cloud-init overwrites the hostname with ubuntuhost
370echo $GUEST_NAME > /etc/hostname
371hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700372su -c "$DEST/run.sh" stack
373EOF
374chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700375chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700376
377# Make our ip address hostnames look nice at the command prompt
378echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
379echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
380
381# Give stack ownership over $DEST so it may do the work needed
382chroot $ROOTFS chown -R stack $DEST
383
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700384# Set the hostname
385echo $GUEST_NAME > $ROOTFS/etc/hostname
386
387# We need the hostname to resolve for rabbit to launch
388if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
389 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
390fi
391
Dean Troyer6fe687b2011-10-31 20:30:04 -0500392# GRUB 2 wants to see /dev
393mount -o bind /dev $ROOTFS/dev
394
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700395# Change boot params so that we get a console log
396G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
397sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700398sed -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 -0700399sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
400echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
401echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
402echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
403
404chroot $ROOTFS update-grub
405umount $ROOTFS/dev
406
407# Pre-generate ssh host keys and allow password login
408chroot $ROOTFS dpkg-reconfigure openssh-server
409sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
410
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700411# Unmount
412umount $ROOTFS || echo 'ok'
Dean Troyer55c02732011-11-01 17:44:03 -0500413ROOTFS=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700414qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500415NBD=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700416
417# Create the instance
418cd $VM_DIR && virsh create libvirt.xml
419
420# Tail the console log till we are done
421WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
422if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
423 # Done creating the container, let's tail the log
424 echo
425 echo "============================================================="
426 echo " -- YAY! --"
427 echo "============================================================="
428 echo
429 echo "We're done launching the vm, about to start tailing the"
430 echo "stack.sh log. It will take a second or two to start."
431 echo
432 echo "Just CTRL-C at any time to stop tailing."
433
434 while [ ! -e "$VM_DIR/console.log" ]; do
435 sleep 1
436 done
437
Jesse Andrews16341962011-10-31 00:21:56 -0700438 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700439
440 TAIL_PID=$!
441
442 function kill_tail() {
443 kill $TAIL_PID
444 exit 1
445 }
446
447 # Let Ctrl-c kill tail and exit
448 trap kill_tail SIGINT
449
Jesse Andrews16341962011-10-31 00:21:56 -0700450 set +o xtrace
451
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700452 echo "Waiting stack.sh to finish..."
453 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700454 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700455 done
456
Jesse Andrews16341962011-10-31 00:21:56 -0700457 set -o xtrace
458
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700459 kill $TAIL_PID
460
Jesse Andrewsa06ac1c2011-10-31 22:29:23 -0700461 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700462 exit 1
463 fi
464 echo ""
465 echo "Finished - Zip-a-dee Doo-dah!"
466fi