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