-
Notifications
You must be signed in to change notification settings - Fork 28
/
install-main
1089 lines (898 loc) · 32.8 KB
/
install-main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/ash
#
# Copyright (c) 2014 Citrix Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
create_lv() {
local dfl_opts="--zero n"
local vg=$1
local name=$2
shift 2
local opts=$@
local version="$(lvcreate --version | sed -ne 's/\s\+LVM version:\s\+\([^0-9.]*\([0-9.]*\)\).*/\1/p')"
if version_ge ${version} "2.02.105"; then
dfl_opts="${dfl_opts} --wipesignatures n --yes"
fi
lvcreate ${dfl_opts} --name ${name} ${opts} ${vg}
ret=$?
udevadm settle
return $ret
}
mk_xc_lvm()
{
local PARTITION_DEV="$1"
do_cmd vgchange -a n || return 1
do_cmd pvcreate -ff -y "${PARTITION_DEV}" || return 1
do_cmd vgcreate xenclient "${PARTITION_DEV}" || return 1
do_cmd create_lv xenclient boot --size 12M || return 1
do_cmd create_lv xenclient config --size 12M || return 1
do_cmd create_lv xenclient root --size ${DOM0_ROOT_LV_SIZE} || return 1
do_cmd create_lv xenclient root.new --size ${DOM0_ROOT_LV_SIZE} || return 1
do_cmd create_lv xenclient swap --size 256M || return 1
do_cmd create_lv xenclient log --size 64M || return 1
do_cmd create_lv xenclient cores --size 64M || return 1
do_cmd create_lv xenclient storage -l +100%FREE || return 1
do_cmd lvresize -f /dev/xenclient/storage -L-1G || return 1
do_cmd vgchange -a y || return 1
}
apply_xc_packages()
{
local APPLY_MODE="$1"
local PACKAGES_DIR="$2"
dom0_uivm_ndvm_and_files()
{
local PACKAGE_FILE="${PACKAGES_DIR}/${FILENAME}"
case "${COMPONENT}" in
dom0)
case "${APPLY_MODE}" in
install)
install_dom0 "${PACKAGE_FILE}" "${PACKAGE_TYPE}"
;;
upgrade)
upgrade_dom0 "${PACKAGE_FILE}" "${PACKAGE_TYPE}"
;;
*)
false
;;
esac
;;
uivm)
mixedgauge "Installing ${UIVM_BRAND} (please wait)..." 50
install_uivm "${PACKAGE_FILE}" "${PACKAGE_TYPE}"
;;
ndvm)
mixedgauge "Installing ${NDVM_BRAND} (please wait)..." 65
install_ndvm "${PACKAGE_FILE}" "${PACKAGE_TYPE}"
;;
syncvm)
mixedgauge "Installing ${SYNCVM_BRAND} (please wait)..." 80
install_syncvm "${PACKAGE_FILE}" "${PACKAGE_TYPE}"
;;
file)
install_file "${PACKAGE_FILE}" "${DESTINATION}" "${PACKAGE_TYPE}"
;;
exec)
# FIXME - revisit this.
#
# If the upgrade fails, the current installation should be left
# untouched, so any changes which affect it ought to be
# deferred until commit_xc_packages.
#
# For example, if this executable makes changes in /, this will
# only affect the new installation, but if it makes changes in
# /storage, this could also affects the current installation.
exec_install_file "${APPLY_MODE}" "${PACKAGE_TYPE}" \
"${PACKAGE_FILE}" "${DESTINATION}"
;;
driverexec|control)
true # no-op at this point
;;
*)
false
;;
esac
if [ "$?" -ne 0 ] ; then
echo "ERROR: Failure installing ${COMPONENT} with file ${FILENAME}.">&2
return 1
fi
}
# Iterate over the install packages, looking for a handler for each one:
iterate_over_packages "${PACKAGES_DIR}" dom0_uivm_ndvm_and_files || return "$?"
return 0
}
commit_xc_packages()
{
local PACKAGES_DIR="$1"
dom0_uivm_ndvm_and_files()
{
local PACKAGE_FILE="${PACKAGES_DIR}/${FILENAME}"
case "${COMPONENT}" in
dom0)
commit_dom0
;;
uivm)
commit_uivm
;;
ndvm)
commit_ndvm
;;
syncvm)
commit_syncvm
;;
file)
commit_file "${DESTINATION}"
;;
exec)
# FIXME - revisit this - see comment in apply_xc_packages.
true
;;
driverexec|control)
true # no-op at this point
;;
*)
false
;;
esac
if [ "$?" -ne 0 ] ; then
echo "ERROR: Failure committing ${COMPONENT} with file ${FILENAME}.">&2
return 1
fi
}
# Iterate over the install packages, looking for a handler for each one:
iterate_over_packages "${PACKAGES_DIR}" dom0_uivm_ndvm_and_files || return "$?"
return 0
}
install_dom0()
{
local DOM0_ROOTFS="$1"
local ROOTFS_TYPE="$2"
local config_lv=/dev/xenclient/config
mixedgauge "Creating disk volumes..." ${PERCENT}
# Genereally PERCENT=10 and next phase will jump to 50, going in 5% increments
# gives 6 status updates without causing MS style progress bars
PERCENT=$((PERCENT + 5))
#calc size in bytes for dom0 rootfs, convert to MB and set DOM0_ROOT_LV_SIZE
DOM0_SIZE_BYTES=$(zcat ${DOM0_ROOTFS} | wc -c)
DOM0_ROOT_LV_SIZE=$(((${DOM0_SIZE_BYTES} / 1048576) + 1))M
local NODE=$(get_partition_node "${XC_PARTITION}")
mk_xc_lvm /dev/${NODE} >&2 || return 1
mixedgauge "Writing filesystem..." ${PERCENT}
PERCENT=$((PERCENT + 5))
# Setup rootfs first
write_rootfs ${DOM0_ROOTFS} ${ROOTFS_TYPE} ${ROOT_DEV}.new >&2 || return 1
do_cmd mke2fs -j /dev/xenclient/boot >&2 || return 1
do_cmd mkfs.ext4 -N 65536 -q /dev/xenclient/storage >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 /dev/xenclient/boot >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 /dev/xenclient/storage >&2 || return 1
mount_dom0 "${ROOT_DEV}.new" || return 1
mixedgauge "Obtaining recovery key..." ${PERCENT}
PERCENT=$((PERCENT + 5))
# Let installer see message before pass prompt
sleep 1
local RECOVERY_KEY=$(get_recovery_key)
mixedgauge "Formatting storage..." ${PERCENT}
PERCENT=$((PERCENT + 5))
# do_cmd doesn't do pipes -- what's wrong with set -x ?
# TODO: need to see if these set -x/set +x switching can be eliminated
set -x
#
# config
#
cryptsetup -q -i 600 --cipher=aes-xts-plain64 luksFormat ${config_lv} ${RECOVERY_KEY} >&2 || {
rm -f ${RECOVERY_KEY}
set +x
return 1
}
local PLATFORM_KEY=$(gen_platform_key ${DOM0_MOUNT})
set_platform_key ${RECOVERY_KEY} ${PLATFORM_KEY} ${config_lv}
# Open config lv.
cryptsetup -q -d ${RECOVERY_KEY} luksOpen /dev/xenclient/config config >&2 || {
set +x
return 1
}
# Formatting and mounting config first so we can lay down the keys directly
set +x
do_cmd mkfs.ext3 -q /dev/mapper/config >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 /dev/mapper/config >&2 || return 1
# mount config and copy dom0 /config
do_mount /dev/mapper/config ${TEMP_CONFIG_MOUNT} >&2 || return 1
do_cmd rsync --archive --xattrs \
${DOM0_MOUNT}/config/ ${TEMP_CONFIG_MOUNT} >&2 || return 1
do_mount --move ${TEMP_CONFIG_MOUNT} \
${DOM0_MOUNT}/config >&2 || return 1
# setup so keys can be written directly
do_cmd mkdir -p -m 500 ${DOM0_MOUNT}/config/keys >&2 || return 1
do_cmd mkdir -p -m 500 ${DOM0_MOUNT}/config/sec >&2 || return 1
# prepare lvm directory to store lvm metadata backups.
do_cmd mkdir -p -m 755 ${DOM0_MOUNT}/config/etc/lvm >&2 || return 1
#
# log
#
set -x
local LOG_KEY=$(gen_log_key ${DOM0_MOUNT})
cryptsetup -q -d ${LOG_KEY} create log /dev/xenclient/log >&2 || {
set +x
return 1
}
#
# cores
#
local CORES_KEY=$(gen_cores_key ${DOM0_MOUNT})
cryptsetup -q -d ${CORES_KEY} create cores /dev/xenclient/cores >&2 || {
set +x
return 1
}
# device key (used to tie encrypted disks to this host)
local DEVICE_KEY=$(gen_device_key ${DOM0_MOUNT})
set +x
do_cmd mkfs.ext3 -q /dev/mapper/log >&2 || return 1
do_cmd mkfs.ext3 -q /dev/mapper/cores >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 /dev/mapper/log >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 /dev/mapper/cores >&2 || return 1
do_mount /dev/mapper/log ${DOM0_MOUNT}/var/log >&2 || return 1
do_mount /dev/mapper/cores ${DOM0_MOUNT}/var/cores >&2 || return 1
# Reserve 4GiB on storage
do_cmd fallocate ${DOM0_MOUNT}/storage/xc-reserved -l 4GiB >&2 || return 1
do_cmd mkdir ${DOM0_MOUNT}/storage/disks >&2 || return 1
do_cmd mkdir ${DOM0_MOUNT}/storage/import >&2 || return 1
if [ -f "${PASSWORD_DEFERRED}" ]; then
touch ${DOM0_MOUNT}/config/deferred_dom0_password || return 1
fi
[ -r "${PASSWORD_HASH}" ] || return 1
sed -i "s|^root:[^:]*|root:$(cat ${PASSWORD_HASH})|" \
${DOM0_MOUNT}/config/etc/shadow || return 1
# do firstboot
touch ${DOM0_MOUNT}/boot/system/firstboot
if [ "${MEASURE_LAUNCH}" = "true" ]; then
mixedgauge "Configuring Measured Launch... Press random keys to speed up the sealing process." ${PERCENT}
PERCENT=$((PERCENT + 5))
CONFIG_KEY=$(configure_measured_launch "${DOM0_MOUNT}" "${RECOVERY_KEY}" "${config_lv}") || return 1
mixedgauge "Keying material generated successfully..." ${PERCENT}
quirk_fix "${DOM0_MOUNT}"
if [ $? -ne 0 ]; then
echo "A quirk \"fix\" script failed." >&2
return 1
fi
fi
finalize_keys "${RECOVERY_KEY}" "${PLATFORM_KEY}" "${CONFIG_KEY}" || return 1
}
upgrade_dom0()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not affect the existing
# installation.
local DOM0_ROOTFS="$1"
local ROOTFS_TYPE="$2"
if lv_in_use "${ROOT_DEV}.old" ; then
# Catch this problem now, as it would otherwise cause a failure while
# committing. (For debugging puposes only, to avoid rebooting before a
# repeated upgrade:
# lvremove -f /dev/xenclient/root
# lvrename /dev/xenclient/root.old /dev/xenclient/root )
echo "ERROR: Device ${ROOT_DEV}.old still in use." \
"(Has the system been rebooted since the last upgrade?)" >&2
return 1
fi
do_cmd lvremove -f ${ROOT_DEV}.new >&2
do_cmd lvremove -f ${ROOT_DEV}.old >&2
mixedgauge "Upgrading filesystem..." ${PERCENT}
PERCENT=$((PERCENT + 30))
DOM0_SIZE_BYTES=$(zcat ${DOM0_ROOTFS} | wc -c)
DOM0_ROOT_LV_SIZE=$(((${DOM0_SIZE_BYTES} / 1048576) + 1))M
local ROOT_LV="$(lvs --noheadings -o lv_name ${ROOT_DEV})"
local ROOT_VG="$(lvs --noheadings -o vg_name ${ROOT_DEV})"
do_cmd create_lv "${ROOT_VG}" "${ROOT_LV}.new" \
--size ${DOM0_ROOT_LV_SIZE} >&2 || return 1
write_rootfs ${DOM0_ROOTFS} ${ROOTFS_TYPE} ${ROOT_DEV}.new >&2 || {
do_cmd lvremove -f ${ROOT_DEV}.new >&2
return 1
}
do_cmd sync >&2
# Mount dom0's rootfs.
mount_dom0 "${ROOT_DEV}.new" || return 1
# do firstboot
touch ${DOM0_MOUNT}/boot/system/firstboot
}
lv_in_use()
{
local DEV="$1"
local OPEN_COUNT="$(lvdisplay -c ${DEV} | cut -f6 -d:)"
[ "${OPEN_COUNT:-0}" -gt 0 ]
}
commit_dom0()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not fail. If they fail,
# the system may be left in a broken state.
do_cmd lvremove -f "${ROOT_DEV}.old" >&2
do_cmd lvrename "${ROOT_DEV}" "${ROOT_DEV}.old" >&2 || return 1
do_cmd lvrename "${ROOT_DEV}.new" "${ROOT_DEV}" >&2 || {
do_cmd lvrename "${ROOT_DEV}.old" "${ROOT_DEV}" >&2
return 1
}
# Give udev a chance to create ${ROOT_DEV}
do_cmd udevadm settle
mount_dom0 "${ROOT_DEV}" || return 1
do_cmd mkdir -p ${DOM0_MOUNT}/storage/disks || return 1
copy_to_esp
if [ $? -eq 1 ]; then
return 1
fi
install_bootloader_from_dom0fs || return 1
if [ "${MEASURE_LAUNCH}" = "true" ] && [ "${INSTALL_MODE}" = "fresh" ]; then
mixedgauge "Taking ownership of TPM" 50
own_tpm || return 1
mixedgauge "Taking ownership of TPM" 100
fi
# handle refresh cases & measured launch misery
if [ "${INSTALL_MODE}" = "upgrade" ]; then
echo "commit_dom0: we're doing an upgrade" >&2
mount_config || return 1
if is_mounted ${DOM0_MOUNT}/config ; then
echo "commit_dom0: ${DOM0_MOUNT}/config is mounted" >&2
echo "commit_dom0: touching rehash-vms" >&2
touch ${DOM0_MOUNT}/config/rehash-vms
[ -e ${DOM0_MOUNT}/config/keys/cores.key ] || {
cp ${DOM0_MOUNT}/config/keys/cores-lv.key \
${DOM0_MOUNT}/config/keys/cores.key
}
[ -e ${DOM0_MOUNT}/config/keys/log.key ] || {
cp ${DOM0_MOUNT}/config/keys/log-lv.key \
${DOM0_MOUNT}/config/keys/log.key
}
fi
fi
}
seal_system() {
if [ "${INSTALL_MODE}" = "upgrade" ]; then
if [ "${EXISTING_MEASURED_STATE}" = "true" ]; then
mount_dom0 ${ROOT_DEV} ||
return 1
mount_config || {
echo "failed to mount config" >&2
do_umount_all ${DOM0_MOUNT}
return 1
}
if is_mounted ${DOM0_MOUNT}/config ; then
echo "seal_system: /config is mounted, forward sealing key" >&2
# Update config.pcrs in case it has changed
do_cmd chroot ${DOM0_MOUNT} \
sh -c '. /usr/lib/openxt/ml-functions ; write_config_pcrs' >&2
do_cmd /etc/init.d/trousers stop >&2
do_cmd chroot ${DOM0_MOUNT} \
/usr/sbin/seal-system -f -r ${ROOT_DEV} >&2
do_cmd /etc/init.d/trousers start >&2
do_umount_all ${DOM0_MOUNT}
return 0
fi
do_umount_all ${DOM0_MOUNT}
return 1
fi
fi
}
mount_dom0()
{
local ROOT="$1"
local tmpfsopts="size=64M"
if selinux_enforced ; then
tmpfsopts="$tmpfsopts,rootcontext=system_u:object_r:tmp_t:s0"
fi
do_mount -o ro ${ROOT} ${DOM0_MOUNT} &&
do_mount -o bind /proc ${DOM0_MOUNT}/proc &&
do_mount -o bind /sys ${DOM0_MOUNT}/sys &&
do_mount -o bind /dev ${DOM0_MOUNT}/dev &&
do_mount -t tmpfs -o $tmpfsopts tmpfs ${DOM0_MOUNT}/tmp &&
do_mount /dev/xenclient/boot ${DOM0_MOUNT}/boot/system &&
do_mount -o user_xattr /dev/xenclient/storage ${DOM0_MOUNT}/storage &&
if grep -qs "^securityfs " /proc/mounts ; then
do_mount -o bind /sys/kernel/security ${DOM0_MOUNT}/sys/kernel/security ||
return 1
fi &&
if grep -qs "^selinuxfs " /proc/mounts ; then
do_mount -o bind /sys/fs/selinux ${DOM0_MOUNT}/sys/fs/selinux ||
return 1
fi
}
mount_config()
{
# mount /config in the proper place if we can, bind it if already mounted
if is_mounted "${DOM0_MOUNT}/config" ; then
echo "mount_config: skipping - \"${DOM0_MOUNT}/config\" already mounted" >&2
return 0
elif is_mounted /config ; then
echo "mount_config: /config mounted, binding to ${DOM0_MOUNT}/config" >&2
do_mount -o bind /config ${DOM0_MOUNT}/config
return $?
elif [ -e /dev/mapper/config ]; then
echo "mount_config: config partition is already mapped, mounting" >&2
elif [ ! "${EXISTING_MEASURED_STATE}" = "true" ]; then
echo "mount_config: not sealed, not luksOpen-ed, using platform key to mount ..." >&2
platform_unlock "/dev/xenclient/config" config >&2
if [ $? -eq 0 ]; then
echo "mount_config: config mapped successfully, mounting" >&2
else
echo "mount_config: ERROR luksOpen-ing /dev/xenclient/config" >&2
return 1
fi
else # not already mounted, mapped and platform state is sealed
recovery_unlock "/dev/xenclient/config" config "${DOM0_MOUNT}" >&2
if [ $? -eq 0 ]; then
echo "mount_config: config mapped successfully, mounting" >&2
else
echo "mount_config: ERROR luksOpen-ing /dev/xenclient/config" >&2
return 1
fi
fi
# Return value of this command is return value for function
do_mount /dev/mapper/config ${DOM0_MOUNT}/config
}
install_bootloader_from_dom0fs()
{
do_cmd chroot ${DOM0_MOUNT} \
/usr/share/xenclient/install-bootloader "${LANGUAGE}" >&2 || {
echo "Error installing the bootloader">&2
return 1
}
return 0
}
remove_efi_boot_entries()
{
echo "Removing any existing OpenXT bootloader entries" >&2
for entry in $( efibootmgr | awk '/ OpenXT/ { print $1 }' ); do
entry=${entry%\*}
entry=${entry#Boot}
do_cmd efibootmgr -B -b $entry >&2
done
}
create_efi_boot_entries()
{
local DEVNODE="$1"
local DISK_DEV="$(get_devnode_disk ${DEVNODE})"
local PART="$(get_devnode_partition ${DEVNODE})"
remove_efi_boot_entries
do_cmd efibootmgr -w -L "OpenXT (safe graphics)" -l "\EFI\OpenXT\shimx64.efi" -c -d ${DISK_DEV} --part ${PART} -u "openxt-support-safe-graphics" >&2
if [ "$?" != "0" ]; then
echo "Couldn't create EFI boot entry" >&2
return 1
fi
do_cmd efibootmgr -w -L "OpenXT (AMT)" -l "\EFI\OpenXT\shimx64.efi" -c -d ${DISK_DEV} --part ${PART} -u "openxt-support-amt" >&2
if [ "$?" != "0" ]; then
echo "Couldn't create EFI boot entry" >&2
return 1
fi
do_cmd efibootmgr -w -L "OpenXT (console)" -l "\EFI\OpenXT\shimx64.efi" -c -d ${DISK_DEV} --part ${PART} -u "openxt-support-console" >&2
if [ "$?" != "0" ]; then
echo "Couldn't create EFI boot entry" >&2
return 1
fi
do_cmd efibootmgr -w -L "OpenXT (console AMT)" -l "\EFI\OpenXT\shimx64.efi" -c -d ${DISK_DEV} --part ${PART} -u "openxt-support-console-amt" >&2
if [ "$?" != "0" ]; then
echo "Couldn't create EFI boot entry" >&2
return 1
fi
do_cmd efibootmgr -w -L "OpenXT" -l "\EFI\OpenXT\shimx64.efi" -c -d ${DISK_DEV} --part ${PART} >&2
if [ "$?" != "0" ]; then
echo "Couldn't create EFI boot entry" >&2
return 1
fi
# Remove duplicate entries (if any)
do_cmd efibootmgr -D >&2
return 0
}
copy_to_esp()
{
# TARGET_DISK not defined in upgrades; locate our
# installation disk manually.
if [ -z "${TARGET_DISK}" ]; then
local devnode="$(vgs xenclient --noheadings -o pv_name | sed 's/\s*//g')"
local disk="$(get_devnode_disk ${devnode})"
else
local disk="/dev/${TARGET_DISK}"
fi
local ESP=$(sfdisk -l -q "${disk}" | grep "EFI System" | awk '{ print $1 }')
if [ "${ESP}" == "" ]; then
echo "No EFI System Partition on disk ${DISK_DEV}" >&2
if [ -d /sys/firmware/efi/efivars ]; then
return 1
else
return 0
fi
fi
do_mount ${ESP} ${ESP_MOUNT} >&2
if [ "$?" != "0" ]; then
echo "Couldn't mount ${ESP}, already mounted?" >&2
return 1
fi
# make sure the ESP is starting from a clean slate
# if we're upgrading. prevents outdated files from
# older versions from populating the partition.
if [ "${INSTALL_MODE}" = "upgrade" ]; then
do_cmd rm -rf ${ESP_MOUNT}/EFI/BOOT >&2
do_cmd rm -rf ${ESP_MOUNT}/EFI/OpenXT >&2
fi
do_cmd mkdir -p ${ESP_MOUNT}/EFI/BOOT >&2
do_cmd mkdir -p ${ESP_MOUNT}/EFI/OpenXT >&2
do_cmd cp -f ${DOM0_MOUNT}/boot/xen.efi \
${DOM0_MOUNT}/boot/shimx64.efi \
${DOM0_MOUNT}/boot/tboot \
${DOM0_MOUNT}/boot/bzImage \
${DOM0_MOUNT}/boot/initrd \
${DOM0_MOUNT}/boot/*.bin \
${DOM0_MOUNT}/boot/*.BIN \
${DOM0_MOUNT}/etc/xen/xenrefpolicy/policy/policy.24 \
${DOM0_MOUNT}/usr/share/xenclient/bootloader/openxt.cfg \
${DOM0_MOUNT}/usr/share/xenclient/bootloader/BOOT.CSV \
${ESP_MOUNT}/EFI/OpenXT >&2
if [ "$?" != "0" ]; then
echo "Failed to copy files to ESP" >&2
do_cmd umount ${ESP_MOUNT}
return 1
fi
# Copy the shim and fallback to the EFI removable path to work around buggy firmwares
do_cmd cp -f ${ESP_MOUNT}/EFI/OpenXT/shimx64.efi \
${ESP_MOUNT}/EFI/BOOT/BOOTX64.EFI >&2
do_cmd cp -f ${DOM0_MOUNT}/boot/fbx64.efi \
${ESP_MOUNT}/EFI/BOOT >&2
do_cmd sync >&2
do_cmd umount ${ESP_MOUNT} >&2
if [ "${INSTALL_MODE}" = "fresh" ] && [ -d /sys/firmware/efi/efivars ]; then
create_efi_boot_entries ${ESP}
return $?
fi
return 0
}
install_uivm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not affect the existing
# uivm installation.
local UIVM_IMG="$1"
local UIVM_IMG_TYPE="$2"
local UIVM_SWAP_SIZE_IN_MB="256"
local UIVM_DISK_SIZE_IN_MB="500"
local UIVM_DIR=`dirname ${UIVM_VHD}.new`
do_cmd mkdir -p ${UIVM_DIR} >&2 || return 1
# UIVM swap space.
create_swap_vhd "${UIVM_SWAP_VHD}.new" "${UIVM_SWAP_SIZE_IN_MB}" || return 1
# UIVM vhd
create_rootfs_vhd "${UIVM_IMG}" "${UIVM_IMG_TYPE}" "${UIVM_VHD}.new" \
"${UIVM_DISK_SIZE_IN_MB}" || return 1
# Remove UIVM kernel left by older releases. This can be removed once we
# no longer support direct upgrade from XT 3.1.x releases.
local UIVM_KERNEL="${DOM0_MOUNT}/storage/uivm/vmlinuz"
do_cmd rm -f "${UIVM_KERNEL}"
# UIVM gconf vhd
if [ ! -e ${UIVM_GCONF_VHD} ]; then
do_cmd vhd-util create -n ${UIVM_GCONF_VHD}.new \
-s 10 >&2 || return 1
do_cmd dd if=/dev/urandom of=${UIVM_GCONF_VHD_KEY} count=32 bs=1 || return 1
local VHD_FILENAME=`basename ${UIVM_GCONF_VHD}`
rm -f /tmp/${VHD_FILENAME}
ln -s ${UIVM_GCONF_VHD}.new /tmp/${VHD_FILENAME}
do_cmd vhd-util key -n /tmp/${VHD_FILENAME} -k ${UIVM_GCONF_VHD_KEY} -s
# We won't need it anymore.
rm -f /tmp/${VHD_FILENAME}
do_cmd sync >&2
fi
}
commit_uivm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not fail. If they fail,
# the system may be left in a broken state.
do_cmd rm -f "${UIVM_VHD}"
do_cmd mv "${UIVM_VHD}.new" "${UIVM_VHD}" || return 1
if [ -e "${UIVM_GCONF_VHD}.new" ]; then
do_cmd mv "${UIVM_GCONF_VHD}.new" "${UIVM_GCONF_VHD}" || return 1
fi
do_cmd rm -f "${UIVM_SWAP_VHD}"
do_cmd mv "${UIVM_SWAP_VHD}.new" "${UIVM_SWAP_VHD}" || return 1
}
install_ndvm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not affect the existing
# ndvm installation.
local NDVM_IMG="$1"
local NDVM_IMG_TYPE="$2"
local NDVM_SWAP_SIZE_IN_MB="256"
local NDVM_DISK_SIZE_IN_MB="500"
local NDVM_DIR=`dirname ${NDVM_VHD}.new`
do_cmd mkdir -p ${NDVM_DIR} >&2 || return 1
# NDVM swap space.
create_swap_vhd "${NDVM_SWAP_VHD}.new" "${NDVM_SWAP_SIZE_IN_MB}" || return 1
# NDVM vhd
create_rootfs_vhd "${NDVM_IMG}" "${NDVM_IMG_TYPE}" "${NDVM_VHD}.new" \
"${NDVM_DISK_SIZE_IN_MB}" || return 1
}
commit_ndvm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not fail. If they fail,
# the system may be left in a broken state.
do_cmd rm -f "${NDVM_VHD}"
do_cmd mv "${NDVM_VHD}.new" "${NDVM_VHD}" || return 1
do_cmd rm -f "${NDVM_SWAP_VHD}"
do_cmd mv "${NDVM_SWAP_VHD}.new" "${NDVM_SWAP_VHD}" || return 1
}
install_syncvm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not affect the existing
# syncvm installation.
local SYNCVM_IMG="$1"
local SYNCVM_IMG_TYPE="$2"
local SYNCVM_DISK_SIZE_IN_MB="100"
local SYNCVM_DIR=`dirname ${SYNCVM_VHD}.new`
do_cmd mkdir -p ${SYNCVM_DIR} >&2 || return 1
# SYNCVM vhd
create_rootfs_vhd "${SYNCVM_IMG}" "${SYNCVM_IMG_TYPE}" "${SYNCVM_VHD}.new" \
"${SYNCVM_DISK_SIZE_IN_MB}" || return 1
}
commit_syncvm()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not fail. If they fail,
# the system may be left in a broken state.
do_cmd rm -f "${SYNCVM_VHD}"
do_cmd mv "${SYNCVM_VHD}.new" "${SYNCVM_VHD}" || return 1
}
create_rootfs_vhd()
{
local IMG="$1"
local IMG_TYPE="$2"
local VHD="$3"
local DISK_SIZE_IN_MB="$4"
do_cmd rm -f "${VHD}" || return 1
if [ "${IMG_TYPE}" = "vhdgz" ] ; then
do_cmd gunzip -c "${IMG}" > "${VHD}" || return 1
else
do_cmd vhd-util create -n "${VHD}" \
-s "${DISK_SIZE_IN_MB}" >&2 || return 1
local DEV=$(tap-ctl create -a "vhd:${VHD}")
if ! tap-ctl list | grep -q "${VHD}"; then
tap-ctl destroy -d ${DEV} >&2
rm -f "${VHD}" >&2
return 1
fi
write_rootfs ${IMG} ${IMG_TYPE} ${DEV} >&2 || return 1
tap-ctl destroy -d ${DEV} >&2 || return 1
fi
}
write_rootfs()
{
local IMG="$1" # Image path
local IMG_TYPE="$2" # Image type
local DEVICE="$3" # Device where to write image
local ERR=0
case ${IMG_TYPE} in
cpiobz2)
do_cmd mkfs.ext3 -q ${DEVICE} >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 ${DEVICE} || return 1
do_mount ${DEVICE} ${TEMP_ROOTFS_MOUNT} >&2 || return 1
do_cmd cd ${TEMP_ROOTFS_MOUNT}
do_cmd bzcat ${IMG} | cpio -idm >&2
ERR=$?
do_cmd cd - >&2
do_umount ${TEMP_ROOTFS_MOUNT} >&2
;;
ext2bz2)
do_cmd bzcat ${IMG} > ${DEVICE} || return 1
do_cmd sync >&2 || return 1
;;
ext3gz)
do_cmd gunzip -c ${IMG} > ${DEVICE} || return 1
do_cmd sync >&2 || return 1
;;
none)
do_cmd mkfs.ext3 -q ${DEVICE} >&2 || return 1
do_cmd tune2fs -i 0 -c -1 -m 0 ${DEVICE} || return 1
;;
*)
echo "Unknown image type '${IMG_TYPE}'" >&2
return 1
esac
return $ERR
}
create_swap_vhd()
{
local VHD="$1"
local SIZE_IN_MB="$2"
do_cmd rm -f "${VHD}" 2>/dev/null
[ ! -e "${VHD}" ] >&2 || return 1
do_cmd vhd-util create -n "${VHD}" -s "${SIZE_IN_MB}" -r || return 1
do_cmd sync >&2
}
install_file()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not affect the existing
# installation.
local SRC="$1"
local DST="$2"
local PACKAGE_TYPE="$3"
local config_mounted="0"
local RET="0"
if [ "$( expr substr "${DST}" 1 8 )" = "/config/" ] ; then
mount_config || return 1
config_mounted="1"
fi
mkdir -p $(dirname "${DOM0_MOUNT}/${DST}") || RET="1"
rm -f "${DOM0_MOUNT}/${DST}.new" 2>/dev/null # ignore errors on this command.
if [ "${PACKAGE_TYPE}" = "gz" ] ; then
do_cmd gunzip -c "${SRC}" > "${DOM0_MOUNT}/${DST}.new" || RET="1"
else
cp "${SRC}" "${DOM0_MOUNT}/${DST}.new" || RET="1"
fi
if [ "$config_mounted" = "1" ] ; then
do_umount "${DOM0_MOUNT}/config"
fi
return "${RET}"
}
commit_file()
{
# Warning: A failed upgrade must leave the system untouched, so it is
# important that the commands in this function do not fail. If they fail,
# the system may be left in a broken state.
local DST="$1"
local RET=0
local config_mounted=0
if [ "$( expr substr "${DST}" 1 8 )" = "/config/" ] ; then
mount_config || return 1
config_mounted="1"
fi
rm -f "${DOM0_MOUNT}/${DST}"
mv "${DOM0_MOUNT}/${DST}.new" "${DOM0_MOUNT}/${DST}" || RET=1
if [ "$config_mounted" = "1" ] ; then
do_umount "${DOM0_MOUNT}/config"
fi
return "${RET}"
}
exec_install_file()
{
local APPLY_MODE="$1"
local PACKAGE_TYPE="$2"
local PACKAGE_FILE="$3"
local DESTINATION="$4"
echo -e >&2 "Executing install file with mode: ${APPLY_MODE}" \
"\nType: ${PACKAGE_TYPE}" \
"\nFrom: ${PACKAGE_FILE}" \
"\nTo: ${DESTINATION}\n"
local RESULT
case "${PACKAGE_TYPE}" in
bz2exe)
local EXE="$(mktemp -t)"
bunzip2 "${PACKAGE_FILE}" -q -c >"${EXE}"
chmod 755 "${EXE}"
"${EXE}" "${APPLY_MODE}" "${DOM0_MOUNT}" "${DESTINATION}"
RESULT="$?"
rm -f "${EXE}"
;;
exe)
local EXE="$(mktemp -t)"
# Copy in order to ensure executable permission is set
cp -f "${PACKAGE_FILE}" "${EXE}"
chmod 755 "${EXE}"
"${EXE}" "${APPLY_MODE}" "${DOM0_MOUNT}" "${DESTINATION}"
RESULT="$?"
rm -f "${EXE}"
;;
ash)
# Execute in place
ash "${PACKAGE_FILE}" "${APPLY_MODE}" "${DOM0_MOUNT}" "${DESTINATION}"
RESULT="$?"
rm -f "${EXE}"
;;
*)
echo "ERROR: Unknown exec package type: ${PACKAGE_TYPE}">&2
RESULT=1
;;
esac
return ${RESULT}
}
install_vhds_and_vms()
{
mixedgauge "Installing virtual machines (please wait)..." 90
if ! install_vhds ; then
echo "ERROR: Failure installing virtual hard disks." >&2
return 1
fi
if ! install_vms ; then
echo "ERROR: Failure installing virtual machines." >&2
return 1
fi
return 0
}
write_disk_gpt()
{
if [ "${INSTALL_GPT}" = "true" ] ; then
mixedgauge "Updating GPT..." 95
local DISK_DEV="/dev/${TARGET_DISK}"
if ! do_cmd sgdisk -g "${DISK_DEV}" >&2 ; then
echo "ERROR: Failure installing the disk GPT: aborting.">&2
return 1
fi
fi