blob: a1d56568a83655586ed5a94358b7d9fb735b0f51 [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 {
Bob Ball83dcf202013-09-29 21:45:49 +010072 xe pool-list params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +000073}
74
75function get_local_sr_path {
Bob Ball83dcf202013-09-29 21:45:49 +010076 pbd_path="/var/run/sr-mount/$(get_local_sr)"
77 pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "`
78 if [ -n "$pbd_device_config_path" ]; then
79 pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true`
80 pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""`
81 fi
82 echo $pbd_path
Mate Lakatfe586b12013-03-28 15:02:27 +000083}
Mate Lakat86446762013-05-12 18:34:29 +010084
85function find_ip_by_name() {
86 local guest_name="$1"
87 local interface="$2"
88
89 local period=10
90 local max_tries=10
91 local i=0
92
93 while true; do
94 if [ $i -ge $max_tries ]; then
95 echo "Timeout: ip address for interface $interface of $guest_name"
96 exit 11
97 fi
98
99 ipaddress=$(xe vm-list --minimal \
100 name-label=$guest_name \
101 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
102
103 if [ -z "$ipaddress" ]; then
104 sleep $period
105 ((i++))
106 else
107 echo $ipaddress
108 break
109 fi
110 done
111}
Mate Lakat9e326772013-05-08 16:42:22 +0100112
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100113function _vm_uuid() {
114 local vm_name_label
115
116 vm_name_label="$1"
117
118 xe vm-list name-label="$vm_name_label" --minimal
119}
120
Mate Lakat9e326772013-05-08 16:42:22 +0100121function _create_new_network() {
122 local name_label
123 name_label=$1
124
125 xe network-create name-label="$name_label"
126}
127
128function _multiple_networks_with_name() {
129 local name_label
130 name_label=$1
131
132 # A comma indicates multiple matches
133 xe network-list name-label="$name_label" --minimal | grep -q ","
134}
135
136function _network_exists() {
137 local name_label
138 name_label=$1
139
140 ! [ -z $(xe network-list name-label="$name_label" --minimal) ]
141}
142
143function _bridge_exists() {
144 local bridge
145 bridge=$1
146
147 ! [ -z $(xe network-list bridge="$bridge" --minimal) ]
148}
149
Mate Lakatf652e0f2013-05-21 18:12:48 +0100150function _network_uuid() {
151 local bridge_or_net_name
152 bridge_or_net_name=$1
153
154 if _bridge_exists "$bridge_or_net_name"; then
155 xe network-list bridge="$bridge_or_net_name" --minimal
156 else
157 xe network-list name-label="$bridge_or_net_name" --minimal
158 fi
159}
160
161function add_interface() {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100162 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100163 local bridge_or_network_name
164
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100165 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100166 bridge_or_network_name="$2"
167 device_number="$3"
168
169 local vm
170 local net
171
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100172 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100173 net=$(_network_uuid "$bridge_or_network_name")
174 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
175}
Mate Lakat9e326772013-05-08 16:42:22 +0100176
177function setup_network() {
178 local bridge_or_net_name
179 bridge_or_net_name=$1
180
181 if ! _bridge_exists "$bridge_or_net_name"; then
182 if _network_exists "$bridge_or_net_name"; then
183 if _multiple_networks_with_name "$bridge_or_net_name"; then
184 cat >&2 << EOF
185ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
186please review your XenServer network configuration / localrc file.
187EOF
188 exit 1
189 fi
190 else
191 _create_new_network "$bridge_or_net_name"
192 fi
193 fi
194}
195
196function bridge_for() {
197 local bridge_or_net_name
198 bridge_or_net_name=$1
199
200 if _bridge_exists "$bridge_or_net_name"; then
201 echo "$bridge_or_net_name"
202 else
203 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
204 fi
205}
206
207function xenapi_ip_on() {
208 local bridge_or_net_name
209 bridge_or_net_name=$1
210
211 ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"
212}
213
214function xenapi_is_listening_on() {
215 local bridge_or_net_name
216 bridge_or_net_name=$1
217
218 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
219}
220
221function parameter_is_specified() {
222 local parameter_name
223 parameter_name=$1
224
225 compgen -v | grep "$parameter_name"
226}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100227
228function append_kernel_cmdline()
229{
230 local vm_name_label
231 local kernel_args
232
233 vm_name_label="$1"
234 kernel_args="$2"
235
236 local vm
237 local pv_args
238
239 vm=$(_vm_uuid "$vm_name_label")
240 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
241 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
242}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100243
244function destroy_all_vifs_of()
245{
246 local vm_name_label
247
248 vm_name_label="$1"
249
250 local vm
251
252 vm=$(_vm_uuid "$vm_name_label")
253 IFS=,
254 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
255 xe vif-destroy uuid="$vif"
256 done
257 unset IFS
258}
Mate Lakatd8511032013-07-03 10:44:44 +0100259
260function have_multiple_hosts() {
261 xe host-list --minimal | grep -q ","
262}
263
264function attach_network() {
265 local bridge_or_net_name
266
267 bridge_or_net_name="$1"
268
269 local net
270 local host
271
272 net=$(_network_uuid "$bridge_or_net_name")
273 host=$(xe host-list --minimal)
274
275 xe network-attach uuid=$net host-uuid=$host
276}
Mate Lakat16ed0682013-08-30 13:28:31 +0100277
278function set_vm_memory() {
279 local vm_name_label
280 local memory
281
282 vm_name_label="$1"
283 memory="$2"
284
285 local vm
286
287 vm=$(_vm_uuid "$vm_name_label")
288
289 xe vm-memory-limits-set \
290 static-min=${memory}MiB \
291 static-max=${memory}MiB \
292 dynamic-min=${memory}MiB \
293 dynamic-max=${memory}MiB \
294 uuid=$vm
295}