summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdinstall/scripts/zfsboot
blob: 2248a0d2050d24833e38d93f6da4fa798f9b4321 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
#!/bin/sh
#-
# Copyright (c) 2013 Allan Jude
# Copyright (c) 2013 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES

BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." "$0"
f_include $BSDCFG_SHARE/device.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/password/password.subr
f_include $BSDCFG_SHARE/variable.subr

############################################################ CONFIGURATION

#
# Default name of the boot-pool
#
: ${ZFSBOOT_POOL_NAME:=zroot}

#
# Default name for the boot environment parent dataset
#
: ${ZFSBOOT_BEROOT_NAME:=ROOT}

#
# Default name for the primany boot environment
#
: ${ZFSBOOT_BOOTFS_NAME:=default}

#
# Default Virtual Device (vdev) type to create
#
: ${ZFSBOOT_VDEV_TYPE:=stripe}

#
# Should we use gnop(8) to configure a transparent mapping to 4K sectors?
#
: ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=1}

#
# Should we use geli(8) to encrypt the drives?
#
: ${ZFSBOOT_GELI_ENCRYPTION=}

#
# Default name the unencrypted pool when using geli(8) to encrypt the drives
#
: ${ZFSBOOT_GELI_POOL_NAME:=bootpool}

#
# Default size for the unencrypted boot pool when using geli(8)
#
: ${ZFSBOOT_GELI_BOOT_SIZE:=2g}

#
# Default path to the geli(8) keyfile used in drive encryption
#
: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}

#
# Default disks to use (always empty unless being scripted)
#
: ${ZFSBOOT_DISKS:=}

#
# Default partitioning scheme to use on disks
#
: ${ZFSBOOT_PARTITION_SCHEME:=GPT}

#
# How much swap to put on each block device in the boot zpool
# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
#
: ${ZFSBOOT_SWAP_SIZE:=2g}

#
# Default ZFS datasets for root zpool
#
# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
# NOTE: Anything after pound/hash character [#] is ignored as a comment.
#
f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
	# DATASET	OPTIONS (comma or space separated; or both)

	# Boot Environment [BE] root and default boot dataset
	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/

	# Compress /tmp, allow exec but not setuid
	/tmp		mountpoint=/tmp,compression=lz4,exec=on,setuid=off

	# Don't mount /usr so that 'base' files go to the BEROOT
	/usr		mountpoint=/usr,canmount=off

	# Home directories separated so they are common to all BEs
	/usr/home	# NB: /home is a symlink to /usr/home

	# Ports tree
	/usr/ports		compression=lz4,setuid=off

	# Source tree (compressed)
	/usr/src	compression=lz4,exec=off,setuid=off

	# Create /var and friends
	/var		mountpoint=/var
	/var/crash	compression=lz4,exec=off,setuid=off
	/var/log	compression=lz4,exec=off,setuid=off
	/var/tmp	compression=lz4,exec=on,setuid=off
" # END-QUOTE

#
# If interactive and the user has not explicitly chosen a vdev type or disks,
# make the user confirm scripted/default choices when proceeding to install.
#
: ${ZFSBOOT_CONFIRM_LAYOUT:=1}

############################################################ GLOBALS

#
# Format of a line in printf(1) syntax to add to fstab(5)
#
FSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"

#
# Command strings for various tasks
#
CHMOD_MODE='chmod %s "%s"'
DD_WITH_OPTIONS='dd if="%s" of="%s" %s'
ECHO_APPEND='echo "%s" >> "%s"'
GELI_ATTACH='geli attach -j - -k "%s" "%s"'
GELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
GNOP_CREATE='gnop create -S 4096 "%s"'
GPART_ADD='gpart add -t %s "%s"'
GPART_ADD_INDEX='gpart add -i %s -t %s "%s"'
GPART_ADD_INDEX_WITH_SIZE='gpart add -i %s -t %s -s %s "%s"'
GPART_ADD_LABEL='gpart add -l %s -t %s "%s"'
GPART_ADD_LABEL_WITH_SIZE='gpart add -l %s -t %s -s %s "%s"'
GPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
GPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
GPART_CREATE='gpart create -s %s "%s"'
GPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
LN_SF='ln -sf "%s" "%s"'
MKDIR_P='mkdir -p "%s"'
MOUNT_TYPE='mount -t %s "%s" "%s"'
PRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
PRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
SHELL_TRUNCATE=':> "%s"'
ZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
ZFS_SET='zfs set "%s" "%s"'
ZFS_UNMOUNT='zfs unmount "%s"'
ZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
ZPOOL_EXPORT='zpool export "%s"'
ZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
ZPOOL_SET='zpool set %s "%s"'

