blob: 7c1600b1ec2d72816d1c2ac353323ebb62710dfe [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)
Dean Troyer7f9aa712012-01-31 12:11:56 -060050TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
51
52# Import common functions
53. $TOP_DIR/functions
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070054
Dean Troyer03412c82011-10-03 09:56:41 -050055# Store cwd
56CWD=`pwd`
57
Dean Troyer2567c812011-11-01 12:36:59 -050058cd $TOP_DIR
59
60# Source params
61source ./stackrc
62
Jesse Andrewsd7326d22011-11-20 10:02:26 -080063CACHEDIR=${CACHEDIR:-/opt/stack/cache}
Dean Troyer2567c812011-11-01 12:36:59 -050064
Dean Troyer11e5e6f2011-10-03 09:40:32 -050065DEST=${DEST:-/opt/stack}
66
Dean Troyer2567c812011-11-01 12:36:59 -050067# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
68ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
69
70# Base image (natty by default)
71DIST_NAME=${DIST_NAME:-natty}
72
Dean Troyer8f851e72011-10-11 20:22:23 -050073# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
74STACKSH_PARAMS=${STACKSH_PARAMS:-}
75
Dean Troyera3379e02011-10-03 11:14:13 -050076# Option to use the version of devstack on which we are currently working
77USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
78
Dean Troyer2567c812011-11-01 12:36:59 -050079# clean install
80if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then
81 $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070082fi
83
Dean Troyerdccd6b92011-11-01 15:46:14 -050084# Finds the next available NBD device
85# Exits script if error connecting or none free
86# map_nbd image
87# returns full nbd device path
88function map_nbd {
89 for i in `seq 0 15`; do
90 if [ ! -e /sys/block/nbd$i/pid ]; then
91 NBD=/dev/nbd$i
92 # Connect to nbd and wait till it is ready
93 qemu-nbd -c $NBD $1
94 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
95 echo "Couldn't connect $NBD"
96 exit 1
97 fi
98 break
99 fi
100 done
101 if [ -z "$NBD" ]; then
102 echo "No free NBD slots"
Dean Troyera6466e02011-10-25 17:53:24 -0500103 exit 1
104 fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500105 echo $NBD
106}
107
108# prime image with as many apt/pips as we can
109DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
110DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
111if [ ! -r $DEV_FILE ]; then
112 cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
113
114 NBD=`map_nbd $DEV_FILE_TMP`
Dean Troyera6466e02011-10-25 17:53:24 -0500115 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
116 mount -t ext4 ${NBD}p1 $MNTDIR
117 cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
118
Anthony Youngca2c0472011-11-03 16:29:32 -0700119 chroot $MNTDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
120 chroot $MNTDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
Dean Troyera6466e02011-10-25 17:53:24 -0500121 chroot $MNTDIR pip install `cat files/pips/*`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700122
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700123 # Create a stack user that is a member of the libvirtd group so that stack
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700124 # is able to interact with libvirt.
Dean Troyera6466e02011-10-25 17:53:24 -0500125 chroot $MNTDIR groupadd libvirtd
126 chroot $MNTDIR useradd stack -s /bin/bash -d $DEST -G libvirtd
127 mkdir -p $MNTDIR/$DEST
128 chroot $MNTDIR chown stack $DEST
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700129
130 # a simple password - pass
Dean Troyerdccd6b92011-11-01 15:46:14 -0500131 echo stack:pass | chroot $MNTDIR chpasswd
132 echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700133
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700134 # and has sudo ability (in the future this should be limited to only what
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700135 # stack requires)
Dean Troyera6466e02011-10-25 17:53:24 -0500136 echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
137
138 umount $MNTDIR
139 rmdir $MNTDIR
140 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500141 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500142 mv $DEV_FILE_TMP $DEV_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700143fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500144rm -f $DEV_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700145
146# clone git repositories onto the system
147# ======================================
148
Dean Troyerdccd6b92011-11-01 15:46:14 -0500149IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
150
Dean Troyera6466e02011-10-25 17:53:24 -0500151if [ ! -r $IMG_FILE ]; then
Dean Troyerdccd6b92011-11-01 15:46:14 -0500152 NBD=`map_nbd $DEV_FILE`
Dean Troyer61be9242011-10-25 22:35:23 -0500153
154 # Pre-create the image file
155 # FIXME(dt): This should really get the partition size to
156 # pre-create the image file
Dean Troyerdccd6b92011-11-01 15:46:14 -0500157 dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
Dean Troyer61be9242011-10-25 22:35:23 -0500158 # Create filesystem image for RAM disk
Dean Troyerdccd6b92011-11-01 15:46:14 -0500159 dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
Dean Troyer61be9242011-10-25 22:35:23 -0500160
161 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500162 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500163 mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700164fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500165rm -f $IMG_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700166
Dean Troyera6466e02011-10-25 17:53:24 -0500167MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
Dean Troyer61be9242011-10-25 22:35:23 -0500168mount -t ext4 -o loop $IMG_FILE $MNTDIR
Dean Troyera6466e02011-10-25 17:53:24 -0500169cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
170
Dean Troyerea442c12011-10-26 12:34:59 -0500171# We need to install a non-virtual kernel and modules to boot from
Dean Troyer7920b0f2011-10-26 15:10:46 -0500172if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
Dean Troyerea442c12011-10-26 12:34:59 -0500173 chroot $MNTDIR apt-get install -y linux-generic
174fi
175
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500176git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
177git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
178git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
179git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700180git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500181git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
182git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700183
Dean Troyerb5da5192011-10-17 13:32:06 -0500184# Use this version of devstack
Dean Troyera6466e02011-10-25 17:53:24 -0500185rm -rf $MNTDIR/$DEST/devstack
186cp -pr $CWD $MNTDIR/$DEST/devstack
187chroot $MNTDIR chown -R stack $DEST/devstack
Dean Troyer03412c82011-10-03 09:56:41 -0500188
Dean Troyercf9db8d2011-10-03 13:42:16 -0500189# Configure host network for DHCP
Dean Troyera6466e02011-10-25 17:53:24 -0500190mkdir -p $MNTDIR/etc/network
191cat > $MNTDIR/etc/network/interfaces <<EOF
Dean Troyercf9db8d2011-10-03 13:42:16 -0500192auto lo
193iface lo inet loopback
194
195auto eth0
196iface eth0 inet dhcp
197EOF
198
Dean Troyer288f3bd2011-10-13 15:50:44 -0500199# Set hostname
Dean Troyera6466e02011-10-25 17:53:24 -0500200echo "ramstack" >$MNTDIR/etc/hostname
201echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts
Dean Troyer288f3bd2011-10-13 15:50:44 -0500202
Dean Troyer8f851e72011-10-11 20:22:23 -0500203# Configure the runner
Dean Troyera6466e02011-10-25 17:53:24 -0500204RUN_SH=$MNTDIR/$DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500205cat > $RUN_SH <<EOF
206#!/usr/bin/env bash
207
Dean Troyer7c076ee2011-10-13 13:20:13 -0500208# Get IP range
209set \`ip addr show dev eth0 | grep inet\`
210PREFIX=\`echo \$2 | cut -d. -f1,2,3\`
211export FLOATING_RANGE="\$PREFIX.224/27"
212
Dean Troyer8f851e72011-10-11 20:22:23 -0500213# Kill any existing screens
214killall screen
215
216# Run stack.sh
217cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log
218echo >> $DEST/run.sh.log
219echo >> $DEST/run.sh.log
220echo "All done! Time to start clicking." >> $DEST/run.sh.log
221EOF
222
223# Make the run.sh executable
224chmod 755 $RUN_SH
Dean Troyera6466e02011-10-25 17:53:24 -0500225chroot $MNTDIR chown stack $DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500226
Dean Troyera6466e02011-10-25 17:53:24 -0500227umount $MNTDIR
228rmdir $MNTDIR