blob: 8412fdc3cacd43380abb034c31fd890913c39e37 [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{
Sean Dague0b865a52013-10-22 11:37:35 -040045 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
Sean Dague16dd8b32014-02-03 09:10:54 +090066 if [[ -z $BRIDGE ]]; then
Sean Dague0b865a52013-10-22 11:37:35 -040067 BRIDGE=xenbr0
68 fi
Mate Lakat2f524bd2013-06-19 12:32:23 +010069
Sean Dague0b865a52013-10-22 11:37:35 -040070 if [[ -z $TEMPLATE_NAME ]]; then
71 echo "Please specify a template name" >&2
72 exit 1
73 fi
Mate Lakat2f524bd2013-06-19 12:32:23 +010074
Sean Dague0b865a52013-10-22 11:37:35 -040075 if [[ -z $NAME_LABEL ]]; then
76 echo "Please specify a name-label for the new VM" >&2
77 exit 1
78 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070079}
80
81
82xe_min()
83{
Sean Dague0b865a52013-10-22 11:37:35 -040084 local cmd="$1"
85 shift
86 xe "$cmd" --minimal "$@"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070087}
88
89
Anthony Youngb62b4ca2011-10-26 22:29:08 -070090find_network()
91{
Sean Dague0b865a52013-10-22 11:37:35 -040092 result=$(xe_min network-list bridge="$1")
Sean Dague16dd8b32014-02-03 09:10:54 +090093 if [ "$result" = "" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -040094 result=$(xe_min network-list name-label="$1")
95 fi
96 echo "$result"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070097}
98
99
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700100create_vif()
101{
Sean Dague0b865a52013-10-22 11:37:35 -0400102 local v="$1"
103 echo "Installing VM interface on [$BRIDGE]"
104 local out_network_uuid=$(find_network "$BRIDGE")
105 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"
135xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid"
136xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"