blob: ebfd4835a264a854424ba9810ef7ac309e88c8aa [file] [log] [blame]
Mate Lakat57e3da92013-03-22 16:34:05 +00001#!/bin/bash
2
3function xapi_plugin_location {
Mate Lakatfe586b12013-03-28 15:02:27 +00004 for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/"; do
5 if [ -d $PLUGIN_DIR ]; then
Mate Lakat57e3da92013-03-22 16:34:05 +00006 echo $PLUGIN_DIR
7 return 0
8 fi
9 done
10 return 1
11}
12
13function zip_snapshot_location {
14 echo $1 | sed "s:\.git$::;s:$:/zipball/$2:g"
15}
16
17function create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +000018 if [ -d "/boot/guest" ]; then
19 echo "INFO: /boot/guest directory already exists, using that" >&2
20 else
21 local LOCALPATH="$(get_local_sr_path)/os-guest-kernels"
22 mkdir -p $LOCALPATH
23 ln -s $LOCALPATH /boot/guest
24 fi
Mate Lakat57e3da92013-03-22 16:34:05 +000025}
26
27function extract_remote_zipball {
28 local ZIPBALL_URL=$1
29
30 local LOCAL_ZIPBALL=$(mktemp)
31 local EXTRACTED_FILES=$(mktemp -d)
32
33 (
34 wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate
35 unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
36 rm -f $LOCAL_ZIPBALL
37 ) >&2
38
39 echo "$EXTRACTED_FILES"
40}
41
42function find_xapi_plugins_dir {
43 find $1 -path '*/xapi.d/plugins' -type d -print
44}
45
46function install_xapi_plugins_from_zipball {
47 local XAPI_PLUGIN_DIR
48 local EXTRACTED_FILES
49 local EXTRACTED_PLUGINS_DIR
50
51 XAPI_PLUGIN_DIR=$(xapi_plugin_location)
52
53 EXTRACTED_FILES=$(extract_remote_zipball $1)
54 EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
55
56 cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
57 rm -rf $EXTRACTED_FILES
58 chmod a+x ${XAPI_PLUGIN_DIR}*
59}
Mate Lakatfe586b12013-03-28 15:02:27 +000060
61function get_local_sr {
62 xe sr-list name-label="Local storage" --minimal
63}
64
65function get_local_sr_path {
66 echo "/var/run/sr-mount/$(get_local_sr)"
67}
Mate Lakat86446762013-05-12 18:34:29 +010068
69function find_ip_by_name() {
70 local guest_name="$1"
71 local interface="$2"
72
73 local period=10
74 local max_tries=10
75 local i=0
76
77 while true; do
78 if [ $i -ge $max_tries ]; then
79 echo "Timeout: ip address for interface $interface of $guest_name"
80 exit 11
81 fi
82
83 ipaddress=$(xe vm-list --minimal \
84 name-label=$guest_name \
85 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
86
87 if [ -z "$ipaddress" ]; then
88 sleep $period
89 ((i++))
90 else
91 echo $ipaddress
92 break
93 fi
94 done
95}
Mate Lakat9e326772013-05-08 16:42:22 +010096
Mate Lakat8ff33ce2013-05-30 13:26:58 +010097function _vm_uuid() {
98 local vm_name_label
99
100 vm_name_label="$1"
101
102 xe vm-list name-label="$vm_name_label" --minimal
103}
104
Mate Lakat9e326772013-05-08 16:42:22 +0100105function _create_new_network() {
106 local name_label
107 name_label=$1
108
109 xe network-create name-label="$name_label"
110}
111
112function _multiple_networks_with_name() {
113 local name_label
114 name_label=$1
115
116 # A comma indicates multiple matches
117 xe network-list name-label="$name_label" --minimal | grep -q ","
118}
119
120function _network_exists() {
121 local name_label
122 name_label=$1
123
124 ! [ -z $(xe network-list name-label="$name_label" --minimal) ]
125}
126
127function _bridge_exists() {
128 local bridge
129 bridge=$1
130
131 ! [ -z $(xe network-list bridge="$bridge" --minimal) ]
132}
133
Mate Lakatf652e0f2013-05-21 18:12:48 +0100134function _network_uuid() {
135 local bridge_or_net_name
136 bridge_or_net_name=$1
137
138 if _bridge_exists "$bridge_or_net_name"; then
139 xe network-list bridge="$bridge_or_net_name" --minimal
140 else
141 xe network-list name-label="$bridge_or_net_name" --minimal
142 fi
143}
144
145function add_interface() {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100146 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100147 local bridge_or_network_name
148
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100149 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100150 bridge_or_network_name="$2"
151 device_number="$3"
152
153 local vm
154 local net
155
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100156 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100157 net=$(_network_uuid "$bridge_or_network_name")
158 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
159}
Mate Lakat9e326772013-05-08 16:42:22 +0100160
161function setup_network() {
162 local bridge_or_net_name
163 bridge_or_net_name=$1
164
165 if ! _bridge_exists "$bridge_or_net_name"; then
166 if _network_exists "$bridge_or_net_name"; then
167 if _multiple_networks_with_name "$bridge_or_net_name"; then
168 cat >&2 << EOF
169ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
170please review your XenServer network configuration / localrc file.
171EOF
172 exit 1
173 fi
174 else
175 _create_new_network "$bridge_or_net_name"
176 fi
177 fi
178}
179
180function bridge_for() {
181 local bridge_or_net_name
182 bridge_or_net_name=$1
183
184 if _bridge_exists "$bridge_or_net_name"; then
185 echo "$bridge_or_net_name"
186 else
187 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
188 fi
189}
190
191function xenapi_ip_on() {
192 local bridge_or_net_name
193 bridge_or_net_name=$1
194
195 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
196}
197
198function xenapi_is_listening_on() {
199 local bridge_or_net_name
200 bridge_or_net_name=$1
201
202 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
203}
204
205function parameter_is_specified() {
206 local parameter_name
207 parameter_name=$1
208
209 compgen -v | grep "$parameter_name"
210}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100211
212function append_kernel_cmdline()
213{
214 local vm_name_label
215 local kernel_args
216
217 vm_name_label="$1"
218 kernel_args="$2"
219
220 local vm
221 local pv_args
222
223 vm=$(_vm_uuid "$vm_name_label")
224 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
225 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
226}