blob: bc0c515e013d1208a29e06c34f5dfa53888322f0 [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
Ian Wienand7ae97292016-02-16 14:50:53 +110026 local local_path
27 local_path="$(get_local_sr_path)/os-guest-kernels"
28 mkdir -p $local_path
29 ln -s $local_path /boot/guest
Mate Lakatfe586b12013-03-28 15:02:27 +000030 fi
Mate Lakat57e3da92013-03-22 16:34:05 +000031}
32
Bob Ball39aeda22013-06-17 12:51:33 +010033function create_directory_for_images {
34 if [ -d "/images" ]; then
35 echo "INFO: /images directory already exists, using that" >&2
36 else
Ian Wienand7ae97292016-02-16 14:50:53 +110037 local local_path
38 local_path="$(get_local_sr_path)/os-images"
39 mkdir -p $local_path
40 ln -s $local_path /images
Bob Ball39aeda22013-06-17 12:51:33 +010041 fi
42}
43
Mate Lakatfe586b12013-03-28 15:02:27 +000044function get_local_sr {
Bob Ball83dcf202013-09-29 21:45:49 +010045 xe pool-list params=default-SR minimal=true
Mate Lakatfe586b12013-03-28 15:02:27 +000046}
47
48function get_local_sr_path {
Bob Ball83dcf202013-09-29 21:45:49 +010049 pbd_path="/var/run/sr-mount/$(get_local_sr)"
50 pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "`
51 if [ -n "$pbd_device_config_path" ]; then
52 pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true`
53 pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""`
54 fi
55 echo $pbd_path
Mate Lakatfe586b12013-03-28 15:02:27 +000056}
Mate Lakat86446762013-05-12 18:34:29 +010057
Ian Wienand62cae132014-09-04 21:25:45 +100058function find_ip_by_name {
Mate Lakat86446762013-05-12 18:34:29 +010059 local guest_name="$1"
60 local interface="$2"
61
62 local period=10
63 local max_tries=10
64 local i=0
65
66 while true; do
67 if [ $i -ge $max_tries ]; then
68 echo "Timeout: ip address for interface $interface of $guest_name"
69 exit 11
70 fi
71
72 ipaddress=$(xe vm-list --minimal \
73 name-label=$guest_name \
74 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
75
76 if [ -z "$ipaddress" ]; then
77 sleep $period
Ian Wienand7ae97292016-02-16 14:50:53 +110078 i=$((i+1))
Mate Lakat86446762013-05-12 18:34:29 +010079 else
80 echo $ipaddress
81 break
82 fi
83 done
84}
Mate Lakat9e326772013-05-08 16:42:22 +010085
Ian Wienand62cae132014-09-04 21:25:45 +100086function _vm_uuid {
Mate Lakat8ff33ce2013-05-30 13:26:58 +010087 local vm_name_label
88
89 vm_name_label="$1"
90
91 xe vm-list name-label="$vm_name_label" --minimal
92}
93
Ian Wienand62cae132014-09-04 21:25:45 +100094function _create_new_network {
Mate Lakat9e326772013-05-08 16:42:22 +010095 local name_label
96 name_label=$1
97
98 xe network-create name-label="$name_label"
99}
100
Ian Wienand62cae132014-09-04 21:25:45 +1000101function _multiple_networks_with_name {
Mate Lakat9e326772013-05-08 16:42:22 +0100102 local name_label
103 name_label=$1
104
105 # A comma indicates multiple matches
106 xe network-list name-label="$name_label" --minimal | grep -q ","
107}
108
Ian Wienand62cae132014-09-04 21:25:45 +1000109function _network_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100110 local name_label
111 name_label=$1
112
Mate Lakat2b8814d2013-09-25 17:07:06 +0100113 ! [ -z "$(xe network-list name-label="$name_label" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100114}
115
Ian Wienand62cae132014-09-04 21:25:45 +1000116function _bridge_exists {
Mate Lakat9e326772013-05-08 16:42:22 +0100117 local bridge
118 bridge=$1
119
Mate Lakat2b8814d2013-09-25 17:07:06 +0100120 ! [ -z "$(xe network-list bridge="$bridge" --minimal)" ]
Mate Lakat9e326772013-05-08 16:42:22 +0100121}
122
Ian Wienand62cae132014-09-04 21:25:45 +1000123function _network_uuid {
Mate Lakatf652e0f2013-05-21 18:12:48 +0100124 local bridge_or_net_name
125 bridge_or_net_name=$1
126
127 if _bridge_exists "$bridge_or_net_name"; then
128 xe network-list bridge="$bridge_or_net_name" --minimal
129 else
130 xe network-list name-label="$bridge_or_net_name" --minimal
131 fi
132}
133
Ian Wienand62cae132014-09-04 21:25:45 +1000134function add_interface {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100135 local vm_name_label
Mate Lakatf652e0f2013-05-21 18:12:48 +0100136 local bridge_or_network_name
137
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100138 vm_name_label="$1"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100139 bridge_or_network_name="$2"
140 device_number="$3"
141
142 local vm
143 local net
144
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100145 vm=$(_vm_uuid "$vm_name_label")
Mate Lakatf652e0f2013-05-21 18:12:48 +0100146 net=$(_network_uuid "$bridge_or_network_name")
147 xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number
148}
Mate Lakat9e326772013-05-08 16:42:22 +0100149
Ian Wienand62cae132014-09-04 21:25:45 +1000150function setup_network {
Mate Lakat9e326772013-05-08 16:42:22 +0100151 local bridge_or_net_name
152 bridge_or_net_name=$1
153
154 if ! _bridge_exists "$bridge_or_net_name"; then
155 if _network_exists "$bridge_or_net_name"; then
156 if _multiple_networks_with_name "$bridge_or_net_name"; then
157 cat >&2 << EOF
158ERROR: Multiple networks found matching name-label to "$bridge_or_net_name"
159please review your XenServer network configuration / localrc file.
160EOF
161 exit 1
162 fi
163 else
164 _create_new_network "$bridge_or_net_name"
165 fi
166 fi
167}
168
Ian Wienand62cae132014-09-04 21:25:45 +1000169function bridge_for {
Mate Lakat9e326772013-05-08 16:42:22 +0100170 local bridge_or_net_name
171 bridge_or_net_name=$1
172
173 if _bridge_exists "$bridge_or_net_name"; then
174 echo "$bridge_or_net_name"
175 else
176 xe network-list name-label="$bridge_or_net_name" params=bridge --minimal
177 fi
178}
179
Ian Wienand62cae132014-09-04 21:25:45 +1000180function xenapi_ip_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100181 local bridge_or_net_name
182 bridge_or_net_name=$1
183
jianghua wang78f6c1d2015-09-18 11:17:46 +0100184 ip -4 addr show $(bridge_for "$bridge_or_net_name") |\
185 awk '/inet/{split($2, ip, "/"); print ip[1];}'
Mate Lakat9e326772013-05-08 16:42:22 +0100186}
187
Ian Wienand62cae132014-09-04 21:25:45 +1000188function xenapi_is_listening_on {
Mate Lakat9e326772013-05-08 16:42:22 +0100189 local bridge_or_net_name
190 bridge_or_net_name=$1
191
192 ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ]
193}
194
Ian Wienand62cae132014-09-04 21:25:45 +1000195function parameter_is_specified {
Mate Lakat9e326772013-05-08 16:42:22 +0100196 local parameter_name
197 parameter_name=$1
198
199 compgen -v | grep "$parameter_name"
200}
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100201
Ian Wienand62cae132014-09-04 21:25:45 +1000202function append_kernel_cmdline {
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100203 local vm_name_label
204 local kernel_args
205
206 vm_name_label="$1"
207 kernel_args="$2"
208
209 local vm
210 local pv_args
211
212 vm=$(_vm_uuid "$vm_name_label")
213 pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm)
214 xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm
215}
Mate Lakat5a56cd62013-06-17 13:54:43 +0100216
Ian Wienand62cae132014-09-04 21:25:45 +1000217function destroy_all_vifs_of {
Mate Lakat5a56cd62013-06-17 13:54:43 +0100218 local vm_name_label
219
220 vm_name_label="$1"
221
222 local vm
223
224 vm=$(_vm_uuid "$vm_name_label")
225 IFS=,
226 for vif in $(xe vif-list vm-uuid=$vm --minimal); do
227 xe vif-destroy uuid="$vif"
228 done
229 unset IFS
230}
Mate Lakatd8511032013-07-03 10:44:44 +0100231
Ian Wienand62cae132014-09-04 21:25:45 +1000232function have_multiple_hosts {
Mate Lakatd8511032013-07-03 10:44:44 +0100233 xe host-list --minimal | grep -q ","
234}
235
Ian Wienand62cae132014-09-04 21:25:45 +1000236function attach_network {
Mate Lakatd8511032013-07-03 10:44:44 +0100237 local bridge_or_net_name
238
239 bridge_or_net_name="$1"
240
241 local net
242 local host
243
244 net=$(_network_uuid "$bridge_or_net_name")
245 host=$(xe host-list --minimal)
246
247 xe network-attach uuid=$net host-uuid=$host
248}
Mate Lakat16ed0682013-08-30 13:28:31 +0100249
Ian Wienand62cae132014-09-04 21:25:45 +1000250function set_vm_memory {
Mate Lakat16ed0682013-08-30 13:28:31 +0100251 local vm_name_label
252 local memory
253
254 vm_name_label="$1"
255 memory="$2"
256
257 local vm
258
259 vm=$(_vm_uuid "$vm_name_label")
260
261 xe vm-memory-limits-set \
262 static-min=${memory}MiB \
263 static-max=${memory}MiB \
264 dynamic-min=${memory}MiB \
265 dynamic-max=${memory}MiB \
266 uuid=$vm
267}
Mate Lakat9f878cb2013-10-04 09:56:24 +0100268
Ian Wienand62cae132014-09-04 21:25:45 +1000269function max_vcpus {
Mate Lakat9f878cb2013-10-04 09:56:24 +0100270 local vm_name_label
271
272 vm_name_label="$1"
273
274 local vm
275 local host
276 local cpu_count
277
278 host=$(xe host-list --minimal)
279 vm=$(_vm_uuid "$vm_name_label")
280
281 cpu_count=$(xe host-param-get \
282 param-name=cpu_info \
283 uuid=$host |
284 sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g')
285
286 if [ -z "$cpu_count" ]; then
287 # get dom0's vcpu count
288 cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l)
289 fi
290
291 # Assert cpu_count is not empty
292 [ -n "$cpu_count" ]
293
294 # Assert ithas a numeric nonzero value
295 expr "$cpu_count" + 0
296
jianghua5f8bd0e2017-03-14 08:04:53 +0000297 # 8 VCPUs should be enough for devstack VM; avoid using too
298 # many VCPUs:
299 # 1. too many VCPUs may trigger a kernel bug which result VM
300 # not able to boot:
301 # https://kernel.googlesource.com/pub/scm/linux/kernel/git/wsa/linux/+/e2e004acc7cbe3c531e752a270a74e95cde3ea48
302 # 2. The remaining CPUs can be used for other purpose:
303 # e.g. boot test VMs.
304 MAX_VCPUS=8
305 if [ $cpu_count -ge $MAX_VCPUS ]; then
306 cpu_count=$MAX_VCPUS
307 fi
308
Mate Lakat9f878cb2013-10-04 09:56:24 +0100309 xe vm-param-set uuid=$vm VCPUs-max=$cpu_count
310 xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count
311}
Mate Lakatd15c8a02014-02-04 12:38:14 +0000312
Ian Wienand62cae132014-09-04 21:25:45 +1000313function get_domid {
Mate Lakatd15c8a02014-02-04 12:38:14 +0000314 local vm_name_label
315
316 vm_name_label="$1"
317
318 xe vm-list name-label="$vm_name_label" params=dom-id minimal=true
319}
Huan Xie26edd7b2016-08-08 07:23:36 +0000320
321function install_conntrack_tools {
322 local xs_host
323 local xs_ver_major
324 local centos_ver
325 local conntrack_conf
326 xs_host=$(xe host-list --minimal)
327 xs_ver_major=$(xe host-param-get uuid=$xs_host param-name=software-version param-key=product_version_text_short | cut -d'.' -f 1)
328 if [ $xs_ver_major -gt 6 ]; then
329 # Only support conntrack-tools in Dom0 with XS7.0 and above
330 if [ ! -f /usr/sbin/conntrackd ]; then
331 sed -i s/#baseurl=/baseurl=/g /etc/yum.repos.d/CentOS-Base.repo
Huan Xieba3c8f42017-01-15 20:07:04 -0800332 centos_ver=$(yum version nogroups |grep Installed | cut -d' ' -f 2 | cut -d'/' -f 1 | cut -d'-' -f 1)
Huan Xie26edd7b2016-08-08 07:23:36 +0000333 yum install -y --enablerepo=base --releasever=$centos_ver conntrack-tools
334 # Backup conntrackd.conf after install conntrack-tools, use the one with statistic mode
335 mv /etc/conntrackd/conntrackd.conf /etc/conntrackd/conntrackd.conf.back
336 conntrack_conf=$(find /usr/share/doc -name conntrackd.conf |grep stats)
337 cp $conntrack_conf /etc/conntrackd/conntrackd.conf
338 fi
339 service conntrackd restart
340 fi
341}