blob: 3eb2ff89d650c0c0d8a52d2dd5cb6b82e0a34d73 [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
15# Echo commands
16set -o xtrace
17
18# Keep track of the current directory
19TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Jesse Andrews31989742011-10-30 18:56:05 -070020TOP_DIR=`cd $TOOLS_DIR/..; pwd`
Jesse Andrewse97a2e72011-10-30 18:37:49 -070021
22# Where to store files and instances
Jesse Andrewsb0559b22011-10-30 19:46:54 -070023WORK_DIR=${WORK_DIR:-/opt/kvmstack}
Jesse Andrewse97a2e72011-10-30 18:37:49 -070024
25# Where to store images
26IMAGES_DIR=$WORK_DIR/images
27
28# Create images dir
29mkdir -p $IMAGES_DIR
30
31# Abort if localrc is not set
32if [ ! -e $TOP_DIR/localrc ]; then
33 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
34 echo "See stack.sh for required passwords."
35 exit 1
36fi
37
Jesse Andrews7fa56132011-10-30 18:43:54 -070038cd $TOP_DIR
39
Jesse Andrewse97a2e72011-10-30 18:37:49 -070040# Source params
41source ./stackrc
42
43# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
44ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
45
Jesse Andrewse97a2e72011-10-30 18:37:49 -070046# Base image (natty by default)
47DIST_NAME=${DIST_NAME:-natty}
48IMAGE_FNAME=$DIST_NAME.raw
49
50# Name of our instance, used by libvirt
51GUEST_NAME=${GUEST_NAME:-devstack}
52
53# Original version of built image
Jesse Andrews2b7d2212011-10-30 19:37:56 -070054BASE_IMAGE=$IMAGES_DIR/$DIST_NAME.raw
Jesse Andrewse97a2e72011-10-30 18:37:49 -070055
56# Copy of base image, which we pre-install with tasty treats
57VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
58
59# Mop up after previous runs
60virsh destroy $GUEST_NAME || true
61
62# Where this vm is stored
63VM_DIR=$WORK_DIR/instances/$GUEST_NAME
64
65# Create vm dir
66mkdir -p $VM_DIR
67
68# Mount point into copied base image
69COPY_DIR=$VM_DIR/copy
70mkdir -p $COPY_DIR
71
Jesse Andrews2b7d2212011-10-30 19:37:56 -070072# Get the base image if it does not yet exist
73if [ ! -e $BASE_IMAGE ]; then
74 $TOOLS_DIR/get_uec_image.sh -f raw -r 5000 $DIST_NAME $BASE_IMAGE
Jesse Andrewse97a2e72011-10-30 18:37:49 -070075fi
76
77# Create a copy of the base image
78if [ ! -e $VM_IMAGE ]; then
79 cp -p $BASE_IMAGE $VM_IMAGE
80fi
81
82# Unmount the copied base image
83function unmount_images() {
84 # unmount the filesystem
85 while df | grep -q $COPY_DIR; do
86 umount $COPY_DIR || echo 'ok'
87 sleep 1
88 done
89}
90
91# Unmount from failed runs
92unmount_images
93
94# Ctrl-c catcher
95function kill_unmount() {
96 unmount_images
97 exit 1
98}
99
100# Install deps if needed
101dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
102
103# Let Ctrl-c kill tail and exit
104trap kill_unmount SIGINT
105
106# Where Openstack code will live in image
107DEST=${DEST:-/opt/stack}
108
109# Mount the file system
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700110# For some reason, UEC-based images want 255 heads * 63 sectors * 512 byte sectors = 8225280
111mount -t ext4 -o loop,offset=8225280 $VM_IMAGE $COPY_DIR
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700112
113# git clone only if directory doesn't exist already. Since ``DEST`` might not
114# be owned by the installation user, we create the directory and change the
115# ownership to the proper user.
116function git_clone {
117 if [ ! -d $2 ]; then
118 sudo mkdir $2
119 sudo chown `whoami` $2
120 git clone $1 $2
121 cd $2
122 # This checkout syntax works for both branches and tags
123 git checkout $3
124 fi
125}
126
127# Make sure that base requirements are installed
128cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
129chroot $COPY_DIR apt-get update
130chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
131chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
132chroot $COPY_DIR pip install `cat files/pips/*`
133
134# Clean out code repos if directed to do so
135if [ "$CLEAN" = "1" ]; then
136 rm -rf $COPY_DIR/$DEST
137fi
138
139# Cache openstack code
140mkdir -p $COPY_DIR/$DEST
141git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
142git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
143git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
144git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
145git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
146git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
147git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
148git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
149git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
150
151# Back to devstack
152cd $TOP_DIR
153
154# Unmount the filesystems
155unmount_images
156
157# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700158GUEST_NETWORK=${GUEST_NETWORK:-1}
159
160GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
161GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
162GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
163GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
164GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
165GUEST_RAM=${GUEST_RAM:-1524288}
166GUEST_CORES=${GUEST_CORES:-1}
167
168# libvirt.xml configuration
169NET_XML=$VM_DIR/net.xml
170cat > $NET_XML <<EOF
171<network>
172 <name>devstack-$GUEST_NETWORK</name>
173 <bridge name="stackbr%d" />
174 <forward/>
175 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
176</network>
177EOF
178
179virsh net-destroy devstack-$GUEST_NETWORK || true
180virsh net-create $VM_DIR/net.xml
181
182# libvirt.xml configuration
183LIBVIRT_XML=$VM_DIR/libvirt.xml
184cat > $LIBVIRT_XML <<EOF
185<domain type='kvm'>
186 <name>$GUEST_NAME</name>
187 <memory>$GUEST_RAM</memory>
188 <os>
189 <type>hvm</type>
190 <bootmenu enable='yes'/>
191 </os>
192 <features>
193 <acpi/>
194 </features>
195 <vcpu>$GUEST_CORES</vcpu>
196 <devices>
197 <disk type='file'>
198 <driver type='qcow2'/>
199 <source file='$VM_DIR/disk'/>
200 <target dev='vda' bus='virtio'/>
201 </disk>
202
203 <interface type='network'>
204 <source network='devstack-$GUEST_NETWORK'/>
205 </interface>
206
207 <!-- The order is significant here. File must be defined first -->
208 <serial type="file">
209 <source path='$VM_DIR/console.log'/>
210 <target port='1'/>
211 </serial>
212
213 <console type='pty' tty='/dev/pts/2'>
214 <source path='/dev/pts/2'/>
215 <target port='0'/>
216 </console>
217
218 <serial type='pty'>
219 <source path='/dev/pts/2'/>
220 <target port='0'/>
221 </serial>
222
223 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
224 </devices>
225</domain>
226EOF
227
228# Mount point for instance fs
229ROOTFS=$VM_DIR/root
230mkdir -p $ROOTFS
231
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700232# Clean up from previous runs
233umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700234
235# Clean up old runs
236cd $VM_DIR
237rm -f $VM_DIR/disk
238
239# Create our instance fs
240qemu-img create -f qcow2 -b $VM_IMAGE disk
241
Dean Troyer7a569732011-10-31 17:34:29 -0500242# Make sure we have nbd-ness
243modprobe nbd max_part=63
244
245# Set up nbd
Dean Troyer6fe687b2011-10-31 20:30:04 -0500246for i in `seq 0 15`; do
Dean Troyer7a569732011-10-31 17:34:29 -0500247 if [ ! -e /sys/block/nbd$i/pid ]; then
248 NBD=/dev/nbd$i
249 # Connect to nbd and wait till it is ready
250 qemu-nbd -c $NBD disk
251 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
252 echo "Couldn't connect $NBD"
253 exit 1
254 fi
255 break
256 fi
257done
258if [ -z "$NBD" ]; then
259 echo "No free NBD slots"
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700260 exit 1
261fi
Dean Troyer7a569732011-10-31 17:34:29 -0500262NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700263
264# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700265mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700266
267# Configure instance network
268INTERFACES=$ROOTFS/etc/network/interfaces
269cat > $INTERFACES <<EOF
270auto lo
271iface lo inet loopback
272
273auto eth0
274iface eth0 inet static
275 address $GUEST_IP
276 netmask $GUEST_NETMASK
277 gateway $GUEST_GATEWAY
278EOF
279
280# User configuration for the instance
281chroot $ROOTFS groupadd libvirtd || true
282chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700283cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700284echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
285echo "stack:pass" | chroot $ROOTFS chpasswd
286echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
287
288# Gracefully cp only if source file/dir exists
289function cp_it {
290 if [ -e $1 ] || [ -d $1 ]; then
291 cp -pRL $1 $2
292 fi
293}
294
295# Copy over your ssh keys and env if desired
296COPYENV=${COPYENV:-1}
297if [ "$COPYENV" = "1" ]; then
298 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
299 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
300 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
301 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
302 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
303fi
304
305# pre-cache uec images
306for image_url in ${IMAGE_URLS//,/ }; do
307 IMAGE_FNAME=`basename "$image_url"`
308 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
309 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
310 fi
311 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
312done
313
314# Configure the runner
315RUN_SH=$ROOTFS/$DEST/run.sh
316cat > $RUN_SH <<EOF
317#!/usr/bin/env bash
318
319# Kill any existing screens
320killall screen
321
322# Install and run stack.sh
323sudo apt-get update
324sudo apt-get -y --force-yes install git-core vim-nox sudo
325if [ ! -d "$DEST/devstack" ]; then
326 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
327fi
328cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
329echo >> /$DEST/run.sh.log
330echo >> /$DEST/run.sh.log
331echo "All done! Time to start clicking." >> /$DEST/run.sh.log
332cat $DEST/run.sh.log
333EOF
334chmod 755 $RUN_SH
335
336# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700337RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700338cat > $RC_LOCAL <<EOF
339#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700340# cloud-init overwrites the hostname with ubuntuhost
341echo $GUEST_NAME > /etc/hostname
342hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700343su -c "$DEST/run.sh" stack
344EOF
345chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700346chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700347
348# Make our ip address hostnames look nice at the command prompt
349echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
350echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
351
352# Give stack ownership over $DEST so it may do the work needed
353chroot $ROOTFS chown -R stack $DEST
354
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700355# Set the hostname
356echo $GUEST_NAME > $ROOTFS/etc/hostname
357
358# We need the hostname to resolve for rabbit to launch
359if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
360 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
361fi
362
Dean Troyer6fe687b2011-10-31 20:30:04 -0500363# GRUB 2 wants to see /dev
364mount -o bind /dev $ROOTFS/dev
365
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700366# Change boot params so that we get a console log
367G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
368sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700369sed -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 -0700370sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
371echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
372echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
373echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
374
375chroot $ROOTFS update-grub
376umount $ROOTFS/dev
377
378# Pre-generate ssh host keys and allow password login
379chroot $ROOTFS dpkg-reconfigure openssh-server
380sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
381
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700382# Unmount
383umount $ROOTFS || echo 'ok'
384qemu-nbd -d $NBD
385
386# Create the instance
387cd $VM_DIR && virsh create libvirt.xml
388
389# Tail the console log till we are done
390WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
391if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
392 # Done creating the container, let's tail the log
393 echo
394 echo "============================================================="
395 echo " -- YAY! --"
396 echo "============================================================="
397 echo
398 echo "We're done launching the vm, about to start tailing the"
399 echo "stack.sh log. It will take a second or two to start."
400 echo
401 echo "Just CTRL-C at any time to stop tailing."
402
403 while [ ! -e "$VM_DIR/console.log" ]; do
404 sleep 1
405 done
406
Jesse Andrews16341962011-10-31 00:21:56 -0700407 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700408
409 TAIL_PID=$!
410
411 function kill_tail() {
412 kill $TAIL_PID
413 exit 1
414 }
415
416 # Let Ctrl-c kill tail and exit
417 trap kill_tail SIGINT
418
Jesse Andrews16341962011-10-31 00:21:56 -0700419 set +o xtrace
420
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700421 echo "Waiting stack.sh to finish..."
422 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700423 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700424 done
425
Jesse Andrews16341962011-10-31 00:21:56 -0700426 set -o xtrace
427
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700428 kill $TAIL_PID
429
430 if grep -q "stack.sh failed" $VM_DIR/console.log; then
431 exit 1
432 fi
433 echo ""
434 echo "Finished - Zip-a-dee Doo-dah!"
435fi