blob: 7c8e620f44fecad3a797239f15bbed8a1d967b92 [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
107 rm -f /var/run/devstack.succeeded
108end 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
122 if su -c "/opt/stack/run.sh" $STACK_USER; then
123 touch /var/run/devstack.succeeded
124 fi
Mate Lakatda481d02013-09-26 13:57:02 +0100125
126 # Update /etc/issue
127 {
128 echo "OpenStack VM - Installed by DevStack"
129 IPADDR=\$(ip -4 address show eth0 | sed -n 's/.*inet \\([0-9\.]\\+\\).*/\1/p')
130 echo " Management IP: \$IPADDR"
131 echo -n " Devstack run: "
132 if [ -e /var/run/devstack.succeeded ]; then
133 echo "SUCCEEDED"
134 else
135 echo "FAILED"
136 fi
137 echo ""
138 } > /etc/issue
Mate Lakat93f3b862013-09-24 17:35:00 +0100139 initctl start hvc0 > /dev/null 2>&1
140end script
Anthony Young11889042012-01-12 17:11:56 -0800141EOF
142
Anthony Young11889042012-01-12 17:11:56 -0800143# Configure the hostname
144echo $GUEST_NAME > $STAGING_DIR/etc/hostname
145
146# Hostname must resolve for rabbit
John Garbuttdaadf742012-04-27 18:28:28 +0100147HOSTS_FILE_IP=$PUB_IP
148if [ $MGT_IP != "dhcp" ]; then
149 HOSTS_FILE_IP=$MGT_IP
150fi
Anthony Young11889042012-01-12 17:11:56 -0800151cat <<EOF >$STAGING_DIR/etc/hosts
John Garbuttdaadf742012-04-27 18:28:28 +0100152$HOSTS_FILE_IP $GUEST_NAME
Anthony Young11889042012-01-12 17:11:56 -0800153127.0.0.1 localhost localhost.localdomain
154EOF
155
156# Configure the network
Mate Lakat5a56cd62013-06-17 13:54:43 +0100157print_interfaces_config > $STAGING_DIR/etc/network/interfaces
John Garbutt030fb232012-04-27 18:28:28 +0100158
Anthony Young11889042012-01-12 17:11:56 -0800159# Gracefully cp only if source file/dir exists
160function cp_it {
161 if [ -e $1 ] || [ -d $1 ]; then
162 cp -pRL $1 $2
163 fi
164}
165
166# Copy over your ssh keys and env if desired
167COPYENV=${COPYENV:-1}
168if [ "$COPYENV" = "1" ]; then
169 cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh
170 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys
171 cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig
172 cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc
173 cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc
174fi
175
176# Configure run.sh
177cat <<EOF >$STAGING_DIR/opt/stack/run.sh
178#!/bin/bash
Mate Lakat93f3b862013-09-24 17:35:00 +0100179set -eux
Anthony Young11889042012-01-12 17:11:56 -0800180cd /opt/stack/devstack
Mate Lakat93f3b862013-09-24 17:35:00 +0100181./unstack.sh || true
182./stack.sh
Anthony Young11889042012-01-12 17:11:56 -0800183EOF
184chmod 755 $STAGING_DIR/opt/stack/run.sh