blob: a7ed41153a4a789dbbc54f946174e0c3f1a0f9c6 [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
246modprobe nbd max_part=63
247for i in `seq 1 15`; do
248 if [ ! -e /sys/block/nbd$i/pid ]; then
249 NBD=/dev/nbd$i
250 # Connect to nbd and wait till it is ready
251 qemu-nbd -c $NBD disk
252 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
253 echo "Couldn't connect $NBD"
254 exit 1
255 fi
256 break
257 fi
258done
259if [ -z "$NBD" ]; then
260 echo "No free NBD slots"
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700261 exit 1
262fi
Dean Troyer7a569732011-10-31 17:34:29 -0500263NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700264
265# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700266mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700267
268# Configure instance network
269INTERFACES=$ROOTFS/etc/network/interfaces
270cat > $INTERFACES <<EOF
271auto lo
272iface lo inet loopback
273
274auto eth0
275iface eth0 inet static
276 address $GUEST_IP
277 netmask $GUEST_NETMASK
278 gateway $GUEST_GATEWAY
279EOF
280
281# User configuration for the instance
282chroot $ROOTFS groupadd libvirtd || true
283chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700284cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700285echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
286echo "stack:pass" | chroot $ROOTFS chpasswd
287echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
288
289# Gracefully cp only if source file/dir exists
290function cp_it {
291 if [ -e $1 ] || [ -d $1 ]; then
292 cp -pRL $1 $2
293 fi
294}
295
296# Copy over your ssh keys and env if desired
297COPYENV=${COPYENV:-1}
298if [ "$COPYENV" = "1" ]; then
299 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
300 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
301 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
302 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
303 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
304fi
305
306# pre-cache uec images
307for image_url in ${IMAGE_URLS//,/ }; do
308 IMAGE_FNAME=`basename "$image_url"`
309 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
310 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
311 fi
312 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
313done
314
315# Configure the runner
316RUN_SH=$ROOTFS/$DEST/run.sh
317cat > $RUN_SH <<EOF
318#!/usr/bin/env bash
319
320# Kill any existing screens
321killall screen
322
323# Install and run stack.sh
324sudo apt-get update
325sudo apt-get -y --force-yes install git-core vim-nox sudo
326if [ ! -d "$DEST/devstack" ]; then
327 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
328fi
329cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
330echo >> /$DEST/run.sh.log
331echo >> /$DEST/run.sh.log
332echo "All done! Time to start clicking." >> /$DEST/run.sh.log
333cat $DEST/run.sh.log
334EOF
335chmod 755 $RUN_SH
336
337# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700338RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700339cat > $RC_LOCAL <<EOF
340#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700341# cloud-init overwrites the hostname with ubuntuhost
342echo $GUEST_NAME > /etc/hostname
343hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700344su -c "$DEST/run.sh" stack
345EOF
346chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700347chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700348
349# Make our ip address hostnames look nice at the command prompt
350echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
351echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
352
353# Give stack ownership over $DEST so it may do the work needed
354chroot $ROOTFS chown -R stack $DEST
355
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700356# GRUB 2 wants to see /dev
357mount -o bind /dev $ROOTFS/dev
358
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700359# Set the hostname
360echo $GUEST_NAME > $ROOTFS/etc/hostname
361
362# We need the hostname to resolve for rabbit to launch
363if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
364 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
365fi
366
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700367# Change boot params so that we get a console log
368G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
369sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700370sed -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 -0700371sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
372echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
373echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
374echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
375
376chroot $ROOTFS update-grub
377umount $ROOTFS/dev
378
379# Pre-generate ssh host keys and allow password login
380chroot $ROOTFS dpkg-reconfigure openssh-server
381sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
382
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700383# Unmount
384umount $ROOTFS || echo 'ok'
385qemu-nbd -d $NBD
386
387# Create the instance
388cd $VM_DIR && virsh create libvirt.xml
389
390# Tail the console log till we are done
391WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
392if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
393 # Done creating the container, let's tail the log
394 echo
395 echo "============================================================="
396 echo " -- YAY! --"
397 echo "============================================================="
398 echo
399 echo "We're done launching the vm, about to start tailing the"
400 echo "stack.sh log. It will take a second or two to start."
401 echo
402 echo "Just CTRL-C at any time to stop tailing."
403
404 while [ ! -e "$VM_DIR/console.log" ]; do
405 sleep 1
406 done
407
Jesse Andrews16341962011-10-31 00:21:56 -0700408 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700409
410 TAIL_PID=$!
411
412 function kill_tail() {
413 kill $TAIL_PID
414 exit 1
415 }
416
417 # Let Ctrl-c kill tail and exit
418 trap kill_tail SIGINT
419
Jesse Andrews16341962011-10-31 00:21:56 -0700420 set +o xtrace
421
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700422 echo "Waiting stack.sh to finish..."
423 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700424 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700425 done
426
Jesse Andrews16341962011-10-31 00:21:56 -0700427 set -o xtrace
428
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700429 kill $TAIL_PID
430
431 if grep -q "stack.sh failed" $VM_DIR/console.log; then
432 exit 1
433 fi
434 echo ""
435 echo "Finished - Zip-a-dee Doo-dah!"
436fi