| 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 |  | 
| Bob Ball | c643ebb | 2014-02-02 09:16:20 +0000 | [diff] [blame] | 24 | # Source lower level functions | 
|  | 25 | . $TOP_DIR/../../functions | 
|  | 26 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 27 | # Include onexit commands | 
|  | 28 | . $TOP_DIR/scripts/on_exit.sh | 
|  | 29 |  | 
| Bob Ball | c643ebb | 2014-02-02 09:16:20 +0000 | [diff] [blame] | 30 | # xapi functions | 
|  | 31 | . $TOP_DIR/functions | 
|  | 32 |  | 
|  | 33 | # Determine what system we are running on. | 
|  | 34 | # Might not be XenServer if we're using xenserver-core | 
|  | 35 | GetDistro | 
|  | 36 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 37 | # Source params - override xenrc params in your localrc to suite your taste | 
|  | 38 | source xenrc | 
|  | 39 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 40 | # | 
|  | 41 | # Parameters | 
|  | 42 | # | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 43 | GUEST_NAME="$1" | 
|  | 44 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 45 | function _print_interface_config { | 
| Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 46 | local device_nr | 
|  | 47 | local ip_address | 
|  | 48 | local netmask | 
|  | 49 |  | 
|  | 50 | device_nr="$1" | 
|  | 51 | ip_address="$2" | 
|  | 52 | netmask="$3" | 
|  | 53 |  | 
|  | 54 | local device | 
|  | 55 |  | 
|  | 56 | device="eth${device_nr}" | 
|  | 57 |  | 
|  | 58 | echo "auto $device" | 
|  | 59 | if [ $ip_address == "dhcp" ]; then | 
|  | 60 | echo "iface $device inet dhcp" | 
|  | 61 | else | 
|  | 62 | echo "iface $device inet static" | 
|  | 63 | echo "  address $ip_address" | 
|  | 64 | echo "  netmask $netmask" | 
|  | 65 | fi | 
|  | 66 |  | 
|  | 67 | # Turn off tx checksumming for better performance | 
|  | 68 | echo "  post-up ethtool -K $device tx off" | 
|  | 69 | } | 
|  | 70 |  | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 71 | function print_interfaces_config { | 
| Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 72 | echo "auto lo" | 
|  | 73 | echo "iface lo inet loopback" | 
|  | 74 |  | 
|  | 75 | _print_interface_config $PUB_DEV_NR $PUB_IP $PUB_NETMASK | 
|  | 76 | _print_interface_config $VM_DEV_NR $VM_IP $VM_NETMASK | 
|  | 77 | _print_interface_config $MGT_DEV_NR $MGT_IP $MGT_NETMASK | 
|  | 78 | } | 
|  | 79 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 80 | # | 
|  | 81 | # Mount the VDI | 
|  | 82 | # | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 83 | STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*") | 
|  | 84 | 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] | 85 |  | 
|  | 86 | # Make sure we have a stage | 
|  | 87 | if [ ! -d $STAGING_DIR/etc ]; then | 
|  | 88 | echo "Stage is not properly set up!" | 
|  | 89 | exit 1 | 
|  | 90 | fi | 
|  | 91 |  | 
| Mate Lakat | ec06efc | 2013-02-01 15:16:51 +0000 | [diff] [blame] | 92 | # Only support DHCP for now - don't support how different versions of Ubuntu handle resolv.conf | 
|  | 93 | if [ "$MGT_IP" != "dhcp" ] && [ "$PUB_IP" != "dhcp" ]; then | 
|  | 94 | echo "Configuration without DHCP not supported" | 
| John Garbutt | d8f1a87 | 2012-06-26 11:16:38 +0100 | [diff] [blame] | 95 | exit 1 | 
|  | 96 | fi | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 97 |  | 
|  | 98 | # Copy over devstack | 
|  | 99 | rm -f /tmp/devstack.tar | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 100 | cd $TOP_DIR/../../ | 
|  | 101 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar . | 
|  | 102 | mkdir -p $STAGING_DIR/opt/stack/devstack | 
|  | 103 | tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 104 | cd $TOP_DIR | 
|  | 105 |  | 
| Mate Lakat | 93f3b86 | 2013-09-24 17:35:00 +0100 | [diff] [blame] | 106 | # Create an upstart job (task) for devstack, which can interact with the console | 
|  | 107 | cat >$STAGING_DIR/etc/init/devstack.conf << EOF | 
|  | 108 | start on stopped rc RUNLEVEL=[2345] | 
|  | 109 |  | 
|  | 110 | console output | 
|  | 111 | task | 
|  | 112 |  | 
|  | 113 | pre-start script | 
|  | 114 | rm -f /var/run/devstack.succeeded | 
|  | 115 | end script | 
|  | 116 |  | 
|  | 117 | script | 
|  | 118 | initctl stop hvc0 || true | 
|  | 119 |  | 
|  | 120 | # Read any leftover characters from standard input | 
|  | 121 | while read -n 1 -s -t 0.1 -r ignored; do | 
|  | 122 | true | 
|  | 123 | done | 
|  | 124 |  | 
|  | 125 | clear | 
|  | 126 |  | 
|  | 127 | chown -R $STACK_USER /opt/stack | 
|  | 128 |  | 
|  | 129 | if su -c "/opt/stack/run.sh" $STACK_USER; then | 
|  | 130 | touch /var/run/devstack.succeeded | 
|  | 131 | fi | 
| Mate Lakat | da481d0 | 2013-09-26 13:57:02 +0100 | [diff] [blame] | 132 |  | 
|  | 133 | # Update /etc/issue | 
|  | 134 | { | 
|  | 135 | echo "OpenStack VM - Installed by DevStack" | 
|  | 136 | IPADDR=\$(ip -4 address show eth0 | sed -n 's/.*inet \\([0-9\.]\\+\\).*/\1/p') | 
|  | 137 | echo "  Management IP:   \$IPADDR" | 
|  | 138 | echo -n "  Devstack run:    " | 
|  | 139 | if [ -e /var/run/devstack.succeeded ]; then | 
|  | 140 | echo "SUCCEEDED" | 
|  | 141 | else | 
|  | 142 | echo "FAILED" | 
|  | 143 | fi | 
|  | 144 | echo "" | 
|  | 145 | } > /etc/issue | 
| Mate Lakat | 93f3b86 | 2013-09-24 17:35:00 +0100 | [diff] [blame] | 146 | initctl start hvc0 > /dev/null 2>&1 | 
|  | 147 | end script | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 148 | EOF | 
|  | 149 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 150 | # Configure the hostname | 
|  | 151 | echo $GUEST_NAME > $STAGING_DIR/etc/hostname | 
|  | 152 |  | 
|  | 153 | # Hostname must resolve for rabbit | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 154 | HOSTS_FILE_IP=$PUB_IP | 
|  | 155 | if [ $MGT_IP != "dhcp" ]; then | 
|  | 156 | HOSTS_FILE_IP=$MGT_IP | 
|  | 157 | fi | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 158 | cat <<EOF >$STAGING_DIR/etc/hosts | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 159 | $HOSTS_FILE_IP $GUEST_NAME | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 160 | 127.0.0.1 localhost localhost.localdomain | 
|  | 161 | EOF | 
|  | 162 |  | 
|  | 163 | # Configure the network | 
| Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 164 | print_interfaces_config > $STAGING_DIR/etc/network/interfaces | 
| John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 165 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 166 | # Gracefully cp only if source file/dir exists | 
|  | 167 | function cp_it { | 
|  | 168 | if [ -e $1 ] || [ -d $1 ]; then | 
|  | 169 | cp -pRL $1 $2 | 
|  | 170 | fi | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | # Copy over your ssh keys and env if desired | 
|  | 174 | COPYENV=${COPYENV:-1} | 
|  | 175 | if [ "$COPYENV" = "1" ]; then | 
|  | 176 | cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh | 
|  | 177 | cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys | 
|  | 178 | cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig | 
|  | 179 | cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc | 
|  | 180 | cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc | 
|  | 181 | fi | 
|  | 182 |  | 
|  | 183 | # Configure run.sh | 
|  | 184 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh | 
|  | 185 | #!/bin/bash | 
| Mate Lakat | 93f3b86 | 2013-09-24 17:35:00 +0100 | [diff] [blame] | 186 | set -eux | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 187 | cd /opt/stack/devstack | 
| Mate Lakat | 93f3b86 | 2013-09-24 17:35:00 +0100 | [diff] [blame] | 188 | ./unstack.sh || true | 
|  | 189 | ./stack.sh | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 190 | EOF | 
|  | 191 | chmod 755 $STAGING_DIR/opt/stack/run.sh |