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