blob: 019b1bdba8d7247448a77bc9f5a7ca309f4d0032 [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
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070048# Original version of built image
49BASE_IMAGE=$KVMSTACK_DIR/images/natty.raw
Anthony Young1b7a42e2011-10-19 02:34:06 -070050
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070051# Copy of base image, which we pre-install with tasty treats
52BASE_IMAGE_COPY=$IMAGES_DIR/$DIST_NAME.raw.copy
53
54# Name of our instance, used by libvirt
Anthony Young1b7a42e2011-10-19 02:34:06 -070055VM_NAME=${VM_NAME:-kvmstack}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070056
57# Mop up after previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -070058virsh destroy $VM_NAME
59
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070060# Where this vm is stored
61VM_DIR=$KVMSTACK_DIR/instances/$VM_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
73 ./make_image.sh -m -r 5000 natty raw
74 mv natty.raw $BASE_IMAGE
75 cd $TOP_DIR
76fi
77
Anthony Youngfa4b5eb2011-10-19 11:27:02 -070078# Create a copy of the base image
79if [ ! -e $BASE_IMAGE_COPY ]; then
80 cp -p $BASE_IMAGE $BASE_IMAGE_COPY
81fi
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
111mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR
112
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# Back to devstack
152cd $TOP_DIR
153
154# Unmount the filesystems
Anthony Young1b7a42e2011-10-19 02:34:06 -0700155unmount_images
156
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700157# Clean up old runs
158cd $VM_DIR
Anthony Young1b7a42e2011-10-19 02:34:06 -0700159rm -f $VM_DIR/disk
160
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700161# Clean up old instance data
Anthony Young1b7a42e2011-10-19 02:34:06 -0700162qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
163
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700164# Network configuration variables
Anthony Young1b7a42e2011-10-19 02:34:06 -0700165BRIDGE=${BRIDGE:-br0}
166CONTAINER=${CONTAINER:-STACK}
167CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
168CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
169CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
170CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
171CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
172
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700173# libvirt.xml configuration
Anthony Young1b7a42e2011-10-19 02:34:06 -0700174LIBVIRT_XML=libvirt.xml
175cat > $LIBVIRT_XML <<EOF
176<domain type='kvm'>
177 <name>$VM_NAME</name>
178 <memory>1524288</memory>
179 <os>
Anthony Youngd51812d2011-10-19 20:09:43 -0700180 <type>hvm</type>
181 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700182 </os>
183 <features>
184 <acpi/>
185 </features>
186 <vcpu>1</vcpu>
187 <devices>
188 <disk type='file'>
189 <driver type='qcow2'/>
190 <source file='$VM_DIR/disk'/>
191 <target dev='vda' bus='virtio'/>
192 </disk>
193
194 <interface type='bridge'>
195 <source bridge='$BRIDGE'/>
196 <mac address='$CONTAINER_MAC'/>
197 </interface>
198
199 <!-- The order is significant here. File must be defined first -->
200 <serial type="file">
201 <source path='$VM_DIR/console.log'/>
202 <target port='1'/>
203 </serial>
204
205 <console type='pty' tty='/dev/pts/2'>
206 <source path='/dev/pts/2'/>
207 <target port='0'/>
208 </console>
209
210 <serial type='pty'>
211 <source path='/dev/pts/2'/>
212 <target port='0'/>
213 </serial>
214
215 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
216 </devices>
217</domain>
218EOF
219
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700220# Mount point for instance fs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700221ROOTFS=$VM_DIR/root
222mkdir -p $ROOTFS
223
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700224# Make sure we have nbd-ness
Anthony Young1b7a42e2011-10-19 02:34:06 -0700225modprobe nbd max_part=63
226
Anthony Young9c0fdd72011-10-19 20:22:32 -0700227# Which NBD device to use?
228NBD=${NBD:-/dev/nbd5}
229
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700230# Clean up from previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700231umount $ROOTFS || echo 'ok'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700232qemu-nbd -d $NBD || echo 'ok'
Anthony Young1b7a42e2011-10-19 02:34:06 -0700233
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700234# Mount the instance
Anthony Young9c0fdd72011-10-19 20:22:32 -0700235qemu-nbd -c $NBD disk
236mount $NBD $ROOTFS -o offset=32256 -t ext4
Anthony Young1b7a42e2011-10-19 02:34:06 -0700237
238# Configure instance network
239INTERFACES=$ROOTFS/etc/network/interfaces
240cat > $INTERFACES <<EOF
241auto lo
242iface lo inet loopback
243
244auto eth0
245iface eth0 inet static
246 address $CONTAINER_IP
247 netmask $CONTAINER_NETMASK
248 gateway $CONTAINER_GATEWAY
249EOF
250
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700251# User configuration for the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700252chroot $ROOTFS groupadd libvirtd
253chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
254cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
255echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700256echo "stack:pass" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700257echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
258
259# Gracefully cp only if source file/dir exists
260function cp_it {
261 if [ -e $1 ] || [ -d $1 ]; then
262 cp -pRL $1 $2
263 fi
264}
265
266# Copy over your ssh keys and env if desired
267COPYENV=${COPYENV:-1}
268if [ "$COPYENV" = "1" ]; then
269 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
270 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
271 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
272 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
273 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
274fi
275
276# Configure the runner
277RUN_SH=$ROOTFS/$DEST/run.sh
278cat > $RUN_SH <<EOF
279#!/usr/bin/env bash
Anthony Young1b7a42e2011-10-19 02:34:06 -0700280
281# Kill any existing screens
282killall screen
283
284# Install and run stack.sh
285sudo apt-get update
286sudo apt-get -y --force-yes install git-core vim-nox sudo
287if [ ! -d "$DEST/devstack" ]; then
288 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
289fi
290cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
291echo >> /$DEST/run.sh.log
292echo >> /$DEST/run.sh.log
293echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Youngd51812d2011-10-19 20:09:43 -0700294cat $DEST/run.sh.log
Anthony Young1b7a42e2011-10-19 02:34:06 -0700295EOF
Anthony Young1b7a42e2011-10-19 02:34:06 -0700296chmod 755 $RUN_SH
297
298# Make runner launch on boot
299RC_LOCAL=$ROOTFS/etc/init.d/local
300cat > $RC_LOCAL <<EOF
301#!/bin/sh -e
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700302# Reboot if this is our first run to enable console log on natty :(
303if [ ! -e /root/firstlaunch ]; then
304 touch /root/firstlaunch
Anthony Youngd51812d2011-10-19 20:09:43 -0700305 reboot -f
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700306 exit 0
307fi
Anthony Young1b7a42e2011-10-19 02:34:06 -0700308su -c "$DEST/run.sh" stack
309EOF
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700310chmod +x $RC_LOCAL
311chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young1b7a42e2011-10-19 02:34:06 -0700312
313# Make our ip address hostnames look nice at the command prompt
314echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
315echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
316
317# Give stack ownership over $DEST so it may do the work needed
318chroot $ROOTFS chown -R stack $DEST
319
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700320# Change boot params so that we get a console log
Anthony Youngf6f52272011-10-19 02:58:18 -0700321sudo 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 -0700322sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
323#chroot $ROOTFS grub-install /dev/vda
Anthony Youngf6f52272011-10-19 02:58:18 -0700324
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700325# Unmount
326umount $ROOTFS || echo 'ok'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700327qemu-nbd -d $NBD
Anthony Young1b7a42e2011-10-19 02:34:06 -0700328
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700329# Create the instance
330cd $VM_DIR && virsh create libvirt.xml
331
332# Tail the console log till we are done
Anthony Youngd51812d2011-10-19 20:09:43 -0700333WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700334if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
335 # Done creating the container, let's tail the log
336 echo
337 echo "============================================================="
338 echo " -- YAY! --"
339 echo "============================================================="
340 echo
341 echo "We're done launching the vm, about to start tailing the"
342 echo "stack.sh log. It will take a second or two to start."
343 echo
344 echo "Just CTRL-C at any time to stop tailing."
345
346 while [ ! -e "$VM_DIR/console.log" ]; do
347 sleep 1
348 done
349
350 tail -F $VM_DIR/console.log &
351
352 TAIL_PID=$!
353
354 function kill_tail() {
355 kill $TAIL_PID
356 exit 1
357 }
358
359 # Let Ctrl-c kill tail and exit
360 trap kill_tail SIGINT
361
362 echo "Waiting stack.sh to finish..."
Anthony Youngd51812d2011-10-19 20:09:43 -0700363 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700364 sleep 5
365 done
366
367 kill $TAIL_PID
368 echo ""
369 echo "Finished - Zip-a-dee Doo-dah!"
370fi