blob: feaa8a97ad2ef29d29a3b3da24b5e6070fad5f29 [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
Jesse Andrewsd7326d22011-11-20 10:02:26 -080060CACHEDIR=${CACHEDIR:-/opt/stack/cache}
Dean Troyer2567c812011-11-01 12:36:59 -050061
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
Anthony Youngca2c0472011-11-03 16:29:32 -0700116 chroot $MNTDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
117 chroot $MNTDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
Dean Troyera6466e02011-10-25 17:53:24 -0500118 chroot $MNTDIR pip install `cat files/pips/*`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700119
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700120 # Create a stack user that is a member of the libvirtd group so that stack
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700121 # is able to interact with libvirt.
Dean Troyera6466e02011-10-25 17:53:24 -0500122 chroot $MNTDIR groupadd libvirtd
123 chroot $MNTDIR useradd stack -s /bin/bash -d $DEST -G libvirtd
124 mkdir -p $MNTDIR/$DEST
125 chroot $MNTDIR chown stack $DEST
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700126
127 # a simple password - pass
Dean Troyerdccd6b92011-11-01 15:46:14 -0500128 echo stack:pass | chroot $MNTDIR chpasswd
129 echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700130
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700131 # and has sudo ability (in the future this should be limited to only what
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700132 # stack requires)
Dean Troyera6466e02011-10-25 17:53:24 -0500133 echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
134
135 umount $MNTDIR
136 rmdir $MNTDIR
137 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500138 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500139 mv $DEV_FILE_TMP $DEV_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700140fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500141rm -f $DEV_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700142
143# clone git repositories onto the system
144# ======================================
145
Dean Troyerdccd6b92011-11-01 15:46:14 -0500146IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
147
Dean Troyera6466e02011-10-25 17:53:24 -0500148if [ ! -r $IMG_FILE ]; then
Dean Troyerdccd6b92011-11-01 15:46:14 -0500149 NBD=`map_nbd $DEV_FILE`
Dean Troyer61be9242011-10-25 22:35:23 -0500150
151 # Pre-create the image file
152 # FIXME(dt): This should really get the partition size to
153 # pre-create the image file
Dean Troyerdccd6b92011-11-01 15:46:14 -0500154 dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
Dean Troyer61be9242011-10-25 22:35:23 -0500155 # Create filesystem image for RAM disk
Dean Troyerdccd6b92011-11-01 15:46:14 -0500156 dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
Dean Troyer61be9242011-10-25 22:35:23 -0500157
158 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500159 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500160 mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700161fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500162rm -f $IMG_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700163
Dean Troyera6466e02011-10-25 17:53:24 -0500164MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
Dean Troyer61be9242011-10-25 22:35:23 -0500165mount -t ext4 -o loop $IMG_FILE $MNTDIR
Dean Troyera6466e02011-10-25 17:53:24 -0500166cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
167
Dean Troyerea442c12011-10-26 12:34:59 -0500168# We need to install a non-virtual kernel and modules to boot from
Dean Troyer7920b0f2011-10-26 15:10:46 -0500169if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
Dean Troyerea442c12011-10-26 12:34:59 -0500170 chroot $MNTDIR apt-get install -y linux-generic
171fi
172
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700173# git clone only if directory doesn't exist already. Since ``DEST`` might not
174# be owned by the installation user, we create the directory and change the
175# ownership to the proper user.
176function git_clone {
177
178 # clone new copy or fetch latest changes
Dean Troyera6466e02011-10-25 17:53:24 -0500179 CHECKOUT=${MNTDIR}$2
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700180 if [ ! -d $CHECKOUT ]; then
181 mkdir -p $CHECKOUT
182 git clone $1 $CHECKOUT
183 else
184 pushd $CHECKOUT
185 git fetch
186 popd
187 fi
188
189 # FIXME(ja): checkout specified version (should works for branches and tags)
190
191 pushd $CHECKOUT
192 # checkout the proper branch/tag
193 git checkout $3
194 # force our local version to be the same as the remote version
195 git reset --hard origin/$3
196 popd
197
198 # give ownership to the stack user
Dean Troyera6466e02011-10-25 17:53:24 -0500199 chroot $MNTDIR chown -R stack $2
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700200}
201
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500202git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
203git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
204git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
205git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700206git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500207git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
208git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700209
Dean Troyerb5da5192011-10-17 13:32:06 -0500210# Use this version of devstack
Dean Troyera6466e02011-10-25 17:53:24 -0500211rm -rf $MNTDIR/$DEST/devstack
212cp -pr $CWD $MNTDIR/$DEST/devstack
213chroot $MNTDIR chown -R stack $DEST/devstack
Dean Troyer03412c82011-10-03 09:56:41 -0500214
Dean Troyercf9db8d2011-10-03 13:42:16 -0500215# Configure host network for DHCP
Dean Troyera6466e02011-10-25 17:53:24 -0500216mkdir -p $MNTDIR/etc/network
217cat > $MNTDIR/etc/network/interfaces <<EOF
Dean Troyercf9db8d2011-10-03 13:42:16 -0500218auto lo
219iface lo inet loopback
220
221auto eth0
222iface eth0 inet dhcp
223EOF
224
Dean Troyer288f3bd2011-10-13 15:50:44 -0500225# Set hostname
Dean Troyera6466e02011-10-25 17:53:24 -0500226echo "ramstack" >$MNTDIR/etc/hostname
227echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts
Dean Troyer288f3bd2011-10-13 15:50:44 -0500228
Dean Troyer8f851e72011-10-11 20:22:23 -0500229# Configure the runner
Dean Troyera6466e02011-10-25 17:53:24 -0500230RUN_SH=$MNTDIR/$DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500231cat > $RUN_SH <<EOF
232#!/usr/bin/env bash
233
Dean Troyer7c076ee2011-10-13 13:20:13 -0500234# Get IP range
235set \`ip addr show dev eth0 | grep inet\`
236PREFIX=\`echo \$2 | cut -d. -f1,2,3\`
237export FLOATING_RANGE="\$PREFIX.224/27"
238
Dean Troyer8f851e72011-10-11 20:22:23 -0500239# Kill any existing screens
240killall screen
241
242# Run stack.sh
243cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log
244echo >> $DEST/run.sh.log
245echo >> $DEST/run.sh.log
246echo "All done! Time to start clicking." >> $DEST/run.sh.log
247EOF
248
249# Make the run.sh executable
250chmod 755 $RUN_SH
Dean Troyera6466e02011-10-25 17:53:24 -0500251chroot $MNTDIR chown stack $DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500252
Dean Troyera6466e02011-10-25 17:53:24 -0500253umount $MNTDIR
254rmdir $MNTDIR