blob: 54be471b4e463bc42f1fe36523885e8bc9e19e0a [file] [log] [blame]
Anthony Young1b7a42e2011-10-19 02:34:06 -07001#!/usr/bin/env bash
2
Anthony Youngfa4b5eb2011-10-19 11:27:02 -07003UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
4if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
5 echo "This script only works with oneiric"
6 exit 1
7fi
8
Anthony Young1b7a42e2011-10-19 02:34:06 -07009# Echo commands
10set -o xtrace
11
12# Keep track of the current directory
13TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070014TOP_DIR=$TOOLS_DIR/..
Anthony Young1b7a42e2011-10-19 02:34:06 -070015
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070016# Configure the root password of the vm
Anthony Young1b7a42e2011-10-19 02:34:06 -070017ROOT_PASSWORD=${ROOT_PASSWORD:password}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070018
19# Where to store files and instances
20KVMSTACK_DIR=${KVMSTACK_DIR:-/opt/kvmstack}
21
22# Where to store images
23IMAGES_DIR=$KVMSTACK_DIR/images
24
25# Create images dir
26mkdir -p $IMAGES_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -070027
28# Move to top devstack dir
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070029cd $TOP_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -070030
31# Abort if localrc is not set
32if [ ! -e ./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
38# Source params
39source ./stackrc
40
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070041# Base image (natty by default)
42DIST_NAME=${DIST_NAME:-natty}
43IMAGE_FNAME=$DIST_NAME.raw
Anthony Young1b7a42e2011-10-19 02:34:06 -070044
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070045# Original version of built image
46BASE_IMAGE=$KVMSTACK_DIR/images/natty.raw
Anthony Young1b7a42e2011-10-19 02:34:06 -070047
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070048# Copy of base image, which we pre-install with tasty treats
49BASE_IMAGE_COPY=$IMAGES_DIR/$DIST_NAME.raw.copy
50
51# Name of our instance, used by libvirt
Anthony Young1b7a42e2011-10-19 02:34:06 -070052VM_NAME=${VM_NAME:-kvmstack}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070053
54# Mop up after previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -070055virsh shutdown $VM_NAME
56virsh destroy $VM_NAME
57
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070058# Where this vm is stored
59VM_DIR=$KVMSTACK_DIR/instances/$VM_NAME
Anthony Young1b7a42e2011-10-19 02:34:06 -070060
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070061# Create vm dir
Anthony Young1b7a42e2011-10-19 02:34:06 -070062mkdir -p $VM_DIR
63
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070064# Mount point into copied base image
Anthony Young1b7a42e2011-10-19 02:34:06 -070065COPY_DIR=$VM_DIR/copy
66mkdir -p $COPY_DIR
67
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070068# Create the base image if it does not yet exist
Anthony Young1b7a42e2011-10-19 02:34:06 -070069if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
70 cd $TOOLS_DIR
71 ./make_image.sh -m -r 5000 natty raw
72 mv natty.raw $BASE_IMAGE
73 cd $TOP_DIR
74fi
75
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070076# Create a copy of the base image
77if [ ! -e $BASE_IMAGE_COPY ]; then
78 cp -p $BASE_IMAGE $BASE_IMAGE_COPY
79fi
80
81# Unmount the copied base image
Anthony Young1b7a42e2011-10-19 02:34:06 -070082function unmount_images() {
83 # unmount the filesystem
84 while df | grep -q $COPY_DIR; do
85 umount $COPY_DIR || echo 'ok'
86 sleep 1
87 done
88}
89
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070090# Unmount from failed runs
Anthony Young1b7a42e2011-10-19 02:34:06 -070091unmount_images
92
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070093# Ctrl-c catcher
94function kill_unmount() {
Anthony Young1b7a42e2011-10-19 02:34:06 -070095 unmount_images
96 exit 1
97}
98
Anthony Young1b7a42e2011-10-19 02:34:06 -070099# Install deps
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700100apt-get install -y --force-yes kvm libvirt-bin kpartx
Anthony Young1b7a42e2011-10-19 02:34:06 -0700101
102# Let Ctrl-c kill tail and exit
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700103trap kill_unmount SIGINT
Anthony Young1b7a42e2011-10-19 02:34:06 -0700104
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700105# Where Openstack code will live in image
Anthony Young1b7a42e2011-10-19 02:34:06 -0700106DEST=${DEST:-/opt/stack}
107
108# Mount the file system
109mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR
110
111# git clone only if directory doesn't exist already. Since ``DEST`` might not
112# be owned by the installation user, we create the directory and change the
113# ownership to the proper user.
114function git_clone {
115 if [ ! -d $2 ]; then
116 sudo mkdir $2
117 sudo chown `whoami` $2
118 git clone $1 $2
119 cd $2
120 # This checkout syntax works for both branches and tags
121 git checkout $3
122 fi
123}
124
125# Make sure that base requirements are installed
126cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
127chroot $COPY_DIR apt-get update
128chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
129chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
130chroot $COPY_DIR pip install `cat files/pips/*`
131
132# Clean out code repos if directed to do so
133if [ "$CLEAN" = "1" ]; then
134 rm -rf $COPY_DIR/$DEST
135fi
136
137# Cache openstack code
138mkdir -p $COPY_DIR/$DEST
139git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
140git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
141git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
142git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
143git_clone $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_TAG
144git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
145git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
146git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
147git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
148
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700149# Back to devstack
150cd $TOP_DIR
151
152# Unmount the filesystems
Anthony Young1b7a42e2011-10-19 02:34:06 -0700153unmount_images
154
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700155# Clean up old runs
156cd $VM_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -0700157rm -f $VM_DIR/disk
158
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700159# Clean up old instance data
Anthony Young1b7a42e2011-10-19 02:34:06 -0700160qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
161
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700162# Network configuration variables
Anthony Young1b7a42e2011-10-19 02:34:06 -0700163BRIDGE=${BRIDGE:-br0}
164CONTAINER=${CONTAINER:-STACK}
165CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
166CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
167CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
168CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
169CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
170
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700171# libvirt.xml configuration
Anthony Young1b7a42e2011-10-19 02:34:06 -0700172LIBVIRT_XML=libvirt.xml
173cat > $LIBVIRT_XML <<EOF
174<domain type='kvm'>
175 <name>$VM_NAME</name>
176 <memory>1524288</memory>
177 <os>
178 <type>hvm</type>
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700179 <boot dev='hd'/>
Anthony Youngf6f52272011-10-19 02:58:18 -0700180 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700181 </os>
182 <features>
183 <acpi/>
184 </features>
185 <vcpu>1</vcpu>
186 <devices>
187 <disk type='file'>
188 <driver type='qcow2'/>
189 <source file='$VM_DIR/disk'/>
190 <target dev='vda' bus='virtio'/>
191 </disk>
192
193 <interface type='bridge'>
194 <source bridge='$BRIDGE'/>
195 <mac address='$CONTAINER_MAC'/>
196 </interface>
197
198 <!-- The order is significant here. File must be defined first -->
199 <serial type="file">
200 <source path='$VM_DIR/console.log'/>
201 <target port='1'/>
202 </serial>
203
204 <console type='pty' tty='/dev/pts/2'>
205 <source path='/dev/pts/2'/>
206 <target port='0'/>
207 </console>
208
209 <serial type='pty'>
210 <source path='/dev/pts/2'/>
211 <target port='0'/>
212 </serial>
213
214 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
215 </devices>
216</domain>
217EOF
218
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700219# Mount point for instance fs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700220ROOTFS=$VM_DIR/root
221mkdir -p $ROOTFS
222
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700223# Make sure we have nbd-ness
Anthony Young1b7a42e2011-10-19 02:34:06 -0700224modprobe nbd max_part=63
225
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700226# Clean up from previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700227umount $ROOTFS || echo 'ok'
228qemu-nbd -d /dev/nbd5 || echo 'ok'
229
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700230# Mount the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700231qemu-nbd -c /dev/nbd5 disk
232mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
233
234# Configure instance network
235INTERFACES=$ROOTFS/etc/network/interfaces
236cat > $INTERFACES <<EOF
237auto lo
238iface lo inet loopback
239
240auto eth0
241iface eth0 inet static
242 address $CONTAINER_IP
243 netmask $CONTAINER_NETMASK
244 gateway $CONTAINER_GATEWAY
245EOF
246
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700247# User configuration for the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700248chroot $ROOTFS groupadd libvirtd
249chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
250cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
251echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700252echo "stack:pass" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700253echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
254
255# Gracefully cp only if source file/dir exists
256function cp_it {
257 if [ -e $1 ] || [ -d $1 ]; then
258 cp -pRL $1 $2
259 fi
260}
261
262# Copy over your ssh keys and env if desired
263COPYENV=${COPYENV:-1}
264if [ "$COPYENV" = "1" ]; then
265 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
266 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
267 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
268 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
269 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
270fi
271
272# Configure the runner
273RUN_SH=$ROOTFS/$DEST/run.sh
274cat > $RUN_SH <<EOF
275#!/usr/bin/env bash
Anthony Young1b7a42e2011-10-19 02:34:06 -0700276
277# Kill any existing screens
278killall screen
279
280# Install and run stack.sh
281sudo apt-get update
282sudo apt-get -y --force-yes install git-core vim-nox sudo
283if [ ! -d "$DEST/devstack" ]; then
284 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
285fi
286cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
287echo >> /$DEST/run.sh.log
288echo >> /$DEST/run.sh.log
289echo "All done! Time to start clicking." >> /$DEST/run.sh.log
290EOF
Anthony Young1b7a42e2011-10-19 02:34:06 -0700291chmod 755 $RUN_SH
292
293# Make runner launch on boot
294RC_LOCAL=$ROOTFS/etc/init.d/local
295cat > $RC_LOCAL <<EOF
296#!/bin/sh -e
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700297# Reboot if this is our first run to enable console log on natty :(
298if [ ! -e /root/firstlaunch ]; then
299 touch /root/firstlaunch
300# reboot
301 exit 0
302fi
Anthony Young1b7a42e2011-10-19 02:34:06 -0700303su -c "$DEST/run.sh" stack
304EOF
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700305chmod +x $RC_LOCAL
306chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young1b7a42e2011-10-19 02:34:06 -0700307
308# Make our ip address hostnames look nice at the command prompt
309echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
310echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
311
312# Give stack ownership over $DEST so it may do the work needed
313chroot $ROOTFS chown -R stack $DEST
314
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700315# Change boot params so that we get a console log
Anthony Youngf6f52272011-10-19 02:58:18 -0700316sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
317
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700318# Unmount
319umount $ROOTFS || echo 'ok'
Anthony Young1b7a42e2011-10-19 02:34:06 -0700320qemu-nbd -d /dev/nbd5
321
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700322# Create the instance
323cd $VM_DIR && virsh create libvirt.xml
324
325# Tail the console log till we are done
326WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-0}
327if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
328 # Done creating the container, let's tail the log
329 echo
330 echo "============================================================="
331 echo " -- YAY! --"
332 echo "============================================================="
333 echo
334 echo "We're done launching the vm, about to start tailing the"
335 echo "stack.sh log. It will take a second or two to start."
336 echo
337 echo "Just CTRL-C at any time to stop tailing."
338
339 while [ ! -e "$VM_DIR/console.log" ]; do
340 sleep 1
341 done
342
343 tail -F $VM_DIR/console.log &
344
345 TAIL_PID=$!
346
347 function kill_tail() {
348 kill $TAIL_PID
349 exit 1
350 }
351
352 # Let Ctrl-c kill tail and exit
353 trap kill_tail SIGINT
354
355 echo "Waiting stack.sh to finish..."
356 while ! cat $VM_DIR/console.log | grep -q 'stack.sh completed' ; do
357 sleep 5
358 done
359
360 kill $TAIL_PID
361 echo ""
362 echo "Finished - Zip-a-dee Doo-dah!"
363fi