blob: 8ee8b675a9e5378135534ce3714168f433de1b4d [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=
Anthony Youngb62b4ca2011-10-26 22:29:08 -070023RAM=
Anthony Youngb62b4ca2011-10-26 22:29:08 -070024BALLOONING=
Mate Lakat2f524bd2013-06-19 12:32:23 +010025NAME_LABEL=
26TEMPLATE_NAME=
Anthony Youngb62b4ca2011-10-26 22:29:08 -070027
28usage()
29{
30cat << EOF
31
Mate Lakat2f524bd2013-06-19 12:32:23 +010032 Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE] [-r RAM] [-b]
Anthony Youngb62b4ca2011-10-26 22:29:08 -070033
Mate Lakat2f524bd2013-06-19 12:32:23 +010034 Install a VM from a template
Anthony Youngb62b4ca2011-10-26 22:29:08 -070035
36 OPTIONS:
37
38 -h Shows this message.
Mate Lakat2f524bd2013-06-19 12:32:23 +010039 -t template VM template to use
Anthony Youngb62b4ca2011-10-26 22:29:08 -070040 -b Enable memory ballooning. When set min_RAM=RAM/2 max_RAM=RAM.
Anthony Youngb62b4ca2011-10-26 22:29:08 -070041 -r MiB Specifies RAM used by the VPX, in MiB.
42 By default it will take the value from the XVA.
Renuka Aptee98cc122012-01-26 11:58:56 -080043 -l name Specifies the name label for the VM.
Mate Lakat2f524bd2013-06-19 12:32:23 +010044 -n bridge The bridge/network to use for eth0. Defaults to xenbr0
Anthony Youngb62b4ca2011-10-26 22:29:08 -070045EOF
46}
47
48get_params()
49{
Mate Lakat2f524bd2013-06-19 12:32:23 +010050 while getopts "hbn:r:l:t:" OPTION;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070051 do
52 case $OPTION in
53 h) usage
54 exit 1
55 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070056 b)
57 BALLOONING=1
58 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070059 r)
60 RAM=$OPTARG
61 ;;
Mate Lakat2f524bd2013-06-19 12:32:23 +010062 n)
63 BRIDGE=$OPTARG
Anthony Youngb62b4ca2011-10-26 22:29:08 -070064 ;;
Renuka Apte836955f2012-04-02 15:22:55 -070065 l)
Renuka Aptee98cc122012-01-26 11:58:56 -080066 NAME_LABEL=$OPTARG
67 ;;
Renuka Apte836955f2012-04-02 15:22:55 -070068 t)
69 TEMPLATE_NAME=$OPTARG
70 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070071 ?)
72 usage
73 exit
74 ;;
75 esac
76 done
Mate Lakat2f524bd2013-06-19 12:32:23 +010077 if [[ -z $BRIDGE ]]
Anthony Youngb62b4ca2011-10-26 22:29:08 -070078 then
Mate Lakat2f524bd2013-06-19 12:32:23 +010079 BRIDGE=xenbr0
80 fi
81
82 if [[ -z $TEMPLATE_NAME ]]; then
83 echo "Please specify a template name" >&2
84 exit 1
85 fi
86
87 if [[ -z $NAME_LABEL ]]; then
88 echo "Please specify a name-label for the new VM" >&2
89 exit 1
Anthony Youngb62b4ca2011-10-26 22:29:08 -070090 fi
91}
92
93
94xe_min()
95{
96 local cmd="$1"
97 shift
98 xe "$cmd" --minimal "$@"
99}
100
101
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700102find_network()
103{
104 result=$(xe_min network-list bridge="$1")
105 if [ "$result" = "" ]
106 then
107 result=$(xe_min network-list name-label="$1")
108 fi
109 echo "$result"
110}
111
112
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700113create_vif()
114{
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700115 local v="$1"
Mate Lakat2f524bd2013-06-19 12:32:23 +0100116 echo "Installing VM interface on [$BRIDGE]"
117 local out_network_uuid=$(find_network "$BRIDGE")
118 xe vif-create vm-uuid="$v" network-uuid="$out_network_uuid" device="0"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700119}
120
121
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700122set_memory()
123{
124 local v="$1"
125 if [ "$RAM" != "" ]
126 then
127 echo "Setting RAM to $RAM MiB."
128 [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM
129 xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \
130 dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \
131 uuid="$v"
132 fi
133}
134
135
136# Make the VM auto-start on server boot.
137set_auto_start()
138{
139 local v="$1"
140 xe vm-param-set uuid="$v" other-config:auto_poweron=true
141}
142
143
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700144destroy_vifs()
145{
146 local v="$1"
147 IFS=,
148 for vif in $(xe_min vif-list vm-uuid="$v")
149 do
150 xe vif-destroy uuid="$vif"
151 done
152 unset IFS
153}
154
155
156get_params "$@"
157
Mate Lakat2f524bd2013-06-19 12:32:23 +0100158vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL")
159destroy_vifs "$vm_uuid"
160set_auto_start "$vm_uuid"
161create_vif "$vm_uuid"
162xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid"
163xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"
164set_memory "$vm_uuid"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700165xe vm-start uuid=$vm_uuid