#
# Strings that should be moved to an i18n file and loaded with f_include_lang()
#
hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
hline_arrows_tab_enter="Press arrows, TAB or ENTER"
msg_an_unknown_error_occurred="An unknown error occurred"
msg_back="Back"
msg_cancel="Cancel"
msg_change_selection="Change Selection"
msg_configure_options="Configure Options:"
msg_detailed_disk_info="gpart(8) show %s:\n%s\n\ncamcontrol(8) inquiry %s:\n%s\n\n\ncamcontrol(8) identify %s:\n%s\n"
msg_disk_info="Disk Info"
msg_disk_info_help="Get detailed information on disk device(s)"
msg_encrypt_disks="Encrypt Disks?"
msg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
msg_error="Error"
msg_force_4k_sectors="Force 4K Sectors?"
msg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
msg_freebsd_installer="FreeBSD Installer"
msg_geli_password="Enter a strong passphrase, used to protect your encryption keys. You will be required to enter this passphrase each time the system is booted"
msg_geli_setup="Initializing encryption on the selected disks, this will take several seconds per disk"
msg_install="Install"
msg_install_desc="Proceed with Installation"
msg_install_help="Create ZFS boot pool with displayed options"
msg_invalid_disk_argument="Invalid disk argument \`%s'"
msg_invalid_geli_boot_size="Invalid geli(8) boot size \`%s'"
msg_invalid_index_argument="Invalid index argument \`%s'"
msg_invalid_swap_size="Invalid swap size \`%s'"
msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
msg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy\nthe current contents of the following disks:\n\n   %s"
msg_last_chance_are_you_sure_color='\\ZrLast Chance!\\ZR Are you \\Z1sure\\Zn you want to \\Zr\\Z1destroy\\Zn\nthe current contents of the following disks:\n\n   %s'
msg_mirror_desc="Mirror - n-Way Mirroring"
msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
msg_missing_disk_arguments="missing disk arguments"
msg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
msg_no="NO"
msg_no_disks_present_to_configure="No disk(s) present to configure"
msg_no_disks_selected="No disks selected."
msg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
msg_null_disk_argument="NULL disk argument"
msg_null_index_argument="NULL index argument"
msg_null_poolname="NULL poolname"
msg_ok="OK"
msg_partition_scheme="Partition Scheme"
msg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
msg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
msg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
msg_pool_name="Pool Name"
msg_pool_name_cannot_be_empty="Pool name cannot be empty."
msg_pool_name_help="Customize the name of the zpool to be created (Required)"
msg_pool_type_disks="Pool Type/Disks:"
msg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
msg_processing_selection="Processing selection..."
msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
msg_rescan_devices="Rescan Devices"
msg_rescan_devices_help="Scan for device changes"
msg_select="Select"
msg_select_a_disk_device="Select a disk device"
msg_select_virtual_device_type="Select Virtual Device type:"
msg_stripe_desc="Stripe - No Redundancy"
msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
msg_swap_size="Swap Size"
msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
msg_these_disks_are_too_small="These disks are too small given the amount of requested\nswap (%s) and/or geli(8) (%s) partitions, which would\ntake 50%% or more of each of the following selected disk\ndevices (not recommended):\n\n  %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of devices."
msg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
msg_unsupported_partition_scheme="%s is an unsupported partition scheme"
msg_user_cancelled="User Cancelled."
msg_yes="YES"
msg_zfs_configuration="ZFS Configuration"

############################################################ FUNCTIONS

# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local title="$DIALOG_TITLE"
	local btitle="$DIALOG_BACKTITLE"
	local prompt="$msg_configure_options"
	local force4k="$msg_no"
	local usegeli="$msg_no"
	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
	local disks n=$( set -- $ZFSBOOT_DISKS; echo $# )
	{ [ $n -eq 1 ] && disks=disk; } || disks=disks # grammar
	local menu_list="
		'>>> $msg_install'        '$msg_install_desc'
		                          '$msg_install_help'
		'T $msg_pool_type_disks'  '$ZFSBOOT_VDEV_TYPE: $n $disks'
                                          '$msg_pool_type_disks_help'
		'- $msg_rescan_devices'   '*'
		                          '$msg_rescan_devices_help'
		'- $msg_disk_info'        '*'
		                          '$msg_disk_info_help'
		'N $msg_pool_name'        '$ZFSBOOT_POOL_NAME'
		                          '$msg_pool_name_help'
		'4 $msg_force_4k_sectors' '$force4k'
		                          '$msg_force_4k_sectors_help'
		'E $msg_encrypt_disks'    '$usegeli'
		                          '$msg_encrypt_disks_help'
		'P $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
		                          '$msg_partition_scheme_help'
		'S $msg_swap_size'        '$ZFSBOOT_SWAP_SIZE'
		                          '$msg_swap_size_help'
	" # END-QUOTE
	local defaultitem= # Calculated below
	local hline="$hline_alnum_arrows_punc_tab_enter"

	local height width rows
	eval f_dialog_menu_with_help_size height width rows \
		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list

	# Obtain default-item from previously stored selection
	f_dialog_default_fetch defaultitem

	local menu_choice
	menu_choice=$( eval $DIALOG \
		--title \"\$title\"              \
		--backtitle \"\$btitle\"         \
		--hline \"\$hline\"              \
		--item-help                      \
		--ok-label \"\$msg_select\"      \
		--cancel-label \"\$msg_cancel\"  \
		--default-item \"\$defaultitem\" \
		--menu \"\$prompt\"              \
		$height $width $rows             \
		$menu_list                       \
		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	)
	local retval=$?
	f_dialog_data_sanitize menu_choice
	f_dialog_menutag_store "$menu_choice"

	# Only update default-item on success
	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"

	return $retval
}

# dialog_last_chance $disks ...
#
# Display a list of the disks that the user is about to destroy. The default
# action is to return error status unless the user explicitly (non-default)
# selects "Yes" from the noyes dialog.
#
dialog_last_chance()
{
	local title="$DIALOG_TITLE"
	local btitle="$DIALOG_BACKTITLE"
	local prompt # Calculated below
	local hline="$hline_arrows_tab_enter"

	local height=8 width=50 prefix="   "
	local plen=${#prefix} list= line=
	local max_width=$(( $width - 3 - $plen ))

	local yes no defaultno extra_args format
	if [ "$USE_XDIALOG" ]; then
		yes=ok no=cancel defaultno=default-no
		extra_args="--wrap --left"
		format="$msg_last_chance_are_you_sure"
	else
		yes=yes no=no defaultno=defaultno
		extra_args="--colors --cr-wrap"
		format="$msg_last_chance_are_you_sure_color"
	fi

	local disk line_width
	for disk in $*; do
		if [ "$line" ]; then
			line_width=${#line}
		else
			line_width=$plen
		fi
		line_width=$(( $line_width + 1 + ${#disk} ))
		# Add newline before disk if it would exceed max_width
		if [ $line_width -gt $max_width ]; then
			list="$list$line\n"
			line="$prefix"
			height=$(( $height + 1 ))
		fi
		# Add the disk to the list
		line="$line $disk"
	done
	# Append the left-overs
	if [ "${line#$prefix}" ]; then
		list="$list$line"
		height=$(( $height + 1 ))
	fi

	# Add height for Xdialog(1)
	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))

	prompt=$( printf "$format" "$list" )
	f_dprintf "%s: Last Chance!" "$0"
	$DIALOG \
		--title "$title"        \
		--backtitle "$btitle"   \
		--hline "$hline"        \
		--$defaultno            \
		--$yes-label "$msg_yes" \
		--$no-label "$msg_no"   \
		$extra_args             \
		--yesno "$prompt" $height $width
}

# dialog_menu_layout
#
# Configure Virtual Device type and disks to use for the ZFS boot pool. User
# must select enough disks to satisfy the chosen vdev type.
#
dialog_menu_layout()
{
	local funcname=dialog_menu_layout
	local title="$DIALOG_TITLE"
	local btitle="$DIALOG_BACKTITLE"
	local vdev_prompt="$msg_select_virtual_device_type"
	local disk_prompt="$msg_please_select_one_or_more_disks"
	local vdev_menu_list="
		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
	" # END-QUOTE
	local disk_check_list= # Calculated below
	local vdev_hline="$hline_arrows_tab_enter"
	local disk_hline="$hline_arrows_space_tab_enter"

	# Warn the user if vdev type is not valid
	case "$ZFSBOOT_VDEV_TYPE" in
	stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
	*)
		f_dprintf "%s: Invalid virtual device type \`%s'" \
			  $funcname "$ZFSBOOT_VDEV_TYPE"
		f_show_err "$msg_invalid_virtual_device_type" \
			   "$ZFSBOOT_VDEV_TYPE"
		f_interactive || return $FAILURE
	esac

	# Calculate size of vdev menu once only
	local vheight vwidth vrows
	eval f_dialog_menu_with_help_size vheight vwidth vrows \
		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
		$vdev_menu_list

	# Get a list of probed disk devices
	local disks=
	f_device_find "" $DEVICE_TYPE_DISK disks
	f_dprintf "$funcname: disks=[%s]" "$disks"
	if [ ! "$disks" ]; then
		f_dprintf "No disk(s) present to configure"
		f_show_err "$msg_no_disks_present_to_configure"
		return $FAILURE
	fi

	# Lets sort the disks array to be more user friendly
	disks=$( echo "$disks" | tr ' ' '\n' | sort | tr '\n' ' ' )

	#
	# Operate in a loop so we can (if interactive) repeat if not enough
	# disks are selected to satisfy the chosen vdev type or user wants to
	# back-up to the previous menu.
	#
	local vardisk ndisks onoff selections vdev_choice
	while :; do
		#
		# Confirm the vdev type that was selected
		#
		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
			vdev_choice=$( eval $DIALOG \
				--title \"\$title\"              \
				--backtitle \"\$btitle\"         \
				--hline \"\$vdev_hline\"         \
				--ok-label \"\$msg_ok\"          \
				--cancel-label \"\$msg_cancel\"  \
				--item-help                      \
				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
				--menu \"\$vdev_prompt\"         \
				$vheight $vwidth $vrows          \
				$vdev_menu_list                  \
				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
			) || return $?
				# Exit if user pressed ESC or chose Cancel/No
			f_dialog_data_sanitize vdev_choice

			ZFSBOOT_VDEV_TYPE="$vdev_choice"
			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
			          "$ZFSBOOT_VDEV_TYPE"
		fi

		# Determine the number of disks needed for this vdev type
		local want_disks=0
		case "$ZFSBOOT_VDEV_TYPE" in
		stripe) want_disks=1 ;;
		mirror) want_disks=2 ;;
		raidz1) want_disks=3 ;;
		raidz2) want_disks=4 ;;
		raidz3) want_disks=5 ;;
		esac

		# Warn the user if any scripted disks are invalid
		local disk valid_disks=
		local all_valid=${ZFSBOOT_DISKS:+1} # optimism
		for disk in $ZFSBOOT_DISKS; do
			if f_struct device_$disk; then
				valid_disks="$valid_disks $disk"
				continue
			fi
			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
			all_valid=
		done
		if [ ! "$all_valid" ]; then
			if [ "$ZFSBOOT_DISKS" ]; then
				f_show_err \
				    "$msg_missing_one_or_more_scripted_disks"
			else
				f_dprintf "No disks selected."
				f_interactive ||
					f_show_err "$msg_no_disks_selected"
			fi
			f_interactive || return $FAILURE
		fi
		ZFSBOOT_DISKS="${valid_disks# }"

		#
		# Short-circuit if we're running non-interactively
		#
		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
			ndisks=$( set -- $ZFSBOOT_DISKS; echo $# )
			[ $ndisks -ge $want_disks ] && break # to success

			# Not enough disks selected
			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
				  "$ZFSBOOT_VDEV_TYPE" \
			          "Not enough disks selected." \
				  $ndisks $want_disks
			f_interactive || return $FAILURE
			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
				f_yesno "%s: $msg_not_enough_disks_selected" \
				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
				return $FAILURE
		fi

		#
		# Confirm the disks that were selected
		# Loop until the user cancels or selects enough disks
		#
		local breakout=
		while :; do
			# Loop over list of available disks, resetting state
			for disk in $disks; do unset _${disk}_status; done

			# Loop over list of selected disks and create temporary
			# locals to map statuses onto up-to-date list of disks
			for disk in $ZFSBOOT_DISKS; do
				local _${disk}_status=on
			done

			# Create the checklist menu of discovered disk devices
			disk_check_list=
			for disk in $disks; do
				local desc=
				device_$disk get desc desc
				f_shell_escape "$desc" desc
				f_getvar _${disk}_status:-off onoff
				disk_check_list="$disk_check_list
					$disk '$desc' $onoff"
			done

			local height width rows
			eval f_dialog_checklist_size height width rows \
				\"\$title\" \"\$btitle\" \"\$prompt\" \
				\"\$hline\" $disk_check_list

			selections=$( eval $DIALOG \
				--title \"\$DIALOG_TITLE\"         \
				--backtitle \"\$DIALOG_BACKTITLE\" \
				--hline \"\$hline\"                \
				--ok-label \"\$msg_ok\"            \
				--cancel-label \"\$msg_back\"      \
				--checklist \"\$prompt\"           \
				$height $width $rows               \
				$disk_check_list                   \
				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
			) || break
				# Loop if user pressed ESC or chose Cancel/No
			f_dialog_data_sanitize selections

			ZFSBOOT_DISKS="$selections"
			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
			          "$ZFSBOOT_DISKS"

			ndisks=$( set -- $ZFSBOOT_DISKS; echo $# )
			[ $ndisks -ge $want_disks ] &&
				breakout=break && break

			# Not enough disks selected
			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
				  "$ZFSBOOT_VDEV_TYPE" \
			          "Not enough disks selected." \
			          $ndisks $want_disks
			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
				f_yesno "%s: $msg_not_enough_disks_selected" \
				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
				break
		done
		[ "$breakout" = "break" ] && break
		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
	done

	return $DIALOG_OK
}

# zfs_create_diskpart $disk $index
#
# For each block device to be used in the zpool, rather than just create the
# zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
# so we can have some real swap. This also provides wiggle room incase your
# replacement drivers do not have the exact same sector counts.
#
# NOTE: The MBR layout is more complicated (GPT is preferred).
#
zfs_create_diskpart()
{
	local funcname=zfs_create_diskpart
	local disk="$1" index="$2"
	local disksize partsize

	# Check arguments
	if [ ! "$disk" ]; then
		f_dprintf "$funcname: NULL disk argument"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_null_disk_argument"
		return $FAILURE
	fi
	if [ "${disk#*[$IFS]}" != "$disk" ]; then
		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_invalid_disk_argument" "$disk"
		return $FAILURE
	fi
	if [ ! "$index" ]; then
		f_dprintf "$funcname: NULL index argument"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_null_index_argument"
		return $FAILURE
	fi
	if ! f_isinteger "$index"; then
		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_invalid_index_argument" "$index"
		return $FAILURE
	fi
	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"

	# Check for unknown partition scheme before proceeding further
	case "$ZFSBOOT_PARTITION_SCHEME" in
	""|MBR|GPT) : known good ;;
	*)
		f_dprintf "$funcname: %s is an unsupported partition scheme" \
		          "$ZFSBOOT_PARTITION_SCHEME"
		msg_error="$msg_error: $funcname" f_show_err \
			"$msg_unsupported_partition_scheme" \
			"$ZFSBOOT_PARTITION_SCHEME"
		return $FAILURE
	esac

	#
	# Destroy whatever partition layout is currently on disk.
	# NOTE: `-F' required to destroy if partitions still exist.
	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
	#
	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
	f_quietly gpart destroy -F $disk
	f_quietly graid destroy $disk
	f_quietly zpool labelclear -f /dev/$disk # Kill it with fire

	# Make doubly-sure backup GPT is destroyed
	f_quietly gpart create -s gpt $disk
	f_quietly gpart destroy -F $disk

	# Calculate partition size given desired amount of swap
	f_dprintf "$funcname: Getting disk capactiy for \`%s'" "$disk"
	if ! device_$disk get capacity disksize; then
		f_dprintf "$funcname: Unable to get disk capacity of \`%s'" \
		          "$disk"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_unable_to_get_disk_capacity" "$disk"
		return $FAILURE
	fi
	partsize=$(( $disksize - $swapsize ))
	f_dprintf "$funcname: disksize=[%s] partsize=[%s]" \
	          "$disksize" "$partsize"

	#
	# Lay down the desired type of partition scheme
	#
	local setsize mbrindex
	case "$ZFSBOOT_PARTITION_SCHEME" in
	""|GPT) f_dprintf "$funcname: Creating GPT layout..."
		#
		# 1. Create GPT layout using labels
		#
		f_eval_catch $funcname gpart "$GPART_CREATE" gpt \$disk ||
		             return $FAILURE

		#
		# 2. Add small freebsd-boot partition labeled `boot#'
		#
		f_eval_catch $funcname gpart "$GPART_ADD_LABEL_WITH_SIZE" \
		             gptboot\$index freebsd-boot 512k \$disk ||
		             return $FAILURE
		f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
		             /boot/pmbr /boot/gptzfsboot 1 \$disk ||
		             return $FAILURE

		# zpool will use the `zfs#' GPT labels
		bootpart=p2 targetpart=p2

		# Change things around if we are using geli(8)
		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
			bootpart=p2 targetpart=p3
			partsize=$(( $partsize - $gelisize ))
			f_dprintf "$funcname: gelisize=[%s]" \
			          "gelisize=[$gelisize]"
			f_eval_catch $funcname gpart \
			             "$GPART_ADD_LABEL_WITH_SIZE" boot\$index \
			             freebsd-zfs \${gelisize}b \$disk ||
			             return $FAILURE

			# Pedantically nuke any old labels, stop geli
			f_quietly zpool labelclear -f "/dev/$disk$bootpart"
			f_quietly geli detach -f "/dev/$disk$targetpart"
		fi

		#
		# 3. Add freebsd-zfs partition labeled `zfs#' for zpool
		# NOTE: Using above calculated partsize to leave room for swap.
		#
		if [ ${swapsize:-0} -gt 0 ]; then
			f_dprintf "$funcname: partsize=[%s]" \
			          "partsize=[$partsize]"
			f_eval_catch $funcname gpart \
			             "$GPART_ADD_LABEL_WITH_SIZE" zfs\$index \
			             freebsd-zfs \${partsize}b \$disk ||
			             return $FAILURE
		else
			f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
			             zfs\$index freebsd-zfs \$disk ||
			             return $FAILURE
		fi
		f_quietly zpool labelclear -f "/dev/$disk$targetpart"

		#
		# 4. Add freebsd-swap partition labeled `swap#'
		#
		if [ $swapsize -gt 0 ]; then
			f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
			             swap\$index freebsd-swap \$disk ||
			             return $FAILURE
			# Update fstab(5)
			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
			             /dev/gpt/swap\$index none swap sw 0 0 \
			             \$BSDINSTALL_TMPETC/fstab ||
			             return $FAILURE
		fi
		;;

	MBR) f_dprintf "$funcname: Creating MBR layout..."
		#
		# 1. Create MBR layout (no labels)
		#
		f_eval_catch $funcname gpart "$GPART_CREATE" mbr \$disk ||
		             return $FAILURE
		f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
		             \$disk || return $FAILURE

		#
		# 2. Add freebsd slice with all available space
		#
		f_eval_catch $funcname gpart "$GPART_ADD" freebsd \$disk ||
		             return $FAILURE
		f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 \$disk ||
		             return $FAILURE
		f_quietly zpool labelclear -f /dev/${disk}s1 # Pedantic
		f_quietly gpart destroy -F ${disk}s1 # Pedantic

		#
		# 3. Write BSD scheme to the freebsd slice
		#
		f_eval_catch $funcname gpart "$GPART_CREATE" BSD \${disk}s1 ||
		             return $FAILURE

		# zpool will use s1a (no labels)
		bootpart=s1a targetpart=s1a mbrindex=1

		# Change things around if we are using geli(8)
		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
			bootpart=s1a targetpart=s1d
			partsize=$(( $partsize - $gelisize ))
			mbrindex=4 # If this is s1a then make the zpool s1d
			f_dprintf "$funcname: mbrindex=[%s] gelisize=[%s]" \
			          "$mbrindex" "$gelisize"
			f_eval_catch $funcname gpart \
			             "$GPART_ADD_INDEX_WITH_SIZE" \
			             1 freebsd-zfs \${gelisize}b \${disk}s1 ||
			             return $FAILURE
			# Pedantically nuke any old labels, stop geli
			f_quietly zpool labelclear -f "/dev/$disk$bootpart"
			f_quietly geli detach -f "/dev/$disk$targetpart"
		fi

		#
		# 4. Partition the BSD slice for ZFS
		# NOTE: Using above calculated partsize to leave room for swap.
		#
		if [ ${swapsize:-0} -gt 0 ]; then
			f_dprintf "$funcname: mbrindex=[%s] partsize=[%s]" \
			          "$mbrindex" "$partsize"
			f_eval_catch $funcname gpart \
			             "$GPART_ADD_INDEX_WITH_SIZE" \
			             \$mbrindex freebsd-zfs \${partsize}b \
			             \${disk}s1 || return $FAILURE
		else
			f_dprintf "$funcname: mbrindex=[%s]" "$mbrindex"
			f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
			             \$mbrindex freebsd-zfs \${disk}s1 ||
			             return $FAILURE
		fi
		f_quietly zpool labelclear -f /dev/$disk$targetpart # Pedantic

		#
		# 5. Add freebsd-swap partition
		#
		if [ $swapsize -gt 0 ]; then
			f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
			             2 freebsd-swap \${disk}s1 ||
			             return $FAILURE
			# Update fstab(5)
			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
			             /dev/\${disk}s1b none swap sw 0 0 \
			             \$BSDINSTALL_TMPETC/fstab ||
			             return $FAILURE
		fi
		;;

	esac # $ZFSBOOT_PARTITION_SCHEME

	return $SUCCESS
}

