| 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 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 45 |     while getopts "hbn:r:l:t:" OPTION; | 
 | 46 |     do | 
 | 47 |         case $OPTION in | 
 | 48 |             h) usage | 
 | 49 |                 exit 1 | 
 | 50 |                 ;; | 
 | 51 |             n) | 
 | 52 |                 BRIDGE=$OPTARG | 
 | 53 |                 ;; | 
 | 54 |             l) | 
 | 55 |                 NAME_LABEL=$OPTARG | 
 | 56 |                 ;; | 
 | 57 |             t) | 
 | 58 |                 TEMPLATE_NAME=$OPTARG | 
 | 59 |                 ;; | 
 | 60 |             ?) | 
 | 61 |                 usage | 
 | 62 |                 exit | 
 | 63 |                 ;; | 
 | 64 |         esac | 
 | 65 |     done | 
 | 66 |     if [[ -z $BRIDGE ]] | 
 | 67 |     then | 
 | 68 |         BRIDGE=xenbr0 | 
 | 69 |     fi | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 70 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 71 |     if [[ -z $TEMPLATE_NAME ]]; then | 
 | 72 |         echo "Please specify a template name" >&2 | 
 | 73 |         exit 1 | 
 | 74 |     fi | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 75 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 76 |     if [[ -z $NAME_LABEL ]]; then | 
 | 77 |         echo "Please specify a name-label for the new VM" >&2 | 
 | 78 |         exit 1 | 
 | 79 |     fi | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 80 | } | 
 | 81 |  | 
 | 82 |  | 
 | 83 | xe_min() | 
 | 84 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 85 |     local cmd="$1" | 
 | 86 |     shift | 
 | 87 |     xe "$cmd" --minimal "$@" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 88 | } | 
 | 89 |  | 
 | 90 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 91 | find_network() | 
 | 92 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 93 |     result=$(xe_min network-list bridge="$1") | 
 | 94 |     if [ "$result" = "" ] | 
 | 95 |     then | 
 | 96 |         result=$(xe_min network-list name-label="$1") | 
 | 97 |     fi | 
 | 98 |     echo "$result" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 99 | } | 
 | 100 |  | 
 | 101 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 102 | create_vif() | 
 | 103 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 104 |     local v="$1" | 
 | 105 |     echo "Installing VM interface on [$BRIDGE]" | 
 | 106 |     local out_network_uuid=$(find_network "$BRIDGE") | 
 | 107 |     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] | 108 | } | 
 | 109 |  | 
 | 110 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 111 |  | 
 | 112 | # Make the VM auto-start on server boot. | 
 | 113 | set_auto_start() | 
 | 114 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 115 |     local v="$1" | 
 | 116 |     xe vm-param-set uuid="$v" other-config:auto_poweron=true | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 117 | } | 
 | 118 |  | 
 | 119 |  | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 120 | destroy_vifs() | 
 | 121 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 122 |     local v="$1" | 
 | 123 |     IFS=, | 
 | 124 |     for vif in $(xe_min vif-list vm-uuid="$v") | 
 | 125 |     do | 
 | 126 |         xe vif-destroy uuid="$vif" | 
 | 127 |     done | 
 | 128 |     unset IFS | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 129 | } | 
 | 130 |  | 
 | 131 |  | 
 | 132 | get_params "$@" | 
 | 133 |  | 
| Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 134 | vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL") | 
 | 135 | destroy_vifs "$vm_uuid" | 
 | 136 | set_auto_start "$vm_uuid" | 
 | 137 | create_vif "$vm_uuid" | 
 | 138 | xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid" | 
 | 139 | xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid" |