blob: 48e285348d7e110de95c939ab0ebf01efcacacc2 [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}
177
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
197virsh net-destroy devstack-$GUEST_NETWORK || true
198virsh net-create $VM_DIR/net.xml
199
200# libvirt.xml configuration
201LIBVIRT_XML=$VM_DIR/libvirt.xml
202cat > $LIBVIRT_XML <<EOF
203<domain type='kvm'>
204 <name>$GUEST_NAME</name>
205 <memory>$GUEST_RAM</memory>
206 <os>
207 <type>hvm</type>
208 <bootmenu enable='yes'/>
209 </os>
210 <features>
211 <acpi/>
212 </features>
213 <vcpu>$GUEST_CORES</vcpu>
214 <devices>
215 <disk type='file'>
216 <driver type='qcow2'/>
217 <source file='$VM_DIR/disk'/>
218 <target dev='vda' bus='virtio'/>
219 </disk>
220
221 <interface type='network'>
222 <source network='devstack-$GUEST_NETWORK'/>
223 </interface>
224
225 <!-- The order is significant here. File must be defined first -->
226 <serial type="file">
227 <source path='$VM_DIR/console.log'/>
228 <target port='1'/>
229 </serial>
230
231 <console type='pty' tty='/dev/pts/2'>
232 <source path='/dev/pts/2'/>
233 <target port='0'/>
234 </console>
235
236 <serial type='pty'>
237 <source path='/dev/pts/2'/>
238 <target port='0'/>
239 </serial>
240
241 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
242 </devices>
243</domain>
244EOF
245
246# Mount point for instance fs
247ROOTFS=$VM_DIR/root
248mkdir -p $ROOTFS
249
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700250# Clean up from previous runs
251umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700252
253# Clean up old runs
254cd $VM_DIR
255rm -f $VM_DIR/disk
256
257# Create our instance fs
258qemu-img create -f qcow2 -b $VM_IMAGE disk
259
Dean Troyerdccd6b92011-11-01 15:46:14 -0500260# Finds the next available NBD device
261# Exits script if error connecting or none free
262# map_nbd image
263# returns full nbd device path
264function map_nbd {
265 for i in `seq 0 15`; do
266 if [ ! -e /sys/block/nbd$i/pid ]; then
267 NBD=/dev/nbd$i
268 # Connect to nbd and wait till it is ready
269 qemu-nbd -c $NBD $1
270 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
271 echo "Couldn't connect $NBD"
272 exit 1
273 fi
274 break
275 fi
276 done
277 if [ -z "$NBD" ]; then
278 echo "No free NBD slots"
279 exit 1
280 fi
281 echo $NBD
282}
283
Dean Troyer7a569732011-10-31 17:34:29 -0500284# Make sure we have nbd-ness
285modprobe nbd max_part=63
286
287# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500288NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500289NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700290
291# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700292mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700293
294# Configure instance network
295INTERFACES=$ROOTFS/etc/network/interfaces
296cat > $INTERFACES <<EOF
297auto lo
298iface lo inet loopback
299
300auto eth0
301iface eth0 inet static
302 address $GUEST_IP
303 netmask $GUEST_NETMASK
304 gateway $GUEST_GATEWAY
305EOF
306
307# User configuration for the instance
308chroot $ROOTFS groupadd libvirtd || true
309chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700310cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700311echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
312echo "stack:pass" | chroot $ROOTFS chpasswd
313echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
314
315# Gracefully cp only if source file/dir exists
316function cp_it {
317 if [ -e $1 ] || [ -d $1 ]; then
318 cp -pRL $1 $2
319 fi
320}
321
322# Copy over your ssh keys and env if desired
323COPYENV=${COPYENV:-1}
324if [ "$COPYENV" = "1" ]; then
325 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
326 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
327 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
328 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
329 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
330fi
331
332# pre-cache uec images
333for image_url in ${IMAGE_URLS//,/ }; do
334 IMAGE_FNAME=`basename "$image_url"`
335 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
336 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
337 fi
338 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
339done
340
341# Configure the runner
342RUN_SH=$ROOTFS/$DEST/run.sh
343cat > $RUN_SH <<EOF
344#!/usr/bin/env bash
345
346# Kill any existing screens
347killall screen
348
349# Install and run stack.sh
350sudo apt-get update
351sudo apt-get -y --force-yes install git-core vim-nox sudo
352if [ ! -d "$DEST/devstack" ]; then
353 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
354fi
355cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
356echo >> /$DEST/run.sh.log
357echo >> /$DEST/run.sh.log
358echo "All done! Time to start clicking." >> /$DEST/run.sh.log
359cat $DEST/run.sh.log
360EOF
361chmod 755 $RUN_SH
362
363# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700364RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700365cat > $RC_LOCAL <<EOF
366#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700367# cloud-init overwrites the hostname with ubuntuhost
368echo $GUEST_NAME > /etc/hostname
369hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700370su -c "$DEST/run.sh" stack
371EOF
372chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700373chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700374
375# Make our ip address hostnames look nice at the command prompt
376echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
377echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
378
379# Give stack ownership over $DEST so it may do the work needed
380chroot $ROOTFS chown -R stack $DEST
381
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700382# Set the hostname
383echo $GUEST_NAME > $ROOTFS/etc/hostname
384
385# We need the hostname to resolve for rabbit to launch
386if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
387 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
388fi
389
Dean Troyer6fe687b2011-10-31 20:30:04 -0500390# GRUB 2 wants to see /dev
391mount -o bind /dev $ROOTFS/dev
392
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700393# Change boot params so that we get a console log
394G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
395sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700396sed -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 -0700397sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
398echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
399echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
400echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
401
402chroot $ROOTFS update-grub
403umount $ROOTFS/dev
404
405# Pre-generate ssh host keys and allow password login
406chroot $ROOTFS dpkg-reconfigure openssh-server
407sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
408
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700409# Unmount
410umount $ROOTFS || echo 'ok'
Dean Troyer55c02732011-11-01 17:44:03 -0500411ROOTFS=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700412qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500413NBD=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700414
415# Create the instance
416cd $VM_DIR && virsh create libvirt.xml
417
418# Tail the console log till we are done
419WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
420if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
421 # Done creating the container, let's tail the log
422 echo
423 echo "============================================================="
424 echo " -- YAY! --"
425 echo "============================================================="
426 echo
427 echo "We're done launching the vm, about to start tailing the"
428 echo "stack.sh log. It will take a second or two to start."
429 echo
430 echo "Just CTRL-C at any time to stop tailing."
431
432 while [ ! -e "$VM_DIR/console.log" ]; do
433 sleep 1
434 done
435
Jesse Andrews16341962011-10-31 00:21:56 -0700436 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700437
438 TAIL_PID=$!
439
440 function kill_tail() {
441 kill $TAIL_PID
442 exit 1
443 }
444
445 # Let Ctrl-c kill tail and exit
446 trap kill_tail SIGINT
447
Jesse Andrews16341962011-10-31 00:21:56 -0700448 set +o xtrace
449
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700450 echo "Waiting stack.sh to finish..."
451 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700452 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700453 done
454
Jesse Andrews16341962011-10-31 00:21:56 -0700455 set -o xtrace
456
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700457 kill $TAIL_PID
458
Jesse Andrewsa06ac1c2011-10-31 22:29:23 -0700459 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700460 exit 1
461 fi
462 echo ""
463 echo "Finished - Zip-a-dee Doo-dah!"
464fi