xref: /freebsd/usr.sbin/bsdinstall/scripts/zfsboot (revision a62eaf71ddb7463cf51d2be8be6506befbcfff8d)
1#!/bin/sh
2#-
3# Copyright (c) 2013-2016 Allan Jude
4# Copyright (c) 2013-2018 Devin Teske
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/password/password.subr
37f_include $BSDCFG_SHARE/variable.subr
38
39############################################################ CONFIGURATION
40
41#
42# Default name of the boot-pool
43#
44: ${ZFSBOOT_POOL_NAME:=zroot}
45
46#
47# Default pool size is optional
48#
49: ${ZFSBOOT_POOL_SIZE=}
50
51#
52# Default options to use when creating zroot pool
53#
54: ${ZFSBOOT_POOL_CREATE_OPTIONS:=-O compression=on -O atime=off}
55
56#
57# Default name for the boot environment parent dataset
58#
59: ${ZFSBOOT_BEROOT_NAME:=ROOT}
60
61#
62# Default name for the primary boot environment
63#
64: ${ZFSBOOT_BOOTFS_NAME:=default}
65
66#
67# Default Virtual Device (vdev) type to create
68#
69: ${ZFSBOOT_VDEV_TYPE:=stripe}
70
71#
72# Should we use sysctl(8) vfs.zfs.vdev.min_auto_ashift=12 to force 4K sectors?
73#
74: ${ZFSBOOT_FORCE_4K_SECTORS=1}
75
76#
77# Should we use geli(8) to encrypt the drives?
78# NB: Automatically enables ZFSBOOT_BOOT_POOL
79#
80: ${ZFSBOOT_GELI_ENCRYPTION=}
81
82#
83# Default path to the geli(8) keyfile used in drive encryption
84#
85: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
86
87#
88# Create a separate boot pool?
89# NB: Automatically set when using geli(8)
90#
91: ${ZFSBOOT_BOOT_POOL=}
92
93#
94# Options to use when creating separate boot pool (if any)
95#
96: ${ZFSBOOT_BOOT_POOL_CREATE_OPTIONS:=}
97
98#
99# Default name for boot pool when enabled (e.g., geli(8))
100#
101: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
102
103#
104# Default size for boot pool when enabled (e.g., geli(8))
105#
106: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
107
108#
109# Default disks to use (always empty unless being scripted)
110#
111: ${ZFSBOOT_DISKS:=}
112
113#
114# Default partitioning scheme to use on disks
115#
116: ${ZFSBOOT_PARTITION_SCHEME:=}
117
118#
119# Default boot type to use on disks
120#
121: ${ZFSBOOT_BOOT_TYPE:=}
122
123#
124# How much swap to put on each block device in the boot pool
125# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
126#
127: ${ZFSBOOT_SWAP_SIZE:=2g}
128
129#
130# Should we use geli(8) to encrypt the swap?
131#
132: ${ZFSBOOT_SWAP_ENCRYPTION=}
133
134#
135# Should we use gmirror(8) to mirror the swap?
136#
137: ${ZFSBOOT_SWAP_MIRROR=}
138
139#
140# Default ZFS datasets for root pool
141#
142# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME
143# NOTE: Anything after pound/hash character [#] is ignored as a comment.
144# NOTE: Keep in sync with release/tools/vmimage.subr
145#
146f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
147	# DATASET	OPTIONS (comma or space separated; or both)
148
149	# Boot Environment [BE] root and default boot dataset
150	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
151	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
152
153	# Home directories separated so they are common to all BEs
154	/home		mountpoint=/home
155
156	# Create /tmp and allow exec but not setuid
157	/tmp		mountpoint=/tmp,exec=on,setuid=off
158
159	# Don't mount /usr so that 'base' files go to the BEROOT
160	/usr		mountpoint=/usr,canmount=off
161
162	# Ports tree
163	/usr/ports	setuid=off
164
165	# Source tree
166	/usr/src
167
168	# Create /var and friends
169	/var		mountpoint=/var,canmount=off
170	/var/audit	exec=off,setuid=off
171	/var/crash	exec=off,setuid=off
172	/var/log	exec=off,setuid=off
173	/var/mail	atime=on
174	/var/tmp	setuid=off
175" # END-QUOTE
176
177#
178# If interactive and the user has not explicitly chosen a vdev type or disks,
179# make the user confirm scripted/default choices when proceeding to install.
180#
181: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
182
183############################################################ GLOBALS
184
185#
186# Format of a line in printf(1) syntax to add to fstab(5)
187#
188FSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
189
190#
191# Command strings for various tasks
192#
193COPY='cp "%s" "%s"'
194CHMOD_MODE='chmod %s "%s"'
195DD_WITH_OPTIONS='dd if="%s" of="%s" %s'
196ECHO_APPEND='echo "%s" >> "%s"'
197ECHO_OVERWRITE='echo "%s" > "%s"'
198GELI_ATTACH='geli attach -j - -k "%s" "%s"'
199GELI_ATTACH_NOKEY='geli attach -j - "%s"'
200GELI_DETACH_F='geli detach -f "%s"'
201GELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
202GELI_PASSWORD_GELIBOOT_INIT='geli init -bg -e %s -J - -l 256 -s 4096 "%s"'
203GPART_ADD_ALIGN='gpart add %s -t %s "%s"'
204GPART_ADD_ALIGN_INDEX='gpart add %s -i %s -t %s "%s"'
205GPART_ADD_ALIGN_INDEX_WITH_SIZE='gpart add %s -i %s -t %s -s %s "%s"'
206GPART_ADD_ALIGN_LABEL='gpart add %s -l %s -t %s "%s"'
207GPART_ADD_ALIGN_LABEL_WITH_SIZE='gpart add %s -l %s -t %s -s %s "%s"'
208GPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
209GPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
210GPART_CREATE='gpart create -s %s "%s"'
211GPART_DESTROY_F='gpart destroy -F "%s"'
212GPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
213GPART_SET_LENOVOFIX='gpart set -a lenovofix "%s"'
214GPART_SET_PMBR_ACTIVE='gpart set -a active "%s"'
215GRAID_DELETE='graid delete "%s"'
216KLDLOAD='kldload %s'
217LN_SF='ln -sf "%s" "%s"'
218MKDIR_P='mkdir -p "%s"'
219MOUNT_TYPE='mount -t %s "%s" "%s"'
220NEWFS_ESP='newfs_msdos -F 32 -c 1 "%s"'
221PRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
222PRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
223SHELL_TRUNCATE=':> "%s"'
224SWAP_GMIRROR_LABEL='gmirror label swap %s'
225SYSCTL_ZFS_MIN_ASHIFT_12='sysctl vfs.zfs.vdev.min_auto_ashift=12'
226UMOUNT='umount "%s"'
227ZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
228ZFS_MOUNT='zfs mount "%s"'
229ZFS_SET='zfs set "%s" "%s"'
230ZFS_UNMOUNT='zfs unmount "%s"'
231ZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
232ZPOOL_DESTROY='zpool destroy "%s"'
233ZPOOL_EXPORT='zpool export "%s"'
234ZPOOL_EXPORT_F='zpool export -f "%s"'
235ZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
236ZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
237ZPOOL_SET='zpool set %s "%s"'
238
239#
240# Strings that should be moved to an i18n file and loaded with f_include_lang()
241#
242hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
243hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
244hline_arrows_tab_enter="Press arrows, TAB or ENTER"
245msg_all_possible_gpt_labels_already_taken="All possible GPT labels already taken"
246msg_an_unknown_error_occurred="An unknown error occurred"
247msg_back="Back"
248msg_cancel="Cancel"
249msg_change_selection="Change Selection"
250msg_configure_options="Configure Options:"
251msg_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"
252msg_disk_info="Disk Info"
253msg_disk_info_help="Get detailed information on disk device(s)"
254msg_disk_plural="disks"
255msg_disk_singular="disk"
256msg_encrypt_disks="Encrypt Disks?"
257msg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
258msg_error="Error"
259msg_force_4k_sectors="Force 4K Sectors?"
260msg_force_4k_sectors_help="Align partitions to 4K sector boundaries and set vfs.zfs.vdev.min_auto_ashift=12"
261msg_freebsd_installer="$OSNAME Installer"
262msg_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"
263msg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
264msg_install="Install"
265msg_install_desc="Proceed with Installation"
266msg_install_help="Create ZFS boot pool with displayed options"
267msg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
268msg_invalid_disk_argument="Invalid disk argument \`%s'"
269msg_invalid_index_argument="Invalid index argument \`%s'"
270msg_invalid_pool_size="Invalid pool size \`%s'"
271msg_invalid_swap_size="Invalid swap size \`%s'"
272msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
273msg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy\nthe current contents of the following disks:\n\n   %s"
274msg_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'
275msg_mirror_desc="Mirror - n-Way Mirroring"
276msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
277msg_missing_disk_arguments="missing disk arguments"
278msg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
279msg_no="NO"
280msg_no_disks_present_to_configure="No disk(s) present to configure"
281msg_no_disks_selected="No disks selected."
282msg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
283msg_null_disk_argument="NULL disk argument"
284msg_null_index_argument="NULL index argument"
285msg_null_poolname="NULL poolname"
286msg_odd_disk_selected="An even number of disks must be selected to create a RAID 1+0. (%u selected)"
287msg_ok="OK"
288msg_partition_scheme="Partition Scheme"
289msg_partition_scheme_help="Select partitioning scheme. GPT is recommended."
290msg_please_enter_a_name_for_your_pool="Please enter a name for your pool:"
291msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
292msg_please_select_one_or_more_disks="Please select one or more disks to create a pool:"
293msg_pool_name="Pool Name"
294msg_pool_name_cannot_be_empty="Pool name cannot be empty."
295msg_pool_name_help="Customize the name of the pool to be created (Required)"
296msg_pool_type_disks="Pool Type/Disks:"
297msg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
298msg_processing_selection="Processing selection..."
299msg_raid10_desc="RAID 1+0 - n x 2-Way Mirrors"
300msg_raid10_help="[4+ Disks] Striped Mirrors provides the best performance, but the least storage"
301msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
302msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
303msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
304msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
305msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
306msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
307msg_rescan_devices="Rescan Devices"
308msg_rescan_devices_help="Scan for device changes"
309msg_select="Select"
310msg_select_a_disk_device="Select a disk device"
311msg_select_virtual_device_type="Select Virtual Device type:"
312msg_stripe_desc="Stripe - No Redundancy"
313msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
314msg_swap_encrypt="Encrypt Swap?"
315msg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
316msg_swap_invalid="The selected swap size (%s) is invalid. Enter a number optionally followed by units. Example: 2G"
317msg_swap_mirror="Mirror Swap?"
318msg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
319msg_swap_size="Swap Size"
320msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
321msg_swap_toosmall="The selected swap size (%s) is too small. Please enter a value greater than 100MB or enter 0 for no swap"
322msg_these_disks_are_too_small="These disks are smaller than the amount of requested\nswap (%s) and/or geli(8) (%s) partitions, which would\ntake 100%% or more of each of the following selected disks:\n\n  %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of disks."
323msg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
324msg_unsupported_partition_scheme="%s is an unsupported partition scheme"
325msg_user_cancelled="User Cancelled."
326msg_yes="YES"
327msg_zfs_configuration="ZFS Configuration"
328msg_please_enter_options_for_your_pool="Please enter options for your pool"
329msg_zfs_options_name="ZFS Pool Options"
330msg_zfs_options_name_help="Customize ZFS options for the pool to be created"
331
332############################################################ FUNCTIONS
333
334# dialog_menu_main
335#
336# Display the dialog(1)-based application main menu.
337#
338dialog_menu_main()
339{
340	local title="$DIALOG_TITLE"
341	local btitle="$DIALOG_BACKTITLE"
342	local prompt="$msg_configure_options"
343	local force4k="$msg_no"
344	local usegeli="$msg_no"
345	local swapgeli="$msg_no"
346	local swapmirror="$msg_no"
347	[ "$ZFSBOOT_FORCE_4K_SECTORS" ] && force4k="$msg_yes"
348	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
349	[ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
350	[ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
351	local disks n disks_grammar
352	f_count n $ZFSBOOT_DISKS
353	{ [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
354		disks_grammar=$msg_disk_plural # grammar
355	local menu_list="
356		'>>> $msg_install'      '$msg_install_desc'
357		                        '$msg_install_help'
358		'T $msg_pool_type_disks'
359		               '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
360		               '$msg_pool_type_disks_help'
361		'- $msg_rescan_devices' '*'
362		                        '$msg_rescan_devices_help'
363		'- $msg_disk_info'      '*'
364		                        '$msg_disk_info_help'
365		'N $msg_pool_name'      '$ZFSBOOT_POOL_NAME'
366		                        '$msg_pool_name_help'
367		'4 $msg_force_4k_sectors'
368		                        '$force4k'
369		                        '$msg_force_4k_sectors_help'
370		'E $msg_encrypt_disks'  '$usegeli'
371		                        '$msg_encrypt_disks_help'
372		'P $msg_partition_scheme'
373		               '$ZFSBOOT_PARTITION_SCHEME ($ZFSBOOT_BOOT_TYPE)'
374		               '$msg_partition_scheme_help'
375		'S $msg_swap_size'      '$ZFSBOOT_SWAP_SIZE'
376		                        '$msg_swap_size_help'
377		'M $msg_swap_mirror'    '$swapmirror'
378		                        '$msg_swap_mirror_help'
379		'W $msg_swap_encrypt'   '$swapgeli'
380		                        '$msg_swap_encrypt_help'
381		'O $msg_zfs_options_name'   '$ZFSBOOT_POOL_CREATE_OPTIONS'
382		                        '$msg_zfs_options_name_help'
383	" # END-QUOTE
384	local defaultitem= # Calculated below
385	local hline="$hline_alnum_arrows_punc_tab_enter"
386
387	local height width rows
388	eval f_dialog_menu_with_help_size height width rows \
389		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
390
391	# Obtain default-item from previously stored selection
392	f_dialog_default_fetch defaultitem
393
394	local menu_choice
395	menu_choice=$( eval $DIALOG \
396		--title \"\$title\"              \
397		--backtitle \"\$btitle\"         \
398		--hline \"\$hline\"              \
399		--item-help                      \
400		--ok-label \"\$msg_select\"      \
401		--cancel-label \"\$msg_cancel\"  \
402		--default-item \"\$defaultitem\" \
403		--menu \"\$prompt\"              \
404		$height $width $rows             \
405		${USE_DIALOG:+--} $menu_list     \
406		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
407	)
408	local retval=$?
409	f_dialog_data_sanitize menu_choice
410	f_dialog_menutag_store "$menu_choice"
411
412	# Only update default-item on success
413	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
414
415	return $retval
416}
417
418# dialog_last_chance $disks ...
419#
420# Display a list of the disks that the user is about to destroy. The default
421# action is to return error status unless the user explicitly (non-default)
422# selects "Yes" from the noyes dialog.
423#
424dialog_last_chance()
425{
426	local title="$DIALOG_TITLE"
427	local btitle="$DIALOG_BACKTITLE"
428	local prompt # Calculated below
429	local hline="$hline_arrows_tab_enter"
430
431	local height=8 width=50 prefix="   "
432	local plen=${#prefix} list= line=
433	local max_width=$(( $width - 3 - $plen ))
434
435	local yes no defaultno extra_args format
436	if [ "$USE_XDIALOG" ]; then
437		yes=ok no=cancel defaultno=default-no
438		extra_args="--wrap --left"
439		format="$msg_last_chance_are_you_sure"
440	else
441		yes=yes no=no defaultno=defaultno
442		extra_args="--colors --cr-wrap"
443		format="$msg_last_chance_are_you_sure_color"
444	fi
445
446	local disk line_width
447	for disk in $*; do
448		if [ "$line" ]; then
449			line_width=${#line}
450		else
451			line_width=$plen
452		fi
453		line_width=$(( $line_width + 1 + ${#disk} ))
454		# Add newline before disk if it would exceed max_width
455		if [ $line_width -gt $max_width ]; then
456			list="$list$line\n"
457			line="$prefix"
458			height=$(( $height + 1 ))
459		fi
460		# Add the disk to the list
461		line="$line $disk"
462	done
463	# Append the left-overs
464	if [ "${line#$prefix}" ]; then
465		list="$list$line"
466		height=$(( $height + 1 ))
467	fi
468
469	# Add height for Xdialog(1)
470	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
471
472	prompt=$( printf "$format" "$list" )
473	f_dprintf "%s: Last Chance!" "$0"
474	$DIALOG \
475		--title "$title"        \
476		--backtitle "$btitle"   \
477		--hline "$hline"        \
478		--$defaultno            \
479		--$yes-label "$msg_yes" \
480		--$no-label "$msg_no"   \
481		$extra_args             \
482		--yesno "$prompt" $height $width
483}
484
485# dialog_menu_layout
486#
487# Configure Virtual Device type and disks to use for the ZFS boot pool. User
488# must select enough disks to satisfy the chosen vdev type.
489#
490dialog_menu_layout()
491{
492	local funcname=dialog_menu_layout
493	local title="$DIALOG_TITLE"
494	local btitle="$DIALOG_BACKTITLE"
495	local vdev_prompt="$msg_select_virtual_device_type"
496	local disk_prompt="$msg_please_select_one_or_more_disks"
497	local vdev_menu_list="
498		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
499		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
500		'raid10' '$msg_raid10_desc' '$msg_raid10_help'
501		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
502		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
503		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
504	" # END-QUOTE
505	local disk_check_list= # Calculated below
506	local vdev_hline="$hline_arrows_tab_enter"
507	local disk_hline="$hline_arrows_space_tab_enter"
508
509	# Warn the user if vdev type is not valid
510	case "$ZFSBOOT_VDEV_TYPE" in
511	stripe|mirror|raid10|raidz1|raidz2|raidz3) : known good ;;
512	*)
513		f_dprintf "%s: Invalid virtual device type \`%s'" \
514			  $funcname "$ZFSBOOT_VDEV_TYPE"
515		f_show_err "$msg_invalid_virtual_device_type" \
516			   "$ZFSBOOT_VDEV_TYPE"
517		f_interactive || return $FAILURE
518	esac
519
520	# Calculate size of vdev menu once only
521	local vheight vwidth vrows
522	eval f_dialog_menu_with_help_size vheight vwidth vrows \
523		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
524		$vdev_menu_list
525
526	# Get a list of probed disk devices
527	local disks=
528	debug= f_device_find "" $DEVICE_TYPE_DISK disks
529
530	# Prune out mounted md(4) devices that may be part of the boot process
531	local disk name new_list=
532	for disk in $disks; do
533		debug= $disk get name name
534		case "$name" in
535		md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
536		esac
537		new_list="$new_list $disk"
538	done
539	disks="${new_list# }"
540
541	# Prune out disks that are not available to install to
542	local avail_disks=
543	for disk in $disks; do
544		debug= $disk get name name
545		geom disk list $name | awk '$1 == "Mode:" && $2 != "r0w0e0" { exit 1 }'
546		[ $? -eq 0 ] && avail_disks="$avail_disks $disk"
547	done
548	disks="${avail_disks# }"
549
550	# Debugging
551	if [ "$debug" ]; then
552		local disk_names=
553		for disk in $disks; do
554			debug= $disk get name name
555			disk_names="$disk_names $name"
556		done
557		f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
558	fi
559
560	if [ ! "$disks" ]; then
561		f_dprintf "No disk(s) present to configure"
562		f_show_err "$msg_no_disks_present_to_configure"
563		return $FAILURE
564	fi
565
566	# Lets sort the disks array to be more user friendly
567	f_device_sort_by name disks disks
568
569	#
570	# Operate in a loop so we can (if interactive) repeat if not enough
571	# disks are selected to satisfy the chosen vdev type or user wants to
572	# back-up to the previous menu.
573	#
574	local vardisk ndisks onoff selections vdev_choice breakout device
575	local valid_disks all_valid want_disks desc height width rows
576	while :; do
577		#
578		# Confirm the vdev type that was selected
579		#
580		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
581			vdev_choice=$( eval $DIALOG \
582				--title \"\$title\"              \
583				--backtitle \"\$btitle\"         \
584				--hline \"\$vdev_hline\"         \
585				--ok-label \"\$msg_ok\"          \
586				--cancel-label \"\$msg_cancel\"  \
587				--item-help                      \
588				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
589				--menu \"\$vdev_prompt\"         \
590				$vheight $vwidth $vrows          \
591				$vdev_menu_list                  \
592				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
593			) || return $?
594				# Exit if user pressed ESC or chose Cancel/No
595			f_dialog_data_sanitize vdev_choice
596
597			ZFSBOOT_VDEV_TYPE="$vdev_choice"
598			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
599			          "$ZFSBOOT_VDEV_TYPE"
600		fi
601
602		# Determine the number of disks needed for this vdev type
603		want_disks=0
604		case "$ZFSBOOT_VDEV_TYPE" in
605		stripe) want_disks=1 ;;
606		mirror) want_disks=2 ;;
607		raid10) want_disks=4 ;;
608		raidz1) want_disks=3 ;;
609		raidz2) want_disks=4 ;;
610		raidz3) want_disks=5 ;;
611		esac
612
613		#
614		# Warn the user if any scripted disks are invalid
615		#
616		valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
617		for disk in $ZFSBOOT_DISKS; do
618			if debug= f_device_find -1 \
619				$disk $DEVICE_TYPE_DISK device
620			then
621				valid_disks="$valid_disks $disk"
622				continue
623			fi
624			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
625			all_valid=
626		done
627		if [ ! "$all_valid" ]; then
628			if [ "$ZFSBOOT_DISKS" ]; then
629				f_show_err \
630				    "$msg_missing_one_or_more_scripted_disks"
631			else
632				f_dprintf "No disks selected."
633				f_interactive ||
634					f_show_err "$msg_no_disks_selected"
635			fi
636			f_interactive || return $FAILURE
637		fi
638		ZFSBOOT_DISKS="${valid_disks# }"
639
640		#
641		# Short-circuit if we're running non-interactively
642		#
643		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
644			f_count ndisks $ZFSBOOT_DISKS
645			[ $ndisks -ge $want_disks ] && break # to success
646
647			# Not enough disks selected
648			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
649				  "$ZFSBOOT_VDEV_TYPE" \
650				  "Not enough disks selected." \
651				  $ndisks $want_disks
652			f_interactive || return $FAILURE
653			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
654				f_yesno "%s: $msg_not_enough_disks_selected" \
655				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
656				return $FAILURE
657		fi
658
659		#
660		# Confirm the disks that were selected
661		# Loop until the user cancels or selects enough disks
662		#
663		breakout=
664		while :; do
665			# Loop over list of available disks, resetting state
666			for disk in $disks; do
667				f_isset _${disk}_status && setvar _${disk}_status
668			done
669
670			# Loop over list of selected disks and create temporary
671			# locals to map statuses onto up-to-date list of disks
672			for disk in $ZFSBOOT_DISKS; do
673				debug= f_device_find -1 \
674					$disk $DEVICE_TYPE_DISK disk
675				f_isset _${disk}_status ||
676					local _${disk}_status
677				setvar _${disk}_status on
678			done
679
680			# Create the checklist menu of discovered disk devices
681			disk_check_list=
682			for disk in $disks; do
683				desc=
684				$disk get name name
685				$disk get desc desc
686				f_shell_escape "$desc" desc
687				f_getvar _${disk}_status:-off onoff
688				disk_check_list="$disk_check_list
689					$name '$desc' $onoff"
690			done
691
692			eval f_dialog_checklist_size height width rows \
693				\"\$title\" \"\$btitle\" \"\$disk_prompt\" \
694				\"\$disk_hline\" $disk_check_list
695
696			selections=$( eval $DIALOG \
697				--title \"\$DIALOG_TITLE\"         \
698				--backtitle \"\$DIALOG_BACKTITLE\" \
699				--separate-output                  \
700				--hline \"\$disk_hline\"           \
701				--ok-label \"\$msg_ok\"            \
702				--cancel-label \"\$msg_back\"      \
703				--checklist \"\$disk_prompt\"      \
704				$height $width $rows               \
705				$disk_check_list                   \
706				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
707			) || break
708				# Loop if user pressed ESC or chose Cancel/No
709			f_dialog_data_sanitize selections
710
711			ZFSBOOT_DISKS="$selections"
712			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
713			          "$ZFSBOOT_DISKS"
714
715			f_count ndisks $ZFSBOOT_DISKS
716
717			if [ "$ZFSBOOT_VDEV_TYPE" == "raid10" ] &&
718			    [ $(( $ndisks % 2 )) -ne 0 ]; then
719				f_dprintf "$funcname: %s: %s (%u %% 2 = %u)" \
720					  "$ZFSBOOT_VDEV_TYPE" \
721					  "Number of disks not even:" \
722					  $ndisks $(( $ndisks % 2 ))
723				msg_yes="$msg_change_selection" \
724					msg_no="$msg_cancel" \
725					f_yesno "%s: $msg_odd_disk_selected" \
726						"$ZFSBOOT_VDEV_TYPE" $ndisks ||
727						break
728				continue
729			fi
730
731			[ $ndisks -ge $want_disks ] &&
732				breakout=break && break
733
734			# Not enough disks selected
735			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
736				  "$ZFSBOOT_VDEV_TYPE" \
737			          "Not enough disks selected." \
738			          $ndisks $want_disks
739			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
740				f_yesno "%s: $msg_not_enough_disks_selected" \
741				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
742				break
743		done
744		[ "$breakout" = "break" ] && break
745		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
746	done
747
748	return $DIALOG_OK
749}
750
751# zfs_create_diskpart $disk $index
752#
753# For each block device to be used in the pool, rather than just create the
754# pool with the raw block devices (e.g., da0, da1, etc.) we create partitions
755# so we can have some real swap. This also provides wiggle room incase your
756# replacement drivers do not have the exact same sector counts.
757#
758# NOTE: $swapsize and $bootsize should be defined by the calling function.
759# NOTE: Sets $bootpart and $targetpart for the calling function.
760#
761zfs_create_diskpart()
762{
763	local funcname=zfs_create_diskpart
764	local disk="$1" index="$2"
765	local efibootpart
766
767	# Check arguments
768	if [ ! "$disk" ]; then
769		f_dprintf "$funcname: NULL disk argument"
770		msg_error="$msg_error: $funcname" \
771			f_show_err "$msg_null_disk_argument"
772		return $FAILURE
773	fi
774	if [ "${disk#*[$IFS]}" != "$disk" ]; then
775		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
776		msg_error="$msg_error: $funcname" \
777			f_show_err "$msg_invalid_disk_argument" "$disk"
778		return $FAILURE
779	fi
780	if [ ! "$index" ]; then
781		f_dprintf "$funcname: NULL index argument"
782		msg_error="$msg_error: $funcname" \
783			f_show_err "$msg_null_index_argument"
784		return $FAILURE
785	fi
786	if ! f_isinteger "$index"; then
787		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
788		msg_error="$msg_error: $funcname" \
789			f_show_err "$msg_invalid_index_argument" "$index"
790		return $FAILURE
791	fi
792	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
793
794	# Check for unknown partition scheme before proceeding further
795	case "$ZFSBOOT_PARTITION_SCHEME" in
796	""|GPT*) : known good ;;
797	*)
798		f_dprintf "$funcname: %s is an unsupported partition scheme" \
799		          "$ZFSBOOT_PARTITION_SCHEME"
800		msg_error="$msg_error: $funcname" f_show_err \
801			"$msg_unsupported_partition_scheme" \
802			"$ZFSBOOT_PARTITION_SCHEME"
803		return $FAILURE
804	esac
805
806	#
807	# Destroy whatever partition layout is currently on disk.
808	# NOTE: `-F' required to destroy if partitions still exist.
809	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
810	#
811	f_dprintf "$funcname: Exporting ZFS pools..."
812	zpool list -Ho name | while read z_name; do
813		f_eval_catch -d $funcname zpool "$ZPOOL_EXPORT_F" $z_name
814	done
815	f_dprintf "$funcname: Detaching all GELI providers..."
816	geli status | tail -n +2 | while read g_name g_status g_component; do
817		f_eval_catch -d $funcname geli "$GELI_DETACH_F" $g_name
818	done
819	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
820	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
821	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
822	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
823
824	# Make doubly-sure backup GPT is destroyed
825	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
826	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
827
828	#
829	# Lay down the desired type of partition scheme
830	#
831	local setsize align_small align_big
832	#
833	# If user has requested 4 K alignment, add these params to the
834	# gpart add calls. With GPT, we align large partitions to 1 M for
835	# improved performance on SSDs.
836	#
837	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
838		align_small="-a 4k"
839		align_big="-a 1m"
840	fi
841
842	case "$ZFSBOOT_PARTITION_SCHEME" in
843	""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
844		#
845		# 0. Check for potential GPT label conflicts with disks not
846		# managed by us and adjust $index accordingly
847		#
848		while [ -e /dev/gpt/efiboot$index ] ||
849			[ -e /dev/gpt/gptboot$index ] ||
850			[ -e /dev/gpt/boot$index ] ||
851			[ -e /dev/gpt/swap$index ] ||
852			[ -e /dev/gpt/zfs$index ]; do
853			index=$(( $index + 1 ))
854			if [ $index -ge 4096 ]; then
855				f_dprintf "$funcname: all possible GPT labels already taken"
856				msg_error="$msg_error: $funcname" f_show_err \
857					"$msg_all_possible_gpt_labels_already_taken"
858				return $FAILURE
859			fi
860		done
861
862		#
863		# 1. Create GPT layout using labels
864		#
865		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
866		             return $FAILURE
867
868		#
869		# Apply workarounds if requested by the user
870		#
871		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Lenovo Fix" ]; then
872			f_eval_catch $funcname gpart "$GPART_SET_LENOVOFIX" \
873			             $disk || return $FAILURE
874		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
875			f_eval_catch $funcname gpart "$GPART_SET_PMBR_ACTIVE" \
876			             $disk || return $FAILURE
877		fi
878
879		#
880		# 2. Add small freebsd-boot and/or efi partition
881		#
882		if [ "$ZFSBOOT_BOOT_TYPE" = "UEFI" -o \
883		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
884		then
885			f_eval_catch -k justaddedpart $funcname gpart \
886			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
887			             "$align_small" efiboot$index efi 260M \
888			             $disk || return $FAILURE
889
890			# We'll configure the ESP in bootconfig
891			# Note: This will always be p1
892			efibootpart="/dev/gpt/efiboot$index"
893			f_dprintf "$funcname: configuring ESP at [%s]" \
894			          "${efibootpart}"
895
896			f_eval_catch $funcname newfs_msdos "$NEWFS_ESP"\
897			             "$efibootpart" \
898			             || return $FAILURE
899			if [ $index -eq 0 ]; then
900				f_eval_catch $funcname printf "$PRINTF_FSTAB" \
901					$efibootpart /boot/efi msdosfs \
902					rw 2 2 "$BSDINSTALL_TMPETC/fstab" \
903					|| return $FAILURE
904			else
905				# Record the extra ones
906				echo "${efibootpart}" >> ${TMPDIR:-"/tmp"}/bsdinstall-esps
907			fi
908		fi
909
910		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" -o \
911		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
912		then
913			f_eval_catch $funcname gpart \
914			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
915			             "$align_small" gptboot$index \
916			             freebsd-boot 512k $disk || return $FAILURE
917			if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]; then
918				f_eval_catch $funcname gpart \
919				             "$GPART_BOOTCODE_PART" \
920				             /boot/pmbr /boot/gptzfsboot 1 \
921				             $disk || return $FAILURE
922			else
923				f_eval_catch $funcname gpart \
924				             "$GPART_BOOTCODE_PART" \
925				             /boot/pmbr /boot/gptzfsboot 2 \
926				             $disk || return $FAILURE
927			fi
928		fi
929
930		# NB: ZFS pools will use `zfs#' GPT labels
931		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]; then
932			if [ "$ZFSBOOT_BOOT_POOL" ]; then
933				bootpart=p3 swappart=p4 targetpart=p4
934				[ ${swapsize:-0} -gt 0 ] && targetpart=p5
935			else
936				# Bootpart unused
937				bootpart=p3 swappart=p3 targetpart=p3
938				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
939			fi
940		else
941			if [ "$ZFSBOOT_BOOT_POOL" ]; then
942				bootpart=p2 swappart=p3 targetpart=p3
943				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
944			else
945				# Bootpart unused
946				bootpart=p2 swappart=p2 targetpart=p2
947				[ ${swapsize:-0} -gt 0 ] && targetpart=p3
948			fi
949		fi
950
951		#
952		# Prepare boot pool if enabled (e.g., for geli(8))
953		#
954		if [ "$ZFSBOOT_BOOT_POOL" ]; then
955			f_eval_catch $funcname gpart \
956			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
957			             "$align_big" boot$index freebsd-zfs \
958			             ${bootsize}b $disk || return $FAILURE
959			# Pedantically nuke any old labels
960			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
961			                /dev/$disk$bootpart
962			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
963				# Pedantically detach targetpart for later
964				f_eval_catch -d $funcname geli \
965				                "$GELI_DETACH_F" \
966				                /dev/$disk$targetpart
967			fi
968		fi
969
970		#
971		# 3. Add freebsd-swap partition labeled `swap#'
972		#
973		if [ ${swapsize:-0} -gt 0 ]; then
974			f_eval_catch $funcname gpart \
975			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
976			             "$align_big" swap$index freebsd-swap \
977			             ${swapsize}b $disk || return $FAILURE
978			# Pedantically nuke any old labels on the swap
979			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
980			                /dev/$disk$swappart
981		fi
982
983		#
984		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
985		#
986		if [ "$ZFSBOOT_POOL_SIZE" ]; then
987			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
988					"$align_big" zfs$index freebsd-zfs $ZFSBOOT_POOL_SIZE $disk ||
989					return $FAILURE
990		else
991			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL" \
992					"$align_big" zfs$index freebsd-zfs $disk ||
993					return $FAILURE
994		fi
995		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
996		                /dev/$disk$targetpart
997		;;
998
999	esac # $ZFSBOOT_PARTITION_SCHEME
1000
1001	# Update fstab(5)
1002	local swapsize
1003	f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1004	if [ "$isswapmirror" ]; then
1005		# This is not the first disk in the mirror, do nothing
1006	elif [ ${swapsize:-0} -eq 0 ]; then
1007		# If swap is 0 sized, don't add it to fstab
1008	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
1009		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1010		             /dev/mirror/swap.eli none swap sw 0 0 \
1011		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1012		isswapmirror=1
1013	elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1014		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1015		             /dev/mirror/swap none swap sw 0 0 \
1016		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1017		isswapmirror=1
1018	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1019		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1020		             /dev/$disk${swappart}.eli none swap sw 0 0 \
1021		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1022	else
1023		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1024		             /dev/$disk$swappart none swap sw 0 0 \
1025		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1026	fi
1027
1028	return $SUCCESS
1029}
1030
1031# zfs_create_boot $poolname $vdev_type $disks ...
1032#
1033# Creates boot pool and dataset layout. Returns error if something goes wrong.
1034# Errors are printed to stderr for collection and display.
1035#
1036zfs_create_boot()
1037{
1038	local funcname=zfs_create_boot
1039	local zroot_name="$1"
1040	local zroot_vdevtype="$2"
1041	local zroot_vdevs= # Calculated below
1042	local swap_devs= # Calculated below
1043	local boot_vdevs= # Used for geli(8) layouts
1044	shift 2 # poolname vdev_type
1045	local disks="$*" disk
1046	local isswapmirror
1047	local bootpart targetpart swappart # Set by zfs_create_diskpart() below
1048	local create_options
1049
1050	#
1051	# Pedantic checks; should never be seen
1052	#
1053	if [ ! "$zroot_name" ]; then
1054		f_dprintf "$funcname: NULL poolname"
1055		msg_error="$msg_error: $funcname" \
1056			f_show_err "$msg_null_poolname"
1057		return $FAILURE
1058	fi
1059	if [ $# -lt 1 ]; then
1060		f_dprintf "$funcname: missing disk arguments"
1061		msg_error="$msg_error: $funcname" \
1062			f_show_err "$msg_missing_disk_arguments"
1063		return $FAILURE
1064	fi
1065	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
1066	          "$zroot_name" "$zroot_vdevtype"
1067
1068	#
1069	# Initialize fstab(5)
1070	#
1071	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1072	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1073	             return $FAILURE
1074	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1075	             "# Device" Mountpoint FStype Options Dump "Pass#" \
1076	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1077
1078	#
1079	# Expand SI units in desired sizes
1080	#
1081	f_dprintf "$funcname: Expanding supplied size values..."
1082	local swapsize bootsize poolsize
1083	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1084		f_dprintf "$funcname: Invalid swap size \`%s'" \
1085		          "$ZFSBOOT_SWAP_SIZE"
1086		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1087		return $FAILURE
1088	fi
1089	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
1090		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
1091		          "$ZFSBOOT_BOOT_POOL_SIZE"
1092		f_show_err "$msg_invalid_boot_pool_size" \
1093		           "$ZFSBOOT_BOOT_POOL_SIZE"
1094		return $FAILURE
1095	fi
1096	if [ "$ZFSBOOT_POOL_SIZE" ]; then
1097		if ! f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize; then
1098			f_dprintf "$funcname: Invalid pool size \`%s'" \
1099				  "$ZFSBOOT_POOL_SIZE"
1100			f_show_err "$msg_invalid_pool_size" \
1101				   "$ZFSBOOT_POOL_SIZE"
1102		fi
1103		f_dprintf "$funcname: ZFSBOOT_POOL_SIZE=[%s] poolsize=[%s]" \
1104			  "$ZFSBOOT_POOL_SIZE" "$poolsize"
1105	fi
1106	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1107	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
1108	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
1109	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1110
1111	#
1112	# Destroy the pool in-case this is our second time 'round (case of
1113	# failure and installer presented ``Retry'' option to come back).
1114	#
1115	# NB: If we don't destroy the pool, later gpart(8) destroy commands
1116	# that try to clear existing partitions (see zfs_create_diskpart())
1117	# will fail with a `Device Busy' error, leading to `GEOM exists'.
1118	#
1119	f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1120
1121	#
1122	# Prepare the disks and build pool device list(s)
1123	#
1124	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1125
1126	# Force 4K sectors using vfs.zfs.vdev.min_auto_ashift=12
1127	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1128		f_dprintf "$funcname: With 4K sectors..."
1129		f_eval_catch $funcname sysctl "$SYSCTL_ZFS_MIN_ASHIFT_12" \
1130		    || return $FAILURE
1131	fi
1132	local n=0
1133	for disk in $disks; do
1134		zfs_create_diskpart $disk $n || return $FAILURE
1135		# Now $bootpart, $targetpart, and $swappart are set (suffix
1136		# for $disk)
1137		if [ "$ZFSBOOT_BOOT_POOL" ]; then
1138			boot_vdevs="$boot_vdevs $disk$bootpart"
1139		fi
1140		zroot_vdevs="$zroot_vdevs $disk$targetpart"
1141		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1142			zroot_vdevs="$zroot_vdevs.eli"
1143		fi
1144
1145		n=$(( $n + 1 ))
1146	done # disks
1147
1148	#
1149	# If we need/want a boot pool, create it
1150	#
1151	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1152		local bootpool_vdevtype= # Calculated below
1153		local bootpool_options= # Calculated below
1154		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
1155		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
1156		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
1157
1158		f_dprintf "$funcname: Setting up boot pool..."
1159		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
1160			f_dprintf "$funcname: For encrypted root disk..."
1161
1162		# Create parent directory for boot pool
1163		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1164		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1165		             $BSDINSTALL_CHROOT || return $FAILURE
1166
1167		# Create mirror across the boot partition on all disks
1168		local nvdevs
1169		f_count nvdevs $boot_vdevs
1170		[ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1171
1172		create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
1173		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
1174		bootpool_options="$bootpool_options $create_options"
1175		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1176		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1177		             "$bootpool_options" "$bootpool_name" \
1178		             "$bootpool_vdevtype" "$boot_vdevs" ||
1179		             return $FAILURE
1180
1181		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1182		             return $FAILURE
1183
1184		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1185			# Generate an encryption key using random(4)
1186			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1187			             /dev/random "$bootpool/$zroot_key" \
1188			             "bs=4096 count=1" || return $FAILURE
1189			f_eval_catch $funcname chmod "$CHMOD_MODE" \
1190			             go-wrx "$bootpool/$zroot_key" ||
1191			             return $FAILURE
1192		fi
1193
1194	fi
1195
1196	#
1197	# Create the geli(8) GEOMS
1198	#
1199	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1200		#
1201		# Load the AES-NI kernel module to accelerate encryption
1202		#
1203		f_eval_catch -d $funcname kldload "$KLDLOAD" "aesni"
1204		# Prompt user for password (twice)
1205		if ! msg_enter_new_password="$msg_geli_password" \
1206			f_dialog_input_password
1207		then
1208			f_dprintf "$funcname: User cancelled"
1209			f_show_err "$msg_user_cancelled"
1210			return $FAILURE
1211		fi
1212
1213		# Initialize geli(8) on each of the target partitions
1214		for disk in $disks; do
1215			f_dialog_info "$msg_geli_setup" \
1216				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1217			if [ "$ZFSBOOT_BOOT_POOL" ]; then
1218				if ! echo "$pw_password" | f_eval_catch \
1219					$funcname geli "$GELI_PASSWORD_INIT" \
1220					"$bootpool/boot/$disk$targetpart.eli" \
1221					AES-XTS "$bootpool/$zroot_key" \
1222					$disk$targetpart
1223				then
1224					f_interactive || f_die
1225					unset pw_password # Sensitive info
1226					return $FAILURE
1227				fi
1228				if ! echo "$pw_password" | f_eval_catch \
1229					$funcname geli "$GELI_ATTACH" \
1230					"$bootpool/$zroot_key" $disk$targetpart
1231				then
1232					f_interactive || f_die
1233					unset pw_password # Sensitive info
1234					return $FAILURE
1235				fi
1236			else
1237				# With no bootpool, there is no place to store
1238				# the key files, use only a password
1239				if ! echo "$pw_password" | f_eval_catch \
1240					$funcname geli \
1241					"$GELI_PASSWORD_GELIBOOT_INIT" \
1242					AES-XTS $disk$targetpart
1243				then
1244					f_interactive || f_die
1245					unset pw_password # Sensitive info
1246					return $FAILURE
1247				fi
1248				if ! echo "$pw_password" | f_eval_catch \
1249					$funcname geli "$GELI_ATTACH_NOKEY" \
1250					$disk$targetpart
1251				then
1252					f_interactive || f_die
1253					unset pw_password # Sensitive info
1254					return $FAILURE
1255				fi
1256			fi
1257		done
1258		unset pw_password # Sensitive info
1259	fi
1260
1261	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1262		# Clean up
1263		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
1264			return $FAILURE
1265		# tmpfs
1266		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1267	fi
1268
1269	#
1270	# Create the gmirror(8) GEOMS for swap
1271	#
1272	if [ ${swapsize:-0} -gt 0 -a "$ZFSBOOT_SWAP_MIRROR" ]; then
1273		for disk in $disks; do
1274			swap_devs="$swap_devs $disk$swappart"
1275		done
1276		f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
1277			"$swap_devs" || return $FAILURE
1278	fi
1279
1280	#
1281	# Create the ZFS root pool with desired type and disk devices
1282	#
1283	f_dprintf "$funcname: Creating root pool..."
1284	create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1285	if [ "$zroot_vdevtype" == "raid10" ]; then
1286		raid10_vdevs=""
1287		for vdev in $zroot_vdevs; do
1288			f_count nvdev $raid10_vdevs
1289			if [ $(( $nvdev % 3 )) -eq 0 ]; then
1290				raid10_vdevs="$raid10_vdevs mirror"
1291			fi
1292			raid10_vdevs="$raid10_vdevs $vdev"
1293		done
1294		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1295			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1296			"$zroot_name" "" "$raid10_vdevs" || return $FAILURE
1297	else
1298		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1299			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1300			"$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
1301			return $FAILURE
1302	fi
1303
1304	#
1305	# Create ZFS dataset layout within the new root pool
1306	#
1307	f_dprintf "$funcname: Creating ZFS datasets..."
1308	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1309		# Skip blank lines and comments
1310		case "$dataset" in "#"*|"") continue; esac
1311		# Remove potential inline comments in options
1312		options="${options%%#*}"
1313		# Replace tabs with spaces
1314		f_replaceall "$options" "	" " " options
1315		# Reduce contiguous runs of space to one single space
1316		oldoptions=
1317		while [ "$oldoptions" != "$options" ]; do
1318			oldoptions="$options"
1319			f_replaceall "$options" "  " " " options
1320		done
1321		# Replace both commas and spaces with ` -o '
1322		f_replaceall "$options" "[ ,]" " -o " options
1323		# Create the dataset with desired options
1324		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
1325		             "${options:+-o $options}" "$zroot_name$dataset" ||
1326		             return $FAILURE
1327	done
1328
1329	#
1330	# Set a mountpoint for the root of the pool so newly created datasets
1331	# have a mountpoint to inherit
1332	#
1333	f_dprintf "$funcname: Setting mountpoint for root of the pool..."
1334	f_eval_catch $funcname zfs "$ZFS_SET" \
1335		"mountpoint=/$zroot_name" "$zroot_name" || return $FAILURE
1336
1337	# Touch up permissions on the tmp directories
1338	f_dprintf "$funcname: Modifying directory permissions..."
1339	local dir
1340	for dir in /tmp /var/tmp; do
1341		f_eval_catch $funcname mkdir "$MKDIR_P" \
1342		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1343		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1344		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1345	done
1346
1347	# Set bootfs property
1348	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1349	f_dprintf "$funcname: Setting bootfs property..."
1350	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1351		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
1352		return $FAILURE
1353
1354	# Remount bootpool and create symlink(s)
1355	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1356		f_eval_catch $funcname zfs "$ZFS_MOUNT" "$bootpool_name" ||
1357			return $FAILURE
1358		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1359		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1360		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1361	fi
1362
1363	# zpool.cache is required to mount more than one pool at boot time
1364	f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1365	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1366	             return $FAILURE
1367	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1368	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1369	             "$zroot_name" || return $FAILURE
1370
1371	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1372		f_eval_catch $funcname printf "$PRINTF_CONF" \
1373			vfs.root.mountfrom \
1374			"\"zfs:$zroot_name/$zroot_bootfs\"" \
1375			$BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
1376	fi
1377	#
1378	# Set canmount=noauto so that the default Boot Environment (BE) does
1379	# not get mounted if a different BE is selected from the beastie menu
1380	#
1381	f_dprintf "$funcname: Set canmount=noauto for any datasets under the BE..."
1382	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1383		# Skip blank lines and comments
1384		case "$dataset" in "#"*|"") continue; esac
1385		options="${options%%#*}"
1386		#
1387		case "$dataset" in "/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"*)
1388			f_eval_catch $funcname zfs "$ZFS_SET" "canmount=noauto" \
1389				"$zroot_name$dataset" || return $FAILURE ;;
1390		*) continue ;;
1391		esac
1392	done
1393
1394	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1395	# NOTE: We later concatenate these into their destination
1396	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1397	          "$funcname"
1398	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1399	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1400	f_eval_catch $funcname echo "$ECHO_APPEND" \
1401	             'kern.geom.label.disk_ident.enable=\"0\"' \
1402	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1403	f_eval_catch $funcname echo "$ECHO_APPEND" \
1404	             'kern.geom.label.gptid.enable=\"0\"' \
1405	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1406
1407	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1408		f_eval_catch $funcname echo "$ECHO_APPEND" \
1409	             'vfs.zfs.vdev.min_auto_ashift=12' \
1410	             $BSDINSTALL_TMPETC/sysctl.conf.zfs || return $FAILURE
1411	fi
1412
1413	if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1414		f_eval_catch $funcname echo "$ECHO_APPEND" \
1415		             'geom_mirror_load=\"YES\"' \
1416		             $BSDINSTALL_TMPBOOT/loader.conf.gmirror ||
1417		             return $FAILURE
1418	fi
1419
1420	# We're all done unless we should go on to do encryption
1421	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
1422
1423	#
1424	# Configure geli(8)-based encryption
1425	#
1426	f_dprintf "$funcname: Configuring disk encryption..."
1427	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1428		$BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1429	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1430		$BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1431
1432	# We're all done unless we should go on for boot pool
1433	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1434
1435	for disk in $disks; do
1436		f_eval_catch $funcname printf "$PRINTF_CONF" \
1437			geli_%s_keyfile0_load "$disk$targetpart YES" \
1438			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1439			return $FAILURE
1440		f_eval_catch $funcname printf "$PRINTF_CONF" \
1441			geli_%s_keyfile0_type \
1442			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1443			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1444			return $FAILURE
1445		f_eval_catch $funcname printf "$PRINTF_CONF" \
1446			geli_%s_keyfile0_name \
1447			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1448			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1449			return $FAILURE
1450	done
1451
1452	# Set cachefile for boot pool so it auto-imports at system start
1453	f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1454	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1455	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1456	             "$bootpool_name" || return $FAILURE
1457
1458	# Some additional geli(8) requirements for loader.conf(5)
1459	for option in \
1460		'zpool_cache_load=\"YES\"' \
1461		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1462		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1463		'geom_eli_passphrase_prompt=\"YES\"' \
1464	; do
1465		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1466		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1467		             return $FAILURE
1468	done
1469	return $SUCCESS
1470}
1471
1472# dialog_menu_diskinfo
1473#
1474# Prompt the user to select a disk and then provide detailed info on it.
1475#
1476dialog_menu_diskinfo()
1477{
1478	local device disk
1479
1480	#
1481	# Break from loop when user cancels disk selection
1482	#
1483	while :; do
1484		device=$( msg_cancel="$msg_back" f_device_menu \
1485			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1486			$DEVICE_TYPE_DISK 2>&1 ) || break
1487		$device get name disk
1488
1489		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1490		f_show_msg "$msg_detailed_disk_info" \
1491			"$disk" "$( gpart show $disk 2> /dev/null )" \
1492			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1493			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1494	done
1495
1496	return $SUCCESS
1497}
1498
1499dialog_pool_name()
1500{
1501	local prompt="$* is already taken, please enter a name for the ZFS pool \
1502	(Or confirm using the same name by just pressing enter)"
1503	local hline=
1504	local value="$*"
1505	local height width
1506
1507	f_dialog_inputbox_size height width \
1508		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
1509
1510	$DIALOG \
1511		--title "$DIALOG_TITLE"         \
1512		--backtitle "$DIALOG_BACKTITLE" \
1513		--hline "$hline"                \
1514		--ok-label "Ok"                 \
1515		--no-cancel                     \
1516		--inputbox "$prompt"            \
1517		$height $width "$value"         \
1518		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1519}
1520
1521############################################################ MAIN
1522
1523#
1524# Initialize
1525#
1526f_dialog_title "$msg_zfs_configuration"
1527f_dialog_backtitle "$msg_freebsd_installer"
1528
1529# User may have specifically requested ZFS-related operations be interactive
1530! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1531
1532#
1533# Debugging
1534#
1535f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1536f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1537f_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1538
1539pools=$(zpool import | awk '$1 == "pool:" { print $2}')
1540for pool in ${pools}; do
1541	f_dprintf "Checking ${pool} against ${ZFSBOOT_POOL_NAME}"
1542	if [ "${pool}" = "${ZFSBOOT_POOL_NAME}" ]; then
1543		f_dprintf "Pool ${pool} already taken"
1544		ZFSBOOT_POOL_NAME=$(dialog_pool_name "${ZFSBOOT_POOL_NAME}")
1545		break
1546	fi
1547done
1548
1549#
1550# Determine default boot type
1551#
1552case $(uname -m) in
1553arm64|riscv)
1554	# We support only UEFI boot for arm64 and riscv.
1555	: ${ZFSBOOT_BOOT_TYPE:=UEFI}
1556	: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1557	;;
1558*)
1559	# If the system was booted with UEFI, set the default boot type to UEFI
1560	bootmethod=$( sysctl -n machdep.bootmethod )
1561	f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
1562	if [ "$bootmethod" = "UEFI" ]; then
1563		: ${ZFSBOOT_BOOT_TYPE:=BIOS+UEFI}
1564		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1565	else
1566		: ${ZFSBOOT_BOOT_TYPE:=BIOS}
1567		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1568	fi
1569	;;
1570esac
1571
1572#
1573# Loop over the main menu until we've accomplished what we came here to do
1574#
1575while :; do
1576	if ! f_interactive; then
1577		retval=$DIALOG_OK
1578		mtag=">>> $msg_install"
1579	else
1580		dialog_menu_main
1581		retval=$?
1582		f_dialog_menutag_fetch mtag
1583	fi
1584
1585	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1586	[ $retval -eq $DIALOG_OK ] || f_die
1587
1588	case "$mtag" in
1589	">>> $msg_install")
1590		#
1591		# First, validate the user's selections
1592		#
1593
1594		# Make sure they gave us a name for the pool
1595		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1596			f_dprintf "Pool name cannot be empty."
1597			f_show_err "$msg_pool_name_cannot_be_empty"
1598			continue
1599		fi
1600
1601		# Validate vdev type against number of disks selected/scripted
1602		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1603		# NB: dialog_menu_layout supports running non-interactively
1604		dialog_menu_layout || continue
1605
1606		# Make sure each disk will have room for ZFS
1607		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
1608		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize &&
1609		   f_expand_number "1g" zpoolmin
1610		then
1611			minsize=$swapsize teeny_disks=
1612			if [ "$ZFSBOOT_POOL_SIZE" ]; then
1613				f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize
1614				minsize=$(( $minsize + $poolsize ))
1615			fi
1616			[ "$ZFSBOOT_BOOT_POOL" ] &&
1617				minsize=$(( $minsize + $bootsize ))
1618			for disk in $ZFSBOOT_DISKS; do
1619				debug= f_device_find -1 \
1620					$disk $DEVICE_TYPE_DISK device
1621				$device get capacity disksize || continue
1622				[ ${disksize:-0} -ge 0 ] || disksize=0
1623				[ $disksize -lt $minsize ] &&
1624					teeny_disks="$teeny_disks $disk"
1625			done
1626			if [ "$teeny_disks" ]; then
1627				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1628				          "$ZFSBOOT_SWAP_SIZE" \
1629				          "$ZFSBOOT_BOOT_POOL_SIZE" \
1630				          "minsize=[$minsize]"
1631				f_dprintf "These disks are too small: %s" \
1632				          "$teeny_disks"
1633				f_show_err "$msg_these_disks_are_too_small" \
1634				           "$ZFSBOOT_SWAP_SIZE" \
1635				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1636				           "$teeny_disks"
1637				continue
1638			fi
1639		fi
1640
1641		#
1642		# Last Chance!
1643		#
1644		if f_interactive; then
1645			dialog_last_chance $ZFSBOOT_DISKS || continue
1646		fi
1647
1648		#
1649		# Let's do this
1650		#
1651
1652		vdev_type="$ZFSBOOT_VDEV_TYPE"
1653
1654		# Blank the vdev type for the default layout
1655		[ "$vdev_type" = "stripe" ] && vdev_type=
1656
1657		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1658		                "$vdev_type" $ZFSBOOT_DISKS || continue
1659
1660		# To be reused by bootconfig
1661		echo "$ZFSBOOT_DISKS" > ${TMPDIR:-"/tmp"}/bsdinstall-zfsboot
1662
1663		break # to success
1664		;;
1665	?" $msg_pool_type_disks")
1666		ZFSBOOT_CONFIRM_LAYOUT=1
1667		dialog_menu_layout
1668		# User has poked settings, disable later confirmation
1669		ZFSBOOT_CONFIRM_LAYOUT=
1670		;;
1671	"- $msg_rescan_devices") f_device_rescan ;;
1672	"- $msg_disk_info") dialog_menu_diskinfo ;;
1673	?" $msg_pool_name")
1674		# Prompt the user to input/change the name for the new pool
1675		f_dialog_input input \
1676			"$msg_please_enter_a_name_for_your_pool" \
1677			"$ZFSBOOT_POOL_NAME" &&
1678			ZFSBOOT_POOL_NAME="$input"
1679		;;
1680	?" $msg_force_4k_sectors")
1681		# Toggle the variable referenced both by the menu and later
1682		if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1683			ZFSBOOT_FORCE_4K_SECTORS=
1684		else
1685			ZFSBOOT_FORCE_4K_SECTORS=1
1686		fi
1687		;;
1688	?" $msg_encrypt_disks")
1689		# Toggle the variable referenced both by the menu and later
1690		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1691			ZFSBOOT_GELI_ENCRYPTION=
1692		else
1693			ZFSBOOT_FORCE_4K_SECTORS=1
1694			ZFSBOOT_GELI_ENCRYPTION=1
1695		fi
1696		;;
1697	?" $msg_partition_scheme")
1698		# Toggle between partition schemes
1699		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1700		     "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]
1701		then
1702			ZFSBOOT_PARTITION_SCHEME="GPT"
1703			ZFSBOOT_BOOT_TYPE="UEFI"
1704		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1705		       "$ZFSBOOT_BOOT_TYPE" = "UEFI" ]
1706		then
1707			ZFSBOOT_PARTITION_SCHEME="GPT"
1708			ZFSBOOT_BOOT_TYPE="BIOS+UEFI"
1709		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" ]; then
1710			ZFSBOOT_PARTITION_SCHEME="GPT + Active"
1711			ZFSBOOT_BOOT_TYPE="BIOS"
1712		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
1713			ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
1714			ZFSBOOT_BOOT_TYPE="BIOS"
1715		else
1716			ZFSBOOT_PARTITION_SCHEME="GPT"
1717			ZFSBOOT_BOOT_TYPE="BIOS"
1718		fi
1719		;;
1720	?" $msg_swap_size")
1721		# Prompt the user to input/change the swap size for each disk
1722		while :; do
1723		    f_dialog_input input \
1724			    "$msg_please_enter_amount_of_swap_space" \
1725			    "$ZFSBOOT_SWAP_SIZE" &&
1726			    ZFSBOOT_SWAP_SIZE="${input:-0}"
1727		    if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1728		    then
1729			if [ $swapsize -ne 0 -a $swapsize -lt 104857600 ]; then
1730			    f_show_err "$msg_swap_toosmall" \
1731				       "$ZFSBOOT_SWAP_SIZE"
1732			    continue
1733			else
1734			    break
1735			fi
1736		    else
1737			f_show_err "$msg_swap_invalid" \
1738				   "$ZFSBOOT_SWAP_SIZE"
1739			continue
1740		    fi
1741		done
1742		;;
1743	?" $msg_swap_mirror")
1744		# Toggle the variable referenced both by the menu and later
1745		if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1746			ZFSBOOT_SWAP_MIRROR=
1747		else
1748			ZFSBOOT_SWAP_MIRROR=1
1749		fi
1750		;;
1751	?" $msg_swap_encrypt")
1752		# Toggle the variable referenced both by the menu and later
1753		if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1754			ZFSBOOT_SWAP_ENCRYPTION=
1755		else
1756			ZFSBOOT_SWAP_ENCRYPTION=1
1757		fi
1758		;;
1759	?" $msg_zfs_options_name")
1760		# Prompt the user to input/change the pool options
1761		f_dialog_input input \
1762			"$msg_please_enter_options_for_your_pool" \
1763			"$ZFSBOOT_POOL_CREATE_OPTIONS" &&
1764			ZFSBOOT_POOL_CREATE_OPTIONS="$input"
1765		;;
1766	esac
1767done
1768
1769exit $SUCCESS
1770
1771################################################################################
1772# END
1773################################################################################
1774