Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Abort if localrc is not set |
| 4 | if [ ! -e ../../localrc ]; then |
| 5 | echo "You must have a localrc with ALL necessary passwords defined before proceeding." |
| 6 | echo "See the xen README for required passwords." |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | # Echo commands |
| 11 | set -o xtrace |
| 12 | |
| 13 | # Name of this guest |
| 14 | GUEST_NAME=${GUEST_NAME:-ALLINONE} |
| 15 | |
| 16 | # dom0 ip |
| 17 | HOST_IP=${HOST_IP:-`ifconfig xenbr0 | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"`} |
| 18 | |
| 19 | # Our nova host's network info |
| 20 | MGT_IP=${MGT_IP:-172.16.100.1} |
| 21 | PUB_IP=${PUB_IP:-192.168.1.55} |
| 22 | |
| 23 | # Public network |
| 24 | PUB_BR=${PUB_BR:-xenbr0} |
| 25 | PUB_NETMASK=${PUB_NETMASK:-255.255.255.0} |
| 26 | |
| 27 | # VM network params |
| 28 | VM_NETMASK=${VM_NETMASK:-255.255.255.0} |
| 29 | VM_BR=${VM_BR:-xapi1} |
| 30 | VM_VLAN=${VM_VLAN:-100} |
| 31 | |
| 32 | # MGMT network params |
| 33 | MGT_NETMASK=${MGT_NETMASK:-255.255.255.0} |
| 34 | MGT_BR=${MGT_BR:-xapi2} |
| 35 | MGT_VLAN=${MGT_VLAN:-101} |
| 36 | |
| 37 | # VM Password |
| 38 | PASSWORD=${PASSWORD:-secrete} |
| 39 | |
| 40 | # Size of image |
| 41 | VDI_MB=${VDI_MB:-2500} |
| 42 | |
| 43 | # This directory |
| 44 | TOP_DIR=$(cd $(dirname "$0") && pwd) |
| 45 | |
| 46 | # Make sure we have git |
| 47 | if ! which git; then |
| 48 | GITDIR=/tmp/git-1.7.7 |
| 49 | cd /tmp |
| 50 | rm -rf $GITDIR* |
| 51 | wget http://git-core.googlecode.com/files/git-1.7.7.tar.gz |
| 52 | tar xfv git-1.7.7.tar.gz |
| 53 | cd $GITDIR |
| 54 | ./configure |
| 55 | make install |
| 56 | cd $TOP_DIR |
| 57 | fi |
| 58 | |
| 59 | # Helper to create networks |
| 60 | function create_network() { |
| 61 | if ! xe network-list | grep bridge | grep -q $1; then |
| 62 | echo "Creating bridge $1" |
| 63 | xe network-create name-label=$1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # Create host, vm, mgmt, pub networks |
| 68 | create_network xapi0 |
| 69 | create_network $VM_BR |
| 70 | create_network $MGT_BR |
| 71 | create_network $PUB_BR |
| 72 | |
| 73 | # Get the uuid for our physical (public) interface |
| 74 | PIF=`xe pif-list --minimal device=eth0` |
| 75 | |
| 76 | # Create networks/bridges for vm and management |
| 77 | VM_NET=`xe network-list --minimal bridge=$VM_BR` |
| 78 | MGT_NET=`xe network-list --minimal bridge=$MGT_BR` |
| 79 | |
| 80 | # Helper to create vlans |
| 81 | function create_vlan() { |
| 82 | pif=$1 |
| 83 | vlan=$2 |
| 84 | net=$3 |
| 85 | if ! xe vlan-list | grep tag | grep -q $vlan; then |
| 86 | xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net |
| 87 | fi |
| 88 | } |
| 89 | |
| 90 | # Create vlans for vm and management |
| 91 | create_vlan $PIF $VM_VLAN $VM_NET |
| 92 | create_vlan $PIF $MGT_VLAN $MGT_NET |
| 93 | |
| 94 | # Setup host-only nat rules |
| 95 | HOST_NET=169.254.0.0/16 |
| 96 | if ! iptables -L -v -t nat | grep -q $HOST_NET; then |
| 97 | iptables -t nat -A POSTROUTING -s $HOST_NET -j SNAT --to-source $HOST_IP |
| 98 | iptables -I FORWARD 1 -s $HOST_NET -j ACCEPT |
| 99 | /etc/init.d/iptables save |
| 100 | fi |
| 101 | |
| 102 | # Set up ip forwarding |
| 103 | if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then |
| 104 | # FIXME: This doesn't work on reboot! |
| 105 | echo "FORWARD_IPV4=YES" >> /etc/sysconfig/network |
| 106 | fi |
| 107 | |
| 108 | # Also, enable ip forwarding in rc.local, since the above trick isn't working |
| 109 | if ! grep -q "echo 1 >/proc/sys/net/ipv4/ip_forward" /etc/rc.local; then |
| 110 | echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.local |
| 111 | fi |
| 112 | |
| 113 | # Enable ip forwarding at runtime as well |
| 114 | echo 1 > /proc/sys/net/ipv4/ip_forward |
| 115 | |
| 116 | # Directory where we stage the build |
| 117 | STAGING_DIR=$TOP_DIR/stage |
| 118 | |
| 119 | # Option to clean out old stuff |
| 120 | CLEAN=${CLEAN:-0} |
| 121 | if [ "$CLEAN" = "1" ]; then |
| 122 | rm -rf $STAGING_DIR |
| 123 | fi |
| 124 | |
| 125 | # Download our base image. This image is made using prepare_guest.sh |
| 126 | BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz} |
| 127 | if [ ! -e $STAGING_DIR ]; then |
| 128 | if [ ! -e /tmp/stage.tgz ]; then |
| 129 | wget $BASE_IMAGE_URL -O /tmp/stage.tgz |
| 130 | fi |
| 131 | tar xfz /tmp/stage.tgz |
| 132 | cd $TOP_DIR |
| 133 | fi |
| 134 | |
| 135 | # Free up precious disk space |
| 136 | rm -f /tmp/stage.tgz |
| 137 | |
| 138 | # Make sure we have a stage |
| 139 | if [ ! -d $STAGING_DIR/etc ]; then |
| 140 | echo "Stage is not properly set up!" |
| 141 | exit 1 |
| 142 | fi |
| 143 | |
| 144 | # Directory where our conf files are stored |
| 145 | FILES_DIR=$TOP_DIR/files |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 146 | TEMPLATES_DIR=$TOP_DIR/templates |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 147 | |
| 148 | # Directory for supporting script files |
| 149 | SCRIPT_DIR=$TOP_DIR/scripts |
| 150 | |
| 151 | # Version of ubuntu with which we are working |
| 152 | UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"` |
| 153 | KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"` |
| 154 | |
| 155 | # Setup fake grub |
| 156 | rm -rf $STAGING_DIR/boot/grub/ |
| 157 | mkdir -p $STAGING_DIR/boot/grub/ |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 158 | cp $TEMPLATES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 159 | sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst |
| 160 | |
| 161 | # Setup fstab, tty, and other system stuff |
| 162 | cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab |
| 163 | cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/ |
| 164 | |
| 165 | # Put the VPX into UTC. |
| 166 | rm -f $STAGING_DIR/etc/localtime |
| 167 | |
| 168 | # Helper to set hostname |
| 169 | function set_hostname() { |
| 170 | echo $1 > $STAGING_DIR/etc/hostname |
| 171 | } |
| 172 | |
| 173 | # We need a resolvable host name for rabbit to launch |
| 174 | if ! grep -q $GUEST_NAME $STAGING_DIR/etc/hosts; then |
| 175 | echo "$MGT_IP $GUEST_NAME" >> $STAGING_DIR/etc/hosts |
| 176 | fi |
| 177 | |
| 178 | # Configre hosts file |
| 179 | cat <<EOF >$STAGING_DIR/etc/hosts |
| 180 | $MGT_IP $GUEST_NAME |
| 181 | 127.0.0.1 localhost localhost.localdomain |
| 182 | EOF |
| 183 | |
| 184 | # Configure dns (use same dns as dom0) |
| 185 | cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf |
| 186 | |
| 187 | # Copy over devstack |
| 188 | rm -f /tmp/devstack.tar |
| 189 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack |
| 190 | cd $STAGING_DIR/opt/stack/ |
| 191 | tar xf /tmp/devstack.tar |
| 192 | cd $TOP_DIR |
| 193 | |
| 194 | # Configure OVA |
| 195 | VDI_SIZE=$(($VDI_MB*1024*1024)) |
| 196 | PRODUCT_BRAND=${PRODUCT_BRAND:-openstack} |
| 197 | PRODUCT_VERSION=${PRODUCT_VERSION:-001} |
| 198 | BUILD_NUMBER=${BUILD_NUMBER:-001} |
| 199 | LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER" |
| 200 | OVA=$STAGING_DIR/tmp/ova.xml |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 201 | cp $TEMPLATES_DIR/ova.xml.in $OVA |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 202 | sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA |
| 203 | sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA |
| 204 | sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA |
| 205 | sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA |
| 206 | |
| 207 | # Directory for xvas |
| 208 | XVA_DIR=$TOP_DIR/xvas |
| 209 | |
| 210 | # Create xva dir |
| 211 | mkdir -p $XVA_DIR |
| 212 | |
| 213 | # Clean nova if desired |
| 214 | if [ "$CLEAN" = "1" ]; then |
| 215 | rm -rf $TOP_DIR/nova |
| 216 | fi |
| 217 | |
| 218 | # Checkout nova |
| 219 | if [ ! -d $TOP_DIR/nova ]; then |
| 220 | git clone git://github.com/cloudbuilders/nova.git |
| 221 | git checkout diablo |
| 222 | fi |
| 223 | |
| 224 | # Run devstack on launch |
| 225 | cat <<EOF >$STAGING_DIR/etc/rc.local |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 226 | GUEST_PASSWORD=$GUEST_PASSWORD $STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 227 | su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack |
| 228 | exit 0 |
| 229 | EOF |
| 230 | |
| 231 | # Install plugins |
| 232 | cp -pr $TOP_DIR/nova/plugins/xenserver/xenapi/etc/xapi.d /etc/ |
| 233 | chmod a+x /etc/xapi.d/plugins/* |
| 234 | yum --enablerepo=base install -y parted |
| 235 | mkdir -p /boot/guest |
| 236 | |
| 237 | # Set local storage il8n |
| 238 | SR_UUID=`xe sr-list --minimal name-label="Local storage"` |
| 239 | xe sr-param-set uuid=$SR_UUID other-config:i18n-key=local-storage |
| 240 | |
| 241 | # Uninstall previous runs |
| 242 | xe vm-list --minimal name-label="$LABEL" | xargs ./scripts/uninstall-os-vpx.sh |
| 243 | |
| 244 | # Destroy any instances that were launched |
| 245 | for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do |
| 246 | echo "Shutting down nova instance $uuid" |
| 247 | xe vm-shutdown uuid=$uuid |
| 248 | xe vm-destroy uuid=$uuid |
| 249 | done |
| 250 | |
| 251 | # Path to head xva. By default keep overwriting the same one to save space |
| 252 | USE_SEPARATE_XVAS=${USE_SEPARATE_XVAS:-0} |
| 253 | if [ "$USE_SEPARATE_XVAS" = "0" ]; then |
| 254 | XVA=$XVA_DIR/$UBUNTU_VERSION.xva |
| 255 | else |
| 256 | XVA=$XVA_DIR/$UBUNTU_VERSION.$GUEST_NAME.xva |
| 257 | fi |
| 258 | |
| 259 | # Clean old xva. In the future may not do this every time. |
| 260 | rm -f $XVA |
| 261 | |
| 262 | # Configure the network |
| 263 | set_hostname $GUEST_NAME |
| 264 | INTERFACES=$STAGING_DIR/etc/network/interfaces |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 265 | cp $TEMPLATES_DIR/interfaces.in $INTERFACES |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 266 | sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES |
| 267 | sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES |
| 268 | sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES |
| 269 | sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES |
| 270 | sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES |
| 271 | |
| 272 | # Configure run.sh |
| 273 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh |
| 274 | #!/bin/bash |
| 275 | cd /opt/stack/devstack |
| 276 | killall screen |
| 277 | UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 $STACKSH_PARAMS ./stack.sh |
| 278 | EOF |
| 279 | chmod 755 $STAGING_DIR/opt/stack/run.sh |
| 280 | |
| 281 | # Create xva |
| 282 | if [ ! -e $XVA ]; then |
| 283 | rm -rf /tmp/mkxva* |
| 284 | UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/ |
| 285 | fi |
| 286 | |
| 287 | # Start guest |
| 288 | $TOP_DIR/scripts/install-os-vpx.sh -f $XVA -v $VM_BR -m $MGT_BR -p $PUB_BR |
Anthony Young | 3eb8f59 | 2011-10-26 23:11:52 -0700 | [diff] [blame^] | 289 | |
| 290 | echo "################################################################################" |
| 291 | echo "" |
| 292 | echo "All Finished!" |
| 293 | echo "Now, you can monitor the progress of the stack.sh installation by " |
| 294 | echo "tailing /opt/stack/run.sh.log from within your domU." |
| 295 | echo "" |
| 296 | echo "ssh into your domU now: 'ssh stack@$PUB_IP' using your password" |
| 297 | echo "and then do: 'tail -f /opt/stack/run.sh.log'" |
| 298 | echo "" |
| 299 | echo "When the script completes, you can then visit the OpenStack Dashboard" |
| 300 | echo "at http://$PUB_IP, and contact other services at the usual ports." |