blob: 14240a750cf503e301e2d95eb8a544ea376c88d1 [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
335 local cmdline=$(cat /proc/cmdline)
336 for word in $cmdline
337 do
338 if echo "$word" | grep -q "geppetto"
339 then
340 args="$word $args"
341 fi
342 done
343 if [ "$args" != "" ]
344 then
345 echo "Passing Geppetto args to VPX: $args."
346 xe vm-param-set PV-args="$args" uuid="$v"
347 fi
348}
349
350
351set_memory()
352{
353 local v="$1"
354 if [ "$RAM" != "" ]
355 then
356 echo "Setting RAM to $RAM MiB."
357 [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM
358 xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \
359 dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \
360 uuid="$v"
361 fi
362}
363
364
365# Make the VM auto-start on server boot.
366set_auto_start()
367{
368 local v="$1"
369 xe vm-param-set uuid="$v" other-config:auto_poweron=true
370}
371
372
373set_all()
374{
375 local v="$1"
376 set_kernel_params "$v"
377 set_memory "$v"
378 set_auto_start "$v"
379 label_system_disk "$v"
380 create_gi_vif "$v"
381 create_vm_vif "$v"
382 create_management_vif "$v"
383 create_public_vif "$v"
384}
385
386
387log_vifs()
388{
389 local v="$1"
390
391 (IFS=,
392 for vif in $(xe_min vif-list vm-uuid="$v")
393 do
394 dev=$(xe_min vif-list uuid="$vif" params=device)
395 mac=$(xe_min vif-list uuid="$vif" params=MAC | sed -e 's/:/-/g')
396 echo "eth$dev has MAC $mac."
397 done
398 unset IFS) | sort
399}
400
401
402destroy_vifs()
403{
404 local v="$1"
405 IFS=,
406 for vif in $(xe_min vif-list vm-uuid="$v")
407 do
408 xe vif-destroy uuid="$vif"
409 done
410 unset IFS
411}
412
413
414get_params "$@"
415
416thisdir=$(dirname "$0")
417
418if [ "$FROM_TEMPLATE" ]
419then
420 template_uuid=$(find_template)
421 name=$(xe_min template-list params=name-label uuid="$template_uuid")
422 echo -n "Cloning $name... "
423 vm_uuid=$(xe vm-clone vm="$template_uuid" new-name-label="$name")
424 xe vm-param-set is-a-template=false uuid="$vm_uuid"
425 echo $vm_uuid.
426
427 destroy_vifs "$vm_uuid"
428 set_all "$vm_uuid"
Renuka Apte836955f2012-04-02 15:22:55 -0700429elif [ "$TEMPLATE_NAME" ]
430then
431 echo $TEMPLATE_NAME
432 vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="DevstackOSDomu")
433 destroy_vifs "$vm_uuid"
434 set_auto_start "$vm_uuid"
435 create_gi_vif "$vm_uuid"
436 create_vm_vif "$vm_uuid"
437 create_management_vif "$vm_uuid"
438 create_public_vif "$vm_uuid"
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700439else
440 if [ ! -f "$VPX_FILE" ]
441 then
442 # Search $thisdir/$VPX_FILE too. In particular, this is used when
443 # installing the VPX from the supp-pack, because we want to be able to
444 # invoke this script from the RPM and the firstboot script.
445 if [ -f "$thisdir/$VPX_FILE" ]
446 then
447 VPX_FILE="$thisdir/$VPX_FILE"
448 else
449 echo "$VPX_FILE does not exist." >&2
450 exit 1
451 fi
452 fi
453
454 echo "Found OS-VPX File: $VPX_FILE. "
455
456 dest_sr=$(get_dest_sr)
457
458 echo -n "Installing $NAME... "
459 vm_uuid=$(xe vm-import filename=$VPX_FILE sr-uuid="$dest_sr")
460 echo $vm_uuid.
461
462 renumber_system_disk "$vm_uuid"
463
Renuka Aptee98cc122012-01-26 11:58:56 -0800464 nl=${NAME_LABEL:-$(xe_min vm-list params=name-label uuid=$vm_uuid)}
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700465 xe vm-param-set \
466 "name-label=${nl/ import/}" \
467 other-config:os-vpx=true \
468 uuid=$vm_uuid
469
470 set_all "$vm_uuid"
471 create_data_disk "$vm_uuid"
472
473 if [ "$AS_TEMPLATE" ]
474 then
475 xe vm-param-set uuid="$vm_uuid" is-a-template=true \
476 other-config:instant=true
477 echo -n "Installing VPX from template... "
478 vm_uuid=$(xe vm-clone vm="$vm_uuid" new-name-label="${nl/ import/}")
479 xe vm-param-set is-a-template=false uuid="$vm_uuid"
480 echo "$vm_uuid."
481 fi
482fi
483
484
485log_vifs "$vm_uuid"
486
487echo -n "Starting VM... "
488xe vm-start uuid=$vm_uuid
489echo "done."
490
491
492show_ip()
493{
494 ip_addr=$(echo "$1" | sed -n "s,^.*"$2"/ip: \([^;]*\).*$,\1,p")
495 echo -n "IP address for $3: "
496 if [ "$ip_addr" = "" ]
497 then
498 echo "did not appear."
499 else
500 echo "$ip_addr."
501 fi
502}
503
504
505if [ "$WAIT_FOR_NETWORK" ]
506then
507 echo "Waiting for network configuration... "
508 i=0
509 while [ $i -lt 600 ]
510 do
511 ip=$(xe_min vm-list params=networks uuid=$vm_uuid)
512 if [ "$ip" != "<not in database>" ]
513 then
514 show_ip "$ip" "1" "$BRIDGE_M"
515 if [[ $BRIDGE_P ]]
516 then
517 show_ip "$ip" "2" "$BRIDGE_P"
518 fi
519 echo "Installation complete."
520 exit 0
521 fi
522 sleep 10
523 let i=i+1
524 done
525fi