blob: 7272fe2664aea3bec03a1bf497f8f5cc1e5dbc28 [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
Anthony Young11889042012-01-12 17:11:56 -080027# Source params - override xenrc params in your localrc to suite your taste
28source xenrc
29
John Garbuttdaadf742012-04-27 18:28:28 +010030#
31# Parameters
32#
Renuka Apte0af143b2012-04-02 15:46:53 -070033GUEST_NAME="$1"
34
Mate Lakat5a56cd62013-06-17 13:54:43 +010035function _print_interface_config() {
36 local device_nr
37 local ip_address
38 local netmask
39
40 device_nr="$1"
41 ip_address="$2"
42 netmask="$3"
43
44 local device
45
46 device="eth${device_nr}"
47
48 echo "auto $device"
49 if [ $ip_address == "dhcp" ]; then
50 echo "iface $device inet dhcp"
51 else
52 echo "iface $device inet static"
53 echo " address $ip_address"
54 echo " netmask $netmask"
55 fi
56
57 # Turn off tx checksumming for better performance
58 echo " post-up ethtool -K $device tx off"
59}
60
61function print_interfaces_config() {
62 echo "auto lo"
63 echo "iface lo inet loopback"
64
65 _print_interface_config $PUB_DEV_NR $PUB_IP $PUB_NETMASK
66 _print_interface_config $VM_DEV_NR $VM_IP $VM_NETMASK
67 _print_interface_config $MGT_DEV_NR $MGT_IP $MGT_NETMASK
68}
69
John Garbuttdaadf742012-04-27 18:28:28 +010070#
71# Mount the VDI
72#
Renuka Apte0af143b2012-04-02 15:46:53 -070073STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*")
74add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1"
Anthony Young11889042012-01-12 17:11:56 -080075
76# Make sure we have a stage
77if [ ! -d $STAGING_DIR/etc ]; then
78 echo "Stage is not properly set up!"
79 exit 1
80fi
81
Mate Lakatec06efc2013-02-01 15:16:51 +000082# Only support DHCP for now - don't support how different versions of Ubuntu handle resolv.conf
83if [ "$MGT_IP" != "dhcp" ] && [ "$PUB_IP" != "dhcp" ]; then
84 echo "Configuration without DHCP not supported"
John Garbuttd8f1a872012-06-26 11:16:38 +010085 exit 1
86fi
Anthony Young11889042012-01-12 17:11:56 -080087
88# Copy over devstack
89rm -f /tmp/devstack.tar
Renuka Apte0af143b2012-04-02 15:46:53 -070090cd $TOP_DIR/../../
91tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar .
92mkdir -p $STAGING_DIR/opt/stack/devstack
93tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack
Anthony Young11889042012-01-12 17:11:56 -080094cd $TOP_DIR
95
Mate Lakat93f3b862013-09-24 17:35:00 +010096# Create an upstart job (task) for devstack, which can interact with the console
97cat >$STAGING_DIR/etc/init/devstack.conf << EOF
98start on stopped rc RUNLEVEL=[2345]
99
100console output
101task
102
103pre-start script
104 rm -f /var/run/devstack.succeeded
105end script
106
107script
108 initctl stop hvc0 || true
109
110 # Read any leftover characters from standard input
111 while read -n 1 -s -t 0.1 -r ignored; do
112 true
113 done
114
115 clear
116
117 chown -R $STACK_USER /opt/stack
118
119 if su -c "/opt/stack/run.sh" $STACK_USER; then
120 touch /var/run/devstack.succeeded
121 fi
122 initctl start hvc0 > /dev/null 2>&1
123end script
Anthony Young11889042012-01-12 17:11:56 -0800124EOF
125
Anthony Young11889042012-01-12 17:11:56 -0800126# Configure the hostname
127echo $GUEST_NAME > $STAGING_DIR/etc/hostname
128
129# Hostname must resolve for rabbit
John Garbuttdaadf742012-04-27 18:28:28 +0100130HOSTS_FILE_IP=$PUB_IP
131if [ $MGT_IP != "dhcp" ]; then
132 HOSTS_FILE_IP=$MGT_IP
133fi
Anthony Young11889042012-01-12 17:11:56 -0800134cat <<EOF >$STAGING_DIR/etc/hosts
John Garbuttdaadf742012-04-27 18:28:28 +0100135$HOSTS_FILE_IP $GUEST_NAME
Anthony Young11889042012-01-12 17:11:56 -0800136127.0.0.1 localhost localhost.localdomain
137EOF
138
139# Configure the network
Mate Lakat5a56cd62013-06-17 13:54:43 +0100140print_interfaces_config > $STAGING_DIR/etc/network/interfaces
John Garbutt030fb232012-04-27 18:28:28 +0100141
Anthony Young11889042012-01-12 17:11:56 -0800142# Gracefully cp only if source file/dir exists
143function cp_it {
144 if [ -e $1 ] || [ -d $1 ]; then
145 cp -pRL $1 $2
146 fi
147}
148
149# Copy over your ssh keys and env if desired
150COPYENV=${COPYENV:-1}
151if [ "$COPYENV" = "1" ]; then
152 cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh
153 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys
154 cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig
155 cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc
156 cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc
157fi
158
159# Configure run.sh
160cat <<EOF >$STAGING_DIR/opt/stack/run.sh
161#!/bin/bash
Mate Lakat93f3b862013-09-24 17:35:00 +0100162set -eux
Anthony Young11889042012-01-12 17:11:56 -0800163cd /opt/stack/devstack
Mate Lakat93f3b862013-09-24 17:35:00 +0100164./unstack.sh || true
165./stack.sh
Anthony Young11889042012-01-12 17:11:56 -0800166EOF
167chmod 755 $STAGING_DIR/opt/stack/run.sh