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