blob: ab0be84bd2335cd88abf98bb110aadc33c95a7c9 [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
96function find_ip_by_name() {
97 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
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100124function _vm_uuid() {
125 local vm_name_label
126
127 vm_name_label="$1"
128
129 xe vm-list name-label="$vm_name_label" --minimal
130}
131
Mate Lakat9e326772013-05-08 16:42:22 +0100132function _create_new_network() {
133 local name_label
134 name_label=$1
135
136 xe network-create name-label="$name_label"
137}
138
139function _multiple_networks_with_name() {
140 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
147function _network_exists() {
148 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
154function _bridge_exists() {
155 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
Mate Lakatf652e0f2013-05-21 18:12:48 +0100161function _network_uuid() {
162 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
172function 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
188function setup_network() {
189 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
207function bridge_for() {
208 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
218function xenapi_ip_on() {
219 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
225function xenapi_is_listening_on() {
226 local bridge_or_net_name
227 bridge_or_net_name=$1
228
229 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
230}
231
232function parameter_is_specified() {
233 local parameter_name
234 parameter_name=$1
235
236 compgen -v | grep "$parameter_name"
237}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100238
239function append_kernel_cmdline()
240{
241 local vm_name_label
242 local kernel_args
243
244 vm_name_label="$1"
245 kernel_args="$2"
246
247 local vm
248 local pv_args
249
250 vm=$(_vm_uuid "$vm_name_label")
251 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
252 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
253}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100254
255function destroy_all_vifs_of()
256{
257 local vm_name_label
258
259 vm_name_label="$1"
260
261 local vm
262
263 vm=$(_vm_uuid "$vm_name_label")
264 IFS=,
265 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
266 xe vif-destroy uuid="$vif"
267 done
268 unset IFS
269}
Mate Lakatd8511032013-07-03 10:44:44 +0100270
271function have_multiple_hosts() {
272 xe host-list --minimal | grep -q ","
273}
274
275function attach_network() {
276 local bridge_or_net_name
277
278 bridge_or_net_name="$1"
279
280 local net
281 local host
282
283 net=$(_network_uuid "$bridge_or_net_name")
284 host=$(xe host-list --minimal)
285
286 xe network-attach uuid=$net host-uuid=$host
287}
Mate Lakat16ed0682013-08-30 13:28:31 +0100288
289function set_vm_memory() {
290 local vm_name_label
291 local memory
292
293 vm_name_label="$1"
294 memory="$2"
295
296 local vm
297
298 vm=$(_vm_uuid "$vm_name_label")
299
300 xe vm-memory-limits-set \
301 static-min=${memory}MiB \
302 static-max=${memory}MiB \
303 dynamic-min=${memory}MiB \
304 dynamic-max=${memory}MiB \
305 uuid=$vm
306}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100307
308function max_vcpus() {
309 local vm_name_label
310
311 vm_name_label="$1"
312
313 local vm
314 local host
315 local cpu_count
316
317 host=$(xe host-list --minimal)
318 vm=$(_vm_uuid "$vm_name_label")
319
320 cpu_count=$(xe host-param-get \
321 param-name=cpu_info \
322 uuid=$host |
323 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
324
325 if [ -z "$cpu_count" ]; then
326 # get dom0's vcpu count
327 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
328 fi
329
330 # Assert cpu_count is not empty
331 [ -n "$cpu_count" ]
332
333 # Assert ithas a numeric nonzero value
334 expr "$cpu_count" + 0
335
336 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
337 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
338}
Mate Lakatd15c8a02014-02-04 12:38:14 +0000339
340function get_domid() {
341 local vm_name_label
342
343 vm_name_label="$1"
344
345 xe vm-list name-label="$vm_name_label" params=dom-id minimal=true
346}