blob: 4cca5f9f7f35c0d8e15c9f9997523857ca767d35 [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 Young03c1fa62011-10-20 11:35:14 -070055CONTAINER_NAME=${CONTAINER_NAME:-kvmstack}
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
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# 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/.*\.//")`"}
Anthony Young1b7a42e2011-10-19 02:34:06 -0700165
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700166# libvirt.xml configuration
Anthony Young1b7a42e2011-10-19 02:34:06 -0700167LIBVIRT_XML=libvirt.xml
168cat > $LIBVIRT_XML <<EOF
169<domain type='kvm'>
Anthony Young03c1fa62011-10-20 11:35:14 -0700170 <name>$CONTAINER_NAME</name>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700171 <memory>1524288</memory>
172 <os>
Anthony Youngd51812d2011-10-19 20:09:43 -0700173 <type>hvm</type>
174 <bootmenu enable='yes'/>
Anthony Young1b7a42e2011-10-19 02:34:06 -0700175 </os>
176 <features>
177 <acpi/>
178 </features>
179 <vcpu>1</vcpu>
180 <devices>
181 <disk type='file'>
182 <driver type='qcow2'/>
183 <source file='$VM_DIR/disk'/>
184 <target dev='vda' bus='virtio'/>
185 </disk>
186
187 <interface type='bridge'>
188 <source bridge='$BRIDGE'/>
189 <mac address='$CONTAINER_MAC'/>
190 </interface>
191
192 <!-- The order is significant here. File must be defined first -->
193 <serial type="file">
194 <source path='$VM_DIR/console.log'/>
195 <target port='1'/>
196 </serial>
197
198 <console type='pty' tty='/dev/pts/2'>
199 <source path='/dev/pts/2'/>
200 <target port='0'/>
201 </console>
202
203 <serial type='pty'>
204 <source path='/dev/pts/2'/>
205 <target port='0'/>
206 </serial>
207
208 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
209 </devices>
210</domain>
211EOF
212
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700213# Mount point for instance fs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700214ROOTFS=$VM_DIR/root
215mkdir -p $ROOTFS
216
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700217# Make sure we have nbd-ness
Anthony Young1b7a42e2011-10-19 02:34:06 -0700218modprobe nbd max_part=63
219
Anthony Young9c0fdd72011-10-19 20:22:32 -0700220# Which NBD device to use?
221NBD=${NBD:-/dev/nbd5}
222
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700223# Clean up from previous runs
Anthony Young1b7a42e2011-10-19 02:34:06 -0700224umount $ROOTFS || echo 'ok'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700225qemu-nbd -d $NBD || echo 'ok'
Anthony Young1b7a42e2011-10-19 02:34:06 -0700226
Anthony Youngbabb2e02011-10-20 12:32:58 -0700227# Clean up old runs
228cd $VM_DIR
229rm -f $VM_DIR/disk
230
231# Create our instance fs
232qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
233
Anthony Young69937462011-10-20 12:55:46 -0700234sleep 5
235
Anthony Young9c0fdd72011-10-19 20:22:32 -0700236qemu-nbd -c $NBD disk
Anthony Young69937462011-10-20 12:55:46 -0700237
238sleep 5
239
240# Mount the instance
Anthony Young9c0fdd72011-10-19 20:22:32 -0700241mount $NBD $ROOTFS -o offset=32256 -t ext4
Anthony Young1b7a42e2011-10-19 02:34:06 -0700242
243# Configure instance network
244INTERFACES=$ROOTFS/etc/network/interfaces
245cat > $INTERFACES <<EOF
246auto lo
247iface lo inet loopback
248
249auto eth0
250iface eth0 inet static
251 address $CONTAINER_IP
252 netmask $CONTAINER_NETMASK
253 gateway $CONTAINER_GATEWAY
254EOF
255
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700256# User configuration for the instance
Anthony Young1b7a42e2011-10-19 02:34:06 -0700257chroot $ROOTFS groupadd libvirtd
258chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
259cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
260echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700261echo "stack:pass" | chroot $ROOTFS chpasswd
Anthony Young1b7a42e2011-10-19 02:34:06 -0700262echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
263
264# Gracefully cp only if source file/dir exists
265function cp_it {
266 if [ -e $1 ] || [ -d $1 ]; then
267 cp -pRL $1 $2
268 fi
269}
270
271# Copy over your ssh keys and env if desired
272COPYENV=${COPYENV:-1}
273if [ "$COPYENV" = "1" ]; then
274 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
275 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
276 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
277 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
278 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
279fi
280
281# Configure the runner
282RUN_SH=$ROOTFS/$DEST/run.sh
283cat > $RUN_SH <<EOF
284#!/usr/bin/env bash
Anthony Young1b7a42e2011-10-19 02:34:06 -0700285
286# Kill any existing screens
287killall screen
288
289# Install and run stack.sh
290sudo apt-get update
291sudo apt-get -y --force-yes install git-core vim-nox sudo
292if [ ! -d "$DEST/devstack" ]; then
293 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
294fi
295cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
296echo >> /$DEST/run.sh.log
297echo >> /$DEST/run.sh.log
298echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Youngd51812d2011-10-19 20:09:43 -0700299cat $DEST/run.sh.log
Anthony Young1b7a42e2011-10-19 02:34:06 -0700300EOF
Anthony Young1b7a42e2011-10-19 02:34:06 -0700301chmod 755 $RUN_SH
302
303# Make runner launch on boot
304RC_LOCAL=$ROOTFS/etc/init.d/local
305cat > $RC_LOCAL <<EOF
306#!/bin/sh -e
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700307# Reboot if this is our first run to enable console log on natty :(
308if [ ! -e /root/firstlaunch ]; then
309 touch /root/firstlaunch
Anthony Youngd51812d2011-10-19 20:09:43 -0700310 reboot -f
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700311 exit 0
312fi
Anthony Young1b7a42e2011-10-19 02:34:06 -0700313su -c "$DEST/run.sh" stack
314EOF
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700315chmod +x $RC_LOCAL
316chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young1b7a42e2011-10-19 02:34:06 -0700317
318# Make our ip address hostnames look nice at the command prompt
319echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
320echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
321
322# Give stack ownership over $DEST so it may do the work needed
323chroot $ROOTFS chown -R stack $DEST
324
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700325# Change boot params so that we get a console log
Anthony Youngf6f52272011-10-19 02:58:18 -0700326sudo 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 -0700327sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
328#chroot $ROOTFS grub-install /dev/vda
Anthony Youngf6f52272011-10-19 02:58:18 -0700329
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700330# Unmount
331umount $ROOTFS || echo 'ok'
Anthony Young9c0fdd72011-10-19 20:22:32 -0700332qemu-nbd -d $NBD
Anthony Young1b7a42e2011-10-19 02:34:06 -0700333
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700334# Create the instance
335cd $VM_DIR && virsh create libvirt.xml
336
337# Tail the console log till we are done
Anthony Youngd51812d2011-10-19 20:09:43 -0700338WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700339if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
340 # Done creating the container, let's tail the log
341 echo
342 echo "============================================================="
343 echo " -- YAY! --"
344 echo "============================================================="
345 echo
346 echo "We're done launching the vm, about to start tailing the"
347 echo "stack.sh log. It will take a second or two to start."
348 echo
349 echo "Just CTRL-C at any time to stop tailing."
350
351 while [ ! -e "$VM_DIR/console.log" ]; do
352 sleep 1
353 done
354
355 tail -F $VM_DIR/console.log &
356
357 TAIL_PID=$!
358
359 function kill_tail() {
360 kill $TAIL_PID
361 exit 1
362 }
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700363
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700364 # Let Ctrl-c kill tail and exit
365 trap kill_tail SIGINT
366
367 echo "Waiting stack.sh to finish..."
Anthony Youngd51812d2011-10-19 20:09:43 -0700368 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700369 sleep 5
370 done
371
372 kill $TAIL_PID
373 echo ""
374 echo "Finished - Zip-a-dee Doo-dah!"
375fi