blob: 16259b1f208290beebe05063a3f8a4f697078b8e [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 Troyerdccd6b92011-11-01 15:46:14 -0500242# Finds the next available NBD device
243# Exits script if error connecting or none free
244# map_nbd image
245# returns full nbd device path
246function map_nbd {
247 for i in `seq 0 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 $1
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
258 done
259 if [ -z "$NBD" ]; then
260 echo "No free NBD slots"
261 exit 1
262 fi
263 echo $NBD
264}
265
Dean Troyer7a569732011-10-31 17:34:29 -0500266# Make sure we have nbd-ness
267modprobe nbd max_part=63
268
269# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500270NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500271NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700272
273# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700274mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700275
276# Configure instance network
277INTERFACES=$ROOTFS/etc/network/interfaces
278cat > $INTERFACES <<EOF
279auto lo
280iface lo inet loopback
281
282auto eth0
283iface eth0 inet static
284 address $GUEST_IP
285 netmask $GUEST_NETMASK
286 gateway $GUEST_GATEWAY
287EOF
288
289# User configuration for the instance
290chroot $ROOTFS groupadd libvirtd || true
291chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700292cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700293echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
294echo "stack:pass" | chroot $ROOTFS chpasswd
295echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
296
297# Gracefully cp only if source file/dir exists
298function cp_it {
299 if [ -e $1 ] || [ -d $1 ]; then
300 cp -pRL $1 $2
301 fi
302}
303
304# Copy over your ssh keys and env if desired
305COPYENV=${COPYENV:-1}
306if [ "$COPYENV" = "1" ]; then
307 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
308 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
309 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
310 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
311 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
312fi
313
314# pre-cache uec images
315for image_url in ${IMAGE_URLS//,/ }; do
316 IMAGE_FNAME=`basename "$image_url"`
317 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
318 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
319 fi
320 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
321done
322
323# Configure the runner
324RUN_SH=$ROOTFS/$DEST/run.sh
325cat > $RUN_SH <<EOF
326#!/usr/bin/env bash
327
328# Kill any existing screens
329killall screen
330
331# Install and run stack.sh
332sudo apt-get update
333sudo apt-get -y --force-yes install git-core vim-nox sudo
334if [ ! -d "$DEST/devstack" ]; then
335 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
336fi
337cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
338echo >> /$DEST/run.sh.log
339echo >> /$DEST/run.sh.log
340echo "All done! Time to start clicking." >> /$DEST/run.sh.log
341cat $DEST/run.sh.log
342EOF
343chmod 755 $RUN_SH
344
345# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700346RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700347cat > $RC_LOCAL <<EOF
348#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700349# cloud-init overwrites the hostname with ubuntuhost
350echo $GUEST_NAME > /etc/hostname
351hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700352su -c "$DEST/run.sh" stack
353EOF
354chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700355chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700356
357# Make our ip address hostnames look nice at the command prompt
358echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
359echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
360
361# Give stack ownership over $DEST so it may do the work needed
362chroot $ROOTFS chown -R stack $DEST
363
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700364# Set the hostname
365echo $GUEST_NAME > $ROOTFS/etc/hostname
366
367# We need the hostname to resolve for rabbit to launch
368if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
369 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
370fi
371
Dean Troyer6fe687b2011-10-31 20:30:04 -0500372# GRUB 2 wants to see /dev
373mount -o bind /dev $ROOTFS/dev
374
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700375# Change boot params so that we get a console log
376G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
377sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700378sed -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 -0700379sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
380echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
381echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
382echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
383
384chroot $ROOTFS update-grub
385umount $ROOTFS/dev
386
387# Pre-generate ssh host keys and allow password login
388chroot $ROOTFS dpkg-reconfigure openssh-server
389sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
390
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700391# Unmount
392umount $ROOTFS || echo 'ok'
393qemu-nbd -d $NBD
394
395# Create the instance
396cd $VM_DIR && virsh create libvirt.xml
397
398# Tail the console log till we are done
399WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
400if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
401 # Done creating the container, let's tail the log
402 echo
403 echo "============================================================="
404 echo " -- YAY! --"
405 echo "============================================================="
406 echo
407 echo "We're done launching the vm, about to start tailing the"
408 echo "stack.sh log. It will take a second or two to start."
409 echo
410 echo "Just CTRL-C at any time to stop tailing."
411
412 while [ ! -e "$VM_DIR/console.log" ]; do
413 sleep 1
414 done
415
Jesse Andrews16341962011-10-31 00:21:56 -0700416 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700417
418 TAIL_PID=$!
419
420 function kill_tail() {
421 kill $TAIL_PID
422 exit 1
423 }
424
425 # Let Ctrl-c kill tail and exit
426 trap kill_tail SIGINT
427
Jesse Andrews16341962011-10-31 00:21:56 -0700428 set +o xtrace
429
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700430 echo "Waiting stack.sh to finish..."
431 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700432 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700433 done
434
Jesse Andrews16341962011-10-31 00:21:56 -0700435 set -o xtrace
436
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700437 kill $TAIL_PID
438
Jesse Andrewsa06ac1c2011-10-31 22:29:23 -0700439 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700440 exit 1
441 fi
442 echo ""
443 echo "Finished - Zip-a-dee Doo-dah!"
444fi