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) |
| 50 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 51 | |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 52 | # Store cwd |
| 53 | CWD=`pwd` |
| 54 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 55 | cd $TOP_DIR |
| 56 | |
| 57 | # Source params |
| 58 | source ./stackrc |
| 59 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame^] | 60 | CACHEDIR=${CACHEDIR:-/opt/stack/cache} |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 61 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 62 | DEST=${DEST:-/opt/stack} |
| 63 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 64 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 65 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 66 | |
| 67 | # Base image (natty by default) |
| 68 | DIST_NAME=${DIST_NAME:-natty} |
| 69 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 70 | # Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova" |
| 71 | STACKSH_PARAMS=${STACKSH_PARAMS:-} |
| 72 | |
Dean Troyer | a3379e0 | 2011-10-03 11:14:13 -0500 | [diff] [blame] | 73 | # Option to use the version of devstack on which we are currently working |
| 74 | USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1} |
| 75 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 76 | # clean install |
| 77 | if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then |
| 78 | $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] | 79 | fi |
| 80 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 81 | # 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 |
| 85 | function 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 Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 100 | exit 1 |
| 101 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 102 | echo $NBD |
| 103 | } |
| 104 | |
| 105 | # prime image with as many apt/pips as we can |
| 106 | DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img |
| 107 | DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX` |
| 108 | if [ ! -r $DEV_FILE ]; then |
| 109 | cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP |
| 110 | |
| 111 | NBD=`map_nbd $DEV_FILE_TMP` |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 112 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
| 113 | mount -t ext4 ${NBD}p1 $MNTDIR |
| 114 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 115 | |
Anthony Young | ca2c047 | 2011-11-03 16:29:32 -0700 | [diff] [blame] | 116 | 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 Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 118 | chroot $MNTDIR pip install `cat files/pips/*` |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 119 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 120 | # 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] | 121 | # is able to interact with libvirt. |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 122 | 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 Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 126 | |
| 127 | # a simple password - pass |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 128 | echo stack:pass | chroot $MNTDIR chpasswd |
| 129 | echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 130 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 131 | # 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] | 132 | # stack requires) |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 133 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers |
| 134 | |
| 135 | umount $MNTDIR |
| 136 | rmdir $MNTDIR |
| 137 | qemu-nbd -d $NBD |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 138 | NBD="" |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 139 | mv $DEV_FILE_TMP $DEV_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 140 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 141 | rm -f $DEV_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 142 | |
| 143 | # clone git repositories onto the system |
| 144 | # ====================================== |
| 145 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 146 | IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX` |
| 147 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 148 | if [ ! -r $IMG_FILE ]; then |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 149 | NBD=`map_nbd $DEV_FILE` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 150 | |
| 151 | # Pre-create the image file |
| 152 | # FIXME(dt): This should really get the partition size to |
| 153 | # pre-create the image file |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 154 | 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] | 155 | # Create filesystem image for RAM disk |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 156 | dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 157 | |
| 158 | qemu-nbd -d $NBD |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 159 | NBD="" |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 160 | mv $IMG_FILE_TMP $IMG_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 161 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 162 | rm -f $IMG_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 163 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 164 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 165 | mount -t ext4 -o loop $IMG_FILE $MNTDIR |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 166 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 167 | |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 168 | # 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] | 169 | if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 170 | chroot $MNTDIR apt-get install -y linux-generic |
| 171 | fi |
| 172 | |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 173 | # 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. |
| 176 | function git_clone { |
| 177 | |
| 178 | # clone new copy or fetch latest changes |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 179 | CHECKOUT=${MNTDIR}$2 |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 180 | 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 Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 199 | chroot $MNTDIR chown -R stack $2 |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 202 | git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH |
| 203 | git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH |
| 204 | git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH |
| 205 | git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH |
Tres Henry | ca85b79 | 2011-10-28 14:00:21 -0700 | [diff] [blame] | 206 | git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 207 | git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH |
| 208 | git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 209 | |
Dean Troyer | b5da519 | 2011-10-17 13:32:06 -0500 | [diff] [blame] | 210 | # Use this version of devstack |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 211 | rm -rf $MNTDIR/$DEST/devstack |
| 212 | cp -pr $CWD $MNTDIR/$DEST/devstack |
| 213 | chroot $MNTDIR chown -R stack $DEST/devstack |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 214 | |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 215 | # Configure host network for DHCP |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 216 | mkdir -p $MNTDIR/etc/network |
| 217 | cat > $MNTDIR/etc/network/interfaces <<EOF |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 218 | auto lo |
| 219 | iface lo inet loopback |
| 220 | |
| 221 | auto eth0 |
| 222 | iface eth0 inet dhcp |
| 223 | EOF |
| 224 | |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 225 | # Set hostname |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 226 | echo "ramstack" >$MNTDIR/etc/hostname |
| 227 | echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 228 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 229 | # Configure the runner |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 230 | RUN_SH=$MNTDIR/$DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 231 | cat > $RUN_SH <<EOF |
| 232 | #!/usr/bin/env bash |
| 233 | |
Dean Troyer | 7c076ee | 2011-10-13 13:20:13 -0500 | [diff] [blame] | 234 | # Get IP range |
| 235 | set \`ip addr show dev eth0 | grep inet\` |
| 236 | PREFIX=\`echo \$2 | cut -d. -f1,2,3\` |
| 237 | export FLOATING_RANGE="\$PREFIX.224/27" |
| 238 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 239 | # Kill any existing screens |
| 240 | killall screen |
| 241 | |
| 242 | # Run stack.sh |
| 243 | cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log |
| 244 | echo >> $DEST/run.sh.log |
| 245 | echo >> $DEST/run.sh.log |
| 246 | echo "All done! Time to start clicking." >> $DEST/run.sh.log |
| 247 | EOF |
| 248 | |
| 249 | # Make the run.sh executable |
| 250 | chmod 755 $RUN_SH |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 251 | chroot $MNTDIR chown stack $DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 252 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 253 | umount $MNTDIR |
| 254 | rmdir $MNTDIR |