Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [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 | # This directory |
| 11 | TOP_DIR=$(cd $(dirname "$0") && pwd) |
| 12 | |
| 13 | # Source params - override xenrc params in your localrc to suite your taste |
| 14 | source xenrc |
| 15 | |
| 16 | # Echo commands |
| 17 | set -o xtrace |
| 18 | |
| 19 | # Directory where we stage the build |
| 20 | STAGING_DIR=$TOP_DIR/stage |
| 21 | |
| 22 | # Option to clean out old stuff |
| 23 | CLEAN=${CLEAN:-0} |
| 24 | if [ "$CLEAN" = "1" ]; then |
| 25 | rm -rf $STAGING_DIR |
| 26 | fi |
| 27 | |
| 28 | # Download our base image. This image is made using prepare_guest.sh |
| 29 | BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz} |
| 30 | if [ ! -e $STAGING_DIR ]; then |
| 31 | if [ ! -e /tmp/stage.tgz ]; then |
| 32 | wget $BASE_IMAGE_URL -O /tmp/stage.tgz |
| 33 | fi |
| 34 | tar xfz /tmp/stage.tgz |
| 35 | cd $TOP_DIR |
| 36 | fi |
| 37 | |
| 38 | # Free up precious disk space |
| 39 | rm -f /tmp/stage.tgz |
| 40 | |
| 41 | # Make sure we have a stage |
| 42 | if [ ! -d $STAGING_DIR/etc ]; then |
| 43 | echo "Stage is not properly set up!" |
| 44 | exit 1 |
| 45 | fi |
| 46 | |
| 47 | # Directory where our conf files are stored |
| 48 | FILES_DIR=$TOP_DIR/files |
| 49 | TEMPLATES_DIR=$TOP_DIR/templates |
| 50 | |
| 51 | # Directory for supporting script files |
| 52 | SCRIPT_DIR=$TOP_DIR/scripts |
| 53 | |
| 54 | # Version of ubuntu with which we are working |
| 55 | UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"` |
| 56 | KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"` |
| 57 | |
| 58 | # Directory for xvas |
| 59 | XVA_DIR=$TOP_DIR/xvas |
| 60 | |
| 61 | # Create xva dir |
| 62 | mkdir -p $XVA_DIR |
| 63 | |
| 64 | # Path to xva |
root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame^] | 65 | XVA=$XVA_DIR/$GUEST_NAME.xva |
Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 66 | |
| 67 | # Setup fake grub |
| 68 | rm -rf $STAGING_DIR/boot/grub/ |
| 69 | mkdir -p $STAGING_DIR/boot/grub/ |
| 70 | cp $TEMPLATES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst |
| 71 | sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst |
| 72 | |
| 73 | # Setup fstab, tty, and other system stuff |
| 74 | cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab |
| 75 | cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/ |
| 76 | |
| 77 | # Put the VPX into UTC. |
| 78 | rm -f $STAGING_DIR/etc/localtime |
| 79 | |
| 80 | # Configure dns (use same dns as dom0) |
| 81 | cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf |
| 82 | |
| 83 | # Copy over devstack |
| 84 | rm -f /tmp/devstack.tar |
| 85 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack |
| 86 | cd $STAGING_DIR/opt/stack/ |
| 87 | tar xf /tmp/devstack.tar |
| 88 | cd $TOP_DIR |
| 89 | |
| 90 | # Configure OVA |
| 91 | VDI_SIZE=$(($VDI_MB*1024*1024)) |
| 92 | PRODUCT_BRAND=${PRODUCT_BRAND:-openstack} |
| 93 | PRODUCT_VERSION=${PRODUCT_VERSION:-001} |
| 94 | BUILD_NUMBER=${BUILD_NUMBER:-001} |
| 95 | LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER" |
| 96 | OVA=$STAGING_DIR/tmp/ova.xml |
| 97 | cp $TEMPLATES_DIR/ova.xml.in $OVA |
| 98 | sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA |
| 99 | sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA |
| 100 | sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA |
| 101 | sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA |
| 102 | |
| 103 | # Run devstack on launch |
| 104 | cat <<EOF >$STAGING_DIR/etc/rc.local |
root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame^] | 105 | # network restart required for getting the right gateway |
| 106 | /etc/init.d/networking restart |
Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 107 | GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh |
| 108 | su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack |
| 109 | exit 0 |
| 110 | EOF |
| 111 | |
| 112 | # Clean old xva. In the future may not do this every time. |
| 113 | rm -f $XVA |
| 114 | |
| 115 | # Configure the hostname |
| 116 | echo $GUEST_NAME > $STAGING_DIR/etc/hostname |
| 117 | |
| 118 | # Hostname must resolve for rabbit |
| 119 | cat <<EOF >$STAGING_DIR/etc/hosts |
| 120 | $MGT_IP $GUEST_NAME |
| 121 | 127.0.0.1 localhost localhost.localdomain |
| 122 | EOF |
| 123 | |
| 124 | # Configure the network |
| 125 | INTERFACES=$STAGING_DIR/etc/network/interfaces |
| 126 | cp $TEMPLATES_DIR/interfaces.in $INTERFACES |
root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame^] | 127 | if [ $VM_IP == "dhcp" ]; then |
| 128 | echo 'eth1 on dhcp' |
| 129 | sed -e "s,iface eth1 inet static,iface eth1 inet dhcp,g" -i $INTERFACES |
| 130 | sed -e '/@ETH1_/d' -i $INTERFACES |
| 131 | else |
| 132 | sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES |
| 133 | sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES |
| 134 | fi |
| 135 | |
| 136 | if [ $MGT_IP == "dhcp" ]; then |
| 137 | echo 'eth2 on dhcp' |
| 138 | sed -e "s,iface eth2 inet static,iface eth2 inet dhcp,g" -i $INTERFACES |
| 139 | sed -e '/@ETH2_/d' -i $INTERFACES |
| 140 | else |
| 141 | sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES |
| 142 | sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES |
| 143 | fi |
| 144 | |
| 145 | if [ $PUB_IP == "dhcp" ]; then |
| 146 | echo 'eth3 on dhcp' |
| 147 | sed -e "s,iface eth3 inet static,iface eth3 inet dhcp,g" -i $INTERFACES |
| 148 | sed -e '/@ETH3_/d' -i $INTERFACES |
| 149 | else |
| 150 | sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES |
| 151 | sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES |
| 152 | fi |
| 153 | |
| 154 | if [ -h $STAGING_DIR/sbin/dhclient3 ]; then |
| 155 | rm -f $STAGING_DIR/sbin/dhclient3 |
| 156 | fi |
Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 157 | |
| 158 | # Gracefully cp only if source file/dir exists |
| 159 | function cp_it { |
| 160 | if [ -e $1 ] || [ -d $1 ]; then |
| 161 | cp -pRL $1 $2 |
| 162 | fi |
| 163 | } |
| 164 | |
| 165 | # Copy over your ssh keys and env if desired |
| 166 | COPYENV=${COPYENV:-1} |
| 167 | if [ "$COPYENV" = "1" ]; then |
| 168 | cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh |
| 169 | cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys |
| 170 | cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig |
| 171 | cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc |
| 172 | cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc |
| 173 | fi |
| 174 | |
| 175 | # Configure run.sh |
| 176 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh |
| 177 | #!/bin/bash |
| 178 | cd /opt/stack/devstack |
| 179 | killall screen |
root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame^] | 180 | UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 HOST_IP_IFACE=$HOST_IP_IFACE $STACKSH_PARAMS ./stack.sh |
Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 181 | EOF |
| 182 | chmod 755 $STAGING_DIR/opt/stack/run.sh |
| 183 | |
| 184 | # Create xva |
| 185 | if [ ! -e $XVA ]; then |
| 186 | rm -rf /tmp/mkxva* |
| 187 | UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/ |
| 188 | fi |
| 189 | |
| 190 | echo "Built $XVA. If your dom0 is on a different machine, copy this to [devstackdir]/tools/xen/$XVA" |