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