blob: 8c674dcce3fe8ad6830d76c5cc3d6f5dc0de51c7 [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 {
Bob Ballf3b49e22014-12-09 17:37:03 +000013 for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/" "/usr/lib/xapi/plugins" "/usr/lib64/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
Mate Lakat57e3da92013-03-22 16:34:05 +000022function create_directory_for_kernels {
Mate Lakatfe586b12013-03-28 15:02:27 +000023 if [ -d "/boot/guest" ]; then
24 echo "INFO: /boot/guest directory already exists, using that" >&2
25 else
26 local LOCALPATH="$(get_local_sr_path)/os-guest-kernels"
27 mkdir -p $LOCALPATH
28 ln -s $LOCALPATH /boot/guest
29 fi
Mate Lakat57e3da92013-03-22 16:34:05 +000030}
31
Bob Ball39aeda22013-06-17 12:51:33 +010032function create_directory_for_images {
33 if [ -d "/images" ]; then
34 echo "INFO: /images directory already exists, using that" >&2
35 else
36 local LOCALPATH="$(get_local_sr_path)/os-images"
37 mkdir -p $LOCALPATH
38 ln -s $LOCALPATH /images
39 fi
40}
41
Mate Lakatfe586b12013-03-28 15:02:27 +000042function get_local_sr {
Bob Ball83dcf202013-09-29 21:45:49 +010043 xe pool-list params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +000044}
45
46function get_local_sr_path {
Bob Ball83dcf202013-09-29 21:45:49 +010047 pbd_path="/var/run/sr-mount/$(get_local_sr)"
48 pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "`
49 if [ -n "$pbd_device_config_path" ]; then
50 pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true`
51 pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""`
52 fi
53 echo $pbd_path
Mate Lakatfe586b12013-03-28 15:02:27 +000054}
Mate Lakat86446762013-05-12 18:34:29 +010055
Ian Wienand62cae132014-09-04 21:25:45 +100056function find_ip_by_name {
Mate Lakat86446762013-05-12 18:34:29 +010057 local guest_name="$1"
58 local interface="$2"
59
60 local period=10
61 local max_tries=10
62 local i=0
63
64 while true; do
65 if [ $i -ge $max_tries ]; then
66 echo "Timeout: ip address for interface $interface of $guest_name"
67 exit 11
68 fi
69
70 ipaddress=$(xe vm-list --minimal \
71 name-label=$guest_name \
72 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
73
74 if [ -z "$ipaddress" ]; then
75 sleep $period
76 ((i++))
77 else
78 echo $ipaddress
79 break
80 fi
81 done
82}
Mate Lakat9e326772013-05-08 16:42:22 +010083
Ian Wienand62cae132014-09-04 21:25:45 +100084function _vm_uuid {
Mate Lakat8ff33ce2013-05-30 13:26:58 +010085 local vm_name_label
86
87 vm_name_label="$1"
88
89 xe vm-list name-label="$vm_name_label" --minimal
90}
91
Ian Wienand62cae132014-09-04 21:25:45 +100092function _create_new_network {
Mate Lakat9e326772013-05-08 16:42:22 +010093 local name_label
94 name_label=$1
95
96 xe network-create name-label="$name_label"
97}
98
Ian Wienand62cae132014-09-04 21:25:45 +100099function _multiple_networks_with_name {
Mate Lakat9e326772013-05-08 16:42:22 +0100100 local name_label
101 name_label=$1
102
103 # A comma indicates multiple matches
104 xe network-list name-label="$name_label" --minimal | grep -q ","
105}
106
Ian Wienand62cae132014-09-04 21:25:45 +1000107function _network_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100108 local name_label
109 name_label=$1
110
Mate Lakat2b8814d2013-09-25 17:07:06 +0100111 ! [ -z "$(xe network-list name-label="$name_label" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100112}
113
Ian Wienand62cae132014-09-04 21:25:45 +1000114function _bridge_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100115 local bridge
116 bridge=$1
117
Mate Lakat2b8814d2013-09-25 17:07:06 +0100118 ! [ -z "$(xe network-list bridge="$bridge" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100119}
120
Ian Wienand62cae132014-09-04 21:25:45 +1000121function _network_uuid {
Mate Lakatf652e0f2013-05-21 18:12:48 +0100122 local bridge_or_net_name
123 bridge_or_net_name=$1
124
125 if _bridge_exists "$bridge_or_net_name"; then
126 xe network-list bridge="$bridge_or_net_name" --minimal
127 else
128 xe network-list name-label="$bridge_or_net_name" --minimal
129 fi
130}
131
Ian Wienand62cae132014-09-04 21:25:45 +1000132function add_interface {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100133 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100134 local bridge_or_network_name
135
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100136 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100137 bridge_or_network_name="$2"
138 device_number="$3"
139
140 local vm
141 local net
142
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100143 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100144 net=$(_network_uuid "$bridge_or_network_name")
145 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
146}
Mate Lakat9e326772013-05-08 16:42:22 +0100147
Ian Wienand62cae132014-09-04 21:25:45 +1000148function setup_network {
Mate Lakat9e326772013-05-08 16:42:22 +0100149 local bridge_or_net_name
150 bridge_or_net_name=$1
151
152 if ! _bridge_exists "$bridge_or_net_name"; then
153 if _network_exists "$bridge_or_net_name"; then
154 if _multiple_networks_with_name "$bridge_or_net_name"; then
155 cat >&2 << EOF
156ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
157please review your XenServer network configuration / localrc file.
158EOF
159 exit 1
160 fi
161 else
162 _create_new_network "$bridge_or_net_name"
163 fi
164 fi
165}
166
Ian Wienand62cae132014-09-04 21:25:45 +1000167function bridge_for {
Mate Lakat9e326772013-05-08 16:42:22 +0100168 local bridge_or_net_name
169 bridge_or_net_name=$1
170
171 if _bridge_exists "$bridge_or_net_name"; then
172 echo "$bridge_or_net_name"
173 else
174 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
175 fi
176}
177
Ian Wienand62cae132014-09-04 21:25:45 +1000178function xenapi_ip_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100179 local bridge_or_net_name
180 bridge_or_net_name=$1
181
jianghua wang78f6c1d2015-09-18 11:17:46 +0100182 ip -4 addr show $(bridge_for "$bridge_or_net_name") |\
183 awk '/inet/{split($2, ip, "/"); print ip[1];}'
Mate Lakat9e326772013-05-08 16:42:22 +0100184}
185
Ian Wienand62cae132014-09-04 21:25:45 +1000186function xenapi_is_listening_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100187 local bridge_or_net_name
188 bridge_or_net_name=$1
189
190 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
191}
192
Ian Wienand62cae132014-09-04 21:25:45 +1000193function parameter_is_specified {
Mate Lakat9e326772013-05-08 16:42:22 +0100194 local parameter_name
195 parameter_name=$1
196
197 compgen -v | grep "$parameter_name"
198}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100199
Ian Wienand62cae132014-09-04 21:25:45 +1000200function append_kernel_cmdline {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100201 local vm_name_label
202 local kernel_args
203
204 vm_name_label="$1"
205 kernel_args="$2"
206
207 local vm
208 local pv_args
209
210 vm=$(_vm_uuid "$vm_name_label")
211 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
212 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
213}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100214
Ian Wienand62cae132014-09-04 21:25:45 +1000215function destroy_all_vifs_of {
Mate Lakat5a56cd62013-06-17 13:54:43 +0100216 local vm_name_label
217
218 vm_name_label="$1"
219
220 local vm
221
222 vm=$(_vm_uuid "$vm_name_label")
223 IFS=,
224 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
225 xe vif-destroy uuid="$vif"
226 done
227 unset IFS
228}
Mate Lakatd8511032013-07-03 10:44:44 +0100229
Ian Wienand62cae132014-09-04 21:25:45 +1000230function have_multiple_hosts {
Mate Lakatd8511032013-07-03 10:44:44 +0100231 xe host-list --minimal | grep -q ","
232}
233
Ian Wienand62cae132014-09-04 21:25:45 +1000234function attach_network {
Mate Lakatd8511032013-07-03 10:44:44 +0100235 local bridge_or_net_name
236
237 bridge_or_net_name="$1"
238
239 local net
240 local host
241
242 net=$(_network_uuid "$bridge_or_net_name")
243 host=$(xe host-list --minimal)
244
245 xe network-attach uuid=$net host-uuid=$host
246}
Mate Lakat16ed0682013-08-30 13:28:31 +0100247
Ian Wienand62cae132014-09-04 21:25:45 +1000248function set_vm_memory {
Mate Lakat16ed0682013-08-30 13:28:31 +0100249 local vm_name_label
250 local memory
251
252 vm_name_label="$1"
253 memory="$2"
254
255 local vm
256
257 vm=$(_vm_uuid "$vm_name_label")
258
259 xe vm-memory-limits-set \
260 static-min=${memory}MiB \
261 static-max=${memory}MiB \
262 dynamic-min=${memory}MiB \
263 dynamic-max=${memory}MiB \
264 uuid=$vm
265}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100266
Ian Wienand62cae132014-09-04 21:25:45 +1000267function max_vcpus {
Mate Lakat9f878cb2013-10-04 09:56:24 +0100268 local vm_name_label
269
270 vm_name_label="$1"
271
272 local vm
273 local host
274 local cpu_count
275
276 host=$(xe host-list --minimal)
277 vm=$(_vm_uuid "$vm_name_label")
278
279 cpu_count=$(xe host-param-get \
280 param-name=cpu_info \
281 uuid=$host |
282 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
283
284 if [ -z "$cpu_count" ]; then
285 # get dom0's vcpu count
286 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
287 fi
288
289 # Assert cpu_count is not empty
290 [ -n "$cpu_count" ]
291
292 # Assert ithas a numeric nonzero value
293 expr "$cpu_count" + 0
294
295 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
296 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
297}
Mate Lakatd15c8a02014-02-04 12:38:14 +0000298
Ian Wienand62cae132014-09-04 21:25:45 +1000299function get_domid {
Mate Lakatd15c8a02014-02-04 12:38:14 +0000300 local vm_name_label
301
302 vm_name_label="$1"
303
304 xe vm-list name-label="$vm_name_label" params=dom-id minimal=true
305}