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