blob: 737255578a97fe58ffae2e45d51fb5fd71e4a040 [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
Attila Fazekas237225d2013-11-06 15:41:04 +010025 if [ -n "$DEV_FILE_TMP" -a -e "$DEV_FILE_TMP" ]; then
Dean Troyer55c02732011-11-01 17:44:03 -050026 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
Adam Spierscb961592013-10-05 12:11:07 +010087# Finds and returns full device path for the next available NBD device.
88# Exits script if error connecting or none free.
Dean Troyerdccd6b92011-11-01 15:46:14 -050089# map_nbd image
Adam Spierscb961592013-10-05 12:11:07 +010090function map_nbd() {
Dean Troyerdccd6b92011-11-01 15:46:14 -050091 for i in `seq 0 15`; do
92 if [ ! -e /sys/block/nbd$i/pid ]; then
93 NBD=/dev/nbd$i
94 # Connect to nbd and wait till it is ready
95 qemu-nbd -c $NBD $1
96 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
97 echo "Couldn't connect $NBD"
98 exit 1
99 fi
100 break
101 fi
102 done
103 if [ -z "$NBD" ]; then
104 echo "No free NBD slots"
Dean Troyera6466e02011-10-25 17:53:24 -0500105 exit 1
106 fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500107 echo $NBD
108}
109
Vincent Untz7c3053d2012-11-29 09:19:16 +0100110# Prime image with as many apt as we can
Dean Troyerdccd6b92011-11-01 15:46:14 -0500111DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
112DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
113if [ ! -r $DEV_FILE ]; then
114 cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
115
116 NBD=`map_nbd $DEV_FILE_TMP`
Dean Troyera6466e02011-10-25 17:53:24 -0500117 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
118 mount -t ext4 ${NBD}p1 $MNTDIR
119 cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
120
Anthony Youngca2c0472011-11-03 16:29:32 -0700121 chroot $MNTDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
122 chroot $MNTDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700123
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700124 # Create a stack user that is a member of the libvirtd group so that stack
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700125 # is able to interact with libvirt.
Dean Troyera6466e02011-10-25 17:53:24 -0500126 chroot $MNTDIR groupadd libvirtd
Dean Troyer74759aa2013-01-24 14:19:55 -0600127 chroot $MNTDIR useradd $STACK_USER -s /bin/bash -d $DEST -G libvirtd
Dean Troyera6466e02011-10-25 17:53:24 -0500128 mkdir -p $MNTDIR/$DEST
Dean Troyer74759aa2013-01-24 14:19:55 -0600129 chroot $MNTDIR chown $STACK_USER $DEST
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700130
Dean Troyere62ba4d2012-06-27 22:07:34 -0500131 # A simple password - pass
Dean Troyer74759aa2013-01-24 14:19:55 -0600132 echo $STACK_USER:pass | chroot $MNTDIR chpasswd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500133 echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700134
Dean Troyere62ba4d2012-06-27 22:07:34 -0500135 # And has sudo ability (in the future this should be limited to only what
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700136 # stack requires)
Dean Troyer74759aa2013-01-24 14:19:55 -0600137 echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
Dean Troyera6466e02011-10-25 17:53:24 -0500138
139 umount $MNTDIR
140 rmdir $MNTDIR
141 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500142 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500143 mv $DEV_FILE_TMP $DEV_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700144fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500145rm -f $DEV_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700146
Dean Troyere62ba4d2012-06-27 22:07:34 -0500147
148# Clone git repositories onto the system
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700149# ======================================
150
Dean Troyerdccd6b92011-11-01 15:46:14 -0500151IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
152
Dean Troyera6466e02011-10-25 17:53:24 -0500153if [ ! -r $IMG_FILE ]; then
Dean Troyerdccd6b92011-11-01 15:46:14 -0500154 NBD=`map_nbd $DEV_FILE`
Dean Troyer61be9242011-10-25 22:35:23 -0500155
156 # Pre-create the image file
157 # FIXME(dt): This should really get the partition size to
Adam Spierscb961592013-10-05 12:11:07 +0100158 # pre-create the image file
Dean Troyerdccd6b92011-11-01 15:46:14 -0500159 dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
Dean Troyer61be9242011-10-25 22:35:23 -0500160 # Create filesystem image for RAM disk
Dean Troyerdccd6b92011-11-01 15:46:14 -0500161 dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
Dean Troyer61be9242011-10-25 22:35:23 -0500162
163 qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500164 NBD=""
Dean Troyerdccd6b92011-11-01 15:46:14 -0500165 mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700166fi
Dean Troyerdccd6b92011-11-01 15:46:14 -0500167rm -f $IMG_FILE_TMP
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700168
Dean Troyera6466e02011-10-25 17:53:24 -0500169MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
Dean Troyer61be9242011-10-25 22:35:23 -0500170mount -t ext4 -o loop $IMG_FILE $MNTDIR
Dean Troyera6466e02011-10-25 17:53:24 -0500171cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
172
Dean Troyerea442c12011-10-26 12:34:59 -0500173# We need to install a non-virtual kernel and modules to boot from
Dean Troyer7920b0f2011-10-26 15:10:46 -0500174if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
Dean Troyerea442c12011-10-26 12:34:59 -0500175 chroot $MNTDIR apt-get install -y linux-generic
176fi
177
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500178git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
179git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH
180git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH
181git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700182git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH
Dean Troyer11e5e6f2011-10-03 09:40:32 -0500183git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH
184git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
Jesse Andrewsd31c4ea2011-09-28 02:30:57 -0700185
Dean Troyerb5da5192011-10-17 13:32:06 -0500186# Use this version of devstack
Dean Troyera6466e02011-10-25 17:53:24 -0500187rm -rf $MNTDIR/$DEST/devstack
188cp -pr $CWD $MNTDIR/$DEST/devstack
Dean Troyer74759aa2013-01-24 14:19:55 -0600189chroot $MNTDIR chown -R $STACK_USER $DEST/devstack
Dean Troyer03412c82011-10-03 09:56:41 -0500190
Dean Troyercf9db8d2011-10-03 13:42:16 -0500191# Configure host network for DHCP
Dean Troyera6466e02011-10-25 17:53:24 -0500192mkdir -p $MNTDIR/etc/network
193cat > $MNTDIR/etc/network/interfaces <<EOF
Dean Troyercf9db8d2011-10-03 13:42:16 -0500194auto lo
195iface lo inet loopback
196
197auto eth0
198iface eth0 inet dhcp
199EOF
200
Dean Troyer288f3bd2011-10-13 15:50:44 -0500201# Set hostname
Dean Troyera6466e02011-10-25 17:53:24 -0500202echo "ramstack" >$MNTDIR/etc/hostname
203echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts
Dean Troyer288f3bd2011-10-13 15:50:44 -0500204
Dean Troyer8f851e72011-10-11 20:22:23 -0500205# Configure the runner
Dean Troyera6466e02011-10-25 17:53:24 -0500206RUN_SH=$MNTDIR/$DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500207cat > $RUN_SH <<EOF
208#!/usr/bin/env bash
209
Dean Troyer7c076ee2011-10-13 13:20:13 -0500210# Get IP range
211set \`ip addr show dev eth0 | grep inet\`
212PREFIX=\`echo \$2 | cut -d. -f1,2,3\`
213export FLOATING_RANGE="\$PREFIX.224/27"
214
Dean Troyer8f851e72011-10-11 20:22:23 -0500215# Kill any existing screens
216killall screen
217
218# Run stack.sh
219cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log
220echo >> $DEST/run.sh.log
221echo >> $DEST/run.sh.log
222echo "All done! Time to start clicking." >> $DEST/run.sh.log
223EOF
224
225# Make the run.sh executable
226chmod 755 $RUN_SH
Dean Troyer74759aa2013-01-24 14:19:55 -0600227chroot $MNTDIR chown $STACK_USER $DEST/run.sh
Dean Troyer8f851e72011-10-11 20:22:23 -0500228
Dean Troyera6466e02011-10-25 17:53:24 -0500229umount $MNTDIR
230rmdir $MNTDIR