blob: 560e98fee4e85052d94f4ce9fbad696f4a361854 [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
232# Make sure we have nbd-ness
233modprobe nbd max_part=63
234
235# Which NBD device to use?
236NBD=${NBD:-/dev/nbd5}
237
238# Clean up from previous runs
239umount $ROOTFS || echo 'ok'
240qemu-nbd -d $NBD || echo 'ok'
241
242# Clean up old runs
243cd $VM_DIR
244rm -f $VM_DIR/disk
245
246# Create our instance fs
247qemu-img create -f qcow2 -b $VM_IMAGE disk
248
249# Connect our nbd and wait till it is mountable
250qemu-nbd -c $NBD disk
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700251if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700252 echo "Couldn't connect $NBD"
253 exit 1
254fi
255
256# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700257mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700258
259# Configure instance network
260INTERFACES=$ROOTFS/etc/network/interfaces
261cat > $INTERFACES <<EOF
262auto lo
263iface lo inet loopback
264
265auto eth0
266iface eth0 inet static
267 address $GUEST_IP
268 netmask $GUEST_NETMASK
269 gateway $GUEST_GATEWAY
270EOF
271
272# User configuration for the instance
273chroot $ROOTFS groupadd libvirtd || true
274chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrewsb0559b22011-10-30 19:46:54 -0700275cp -pr $TOPDIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700276echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
277echo "stack:pass" | chroot $ROOTFS chpasswd
278echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
279
280# Gracefully cp only if source file/dir exists
281function cp_it {
282 if [ -e $1 ] || [ -d $1 ]; then
283 cp -pRL $1 $2
284 fi
285}
286
287# Copy over your ssh keys and env if desired
288COPYENV=${COPYENV:-1}
289if [ "$COPYENV" = "1" ]; then
290 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
291 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
292 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
293 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
294 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
295fi
296
297# pre-cache uec images
298for image_url in ${IMAGE_URLS//,/ }; do
299 IMAGE_FNAME=`basename "$image_url"`
300 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
301 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
302 fi
303 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
304done
305
306# Configure the runner
307RUN_SH=$ROOTFS/$DEST/run.sh
308cat > $RUN_SH <<EOF
309#!/usr/bin/env bash
310
311# Kill any existing screens
312killall screen
313
314# Install and run stack.sh
315sudo apt-get update
316sudo apt-get -y --force-yes install git-core vim-nox sudo
317if [ ! -d "$DEST/devstack" ]; then
318 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
319fi
320cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
321echo >> /$DEST/run.sh.log
322echo >> /$DEST/run.sh.log
323echo "All done! Time to start clicking." >> /$DEST/run.sh.log
324cat $DEST/run.sh.log
325EOF
326chmod 755 $RUN_SH
327
328# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700329RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700330cat > $RC_LOCAL <<EOF
331#!/bin/sh -e
332# Reboot if this is our first run to enable console log on $DIST_NAME :(
333if [ ! -e /root/firstlaunch ]; then
334 touch /root/firstlaunch
335 reboot -f
336 exit 0
337fi
338su -c "$DEST/run.sh" stack
339EOF
340chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700341chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700342
343# Make our ip address hostnames look nice at the command prompt
344echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
345echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
346
347# Give stack ownership over $DEST so it may do the work needed
348chroot $ROOTFS chown -R stack $DEST
349
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700350# GRUB 2 wants to see /dev
351mount -o bind /dev $ROOTFS/dev
352
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700353# Change boot params so that we get a console log
354sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
355sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
356
357# Set the hostname
358echo $GUEST_NAME > $ROOTFS/etc/hostname
359
360# We need the hostname to resolve for rabbit to launch
361if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
362 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
363fi
364
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700365# Change boot params so that we get a console log
366G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
367sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
368sed -e 's/GRUB_CMDLINE_LINUX_DEFAULT=.*$/GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 console=tty0 ds=nocloud ubuntu-pass=pass"/' -i $ROOTFS/etc/default/grub
369sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
370echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
371echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
372echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
373
374chroot $ROOTFS update-grub
375umount $ROOTFS/dev
376
377# Pre-generate ssh host keys and allow password login
378chroot $ROOTFS dpkg-reconfigure openssh-server
379sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
380
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700381# Unmount
382umount $ROOTFS || echo 'ok'
383qemu-nbd -d $NBD
384
385# Create the instance
386cd $VM_DIR && virsh create libvirt.xml
387
388# Tail the console log till we are done
389WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
390if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
391 # Done creating the container, let's tail the log
392 echo
393 echo "============================================================="
394 echo " -- YAY! --"
395 echo "============================================================="
396 echo
397 echo "We're done launching the vm, about to start tailing the"
398 echo "stack.sh log. It will take a second or two to start."
399 echo
400 echo "Just CTRL-C at any time to stop tailing."
401
402 while [ ! -e "$VM_DIR/console.log" ]; do
403 sleep 1
404 done
405
406 tail -F $VM_DIR/console.log &
407
408 TAIL_PID=$!
409
410 function kill_tail() {
411 kill $TAIL_PID
412 exit 1
413 }
414
415 # Let Ctrl-c kill tail and exit
416 trap kill_tail SIGINT
417
418 echo "Waiting stack.sh to finish..."
419 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
420 sleep 5
421 done
422
423 kill $TAIL_PID
424
425 if grep -q "stack.sh failed" $VM_DIR/console.log; then
426 exit 1
427 fi
428 echo ""
429 echo "Finished - Zip-a-dee Doo-dah!"
430fi