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= |
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 | { |
Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 45 | while getopts "hbn:r:l:t:" OPTION; |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 46 | do |
| 47 | case $OPTION in |
| 48 | h) usage |
| 49 | exit 1 |
| 50 | ;; |
Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 51 | n) |
| 52 | BRIDGE=$OPTARG |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 53 | ;; |
Renuka Apte | 836955f | 2012-04-02 15:22:55 -0700 | [diff] [blame] | 54 | l) |
Renuka Apte | e98cc12 | 2012-01-26 11:58:56 -0800 | [diff] [blame] | 55 | NAME_LABEL=$OPTARG |
| 56 | ;; |
Renuka Apte | 836955f | 2012-04-02 15:22:55 -0700 | [diff] [blame] | 57 | t) |
| 58 | TEMPLATE_NAME=$OPTARG |
| 59 | ;; |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 60 | ?) |
| 61 | usage |
| 62 | exit |
| 63 | ;; |
| 64 | esac |
| 65 | done |
Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 66 | if [[ -z $BRIDGE ]] |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 67 | then |
Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 68 | BRIDGE=xenbr0 |
| 69 | fi |
| 70 | |
| 71 | if [[ -z $TEMPLATE_NAME ]]; then |
| 72 | echo "Please specify a template name" >&2 |
| 73 | exit 1 |
| 74 | fi |
| 75 | |
| 76 | if [[ -z $NAME_LABEL ]]; then |
| 77 | echo "Please specify a name-label for the new VM" >&2 |
| 78 | exit 1 |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 79 | fi |
| 80 | } |
| 81 | |
| 82 | |
| 83 | xe_min() |
| 84 | { |
| 85 | local cmd="$1" |
| 86 | shift |
| 87 | xe "$cmd" --minimal "$@" |
| 88 | } |
| 89 | |
| 90 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 91 | find_network() |
| 92 | { |
| 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" |
| 99 | } |
| 100 | |
| 101 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 102 | create_vif() |
| 103 | { |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 104 | local v="$1" |
Mate Lakat | 2f524bd | 2013-06-19 12:32:23 +0100 | [diff] [blame] | 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 | { |
| 115 | local v="$1" |
| 116 | xe vm-param-set uuid="$v" other-config:auto_poweron=true |
| 117 | } |
| 118 | |
| 119 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 120 | destroy_vifs() |
| 121 | { |
| 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 |
| 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" |