blob: c65d919e3f4a4e9ed5cfa4818b1552858eb18da6 [file] [log] [blame]
Mate Lakat57e3da92013-03-22 16:34:05 +00001#!/bin/bash
2
3function xapi_plugin_location {
Euan Harris12229a72013-07-03 17:51:01 +01004 for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/" "/usr/lib/xapi/plugins"; do
Mate Lakatfe586b12013-03-28 15:02:27 +00005 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
Bob Ball39aeda22013-06-17 12:51:33 +010027function create_directory_for_images {
28 if [ -d "/images" ]; then
29 echo "INFO: /images directory already exists, using that" >&2
30 else
31 local LOCALPATH="$(get_local_sr_path)/os-images"
32 mkdir -p $LOCALPATH
33 ln -s $LOCALPATH /images
34 fi
35}
36
Mate Lakat57e3da92013-03-22 16:34:05 +000037function extract_remote_zipball {
38 local ZIPBALL_URL=$1
39
40 local LOCAL_ZIPBALL=$(mktemp)
41 local EXTRACTED_FILES=$(mktemp -d)
42
Euan Harris6f001712013-07-10 16:30:31 +010043 {
Mate Lakat57e3da92013-03-22 16:34:05 +000044 wget -nv $ZIPBALL_URL -O $LOCAL_ZIPBALL --no-check-certificate
45 unzip -q -o $LOCAL_ZIPBALL -d $EXTRACTED_FILES
46 rm -f $LOCAL_ZIPBALL
Euan Harris6f001712013-07-10 16:30:31 +010047 } >&2
Mate Lakat57e3da92013-03-22 16:34:05 +000048
49 echo "$EXTRACTED_FILES"
50}
51
52function find_xapi_plugins_dir {
53 find $1 -path '*/xapi.d/plugins' -type d -print
54}
55
Mate Lakatabe56ee2013-07-24 11:06:27 +010056function install_xapi_plugins_from {
Mate Lakat57e3da92013-03-22 16:34:05 +000057 local XAPI_PLUGIN_DIR
58 local EXTRACTED_FILES
59 local EXTRACTED_PLUGINS_DIR
60
Mate Lakatabe56ee2013-07-24 11:06:27 +010061 EXTRACTED_FILES="$1"
62
Mate Lakat57e3da92013-03-22 16:34:05 +000063 XAPI_PLUGIN_DIR=$(xapi_plugin_location)
64
Mate Lakat57e3da92013-03-22 16:34:05 +000065 EXTRACTED_PLUGINS_DIR=$(find_xapi_plugins_dir $EXTRACTED_FILES)
66
67 cp -pr $EXTRACTED_PLUGINS_DIR/* $XAPI_PLUGIN_DIR
Mate Lakat57e3da92013-03-22 16:34:05 +000068 chmod a+x ${XAPI_PLUGIN_DIR}*
69}
Mate Lakatfe586b12013-03-28 15:02:27 +000070
71function get_local_sr {
72 xe sr-list name-label="Local storage" --minimal
73}
74
75function get_local_sr_path {
76 echo "/var/run/sr-mount/$(get_local_sr)"
77}
Mate Lakat86446762013-05-12 18:34:29 +010078
79function find_ip_by_name() {
80 local guest_name="$1"
81 local interface="$2"
82
83 local period=10
84 local max_tries=10
85 local i=0
86
87 while true; do
88 if [ $i -ge $max_tries ]; then
89 echo "Timeout: ip address for interface $interface of $guest_name"
90 exit 11
91 fi
92
93 ipaddress=$(xe vm-list --minimal \
94 name-label=$guest_name \
95 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
96
97 if [ -z "$ipaddress" ]; then
98 sleep $period
99 ((i++))
100 else
101 echo $ipaddress
102 break
103 fi
104 done
105}
Mate Lakat9e326772013-05-08 16:42:22 +0100106
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100107function _vm_uuid() {
108 local vm_name_label
109
110 vm_name_label="$1"
111
112 xe vm-list name-label="$vm_name_label" --minimal
113}
114
Mate Lakat9e326772013-05-08 16:42:22 +0100115function _create_new_network() {
116 local name_label
117 name_label=$1
118
119 xe network-create name-label="$name_label"
120}
121
122function _multiple_networks_with_name() {
123 local name_label
124 name_label=$1
125
126 # A comma indicates multiple matches
127 xe network-list name-label="$name_label" --minimal | grep -q ","
128}
129
130function _network_exists() {
131 local name_label
132 name_label=$1
133
134 ! [ -z $(xe network-list name-label="$name_label" --minimal) ]
135}
136
137function _bridge_exists() {
138 local bridge
139 bridge=$1
140
141 ! [ -z $(xe network-list bridge="$bridge" --minimal) ]
142}
143
Mate Lakatf652e0f2013-05-21 18:12:48 +0100144function _network_uuid() {
145 local bridge_or_net_name
146 bridge_or_net_name=$1
147
148 if _bridge_exists "$bridge_or_net_name"; then
149 xe network-list bridge="$bridge_or_net_name" --minimal
150 else
151 xe network-list name-label="$bridge_or_net_name" --minimal
152 fi
153}
154
155function add_interface() {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100156 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100157 local bridge_or_network_name
158
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100159 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100160 bridge_or_network_name="$2"
161 device_number="$3"
162
163 local vm
164 local net
165
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100166 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100167 net=$(_network_uuid "$bridge_or_network_name")
168 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
169}
Mate Lakat9e326772013-05-08 16:42:22 +0100170
171function setup_network() {
172 local bridge_or_net_name
173 bridge_or_net_name=$1
174
175 if ! _bridge_exists "$bridge_or_net_name"; then
176 if _network_exists "$bridge_or_net_name"; then
177 if _multiple_networks_with_name "$bridge_or_net_name"; then
178 cat >&2 << EOF
179ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
180please review your XenServer network configuration / localrc file.
181EOF
182 exit 1
183 fi
184 else
185 _create_new_network "$bridge_or_net_name"
186 fi
187 fi
188}
189
190function bridge_for() {
191 local bridge_or_net_name
192 bridge_or_net_name=$1
193
194 if _bridge_exists "$bridge_or_net_name"; then
195 echo "$bridge_or_net_name"
196 else
197 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
198 fi
199}
200
201function xenapi_ip_on() {
202 local bridge_or_net_name
203 bridge_or_net_name=$1
204
205 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
206}
207
208function xenapi_is_listening_on() {
209 local bridge_or_net_name
210 bridge_or_net_name=$1
211
212 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
213}
214
215function parameter_is_specified() {
216 local parameter_name
217 parameter_name=$1
218
219 compgen -v | grep "$parameter_name"
220}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100221
222function append_kernel_cmdline()
223{
224 local vm_name_label
225 local kernel_args
226
227 vm_name_label="$1"
228 kernel_args="$2"
229
230 local vm
231 local pv_args
232
233 vm=$(_vm_uuid "$vm_name_label")
234 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
235 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
236}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100237
238function destroy_all_vifs_of()
239{
240 local vm_name_label
241
242 vm_name_label="$1"
243
244 local vm
245
246 vm=$(_vm_uuid "$vm_name_label")
247 IFS=,
248 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
249 xe vif-destroy uuid="$vif"
250 done
251 unset IFS
252}
Mate Lakatd8511032013-07-03 10:44:44 +0100253
254function have_multiple_hosts() {
255 xe host-list --minimal | grep -q ","
256}
257
258function attach_network() {
259 local bridge_or_net_name
260
261 bridge_or_net_name="$1"
262
263 local net
264 local host
265
266 net=$(_network_uuid "$bridge_or_net_name")
267 host=$(xe host-list --minimal)
268
269 xe network-attach uuid=$net host-uuid=$host
270}
Mate Lakat16ed0682013-08-30 13:28:31 +0100271
272function set_vm_memory() {
273 local vm_name_label
274 local memory
275
276 vm_name_label="$1"
277 memory="$2"
278
279 local vm
280
281 vm=$(_vm_uuid "$vm_name_label")
282
283 xe vm-memory-limits-set \
284 static-min=${memory}MiB \
285 static-max=${memory}MiB \
286 dynamic-min=${memory}MiB \
287 dynamic-max=${memory}MiB \
288 uuid=$vm
289}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100290
291function max_vcpus() {
292 local vm_name_label
293
294 vm_name_label="$1"
295
296 local vm
297 local host
298 local cpu_count
299
300 host=$(xe host-list --minimal)
301 vm=$(_vm_uuid "$vm_name_label")
302
303 cpu_count=$(xe host-param-get \
304 param-name=cpu_info \
305 uuid=$host |
306 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
307
308 if [ -z "$cpu_count" ]; then
309 # get dom0's vcpu count
310 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
311 fi
312
313 # Assert cpu_count is not empty
314 [ -n "$cpu_count" ]
315
316 # Assert ithas a numeric nonzero value
317 expr "$cpu_count" + 0
318
319 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
320 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
321}