blob: b9f6163c1fdeb442e68025888cd4823b45f48b0b [file] [log] [blame]
Anthony Young1b7a42e2011-10-19 02:34:06 -07001#!/usr/bin/env bash
2
Anthony Young3ee09ec2011-10-19 20:35:04 -07003# Make sure that we have the proper version of ubuntu
Anthony Youngfa4b5eb2011-10-19 11:27:02 -07004UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
5if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
Anthony Young3ee09ec2011-10-19 20:35:04 -07006 if [ ! "natty" = "$UBUNTU_VERSION" ]; then
7 echo "This script only works with oneiric and natty"
8 exit 1
9 fi
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070010fi
11
Anthony Young1b7a42e2011-10-19 02:34:06 -070012# Echo commands
13set -o xtrace
14
15# Keep track of the current directory
16TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070017TOP_DIR=$TOOLS_DIR/..
Anthony Young1b7a42e2011-10-19 02:34:06 -070018
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070019# Configure the root password of the vm
Anthony Young1b7a42e2011-10-19 02:34:06 -070020ROOT_PASSWORD=${ROOT_PASSWORD:password}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070021
22# Where to store files and instances
23KVMSTACK_DIR=${KVMSTACK_DIR:-/opt/kvmstack}
24
25# Where to store images
26IMAGES_DIR=$KVMSTACK_DIR/images
27
28# Create images dir
29mkdir -p $IMAGES_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -070030
31# Move to top devstack dir
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070032cd $TOP_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -070033
34# Abort if localrc is not set
35if [ ! -e ./localrc ]; then
36 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
37 echo "See stack.sh for required passwords."
38 exit 1
39fi
40
41# Source params
42source ./stackrc
43
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070044# Base image (natty by default)
45DIST_NAME=${DIST_NAME:-natty}
46IMAGE_FNAME=$DIST_NAME.raw
Anthony Young1b7a42e2011-10-19 02:34:06 -070047
Dean Troyere4f030f2011-10-21 14:28:03 -050048# Name of our instance, used by libvirt
49CONTAINER_NAME=${CONTAINER_NAME:-kvmstack}
50
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070051# Original version of built image
Dean Troyerad57a3a2011-10-21 14:29:30 -050052BASE_IMAGE=$KVMSTACK_DIR/images/$DIST_NAME.raw
Anthony Young1b7a42e2011-10-19 02:34:06 -070053
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070054# Copy of base image, which we pre-install with tasty treats
Dean Troyerd0332912011-10-21 14:58:44 -050055VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$CONTAINER_NAME.raw
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070056
57# Mop up after previous runs
Anthony Young03c1fa62011-10-20 11:35:14 -070058virsh destroy $CONTAINER_NAME
Anthony Young1b7a42e2011-10-19 02:34:06 -070059
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070060# Where this vm is stored
Anthony Young03c1fa62011-10-20 11:35:14 -070061VM_DIR=$KVMSTACK_DIR/instances/$CONTAINER_NAME
Anthony Young1b7a42e2011-10-19 02:34:06 -070062
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070063# Create vm dir
Anthony Young1b7a42e2011-10-19 02:34:06 -070064mkdir -p $VM_DIR
65
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070066# Mount point into copied base image
Anthony Young1b7a42e2011-10-19 02:34:06 -070067COPY_DIR=$VM_DIR/copy
68mkdir -p $COPY_DIR
69
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070070# Create the base image if it does not yet exist
Anthony Young1b7a42e2011-10-19 02:34:06 -070071if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
72 cd $TOOLS_DIR
Dean Troyerad57a3a2011-10-21 14:29:30 -050073 ./make_image.sh -m -r 5000 $DIST_NAME raw
74 mv $DIST_NAME.raw $BASE_IMAGE
Anthony Young1b7a42e2011-10-19 02:34:06 -070075 cd $TOP_DIR
76fi
77
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070078# Create a copy of the base image
Dean Troyerd0332912011-10-21 14:58:44 -050079if [ ! -e $VM_IMAGE ]; then
80 cp -p $BASE_IMAGE $VM_IMAGE
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070081fi
82
83# Unmount the copied base image
Anthony Young1b7a42e2011-10-19 02:34:06 -070084function unmount_images() {
85 # unmount the filesystem
86 while df | grep -q $COPY_DIR; do
87 umount $COPY_DIR || echo 'ok'
88 sleep 1
89 done
90}
91
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070092# Unmount from failed runs
Anthony Young1b7a42e2011-10-19 02:34:06 -070093unmount_images
94
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070095# Ctrl-c catcher
96function kill_unmount() {
Anthony Young1b7a42e2011-10-19 02:34:06 -070097 unmount_images
98 exit 1
99}
100
Anthony Young1b7a42e2011-10-19 02:34:06 -0700101# Install deps
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700102apt-get install -y --force-yes kvm libvirt-bin kpartx
Anthony Young1b7a42e2011-10-19 02:34:06 -0700103
104# Let Ctrl-c kill tail and exit
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700105trap kill_unmount SIGINT
Anthony Young1b7a42e2011-10-19 02:34:06 -0700106
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700107# Where Openstack code will live in image
Anthony Young1b7a42e2011-10-19 02:34:06 -0700108DEST=${DEST:-/opt/stack}
109
110# Mount the file system
Dean Troyerd0332912011-10-21 14:58:44 -0500111mount -o loop,offset=32256 $VM_IMAGE $COPY_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -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 $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_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
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700151# Unmount the filesystems
Anthony Young1b7a42e2011-10-19 02:34:06 -0700152unmount_images
153
Anthony Youngbabb2e02011-10-20 12:32:58 -0700154# Back to devstack
155cd $TOP_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -0700156
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700157# Network configuration variables
Anthony Young1b7a42e2011-10-19 02:34:06 -0700158BRIDGE=${BRIDGE:-br0}
159CONTAINER=${CONTAINER:-STACK}
160CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
161CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
162CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
163CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young03c1fa62011-10-20 11:35:14 -0700164CONTAINER_MAC=${CONTAINER_MAC:-"02:16:3e:07:69:`printf '%02X' $(echo $CONTAINER_IP | sed "s/.*\.//")`"}
Jesse Andrews6960eff2011-10-20 14:57:18 -0700165CONTAINER_RAM=${CONTAINER_RAM:-1524288}
166CONTAINER_CORES=${CONTAINER_CORES:-1}
Anthony Young1b7a42e2011-10-19 02:34:06 -0700167
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700168# libvirt.xml configuration
Jesse Andrewsec1d0312011-10-21 19:22:55 -0700169LIBVIRT_XML=$VM_DIR/libvirt.xml
Anthony Young1b7a42e2011-10-19 02:34:06 -0700170cat > $LIBVIRT_XML <<EOF
171<domain type='kvm'>
Anthony Young03c1fa62011-10-20 11:35:14 -0700172 <name>$CONTAINER_NAME</name>
Jesse Andrews6960eff2011-10-20 14:57:18 -0700173 <memory>$CONTAINER_RAM</memory>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700174 <os>
Anthony Youngd51812d2011-10-19 20:09:43 -0700175 <type>hvm</type>
176 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700177 </os>
178 <features>
179 <acpi/>
180 </features>
Jesse Andrews6960eff2011-10-20 14:57:18 -0700181 <vcpu>$CONTAINER_CORES</vcpu>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700182 <devices>
183 <disk type='file'>
184 <driver type='qcow2'/>
185 <source file='$VM_DIR/disk'/>
186 <target dev='vda' bus='virtio'/>
187 </disk>
188
189 <interface type='bridge'>
190 <source bridge='$BRIDGE'/>
191 <mac address='$CONTAINER_MAC'/>
192 </interface>
193
194 <!-- The order is significant here. File must be defined first -->
195 <serial type="file">
196 <source path='$VM_DIR/console.log'/>
197 <target port='1'/>
198 </serial>
199
200 <console type='pty' tty='/dev/pts/2'>
201 <source path='/dev/pts/2'/>
202 <target port='0'/>
203 </console>
204
205 <serial type='pty'>
206 <source path='/dev/pts/2'/>
207 <target port='0'/>
208 </serial>
209
210 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
211 </devices>
212</domain>
213EOF
214
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700215# Mount point for instance fs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700216ROOTFS=$VM_DIR/root
217mkdir -p $ROOTFS
218
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700219# Make sure we have nbd-ness
Anthony Young1b7a42e2011-10-19 02:34:06 -0700220modprobe nbd max_part=63
221
Anthony Young9c0fdd72011-10-19 20:22:32 -0700222# Which NBD device to use?
223NBD=${NBD:-/dev/nbd5}
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'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700227qemu-nbd -d $NBD || echo 'ok'
Anthony Young1b7a42e2011-10-19 02:34:06 -0700228
Anthony Youngbabb2e02011-10-20 12:32:58 -0700229# Clean up old runs
230cd $VM_DIR
231rm -f $VM_DIR/disk
232
233# Create our instance fs
Dean Troyerd0332912011-10-21 14:58:44 -0500234qemu-img create -f qcow2 -b $VM_IMAGE disk
Anthony Youngbabb2e02011-10-20 12:32:58 -0700235
Anthony Young69937462011-10-20 12:55:46 -0700236sleep 5
237
Anthony Young9c0fdd72011-10-19 20:22:32 -0700238qemu-nbd -c $NBD disk
Anthony Young69937462011-10-20 12:55:46 -0700239
240sleep 5
241
242# Mount the instance
Anthony Young9c0fdd72011-10-19 20:22:32 -0700243mount $NBD $ROOTFS -o offset=32256 -t ext4
Anthony Young1b7a42e2011-10-19 02:34:06 -0700244
245# Configure instance network
246INTERFACES=$ROOTFS/etc/network/interfaces
247cat > $INTERFACES <<EOF
248auto lo
249iface lo inet loopback
250
251auto eth0
252iface eth0 inet static
253 address $CONTAINER_IP
254 netmask $CONTAINER_NETMASK
255 gateway $CONTAINER_GATEWAY
256EOF
257
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700258# User configuration for the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700259chroot $ROOTFS groupadd libvirtd
260chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
261cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
262echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700263echo "stack:pass" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700264echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
265
266# Gracefully cp only if source file/dir exists
267function cp_it {
268 if [ -e $1 ] || [ -d $1 ]; then
269 cp -pRL $1 $2
270 fi
271}
272
273# Copy over your ssh keys and env if desired
274COPYENV=${COPYENV:-1}
275if [ "$COPYENV" = "1" ]; then
276 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
277 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
278 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
279 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
280 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
281fi
282
283# Configure the runner
284RUN_SH=$ROOTFS/$DEST/run.sh
285cat > $RUN_SH <<EOF
286#!/usr/bin/env bash
Anthony Young1b7a42e2011-10-19 02:34:06 -0700287
288# Kill any existing screens
289killall screen
290
291# Install and run stack.sh
292sudo apt-get update
293sudo apt-get -y --force-yes install git-core vim-nox sudo
294if [ ! -d "$DEST/devstack" ]; then
295 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
296fi
297cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
298echo >> /$DEST/run.sh.log
299echo >> /$DEST/run.sh.log
300echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Youngd51812d2011-10-19 20:09:43 -0700301cat $DEST/run.sh.log
Anthony Young1b7a42e2011-10-19 02:34:06 -0700302EOF
Anthony Young1b7a42e2011-10-19 02:34:06 -0700303chmod 755 $RUN_SH
304
305# Make runner launch on boot
306RC_LOCAL=$ROOTFS/etc/init.d/local
307cat > $RC_LOCAL <<EOF
308#!/bin/sh -e
Dean Troyerad57a3a2011-10-21 14:29:30 -0500309# Reboot if this is our first run to enable console log on $DIST_NAME :(
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700310if [ ! -e /root/firstlaunch ]; then
311 touch /root/firstlaunch
Anthony Youngd51812d2011-10-19 20:09:43 -0700312 reboot -f
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700313 exit 0
314fi
Anthony Young1b7a42e2011-10-19 02:34:06 -0700315su -c "$DEST/run.sh" stack
316EOF
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700317chmod +x $RC_LOCAL
318chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young1b7a42e2011-10-19 02:34:06 -0700319
320# Make our ip address hostnames look nice at the command prompt
321echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
322echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
323
324# Give stack ownership over $DEST so it may do the work needed
325chroot $ROOTFS chown -R stack $DEST
326
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700327# Change boot params so that we get a console log
Anthony Youngf6f52272011-10-19 02:58:18 -0700328sudo 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 -0700329sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
330#chroot $ROOTFS grub-install /dev/vda
Anthony Youngf6f52272011-10-19 02:58:18 -0700331
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700332# Unmount
333umount $ROOTFS || echo 'ok'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700334qemu-nbd -d $NBD
Anthony Young1b7a42e2011-10-19 02:34:06 -0700335
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700336# Create the instance
337cd $VM_DIR && virsh create libvirt.xml
338
339# Tail the console log till we are done
Anthony Youngd51812d2011-10-19 20:09:43 -0700340WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700341if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
342 # Done creating the container, let's tail the log
343 echo
344 echo "============================================================="
345 echo " -- YAY! --"
346 echo "============================================================="
347 echo
348 echo "We're done launching the vm, about to start tailing the"
349 echo "stack.sh log. It will take a second or two to start."
350 echo
351 echo "Just CTRL-C at any time to stop tailing."
352
353 while [ ! -e "$VM_DIR/console.log" ]; do
354 sleep 1
355 done
356
357 tail -F $VM_DIR/console.log &
358
359 TAIL_PID=$!
360
361 function kill_tail() {
362 kill $TAIL_PID
363 exit 1
364 }
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700365
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700366 # Let Ctrl-c kill tail and exit
367 trap kill_tail SIGINT
368
369 echo "Waiting stack.sh to finish..."
Anthony Youngd51812d2011-10-19 20:09:43 -0700370 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700371 sleep 5
372 done
373
374 kill $TAIL_PID
375 echo ""
376 echo "Finished - Zip-a-dee Doo-dah!"
377fi