blob: c94a593e3d69b71a05df8f8578d909359ebf7966 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2#
3# Copyright (c) 2011 Citrix Systems, Inc.
4# Copyright 2011 OpenStack LLC.
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{
Mate Lakat2f524bd2013-06-19 12:32:23 +010045 while getopts "hbn:r:l:t:" OPTION;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070046 do
47 case $OPTION in
48 h) usage
49 exit 1
50 ;;
Mate Lakat2f524bd2013-06-19 12:32:23 +010051 n)
52 BRIDGE=$OPTARG
Anthony Youngb62b4ca2011-10-26 22:29:08 -070053 ;;
Renuka Apte836955f2012-04-02 15:22:55 -070054 l)
Renuka Aptee98cc122012-01-26 11:58:56 -080055 NAME_LABEL=$OPTARG
56 ;;
Renuka Apte836955f2012-04-02 15:22:55 -070057 t)
58 TEMPLATE_NAME=$OPTARG
59 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070060 ?)
61 usage
62 exit
63 ;;
64 esac
65 done
Mate Lakat2f524bd2013-06-19 12:32:23 +010066 if [[ -z $BRIDGE ]]
Anthony Youngb62b4ca2011-10-26 22:29:08 -070067 then
Mate Lakat2f524bd2013-06-19 12:32:23 +010068 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 Youngb62b4ca2011-10-26 22:29:08 -070079 fi
80}
81
82
83xe_min()
84{
85 local cmd="$1"
86 shift
87 xe "$cmd" --minimal "$@"
88}
89
90
Anthony Youngb62b4ca2011-10-26 22:29:08 -070091find_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 Youngb62b4ca2011-10-26 22:29:08 -0700102create_vif()
103{
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700104 local v="$1"
Mate Lakat2f524bd2013-06-19 12:32:23 +0100105 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 Youngb62b4ca2011-10-26 22:29:08 -0700108}
109
110
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700111
112# Make the VM auto-start on server boot.
113set_auto_start()
114{
115 local v="$1"
116 xe vm-param-set uuid="$v" other-config:auto_poweron=true
117}
118
119
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700120destroy_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
132get_params "$@"
133
Mate Lakat2f524bd2013-06-19 12:32:23 +0100134vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL")
135destroy_vifs "$vm_uuid"
136set_auto_start "$vm_uuid"
137create_vif "$vm_uuid"
138xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid"
139xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"