blob: 2c455685316e67ffcabab1c310cb6d8db0eef83b [file] [log] [blame]
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -07001#!/bin/bash
2
Dean Troyere62ba4d2012-06-27 22:07:34 -05003# **build_ramdisk.sh**
4
5# Build RAM disk images
6
7# Exit on error to stop unexpected errors
Dean Troyer2567c812011-11-01 12:36:59 -05008set -o errexit
9
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070010if [ ! "$#" -eq "1" ]; then
Dean Troyer2567c812011-11-01 12:36:59 -050011 echo "$0 builds a gziped Ubuntu OpenStack install"
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070012 echo "usage: $0 dest"
13 exit 1
14fi
15
Dean Troyer55c02732011-11-01 17:44:03 -050016# Clean up any resources that may be in use
17cleanup() {
18 set +o errexit
19
20 # Mop up temporary files
21 if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
22 umount $MNTDIR
23 rmdir $MNTDIR
24 fi
25 if [ -n "$DEV_FILE_TMP" -a -e "$DEV_FILE_TMP "]; then
26 rm -f $DEV_FILE_TMP
27 fi
28 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
29 rm -f $IMG_FILE_TMP
30 fi
31
32 # Release NBD devices
33 if [ -n "$NBD" ]; then
34 qemu-nbd -d $NBD
35 fi
36
37 # Kill ourselves to signal any calling process
38 trap 2; kill -2 $$
39}
40
41trap cleanup SIGHUP SIGINT SIGTERM
42
Dean Troyerdccd6b92011-11-01 15:46:14 -050043# Set up nbd
44modprobe nbd max_part=63
45
Dean Troyer2567c812011-11-01 12:36:59 -050046# Echo commands
47set -o xtrace
48
Dean Troyera6466e02011-10-25 17:53:24 -050049IMG_FILE=$1
50
Dean Troyer2567c812011-11-01 12:36:59 -050051# Keep track of the current directory
52TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer7f9aa712012-01-31 12:11:56 -060053TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
54
55# Import common functions
56. $TOP_DIR/functions
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070057
Dean Troyer03412c82011-10-03 09:56:41 -050058# Store cwd
59CWD=`pwd`
60
Dean Troyer2567c812011-11-01 12:36:59 -050061cd $TOP_DIR
62
63# Source params
64source ./stackrc
65
Jesse Andrewsd7326d22011-11-20 10:02:26 -080066CACHEDIR=${CACHEDIR:-/opt/stack/cache}
Dean Troyer2567c812011-11-01 12:36:59 -050067
Dean Troyer11e5e6f2011-10-03 09:40:32 -050068DEST=${DEST:-/opt/stack}
69
Dean Troyer2567c812011-11-01 12:36:59 -050070# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
71ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
72
73# Base image (natty by default)
74DIST_NAME=${DIST_NAME:-natty}
75
Dean Troyer8f851e72011-10-11 20:22:23 -050076# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
77STACKSH_PARAMS=${STACKSH_PARAMS:-}
78
Dean Troyera3379e02011-10-03 11:14:13 -050079# Option to use the version of devstack on which we are currently working
80USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
81
Dean Troyer2567c812011-11-01 12:36:59 -050082# clean install
83if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then
84 $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -070085fi
86
Dean Troyerdccd6b92011-11-01 15:46:14 -050087# Finds the next available NBD device
88# Exits script if error connecting or none free
89# map_nbd image
Dean Troyere62ba4d2012-06-27 22:07:34 -050090# Returns full nbd device path
Dean Troyerdccd6b92011-11-01 15:46:14 -050091function map_nbd {
92 for i in `seq 0 15`; do
93 if [ ! -e /sys/block/nbd$i/pid ]; then
94 NBD=/dev/nbd$i
95 # Connect to nbd and wait till it is ready
96 qemu-nbd -c $NBD $1
97 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
98 echo "Couldn't connect $NBD"
99 exit 1
100 fi
101 break
102 fi
103 done
104 if [ -z "$NBD" ]; then
105 echo "No free NBD slots"
Dean Troyera6466e02011-10-25 17:53:24 -0500106 exit 1
107 fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500108 echo $NBD
109}
110
Vincent Untz7c3053d2012-11-29 09:19:16 +0100111# Prime image with as many apt as we can
Dean Troyerdccd6b92011-11-01 15:46:14 -0500112DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
113DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
114if [ ! -r $DEV_FILE ]; then
115 cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
116
117 NBD=`map_nbd $DEV_FILE_TMP`
Dean Troyera6466e02011-10-25 17:53:24 -0500118 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
119 mount -t ext4 ${NBD}p1 $MNTDIR
120 cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
121
Anthony Youngca2c0472011-11-03 16:29:32 -0700122 chroot $MNTDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
123 chroot $MNTDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700124
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700125 # Create a stack user that is a member of the libvirtd group so that stack
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700126 # is able to interact with libvirt.
Dean Troyera6466e02011-10-25 17:53:24 -0500127 chroot $MNTDIR groupadd libvirtd
Dean Troyer74759aa2013-01-24 14:19:55 -0600128 chroot $MNTDIR useradd $STACK_USER -s /bin/bash -d $DEST -G libvirtd
Dean Troyera6466e02011-10-25 17:53:24 -0500129 mkdir -p $MNTDIR/$DEST
Dean Troyer74759aa2013-01-24 14:19:55 -0600130 chroot $MNTDIR chown $STACK_USER $DEST
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700131
Dean Troyere62ba4d2012-06-27 22:07:34 -0500132 # A simple password - pass
Dean Troyer74759aa2013-01-24 14:19:55 -0600133 echo $STACK_USER:pass | chroot $MNTDIR chpasswd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500134 echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700135
Dean Troyere62ba4d2012-06-27 22:07:34 -0500136 # And has sudo ability (in the future this should be limited to only what
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700137 # stack requires)
Dean Troyer74759aa2013-01-24 14:19:55 -0600138 echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
Dean Troyera6466e02011-10-25 17:53:24 -0500139
140 umount $MNTDIR
141 rmdir $MNTDIR
142 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500143 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500144 mv $DEV_FILE_TMP $DEV_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700145fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500146rm -f $DEV_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700147
Dean Troyere62ba4d2012-06-27 22:07:34 -0500148
149# Clone git repositories onto the system
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700150# ======================================
151
Dean Troyerdccd6b92011-11-01 15:46:14 -0500152IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
153
Dean Troyera6466e02011-10-25 17:53:24 -0500154if [ ! -r $IMG_FILE ]; then
Dean Troyerdccd6b92011-11-01 15:46:14 -0500155 NBD=`map_nbd $DEV_FILE`
Dean Troyer61be9242011-10-25 22:35:23 -0500156
157 # Pre-create the image file
158 # FIXME(dt): This should really get the partition size to
159 # pre-create the image file
Dean Troyerdccd6b92011-11-01 15:46:14 -0500160 dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
Dean Troyer61be9242011-10-25 22:35:23 -0500161 # Create filesystem image for RAM disk
Dean Troyerdccd6b92011-11-01 15:46:14 -0500162 dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
Dean Troyer61be9242011-10-25 22:35:23 -0500163
164 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500165 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500166 mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700167fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500168rm -f $IMG_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700169
Dean Troyera6466e02011-10-25 17:53:24 -0500170MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
Dean Troyer61be9242011-10-25 22:35:23 -0500171mount -t ext4 -o loop $IMG_FILE $MNTDIR
Dean Troyera6466e02011-10-25 17:53:24 -0500172cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
173
Dean Troyerea442c12011-10-26 12:34:59 -0500174# We need to install a non-virtual kernel and modules to boot from
Dean Troyer7920b0f2011-10-26 15:10:46 -0500175if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
Dean Troyerea442c12011-10-26 12:34:59 -0500176 chroot $MNTDIR apt-get install -y linux-generic
177fi
178
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500179git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
180git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
181git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
182git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700183git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500184git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
185git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700186
Dean Troyerb5da5192011-10-17 13:32:06 -0500187# Use this version of devstack
Dean Troyera6466e02011-10-25 17:53:24 -0500188rm -rf $MNTDIR/$DEST/devstack
189cp -pr $CWD $MNTDIR/$DEST/devstack
Dean Troyer74759aa2013-01-24 14:19:55 -0600190chroot $MNTDIR chown -R $STACK_USER $DEST/devstack
Dean Troyer03412c82011-10-03 09:56:41 -0500191
Dean Troyercf9db8d2011-10-03 13:42:16 -0500192# Configure host network for DHCP
Dean Troyera6466e02011-10-25 17:53:24 -0500193mkdir -p $MNTDIR/etc/network
194cat > $MNTDIR/etc/network/interfaces <<EOF
Dean Troyercf9db8d2011-10-03 13:42:16 -0500195auto lo
196iface lo inet loopback
197
198auto eth0
199iface eth0 inet dhcp
200EOF
201
Dean Troyer288f3bd2011-10-13 15:50:44 -0500202# Set hostname
Dean Troyera6466e02011-10-25 17:53:24 -0500203echo "ramstack" >$MNTDIR/etc/hostname
204echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts
Dean Troyer288f3bd2011-10-13 15:50:44 -0500205
Dean Troyer8f851e72011-10-11 20:22:23 -0500206# Configure the runner
Dean Troyera6466e02011-10-25 17:53:24 -0500207RUN_SH=$MNTDIR/$DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500208cat > $RUN_SH <<EOF
209#!/usr/bin/env bash
210
Dean Troyer7c076ee2011-10-13 13:20:13 -0500211# Get IP range
212set \`ip addr show dev eth0 | grep inet\`
213PREFIX=\`echo \$2 | cut -d. -f1,2,3\`
214export FLOATING_RANGE="\$PREFIX.224/27"
215
Dean Troyer8f851e72011-10-11 20:22:23 -0500216# Kill any existing screens
217killall screen
218
219# Run stack.sh
220cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log
221echo >> $DEST/run.sh.log
222echo >> $DEST/run.sh.log
223echo "All done! Time to start clicking." >> $DEST/run.sh.log
224EOF
225
226# Make the run.sh executable
227chmod 755 $RUN_SH
Dean Troyer74759aa2013-01-24 14:19:55 -0600228chroot $MNTDIR chown $STACK_USER $DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500229
Dean Troyera6466e02011-10-25 17:53:24 -0500230umount $MNTDIR
231rmdir $MNTDIR