blob: 25bf58cb8a907bfa7332a0f1cac751fe935386be [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
Mate Lakat93f3b862013-09-24 17:35:00 +010099# Create an upstart job (task) for devstack, which can interact with the console
100cat >$STAGING_DIR/etc/init/devstack.conf << EOF
101start on stopped rc RUNLEVEL=[2345]
102
103console output
104task
105
106pre-start script
Bob Ballf35e9572015-01-26 14:13:02 +0000107 rm -f /opt/stack/runsh.succeeded
Mate Lakat93f3b862013-09-24 17:35:00 +0100108end script
109
110script
111 initctl stop hvc0 || true
112
113 # Read any leftover characters from standard input
114 while read -n 1 -s -t 0.1 -r ignored; do
115 true
116 done
117
118 clear
119
120 chown -R $STACK_USER /opt/stack
121
Bob Ball0686dae2015-01-15 12:48:26 +0000122 su -c "/opt/stack/run.sh" $STACK_USER
Mate Lakatda481d02013-09-26 13:57:02 +0100123
124 # Update /etc/issue
125 {
126 echo "OpenStack VM - Installed by DevStack"
127 IPADDR=\$(ip -4 address show eth0 | sed -n 's/.*inet \\([0-9\.]\\+\\).*/\1/p')
128 echo " Management IP: \$IPADDR"
129 echo -n " Devstack run: "
Bob Ballf35e9572015-01-26 14:13:02 +0000130 if [ -e /opt/stack/runsh.succeeded ]; then
Mate Lakatda481d02013-09-26 13:57:02 +0100131 echo "SUCCEEDED"
132 else
133 echo "FAILED"
134 fi
135 echo ""
136 } > /etc/issue
Mate Lakat93f3b862013-09-24 17:35:00 +0100137 initctl start hvc0 > /dev/null 2>&1
138end script
Anthony Young11889042012-01-12 17:11:56 -0800139EOF
140
Anthony Young11889042012-01-12 17:11:56 -0800141# Configure the hostname
142echo $GUEST_NAME > $STAGING_DIR/etc/hostname
143
144# Hostname must resolve for rabbit
John Garbuttdaadf742012-04-27 18:28:28 +0100145HOSTS_FILE_IP=$PUB_IP
146if [ $MGT_IP != "dhcp" ]; then
147 HOSTS_FILE_IP=$MGT_IP
148fi
Anthony Young11889042012-01-12 17:11:56 -0800149cat <<EOF >$STAGING_DIR/etc/hosts
John Garbuttdaadf742012-04-27 18:28:28 +0100150$HOSTS_FILE_IP $GUEST_NAME
Anthony Young11889042012-01-12 17:11:56 -0800151127.0.0.1 localhost localhost.localdomain
152EOF
153
154# Configure the network
Mate Lakat5a56cd62013-06-17 13:54:43 +0100155print_interfaces_config > $STAGING_DIR/etc/network/interfaces
John Garbutt030fb232012-04-27 18:28:28 +0100156
Anthony Young11889042012-01-12 17:11:56 -0800157# Gracefully cp only if source file/dir exists
158function cp_it {
159 if [ -e $1 ] || [ -d $1 ]; then
160 cp -pRL $1 $2
161 fi
162}
163
164# Copy over your ssh keys and env if desired
165COPYENV=${COPYENV:-1}
166if [ "$COPYENV" = "1" ]; then
167 cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh
168 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys
169 cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig
170 cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc
171 cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc
172fi
173
174# Configure run.sh
175cat <<EOF >$STAGING_DIR/opt/stack/run.sh
176#!/bin/bash
Mate Lakat93f3b862013-09-24 17:35:00 +0100177set -eux
Bob Ball0686dae2015-01-15 12:48:26 +0000178(
179 flock -n 9 || exit 1
180
181 [ -e /opt/stack/runsh.succeeded ] && rm /opt/stack/runsh.succeeded
182 echo \$\$ >> /opt/stack/run_sh.pid
183
184 cd /opt/stack/devstack
185 ./unstack.sh || true
186 ./stack.sh
187
188 # Got to the end - success
189 touch /opt/stack/runsh.succeeded
190 rm /opt/stack/run_sh.pid
191) 9> /opt/stack/.runsh_lock
Anthony Young11889042012-01-12 17:11:56 -0800192EOF
193chmod 755 $STAGING_DIR/opt/stack/run.sh