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