blob: 4e9fede38788d7e984f082f028cfab9d77019298 [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
182 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
183}
184
Ian Wienand62cae132014-09-04 21:25:45 +1000185function xenapi_is_listening_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100186 local bridge_or_net_name
187 bridge_or_net_name=$1
188
189 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
190}
191
Ian Wienand62cae132014-09-04 21:25:45 +1000192function parameter_is_specified {
Mate Lakat9e326772013-05-08 16:42:22 +0100193 local parameter_name
194 parameter_name=$1
195
196 compgen -v | grep "$parameter_name"
197}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100198
Ian Wienand62cae132014-09-04 21:25:45 +1000199function append_kernel_cmdline {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100200 local vm_name_label
201 local kernel_args
202
203 vm_name_label="$1"
204 kernel_args="$2"
205
206 local vm
207 local pv_args
208
209 vm=$(_vm_uuid "$vm_name_label")
210 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
211 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
212}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100213
Ian Wienand62cae132014-09-04 21:25:45 +1000214function destroy_all_vifs_of {
Mate Lakat5a56cd62013-06-17 13:54:43 +0100215 local vm_name_label
216
217 vm_name_label="$1"
218
219 local vm
220
221 vm=$(_vm_uuid "$vm_name_label")
222 IFS=,
223 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
224 xe vif-destroy uuid="$vif"
225 done
226 unset IFS
227}
Mate Lakatd8511032013-07-03 10:44:44 +0100228
Ian Wienand62cae132014-09-04 21:25:45 +1000229function have_multiple_hosts {
Mate Lakatd8511032013-07-03 10:44:44 +0100230 xe host-list --minimal | grep -q ","
231}
232
Ian Wienand62cae132014-09-04 21:25:45 +1000233function attach_network {
Mate Lakatd8511032013-07-03 10:44:44 +0100234 local bridge_or_net_name
235
236 bridge_or_net_name="$1"
237
238 local net
239 local host
240
241 net=$(_network_uuid "$bridge_or_net_name")
242 host=$(xe host-list --minimal)
243
244 xe network-attach uuid=$net host-uuid=$host
245}
Mate Lakat16ed0682013-08-30 13:28:31 +0100246
Ian Wienand62cae132014-09-04 21:25:45 +1000247function set_vm_memory {
Mate Lakat16ed0682013-08-30 13:28:31 +0100248 local vm_name_label
249 local memory
250
251 vm_name_label="$1"
252 memory="$2"
253
254 local vm
255
256 vm=$(_vm_uuid "$vm_name_label")
257
258 xe vm-memory-limits-set \
259 static-min=${memory}MiB \
260 static-max=${memory}MiB \
261 dynamic-min=${memory}MiB \
262 dynamic-max=${memory}MiB \
263 uuid=$vm
264}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100265
Ian Wienand62cae132014-09-04 21:25:45 +1000266function max_vcpus {
Mate Lakat9f878cb2013-10-04 09:56:24 +0100267 local vm_name_label
268
269 vm_name_label="$1"
270
271 local vm
272 local host
273 local cpu_count
274
275 host=$(xe host-list --minimal)
276 vm=$(_vm_uuid "$vm_name_label")
277
278 cpu_count=$(xe host-param-get \
279 param-name=cpu_info \
280 uuid=$host |
281 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
282
283 if [ -z "$cpu_count" ]; then
284 # get dom0's vcpu count
285 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
286 fi
287
288 # Assert cpu_count is not empty
289 [ -n "$cpu_count" ]
290
291 # Assert ithas a numeric nonzero value
292 expr "$cpu_count" + 0
293
294 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
295 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
296}
Mate Lakatd15c8a02014-02-04 12:38:14 +0000297
Ian Wienand62cae132014-09-04 21:25:45 +1000298function get_domid {
Mate Lakatd15c8a02014-02-04 12:38:14 +0000299 local vm_name_label
300
301 vm_name_label="$1"
302
303 xe vm-list name-label="$vm_name_label" params=dom-id minimal=true
304}