| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 3 | # This script is run by install_os_domU.sh | 
|  | 4 | # | 
|  | 5 | # It modifies the ubuntu image created by install_os_domU.sh | 
|  | 6 | # and previously moodified by prepare_guest_template.sh | 
|  | 7 | # | 
|  | 8 | # This script is responsible for: | 
|  | 9 | # - pushing in the DevStack code | 
|  | 10 | # - creating run.sh, to run the code on boot | 
|  | 11 | # It does this by mounting the disk image of the VM. | 
|  | 12 | # | 
|  | 13 | # The resultant image is then templated and started | 
|  | 14 | # by install_os_domU.sh | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 15 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 16 | # Exit on errors | 
|  | 17 | set -o errexit | 
|  | 18 | # Echo commands | 
|  | 19 | set -o xtrace | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 20 |  | 
|  | 21 | # This directory | 
|  | 22 | TOP_DIR=$(cd $(dirname "$0") && pwd) | 
|  | 23 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 24 | # Include onexit commands | 
|  | 25 | . $TOP_DIR/scripts/on_exit.sh | 
|  | 26 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 27 | # Source params - override xenrc params in your localrc to suite your taste | 
|  | 28 | source xenrc | 
|  | 29 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 30 | # | 
|  | 31 | # Parameters | 
|  | 32 | # | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 33 | GUEST_NAME="$1" | 
|  | 34 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 35 | # | 
|  | 36 | # Mount the VDI | 
|  | 37 | # | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 38 | STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*") | 
|  | 39 | add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1" | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 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 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 58 | # Configure dns (use same dns as dom0) | 
|  | 59 | cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf | 
|  | 60 |  | 
|  | 61 | # Copy over devstack | 
|  | 62 | rm -f /tmp/devstack.tar | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 63 | cd $TOP_DIR/../../ | 
|  | 64 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar . | 
|  | 65 | mkdir -p $STAGING_DIR/opt/stack/devstack | 
|  | 66 | tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 67 | cd $TOP_DIR | 
|  | 68 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 69 | # Run devstack on launch | 
|  | 70 | cat <<EOF >$STAGING_DIR/etc/rc.local | 
| root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame] | 71 | # network restart required for getting the right gateway | 
|  | 72 | /etc/init.d/networking restart | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 73 | chown -R stack /opt/stack | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 74 | su -c "/opt/stack/run.sh > /opt/stack/run.sh.log 2>&1" stack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 75 | exit 0 | 
|  | 76 | EOF | 
|  | 77 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 78 | # Configure the hostname | 
|  | 79 | echo $GUEST_NAME > $STAGING_DIR/etc/hostname | 
|  | 80 |  | 
|  | 81 | # Hostname must resolve for rabbit | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 82 | HOSTS_FILE_IP=$PUB_IP | 
|  | 83 | if [ $MGT_IP != "dhcp" ]; then | 
|  | 84 | HOSTS_FILE_IP=$MGT_IP | 
|  | 85 | fi | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 86 | cat <<EOF >$STAGING_DIR/etc/hosts | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 87 | $HOSTS_FILE_IP $GUEST_NAME | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 88 | 127.0.0.1 localhost localhost.localdomain | 
|  | 89 | EOF | 
|  | 90 |  | 
|  | 91 | # Configure the network | 
|  | 92 | INTERFACES=$STAGING_DIR/etc/network/interfaces | 
|  | 93 | cp $TEMPLATES_DIR/interfaces.in  $INTERFACES | 
| root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame] | 94 | if [ $VM_IP == "dhcp" ]; then | 
|  | 95 | echo 'eth1 on dhcp' | 
|  | 96 | sed -e "s,iface eth1 inet static,iface eth1 inet dhcp,g" -i $INTERFACES | 
|  | 97 | sed -e '/@ETH1_/d' -i $INTERFACES | 
|  | 98 | else | 
|  | 99 | sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES | 
|  | 100 | sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES | 
|  | 101 | fi | 
|  | 102 |  | 
|  | 103 | if [ $MGT_IP == "dhcp" ]; then | 
|  | 104 | echo 'eth2 on dhcp' | 
|  | 105 | sed -e "s,iface eth2 inet static,iface eth2 inet dhcp,g" -i $INTERFACES | 
|  | 106 | sed -e '/@ETH2_/d' -i $INTERFACES | 
|  | 107 | else | 
|  | 108 | sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES | 
|  | 109 | sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES | 
|  | 110 | fi | 
|  | 111 |  | 
|  | 112 | if [ $PUB_IP == "dhcp" ]; then | 
|  | 113 | echo 'eth3 on dhcp' | 
|  | 114 | sed -e "s,iface eth3 inet static,iface eth3 inet dhcp,g" -i $INTERFACES | 
|  | 115 | sed -e '/@ETH3_/d' -i $INTERFACES | 
|  | 116 | else | 
|  | 117 | sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES | 
|  | 118 | sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES | 
|  | 119 | fi | 
|  | 120 |  | 
| John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 121 | if [ "$ENABLE_GI" == "true" ]; then | 
|  | 122 | cat <<EOF >>$INTERFACES | 
|  | 123 | auto eth0 | 
|  | 124 | iface eth0 inet dhcp | 
|  | 125 | EOF | 
|  | 126 | fi | 
|  | 127 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 128 | # Gracefully cp only if source file/dir exists | 
|  | 129 | function cp_it { | 
|  | 130 | if [ -e $1 ] || [ -d $1 ]; then | 
|  | 131 | cp -pRL $1 $2 | 
|  | 132 | fi | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | # Copy over your ssh keys and env if desired | 
|  | 136 | COPYENV=${COPYENV:-1} | 
|  | 137 | if [ "$COPYENV" = "1" ]; then | 
|  | 138 | cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh | 
|  | 139 | cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys | 
|  | 140 | cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig | 
|  | 141 | cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc | 
|  | 142 | cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc | 
|  | 143 | fi | 
|  | 144 |  | 
|  | 145 | # Configure run.sh | 
|  | 146 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh | 
|  | 147 | #!/bin/bash | 
|  | 148 | cd /opt/stack/devstack | 
|  | 149 | killall screen | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 150 | VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=$MULTI_HOST HOST_IP_IFACE=$HOST_IP_IFACE $STACKSH_PARAMS ./stack.sh | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 151 | EOF | 
|  | 152 | chmod 755 $STAGING_DIR/opt/stack/run.sh |