blob: 4317796e39326e990e6d5e89f6091f55543edbf9 [file] [log] [blame]
Mate Lakat57e3da92013-03-22 16:34:05 +00001#!/bin/bash
2
Mate Lakat2781f3b2013-12-11 13:41:54 +00003function die_with_error {
4 local err_msg
5
6 err_msg="$1"
7
8 echo "$err_msg" >&2
9 exit 1
10}
11
Mate Lakat57e3da92013-03-22 16:34:05 +000012function xapi_plugin_location {
Euan Harris12229a72013-07-03 17:51:01 +010013 for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/" "/usr/lib/xapi/plugins"; do
Mate Lakatfe586b12013-03-28 15:02:27 +000014 if [ -d $PLUGIN_DIR ]; then
Mate Lakat57e3da92013-03-22 16:34:05 +000015 echo $PLUGIN_DIR
16 return 0
17 fi
18 done
19 return 1
20}
21
22function zip_snapshot_location {
Mate Lakat2781f3b2013-12-11 13:41:54 +000023 echo $1 | sed "s,^git://,http://,g;s:\.git$::;s:$:/zipball/$2:g"
Mate Lakat57e3da92013-03-22 16:34:05 +000024}
25
26function create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +000027 if [ -d "/boot/guest" ]; then
28 echo "INFO: /boot/guest directory already exists, using that" >&2
29 else
30 local LOCALPATH="$(get_local_sr_path)/os-guest-kernels"
31 mkdir -p $LOCALPATH
32 ln -s $LOCALPATH /boot/guest
33 fi
Mate Lakat57e3da92013-03-22 16:34:05 +000034}
35
Bob Ball39aeda22013-06-17 12:51:33 +010036function create_directory_for_images {
37 if [ -d "/images" ]; then
38 echo "INFO: /images directory already exists, using that" >&2
39 else
40 local LOCALPATH="$(get_local_sr_path)/os-images"
41 mkdir -p $LOCALPATH
42 ln -s $LOCALPATH /images
43 fi
44}
45
Mate Lakat57e3da92013-03-22 16:34:05 +000046function extract_remote_zipball {
47 local ZIPBALL_URL=$1
48
49 local LOCAL_ZIPBALL=$(mktemp)
50 local EXTRACTED_FILES=$(mktemp -d)
51
Euan Harris6f001712013-07-10 16:30:31 +010052 {
Mate Lakat2781f3b2013-12-11 13:41:54 +000053 if ! wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate; then
54 die_with_error "Failed to download [$ZIPBALL_URL]"
55 fi
Mate Lakat57e3da92013-03-22 16:34:05 +000056 unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
57 rm -f $LOCAL_ZIPBALL
Euan Harris6f001712013-07-10 16:30:31 +010058 } >&2
Mate Lakat57e3da92013-03-22 16:34:05 +000059
60 echo "$EXTRACTED_FILES"
61}
62
63function find_xapi_plugins_dir {
64 find $1 -path '*/xapi.d/plugins' -type d -print
65}
66
Mate Lakatabe56ee2013-07-24 11:06:27 +010067function install_xapi_plugins_from {
Mate Lakat57e3da92013-03-22 16:34:05 +000068 local XAPI_PLUGIN_DIR
69 local EXTRACTED_FILES
70 local EXTRACTED_PLUGINS_DIR
71
Mate Lakatabe56ee2013-07-24 11:06:27 +010072 EXTRACTED_FILES="$1"
73
Mate Lakat57e3da92013-03-22 16:34:05 +000074 XAPI_PLUGIN_DIR=$(xapi_plugin_location)
75
Mate Lakat57e3da92013-03-22 16:34:05 +000076 EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
77
78 cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
Mate Lakat57e3da92013-03-22 16:34:05 +000079 chmod a+x ${XAPI_PLUGIN_DIR}*
80}
Mate Lakatfe586b12013-03-28 15:02:27 +000081
82function get_local_sr {
Bob Ball83dcf202013-09-29 21:45:49 +010083 xe pool-list params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +000084}
85
86function get_local_sr_path {
Bob Ball83dcf202013-09-29 21:45:49 +010087 pbd_path="/var/run/sr-mount/$(get_local_sr)"
88 pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "`
89 if [ -n "$pbd_device_config_path" ]; then
90 pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true`
91 pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""`
92 fi
93 echo $pbd_path
Mate Lakatfe586b12013-03-28 15:02:27 +000094}
Mate Lakat86446762013-05-12 18:34:29 +010095
Ian Wienand62cae132014-09-04 21:25:45 +100096function find_ip_by_name {
Mate Lakat86446762013-05-12 18:34:29 +010097 local guest_name="$1"
98 local interface="$2"
99
100 local period=10
101 local max_tries=10
102 local i=0
103
104 while true; do
105 if [ $i -ge $max_tries ]; then
106 echo "Timeout: ip address for interface $interface of $guest_name"
107 exit 11
108 fi
109
110 ipaddress=$(xe vm-list --minimal \
111 name-label=$guest_name \
112 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
113
114 if [ -z "$ipaddress" ]; then
115 sleep $period
116 ((i++))
117 else
118 echo $ipaddress
119 break
120 fi
121 done
122}
Mate Lakat9e326772013-05-08 16:42:22 +0100123
Ian Wienand62cae132014-09-04 21:25:45 +1000124function _vm_uuid {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100125 local vm_name_label
126
127 vm_name_label="$1"
128
129 xe vm-list name-label="$vm_name_label" --minimal
130}
131
Ian Wienand62cae132014-09-04 21:25:45 +1000132function _create_new_network {
Mate Lakat9e326772013-05-08 16:42:22 +0100133 local name_label
134 name_label=$1
135
136 xe network-create name-label="$name_label"
137}
138
Ian Wienand62cae132014-09-04 21:25:45 +1000139function _multiple_networks_with_name {
Mate Lakat9e326772013-05-08 16:42:22 +0100140 local name_label
141 name_label=$1
142
143 # A comma indicates multiple matches
144 xe network-list name-label="$name_label" --minimal | grep -q ","
145}
146
Ian Wienand62cae132014-09-04 21:25:45 +1000147function _network_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100148 local name_label
149 name_label=$1
150
Mate Lakat2b8814d2013-09-25 17:07:06 +0100151 ! [ -z "$(xe network-list name-label="$name_label" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100152}
153
Ian Wienand62cae132014-09-04 21:25:45 +1000154function _bridge_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100155 local bridge
156 bridge=$1
157
Mate Lakat2b8814d2013-09-25 17:07:06 +0100158 ! [ -z "$(xe network-list bridge="$bridge" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100159}
160
Ian Wienand62cae132014-09-04 21:25:45 +1000161function _network_uuid {
Mate Lakatf652e0f2013-05-21 18:12:48 +0100162 local bridge_or_net_name
163 bridge_or_net_name=$1
164
165 if _bridge_exists "$bridge_or_net_name"; then
166 xe network-list bridge="$bridge_or_net_name" --minimal
167 else
168 xe network-list name-label="$bridge_or_net_name" --minimal
169 fi
170}
171
Ian Wienand62cae132014-09-04 21:25:45 +1000172function add_interface {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100173 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100174 local bridge_or_network_name
175
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100176 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100177 bridge_or_network_name="$2"
178 device_number="$3"
179
180 local vm
181 local net
182
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100183 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100184 net=$(_network_uuid "$bridge_or_network_name")
185 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
186}
Mate Lakat9e326772013-05-08 16:42:22 +0100187
Ian Wienand62cae132014-09-04 21:25:45 +1000188function setup_network {
Mate Lakat9e326772013-05-08 16:42:22 +0100189 local bridge_or_net_name
190 bridge_or_net_name=$1
191
192 if ! _bridge_exists "$bridge_or_net_name"; then
193 if _network_exists "$bridge_or_net_name"; then
194 if _multiple_networks_with_name "$bridge_or_net_name"; then
195 cat >&2 << EOF
196ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
197please review your XenServer network configuration / localrc file.
198EOF
199 exit 1
200 fi
201 else
202 _create_new_network "$bridge_or_net_name"
203 fi
204 fi
205}
206
Ian Wienand62cae132014-09-04 21:25:45 +1000207function bridge_for {
Mate Lakat9e326772013-05-08 16:42:22 +0100208 local bridge_or_net_name
209 bridge_or_net_name=$1
210
211 if _bridge_exists "$bridge_or_net_name"; then
212 echo "$bridge_or_net_name"
213 else
214 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
215 fi
216}
217
Ian Wienand62cae132014-09-04 21:25:45 +1000218function xenapi_ip_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100219 local bridge_or_net_name
220 bridge_or_net_name=$1
221
222 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
223}
224
Ian Wienand62cae132014-09-04 21:25:45 +1000225function xenapi_is_listening_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100226 local bridge_or_net_name
227 bridge_or_net_name=$1
228
229 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
230}
231
Ian Wienand62cae132014-09-04 21:25:45 +1000232function parameter_is_specified {
Mate Lakat9e326772013-05-08 16:42:22 +0100233 local parameter_name
234 parameter_name=$1
235
236 compgen -v | grep "$parameter_name"
237}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100238
Ian Wienand62cae132014-09-04 21:25:45 +1000239function append_kernel_cmdline {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100240 local vm_name_label
241 local kernel_args
242
243 vm_name_label="$1"
244 kernel_args="$2"
245
246 local vm
247 local pv_args
248
249 vm=$(_vm_uuid "$vm_name_label")
250 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
251 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
252}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100253
Ian Wienand62cae132014-09-04 21:25:45 +1000254function destroy_all_vifs_of {
Mate Lakat5a56cd62013-06-17 13:54:43 +0100255 local vm_name_label
256
257 vm_name_label="$1"
258
259 local vm
260
261 vm=$(_vm_uuid "$vm_name_label")
262 IFS=,
263 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
264 xe vif-destroy uuid="$vif"
265 done
266 unset IFS
267}
Mate Lakatd8511032013-07-03 10:44:44 +0100268
Ian Wienand62cae132014-09-04 21:25:45 +1000269function have_multiple_hosts {
Mate Lakatd8511032013-07-03 10:44:44 +0100270 xe host-list --minimal | grep -q ","
271}
272
Ian Wienand62cae132014-09-04 21:25:45 +1000273function attach_network {
Mate Lakatd8511032013-07-03 10:44:44 +0100274 local bridge_or_net_name
275
276 bridge_or_net_name="$1"
277
278 local net
279 local host
280
281 net=$(_network_uuid "$bridge_or_net_name")
282 host=$(xe host-list --minimal)
283
284 xe network-attach uuid=$net host-uuid=$host
285}
Mate Lakat16ed0682013-08-30 13:28:31 +0100286
Ian Wienand62cae132014-09-04 21:25:45 +1000287function set_vm_memory {
Mate Lakat16ed0682013-08-30 13:28:31 +0100288 local vm_name_label
289 local memory
290
291 vm_name_label="$1"
292 memory="$2"
293
294 local vm
295
296 vm=$(_vm_uuid "$vm_name_label")
297
298 xe vm-memory-limits-set \
299 static-min=${memory}MiB \
300 static-max=${memory}MiB \
301 dynamic-min=${memory}MiB \
302 dynamic-max=${memory}MiB \
303 uuid=$vm
304}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100305
Ian Wienand62cae132014-09-04 21:25:45 +1000306function max_vcpus {
Mate Lakat9f878cb2013-10-04 09:56:24 +0100307 local vm_name_label
308
309 vm_name_label="$1"
310
311 local vm
312 local host
313 local cpu_count
314
315 host=$(xe host-list --minimal)
316 vm=$(_vm_uuid "$vm_name_label")
317
318 cpu_count=$(xe host-param-get \
319 param-name=cpu_info \
320 uuid=$host |
321 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
322
323 if [ -z "$cpu_count" ]; then
324 # get dom0's vcpu count
325 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
326 fi
327
328 # Assert cpu_count is not empty
329 [ -n "$cpu_count" ]
330
331 # Assert ithas a numeric nonzero value
332 expr "$cpu_count" + 0
333
334 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
335 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
336}
Mate Lakatd15c8a02014-02-04 12:38:14 +0000337
Ian Wienand62cae132014-09-04 21:25:45 +1000338function get_domid {
Mate Lakatd15c8a02014-02-04 12:38:14 +0000339 local vm_name_label
340
341 vm_name_label="$1"
342
343 xe vm-list name-label="$vm_name_label" params=dom-id minimal=true
344}