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 |
| 26 | local LOCALPATH="$(get_local_sr_path)/os-guest-kernels" |
| 27 | mkdir -p $LOCALPATH |
| 28 | ln -s $LOCALPATH /boot/guest |
| 29 | fi |
Mate Lakat | 57e3da9 | 2013-03-22 16:34:05 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Bob Ball | 39aeda2 | 2013-06-17 12:51:33 +0100 | [diff] [blame] | 32 | function create_directory_for_images { |
| 33 | if [ -d "/images" ]; then |
| 34 | echo "INFO: /images directory already exists, using that" >&2 |
| 35 | else |
| 36 | local LOCALPATH="$(get_local_sr_path)/os-images" |
| 37 | mkdir -p $LOCALPATH |
| 38 | ln -s $LOCALPATH /images |
| 39 | fi |
| 40 | } |
| 41 | |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 42 | function get_local_sr { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame] | 43 | xe pool-list params=default-SR minimal=true |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | function get_local_sr_path { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame] | 47 | pbd_path="/var/run/sr-mount/$(get_local_sr)" |
| 48 | pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "` |
| 49 | if [ -n "$pbd_device_config_path" ]; then |
| 50 | pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true` |
| 51 | pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""` |
| 52 | fi |
| 53 | echo $pbd_path |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 54 | } |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 55 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 56 | function find_ip_by_name { |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 57 | local guest_name="$1" |
| 58 | local interface="$2" |
| 59 | |
| 60 | local period=10 |
| 61 | local max_tries=10 |
| 62 | local i=0 |
| 63 | |
| 64 | while true; do |
| 65 | if [ $i -ge $max_tries ]; then |
| 66 | echo "Timeout: ip address for interface $interface of $guest_name" |
| 67 | exit 11 |
| 68 | fi |
| 69 | |
| 70 | ipaddress=$(xe vm-list --minimal \ |
| 71 | name-label=$guest_name \ |
| 72 | params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p") |
| 73 | |
| 74 | if [ -z "$ipaddress" ]; then |
| 75 | sleep $period |
| 76 | ((i++)) |
| 77 | else |
| 78 | echo $ipaddress |
| 79 | break |
| 80 | fi |
| 81 | done |
| 82 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 83 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 84 | function _vm_uuid { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 85 | local vm_name_label |
| 86 | |
| 87 | vm_name_label="$1" |
| 88 | |
| 89 | xe vm-list name-label="$vm_name_label" --minimal |
| 90 | } |
| 91 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 92 | function _create_new_network { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 93 | local name_label |
| 94 | name_label=$1 |
| 95 | |
| 96 | xe network-create name-label="$name_label" |
| 97 | } |
| 98 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 99 | function _multiple_networks_with_name { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 100 | local name_label |
| 101 | name_label=$1 |
| 102 | |
| 103 | # A comma indicates multiple matches |
| 104 | xe network-list name-label="$name_label" --minimal | grep -q "," |
| 105 | } |
| 106 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 107 | function _network_exists { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 108 | local name_label |
| 109 | name_label=$1 |
| 110 | |
Mate Lakat | 2b8814d | 2013-09-25 17:07:06 +0100 | [diff] [blame] | 111 | ! [ -z "$(xe network-list name-label="$name_label" --minimal)" ] |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 114 | function _bridge_exists { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 115 | local bridge |
| 116 | bridge=$1 |
| 117 | |
Mate Lakat | 2b8814d | 2013-09-25 17:07:06 +0100 | [diff] [blame] | 118 | ! [ -z "$(xe network-list bridge="$bridge" --minimal)" ] |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 121 | function _network_uuid { |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 122 | local bridge_or_net_name |
| 123 | bridge_or_net_name=$1 |
| 124 | |
| 125 | if _bridge_exists "$bridge_or_net_name"; then |
| 126 | xe network-list bridge="$bridge_or_net_name" --minimal |
| 127 | else |
| 128 | xe network-list name-label="$bridge_or_net_name" --minimal |
| 129 | fi |
| 130 | } |
| 131 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 132 | function add_interface { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 133 | local vm_name_label |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 134 | local bridge_or_network_name |
| 135 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 136 | vm_name_label="$1" |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 137 | bridge_or_network_name="$2" |
| 138 | device_number="$3" |
| 139 | |
| 140 | local vm |
| 141 | local net |
| 142 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 143 | vm=$(_vm_uuid "$vm_name_label") |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 144 | net=$(_network_uuid "$bridge_or_network_name") |
| 145 | xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number |
| 146 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 147 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 148 | function setup_network { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 149 | local bridge_or_net_name |
| 150 | bridge_or_net_name=$1 |
| 151 | |
| 152 | if ! _bridge_exists "$bridge_or_net_name"; then |
| 153 | if _network_exists "$bridge_or_net_name"; then |
| 154 | if _multiple_networks_with_name "$bridge_or_net_name"; then |
| 155 | cat >&2 << EOF |
| 156 | ERROR: Multiple networks found matching name-label to "$bridge_or_net_name" |
| 157 | please review your XenServer network configuration / localrc file. |
| 158 | EOF |
| 159 | exit 1 |
| 160 | fi |
| 161 | else |
| 162 | _create_new_network "$bridge_or_net_name" |
| 163 | fi |
| 164 | fi |
| 165 | } |
| 166 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 167 | function bridge_for { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 168 | local bridge_or_net_name |
| 169 | bridge_or_net_name=$1 |
| 170 | |
| 171 | if _bridge_exists "$bridge_or_net_name"; then |
| 172 | echo "$bridge_or_net_name" |
| 173 | else |
| 174 | xe network-list name-label="$bridge_or_net_name" params=bridge --minimal |
| 175 | fi |
| 176 | } |
| 177 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 178 | function xenapi_ip_on { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 179 | local bridge_or_net_name |
| 180 | bridge_or_net_name=$1 |
| 181 | |
jianghua wang | 78f6c1d | 2015-09-18 11:17:46 +0100 | [diff] [blame] | 182 | ip -4 addr show $(bridge_for "$bridge_or_net_name") |\ |
| 183 | awk '/inet/{split($2, ip, "/"); print ip[1];}' |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 186 | function xenapi_is_listening_on { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 187 | local bridge_or_net_name |
| 188 | bridge_or_net_name=$1 |
| 189 | |
| 190 | ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ] |
| 191 | } |
| 192 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 193 | function parameter_is_specified { |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 194 | local parameter_name |
| 195 | parameter_name=$1 |
| 196 | |
| 197 | compgen -v | grep "$parameter_name" |
| 198 | } |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 199 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 200 | function append_kernel_cmdline { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 201 | local vm_name_label |
| 202 | local kernel_args |
| 203 | |
| 204 | vm_name_label="$1" |
| 205 | kernel_args="$2" |
| 206 | |
| 207 | local vm |
| 208 | local pv_args |
| 209 | |
| 210 | vm=$(_vm_uuid "$vm_name_label") |
| 211 | pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm) |
| 212 | xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm |
| 213 | } |
Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 214 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 215 | function destroy_all_vifs_of { |
Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 216 | local vm_name_label |
| 217 | |
| 218 | vm_name_label="$1" |
| 219 | |
| 220 | local vm |
| 221 | |
| 222 | vm=$(_vm_uuid "$vm_name_label") |
| 223 | IFS=, |
| 224 | for vif in $(xe vif-list vm-uuid=$vm --minimal); do |
| 225 | xe vif-destroy uuid="$vif" |
| 226 | done |
| 227 | unset IFS |
| 228 | } |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 229 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 230 | function have_multiple_hosts { |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 231 | xe host-list --minimal | grep -q "," |
| 232 | } |
| 233 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 234 | function attach_network { |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 235 | local bridge_or_net_name |
| 236 | |
| 237 | bridge_or_net_name="$1" |
| 238 | |
| 239 | local net |
| 240 | local host |
| 241 | |
| 242 | net=$(_network_uuid "$bridge_or_net_name") |
| 243 | host=$(xe host-list --minimal) |
| 244 | |
| 245 | xe network-attach uuid=$net host-uuid=$host |
| 246 | } |
Mate Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 247 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 248 | function set_vm_memory { |
Mate Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 249 | local vm_name_label |
| 250 | local memory |
| 251 | |
| 252 | vm_name_label="$1" |
| 253 | memory="$2" |
| 254 | |
| 255 | local vm |
| 256 | |
| 257 | vm=$(_vm_uuid "$vm_name_label") |
| 258 | |
| 259 | xe vm-memory-limits-set \ |
| 260 | static-min=${memory}MiB \ |
| 261 | static-max=${memory}MiB \ |
| 262 | dynamic-min=${memory}MiB \ |
| 263 | dynamic-max=${memory}MiB \ |
| 264 | uuid=$vm |
| 265 | } |
Mate Lakat | 9f878cb | 2013-10-04 09:56:24 +0100 | [diff] [blame] | 266 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 267 | function max_vcpus { |
Mate Lakat | 9f878cb | 2013-10-04 09:56:24 +0100 | [diff] [blame] | 268 | local vm_name_label |
| 269 | |
| 270 | vm_name_label="$1" |
| 271 | |
| 272 | local vm |
| 273 | local host |
| 274 | local cpu_count |
| 275 | |
| 276 | host=$(xe host-list --minimal) |
| 277 | vm=$(_vm_uuid "$vm_name_label") |
| 278 | |
| 279 | cpu_count=$(xe host-param-get \ |
| 280 | param-name=cpu_info \ |
| 281 | uuid=$host | |
| 282 | sed -e 's/^.*cpu_count: \([0-9]*\);.*$/\1/g') |
| 283 | |
| 284 | if [ -z "$cpu_count" ]; then |
| 285 | # get dom0's vcpu count |
| 286 | cpu_count=$(cat /proc/cpuinfo | grep processor | wc -l) |
| 287 | fi |
| 288 | |
| 289 | # Assert cpu_count is not empty |
| 290 | [ -n "$cpu_count" ] |
| 291 | |
| 292 | # Assert ithas a numeric nonzero value |
| 293 | expr "$cpu_count" + 0 |
| 294 | |
| 295 | xe vm-param-set uuid=$vm VCPUs-max=$cpu_count |
| 296 | xe vm-param-set uuid=$vm VCPUs-at-startup=$cpu_count |
| 297 | } |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 298 | |
Ian Wienand | 62cae13 | 2014-09-04 21:25:45 +1000 | [diff] [blame] | 299 | function get_domid { |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 300 | local vm_name_label |
| 301 | |
| 302 | vm_name_label="$1" |
| 303 | |
| 304 | xe vm-list name-label="$vm_name_label" params=dom-id minimal=true |
| 305 | } |