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 { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame^] | 72 | xe pool-list params=default-SR minimal=true |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | function get_local_sr_path { |
Bob Ball | 83dcf20 | 2013-09-29 21:45:49 +0100 | [diff] [blame^] | 76 | pbd_path="/var/run/sr-mount/$(get_local_sr)" |
| 77 | pbd_device_config_path=`xe pbd-list sr-uuid=$(get_local_sr) params=device-config | grep " path: "` |
| 78 | if [ -n "$pbd_device_config_path" ]; then |
| 79 | pbd_uuid=`xe pbd-list sr-uuid=$(get_local_sr) minimal=true` |
| 80 | pbd_path=`xe pbd-param-get uuid=$pbd_uuid param-name=device-config param-key=path || echo ""` |
| 81 | fi |
| 82 | echo $pbd_path |
Mate Lakat | fe586b1 | 2013-03-28 15:02:27 +0000 | [diff] [blame] | 83 | } |
Mate Lakat | 8644676 | 2013-05-12 18:34:29 +0100 | [diff] [blame] | 84 | |
| 85 | function find_ip_by_name() { |
| 86 | local guest_name="$1" |
| 87 | local interface="$2" |
| 88 | |
| 89 | local period=10 |
| 90 | local max_tries=10 |
| 91 | local i=0 |
| 92 | |
| 93 | while true; do |
| 94 | if [ $i -ge $max_tries ]; then |
| 95 | echo "Timeout: ip address for interface $interface of $guest_name" |
| 96 | exit 11 |
| 97 | fi |
| 98 | |
| 99 | ipaddress=$(xe vm-list --minimal \ |
| 100 | name-label=$guest_name \ |
| 101 | params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p") |
| 102 | |
| 103 | if [ -z "$ipaddress" ]; then |
| 104 | sleep $period |
| 105 | ((i++)) |
| 106 | else |
| 107 | echo $ipaddress |
| 108 | break |
| 109 | fi |
| 110 | done |
| 111 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 112 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 113 | function _vm_uuid() { |
| 114 | local vm_name_label |
| 115 | |
| 116 | vm_name_label="$1" |
| 117 | |
| 118 | xe vm-list name-label="$vm_name_label" --minimal |
| 119 | } |
| 120 | |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 121 | function _create_new_network() { |
| 122 | local name_label |
| 123 | name_label=$1 |
| 124 | |
| 125 | xe network-create name-label="$name_label" |
| 126 | } |
| 127 | |
| 128 | function _multiple_networks_with_name() { |
| 129 | local name_label |
| 130 | name_label=$1 |
| 131 | |
| 132 | # A comma indicates multiple matches |
| 133 | xe network-list name-label="$name_label" --minimal | grep -q "," |
| 134 | } |
| 135 | |
| 136 | function _network_exists() { |
| 137 | local name_label |
| 138 | name_label=$1 |
| 139 | |
| 140 | ! [ -z $(xe network-list name-label="$name_label" --minimal) ] |
| 141 | } |
| 142 | |
| 143 | function _bridge_exists() { |
| 144 | local bridge |
| 145 | bridge=$1 |
| 146 | |
| 147 | ! [ -z $(xe network-list bridge="$bridge" --minimal) ] |
| 148 | } |
| 149 | |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 150 | function _network_uuid() { |
| 151 | local bridge_or_net_name |
| 152 | bridge_or_net_name=$1 |
| 153 | |
| 154 | if _bridge_exists "$bridge_or_net_name"; then |
| 155 | xe network-list bridge="$bridge_or_net_name" --minimal |
| 156 | else |
| 157 | xe network-list name-label="$bridge_or_net_name" --minimal |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | function add_interface() { |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 162 | local vm_name_label |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 163 | local bridge_or_network_name |
| 164 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 165 | vm_name_label="$1" |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 166 | bridge_or_network_name="$2" |
| 167 | device_number="$3" |
| 168 | |
| 169 | local vm |
| 170 | local net |
| 171 | |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 172 | vm=$(_vm_uuid "$vm_name_label") |
Mate Lakat | f652e0f | 2013-05-21 18:12:48 +0100 | [diff] [blame] | 173 | net=$(_network_uuid "$bridge_or_network_name") |
| 174 | xe vif-create network-uuid=$net vm-uuid=$vm device=$device_number |
| 175 | } |
Mate Lakat | 9e32677 | 2013-05-08 16:42:22 +0100 | [diff] [blame] | 176 | |
| 177 | function setup_network() { |
| 178 | local bridge_or_net_name |
| 179 | bridge_or_net_name=$1 |
| 180 | |
| 181 | if ! _bridge_exists "$bridge_or_net_name"; then |
| 182 | if _network_exists "$bridge_or_net_name"; then |
| 183 | if _multiple_networks_with_name "$bridge_or_net_name"; then |
| 184 | cat >&2 << EOF |
| 185 | ERROR: Multiple networks found matching name-label to "$bridge_or_net_name" |
| 186 | please review your XenServer network configuration / localrc file. |
| 187 | EOF |
| 188 | exit 1 |
| 189 | fi |
| 190 | else |
| 191 | _create_new_network "$bridge_or_net_name" |
| 192 | fi |
| 193 | fi |
| 194 | } |
| 195 | |
| 196 | function bridge_for() { |
| 197 | local bridge_or_net_name |
| 198 | bridge_or_net_name=$1 |
| 199 | |
| 200 | if _bridge_exists "$bridge_or_net_name"; then |
| 201 | echo "$bridge_or_net_name" |
| 202 | else |
| 203 | xe network-list name-label="$bridge_or_net_name" params=bridge --minimal |
| 204 | fi |
| 205 | } |
| 206 | |
| 207 | function xenapi_ip_on() { |
| 208 | local bridge_or_net_name |
| 209 | bridge_or_net_name=$1 |
| 210 | |
| 211 | ifconfig $(bridge_for "$bridge_or_net_name") | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//" |
| 212 | } |
| 213 | |
| 214 | function xenapi_is_listening_on() { |
| 215 | local bridge_or_net_name |
| 216 | bridge_or_net_name=$1 |
| 217 | |
| 218 | ! [ -z $(xenapi_ip_on "$bridge_or_net_name") ] |
| 219 | } |
| 220 | |
| 221 | function parameter_is_specified() { |
| 222 | local parameter_name |
| 223 | parameter_name=$1 |
| 224 | |
| 225 | compgen -v | grep "$parameter_name" |
| 226 | } |
Mate Lakat | 8ff33ce | 2013-05-30 13:26:58 +0100 | [diff] [blame] | 227 | |
| 228 | function append_kernel_cmdline() |
| 229 | { |
| 230 | local vm_name_label |
| 231 | local kernel_args |
| 232 | |
| 233 | vm_name_label="$1" |
| 234 | kernel_args="$2" |
| 235 | |
| 236 | local vm |
| 237 | local pv_args |
| 238 | |
| 239 | vm=$(_vm_uuid "$vm_name_label") |
| 240 | pv_args=$(xe vm-param-get param-name=PV-args uuid=$vm) |
| 241 | xe vm-param-set PV-args="$pv_args $kernel_args" uuid=$vm |
| 242 | } |
Mate Lakat | 5a56cd6 | 2013-06-17 13:54:43 +0100 | [diff] [blame] | 243 | |
| 244 | function destroy_all_vifs_of() |
| 245 | { |
| 246 | local vm_name_label |
| 247 | |
| 248 | vm_name_label="$1" |
| 249 | |
| 250 | local vm |
| 251 | |
| 252 | vm=$(_vm_uuid "$vm_name_label") |
| 253 | IFS=, |
| 254 | for vif in $(xe vif-list vm-uuid=$vm --minimal); do |
| 255 | xe vif-destroy uuid="$vif" |
| 256 | done |
| 257 | unset IFS |
| 258 | } |
Mate Lakat | d851103 | 2013-07-03 10:44:44 +0100 | [diff] [blame] | 259 | |
| 260 | function have_multiple_hosts() { |
| 261 | xe host-list --minimal | grep -q "," |
| 262 | } |
| 263 | |
| 264 | function attach_network() { |
| 265 | local bridge_or_net_name |
| 266 | |
| 267 | bridge_or_net_name="$1" |
| 268 | |
| 269 | local net |
| 270 | local host |
| 271 | |
| 272 | net=$(_network_uuid "$bridge_or_net_name") |
| 273 | host=$(xe host-list --minimal) |
| 274 | |
| 275 | xe network-attach uuid=$net host-uuid=$host |
| 276 | } |
Mate Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 277 | |
| 278 | function set_vm_memory() { |
| 279 | local vm_name_label |
| 280 | local memory |
| 281 | |
| 282 | vm_name_label="$1" |
| 283 | memory="$2" |
| 284 | |
| 285 | local vm |
| 286 | |
| 287 | vm=$(_vm_uuid "$vm_name_label") |
| 288 | |
| 289 | xe vm-memory-limits-set \ |
| 290 | static-min=${memory}MiB \ |
| 291 | static-max=${memory}MiB \ |
| 292 | dynamic-min=${memory}MiB \ |
| 293 | dynamic-max=${memory}MiB \ |
| 294 | uuid=$vm |
| 295 | } |