blob: 66f7ef4763ae0bca8a7326bdd5c70f068822be50 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2#
3# Copyright (c) 2011 Citrix Systems, Inc.
ZhiQiang Fan7d562152013-09-20 02:20:35 +08004# Copyright 2011 OpenStack Foundation
Anthony Youngb62b4ca2011-10-26 22:29:08 -07005# 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
20set -eux
21
Mate Lakat2f524bd2013-06-19 12:32:23 +010022BRIDGE=
Mate Lakat2f524bd2013-06-19 12:32:23 +010023NAME_LABEL=
24TEMPLATE_NAME=
Anthony Youngb62b4ca2011-10-26 22:29:08 -070025
26usage()
27{
28cat << EOF
29
Mate Lakat16ed0682013-08-30 13:28:31 +010030 Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE]
Anthony Youngb62b4ca2011-10-26 22:29:08 -070031
Mate Lakat2f524bd2013-06-19 12:32:23 +010032 Install a VM from a template
Anthony Youngb62b4ca2011-10-26 22:29:08 -070033
34 OPTIONS:
35
36 -h Shows this message.
Mate Lakat2f524bd2013-06-19 12:32:23 +010037 -t template VM template to use
Renuka Aptee98cc122012-01-26 11:58:56 -080038 -l name Specifies the name label for the VM.
Mate Lakat2f524bd2013-06-19 12:32:23 +010039 -n bridge The bridge/network to use for eth0. Defaults to xenbr0
Anthony Youngb62b4ca2011-10-26 22:29:08 -070040EOF
41}
42
43get_params()
44{
Chmouel Boudjnah86a8e972014-02-04 15:20:15 +010045 while getopts "hbn:r:l:t:" OPTION; do
Sean Dague0b865a52013-10-22 11:37:35 -040046 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 Dague16dd8b32014-02-03 09:10:54 +090065 if [[ -z $BRIDGE ]]; then
Sean Dague0b865a52013-10-22 11:37:35 -040066 BRIDGE=xenbr0
67 fi
Mate Lakat2f524bd2013-06-19 12:32:23 +010068
Sean Dague0b865a52013-10-22 11:37:35 -040069 if [[ -z $TEMPLATE_NAME ]]; then
70 echo "Please specify a template name" >&2
71 exit 1
72 fi
Mate Lakat2f524bd2013-06-19 12:32:23 +010073
Sean Dague0b865a52013-10-22 11:37:35 -040074 if [[ -z $NAME_LABEL ]]; then
75 echo "Please specify a name-label for the new VM" >&2
76 exit 1
77 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070078}
79
80
81xe_min()
82{
Sean Dague0b865a52013-10-22 11:37:35 -040083 local cmd="$1"
84 shift
85 xe "$cmd" --minimal "$@"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070086}
87
88
Anthony Youngb62b4ca2011-10-26 22:29:08 -070089find_network()
90{
Sean Dague0b865a52013-10-22 11:37:35 -040091 result=$(xe_min network-list bridge="$1")
Sean Dague16dd8b32014-02-03 09:10:54 +090092 if [ "$result" = "" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -040093 result=$(xe_min network-list name-label="$1")
94 fi
95 echo "$result"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070096}
97
98
Anthony Youngb62b4ca2011-10-26 22:29:08 -070099create_vif()
100{
Sean Dague0b865a52013-10-22 11:37:35 -0400101 local v="$1"
102 echo "Installing VM interface on [$BRIDGE]"
Ian Wienandada886d2015-10-07 14:06:26 +1100103 local out_network_uuid
104 out_network_uuid=$(find_network "$BRIDGE")
Sean Dague0b865a52013-10-22 11:37:35 -0400105 xe vif-create vm-uuid="$v" network-uuid="$out_network_uuid" device="0"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700106}
107
108
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700109
110# Make the VM auto-start on server boot.
111set_auto_start()
112{
Sean Dague0b865a52013-10-22 11:37:35 -0400113 local v="$1"
114 xe vm-param-set uuid="$v" other-config:auto_poweron=true
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700115}
116
117
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700118destroy_vifs()
119{
Sean Dague0b865a52013-10-22 11:37:35 -0400120 local v="$1"
121 IFS=,
Sean Dague16dd8b32014-02-03 09:10:54 +0900122 for vif in $(xe_min vif-list vm-uuid="$v"); do
Sean Dague0b865a52013-10-22 11:37:35 -0400123 xe vif-destroy uuid="$vif"
124 done
125 unset IFS
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700126}
127
128
129get_params "$@"
130
Mate Lakat2f524bd2013-06-19 12:32:23 +0100131vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL")
132destroy_vifs "$vm_uuid"
133set_auto_start "$vm_uuid"
134create_vif "$vm_uuid"
Mate Lakat2f524bd2013-06-19 12:32:23 +0100135xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"