| 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 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 22 | BRIDGE= | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 23 | RAM= | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 24 | BALLOONING= | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 25 | NAME_LABEL= | 
|  | 26 | TEMPLATE_NAME= | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | usage() | 
|  | 29 | { | 
|  | 30 | cat << EOF | 
|  | 31 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 32 | Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE] [-r RAM] [-b] | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 33 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 34 | Install a VM from a template | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | OPTIONS: | 
|  | 37 |  | 
|  | 38 | -h           Shows this message. | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 39 | -t template  VM template to use | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 40 | -b           Enable memory ballooning. When set min_RAM=RAM/2 max_RAM=RAM. | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 41 | -r MiB       Specifies RAM used by the VPX, in MiB. | 
|  | 42 | By default it will take the value from the XVA. | 
| Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame] | 43 | -l name      Specifies the name label for the VM. | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 44 | -n bridge    The bridge/network to use for eth0. Defaults to xenbr0 | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 45 | EOF | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | get_params() | 
|  | 49 | { | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 50 | while getopts "hbn:r:l:t:" OPTION; | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 51 | do | 
|  | 52 | case $OPTION in | 
|  | 53 | h) usage | 
|  | 54 | exit 1 | 
|  | 55 | ;; | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 56 | b) | 
|  | 57 | BALLOONING=1 | 
|  | 58 | ;; | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 59 | r) | 
|  | 60 | RAM=$OPTARG | 
|  | 61 | ;; | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 62 | n) | 
|  | 63 | BRIDGE=$OPTARG | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 64 | ;; | 
| Renuka Apte | 836955f | 2012-04-02 15:22:55 -0700 | [diff] [blame] | 65 | l) | 
| Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame] | 66 | NAME_LABEL=$OPTARG | 
|  | 67 | ;; | 
| Renuka Apte | 836955f | 2012-04-02 15:22:55 -0700 | [diff] [blame] | 68 | t) | 
|  | 69 | TEMPLATE_NAME=$OPTARG | 
|  | 70 | ;; | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 71 | ?) | 
|  | 72 | usage | 
|  | 73 | exit | 
|  | 74 | ;; | 
|  | 75 | esac | 
|  | 76 | done | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 77 | if [[ -z $BRIDGE ]] | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 78 | then | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 79 | BRIDGE=xenbr0 | 
|  | 80 | fi | 
|  | 81 |  | 
|  | 82 | if [[ -z $TEMPLATE_NAME ]]; then | 
|  | 83 | echo "Please specify a template name" >&2 | 
|  | 84 | exit 1 | 
|  | 85 | fi | 
|  | 86 |  | 
|  | 87 | if [[ -z $NAME_LABEL ]]; then | 
|  | 88 | echo "Please specify a name-label for the new VM" >&2 | 
|  | 89 | exit 1 | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 90 | fi | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 |  | 
|  | 94 | xe_min() | 
|  | 95 | { | 
|  | 96 | local cmd="$1" | 
|  | 97 | shift | 
|  | 98 | xe "$cmd" --minimal "$@" | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 102 | find_network() | 
|  | 103 | { | 
|  | 104 | result=$(xe_min network-list bridge="$1") | 
|  | 105 | if [ "$result" = "" ] | 
|  | 106 | then | 
|  | 107 | result=$(xe_min network-list name-label="$1") | 
|  | 108 | fi | 
|  | 109 | echo "$result" | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 113 | create_vif() | 
|  | 114 | { | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 115 | local v="$1" | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 116 | echo "Installing VM interface on [$BRIDGE]" | 
|  | 117 | local out_network_uuid=$(find_network "$BRIDGE") | 
|  | 118 | xe vif-create vm-uuid="$v" network-uuid="$out_network_uuid" device="0" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 121 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 122 | set_memory() | 
|  | 123 | { | 
|  | 124 | local v="$1" | 
|  | 125 | if [ "$RAM" != "" ] | 
|  | 126 | then | 
|  | 127 | echo "Setting RAM to $RAM MiB." | 
|  | 128 | [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM | 
|  | 129 | xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \ | 
|  | 130 | dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \ | 
|  | 131 | uuid="$v" | 
|  | 132 | fi | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 |  | 
|  | 136 | # Make the VM auto-start on server boot. | 
|  | 137 | set_auto_start() | 
|  | 138 | { | 
|  | 139 | local v="$1" | 
|  | 140 | xe vm-param-set uuid="$v" other-config:auto_poweron=true | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 144 | destroy_vifs() | 
|  | 145 | { | 
|  | 146 | local v="$1" | 
|  | 147 | IFS=, | 
|  | 148 | for vif in $(xe_min vif-list vm-uuid="$v") | 
|  | 149 | do | 
|  | 150 | xe vif-destroy uuid="$vif" | 
|  | 151 | done | 
|  | 152 | unset IFS | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 |  | 
|  | 156 | get_params "$@" | 
|  | 157 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 158 | vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL") | 
|  | 159 | destroy_vifs "$vm_uuid" | 
|  | 160 | set_auto_start "$vm_uuid" | 
|  | 161 | create_vif "$vm_uuid" | 
|  | 162 | xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid" | 
|  | 163 | xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid" | 
|  | 164 | set_memory "$vm_uuid" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 165 | xe vm-start uuid=$vm_uuid |