blob: fbbfd6fbe5ceb6a5fe0b524c327b886ecbc3196b [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
Bob Ballc643ebb2014-02-02 09:16:20 +000024# Source lower level functions
25. $TOP_DIR/../../functions
26
John Garbuttdaadf742012-04-27 18:28:28 +010027# Include onexit commands
28. $TOP_DIR/scripts/on_exit.sh
29
Bob Ballc643ebb2014-02-02 09:16:20 +000030# 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
35GetDistro
36
Anthony Young11889042012-01-12 17:11:56 -080037# Source params - override xenrc params in your localrc to suite your taste
38source xenrc
39
John Garbuttdaadf742012-04-27 18:28:28 +010040#
41# Parameters
42#
Renuka Apte0af143b2012-04-02 15:46:53 -070043GUEST_NAME="$1"
44
Mate Lakat5a56cd62013-06-17 13:54:43 +010045function _print_interface_config() {
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
71function print_interfaces_config() {
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 Garbuttdaadf742012-04-27 18:28:28 +010080#
81# Mount the VDI
82#
Renuka Apte0af143b2012-04-02 15:46:53 -070083STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*")
84add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1"
Anthony Young11889042012-01-12 17:11:56 -080085
86# Make sure we have a stage
87if [ ! -d $STAGING_DIR/etc ]; then
88 echo "Stage is not properly set up!"
89 exit 1
90fi
91
Mate Lakatec06efc2013-02-01 15:16:51 +000092# Only support DHCP for now - don't support how different versions of Ubuntu handle resolv.conf
93if [ "$MGT_IP" != "dhcp" ] && [ "$PUB_IP" != "dhcp" ]; then
94 echo "Configuration without DHCP not supported"
John Garbuttd8f1a872012-06-26 11:16:38 +010095 exit 1
96fi
Anthony Young11889042012-01-12 17:11:56 -080097
98# Copy over devstack
99rm -f /tmp/devstack.tar
Renuka Apte0af143b2012-04-02 15:46:53 -0700100cd $TOP_DIR/../../
101tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar .
102mkdir -p $STAGING_DIR/opt/stack/devstack
103tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack
Anthony Young11889042012-01-12 17:11:56 -0800104cd $TOP_DIR
105
Mate Lakat93f3b862013-09-24 17:35:00 +0100106# Create an upstart job (task) for devstack, which can interact with the console
107cat >$STAGING_DIR/etc/init/devstack.conf << EOF
108start on stopped rc RUNLEVEL=[2345]
109
110console output
111task
112
113pre-start script
114 rm -f /var/run/devstack.succeeded
115end script
116
117script
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 Lakatda481d02013-09-26 13:57:02 +0100132
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 Lakat93f3b862013-09-24 17:35:00 +0100146 initctl start hvc0 > /dev/null 2>&1
147end script
Anthony Young11889042012-01-12 17:11:56 -0800148EOF
149
Anthony Young11889042012-01-12 17:11:56 -0800150# Configure the hostname
151echo $GUEST_NAME > $STAGING_DIR/etc/hostname
152
153# Hostname must resolve for rabbit
John Garbuttdaadf742012-04-27 18:28:28 +0100154HOSTS_FILE_IP=$PUB_IP
155if [ $MGT_IP != "dhcp" ]; then
156 HOSTS_FILE_IP=$MGT_IP
157fi
Anthony Young11889042012-01-12 17:11:56 -0800158cat <<EOF >$STAGING_DIR/etc/hosts
John Garbuttdaadf742012-04-27 18:28:28 +0100159$HOSTS_FILE_IP $GUEST_NAME
Anthony Young11889042012-01-12 17:11:56 -0800160127.0.0.1 localhost localhost.localdomain
161EOF
162
163# Configure the network
Mate Lakat5a56cd62013-06-17 13:54:43 +0100164print_interfaces_config > $STAGING_DIR/etc/network/interfaces
John Garbutt030fb232012-04-27 18:28:28 +0100165
Anthony Young11889042012-01-12 17:11:56 -0800166# Gracefully cp only if source file/dir exists
167function 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
174COPYENV=${COPYENV:-1}
175if [ "$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
181fi
182
183# Configure run.sh
184cat <<EOF >$STAGING_DIR/opt/stack/run.sh
185#!/bin/bash
Mate Lakat93f3b862013-09-24 17:35:00 +0100186set -eux
Anthony Young11889042012-01-12 17:11:56 -0800187cd /opt/stack/devstack
Mate Lakat93f3b862013-09-24 17:35:00 +0100188./unstack.sh || true
189./stack.sh
Anthony Young11889042012-01-12 17:11:56 -0800190EOF
191chmod 755 $STAGING_DIR/opt/stack/run.sh