| 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 |  | 
| Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 35 | function _print_interface_config() { | 
|  | 36 | local device_nr | 
|  | 37 | local ip_address | 
|  | 38 | local netmask | 
|  | 39 |  | 
|  | 40 | device_nr="$1" | 
|  | 41 | ip_address="$2" | 
|  | 42 | netmask="$3" | 
|  | 43 |  | 
|  | 44 | local device | 
|  | 45 |  | 
|  | 46 | device="eth${device_nr}" | 
|  | 47 |  | 
|  | 48 | echo "auto $device" | 
|  | 49 | if [ $ip_address == "dhcp" ]; then | 
|  | 50 | echo "iface $device inet dhcp" | 
|  | 51 | else | 
|  | 52 | echo "iface $device inet static" | 
|  | 53 | echo "  address $ip_address" | 
|  | 54 | echo "  netmask $netmask" | 
|  | 55 | fi | 
|  | 56 |  | 
|  | 57 | # Turn off tx checksumming for better performance | 
|  | 58 | echo "  post-up ethtool -K $device tx off" | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | function print_interfaces_config() { | 
|  | 62 | echo "auto lo" | 
|  | 63 | echo "iface lo inet loopback" | 
|  | 64 |  | 
|  | 65 | _print_interface_config $PUB_DEV_NR $PUB_IP $PUB_NETMASK | 
|  | 66 | _print_interface_config $VM_DEV_NR $VM_IP $VM_NETMASK | 
|  | 67 | _print_interface_config $MGT_DEV_NR $MGT_IP $MGT_NETMASK | 
|  | 68 | } | 
|  | 69 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 70 | # | 
|  | 71 | # Mount the VDI | 
|  | 72 | # | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 73 | STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*") | 
|  | 74 | 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] | 75 |  | 
|  | 76 | # Make sure we have a stage | 
|  | 77 | if [ ! -d $STAGING_DIR/etc ]; then | 
|  | 78 | echo "Stage is not properly set up!" | 
|  | 79 | exit 1 | 
|  | 80 | fi | 
|  | 81 |  | 
| Mate Lakat | ec06efc | 2013-02-01 15:16:51 +0000 | [diff] [blame] | 82 | # Only support DHCP for now - don't support how different versions of Ubuntu handle resolv.conf | 
|  | 83 | if [ "$MGT_IP" != "dhcp" ] && [ "$PUB_IP" != "dhcp" ]; then | 
|  | 84 | echo "Configuration without DHCP not supported" | 
| John Garbutt | d8f1a87 | 2012-06-26 11:16:38 +0100 | [diff] [blame] | 85 | exit 1 | 
|  | 86 | fi | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 87 |  | 
|  | 88 | # Copy over devstack | 
|  | 89 | rm -f /tmp/devstack.tar | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 90 | cd $TOP_DIR/../../ | 
|  | 91 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar . | 
|  | 92 | mkdir -p $STAGING_DIR/opt/stack/devstack | 
|  | 93 | tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 94 | cd $TOP_DIR | 
|  | 95 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 96 | # Run devstack on launch | 
|  | 97 | cat <<EOF >$STAGING_DIR/etc/rc.local | 
| root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame] | 98 | # network restart required for getting the right gateway | 
|  | 99 | /etc/init.d/networking restart | 
| Dean Troyer | 74759aa | 2013-01-24 14:19:55 -0600 | [diff] [blame] | 100 | chown -R $STACK_USER /opt/stack | 
|  | 101 | su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" $STACK_USER | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 102 | exit 0 | 
|  | 103 | EOF | 
|  | 104 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 105 | # Configure the hostname | 
|  | 106 | echo $GUEST_NAME > $STAGING_DIR/etc/hostname | 
|  | 107 |  | 
|  | 108 | # Hostname must resolve for rabbit | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 109 | HOSTS_FILE_IP=$PUB_IP | 
|  | 110 | if [ $MGT_IP != "dhcp" ]; then | 
|  | 111 | HOSTS_FILE_IP=$MGT_IP | 
|  | 112 | fi | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 113 | cat <<EOF >$STAGING_DIR/etc/hosts | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 114 | $HOSTS_FILE_IP $GUEST_NAME | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 115 | 127.0.0.1 localhost localhost.localdomain | 
|  | 116 | EOF | 
|  | 117 |  | 
|  | 118 | # Configure the network | 
| Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 119 | print_interfaces_config > $STAGING_DIR/etc/network/interfaces | 
| John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 120 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 121 | # Gracefully cp only if source file/dir exists | 
|  | 122 | function cp_it { | 
|  | 123 | if [ -e $1 ] || [ -d $1 ]; then | 
|  | 124 | cp -pRL $1 $2 | 
|  | 125 | fi | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | # Copy over your ssh keys and env if desired | 
|  | 129 | COPYENV=${COPYENV:-1} | 
|  | 130 | if [ "$COPYENV" = "1" ]; then | 
|  | 131 | cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh | 
|  | 132 | cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys | 
|  | 133 | cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig | 
|  | 134 | cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc | 
|  | 135 | cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc | 
|  | 136 | fi | 
|  | 137 |  | 
|  | 138 | # Configure run.sh | 
|  | 139 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh | 
|  | 140 | #!/bin/bash | 
|  | 141 | cd /opt/stack/devstack | 
|  | 142 | killall screen | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 143 | 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] | 144 | EOF | 
|  | 145 | chmod 755 $STAGING_DIR/opt/stack/run.sh |