Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2011 Citrix Systems, Inc. |
| 4 | # Copyright 2011 OpenStack LLC. |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | set -eux |
| 21 | |
| 22 | . /etc/xensource-inventory |
| 23 | |
| 24 | NAME="XenServer OpenStack VPX" |
| 25 | DATA_VDI_SIZE="500MiB" |
| 26 | BRIDGE_M= |
| 27 | BRIDGE_P= |
| 28 | KERNEL_PARAMS= |
| 29 | VPX_FILE=os-vpx.xva |
| 30 | AS_TEMPLATE= |
| 31 | FROM_TEMPLATE= |
| 32 | RAM= |
| 33 | WAIT_FOR_NETWORK= |
| 34 | BALLOONING= |
| 35 | |
| 36 | usage() |
| 37 | { |
| 38 | cat << EOF |
| 39 | |
| 40 | Usage: $0 [-f FILE_PATH] [-d DISK_SIZE] [-v BRIDGE_NAME] [-m BRIDGE_NAME] [-p BRIDGE_NAME] |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame^] | 41 | [-k PARAMS] [-r RAM] [-i|-c] [-w] [-b] [-l NAME_LABEL] |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 42 | |
| 43 | Installs XenServer OpenStack VPX. |
| 44 | |
| 45 | OPTIONS: |
| 46 | |
| 47 | -h Shows this message. |
| 48 | -i Install OpenStack VPX as template. |
| 49 | -c Clone from existing template. |
| 50 | -w Wait for the network settings to show up before exiting. |
| 51 | -b Enable memory ballooning. When set min_RAM=RAM/2 max_RAM=RAM. |
| 52 | -f path Specifies the path to the XVA. |
| 53 | Default to ./os-vpx.xva. |
| 54 | -d disk-size Specifies the size in MiB for the data disk. |
| 55 | Defaults to 500 MiB. |
| 56 | -m bridge Specifies the bridge for the isolated management network. |
| 57 | Defaults to xenbr0. |
| 58 | -v bridge Specifies the bridge for the vm network |
| 59 | -p bridge Specifies the bridge for the externally facing network. |
| 60 | -k params Specifies kernel parameters. |
| 61 | -r MiB Specifies RAM used by the VPX, in MiB. |
| 62 | By default it will take the value from the XVA. |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame^] | 63 | -l name Specifies the name label for the VM. |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 64 | |
| 65 | EXAMPLES: |
| 66 | |
| 67 | Create a VPX that connects to the isolated management network using the |
| 68 | default bridge with a data disk of 1GiB: |
| 69 | install-os-vpx.sh -f /root/os-vpx-devel.xva -d 1024 |
| 70 | |
| 71 | Create a VPX that connects to the isolated management network using xenbr1 |
| 72 | as bridge: |
| 73 | install-os-vpx.sh -m xenbr1 |
| 74 | |
| 75 | Create a VPX that connects to both the management and public networks |
| 76 | using xenbr1 and xapi4 as bridges: |
| 77 | install-os-vpx.sh -m xenbr1 -p xapi4 |
| 78 | |
| 79 | Create a VPX that connects to both the management and public networks |
| 80 | using the default for management traffic: |
| 81 | install-os-vpx.sh -m xapi4 |
| 82 | |
| 83 | Create a VPX that automatically becomes the master: |
| 84 | install-os-vpx.sh -k geppetto_master=true |
| 85 | |
| 86 | EOF |
| 87 | } |
| 88 | |
| 89 | get_params() |
| 90 | { |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame^] | 91 | while getopts "hicwbf:d:v:m:p:k:r:l:" OPTION; |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 92 | do |
| 93 | case $OPTION in |
| 94 | h) usage |
| 95 | exit 1 |
| 96 | ;; |
| 97 | i) |
| 98 | AS_TEMPLATE=1 |
| 99 | ;; |
| 100 | c) |
| 101 | FROM_TEMPLATE=1 |
| 102 | ;; |
| 103 | w) |
| 104 | WAIT_FOR_NETWORK=1 |
| 105 | ;; |
| 106 | b) |
| 107 | BALLOONING=1 |
| 108 | ;; |
| 109 | f) |
| 110 | VPX_FILE=$OPTARG |
| 111 | ;; |
| 112 | d) |
| 113 | DATA_VDI_SIZE="${OPTARG}MiB" |
| 114 | ;; |
| 115 | m) |
| 116 | BRIDGE_M=$OPTARG |
| 117 | ;; |
| 118 | p) |
| 119 | BRIDGE_P=$OPTARG |
| 120 | ;; |
| 121 | k) |
| 122 | KERNEL_PARAMS=$OPTARG |
| 123 | ;; |
| 124 | r) |
| 125 | RAM=$OPTARG |
| 126 | ;; |
| 127 | v) |
| 128 | BRIDGE_V=$OPTARG |
| 129 | ;; |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame^] | 130 | l) |
| 131 | NAME_LABEL=$OPTARG |
| 132 | ;; |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 133 | ?) |
| 134 | usage |
| 135 | exit |
| 136 | ;; |
| 137 | esac |
| 138 | done |
| 139 | if [[ -z $BRIDGE_M ]] |
| 140 | then |
| 141 | BRIDGE_M=xenbr0 |
| 142 | fi |
| 143 | } |
| 144 | |
| 145 | |
| 146 | xe_min() |
| 147 | { |
| 148 | local cmd="$1" |
| 149 | shift |
| 150 | xe "$cmd" --minimal "$@" |
| 151 | } |
| 152 | |
| 153 | |
| 154 | get_dest_sr() |
| 155 | { |
| 156 | IFS=, |
| 157 | sr_uuids=$(xe sr-list --minimal other-config:i18n-key=local-storage) |
| 158 | dest_sr="" |
| 159 | for sr_uuid in $sr_uuids |
| 160 | do |
| 161 | pbd=$(xe pbd-list --minimal sr-uuid=$sr_uuid host-uuid=$INSTALLATION_UUID) |
| 162 | if [ "$pbd" ] |
| 163 | then |
| 164 | echo "$sr_uuid" |
| 165 | unset IFS |
| 166 | return |
| 167 | fi |
| 168 | done |
| 169 | unset IFS |
| 170 | |
| 171 | dest_sr=$(xe_min sr-list uuid=$(xe_min pool-list params=default-SR)) |
| 172 | if [ "$dest_sr" = "" ] |
| 173 | then |
| 174 | echo "No local storage and no default storage; cannot import VPX." >&2 |
| 175 | exit 1 |
| 176 | else |
| 177 | echo "$dest_sr" |
| 178 | fi |
| 179 | } |
| 180 | |
| 181 | |
| 182 | find_network() |
| 183 | { |
| 184 | result=$(xe_min network-list bridge="$1") |
| 185 | if [ "$result" = "" ] |
| 186 | then |
| 187 | result=$(xe_min network-list name-label="$1") |
| 188 | fi |
| 189 | echo "$result" |
| 190 | } |
| 191 | |
| 192 | |
| 193 | find_template() |
| 194 | { |
| 195 | xe_min template-list other-config:os-vpx=true |
| 196 | } |
| 197 | |
| 198 | |
| 199 | renumber_system_disk() |
| 200 | { |
| 201 | local v="$1" |
| 202 | local vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk userdevice=xvda \ |
| 203 | params=vdi-uuid) |
| 204 | if [ "$vdi_uuid" ] |
| 205 | then |
| 206 | local vbd_uuid=$(xe_min vbd-list vm-uuid="$v" vdi-uuid="$vdi_uuid") |
| 207 | xe vbd-destroy uuid="$vbd_uuid" |
| 208 | local new_vbd_uuid=$(xe vbd-create vm-uuid="$v" vdi-uuid="$vdi_uuid" \ |
| 209 | device=0 bootable=true type=Disk) |
| 210 | xe vbd-param-set other-config:owner uuid="$new_vbd_uuid" |
| 211 | fi |
| 212 | } |
| 213 | |
| 214 | |
| 215 | create_vif() |
| 216 | { |
| 217 | xe vif-create vm-uuid="$1" network-uuid="$2" device="$3" |
| 218 | } |
| 219 | |
| 220 | create_gi_vif() |
| 221 | { |
| 222 | local v="$1" |
| 223 | # Note that we've made the outbound device eth1, so that it comes up after |
| 224 | # the guest installer VIF, which means that the outbound one wins in terms |
| 225 | # of gateway. |
| 226 | local gi_network_uuid=$(xe_min network-list \ |
| 227 | other-config:is_guest_installer_network=true) |
| 228 | create_vif "$v" "$gi_network_uuid" "0" >/dev/null |
| 229 | } |
| 230 | |
| 231 | create_vm_vif() |
| 232 | { |
| 233 | local v="$1" |
| 234 | echo "Installing management interface on $BRIDGE_V." |
| 235 | local out_network_uuid=$(find_network "$BRIDGE_V") |
| 236 | create_vif "$v" "$out_network_uuid" "1" >/dev/null |
| 237 | } |
| 238 | |
| 239 | create_management_vif() |
| 240 | { |
| 241 | local v="$1" |
| 242 | echo "Installing management interface on $BRIDGE_M." |
| 243 | local out_network_uuid=$(find_network "$BRIDGE_M") |
| 244 | create_vif "$v" "$out_network_uuid" "2" >/dev/null |
| 245 | } |
| 246 | |
| 247 | |
| 248 | # This installs the interface for public traffic, only if a bridge is specified |
| 249 | # The interface is not configured at this stage, but it will be, once the admin |
| 250 | # tasks are complete for the services of this VPX |
| 251 | create_public_vif() |
| 252 | { |
| 253 | local v="$1" |
| 254 | if [[ -z $BRIDGE_P ]] |
| 255 | then |
| 256 | echo "Skipping installation of interface for public traffic." |
| 257 | else |
| 258 | echo "Installing public interface on $BRIDGE_P." |
| 259 | pub_network_uuid=$(find_network "$BRIDGE_P") |
| 260 | create_vif "$v" "$pub_network_uuid" "3" >/dev/null |
| 261 | fi |
| 262 | } |
| 263 | |
| 264 | |
| 265 | label_system_disk() |
| 266 | { |
| 267 | local v="$1" |
| 268 | local vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk userdevice=0 \ |
| 269 | params=vdi-uuid) |
| 270 | xe vdi-param-set \ |
| 271 | name-label="$NAME system disk" \ |
| 272 | other-config:os-vpx=true \ |
| 273 | uuid=$vdi_uuid |
| 274 | } |
| 275 | |
| 276 | |
| 277 | create_data_disk() |
| 278 | { |
| 279 | local v="$1" |
| 280 | |
| 281 | local sys_vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk params=vdi-uuid) |
| 282 | local data_vdi_uuid=$(xe_min vdi-list other-config:os-vpx-data=true) |
| 283 | |
| 284 | if echo "$data_vdi_uuid" | grep -q , |
| 285 | then |
| 286 | echo "Multiple data disks found -- assuming that you want a new one." |
| 287 | data_vdi_uuid="" |
| 288 | else |
| 289 | data_in_use=$(xe_min vbd-list vdi-uuid="$data_vdi_uuid") |
| 290 | if [ "$data_in_use" != "" ] |
| 291 | then |
| 292 | echo "Data disk already in use -- will create another one." |
| 293 | data_vdi_uuid="" |
| 294 | fi |
| 295 | fi |
| 296 | |
| 297 | if [ "$data_vdi_uuid" = "" ] |
| 298 | then |
| 299 | echo -n "Creating new data disk ($DATA_VDI_SIZE)... " |
| 300 | sr_uuid=$(xe_min vdi-list params=sr-uuid uuid="$sys_vdi_uuid") |
| 301 | data_vdi_uuid=$(xe vdi-create name-label="$NAME data disk" \ |
| 302 | sr-uuid="$sr_uuid" \ |
| 303 | type=user \ |
| 304 | virtual-size="$DATA_VDI_SIZE") |
| 305 | xe vdi-param-set \ |
| 306 | other-config:os-vpx-data=true \ |
| 307 | uuid="$data_vdi_uuid" |
| 308 | dom0_uuid=$(xe_min vm-list is-control-domain=true) |
| 309 | vbd_uuid=$(xe vbd-create device=autodetect type=Disk \ |
| 310 | vdi-uuid="$data_vdi_uuid" vm-uuid="$dom0_uuid") |
| 311 | xe vbd-plug uuid=$vbd_uuid |
| 312 | dev=$(xe_min vbd-list params=device uuid=$vbd_uuid) |
| 313 | mke2fs -q -j -m0 /dev/$dev |
| 314 | e2label /dev/$dev vpxstate |
| 315 | xe vbd-unplug uuid=$vbd_uuid |
| 316 | xe vbd-destroy uuid=$vbd_uuid |
| 317 | else |
| 318 | echo -n "Attaching old data disk... " |
| 319 | fi |
| 320 | vbd_uuid=$(xe vbd-create device=2 type=Disk \ |
| 321 | vdi-uuid="$data_vdi_uuid" vm-uuid="$v") |
| 322 | xe vbd-param-set other-config:os-vpx-data=true uuid=$vbd_uuid |
| 323 | echo "done." |
| 324 | } |
| 325 | |
| 326 | |
| 327 | set_kernel_params() |
| 328 | { |
| 329 | local v="$1" |
| 330 | local args=$KERNEL_PARAMS |
| 331 | local cmdline=$(cat /proc/cmdline) |
| 332 | for word in $cmdline |
| 333 | do |
| 334 | if echo "$word" | grep -q "geppetto" |
| 335 | then |
| 336 | args="$word $args" |
| 337 | fi |
| 338 | done |
| 339 | if [ "$args" != "" ] |
| 340 | then |
| 341 | echo "Passing Geppetto args to VPX: $args." |
| 342 | xe vm-param-set PV-args="$args" uuid="$v" |
| 343 | fi |
| 344 | } |
| 345 | |
| 346 | |
| 347 | set_memory() |
| 348 | { |
| 349 | local v="$1" |
| 350 | if [ "$RAM" != "" ] |
| 351 | then |
| 352 | echo "Setting RAM to $RAM MiB." |
| 353 | [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM |
| 354 | xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \ |
| 355 | dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \ |
| 356 | uuid="$v" |
| 357 | fi |
| 358 | } |
| 359 | |
| 360 | |
| 361 | # Make the VM auto-start on server boot. |
| 362 | set_auto_start() |
| 363 | { |
| 364 | local v="$1" |
| 365 | xe vm-param-set uuid="$v" other-config:auto_poweron=true |
| 366 | } |
| 367 | |
| 368 | |
| 369 | set_all() |
| 370 | { |
| 371 | local v="$1" |
| 372 | set_kernel_params "$v" |
| 373 | set_memory "$v" |
| 374 | set_auto_start "$v" |
| 375 | label_system_disk "$v" |
| 376 | create_gi_vif "$v" |
| 377 | create_vm_vif "$v" |
| 378 | create_management_vif "$v" |
| 379 | create_public_vif "$v" |
| 380 | } |
| 381 | |
| 382 | |
| 383 | log_vifs() |
| 384 | { |
| 385 | local v="$1" |
| 386 | |
| 387 | (IFS=, |
| 388 | for vif in $(xe_min vif-list vm-uuid="$v") |
| 389 | do |
| 390 | dev=$(xe_min vif-list uuid="$vif" params=device) |
| 391 | mac=$(xe_min vif-list uuid="$vif" params=MAC | sed -e 's/:/-/g') |
| 392 | echo "eth$dev has MAC $mac." |
| 393 | done |
| 394 | unset IFS) | sort |
| 395 | } |
| 396 | |
| 397 | |
| 398 | destroy_vifs() |
| 399 | { |
| 400 | local v="$1" |
| 401 | IFS=, |
| 402 | for vif in $(xe_min vif-list vm-uuid="$v") |
| 403 | do |
| 404 | xe vif-destroy uuid="$vif" |
| 405 | done |
| 406 | unset IFS |
| 407 | } |
| 408 | |
| 409 | |
| 410 | get_params "$@" |
| 411 | |
| 412 | thisdir=$(dirname "$0") |
| 413 | |
| 414 | if [ "$FROM_TEMPLATE" ] |
| 415 | then |
| 416 | template_uuid=$(find_template) |
| 417 | name=$(xe_min template-list params=name-label uuid="$template_uuid") |
| 418 | echo -n "Cloning $name... " |
| 419 | vm_uuid=$(xe vm-clone vm="$template_uuid" new-name-label="$name") |
| 420 | xe vm-param-set is-a-template=false uuid="$vm_uuid" |
| 421 | echo $vm_uuid. |
| 422 | |
| 423 | destroy_vifs "$vm_uuid" |
| 424 | set_all "$vm_uuid" |
| 425 | else |
| 426 | if [ ! -f "$VPX_FILE" ] |
| 427 | then |
| 428 | # Search $thisdir/$VPX_FILE too. In particular, this is used when |
| 429 | # installing the VPX from the supp-pack, because we want to be able to |
| 430 | # invoke this script from the RPM and the firstboot script. |
| 431 | if [ -f "$thisdir/$VPX_FILE" ] |
| 432 | then |
| 433 | VPX_FILE="$thisdir/$VPX_FILE" |
| 434 | else |
| 435 | echo "$VPX_FILE does not exist." >&2 |
| 436 | exit 1 |
| 437 | fi |
| 438 | fi |
| 439 | |
| 440 | echo "Found OS-VPX File: $VPX_FILE. " |
| 441 | |
| 442 | dest_sr=$(get_dest_sr) |
| 443 | |
| 444 | echo -n "Installing $NAME... " |
| 445 | vm_uuid=$(xe vm-import filename=$VPX_FILE sr-uuid="$dest_sr") |
| 446 | echo $vm_uuid. |
| 447 | |
| 448 | renumber_system_disk "$vm_uuid" |
| 449 | |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame^] | 450 | nl=${NAME_LABEL:-$(xe_min vm-list params=name-label uuid=$vm_uuid)} |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 451 | xe vm-param-set \ |
| 452 | "name-label=${nl/ import/}" \ |
| 453 | other-config:os-vpx=true \ |
| 454 | uuid=$vm_uuid |
| 455 | |
| 456 | set_all "$vm_uuid" |
| 457 | create_data_disk "$vm_uuid" |
| 458 | |
| 459 | if [ "$AS_TEMPLATE" ] |
| 460 | then |
| 461 | xe vm-param-set uuid="$vm_uuid" is-a-template=true \ |
| 462 | other-config:instant=true |
| 463 | echo -n "Installing VPX from template... " |
| 464 | vm_uuid=$(xe vm-clone vm="$vm_uuid" new-name-label="${nl/ import/}") |
| 465 | xe vm-param-set is-a-template=false uuid="$vm_uuid" |
| 466 | echo "$vm_uuid." |
| 467 | fi |
| 468 | fi |
| 469 | |
| 470 | |
| 471 | log_vifs "$vm_uuid" |
| 472 | |
| 473 | echo -n "Starting VM... " |
| 474 | xe vm-start uuid=$vm_uuid |
| 475 | echo "done." |
| 476 | |
| 477 | |
| 478 | show_ip() |
| 479 | { |
| 480 | ip_addr=$(echo "$1" | sed -n "s,^.*"$2"/ip: \([^;]*\).*$,\1,p") |
| 481 | echo -n "IP address for $3: " |
| 482 | if [ "$ip_addr" = "" ] |
| 483 | then |
| 484 | echo "did not appear." |
| 485 | else |
| 486 | echo "$ip_addr." |
| 487 | fi |
| 488 | } |
| 489 | |
| 490 | |
| 491 | if [ "$WAIT_FOR_NETWORK" ] |
| 492 | then |
| 493 | echo "Waiting for network configuration... " |
| 494 | i=0 |
| 495 | while [ $i -lt 600 ] |
| 496 | do |
| 497 | ip=$(xe_min vm-list params=networks uuid=$vm_uuid) |
| 498 | if [ "$ip" != "<not in database>" ] |
| 499 | then |
| 500 | show_ip "$ip" "1" "$BRIDGE_M" |
| 501 | if [[ $BRIDGE_P ]] |
| 502 | then |
| 503 | show_ip "$ip" "2" "$BRIDGE_P" |
| 504 | fi |
| 505 | echo "Installation complete." |
| 506 | exit 0 |
| 507 | fi |
| 508 | sleep 10 |
| 509 | let i=i+1 |
| 510 | done |
| 511 | fi |