| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # Copyright (c) 2011 Citrix Systems, Inc. | 
| ZhiQiang Fan | 7d56215 | 2013-09-20 02:20:35 +0800 | [diff] [blame] | 4 | # Copyright 2011 OpenStack Foundation | 
| 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= | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 23 | NAME_LABEL= | 
|  | 24 | TEMPLATE_NAME= | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | usage() | 
|  | 27 | { | 
|  | 28 | cat << EOF | 
|  | 29 |  | 
| Mate Lakat | 16ed068 | 2013-08-30 13:28:31 +0100 | [diff] [blame] | 30 | Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE] | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 31 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 32 | Install a VM from a template | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | OPTIONS: | 
|  | 35 |  | 
|  | 36 | -h           Shows this message. | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 37 | -t template  VM template to use | 
| Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame] | 38 | -l name      Specifies the name label for the VM. | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 39 | -n bridge    The bridge/network to use for eth0. Defaults to xenbr0 | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 40 | EOF | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | get_params() | 
|  | 44 | { | 
| Chmouel Boudjnah | 86a8e97 | 2014-02-04 15:20:15 +0100 | [diff] [blame] | 45 | while getopts "hbn:r:l:t:" OPTION; do | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 46 | case $OPTION in | 
|  | 47 | h) usage | 
|  | 48 | exit 1 | 
|  | 49 | ;; | 
|  | 50 | n) | 
|  | 51 | BRIDGE=$OPTARG | 
|  | 52 | ;; | 
|  | 53 | l) | 
|  | 54 | NAME_LABEL=$OPTARG | 
|  | 55 | ;; | 
|  | 56 | t) | 
|  | 57 | TEMPLATE_NAME=$OPTARG | 
|  | 58 | ;; | 
|  | 59 | ?) | 
|  | 60 | usage | 
|  | 61 | exit | 
|  | 62 | ;; | 
|  | 63 | esac | 
|  | 64 | done | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 65 | if [[ -z $BRIDGE ]]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 66 | BRIDGE=xenbr0 | 
|  | 67 | fi | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 68 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 69 | if [[ -z $TEMPLATE_NAME ]]; then | 
|  | 70 | echo "Please specify a template name" >&2 | 
|  | 71 | exit 1 | 
|  | 72 | fi | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 73 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 74 | if [[ -z $NAME_LABEL ]]; then | 
|  | 75 | echo "Please specify a name-label for the new VM" >&2 | 
|  | 76 | exit 1 | 
|  | 77 | fi | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
|  | 80 |  | 
|  | 81 | xe_min() | 
|  | 82 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 83 | local cmd="$1" | 
|  | 84 | shift | 
|  | 85 | xe "$cmd" --minimal "$@" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 89 | find_network() | 
|  | 90 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 91 | result=$(xe_min network-list bridge="$1") | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 92 | if [ "$result" = "" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 93 | result=$(xe_min network-list name-label="$1") | 
|  | 94 | fi | 
|  | 95 | echo "$result" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
|  | 98 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 99 | create_vif() | 
|  | 100 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 101 | local v="$1" | 
|  | 102 | echo "Installing VM interface on [$BRIDGE]" | 
| Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 103 | local out_network_uuid | 
|  | 104 | out_network_uuid=$(find_network "$BRIDGE") | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 105 | 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] | 106 | } | 
|  | 107 |  | 
|  | 108 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | # Make the VM auto-start on server boot. | 
|  | 111 | set_auto_start() | 
|  | 112 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 113 | local v="$1" | 
|  | 114 | xe vm-param-set uuid="$v" other-config:auto_poweron=true | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 118 | destroy_vifs() | 
|  | 119 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 120 | local v="$1" | 
|  | 121 | IFS=, | 
| Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 122 | for vif in $(xe_min vif-list vm-uuid="$v"); do | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 123 | xe vif-destroy uuid="$vif" | 
|  | 124 | done | 
|  | 125 | unset IFS | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
|  | 128 |  | 
|  | 129 | get_params "$@" | 
|  | 130 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 131 | vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL") | 
|  | 132 | destroy_vifs "$vm_uuid" | 
|  | 133 | set_auto_start "$vm_uuid" | 
|  | 134 | create_vif "$vm_uuid" | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 135 | xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid" |