Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Mate Lakat | 2781f3b | 2013-12-11 13:41:54 +0000 | [diff] [blame] | 3 | function die_with_error { |
| 4 | local err_msg |
| 5 | |
| 6 | err_msg="$1" |
| 7 | |
| 8 | echo "$err_msg" >&2 |
| 9 | exit 1 |
| 10 | } |
| 11 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 12 | function xapi_plugin_location { |
Bob Ball | f3b49e2 | 2014-12-09 17:37:03 +0000 | [diff] [blame] | 13 | for PLUGIN_DIR in "/etc/xapi.d/plugins/" "/usr/lib/xcp/plugins/" "/usr/lib/xapi/plugins" "/usr/lib64/xapi/plugins"; do |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 14 | if [ -d $PLUGIN_DIR ]; then |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 15 | echo $PLUGIN_DIR |
| 16 | return 0 |
| 17 | fi |
| 18 | done |
| 19 | return 1 |
| 20 | } |
| 21 | |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 22 | function create_directory_for_kernels { |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 23 | if [ -d "/boot/guest" ]; then |
| 24 | echo "INFO: /boot/guest directory already exists, using that" >&2 |
| 25 | else |
Ian Wienand | 7ae9729 | 2016-02-16 14:50:53 +1100 | [diff] [blame] | 26 | 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 30 | fi |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 33 | function create_directory_for_images { |
| 34 | if [ -d "/images" ]; then |
| 35 | echo "INFO: /images directory already exists, using that" >&2 |
| 36 | else |
Ian Wienand | 7ae9729 | 2016-02-16 14:50:53 +1100 | [diff] [blame] | 37 | local local_path |
| 38 | local_path="$(get_local_sr_path)/os-images" |
| 39 | mkdir -p $local_path |
| 40 | ln -s $local_path /images |
Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 41 | fi |
| 42 | } |
| 43 | |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 44 | function get_local_sr { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame] | 45 | xe pool-list params=default-SR minimal=true |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | function get_local_sr_path { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame] | 49 | 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 Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 56 | } |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 57 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 58 | function find_ip_by_name { |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 59 | 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 Wienand | 7ae9729 | 2016-02-16 14:50:53 +1100 | [diff] [blame] | 78 | i=$((i+1)) |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 79 | else |
| 80 | echo $ipaddress |
| 81 | break |
| 82 | fi |
| 83 | done |
| 84 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 85 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 86 | function _vm_uuid { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 87 | local vm_name_label |
| 88 | |
| 89 | vm_name_label="$1" |
| 90 | |
| 91 | xe vm-list name-label="$vm_name_label" --minimal |
| 92 | } |
| 93 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 94 | function _create_new_network { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 95 | local name_label |
| 96 | name_label=$1 |
| 97 | |
| 98 | xe network-create name-label="$name_label" |
| 99 | } |
| 100 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 101 | function _multiple_networks_with_name { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 102 | 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 Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 109 | function _network_exists { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 110 | local name_label |
| 111 | name_label=$1 |
| 112 | |
Mate Lakat | 2b8814d | 2013-09-25 17:07:06 +0100 | [diff] [blame] | 113 | ! [ -z "$(xe network-list name-label="$name_label" --minimal)" ] |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 116 | function _bridge_exists { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 117 | local bridge |
| 118 | bridge=$1 |
| 119 | |
Mate Lakat | 2b8814d | 2013-09-25 17:07:06 +0100 | [diff] [blame] | 120 | ! [ -z "$(xe network-list bridge="$bridge" --minimal)" ] |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 123 | function _network_uuid { |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 124 | 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 Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 134 | function add_interface { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 135 | local vm_name_label |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 136 | local bridge_or_network_name |
| 137 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 138 | vm_name_label="$1" |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 139 | bridge_or_network_name="$2" |
| 140 | device_number="$3" |
| 141 | |
| 142 | local vm |
| 143 | local net |
| 144 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 145 | vm=$(_vm_uuid "$vm_name_label") |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 146 | net=$(_network_uuid "$bridge_or_network_name") |
| 147 | xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number |
| 148 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 149 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 150 | function setup_network { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 151 | 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 |
| 158 | ERROR: Multiple networks found matching name-label to "$bridge_or_net_name" |
| 159 | please review your XenServer network configuration / localrc file. |
| 160 | EOF |
| 161 | exit 1 |
| 162 | fi |
| 163 | else |
| 164 | _create_new_network "$bridge_or_net_name" |
| 165 | fi |
| 166 | fi |
| 167 | } |
| 168 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 169 | function bridge_for { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 170 | 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 Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 180 | function xenapi_ip_on { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 181 | local bridge_or_net_name |
| 182 | bridge_or_net_name=$1 |
| 183 | |
jianghua wang | 78f6c1d | 2015-09-18 11:17:46 +0100 | [diff] [blame] | 184 | ip -4 addr show $(bridge_for "$bridge_or_net_name") |\ |
| 185 | awk '/inet/{split($2, ip, "/"); print ip[1];}' |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 188 | function xenapi_is_listening_on { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 189 | local bridge_or_net_name |
| 190 | bridge_or_net_name=$1 |
| 191 | |
| 192 | ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ] |
| 193 | } |
| 194 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 195 | function parameter_is_specified { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 196 | local parameter_name |
| 197 | parameter_name=$1 |
| 198 | |
| 199 | compgen -v | grep "$parameter_name" |
| 200 | } |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 201 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 202 | function append_kernel_cmdline { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 203 | 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 Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 216 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 217 | function destroy_all_vifs_of { |
Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 218 | 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 Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 231 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 232 | function have_multiple_hosts { |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 233 | xe host-list --minimal | grep -q "," |
| 234 | } |
| 235 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 236 | function attach_network { |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 237 | 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 Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 249 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 250 | function set_vm_memory { |
Mate Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 251 | 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 Lakat | 9f878cb | 2013-10-04 09:56:24 +0100 | [diff] [blame] | 268 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 269 | function max_vcpus { |
Mate Lakat | 9f878cb | 2013-10-04 09:56:24 +0100 | [diff] [blame] | 270 | 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 | |
| 297 | xe vm-param-set uuid=$vm VCPUs-max=$cpu_count |
| 298 | xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count |
| 299 | } |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 300 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 301 | function get_domid { |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 302 | local vm_name_label |
| 303 | |
| 304 | vm_name_label="$1" |
| 305 | |
| 306 | xe vm-list name-label="$vm_name_label" params=dom-id minimal=true |
| 307 | } |
Huan Xie | 26edd7b | 2016-08-08 07:23:36 +0000 | [diff] [blame] | 308 | |
| 309 | function install_conntrack_tools { |
| 310 | local xs_host |
| 311 | local xs_ver_major |
| 312 | local centos_ver |
| 313 | local conntrack_conf |
| 314 | xs_host=$(xe host-list --minimal) |
| 315 | xs_ver_major=$(xe host-param-get uuid=$xs_host param-name=software-version param-key=product_version_text_short | cut -d'.' -f 1) |
| 316 | if [ $xs_ver_major -gt 6 ]; then |
| 317 | # Only support conntrack-tools in Dom0 with XS7.0 and above |
| 318 | if [ ! -f /usr/sbin/conntrackd ]; then |
| 319 | sed -i s/#baseurl=/baseurl=/g /etc/yum.repos.d/CentOS-Base.repo |
Huan Xie | ba3c8f4 | 2017-01-15 20:07:04 -0800 | [diff] [blame] | 320 | centos_ver=$(yum version nogroups |grep Installed | cut -d' ' -f 2 | cut -d'/' -f 1 | cut -d'-' -f 1) |
Huan Xie | 26edd7b | 2016-08-08 07:23:36 +0000 | [diff] [blame] | 321 | yum install -y --enablerepo=base --releasever=$centos_ver conntrack-tools |
| 322 | # Backup conntrackd.conf after install conntrack-tools, use the one with statistic mode |
| 323 | mv /etc/conntrackd/conntrackd.conf /etc/conntrackd/conntrackd.conf.back |
| 324 | conntrack_conf=$(find /usr/share/doc -name conntrackd.conf |grep stats) |
| 325 | cp $conntrack_conf /etc/conntrackd/conntrackd.conf |
| 326 | fi |
| 327 | service conntrackd restart |
| 328 | fi |
| 329 | } |