Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 1 | #!/bin/bash |
Dean Troyer | 5611951 | 2011-10-11 19:39:34 -0500 | [diff] [blame] | 2 | # build_ramdisk.sh - Build RAM disk images |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 3 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 4 | # exit on error to stop unexpected errors |
| 5 | set -o errexit |
| 6 | |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 7 | if [ ! "$#" -eq "1" ]; then |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 8 | echo "$0 builds a gziped Ubuntu OpenStack install" |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 9 | echo "usage: $0 dest" |
| 10 | exit 1 |
| 11 | fi |
| 12 | |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 13 | # Clean up any resources that may be in use |
| 14 | cleanup() { |
| 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 | |
| 38 | trap cleanup SIGHUP SIGINT SIGTERM |
| 39 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 40 | # Set up nbd |
| 41 | modprobe nbd max_part=63 |
| 42 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 43 | # Echo commands |
| 44 | set -o xtrace |
| 45 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 46 | IMG_FILE=$1 |
| 47 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 48 | # Keep track of the current directory |
| 49 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 50 | TOP_DIR=$(cd $TOOLS_DIR/..; pwd) |
| 51 | |
| 52 | # Import common functions |
| 53 | . $TOP_DIR/functions |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 54 | |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 55 | # Store cwd |
| 56 | CWD=`pwd` |
| 57 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 58 | cd $TOP_DIR |
| 59 | |
| 60 | # Source params |
| 61 | source ./stackrc |
| 62 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 63 | CACHEDIR=${CACHEDIR:-/opt/stack/cache} |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 64 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 65 | DEST=${DEST:-/opt/stack} |
| 66 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 67 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 68 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 69 | |
| 70 | # Base image (natty by default) |
| 71 | DIST_NAME=${DIST_NAME:-natty} |
| 72 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 73 | # Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova" |
| 74 | STACKSH_PARAMS=${STACKSH_PARAMS:-} |
| 75 | |
Dean Troyer | a3379e0 | 2011-10-03 11:14:13 -0500 | [diff] [blame] | 76 | # Option to use the version of devstack on which we are currently working |
| 77 | USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1} |
| 78 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 79 | # clean install |
| 80 | if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then |
| 81 | $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 82 | fi |
| 83 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 84 | # 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 |
| 88 | function 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 Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 103 | exit 1 |
| 104 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 105 | echo $NBD |
| 106 | } |
| 107 | |
| 108 | # prime image with as many apt/pips as we can |
| 109 | DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img |
| 110 | DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX` |
| 111 | if [ ! -r $DEV_FILE ]; then |
| 112 | cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP |
| 113 | |
| 114 | NBD=`map_nbd $DEV_FILE_TMP` |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 115 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
| 116 | mount -t ext4 ${NBD}p1 $MNTDIR |
| 117 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 118 | |
Anthony Young | ca2c047 | 2011-11-03 16:29:32 -0700 | [diff] [blame] | 119 | 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 Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 121 | chroot $MNTDIR pip install `cat files/pips/*` |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 122 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 123 | # Create a stack user that is a member of the libvirtd group so that stack |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 124 | # is able to interact with libvirt. |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 125 | 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 Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 129 | |
| 130 | # a simple password - pass |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 131 | echo stack:pass | chroot $MNTDIR chpasswd |
| 132 | echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 133 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 134 | # and has sudo ability (in the future this should be limited to only what |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 135 | # stack requires) |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 136 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers |
| 137 | |
| 138 | umount $MNTDIR |
| 139 | rmdir $MNTDIR |
| 140 | qemu-nbd -d $NBD |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 141 | NBD="" |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 142 | mv $DEV_FILE_TMP $DEV_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 143 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 144 | rm -f $DEV_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 145 | |
| 146 | # clone git repositories onto the system |
| 147 | # ====================================== |
| 148 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 149 | IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX` |
| 150 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 151 | if [ ! -r $IMG_FILE ]; then |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 152 | NBD=`map_nbd $DEV_FILE` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 153 | |
| 154 | # Pre-create the image file |
| 155 | # FIXME(dt): This should really get the partition size to |
| 156 | # pre-create the image file |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 157 | dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024)) |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 158 | # Create filesystem image for RAM disk |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 159 | dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 160 | |
| 161 | qemu-nbd -d $NBD |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 162 | NBD="" |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 163 | mv $IMG_FILE_TMP $IMG_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 164 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 165 | rm -f $IMG_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 166 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 167 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 168 | mount -t ext4 -o loop $IMG_FILE $MNTDIR |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 169 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 170 | |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 171 | # We need to install a non-virtual kernel and modules to boot from |
Dean Troyer | 7920b0f | 2011-10-26 15:10:46 -0500 | [diff] [blame] | 172 | if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 173 | chroot $MNTDIR apt-get install -y linux-generic |
| 174 | fi |
| 175 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 176 | git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH |
| 177 | git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH |
| 178 | git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH |
| 179 | git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH |
Tres Henry | ca85b79 | 2011-10-28 14:00:21 -0700 | [diff] [blame] | 180 | git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 181 | git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH |
| 182 | git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 183 | |
Dean Troyer | b5da519 | 2011-10-17 13:32:06 -0500 | [diff] [blame] | 184 | # Use this version of devstack |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 185 | rm -rf $MNTDIR/$DEST/devstack |
| 186 | cp -pr $CWD $MNTDIR/$DEST/devstack |
| 187 | chroot $MNTDIR chown -R stack $DEST/devstack |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 188 | |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 189 | # Configure host network for DHCP |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 190 | mkdir -p $MNTDIR/etc/network |
| 191 | cat > $MNTDIR/etc/network/interfaces <<EOF |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 192 | auto lo |
| 193 | iface lo inet loopback |
| 194 | |
| 195 | auto eth0 |
| 196 | iface eth0 inet dhcp |
| 197 | EOF |
| 198 | |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 199 | # Set hostname |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 200 | echo "ramstack" >$MNTDIR/etc/hostname |
| 201 | echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 202 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 203 | # Configure the runner |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 204 | RUN_SH=$MNTDIR/$DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 205 | cat > $RUN_SH <<EOF |
| 206 | #!/usr/bin/env bash |
| 207 | |
Dean Troyer | 7c076ee | 2011-10-13 13:20:13 -0500 | [diff] [blame] | 208 | # Get IP range |
| 209 | set \`ip addr show dev eth0 | grep inet\` |
| 210 | PREFIX=\`echo \$2 | cut -d. -f1,2,3\` |
| 211 | export FLOATING_RANGE="\$PREFIX.224/27" |
| 212 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 213 | # Kill any existing screens |
| 214 | killall screen |
| 215 | |
| 216 | # Run stack.sh |
| 217 | cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log |
| 218 | echo >> $DEST/run.sh.log |
| 219 | echo >> $DEST/run.sh.log |
| 220 | echo "All done! Time to start clicking." >> $DEST/run.sh.log |
| 221 | EOF |
| 222 | |
| 223 | # Make the run.sh executable |
| 224 | chmod 755 $RUN_SH |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 225 | chroot $MNTDIR chown stack $DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 226 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 227 | umount $MNTDIR |
| 228 | rmdir $MNTDIR |