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