blob: 34ef719ab9181ee47682da2622d78feb22c00c83 [file] [log] [blame]
Anthony Young11889042012-01-12 17:11:56 -08001#!/bin/bash
2
John Garbuttdaadf742012-04-27 18:28:28 +01003# 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 Apte0af143b2012-04-02 15:46:53 -070015
John Garbuttdaadf742012-04-27 18:28:28 +010016# Exit on errors
17set -o errexit
18# Echo commands
19set -o xtrace
Anthony Young11889042012-01-12 17:11:56 -080020
21# This directory
22TOP_DIR=$(cd $(dirname "$0") && pwd)
23
John Garbuttdaadf742012-04-27 18:28:28 +010024# Include onexit commands
25. $TOP_DIR/scripts/on_exit.sh
26
Bob Ballc643ebb2014-02-02 09:16:20 +000027# xapi functions
28. $TOP_DIR/functions
29
Anthony Young11889042012-01-12 17:11:56 -080030# Source params - override xenrc params in your localrc to suite your taste
31source xenrc
32
John Garbuttdaadf742012-04-27 18:28:28 +010033#
34# Parameters
35#
Renuka Apte0af143b2012-04-02 15:46:53 -070036GUEST_NAME="$1"
37
Ian Wienandaee18c72014-02-21 15:35:08 +110038function _print_interface_config {
Mate Lakat5a56cd62013-06-17 13:54:43 +010039 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 Wienandaee18c72014-02-21 15:35:08 +110064function print_interfaces_config {
Mate Lakat5a56cd62013-06-17 13:54:43 +010065 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 Garbuttdaadf742012-04-27 18:28:28 +010073#
74# Mount the VDI
75#
Renuka Apte0af143b2012-04-02 15:46:53 -070076STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*")
77add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1"
Anthony Young11889042012-01-12 17:11:56 -080078
79# Make sure we have a stage
80if [ ! -d $STAGING_DIR/etc ]; then
81 echo "Stage is not properly set up!"
82 exit 1
83fi
84
Mate Lakatec06efc2013-02-01 15:16:51 +000085# Only support DHCP for now - don't support how different versions of Ubuntu handle resolv.conf
86if [ "$MGT_IP" != "dhcp" ] && [ "$PUB_IP" != "dhcp" ]; then
87 echo "Configuration without DHCP not supported"
John Garbuttd8f1a872012-06-26 11:16:38 +010088 exit 1
89fi
Anthony Young11889042012-01-12 17:11:56 -080090
91# Copy over devstack
92rm -f /tmp/devstack.tar
Renuka Apte0af143b2012-04-02 15:46:53 -070093cd $TOP_DIR/../../
94tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar .
95mkdir -p $STAGING_DIR/opt/stack/devstack
96tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack
Anthony Young11889042012-01-12 17:11:56 -080097cd $TOP_DIR
98
Jianghua Wang6e49cab2017-02-22 11:42:22 +080099# Create an systemd task for devstack
100cat >$STAGING_DIR/etc/systemd/system/devstack.service << EOF
101[Unit]
102Description=Install OpenStack by DevStack
Mate Lakat93f3b862013-09-24 17:35:00 +0100103
Jianghua Wang6e49cab2017-02-22 11:42:22 +0800104[Service]
105Type=oneshot
106RemainAfterExit=yes
107ExecStartPre=/bin/rm -f /opt/stack/runsh.succeeded
108ExecStart=/bin/su -c "/opt/stack/run.sh" stack
109StandardOutput=tty
110StandardError=tty
Mate Lakat93f3b862013-09-24 17:35:00 +0100111
Jianghua Wang6e49cab2017-02-22 11:42:22 +0800112[Install]
113WantedBy=multi-user.target
Mate Lakat93f3b862013-09-24 17:35:00 +0100114
Anthony Young11889042012-01-12 17:11:56 -0800115EOF
116
Jianghua Wang6e49cab2017-02-22 11:42:22 +0800117# enable this service
118ln -s $STAGING_DIR/etc/systemd/system/devstack.service $STAGING_DIR/etc/systemd/system/multi-user.target.wants/devstack.service
119
Anthony Young11889042012-01-12 17:11:56 -0800120# Configure the hostname
121echo $GUEST_NAME > $STAGING_DIR/etc/hostname
122
123# Hostname must resolve for rabbit
John Garbuttdaadf742012-04-27 18:28:28 +0100124HOSTS_FILE_IP=$PUB_IP
125if [ $MGT_IP != "dhcp" ]; then
126 HOSTS_FILE_IP=$MGT_IP
127fi
Anthony Young11889042012-01-12 17:11:56 -0800128cat <<EOF >$STAGING_DIR/etc/hosts
John Garbuttdaadf742012-04-27 18:28:28 +0100129$HOSTS_FILE_IP $GUEST_NAME
Anthony Young11889042012-01-12 17:11:56 -0800130127.0.0.1 localhost localhost.localdomain
131EOF
132
133# Configure the network
Mate Lakat5a56cd62013-06-17 13:54:43 +0100134print_interfaces_config > $STAGING_DIR/etc/network/interfaces
John Garbutt030fb232012-04-27 18:28:28 +0100135
Anthony Young11889042012-01-12 17:11:56 -0800136# Gracefully cp only if source file/dir exists
137function 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
144COPYENV=${COPYENV:-1}
145if [ "$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
151fi
152
153# Configure run.sh
154cat <<EOF >$STAGING_DIR/opt/stack/run.sh
155#!/bin/bash
Mate Lakat93f3b862013-09-24 17:35:00 +0100156set -eux
Bob Ball0686dae2015-01-15 12:48:26 +0000157(
158 flock -n 9 || exit 1
159
Jianghua Wang6e49cab2017-02-22 11:42:22 +0800160 sudo chown -R stack /opt/stack
161
Bob Ball0686dae2015-01-15 12:48:26 +0000162 [ -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 Wang6e49cab2017-02-22 11:42:22 +0800171
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 Ball0686dae2015-01-15 12:48:26 +0000187 rm /opt/stack/run_sh.pid
188) 9> /opt/stack/.runsh_lock
Anthony Young11889042012-01-12 17:11:56 -0800189EOF
Jianghua Wang6e49cab2017-02-22 11:42:22 +0800190
Anthony Young11889042012-01-12 17:11:56 -0800191chmod 755 $STAGING_DIR/opt/stack/run.sh