# zfs_create_boot $poolname $vdev_type $disks ...
#
# Creates boot pool and dataset layout. Returns error if something goes wrong.
# Errors are printed to stderr for collection and display.
#
zfs_create_boot()
{
	local funcname=zfs_create_boot
	local poolname="$1" vdev_type="$2"
	local bootpart targetpart

	shift 2 # name vdev_type

	# We may need this later
	local disks="$*"

	# Pedantic checks; should never be seen
	if [ ! "$poolname" ]; then
		f_dprintf "$funcname: NULL poolname"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_null_poolname"
		return $FAILURE
	fi
	if [ $# -lt 1 ]; then
		f_dprintf "$funcname: missing disk arguments"
		msg_error="$msg_error: $funcname" \
			f_show_err "$msg_missing_disk_arguments"
		return $FAILURE
	fi
	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
	          "$poolname" "$vdev_type"

	# Initialize fstab(5)
	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
	f_eval_catch $funcname sh \
	             "$SHELL_TRUNCATE" \$BSDINSTALL_TMPETC/fstab ||
	             return $FAILURE
	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
	             "# Device" Mountpoint FStype Options Dump "Pass#" \
	             \$BSDINSTALL_TMPETC/fstab || return $FAILURE

	# Expand SI units in desired sizes
	f_dprintf "$funcname: Expanding supplied swapsize/gelisize values..."
	local swapsize gelisize
	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
		f_dprintf "$funcname: Invalid swap size \`%s'" \
		          "$ZFSBOOT_SWAP_SIZE"
		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
		return $FAILURE
	fi
	if ! f_expand_number "$ZFSBOOT_GELI_BOOT_SIZE" gelisize; then
		f_dprintf "$funcname: Invalid geli(8) boot size \`%s'" \
		          "$ZFSBOOT_GELI_BOOT_SIZE"
		f_show_err "$msg_invalid_geli_boot_size" \
		           "$ZFSBOOT_GELI_BOOT_SIZE"
		return $FAILURE
	fi
	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
	f_dprintf "$funcname: ZFSBOOT_GELI_BOOT_SIZE=[%s] gelisize=[%s]" \
	          "$ZFSBOOT_GELI_BOOT_SIZE" "$gelisize"

	# Prepare the disks
	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
	local n=0
	for disk in $*; do
		zfs_create_diskpart $disk $n || return $FAILURE
		n=$(( $n + 1 ))
	done

	# MBR boot loader hack part 1
	# We have to do this early because geli gets in the way later
	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
		f_dprintf "$funcname: Copying MBR boot loader to disks..."
		f_dprintf "$funcname: disks=[%s]" "$disks"
		for disk in $disks; do
			f_dprintf "$funcname: disk=[%s]" "$disk"
			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
			             /boot/zfsboot /dev/\${disk}s1 count=1 ||
			             return $FAILURE
		done
	fi

	# Forced 4k alignment support provided by Geom NOP (see gnop(8))
	local unenc_list=
	if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
		local part="$targetpart" new_list=

		# We don't gnop the encrypted partition because geli will do
		# this for us gnop the unencrypted disk
		[ "$ZFSBOOT_GELI_ENCRYPTION" ] && part="$bootpart"

		f_dprintf "$funcname: Applying 4k alignment with gnop(8)..."
		f_dprintf "$funcname: *=[%s]" "$*"
		for disk in $*; do
			f_dprintf "$funcname: disk=[%s] part=[%s]" \
			          "$disk" "$part"
			f_eval_catch $funcname gnop "$GNOP_CREATE" \
			             \$disk\$part || return $FAILURE
			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
				unenc_list="$unenc_list $disk$part.nop"
			else
				new_list="$new_list $disk$targetpart.nop"
			fi
		done
		set -- $new_list
	else
		local new_list=
		for disk in $*; do
			new_list="$new_list $disk$targetpart"
			[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
				unenc_list="$unenc_list $disk$bootpart"
		done
		set -- $new_list
	fi

	#
	# If encryption is enabled, we need to create the GEOMs
	#
	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
		local bootvdev=
		local geli_pool="$BSDINSTALL_CHROOT/$ZFSBOOT_GELI_POOL_NAME"
		local key="$ZFSBOOT_GELI_KEY_FILE"

		f_dprintf "$funcname: Setting up disk encryption..."

		# Create the parent directories for our unencrypted pool
		f_quietly umount /mnt
		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
		             \$BSDINSTALL_CHROOT || return $FAILURE

		# Create mirror across the unencrypted partition on all disks
		[ $( set -- $unenc_list; echo $# ) -gt 1 ] && bootvdev=mirror

		f_dprintf "$funcname: %s %s %s" \
		          "ZFSBOOT_GELI_POOL_NAME=[$ZFSBOOT_GELI_POOL_NAME]" \
		          "bootvdev=[$bootvdev]" "unenc_list=[$unenc_list]"
		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
		             "-o altroot=\"\$BSDINSTALL_CHROOT\"
		              -m \"/\$ZFSBOOT_GELI_POOL_NAME\" -f" \
		             \$ZFSBOOT_GELI_POOL_NAME \$bootvdev \
		             \$unenc_list || return $FAILURE
		f_dprintf "$funcname: geli_pool=[%s]" "$geli_pool"
		f_eval_catch $funcname mkdir "$MKDIR_P" \$geli_pool/boot ||
		             return $FAILURE

		# Generate an encryption key using random(4)
		f_dprintf "$funcname: key=[%s]" "$key"
		f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
		             /dev/random \$geli_pool/\$key "bs=4096 count=1" ||
		             return $FAILURE

		# Create the geli(8) GEOMS
		local geli_list
		if ! msg_enter_new_password="$msg_geli_password" \
			f_dialog_input_password
		then
			f_dprintf "$funcname: User cancelled"
			f_show_err "$msg_user_cancelled"
			return $FAILURE
		fi
		f_dprintf "$funcname: disks=[%s]" "$disks"
		for disk in $disks; do
			f_dprintf "$funcname: disk=[%s] targetpart=[%s]" \
			          "$disk" "$targetpart"
			f_dialog_info "$msg_geli_setup" \
				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
			if ! echo "$pw_password" | f_eval_catch $funcname \
				geli "$GELI_PASSWORD_INIT" \
				\$geli_pool/boot/\$disk\$targetpart.eli \
				AES-XTS \$geli_pool/\$key \$disk\$targetpart
			then
				f_interactive || f_die
				return $FAILURE
			fi
			if ! echo "$pw_password" | f_eval_catch $funcname \
				geli "$GELI_ATTACH" \$geli_pool/\$key \
				\$disk\$targetpart
			then
				f_interactive || f_die
				return $FAILURE
			fi
			geli_list="$geli_list $disk$targetpart.eli"
		done
		set -- $geli_list
		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
		             \$ZFSBOOT_GELI_POOL_NAME || return $FAILURE
		f_quietly umount /mnt # done with tmpfs
	fi

	#
	# Create the ZFS pool with desired type and disk devices
	#
	local vdevs="$*"
	f_dprintf "$funcname: Creating boot pool..."
	f_dprintf "poolname=[%s] vdev_type=[%s] vdevs=[%s]" \
	          "$poolname" "$vdev_type" "$vdevs"
	f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
	             "-o altroot=\"\$BSDINSTALL_CHROOT\" -m none -f" \
	             \$poolname \$vdev_type \$vdevs

	# Customize the zpool a bit...
	local option
	f_dprintf "$funcname: Setting miscellaneous options on boot pool..."
	for option in checksum=fletcher4 atime=off; do
		f_dprintf "$funcname: option=[%s]" "$option"
		f_eval_catch $funcname zfs "$ZFS_SET" \$option \$poolname ||
		             return $FAILURE
	done

	#
	# Create ZFS dataset layout within the new boot pool
	#
	f_dprintf "$funcname: Creating ZFS datasets..."
	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
		# Skip blank lines and comments
		case "$dataset" in "#"*|"") continue; esac
		# Remove potential inline comments in options
		options="${options%%#*}"
		# Replace tabs with spaces
		f_replaceall "$options" "	" " " options
		# Reduce contiguous runs of space to one single space
		oldoptions=
		while [ "$oldoptions" != "$options" ]; do
			oldoptions="$options"
			f_replaceall "$options" "  " " " options
		done
		# Replace both commas and spaces with ` -o '
		f_replaceall "$options" "[ ,]" " -o " options
		# Create the dataset with desired options
		f_dprintf "$funcname: dataset=[%s] options=[%s]" \
		          "$dataset" "$options"
		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
		             "\${options:+-o \$options}" \$poolname\$dataset ||
		             return $FAILURE
	done

	# Touch up permissions on the tmp directories
	f_dprintf "$funcname: Modifying directory permissions..."
	local dir
	for dir in /tmp /var/tmp; do
		f_dprintf "$funcname: dir=[%s]" "$dir"
		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
		             \$BSDINSTALL_CHROOTDIR\$dir || return $FAILURE
	done

	# Create symlink(s)
	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
		f_dprintf "$funcname: Creating /boot symlink for GELI..."
		f_eval_catch $funcname ln "$LN_SF" \
		             \$ZFSBOOT_GELI_POOL_NAME/boot \
		             \$BSDINSTALL_CHROOT/boot || return $FAILURE
	fi

	# Set bootfs property
	f_dprintf "$funcname: Setting bootfs property..."
	f_dprintf "$funcname: %s %s" \
	          "ZFSBOOT_BEROOT_NAME=[$ZFSBOOT_BEROOT_NAME]" \
	          "ZFSBOOT_BOOTFS_NAME=[$ZFSBOOT_BOOTFS_NAME]"
	f_eval_catch $funcname zpool "$ZPOOL_SET" \
	    bootfs=\"\$poolname/\$ZFSBOOT_BEROOT_NAME/\$ZFSBOOT_BOOTFS_NAME\" \
	    \$poolname || return $FAILURE

	# Export the pool(s)
	f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
	f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \$poolname ||
	             return $FAILURE
	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
		f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
		             \$ZFSBOOT_GELI_POOL_NAME || return $FAILURE
	fi

	# Destroy the gnop devices (if enabled)
	for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$disks}; do
		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
			f_quietly gnop destroy $disk$bootpart.nop
		else
			f_quietly gnop destroy $disk$targetpart.nop
		fi
	done

	# MBR boot loader hack part 2
	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
		f_dprintf "$funcname: Updating MBR boot loader on disks..."
		f_dprintf "$funcname: disks=[%s]"
		# Stick the ZFS boot loader in the "convienient hole" after 
		# the ZFS internal metadata
		for disk in $disks; do
			f_dprintf "$funcname: disk=[%s] bootpart=[%s]" \
			          "$disk" "$bootpart"
			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
			             /boot/zfsboot /dev/\$disk\$bootpart \
			             "skip=1 seek=1024" || return $FAILURE
		done
	fi

	# Re-import the ZFS pool(s)
	f_dprintf "$funcname: Re-importing ZFS pool(s)..."
	f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
	             "-o altroot=\"\$BSDINSTALL_CHROOT\"" \$poolname ||
	             return $FAILURE
	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
		f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
		             "-o altroot=\"\$BSDINSTALL_CHROOT\"" \
		             \$ZFSBOOT_GELI_POOL_NAME || return $FAILURE
	fi

	# While this is apparently not needed, it seems to help MBR
	f_dprintf "$funcname: Configuring zpool.cache..."
	f_eval_catch $funcname mkdir "$MKDIR_P" \$BSDINSTALL_CHROOT/boot/zfs ||
	             return $FAILURE
	f_eval_catch $funcname zpool "$ZPOOL_SET" \
	             cachefile=\"\$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\" \
	             \$poolname || return $FAILURE

	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
	# NOTE: We later concatenate these into their destination
	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
	          "$funcname"
	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
	             \$BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_load=\"YES\"' \
	             \$BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE

	# We're all done unless we should go on to do encryption
	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS

	# Some additional geli(8) requirements for loader.conf(5)
	for option in \
		zpool_cache_load=\"YES\" \
		zpool_cache_type=\"/boot/zfs/zpool.cache\" \
		zpool_cache_name=\"/boot/zfs/zpool.cache\" \
	; do
		f_eval_catch $funcname echo "$ECHO_APPEND" \$option \
		             \$BSDINSTALL_TMPBOOT/loader.conf.zfs ||
		             return $FAILURE
	done

	#
	# Configure geli(8)-based encryption
	#
	f_dprintf "$funcname: Configuring disk encryption..."
	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
	    \$BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
	    \$BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
	f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
	    '"zfs:$poolname/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOT_FSNAME"' \
	    \$BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
	f_dprintf "$funcname: disks=[%s]" "$disks"
	for disk in $disks; do
		f_dprintf "$funcname: %s %s %s" \
		          "disk=[$disk]" "targetpart=[$targetpart]" \
		          "ZFSBOOT_GELI_KEY_FILE=[$ZFSBOOT_GELI_KEY_FILE]"
		f_eval_catch $funcname printf "$PRINTF_CONF" \
			geli_%s_keyfile0_load '"$disk$targetpart" YES' \
			\$BSDINSTALL_TMPBOOT/loader.conf.\$disk\$targetpart ||
			return $FAILURE
		f_eval_catch $funcname printf "$PRINTF_CONF" \
			geli_%s_keyfile0_type \
			'"$disk$targetpart" "$disk$targetpart"' \
			\$BSDINSTALL_TMPBOOT/loader.conf.\$disk\$targetpart ||
			return $FAILURE
		f_eval_catch $funcname printf "$PRINTF_CONF" \
			geli_%s_keyfile0_name \
			'"$disk$targetpart" "$ZFSBOOT_GELI_KEY_FILE"' \
			\$BSDINSTALL_TMPBOOT/loader.conf.\$disk\$targetpart ||
			return $FAILURE
	done

	return $SUCCESS
}

# dialog_menu_diskinfo
#
# Prompt the user to select a disk and then provide detailed info on it.
#
dialog_menu_diskinfo()
{
	local disk

	#
	# Break from loop when user cancels disk selection
	#
	while :; do
		disk=$( msg_cancel="$msg_back" f_device_menu \
			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
			$DEVICE_TYPE_DISK 2>&1 ) || break

		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
		f_show_msg "$msg_detailed_disk_info" \
			"$disk" "$( gpart show $disk 2> /dev/null )" \
			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
	done

	return $SUCCESS
}

############################################################ MAIN

#
# Initialize
#
f_dialog_title "$msg_zfs_configuration"
f_dialog_backtitle "$msg_freebsd_installer"

# User may have specifically requested ZFS-related operations be interactive
! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE

#
# Debugging
#
f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
f_dprintf "PRINTF_FSTAB=[%s]" "$PRINTF_FSTAB"

#
# Loop over the main menu until we've accomplished what we came here to do
#
while :; do
	if ! f_interactive; then
		retval=$DIALOG_OK
		mtag=">>> $msg_install"
	else
		dialog_menu_main
		retval=$?
		f_dialog_menutag_fetch mtag
	fi

	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
	[ $retval -eq $DIALOG_OK ] || f_die

	case "$mtag" in
	">>> $msg_install")
		#
		# First, validate the user's selections
		#

		# Make sure they gave us a name for the pool
		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
			f_dprintf "Pool name cannot be empty."
			f_show_err "$msg_pool_name_cannot_be_empty"
			continue
		fi

		# Validate vdev type against number of disks selected/scripted
		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
		# NB: dialog_menu_layout supports running non-interactively
		dialog_menu_layout || continue

		# Make sure each disk will be at least 50% ZFS
		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
		   f_expand_number "$ZFSBOOT_GELI_BOOT_SIZE" gelisize
		then
			minsize=$swapsize teeny_disks=
			[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
				minsize=$(( $minsize + $gelisize ))
			for disk in $ZFSBOOT_DISKS; do
				device_$disk get capacity disksize || continue
				disksize=$(( $disksize - $minsize ))
				[ $disksize -lt $minsize ] &&
					teeny_disks="$teeny_disks $disk"
			done
			if [ "$teeny_disks" ]; then
				f_dprintf "swapsize=[%s] gelisize[%s]" \
				          "$ZFSBOOT_SWAP_SIZE" \
				          "$ZFSBOOT_GELI_BOOT_SIZE"
				f_dprintf "These disks are too small: %s" \
				          "$teeny_disks"
				f_show_err "$msg_these_disks_are_too_small" \
				           "$ZFSBOOT_SWAP_SIZE" \
				           "$ZFSBOOT_GELI_BOOT_SIZE" \
				           "$teeny_disks"
				continue
			fi
		fi

		#
		# Last Chance!
		#
		if f_interactive; then
			dialog_last_chance $ZFSBOOT_DISKS || continue
		fi

		#
		# Let's do this
		#

		vdev_type="$ZFSBOOT_VDEV_TYPE"

		# Blank the vdev type for the default layout
		[ "$vdev_type" = "stripe" ] && vdev_type=

		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
		                "$vdev_type" $ZFSBOOT_DISKS || continue

		break # to success
		;;
	?" $msg_pool_type_disks")
		ZFSBOOT_CONFIRM_LAYOUT=1
		dialog_menu_layout
		# User has poked settings, disable later confirmation
		ZFSBOOT_CONFIRM_LAYOUT=
		;;
	"- $msg_rescan_devices") f_device_rescan ;;
	"- $msg_disk_info") dialog_menu_diskinfo ;;
	?" $msg_pool_name")
		# Prompt the user to input/change the name for the new pool
		f_dialog_input input \
			"$msg_please_enter_a_name_for_your_zpool" \
			"$ZFSBOOT_POOL_NAME" &&
			ZFSBOOT_POOL_NAME="$input"
		;;
	?" $msg_force_4k_sectors")
		# Toggle the variable referenced both by the menu and later
		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
			ZFSBOOT_GNOP_4K_FORCE_ALIGN=
		else
			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
		fi
		;;
	?" $msg_encrypt_disks")
		# Toggle the variable referenced both by the menu and later
		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
			ZFSBOOT_GELI_ENCRYPTION=
		else
			ZFSBOOT_GELI_ENCRYPTION=1
		fi
		;;
	?" $msg_partition_scheme")
		# Toggle between GPT and MBR
		if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
			ZFSBOOT_PARTITION_SCHEME=MBR
		else
			ZFSBOOT_PARTITION_SCHEME=GPT
		fi
		;;
	?" $msg_swap_size")
		# Prompt the user to input/change the swap size for each disk
		f_dialog_input input \
			"$msg_please_enter_amount_of_swap_space" \
			"$ZFSBOOT_SWAP_SIZE" &&
			ZFSBOOT_SWAP_SIZE="$input"
		;;
	esac
done

return $SUCCESS

################################################################################
# END
################################################################################
OpenPOWER on IntegriCloud