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