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 | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 13 | # Set up nbd |
| 14 | modprobe nbd max_part=63 |
| 15 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 16 | # Echo commands |
| 17 | set -o xtrace |
| 18 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 19 | IMG_FILE=$1 |
| 20 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 21 | # Keep track of the current directory |
| 22 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 23 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 24 | |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 25 | # Store cwd |
| 26 | CWD=`pwd` |
| 27 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 28 | cd $TOP_DIR |
| 29 | |
| 30 | # Source params |
| 31 | source ./stackrc |
| 32 | |
| 33 | CACHEDIR=${CACHEDIR:-/var/cache/devstack} |
| 34 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 35 | DEST=${DEST:-/opt/stack} |
| 36 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 37 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 38 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 39 | |
| 40 | # Base image (natty by default) |
| 41 | DIST_NAME=${DIST_NAME:-natty} |
| 42 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 43 | # Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova" |
| 44 | STACKSH_PARAMS=${STACKSH_PARAMS:-} |
| 45 | |
Dean Troyer | a3379e0 | 2011-10-03 11:14:13 -0500 | [diff] [blame] | 46 | # Option to use the version of devstack on which we are currently working |
| 47 | USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1} |
| 48 | |
Dean Troyer | 2567c81 | 2011-11-01 12:36:59 -0500 | [diff] [blame] | 49 | # clean install |
| 50 | if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then |
| 51 | $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] | 52 | fi |
| 53 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 54 | # Finds the next available NBD device |
| 55 | # Exits script if error connecting or none free |
| 56 | # map_nbd image |
| 57 | # returns full nbd device path |
| 58 | function map_nbd { |
| 59 | for i in `seq 0 15`; do |
| 60 | if [ ! -e /sys/block/nbd$i/pid ]; then |
| 61 | NBD=/dev/nbd$i |
| 62 | # Connect to nbd and wait till it is ready |
| 63 | qemu-nbd -c $NBD $1 |
| 64 | if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then |
| 65 | echo "Couldn't connect $NBD" |
| 66 | exit 1 |
| 67 | fi |
| 68 | break |
| 69 | fi |
| 70 | done |
| 71 | if [ -z "$NBD" ]; then |
| 72 | echo "No free NBD slots" |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 73 | exit 1 |
| 74 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 75 | echo $NBD |
| 76 | } |
| 77 | |
| 78 | # prime image with as many apt/pips as we can |
| 79 | DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img |
| 80 | DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX` |
| 81 | if [ ! -r $DEV_FILE ]; then |
| 82 | cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP |
| 83 | |
| 84 | NBD=`map_nbd $DEV_FILE_TMP` |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 85 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
| 86 | mount -t ext4 ${NBD}p1 $MNTDIR |
| 87 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 88 | |
| 89 | chroot $MNTDIR apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"` |
| 90 | chroot $MNTDIR pip install `cat files/pips/*` |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 91 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 92 | # 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] | 93 | # is able to interact with libvirt. |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 94 | chroot $MNTDIR groupadd libvirtd |
| 95 | chroot $MNTDIR useradd stack -s /bin/bash -d $DEST -G libvirtd |
| 96 | mkdir -p $MNTDIR/$DEST |
| 97 | chroot $MNTDIR chown stack $DEST |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 98 | |
| 99 | # a simple password - pass |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 100 | echo stack:pass | chroot $MNTDIR chpasswd |
| 101 | echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 102 | |
Vishvananda Ishaya | 9b35367 | 2011-10-20 10:07:10 -0700 | [diff] [blame] | 103 | # 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] | 104 | # stack requires) |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 105 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers |
| 106 | |
| 107 | umount $MNTDIR |
| 108 | rmdir $MNTDIR |
| 109 | qemu-nbd -d $NBD |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 110 | mv $DEV_FILE_TMP $DEV_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 111 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 112 | rm -f $DEV_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 113 | |
| 114 | # clone git repositories onto the system |
| 115 | # ====================================== |
| 116 | |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 117 | IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX` |
| 118 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 119 | if [ ! -r $IMG_FILE ]; then |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 120 | NBD=`map_nbd $DEV_FILE` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 121 | |
| 122 | # Pre-create the image file |
| 123 | # FIXME(dt): This should really get the partition size to |
| 124 | # pre-create the image file |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 125 | 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] | 126 | # Create filesystem image for RAM disk |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 127 | dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 128 | |
| 129 | qemu-nbd -d $NBD |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 130 | mv $IMG_FILE_TMP $IMG_FILE |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 131 | fi |
Dean Troyer | dccd6b9 | 2011-11-01 15:46:14 -0500 | [diff] [blame] | 132 | rm -f $IMG_FILE_TMP |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 133 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 134 | MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX` |
Dean Troyer | 61be924 | 2011-10-25 22:35:23 -0500 | [diff] [blame] | 135 | mount -t ext4 -o loop $IMG_FILE $MNTDIR |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 136 | cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf |
| 137 | |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 138 | # 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] | 139 | if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then |
Dean Troyer | ea442c1 | 2011-10-26 12:34:59 -0500 | [diff] [blame] | 140 | chroot $MNTDIR apt-get install -y linux-generic |
| 141 | fi |
| 142 | |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 143 | # git clone only if directory doesn't exist already. Since ``DEST`` might not |
| 144 | # be owned by the installation user, we create the directory and change the |
| 145 | # ownership to the proper user. |
| 146 | function git_clone { |
| 147 | |
| 148 | # clone new copy or fetch latest changes |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 149 | CHECKOUT=${MNTDIR}$2 |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 150 | if [ ! -d $CHECKOUT ]; then |
| 151 | mkdir -p $CHECKOUT |
| 152 | git clone $1 $CHECKOUT |
| 153 | else |
| 154 | pushd $CHECKOUT |
| 155 | git fetch |
| 156 | popd |
| 157 | fi |
| 158 | |
| 159 | # FIXME(ja): checkout specified version (should works for branches and tags) |
| 160 | |
| 161 | pushd $CHECKOUT |
| 162 | # checkout the proper branch/tag |
| 163 | git checkout $3 |
| 164 | # force our local version to be the same as the remote version |
| 165 | git reset --hard origin/$3 |
| 166 | popd |
| 167 | |
| 168 | # give ownership to the stack user |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 169 | chroot $MNTDIR chown -R stack $2 |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 172 | git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH |
| 173 | git_clone $GLANCE_REPO $DEST/glance $GLANCE_BRANCH |
| 174 | git_clone $KEYSTONE_REPO $DEST/keystone $KEYSTONE_BRANCH |
| 175 | git_clone $NOVNC_REPO $DEST/novnc $NOVNC_BRANCH |
Tres Henry | ca85b79 | 2011-10-28 14:00:21 -0700 | [diff] [blame] | 176 | git_clone $HORIZON_REPO $DEST/horizon $HORIZON_BRANCH |
Dean Troyer | 11e5e6f | 2011-10-03 09:40:32 -0500 | [diff] [blame] | 177 | git_clone $NOVACLIENT_REPO $DEST/python-novaclient $NOVACLIENT_BRANCH |
| 178 | git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH |
Jesse Andrews | d31c4ea | 2011-09-28 02:30:57 -0700 | [diff] [blame] | 179 | |
Dean Troyer | b5da519 | 2011-10-17 13:32:06 -0500 | [diff] [blame] | 180 | # Use this version of devstack |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 181 | rm -rf $MNTDIR/$DEST/devstack |
| 182 | cp -pr $CWD $MNTDIR/$DEST/devstack |
| 183 | chroot $MNTDIR chown -R stack $DEST/devstack |
Dean Troyer | 03412c8 | 2011-10-03 09:56:41 -0500 | [diff] [blame] | 184 | |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 185 | # Configure host network for DHCP |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 186 | mkdir -p $MNTDIR/etc/network |
| 187 | cat > $MNTDIR/etc/network/interfaces <<EOF |
Dean Troyer | cf9db8d | 2011-10-03 13:42:16 -0500 | [diff] [blame] | 188 | auto lo |
| 189 | iface lo inet loopback |
| 190 | |
| 191 | auto eth0 |
| 192 | iface eth0 inet dhcp |
| 193 | EOF |
| 194 | |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 195 | # Set hostname |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 196 | echo "ramstack" >$MNTDIR/etc/hostname |
| 197 | echo "127.0.0.1 localhost ramstack" >$MNTDIR/etc/hosts |
Dean Troyer | 288f3bd | 2011-10-13 15:50:44 -0500 | [diff] [blame] | 198 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 199 | # Configure the runner |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 200 | RUN_SH=$MNTDIR/$DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 201 | cat > $RUN_SH <<EOF |
| 202 | #!/usr/bin/env bash |
| 203 | |
Dean Troyer | 7c076ee | 2011-10-13 13:20:13 -0500 | [diff] [blame] | 204 | # Get IP range |
| 205 | set \`ip addr show dev eth0 | grep inet\` |
| 206 | PREFIX=\`echo \$2 | cut -d. -f1,2,3\` |
| 207 | export FLOATING_RANGE="\$PREFIX.224/27" |
| 208 | |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 209 | # Kill any existing screens |
| 210 | killall screen |
| 211 | |
| 212 | # Run stack.sh |
| 213 | cd $DEST/devstack && \$STACKSH_PARAMS ./stack.sh > $DEST/run.sh.log |
| 214 | echo >> $DEST/run.sh.log |
| 215 | echo >> $DEST/run.sh.log |
| 216 | echo "All done! Time to start clicking." >> $DEST/run.sh.log |
| 217 | EOF |
| 218 | |
| 219 | # Make the run.sh executable |
| 220 | chmod 755 $RUN_SH |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 221 | chroot $MNTDIR chown stack $DEST/run.sh |
Dean Troyer | 8f851e7 | 2011-10-11 20:22:23 -0500 | [diff] [blame] | 222 | |
Dean Troyer | a6466e0 | 2011-10-25 17:53:24 -0500 | [diff] [blame] | 223 | umount $MNTDIR |
| 224 | rmdir $MNTDIR |