blob: c82f8702badb47fdbfda808de2ea47889d790b3d [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
Armando Migliaccio1f8efd92012-06-02 01:40:00 +010022[[ -f "/etc/xensource-inventory" ]] && source "/etc/xensource-inventory" || source "/etc/xcp/inventory"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070023
24NAME="XenServer OpenStack VPX"
25DATA_VDI_SIZE="500MiB"
26BRIDGE_M=
27BRIDGE_P=
Anthony Youngb62b4ca2011-10-26 22:29:08 -070028VPX_FILE=os-vpx.xva
29AS_TEMPLATE=
30FROM_TEMPLATE=
31RAM=
32WAIT_FOR_NETWORK=
33BALLOONING=
34
35usage()
36{
37cat << EOF
38
39 Usage: $0 [-f FILE_PATH] [-d DISK_SIZE] [-v BRIDGE_NAME] [-m BRIDGE_NAME] [-p BRIDGE_NAME]
Mate Lakat8ff33ce2013-05-30 13:26:58 +010040 [-r RAM] [-i|-c] [-w] [-b] [-l NAME_LABEL] [-t TEMPLATE_NW_INSTALL]
Anthony Youngb62b4ca2011-10-26 22:29:08 -070041
42 Installs XenServer OpenStack VPX.
43
44 OPTIONS:
45
46 -h Shows this message.
47 -i Install OpenStack VPX as template.
48 -c Clone from existing template.
49 -w Wait for the network settings to show up before exiting.
50 -b Enable memory ballooning. When set min_RAM=RAM/2 max_RAM=RAM.
51 -f path Specifies the path to the XVA.
52 Default to ./os-vpx.xva.
53 -d disk-size Specifies the size in MiB for the data disk.
54 Defaults to 500 MiB.
55 -m bridge Specifies the bridge for the isolated management network.
56 Defaults to xenbr0.
57 -v bridge Specifies the bridge for the vm network
58 -p bridge Specifies the bridge for the externally facing network.
Anthony Youngb62b4ca2011-10-26 22:29:08 -070059 -r MiB Specifies RAM used by the VPX, in MiB.
60 By default it will take the value from the XVA.
Renuka Aptee98cc122012-01-26 11:58:56 -080061 -l name Specifies the name label for the VM.
Renuka Apte836955f2012-04-02 15:22:55 -070062 -t template Network install an openstack domU from this template
Anthony Youngb62b4ca2011-10-26 22:29:08 -070063
64 EXAMPLES:
65
66 Create a VPX that connects to the isolated management network using the
67 default bridge with a data disk of 1GiB:
68 install-os-vpx.sh -f /root/os-vpx-devel.xva -d 1024
69
70 Create a VPX that connects to the isolated management network using xenbr1
71 as bridge:
72 install-os-vpx.sh -m xenbr1
73
74 Create a VPX that connects to both the management and public networks
75 using xenbr1 and xapi4 as bridges:
76 install-os-vpx.sh -m xenbr1 -p xapi4
77
78 Create a VPX that connects to both the management and public networks
79 using the default for management traffic:
80 install-os-vpx.sh -m xapi4
81
Anthony Youngb62b4ca2011-10-26 22:29:08 -070082EOF
83}
84
85get_params()
86{
Mate Lakat8ff33ce2013-05-30 13:26:58 +010087 while getopts "hicwbf:d:v:m:p:r:l:t:" OPTION;
Anthony Youngb62b4ca2011-10-26 22:29:08 -070088 do
89 case $OPTION in
90 h) usage
91 exit 1
92 ;;
93 i)
94 AS_TEMPLATE=1
95 ;;
96 c)
97 FROM_TEMPLATE=1
98 ;;
99 w)
100 WAIT_FOR_NETWORK=1
101 ;;
102 b)
103 BALLOONING=1
104 ;;
105 f)
106 VPX_FILE=$OPTARG
107 ;;
108 d)
109 DATA_VDI_SIZE="${OPTARG}MiB"
110 ;;
111 m)
112 BRIDGE_M=$OPTARG
113 ;;
114 p)
115 BRIDGE_P=$OPTARG
116 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700117 r)
118 RAM=$OPTARG
119 ;;
120 v)
121 BRIDGE_V=$OPTARG
122 ;;
Renuka Apte836955f2012-04-02 15:22:55 -0700123 l)
Renuka Aptee98cc122012-01-26 11:58:56 -0800124 NAME_LABEL=$OPTARG
125 ;;
Renuka Apte836955f2012-04-02 15:22:55 -0700126 t)
127 TEMPLATE_NAME=$OPTARG
128 ;;
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700129 ?)
130 usage
131 exit
132 ;;
133 esac
134 done
135 if [[ -z $BRIDGE_M ]]
136 then
137 BRIDGE_M=xenbr0
138 fi
139}
140
141
142xe_min()
143{
144 local cmd="$1"
145 shift
146 xe "$cmd" --minimal "$@"
147}
148
149
150get_dest_sr()
151{
152 IFS=,
153 sr_uuids=$(xe sr-list --minimal other-config:i18n-key=local-storage)
154 dest_sr=""
155 for sr_uuid in $sr_uuids
156 do
157 pbd=$(xe pbd-list --minimal sr-uuid=$sr_uuid host-uuid=$INSTALLATION_UUID)
158 if [ "$pbd" ]
159 then
160 echo "$sr_uuid"
161 unset IFS
162 return
163 fi
164 done
165 unset IFS
166
167 dest_sr=$(xe_min sr-list uuid=$(xe_min pool-list params=default-SR))
168 if [ "$dest_sr" = "" ]
169 then
170 echo "No local storage and no default storage; cannot import VPX." >&2
171 exit 1
172 else
173 echo "$dest_sr"
174 fi
175}
176
177
178find_network()
179{
180 result=$(xe_min network-list bridge="$1")
181 if [ "$result" = "" ]
182 then
183 result=$(xe_min network-list name-label="$1")
184 fi
185 echo "$result"
186}
187
188
189find_template()
190{
191 xe_min template-list other-config:os-vpx=true
192}
193
194
195renumber_system_disk()
196{
197 local v="$1"
198 local vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk userdevice=xvda \
199 params=vdi-uuid)
200 if [ "$vdi_uuid" ]
201 then
202 local vbd_uuid=$(xe_min vbd-list vm-uuid="$v" vdi-uuid="$vdi_uuid")
203 xe vbd-destroy uuid="$vbd_uuid"
204 local new_vbd_uuid=$(xe vbd-create vm-uuid="$v" vdi-uuid="$vdi_uuid" \
205 device=0 bootable=true type=Disk)
206 xe vbd-param-set other-config:owner uuid="$new_vbd_uuid"
207 fi
208}
209
210
211create_vif()
212{
213 xe vif-create vm-uuid="$1" network-uuid="$2" device="$3"
214}
215
216create_gi_vif()
217{
218 local v="$1"
219 # Note that we've made the outbound device eth1, so that it comes up after
220 # the guest installer VIF, which means that the outbound one wins in terms
221 # of gateway.
222 local gi_network_uuid=$(xe_min network-list \
223 other-config:is_guest_installer_network=true)
224 create_vif "$v" "$gi_network_uuid" "0" >/dev/null
225}
226
227create_vm_vif()
228{
229 local v="$1"
Mate Lakat9e326772013-05-08 16:42:22 +0100230 echo "Installing VM interface on $BRIDGE_V."
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700231 local out_network_uuid=$(find_network "$BRIDGE_V")
232 create_vif "$v" "$out_network_uuid" "1" >/dev/null
233}
234
235create_management_vif()
236{
237 local v="$1"
238 echo "Installing management interface on $BRIDGE_M."
239 local out_network_uuid=$(find_network "$BRIDGE_M")
240 create_vif "$v" "$out_network_uuid" "2" >/dev/null
241}
242
243
244# This installs the interface for public traffic, only if a bridge is specified
Hengqing Hu3b719e52012-03-09 16:03:00 +0800245# The interface is not configured at this stage, but it will be, once the admin
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700246# tasks are complete for the services of this VPX
247create_public_vif()
248{
249 local v="$1"
250 if [[ -z $BRIDGE_P ]]
251 then
252 echo "Skipping installation of interface for public traffic."
253 else
254 echo "Installing public interface on $BRIDGE_P."
255 pub_network_uuid=$(find_network "$BRIDGE_P")
256 create_vif "$v" "$pub_network_uuid" "3" >/dev/null
257 fi
258}
259
260
261label_system_disk()
262{
263 local v="$1"
264 local vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk userdevice=0 \
265 params=vdi-uuid)
266 xe vdi-param-set \
267 name-label="$NAME system disk" \
268 other-config:os-vpx=true \
269 uuid=$vdi_uuid
270}
271
272
273create_data_disk()
274{
275 local v="$1"
276
277 local sys_vdi_uuid=$(xe_min vbd-list vm-uuid="$v" type=Disk params=vdi-uuid)
278 local data_vdi_uuid=$(xe_min vdi-list other-config:os-vpx-data=true)
279
280 if echo "$data_vdi_uuid" | grep -q ,
281 then
282 echo "Multiple data disks found -- assuming that you want a new one."
283 data_vdi_uuid=""
284 else
285 data_in_use=$(xe_min vbd-list vdi-uuid="$data_vdi_uuid")
286 if [ "$data_in_use" != "" ]
287 then
288 echo "Data disk already in use -- will create another one."
289 data_vdi_uuid=""
290 fi
291 fi
292
293 if [ "$data_vdi_uuid" = "" ]
294 then
295 echo -n "Creating new data disk ($DATA_VDI_SIZE)... "
296 sr_uuid=$(xe_min vdi-list params=sr-uuid uuid="$sys_vdi_uuid")
297 data_vdi_uuid=$(xe vdi-create name-label="$NAME data disk" \
298 sr-uuid="$sr_uuid" \
299 type=user \
300 virtual-size="$DATA_VDI_SIZE")
301 xe vdi-param-set \
302 other-config:os-vpx-data=true \
303 uuid="$data_vdi_uuid"
304 dom0_uuid=$(xe_min vm-list is-control-domain=true)
305 vbd_uuid=$(xe vbd-create device=autodetect type=Disk \
306 vdi-uuid="$data_vdi_uuid" vm-uuid="$dom0_uuid")
307 xe vbd-plug uuid=$vbd_uuid
308 dev=$(xe_min vbd-list params=device uuid=$vbd_uuid)
309 mke2fs -q -j -m0 /dev/$dev
310 e2label /dev/$dev vpxstate
311 xe vbd-unplug uuid=$vbd_uuid
312 xe vbd-destroy uuid=$vbd_uuid
313 else
314 echo -n "Attaching old data disk... "
315 fi
316 vbd_uuid=$(xe vbd-create device=2 type=Disk \
317 vdi-uuid="$data_vdi_uuid" vm-uuid="$v")
318 xe vbd-param-set other-config:os-vpx-data=true uuid=$vbd_uuid
319 echo "done."
320}
321
322
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700323set_memory()
324{
325 local v="$1"
326 if [ "$RAM" != "" ]
327 then
328 echo "Setting RAM to $RAM MiB."
329 [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM
330 xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \
331 dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \
332 uuid="$v"
333 fi
334}
335
336
337# Make the VM auto-start on server boot.
338set_auto_start()
339{
340 local v="$1"
341 xe vm-param-set uuid="$v" other-config:auto_poweron=true
342}
343
344
345set_all()
346{
347 local v="$1"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700348 set_memory "$v"
349 set_auto_start "$v"
350 label_system_disk "$v"
351 create_gi_vif "$v"
352 create_vm_vif "$v"
353 create_management_vif "$v"
354 create_public_vif "$v"
355}
356
357
358log_vifs()
359{
360 local v="$1"
361
362 (IFS=,
363 for vif in $(xe_min vif-list vm-uuid="$v")
364 do
365 dev=$(xe_min vif-list uuid="$vif" params=device)
366 mac=$(xe_min vif-list uuid="$vif" params=MAC | sed -e 's/:/-/g')
367 echo "eth$dev has MAC $mac."
368 done
369 unset IFS) | sort
370}
371
372
373destroy_vifs()
374{
375 local v="$1"
376 IFS=,
377 for vif in $(xe_min vif-list vm-uuid="$v")
378 do
379 xe vif-destroy uuid="$vif"
380 done
381 unset IFS
382}
383
384
385get_params "$@"
386
387thisdir=$(dirname "$0")
388
389if [ "$FROM_TEMPLATE" ]
390then
391 template_uuid=$(find_template)
392 name=$(xe_min template-list params=name-label uuid="$template_uuid")
393 echo -n "Cloning $name... "
394 vm_uuid=$(xe vm-clone vm="$template_uuid" new-name-label="$name")
395 xe vm-param-set is-a-template=false uuid="$vm_uuid"
396 echo $vm_uuid.
397
398 destroy_vifs "$vm_uuid"
399 set_all "$vm_uuid"
Renuka Apte836955f2012-04-02 15:22:55 -0700400elif [ "$TEMPLATE_NAME" ]
401then
402 echo $TEMPLATE_NAME
Renuka Apte0af143b2012-04-02 15:46:53 -0700403 vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL")
Renuka Apte836955f2012-04-02 15:22:55 -0700404 destroy_vifs "$vm_uuid"
405 set_auto_start "$vm_uuid"
406 create_gi_vif "$vm_uuid"
407 create_vm_vif "$vm_uuid"
408 create_management_vif "$vm_uuid"
409 create_public_vif "$vm_uuid"
Renuka Apte0af143b2012-04-02 15:46:53 -0700410 xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid"
411 xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"
412 set_memory "$vm_uuid"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700413else
414 if [ ! -f "$VPX_FILE" ]
415 then
416 # Search $thisdir/$VPX_FILE too. In particular, this is used when
417 # installing the VPX from the supp-pack, because we want to be able to
418 # invoke this script from the RPM and the firstboot script.
419 if [ -f "$thisdir/$VPX_FILE" ]
420 then
421 VPX_FILE="$thisdir/$VPX_FILE"
422 else
423 echo "$VPX_FILE does not exist." >&2
424 exit 1
425 fi
426 fi
427
428 echo "Found OS-VPX File: $VPX_FILE. "
429
430 dest_sr=$(get_dest_sr)
431
432 echo -n "Installing $NAME... "
433 vm_uuid=$(xe vm-import filename=$VPX_FILE sr-uuid="$dest_sr")
434 echo $vm_uuid.
435
436 renumber_system_disk "$vm_uuid"
437
Renuka Aptee98cc122012-01-26 11:58:56 -0800438 nl=${NAME_LABEL:-$(xe_min vm-list params=name-label uuid=$vm_uuid)}
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700439 xe vm-param-set \
440 "name-label=${nl/ import/}" \
441 other-config:os-vpx=true \
442 uuid=$vm_uuid
443
444 set_all "$vm_uuid"
445 create_data_disk "$vm_uuid"
446
447 if [ "$AS_TEMPLATE" ]
448 then
449 xe vm-param-set uuid="$vm_uuid" is-a-template=true \
450 other-config:instant=true
451 echo -n "Installing VPX from template... "
452 vm_uuid=$(xe vm-clone vm="$vm_uuid" new-name-label="${nl/ import/}")
453 xe vm-param-set is-a-template=false uuid="$vm_uuid"
454 echo "$vm_uuid."
455 fi
456fi
457
458
459log_vifs "$vm_uuid"
460
461echo -n "Starting VM... "
462xe vm-start uuid=$vm_uuid
463echo "done."
464
465
466show_ip()
467{
468 ip_addr=$(echo "$1" | sed -n "s,^.*"$2"/ip: \([^;]*\).*$,\1,p")
469 echo -n "IP address for $3: "
470 if [ "$ip_addr" = "" ]
471 then
472 echo "did not appear."
473 else
474 echo "$ip_addr."
475 fi
476}
477
478
479if [ "$WAIT_FOR_NETWORK" ]
480then
481 echo "Waiting for network configuration... "
482 i=0
483 while [ $i -lt 600 ]
484 do
485 ip=$(xe_min vm-list params=networks uuid=$vm_uuid)
486 if [ "$ip" != "<not in database>" ]
487 then
488 show_ip "$ip" "1" "$BRIDGE_M"
489 if [[ $BRIDGE_P ]]
490 then
491 show_ip "$ip" "2" "$BRIDGE_P"
492 fi
493 echo "Installation complete."
494 exit 0
495 fi
496 sleep 10
497 let i=i+1
498 done
499fi