blob: 187112a7a572f986a5a31f7aa963a549da63b665 [file] [log] [blame]
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -07001#!/bin/bash
Dean Troyer56119512011-10-11 19:39:34 -05002# build_ramdisk.sh - Build RAM disk images
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -07003
Dean Troyer2567c812011-11-01 12:36:59 -05004# exit on error to stop unexpected errors
5set -o errexit
6
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -07007if [ ! "$#" -eq "1" ]; then
Dean Troyer2567c812011-11-01 12:36:59 -05008 echo "$0 builds a gziped Ubuntu OpenStack install"
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -07009 echo "usage: $0 dest"
10 exit 1
11fi
12
Dean Troyer55c02732011-11-01 17:44:03 -050013# Clean up any resources that may be in use
14cleanup() {
15 set +o errexit
16
17 # Mop up temporary files
18 if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
19 umount $MNTDIR
20 rmdir $MNTDIR
21 fi
22 if [ -n "$DEV_FILE_TMP" -a -e "$DEV_FILE_TMP "]; then
23 rm -f $DEV_FILE_TMP
24 fi
25 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
26 rm -f $IMG_FILE_TMP
27 fi
28
29 # Release NBD devices
30 if [ -n "$NBD" ]; then
31 qemu-nbd -d $NBD
32 fi
33
34 # Kill ourselves to signal any calling process
35 trap 2; kill -2 $$
36}
37
38trap cleanup SIGHUP SIGINT SIGTERM
39
Dean Troyerdccd6b92011-11-01 15:46:14 -050040# Set up nbd
41modprobe nbd max_part=63
42
Dean Troyer2567c812011-11-01 12:36:59 -050043# Echo commands
44set -o xtrace
45
Dean Troyera6466e02011-10-25 17:53:24 -050046IMG_FILE=$1
47
Dean Troyer2567c812011-11-01 12:36:59 -050048# Keep track of the current directory
49TOOLS_DIR=$(cd $(dirname "$0") && pwd)
50TOP_DIR=`cd $TOOLS_DIR/..; pwd`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070051
Dean Troyer03412c82011-10-03 09:56:41 -050052# Store cwd
53CWD=`pwd`
54
Dean Troyer2567c812011-11-01 12:36:59 -050055cd $TOP_DIR
56
57# Source params
58source ./stackrc
59
60CACHEDIR=${CACHEDIR:-/var/cache/devstack}
61
Dean Troyer11e5e6f2011-10-03 09:40:32 -050062DEST=${DEST:-/opt/stack}
63
Dean Troyer2567c812011-11-01 12:36:59 -050064# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
65ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
66
67# Base image (natty by default)
68DIST_NAME=${DIST_NAME:-natty}
69
Dean Troyer8f851e72011-10-11 20:22:23 -050070# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
71STACKSH_PARAMS=${STACKSH_PARAMS:-}
72
Dean Troyera3379e02011-10-03 11:14:13 -050073# Option to use the version of devstack on which we are currently working
74USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
75
Dean Troyer2567c812011-11-01 12:36:59 -050076# clean install
77if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then
78 $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070079fi
80
Dean Troyerdccd6b92011-11-01 15:46:14 -050081# Finds the next available NBD device
82# Exits script if error connecting or none free
83# map_nbd image
84# returns full nbd device path
85function map_nbd {
86 for i in `seq 0 15`; do
87 if [ ! -e /sys/block/nbd$i/pid ]; then
88 NBD=/dev/nbd$i
89 # Connect to nbd and wait till it is ready
90 qemu-nbd -c $NBD $1
91 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
92 echo "Couldn't connect $NBD"
93 exit 1
94 fi
95 break
96 fi
97 done
98 if [ -z "$NBD" ]; then
99 echo "No free NBD slots"
Dean Troyera6466e02011-10-25 17:53:24 -0500100 exit 1
101 fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500102 echo $NBD
103}
104
105# prime image with as many apt/pips as we can
106DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
107DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
108if [ ! -r $DEV_FILE ]; then
109 cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
110
111 NBD=`map_nbd $DEV_FILE_TMP`
Dean Troyera6466e02011-10-25 17:53:24 -0500112 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
113 mount -t ext4 ${NBD}p1 $MNTDIR
114 cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
115
116 chroot $MNTDIR apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
117 chroot $MNTDIR pip install `cat files/pips/*`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700118
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700119 # Create a stack user that is a member of the libvirtd group so that stack
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700120 # is able to interact with libvirt.
Dean Troyera6466e02011-10-25 17:53:24 -0500121 chroot $MNTDIR groupadd libvirtd
122 chroot $MNTDIR useradd stack -s /bin/bash -d $DEST -G libvirtd
123 mkdir -p $MNTDIR/$DEST
124 chroot $MNTDIR chown stack $DEST
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700125
126 # a simple password - pass
Dean Troyerdccd6b92011-11-01 15:46:14 -0500127 echo stack:pass | chroot $MNTDIR chpasswd
128 echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700129
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700130 # and has sudo ability (in the future this should be limited to only what
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700131 # stack requires)
Dean Troyera6466e02011-10-25 17:53:24 -0500132 echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
133
134 umount $MNTDIR
135 rmdir $MNTDIR
136 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500137 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500138 mv $DEV_FILE_TMP $DEV_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700139fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500140rm -f $DEV_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700141
142# clone git repositories onto the system
143# ======================================
144
Dean Troyerdccd6b92011-11-01 15:46:14 -0500145IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
146
Dean Troyera6466e02011-10-25 17:53:24 -0500147if [ ! -r $IMG_FILE ]; then
Dean Troyerdccd6b92011-11-01 15:46:14 -0500148 NBD=`map_nbd $DEV_FILE`
Dean Troyer61be9242011-10-25 22:35:23 -0500149
150 # Pre-create the image file
151 # FIXME(dt): This should really get the partition size to
152 # pre-create the image file
Dean Troyerdccd6b92011-11-01 15:46:14 -0500153 dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
Dean Troyer61be9242011-10-25 22:35:23 -0500154 # Create filesystem image for RAM disk
Dean Troyerdccd6b92011-11-01 15:46:14 -0500155 dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
Dean Troyer61be9242011-10-25 22:35:23 -0500156
157 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500158 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500159 mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700160fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500161rm -f $IMG_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700162
Dean Troyera6466e02011-10-25 17:53:24 -0500163MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
Dean Troyer61be9242011-10-25 22:35:23 -0500164mount -t ext4 -o loop $IMG_FILE $MNTDIR
Dean Troyera6466e02011-10-25 17:53:24 -0500165cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
166
Dean Troyerea442c12011-10-26 12:34:59 -0500167# We need to install a non-virtual kernel and modules to boot from
Dean Troyer7920b0f2011-10-26 15:10:46 -0500168if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
Dean Troyerea442c12011-10-26 12:34:59 -0500169 chroot $MNTDIR apt-get install -y linux-generic
170fi
171
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700172# git clone only if directory doesn't exist already. Since ``DEST`` might not
173# be owned by the installation user, we create the directory and change the
174# ownership to the proper user.
175function git_clone {
176
177 # clone new copy or fetch latest changes
Dean Troyera6466e02011-10-25 17:53:24 -0500178 CHECKOUT=${MNTDIR}$2
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700179 if [ ! -d $CHECKOUT ]; then
180 mkdir -p $CHECKOUT
181 git clone $1 $CHECKOUT
182 else
183 pushd $CHECKOUT
184 git fetch
185 popd
186 fi
187
188 # FIXME(ja): checkout specified version (should works for branches and tags)
189
190 pushd $CHECKOUT
191 # checkout the proper branch/tag
192 git checkout $3
193 # force our local version to be the same as the remote version
194 git reset --hard origin/$3
195 popd
196
197 # give ownership to the stack user
Dean Troyera6466e02011-10-25 17:53:24 -0500198 chroot $MNTDIR chown -R stack $2
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700199}
200
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500201git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
202git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
203git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
204git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700205git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500206git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
207git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700208
Dean Troyerb5da5192011-10-17 13:32:06 -0500209# Use this version of devstack
Dean Troyera6466e02011-10-25 17:53:24 -0500210rm -rf $MNTDIR/$DEST/devstack
211cp -pr $CWD $MNTDIR/$DEST/devstack
212chroot $MNTDIR chown -R stack $DEST/devstack
Dean Troyer03412c82011-10-03 09:56:41 -0500213
Dean Troyercf9db8d2011-10-03 13:42:16 -0500214# Configure host network for DHCP
Dean Troyera6466e02011-10-25 17:53:24 -0500215mkdir -p $MNTDIR/etc/network
216cat > $MNTDIR/etc/network/interfaces <<EOF
Dean Troyercf9db8d2011-10-03 13:42:16 -0500217auto lo
218iface lo inet loopback
219
220auto eth0
221iface eth0 inet dhcp
222EOF
223
Dean Troyer288f3bd2011-10-13 15:50:44 -0500224# Set hostname
Dean Troyera6466e02011-10-25 17:53:24 -0500225echo "ramstack" >$MNTDIR/etc/hostname
226echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts
Dean Troyer288f3bd2011-10-13 15:50:44 -0500227
Dean Troyer8f851e72011-10-11 20:22:23 -0500228# Configure the runner
Dean Troyera6466e02011-10-25 17:53:24 -0500229RUN_SH=$MNTDIR/$DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500230cat > $RUN_SH <<EOF
231#!/usr/bin/env bash
232
Dean Troyer7c076ee2011-10-13 13:20:13 -0500233# Get IP range
234set \`ip addr show dev eth0 | grep inet\`
235PREFIX=\`echo \$2 | cut -d. -f1,2,3\`
236export FLOATING_RANGE="\$PREFIX.224/27"
237
Dean Troyer8f851e72011-10-11 20:22:23 -0500238# Kill any existing screens
239killall screen
240
241# Run stack.sh
242cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log
243echo >> $DEST/run.sh.log
244echo >> $DEST/run.sh.log
245echo "All done! Time to start clicking." >> $DEST/run.sh.log
246EOF
247
248# Make the run.sh executable
249chmod 755 $RUN_SH
Dean Troyera6466e02011-10-25 17:53:24 -0500250chroot $MNTDIR chown stack $DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500251
Dean Troyera6466e02011-10-25 17:53:24 -0500252umount $MNTDIR
253rmdir $MNTDIR