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