blob: 332683c95c696ea03987e7b5efaf73e9870d812f [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>
Anthony Youngd51812d2011-10-19 20:09:43 -0700178 <type>hvm</type>
179 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700180 </os>
181 <features>
182 <acpi/>
183 </features>
184 <vcpu>1</vcpu>
185 <devices>
186 <disk type='file'>
187 <driver type='qcow2'/>
188 <source file='$VM_DIR/disk'/>
189 <target dev='vda' bus='virtio'/>
190 </disk>
191
192 <interface type='bridge'>
193 <source bridge='$BRIDGE'/>
194 <mac address='$CONTAINER_MAC'/>
195 </interface>
196
197 <!-- The order is significant here. File must be defined first -->
198 <serial type="file">
199 <source path='$VM_DIR/console.log'/>
200 <target port='1'/>
201 </serial>
202
203 <console type='pty' tty='/dev/pts/2'>
204 <source path='/dev/pts/2'/>
205 <target port='0'/>
206 </console>
207
208 <serial type='pty'>
209 <source path='/dev/pts/2'/>
210 <target port='0'/>
211 </serial>
212
213 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
214 </devices>
215</domain>
216EOF
217
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700218# Mount point for instance fs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700219ROOTFS=$VM_DIR/root
220mkdir -p $ROOTFS
221
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700222# Make sure we have nbd-ness
Anthony Young1b7a42e2011-10-19 02:34:06 -0700223modprobe nbd max_part=63
224
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700225# Clean up from previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700226umount $ROOTFS || echo 'ok'
227qemu-nbd -d /dev/nbd5 || echo 'ok'
228
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700229# Mount the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700230qemu-nbd -c /dev/nbd5 disk
231mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
232
233# Configure instance network
234INTERFACES=$ROOTFS/etc/network/interfaces
235cat > $INTERFACES <<EOF
236auto lo
237iface lo inet loopback
238
239auto eth0
240iface eth0 inet static
241 address $CONTAINER_IP
242 netmask $CONTAINER_NETMASK
243 gateway $CONTAINER_GATEWAY
244EOF
245
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700246# User configuration for the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700247chroot $ROOTFS groupadd libvirtd
248chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
249cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
250echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700251echo "stack:pass" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700252echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
253
254# Gracefully cp only if source file/dir exists
255function cp_it {
256 if [ -e $1 ] || [ -d $1 ]; then
257 cp -pRL $1 $2
258 fi
259}
260
261# Copy over your ssh keys and env if desired
262COPYENV=${COPYENV:-1}
263if [ "$COPYENV" = "1" ]; then
264 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
265 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
266 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
267 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
268 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
269fi
270
271# Configure the runner
272RUN_SH=$ROOTFS/$DEST/run.sh
273cat > $RUN_SH <<EOF
274#!/usr/bin/env bash
Anthony Young1b7a42e2011-10-19 02:34:06 -0700275
276# Kill any existing screens
277killall screen
278
279# Install and run stack.sh
280sudo apt-get update
281sudo apt-get -y --force-yes install git-core vim-nox sudo
282if [ ! -d "$DEST/devstack" ]; then
283 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
284fi
285cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
286echo >> /$DEST/run.sh.log
287echo >> /$DEST/run.sh.log
288echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Youngd51812d2011-10-19 20:09:43 -0700289cat $DEST/run.sh.log
Anthony Young1b7a42e2011-10-19 02:34:06 -0700290EOF
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
Anthony Youngd51812d2011-10-19 20:09:43 -0700300 reboot -f
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700301 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 Youngd51812d2011-10-19 20:09:43 -0700307#chroot $ROOTFS update-rc.d local start 80 2 . stop 80 0 1 6
Anthony Young1b7a42e2011-10-19 02:34:06 -0700308
309# Make our ip address hostnames look nice at the command prompt
310echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
311echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
312
313# Give stack ownership over $DEST so it may do the work needed
314chroot $ROOTFS chown -R stack $DEST
315
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700316# Change boot params so that we get a console log
Anthony Youngf6f52272011-10-19 02:58:18 -0700317sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
Anthony Youngd51812d2011-10-19 20:09:43 -0700318sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
319#chroot $ROOTFS grub-install /dev/vda
Anthony Youngf6f52272011-10-19 02:58:18 -0700320
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700321# Unmount
322umount $ROOTFS || echo 'ok'
Anthony Young1b7a42e2011-10-19 02:34:06 -0700323qemu-nbd -d /dev/nbd5
324
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700325# Create the instance
326cd $VM_DIR && virsh create libvirt.xml
327
328# Tail the console log till we are done
Anthony Youngd51812d2011-10-19 20:09:43 -0700329WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700330if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
331 # Done creating the container, let's tail the log
332 echo
333 echo "============================================================="
334 echo " -- YAY! --"
335 echo "============================================================="
336 echo
337 echo "We're done launching the vm, about to start tailing the"
338 echo "stack.sh log. It will take a second or two to start."
339 echo
340 echo "Just CTRL-C at any time to stop tailing."
341
342 while [ ! -e "$VM_DIR/console.log" ]; do
343 sleep 1
344 done
345
346 tail -F $VM_DIR/console.log &
347
348 TAIL_PID=$!
349
350 function kill_tail() {
351 kill $TAIL_PID
352 exit 1
353 }
354
355 # Let Ctrl-c kill tail and exit
356 trap kill_tail SIGINT
357
358 echo "Waiting stack.sh to finish..."
Anthony Youngd51812d2011-10-19 20:09:43 -0700359 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700360 sleep 5
361 done
362
363 kill $TAIL_PID
364 echo ""
365 echo "Finished - Zip-a-dee Doo-dah!"
366fi