zfsboot revision 267654
1184610Salfred#!/bin/sh
2184610Salfred#-
3184610Salfred# Copyright (c) 2013 Allan Jude
4184610Salfred# Copyright (c) 2013 Devin Teske
5184610Salfred# All rights reserved.
6184610Salfred#
7184610Salfred# Redistribution and use in source and binary forms, with or without
8184610Salfred# modification, are permitted provided that the following conditions
9184610Salfred# are met:
10184610Salfred# 1. Redistributions of source code must retain the above copyright
11184610Salfred#    notice, this list of conditions and the following disclaimer.
12184610Salfred# 2. Redistributions in binary form must reproduce the above copyright
13184610Salfred#    notice, this list of conditions and the following disclaimer in the
14184610Salfred#    documentation and/or other materials provided with the distribution.
15184610Salfred#
16184610Salfred# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17184610Salfred# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18184610Salfred# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19184610Salfred# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20184610Salfred# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21184610Salfred# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22184610Salfred# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23184610Salfred# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24184610Salfred# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25184610Salfred# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26184610Salfred# SUCH DAMAGE.
27184610Salfred#
28184610Salfred# $FreeBSD: releng/9.3/usr.sbin/bsdinstall/scripts/zfsboot 266295 2014-05-17 04:00:18Z dteske $
29184610Salfred#
30184610Salfred############################################################ INCLUDES
31184610Salfred
32184610SalfredBSDCFG_SHARE="/usr/share/bsdconfig"
33184610Salfred. $BSDCFG_SHARE/common.subr || exit 1
34184610Salfredf_dprintf "%s: loading includes..." "$0"
35184610Salfredf_include $BSDCFG_SHARE/device.subr
36184610Salfredf_include $BSDCFG_SHARE/dialog.subr
37184610Salfredf_include $BSDCFG_SHARE/password/password.subr
38184610Salfredf_include $BSDCFG_SHARE/variable.subr
39184610Salfred
40184610Salfred############################################################ CONFIGURATION
41184610Salfred
42184610Salfred#
43194677Sthompsa# Default name of the boot-pool
44194677Sthompsa#
45194677Sthompsa: ${ZFSBOOT_POOL_NAME:=zroot}
46194677Sthompsa
47194677Sthompsa#
48194677Sthompsa# Default name for the boot environment parent dataset
49194677Sthompsa#
50194677Sthompsa: ${ZFSBOOT_BEROOT_NAME:=ROOT}
51194677Sthompsa
52194677Sthompsa#
53194677Sthompsa# Default name for the primany boot environment
54194677Sthompsa#
55194677Sthompsa: ${ZFSBOOT_BOOTFS_NAME:=default}
56194677Sthompsa
57194677Sthompsa#
58194677Sthompsa# Default Virtual Device (vdev) type to create
59194677Sthompsa#
60194677Sthompsa: ${ZFSBOOT_VDEV_TYPE:=stripe}
61194677Sthompsa
62194677Sthompsa#
63188942Sthompsa# Should we use gnop(8) to configure a transparent mapping to 4K sectors?
64194677Sthompsa#
65194677Sthompsa: ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=1}
66188942Sthompsa
67184610Salfred#
68194228Sthompsa# Should we use geli(8) to encrypt the drives?
69184610Salfred# NB: Automatically enables ZFSBOOT_BOOT_POOL
70188942Sthompsa#
71188942Sthompsa: ${ZFSBOOT_GELI_ENCRYPTION=}
72188942Sthompsa
73188942Sthompsa#
74188942Sthompsa# Default path to the geli(8) keyfile used in drive encryption
75184610Salfred#
76184610Salfred: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
77189547Sthompsa
78184610Salfred#
79189547Sthompsa# Create a separate boot pool?
80189547Sthompsa# NB: Automatically set when using geli(8) or MBR
81195967Salfred#
82195967Salfred: ${ZFSBOOT_BOOT_POOL=}
83195967Salfred
84195967Salfred#
85195967Salfred# Default name for boot pool when enabled (e.g., geli(8) or MBR)
86195967Salfred#
87195967Salfred: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
88184610Salfred
89184610Salfred#
90184610Salfred# Default size for boot pool when enabled (e.g., geli(8) or MBR)
91184610Salfred#
92189547Sthompsa: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
93195967Salfred
94189547Sthompsa#
95189547Sthompsa# Default disks to use (always empty unless being scripted)
96189718Sthompsa#
97189718Sthompsa: ${ZFSBOOT_DISKS:=}
98189718Sthompsa
99189718Sthompsa#
100189547Sthompsa# Default partitioning scheme to use on disks
101189547Sthompsa#
102189547Sthompsa: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
103189547Sthompsa
104189547Sthompsa#
105189547Sthompsa# How much swap to put on each block device in the boot zpool
106189547Sthompsa# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
107184610Salfred#
108184610Salfred: ${ZFSBOOT_SWAP_SIZE:=2g}
109184610Salfred
110184610Salfred#
111184610Salfred# Default ZFS datasets for root zpool
112184610Salfred#
113184610Salfred# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
114184610Salfred# NOTE: Anything after pound/hash character [#] is ignored as a comment.
115184610Salfred#
116189547Sthompsaf_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
117189547Sthompsa	# DATASET	OPTIONS (comma or space separated; or both)
118184610Salfred
119184610Salfred	# Boot Environment [BE] root and default boot dataset
120184610Salfred	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
121184610Salfred	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
122184610Salfred
123184610Salfred	# Compress /tmp, allow exec but not setuid
124184610Salfred	/tmp		mountpoint=/tmp,compression=lz4,exec=on,setuid=off
125184610Salfred
126184610Salfred	# Don't mount /usr so that 'base' files go to the BEROOT
127184610Salfred	/usr		mountpoint=/usr,canmount=off
128184610Salfred
129184610Salfred	# Home directories separated so they are common to all BEs
130195967Salfred	/usr/home	# NB: /home is a symlink to /usr/home
131195967Salfred
132195967Salfred	# Ports tree
133195967Salfred	/usr/ports		compression=lz4,setuid=off
134195967Salfred
135195967Salfred	# Source tree (compressed)
136195967Salfred	/usr/src	compression=lz4,exec=off,setuid=off
137195967Salfred
138195967Salfred	# Create /var and friends
139195967Salfred	/var		mountpoint=/var
140195967Salfred	/var/crash	compression=lz4,exec=off,setuid=off
141195967Salfred	/var/log	compression=lz4,exec=off,setuid=off
142195967Salfred	/var/mail	compression=lz4,atime=on
143195967Salfred	/var/tmp	compression=lz4,exec=on,setuid=off
144195967Salfred" # END-QUOTE
145195967Salfred
146195967Salfred#
147195967Salfred# If interactive and the user has not explicitly chosen a vdev type or disks,
148195967Salfred# make the user confirm scripted/default choices when proceeding to install.
149195967Salfred#
150195967Salfred: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
151195967Salfred
152195967Salfred############################################################ GLOBALS
153195967Salfred
154195967Salfred#
155195967Salfred# Format of a line in printf(1) syntax to add to fstab(5)
156195967Salfred#
157195967SalfredFSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
158195967Salfred
159195967Salfred#
160195967Salfred# Command strings for various tasks
161195967Salfred#
162195967SalfredCHMOD_MODE='chmod %s "%s"'
163195967SalfredDD_WITH_OPTIONS='dd if="%s" of="%s" %s'
164195967SalfredECHO_APPEND='echo "%s" >> "%s"'
165195967SalfredGELI_ATTACH='geli attach -j - -k "%s" "%s"'
166195967SalfredGELI_DETACH_F='geli detach -f "%s"'
167195967SalfredGELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
168195967SalfredGNOP_CREATE='gnop create -S 4096 "%s"'
169195967SalfredGNOP_DESTROY='gnop destroy "%s"'
170195967SalfredGPART_ADD='gpart add -t %s "%s"'
171195967SalfredGPART_ADD_INDEX='gpart add -i %s -t %s "%s"'
172195967SalfredGPART_ADD_INDEX_WITH_SIZE='gpart add -i %s -t %s -s %s "%s"'
173195967SalfredGPART_ADD_LABEL='gpart add -l %s -t %s "%s"'
174195967SalfredGPART_ADD_LABEL_WITH_SIZE='gpart add -l %s -t %s -s %s "%s"'
175195967SalfredGPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
176195967SalfredGPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
177195967SalfredGPART_CREATE='gpart create -s %s "%s"'
178195967SalfredGPART_DESTROY_F='gpart destroy -F "%s"'
179195967SalfredGPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
180195967SalfredGRAID_DELETE='graid delete "%s"'
181195967SalfredLN_SF='ln -sf "%s" "%s"'
182184610SalfredMKDIR_P='mkdir -p "%s"'
183184610SalfredMOUNT_TYPE='mount -t %s "%s" "%s"'
184184610SalfredPRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
185184610SalfredPRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
186193074SthompsaSHELL_TRUNCATE=':> "%s"'
187184610SalfredUMOUNT='umount "%s"'
188184610SalfredZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
189184610SalfredZFS_SET='zfs set "%s" "%s"'
190189547SthompsaZFS_UNMOUNT='zfs unmount "%s"'
191189547SthompsaZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
192189547SthompsaZPOOL_DESTROY='zpool destroy "%s"'
193189547SthompsaZPOOL_EXPORT='zpool export "%s"'
194189547SthompsaZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
195189547SthompsaZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
196184610SalfredZPOOL_SET='zpool set %s "%s"'
197184610Salfred
198184610Salfred#
199184610Salfred# Strings that should be moved to an i18n file and loaded with f_include_lang()
200184610Salfred#
201184610Salfredhline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
202184610Salfredhline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
203184610Salfredhline_arrows_tab_enter="Press arrows, TAB or ENTER"
204184610Salfredmsg_an_unknown_error_occurred="An unknown error occurred"
205184610Salfredmsg_back="Back"
206184610Salfredmsg_cancel="Cancel"
207184610Salfredmsg_change_selection="Change Selection"
208184610Salfredmsg_configure_options="Configure Options:"
209189547Sthompsamsg_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"
210189547Sthompsamsg_disk_info="Disk Info"
211184610Salfredmsg_disk_info_help="Get detailed information on disk device(s)"
212184610Salfredmsg_encrypt_disks="Encrypt Disks?"
213184610Salfredmsg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
214184610Salfredmsg_error="Error"
215184610Salfredmsg_force_4k_sectors="Force 4K Sectors?"
216189547Sthompsamsg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
217189547Sthompsamsg_freebsd_installer="FreeBSD Installer"
218189547Sthompsamsg_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"
219189547Sthompsamsg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
220189547Sthompsamsg_install="Install"
221189547Sthompsamsg_install_desc="Proceed with Installation"
222189547Sthompsamsg_install_help="Create ZFS boot pool with displayed options"
223189547Sthompsamsg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
224189547Sthompsamsg_invalid_disk_argument="Invalid disk argument \`%s'"
225189547Sthompsamsg_invalid_index_argument="Invalid index argument \`%s'"
226189547Sthompsamsg_invalid_swap_size="Invalid swap size \`%s'"
227189547Sthompsamsg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
228189547Sthompsamsg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy\nthe current contents of the following disks:\n\n   %s"
229189547Sthompsamsg_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'
230189547Sthompsamsg_mirror_desc="Mirror - n-Way Mirroring"
231189547Sthompsamsg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
232189547Sthompsamsg_missing_disk_arguments="missing disk arguments"
233189547Sthompsamsg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
234189547Sthompsamsg_no="NO"
235189547Sthompsamsg_no_disks_present_to_configure="No disk(s) present to configure"
236189547Sthompsamsg_no_disks_selected="No disks selected."
237189547Sthompsamsg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
238189547Sthompsamsg_null_disk_argument="NULL disk argument"
239189547Sthompsamsg_null_index_argument="NULL index argument"
240189547Sthompsamsg_null_poolname="NULL poolname"
241189547Sthompsamsg_ok="OK"
242189547Sthompsamsg_partition_scheme="Partition Scheme"
243189547Sthompsamsg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
244189547Sthompsamsg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
245189547Sthompsamsg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
246184610Salfredmsg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
247184610Salfredmsg_pool_name="Pool Name"
248184610Salfredmsg_pool_name_cannot_be_empty="Pool name cannot be empty."
249184610Salfredmsg_pool_name_help="Customize the name of the zpool to be created (Required)"
250184610Salfredmsg_pool_type_disks="Pool Type/Disks:"
251189547Sthompsamsg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
252184610Salfredmsg_processing_selection="Processing selection..."
253184610Salfredmsg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
254189547Sthompsamsg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
255184610Salfredmsg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
256184610Salfredmsg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
257189547Sthompsamsg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
258189547Sthompsamsg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
259189547Sthompsamsg_rescan_devices="Rescan Devices"
260189547Sthompsamsg_rescan_devices_help="Scan for device changes"
261189547Sthompsamsg_select="Select"
262189547Sthompsamsg_select_a_disk_device="Select a disk device"
263189547Sthompsamsg_select_virtual_device_type="Select Virtual Device type:"
264189718Sthompsamsg_stripe_desc="Stripe - No Redundancy"
265189718Sthompsamsg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
266189718Sthompsamsg_swap_size="Swap Size"
267189718Sthompsamsg_swap_size_help="Customize how much swap space is allocated to each selected disk"
268189718Sthompsamsg_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."
269189718Sthompsamsg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
270189718Sthompsamsg_unsupported_partition_scheme="%s is an unsupported partition scheme"
271189718Sthompsamsg_user_cancelled="User Cancelled."
272189718Sthompsamsg_yes="YES"
273189718Sthompsamsg_zfs_configuration="ZFS Configuration"
274189718Sthompsa
275189718Sthompsa############################################################ FUNCTIONS
276189547Sthompsa
277189718Sthompsa# dialog_menu_main
278189718Sthompsa#
279189547Sthompsa# Display the dialog(1)-based application main menu.
280189547Sthompsa#
281189547Sthompsadialog_menu_main()
282189547Sthompsa{
283189547Sthompsa	local title="$DIALOG_TITLE"
284189547Sthompsa	local btitle="$DIALOG_BACKTITLE"
285189547Sthompsa	local prompt="$msg_configure_options"
286184610Salfred	local force4k="$msg_no"
287189547Sthompsa	local usegeli="$msg_no"
288189547Sthompsa	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
289189547Sthompsa	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
290184610Salfred	local disks n
291184610Salfred	f_count n $ZFSBOOT_DISKS
292184610Salfred	{ [ $n -eq 1 ] && disks=disk; } || disks=disks # grammar
293184610Salfred	local menu_list="
294189547Sthompsa		'>>> $msg_install'        '$msg_install_desc'
295189547Sthompsa		                          '$msg_install_help'
296189547Sthompsa		'T $msg_pool_type_disks'  '$ZFSBOOT_VDEV_TYPE: $n $disks'
297189547Sthompsa                                          '$msg_pool_type_disks_help'
298189547Sthompsa		'- $msg_rescan_devices'   '*'
299189547Sthompsa		                          '$msg_rescan_devices_help'
300189547Sthompsa		'- $msg_disk_info'        '*'
301189547Sthompsa		                          '$msg_disk_info_help'
302189547Sthompsa		'N $msg_pool_name'        '$ZFSBOOT_POOL_NAME'
303189547Sthompsa		                          '$msg_pool_name_help'
304189547Sthompsa		'4 $msg_force_4k_sectors' '$force4k'
305189547Sthompsa		                          '$msg_force_4k_sectors_help'
306189547Sthompsa		'E $msg_encrypt_disks'    '$usegeli'
307184610Salfred		                          '$msg_encrypt_disks_help'
308184610Salfred		'P $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
309189547Sthompsa		                          '$msg_partition_scheme_help'
310189547Sthompsa		'S $msg_swap_size'        '$ZFSBOOT_SWAP_SIZE'
311189547Sthompsa		                          '$msg_swap_size_help'
312184610Salfred	" # END-QUOTE
313184610Salfred	local defaultitem= # Calculated below
314184610Salfred	local hline="$hline_alnum_arrows_punc_tab_enter"
315184610Salfred
316184610Salfred	local height width rows
317184610Salfred	eval f_dialog_menu_with_help_size height width rows \
318184610Salfred		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
319184610Salfred
320184610Salfred	# Obtain default-item from previously stored selection
321184610Salfred	f_dialog_default_fetch defaultitem
322184610Salfred
323184610Salfred	local menu_choice
324189547Sthompsa	menu_choice=$( eval $DIALOG \
325184610Salfred		--title \"\$title\"              \
326184610Salfred		--backtitle \"\$btitle\"         \
327189547Sthompsa		--hline \"\$hline\"              \
328189547Sthompsa		--item-help                      \
329184610Salfred		--ok-label \"\$msg_select\"      \
330184610Salfred		--cancel-label \"\$msg_cancel\"  \
331189547Sthompsa		--default-item \"\$defaultitem\" \
332189547Sthompsa		--menu \"\$prompt\"              \
333184610Salfred		$height $width $rows             \
334189547Sthompsa		$menu_list                       \
335184610Salfred		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
336184610Salfred	)
337189547Sthompsa	local retval=$?
338189547Sthompsa	f_dialog_data_sanitize menu_choice
339189547Sthompsa	f_dialog_menutag_store "$menu_choice"
340189547Sthompsa
341189547Sthompsa	# Only update default-item on success
342184610Salfred	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
343184610Salfred
344189547Sthompsa	return $retval
345189547Sthompsa}
346189547Sthompsa
347184610Salfred# dialog_last_chance $disks ...
348184610Salfred#
349184610Salfred# Display a list of the disks that the user is about to destroy. The default
350184610Salfred# action is to return error status unless the user explicitly (non-default)
351184610Salfred# selects "Yes" from the noyes dialog.
352184610Salfred#
353184610Salfreddialog_last_chance()
354184610Salfred{
355184610Salfred	local title="$DIALOG_TITLE"
356184610Salfred	local btitle="$DIALOG_BACKTITLE"
357189718Sthompsa	local prompt # Calculated below
358189718Sthompsa	local hline="$hline_arrows_tab_enter"
359189718Sthompsa
360184610Salfred	local height=8 width=50 prefix="   "
361189547Sthompsa	local plen=${#prefix} list= line=
362189547Sthompsa	local max_width=$(( $width - 3 - $plen ))
363189547Sthompsa
364189547Sthompsa	local yes no defaultno extra_args format
365189547Sthompsa	if [ "$USE_XDIALOG" ]; then
366189547Sthompsa		yes=ok no=cancel defaultno=default-no
367189547Sthompsa		extra_args="--wrap --left"
368189547Sthompsa		format="$msg_last_chance_are_you_sure"
369189547Sthompsa	else
370189547Sthompsa		yes=yes no=no defaultno=defaultno
371189547Sthompsa		extra_args="--colors --cr-wrap"
372189547Sthompsa		format="$msg_last_chance_are_you_sure_color"
373184610Salfred	fi
374189547Sthompsa
375189547Sthompsa	local disk line_width
376184610Salfred	for disk in $*; do
377189547Sthompsa		if [ "$line" ]; then
378189547Sthompsa			line_width=${#line}
379184610Salfred		else
380184610Salfred			line_width=$plen
381184610Salfred		fi
382184610Salfred		line_width=$(( $line_width + 1 + ${#disk} ))
383184610Salfred		# Add newline before disk if it would exceed max_width
384184610Salfred		if [ $line_width -gt $max_width ]; then
385184610Salfred			list="$list$line\n"
386184610Salfred			line="$prefix"
387192055Sthompsa			height=$(( $height + 1 ))
388184610Salfred		fi
389184610Salfred		# Add the disk to the list
390184610Salfred		line="$line $disk"
391184610Salfred	done
392184610Salfred	# Append the left-overs
393184610Salfred	if [ "${line#$prefix}" ]; then
394184610Salfred		list="$list$line"
395184610Salfred		height=$(( $height + 1 ))
396189547Sthompsa	fi
397189547Sthompsa
398189547Sthompsa	# Add height for Xdialog(1)
399189547Sthompsa	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
400184610Salfred
401184610Salfred	prompt=$( printf "$format" "$list" )
402184610Salfred	f_dprintf "%s: Last Chance!" "$0"
403184610Salfred	$DIALOG \
404189547Sthompsa		--title "$title"        \
405184610Salfred		--backtitle "$btitle"   \
406184610Salfred		--hline "$hline"        \
407184610Salfred		--$defaultno            \
408184610Salfred		--$yes-label "$msg_yes" \
409184610Salfred		--$no-label "$msg_no"   \
410184610Salfred		$extra_args             \
411184610Salfred		--yesno "$prompt" $height $width
412184610Salfred}
413184610Salfred
414184610Salfred# dialog_menu_layout
415184610Salfred#
416184610Salfred# Configure Virtual Device type and disks to use for the ZFS boot pool. User
417184610Salfred# must select enough disks to satisfy the chosen vdev type.
418184610Salfred#
419184610Salfreddialog_menu_layout()
420184610Salfred{
421184610Salfred	local funcname=dialog_menu_layout
422184610Salfred	local title="$DIALOG_TITLE"
423184610Salfred	local btitle="$DIALOG_BACKTITLE"
424184610Salfred	local vdev_prompt="$msg_select_virtual_device_type"
425184610Salfred	local disk_prompt="$msg_please_select_one_or_more_disks"
426184610Salfred	local vdev_menu_list="
427184610Salfred		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
428184610Salfred		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
429184610Salfred		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
430184610Salfred		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
431184610Salfred		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
432189718Sthompsa	" # END-QUOTE
433189718Sthompsa	local disk_check_list= # Calculated below
434184610Salfred	local vdev_hline="$hline_arrows_tab_enter"
435184610Salfred	local disk_hline="$hline_arrows_space_tab_enter"
436195967Salfred
437184610Salfred	# Warn the user if vdev type is not valid
438184610Salfred	case "$ZFSBOOT_VDEV_TYPE" in
439189718Sthompsa	stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
440189718Sthompsa	*)
441184610Salfred		f_dprintf "%s: Invalid virtual device type \`%s'" \
442184610Salfred			  $funcname "$ZFSBOOT_VDEV_TYPE"
443189547Sthompsa		f_show_err "$msg_invalid_virtual_device_type" \
444189547Sthompsa			   "$ZFSBOOT_VDEV_TYPE"
445189547Sthompsa		f_interactive || return $FAILURE
446189718Sthompsa	esac
447189718Sthompsa
448189718Sthompsa	# Calculate size of vdev menu once only
449189718Sthompsa	local vheight vwidth vrows
450189547Sthompsa	eval f_dialog_menu_with_help_size vheight vwidth vrows \
451189547Sthompsa		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
452189547Sthompsa		$vdev_menu_list
453189547Sthompsa
454189547Sthompsa	# Get a list of probed disk devices
455184610Salfred	local disks=
456184610Salfred	debug= f_device_find "" $DEVICE_TYPE_DISK disks
457189547Sthompsa
458189547Sthompsa	# Prune out mounted md(4) devices that may be part of the boot process
459189547Sthompsa	local disk name new_list=
460189547Sthompsa	for disk in $disks; do
461189547Sthompsa		debug= $disk get name name
462189718Sthompsa		case "$name" in
463189718Sthompsa		md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
464189718Sthompsa		esac
465189718Sthompsa		new_list="$new_list $disk"
466189547Sthompsa	done
467189718Sthompsa	disks="${new_list# }"
468189718Sthompsa
469189547Sthompsa	# Debugging
470189547Sthompsa	if [ "$debug" ]; then
471189547Sthompsa		local disk_names=
472189547Sthompsa		for disk in $disks; do
473184610Salfred			debug= $disk get name name
474184610Salfred			disk_names="$disk_names $name"
475189547Sthompsa		done
476184610Salfred		f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
477184610Salfred	fi
478184610Salfred
479184610Salfred	if [ ! "$disks" ]; then
480184610Salfred		f_dprintf "No disk(s) present to configure"
481184610Salfred		f_show_err "$msg_no_disks_present_to_configure"
482189547Sthompsa		return $FAILURE
483189547Sthompsa	fi
484189547Sthompsa
485192055Sthompsa	# Lets sort the disks array to be more user friendly
486192055Sthompsa	f_device_sort_by name disks disks
487192055Sthompsa
488189547Sthompsa	#
489189547Sthompsa	# Operate in a loop so we can (if interactive) repeat if not enough
490189547Sthompsa	# disks are selected to satisfy the chosen vdev type or user wants to
491189547Sthompsa	# back-up to the previous menu.
492189547Sthompsa	#
493189547Sthompsa	local vardisk ndisks onoff selections vdev_choice breakout device
494189547Sthompsa	local valid_disks all_valid want_disks desc height width rows
495189547Sthompsa	while :; do
496189547Sthompsa		#
497189547Sthompsa		# Confirm the vdev type that was selected
498184610Salfred		#
499184610Salfred		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
500189547Sthompsa			vdev_choice=$( eval $DIALOG \
501189547Sthompsa				--title \"\$title\"              \
502189547Sthompsa				--backtitle \"\$btitle\"         \
503189547Sthompsa				--hline \"\$vdev_hline\"         \
504184610Salfred				--ok-label \"\$msg_ok\"          \
505189547Sthompsa				--cancel-label \"\$msg_cancel\"  \
506189547Sthompsa				--item-help                      \
507184610Salfred				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
508189547Sthompsa				--menu \"\$vdev_prompt\"         \
509189547Sthompsa				$vheight $vwidth $vrows          \
510189547Sthompsa				$vdev_menu_list                  \
511189547Sthompsa				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
512184610Salfred			) || return $?
513189547Sthompsa				# Exit if user pressed ESC or chose Cancel/No
514189547Sthompsa			f_dialog_data_sanitize vdev_choice
515189547Sthompsa
516189547Sthompsa			ZFSBOOT_VDEV_TYPE="$vdev_choice"
517189547Sthompsa			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
518189547Sthompsa			          "$ZFSBOOT_VDEV_TYPE"
519189547Sthompsa		fi
520189776Sthompsa
521189547Sthompsa		# Determine the number of disks needed for this vdev type
522189547Sthompsa		want_disks=0
523189547Sthompsa		case "$ZFSBOOT_VDEV_TYPE" in
524189547Sthompsa		stripe) want_disks=1 ;;
525189547Sthompsa		mirror) want_disks=2 ;;
526189547Sthompsa		raidz1) want_disks=3 ;;
527189547Sthompsa		raidz2) want_disks=4 ;;
528189547Sthompsa		raidz3) want_disks=5 ;;
529189547Sthompsa		esac
530189547Sthompsa
531184610Salfred		#
532184610Salfred		# Warn the user if any scripted disks are invalid
533184610Salfred		#
534184610Salfred		valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
535184610Salfred		for disk in $ZFSBOOT_DISKS; do
536184610Salfred			if debug= f_device_find -1 \
537184610Salfred				$disk $DEVICE_TYPE_DISK device
538184610Salfred			then
539184610Salfred				valid_disks="$valid_disks $disk"
540184610Salfred				continue
541184610Salfred			fi
542184610Salfred			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
543184610Salfred			all_valid=
544184610Salfred		done
545184610Salfred		if [ ! "$all_valid" ]; then
546184610Salfred			if [ "$ZFSBOOT_DISKS" ]; then
547184610Salfred				f_show_err \
548184610Salfred				    "$msg_missing_one_or_more_scripted_disks"
549184610Salfred			else
550184610Salfred				f_dprintf "No disks selected."
551184610Salfred				f_interactive ||
552184610Salfred					f_show_err "$msg_no_disks_selected"
553184610Salfred			fi
554189547Sthompsa			f_interactive || return $FAILURE
555184610Salfred		fi
556184610Salfred		ZFSBOOT_DISKS="${valid_disks# }"
557184610Salfred
558184610Salfred		#
559189547Sthompsa		# Short-circuit if we're running non-interactively
560184610Salfred		#
561184610Salfred		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
562184610Salfred			f_count ndisks $ZFSBOOT_DISKS
563189547Sthompsa			[ $ndisks -ge $want_disks ] && break # to success
564184610Salfred
565184610Salfred			# Not enough disks selected
566184610Salfred			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
567184610Salfred				  "$ZFSBOOT_VDEV_TYPE" \
568184610Salfred			          "Not enough disks selected." \
569184610Salfred				  $ndisks $want_disks
570193074Sthompsa			f_interactive || return $FAILURE
571184610Salfred			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
572184610Salfred				f_yesno "%s: $msg_not_enough_disks_selected" \
573184610Salfred				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
574188981Sthompsa				return $FAILURE
575188981Sthompsa		fi
576188981Sthompsa
577188981Sthompsa		#
578184610Salfred		# Confirm the disks that were selected
579188981Sthompsa		# Loop until the user cancels or selects enough disks
580188981Sthompsa		#
581188981Sthompsa		breakout=
582188981Sthompsa		while :; do
583188981Sthompsa			# Loop over list of available disks, resetting state
584184610Salfred			for disk in $disks; do
585188981Sthompsa				f_isset _${disk}_status && _${disk}_status=
586188981Sthompsa			done
587188981Sthompsa
588188981Sthompsa			# Loop over list of selected disks and create temporary
589188981Sthompsa			# locals to map statuses onto up-to-date list of disks
590184610Salfred			for disk in $ZFSBOOT_DISKS; do
591188981Sthompsa				debug= f_device_find -1 \
592188981Sthompsa					$disk $DEVICE_TYPE_DISK disk
593188981Sthompsa				f_isset _${disk}_status ||
594188981Sthompsa					local _${disk}_status
595188981Sthompsa				_${disk}_status=on
596188981Sthompsa			done
597188981Sthompsa
598188981Sthompsa			# Create the checklist menu of discovered disk devices
599184610Salfred			disk_check_list=
600188981Sthompsa			for disk in $disks; do
601184610Salfred				desc=
602188981Sthompsa				$disk get name name
603188981Sthompsa				$disk get desc desc
604188981Sthompsa				f_shell_escape "$desc" desc
605188981Sthompsa				f_getvar _${disk}_status:-off onoff
606188981Sthompsa				disk_check_list="$disk_check_list
607188981Sthompsa					$name '$desc' $onoff"
608188981Sthompsa			done
609188981Sthompsa
610188981Sthompsa			eval f_dialog_checklist_size height width rows \
611188981Sthompsa				\"\$title\" \"\$btitle\" \"\$prompt\" \
612188981Sthompsa				\"\$hline\" $disk_check_list
613188981Sthompsa
614188981Sthompsa			selections=$( eval $DIALOG \
615188981Sthompsa				--title \"\$DIALOG_TITLE\"         \
616188981Sthompsa				--backtitle \"\$DIALOG_BACKTITLE\" \
617184610Salfred				--separate-output                  \
618184610Salfred				--hline \"\$hline\"                \
619184610Salfred				--ok-label \"\$msg_ok\"            \
620184610Salfred				--cancel-label \"\$msg_back\"      \
621184610Salfred				--checklist \"\$prompt\"           \
622184610Salfred				$height $width $rows               \
623193074Sthompsa				$disk_check_list                   \
624190741Sthompsa				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
625184610Salfred			) || break
626184610Salfred				# Loop if user pressed ESC or chose Cancel/No
627184610Salfred			f_dialog_data_sanitize selections
628184610Salfred
629184610Salfred			ZFSBOOT_DISKS="$selections"
630184610Salfred			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
631190741Sthompsa			          "$ZFSBOOT_DISKS"
632190741Sthompsa
633184610Salfred			f_count ndisks $ZFSBOOT_DISKS
634184610Salfred			[ $ndisks -ge $want_disks ] &&
635184610Salfred				breakout=break && break
636184610Salfred
637188981Sthompsa			# Not enough disks selected
638188981Sthompsa			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
639184610Salfred				  "$ZFSBOOT_VDEV_TYPE" \
640184610Salfred			          "Not enough disks selected." \
641184610Salfred			          $ndisks $want_disks
642184610Salfred			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
643188981Sthompsa				f_yesno "%s: $msg_not_enough_disks_selected" \
644188981Sthompsa				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
645188981Sthompsa				break
646188981Sthompsa		done
647188981Sthompsa		[ "$breakout" = "break" ] && break
648188981Sthompsa		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
649184610Salfred	done
650184610Salfred
651184610Salfred	return $DIALOG_OK
652184610Salfred}
653184610Salfred
654184610Salfred# zfs_create_diskpart $disk $index
655184610Salfred#
656184610Salfred# For each block device to be used in the zpool, rather than just create the
657193074Sthompsa# zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
658184610Salfred# so we can have some real swap. This also provides wiggle room incase your
659184610Salfred# replacement drivers do not have the exact same sector counts.
660184610Salfred#
661184610Salfred# NOTE: $swapsize and $bootsize should be defined by the calling function.
662188981Sthompsa# NOTE: Sets $bootpart and $targetpart for the calling function.
663188981Sthompsa#
664184610Salfredzfs_create_diskpart()
665184610Salfred{
666184610Salfred	local funcname=zfs_create_diskpart
667188981Sthompsa	local disk="$1" index="$2"
668184610Salfred
669184610Salfred	# Check arguments
670188981Sthompsa	if [ ! "$disk" ]; then
671188981Sthompsa		f_dprintf "$funcname: NULL disk argument"
672184610Salfred		msg_error="$msg_error: $funcname" \
673188981Sthompsa			f_show_err "$msg_null_disk_argument"
674184610Salfred		return $FAILURE
675188981Sthompsa	fi
676188981Sthompsa	if [ "${disk#*[$IFS]}" != "$disk" ]; then
677188981Sthompsa		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
678188981Sthompsa		msg_error="$msg_error: $funcname" \
679188981Sthompsa			f_show_err "$msg_invalid_disk_argument" "$disk"
680188981Sthompsa		return $FAILURE
681188981Sthompsa	fi
682184610Salfred	if [ ! "$index" ]; then
683188981Sthompsa		f_dprintf "$funcname: NULL index argument"
684188981Sthompsa		msg_error="$msg_error: $funcname" \
685188981Sthompsa			f_show_err "$msg_null_index_argument"
686188981Sthompsa		return $FAILURE
687188981Sthompsa	fi
688188981Sthompsa	if ! f_isinteger "$index"; then
689188981Sthompsa		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
690188981Sthompsa		msg_error="$msg_error: $funcname" \
691184610Salfred			f_show_err "$msg_invalid_index_argument" "$index"
692184610Salfred		return $FAILURE
693184610Salfred	fi
694184610Salfred	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
695184610Salfred
696184610Salfred	# Check for unknown partition scheme before proceeding further
697184610Salfred	case "$ZFSBOOT_PARTITION_SCHEME" in
698184610Salfred	""|MBR|GPT) : known good ;;
699184610Salfred	*)
700193074Sthompsa		f_dprintf "$funcname: %s is an unsupported partition scheme" \
701184610Salfred		          "$ZFSBOOT_PARTITION_SCHEME"
702184610Salfred		msg_error="$msg_error: $funcname" f_show_err \
703184610Salfred			"$msg_unsupported_partition_scheme" \
704184610Salfred			"$ZFSBOOT_PARTITION_SCHEME"
705184610Salfred		return $FAILURE
706184610Salfred	esac
707184610Salfred
708184610Salfred	#
709184610Salfred	# Destroy whatever partition layout is currently on disk.
710192055Sthompsa	# NOTE: `-F' required to destroy if partitions still exist.
711192055Sthompsa	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
712192055Sthompsa	#
713192055Sthompsa	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
714192055Sthompsa	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
715184610Salfred	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
716184610Salfred	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
717184610Salfred
718184610Salfred	# Make doubly-sure backup GPT is destroyed
719184610Salfred	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
720184610Salfred	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
721184610Salfred
722184610Salfred	#
723184610Salfred	# Enable boot pool if encryption is desired
724184610Salfred	#
725184610Salfred	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
726184610Salfred
727184610Salfred	#
728184610Salfred	# Lay down the desired type of partition scheme
729192984Sthompsa	#
730192984Sthompsa	local setsize mbrindex
731192984Sthompsa	case "$ZFSBOOT_PARTITION_SCHEME" in
732184610Salfred	""|GPT) f_dprintf "$funcname: Creating GPT layout..."
733192984Sthompsa		#
734184610Salfred		# 1. Create GPT layout using labels
735184610Salfred		#
736184610Salfred		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
737184610Salfred		             return $FAILURE
738194228Sthompsa
739184610Salfred		#
740184610Salfred		# 2. Add small freebsd-boot partition labeled `boot#'
741184610Salfred		#
742184610Salfred		f_eval_catch $funcname gpart "$GPART_ADD_LABEL_WITH_SIZE" \
743184610Salfred		             gptboot$index freebsd-boot 512k $disk ||
744184610Salfred		             return $FAILURE
745184610Salfred		f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
746184610Salfred		             /boot/pmbr /boot/gptzfsboot 1 $disk ||
747184610Salfred		             return $FAILURE
748184610Salfred
749184610Salfred		# NB: zpool will use the `zfs#' GPT labels
750184610Salfred		bootpart=p2 targetpart=p2
751194228Sthompsa		[ ${swapsize:-0} -gt 0 ] && targetpart=p3
752184610Salfred
753184610Salfred		#
754184610Salfred		# Prepare boot pool if enabled (e.g., for geli(8))
755184610Salfred		#
756184610Salfred		if [ "$ZFSBOOT_BOOT_POOL" ]; then
757184610Salfred			bootpart=p2 targetpart=p3
758184610Salfred			[ ${swapsize:-0} -gt 0 ] && targetpart=p4
759184610Salfred			f_eval_catch $funcname gpart \
760193045Sthompsa			             "$GPART_ADD_LABEL_WITH_SIZE" boot$index \
761194228Sthompsa			             freebsd-zfs ${bootsize}b $disk ||
762184610Salfred			             return $FAILURE
763193045Sthompsa			# Pedantically nuke any old labels
764184610Salfred			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
765194228Sthompsa			                /dev/$disk$bootpart
766192984Sthompsa			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
767193045Sthompsa				# Pedantically detach targetpart for later
768184610Salfred				f_eval_catch -d $funcname geli \
769184610Salfred				                "$GELI_DETACH_F" \
770184610Salfred				                /dev/$disk$targetpart
771184610Salfred			fi
772184610Salfred		fi
773194228Sthompsa
774184610Salfred		#
775184610Salfred		# 3. Add freebsd-swap partition labeled `swap#'
776184610Salfred		#
777184610Salfred		if [ ${swapsize:-0} -gt 0 ]; then
778184610Salfred			f_eval_catch $funcname gpart \
779184610Salfred			             "$GPART_ADD_LABEL_WITH_SIZE" swap$index \
780184610Salfred			             freebsd-swap ${swapsize}b $disk ||
781184610Salfred			             return $FAILURE
782184610Salfred			# Pedantically nuke any old labels on the swap
783184610Salfred			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
784184610Salfred			                /dev/gpt/swap$index
785184610Salfred			# Update fstab(5)
786184610Salfred			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
787184610Salfred			             /dev/gpt/swap$index none swap sw 0 0 \
788184610Salfred			             $BSDINSTALL_TMPETC/fstab ||
789184610Salfred			             return $FAILURE
790184610Salfred		fi
791184610Salfred
792184610Salfred		#
793194228Sthompsa		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
794184610Salfred		#
795184610Salfred		f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
796184610Salfred		             zfs$index freebsd-zfs $disk || return $FAILURE
797184610Salfred		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
798184610Salfred		                /dev/$disk$targetpart
799184610Salfred		;;
800184610Salfred
801184610Salfred	MBR) f_dprintf "$funcname: Creating MBR layout..."
802184610Salfred		#
803		# 1. Create MBR layout (no labels)
804		#
805		f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
806		             return $FAILURE
807		f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
808		             $disk || return $FAILURE
809
810		#
811		# 2. Add freebsd slice with all available space
812		#
813		f_eval_catch $funcname gpart "$GPART_ADD" freebsd $disk ||
814		             return $FAILURE
815		f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
816		             return $FAILURE
817		# Pedantically nuke any old labels
818		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
819		                /dev/${disk}s1
820		# Pedantically nuke any old scheme
821		f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
822
823		#
824		# 3. Write BSD scheme to the freebsd slice
825		#
826		f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
827		             return $FAILURE
828
829		# NB: zpool will use s1a (no labels)
830		bootpart=s1a targetpart=s1d mbrindex=4
831
832		#
833		# Always prepare a boot pool on MBR
834		#
835		ZFSBOOT_BOOT_POOL=1
836		f_eval_catch $funcname gpart \
837		             "$GPART_ADD_INDEX_WITH_SIZE" \
838		             1 freebsd-zfs ${bootsize}b ${disk}s1 ||
839		             return $FAILURE
840		# Pedantically nuke any old labels
841		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
842		                /dev/$disk$bootpart
843		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
844			# Pedantically detach targetpart for later
845			f_eval_catch -d $funcname geli \
846			                "$GELI_DETACH_F" \
847					/dev/$disk$targetpart
848		fi
849
850		#
851		# 4. Add freebsd-swap partition
852		#
853		if [ ${swapsize:-0} -gt 0 ]; then
854			f_eval_catch $funcname gpart \
855			             "$GPART_ADD_INDEX_WITH_SIZE" 2 \
856			             freebsd-swap ${swapsize}b ${disk}s1 ||
857			             return $FAILURE
858			# Pedantically nuke any old labels on the swap
859			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
860			                /dev/${disk}s1b
861			# Update fstab(5)
862			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
863			             /dev/${disk}s1b none swap sw 0 0 \
864			             $BSDINSTALL_TMPETC/fstab ||
865			             return $FAILURE
866		fi
867
868		#
869		# 5. Add freebsd-zfs partition for zroot
870		#
871		f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
872			     $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
873		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
874				/dev/$disk$targetpart # Pedantic
875		f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
876			     /boot/zfsboot /dev/${disk}s1 count=1 ||
877			     return $FAILURE
878		;;
879
880	esac # $ZFSBOOT_PARTITION_SCHEME
881
882	return $SUCCESS
883}
884
885# zfs_create_boot $poolname $vdev_type $disks ...
886#
887# Creates boot pool and dataset layout. Returns error if something goes wrong.
888# Errors are printed to stderr for collection and display.
889#
890zfs_create_boot()
891{
892	local funcname=zfs_create_boot
893	local zroot_name="$1"
894	local zroot_vdevtype="$2"
895	local zroot_vdevs= # Calculated below
896	local boot_vdevs= # Used for geli(8) and/or MBR layouts
897	shift 2 # poolname vdev_type
898	local disks="$*" disk
899	local bootpart targetpart # Set by zfs_create_diskpart() below
900
901	#
902	# Pedantic checks; should never be seen
903	#
904	if [ ! "$zroot_name" ]; then
905		f_dprintf "$funcname: NULL poolname"
906		msg_error="$msg_error: $funcname" \
907			f_show_err "$msg_null_poolname"
908		return $FAILURE
909	fi
910	if [ $# -lt 1 ]; then
911		f_dprintf "$funcname: missing disk arguments"
912		msg_error="$msg_error: $funcname" \
913			f_show_err "$msg_missing_disk_arguments"
914		return $FAILURE
915	fi
916	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
917	          "$zroot_name" "$zroot_vdevtype"
918
919	#
920	# Initialize fstab(5)
921	#
922	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
923	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
924	             return $FAILURE
925	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
926	             "# Device" Mountpoint FStype Options Dump "Pass#" \
927	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
928
929	#
930	# Expand SI units in desired sizes
931	#
932	f_dprintf "$funcname: Expanding supplied size values..."
933	local swapsize bootsize
934	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
935		f_dprintf "$funcname: Invalid swap size \`%s'" \
936		          "$ZFSBOOT_SWAP_SIZE"
937		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
938		return $FAILURE
939	fi
940	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
941		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
942		          "$ZFSBOOT_BOOT_POOL_SIZE"
943		f_show_err "$msg_invalid_boot_pool_size" \
944		           "$ZFSBOOT_BOOT_POOL_SIZE"
945		return $FAILURE
946	fi
947	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
948	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
949	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
950	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
951
952	#
953	# Destroy the pool in-case this is our second time 'round (case of
954	# failure and installer presented ``Retry'' option to come back).
955	#
956	# NB: If we don't destroy the pool, later gpart(8) destroy commands
957	# that try to clear existing partitions (see zfs_create_diskpart())
958	# will fail with a `Device Busy' error, leading to `GEOM exists'.
959	#
960	f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
961
962	#
963	# Prepare the disks and build pool device list(s)
964	#
965	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
966	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] &&
967		f_dprintf "$funcname: With 4k alignment using gnop(8)..."
968	local n=0
969	for disk in $disks; do
970		zfs_create_diskpart $disk $n || return $FAILURE
971		# Now $bootpart and $targetpart are set (suffix for $disk)
972		
973		# Forced 4k alignment support using Geom NOP (see gnop(8))
974		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
975			if [ "$ZFSBOOT_BOOT_POOL" ]; then
976				boot_vdevs="$boot_vdevs $disk$bootpart.nop"
977				f_eval_catch $funcname gnop "$GNOP_CREATE" \
978				             $disk$bootpart || return $FAILURE
979			fi
980			# Don't gnop encrypted partition
981			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
982				zroot_vdevs="$zroot_vdevs $disk$targetpart.eli"
983			else
984				zroot_vdevs="$zroot_vdevs $disk$targetpart.nop"
985				f_eval_catch $funcname gnop "$GNOP_CREATE" \
986					     $disk$targetpart ||
987				             return $FAILURE
988			fi
989		else
990			if [ "$ZFSBOOT_BOOT_POOL" ]; then
991				boot_vdevs="$boot_vdevs $disk$bootpart"
992			fi
993			zroot_vdevs="$zroot_vdevs $disk$targetpart"
994		fi
995
996		n=$(( $n + 1 ))
997	done # disks
998
999	#
1000	# If we need/want a boot pool, create it
1001	#
1002	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1003		local bootpool_vdevtype= # Calculated below
1004		local bootpool_options= # Calculated below
1005		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
1006		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
1007		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
1008
1009		f_dprintf "$funcname: Setting up boot pool..."
1010		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
1011			f_dprintf "$funcname: For encrypted root disk..."
1012
1013		# Create parent directory for boot pool
1014		f_eval_catch -d $funcname umount "$UMOUNT" /mnt
1015		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1016		             $BSDINSTALL_CHROOT || return $FAILURE
1017
1018		# Create mirror across the boot partition on all disks
1019		local nvdevs
1020		f_count nvdevs $boot_vdevs
1021		[ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1022
1023		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
1024		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1025		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1026		             "$bootpool_options" "$bootpool_name" \
1027		             "$bootpool_vdevtype" "$boot_vdevs" ||
1028		             return $FAILURE
1029
1030		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1031		             return $FAILURE
1032
1033		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1034			# Generate an encryption key using random(4)
1035			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1036				     /dev/random "$bootpool/$zroot_key" \
1037			             "bs=4096 count=1" || return $FAILURE
1038		else
1039			# Clean up
1040			f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
1041			             "$bootpool_name" || return $FAILURE
1042			f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1043		fi
1044
1045	fi
1046
1047	#
1048	# Create the geli(8) GEOMS
1049	#
1050	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1051		# Prompt user for password (twice)
1052		if ! msg_enter_new_password="$msg_geli_password" \
1053			f_dialog_input_password
1054		then
1055			f_dprintf "$funcname: User cancelled"
1056			f_show_err "$msg_user_cancelled"
1057			return $FAILURE
1058		fi
1059
1060		# Initialize geli(8) on each of the target partitions
1061		for disk in $disks; do
1062			f_dialog_info "$msg_geli_setup" \
1063				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1064			if ! echo "$pw_password" | f_eval_catch \
1065				$funcname geli "$GELI_PASSWORD_INIT" \
1066				"$bootpool/boot/$disk$targetpart.eli" \
1067				AES-XTS "$bootpool/$zroot_key" \
1068				$disk$targetpart
1069			then
1070				f_interactive || f_die
1071				unset pw_password # Sensitive info
1072				return $FAILURE
1073			fi
1074			if ! echo "$pw_password" | f_eval_catch \
1075				$funcname geli "$GELI_ATTACH" \
1076				"$bootpool/$zroot_key" $disk$targetpart
1077			then
1078				f_interactive || f_die
1079				unset pw_password # Sensitive info
1080				return $FAILURE
1081			fi
1082		done
1083		unset pw_password # Sensitive info
1084
1085		# Clean up
1086		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
1087			return $FAILURE
1088		f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1089	fi
1090
1091	#
1092	# Create the ZFS root pool with desired type and disk devices
1093	#
1094	f_dprintf "$funcname: Creating root pool..."
1095	f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1096	             "-o altroot=$BSDINSTALL_CHROOT -m none -f" \
1097	             "$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
1098	             return $FAILURE
1099
1100	# Customize the zroot a bit...
1101	local option
1102	f_dprintf "$funcname: Setting miscellaneous options on root pool..."
1103	for option in atime=off; do
1104		f_eval_catch $funcname zfs "$ZFS_SET" $option "$zroot_name" ||
1105			return $FAILURE
1106	done
1107
1108	#
1109	# Create ZFS dataset layout within the new root pool
1110	#
1111	f_dprintf "$funcname: Creating ZFS datasets..."
1112	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1113		# Skip blank lines and comments
1114		case "$dataset" in "#"*|"") continue; esac
1115		# Remove potential inline comments in options
1116		options="${options%%#*}"
1117		# Replace tabs with spaces
1118		f_replaceall "$options" "	" " " options
1119		# Reduce contiguous runs of space to one single space
1120		oldoptions=
1121		while [ "$oldoptions" != "$options" ]; do
1122			oldoptions="$options"
1123			f_replaceall "$options" "  " " " options
1124		done
1125		# Replace both commas and spaces with ` -o '
1126		f_replaceall "$options" "[ ,]" " -o " options
1127		# Create the dataset with desired options
1128		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
1129		             "${options:+-o $options}" "$zroot_name$dataset" ||
1130		             return $FAILURE
1131	done
1132
1133	# Touch up permissions on the tmp directories
1134	f_dprintf "$funcname: Modifying directory permissions..."
1135	local dir
1136	for dir in /tmp /var/tmp; do
1137		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1138		             $BSDINSTALL_CHROOTDIR$dir || return $FAILURE
1139	done
1140
1141	# Create symlink(s)
1142	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1143		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1144		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1145		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1146	fi
1147
1148	# Set bootfs property
1149	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1150	f_dprintf "$funcname: Setting bootfs property..."
1151	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1152		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
1153		return $FAILURE
1154
1155	# Export the pool(s)
1156	f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
1157	f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1158	             return $FAILURE
1159	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1160		f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
1161		             "$bootpool_name" || return $FAILURE
1162	fi
1163
1164	# Destroy the gnop devices (if enabled)
1165	for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$disks}; do
1166		if [ "$ZFSBOOT_BOOT_POOL" ]; then
1167			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1168			                $disk$bootpart.nop
1169		fi
1170		if [ ! "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1171			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1172			                $disk$targetpart.nop
1173		fi
1174	done
1175
1176	# MBR boot loader touch-up
1177	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1178		f_dprintf "$funcname: Updating MBR boot loader on disks..."
1179		# Stick the ZFS boot loader in the "convienient hole" after 
1180		# the ZFS internal metadata
1181		for disk in $disks; do
1182			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1183			             /boot/zfsboot /dev/$disk$bootpart \
1184			             "skip=1 seek=1024" || return $FAILURE
1185		done
1186	fi
1187
1188	# Re-import the ZFS pool(s)
1189	f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1190	f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1191	             "-o altroot=\"$BSDINSTALL_CHROOT\"" "$zroot_name" ||
1192	             return $FAILURE
1193	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1194		f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1195		             "-o altroot=\"$BSDINSTALL_CHROOT\"" \
1196		             "$bootpool_name" || return $FAILURE
1197	fi
1198
1199	# While this is apparently not needed, it seems to help MBR
1200	f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1201	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1202	             return $FAILURE
1203	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1204	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1205	             "$zroot_name" || return $FAILURE
1206
1207	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1208	# NOTE: We later concatenate these into their destination
1209	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1210	          "$funcname"
1211	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1212	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1213	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_load=\"YES\"' \
1214	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1215
1216	# We're all done unless we should go on for boot pool
1217	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1218
1219	# Set cachefile for boot pool so it auto-imports at system start
1220	f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1221	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1222	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1223	             "$bootpool_name" || return $FAILURE
1224
1225	# Some additional geli(8) requirements for loader.conf(5)
1226	for option in \
1227		'zpool_cache_load=\"YES\"' \
1228		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1229		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1230	; do
1231		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1232		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1233		             return $FAILURE
1234	done
1235	f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
1236	    "\"zfs:$zroot_name/$zroot_bootfs\"" \
1237	    $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
1238
1239	# We're all done unless we should go on to do encryption
1240	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
1241
1242	#
1243	# Configure geli(8)-based encryption
1244	#
1245	f_dprintf "$funcname: Configuring disk encryption..."
1246	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1247	    $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1248	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1249	    $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1250	for disk in $disks; do
1251		f_eval_catch $funcname printf "$PRINTF_CONF" \
1252			geli_%s_keyfile0_load "$disk$targetpart YES" \
1253			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1254			return $FAILURE
1255		f_eval_catch $funcname printf "$PRINTF_CONF" \
1256			geli_%s_keyfile0_type \
1257			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1258			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1259			return $FAILURE
1260		f_eval_catch $funcname printf "$PRINTF_CONF" \
1261			geli_%s_keyfile0_name \
1262			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1263			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1264			return $FAILURE
1265	done
1266
1267	return $SUCCESS
1268}
1269
1270# dialog_menu_diskinfo
1271#
1272# Prompt the user to select a disk and then provide detailed info on it.
1273#
1274dialog_menu_diskinfo()
1275{
1276	local device disk
1277
1278	#
1279	# Break from loop when user cancels disk selection
1280	#
1281	while :; do
1282		device=$( msg_cancel="$msg_back" f_device_menu \
1283			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1284			$DEVICE_TYPE_DISK 2>&1 ) || break
1285		$device get name disk
1286
1287		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1288		f_show_msg "$msg_detailed_disk_info" \
1289			"$disk" "$( gpart show $disk 2> /dev/null )" \
1290			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1291			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1292	done
1293
1294	return $SUCCESS
1295}
1296
1297############################################################ MAIN
1298
1299#
1300# Initialize
1301#
1302f_dialog_title "$msg_zfs_configuration"
1303f_dialog_backtitle "$msg_freebsd_installer"
1304
1305# User may have specifically requested ZFS-related operations be interactive
1306! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1307
1308#
1309# Debugging
1310#
1311f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1312f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1313f_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1314
1315#
1316# Loop over the main menu until we've accomplished what we came here to do
1317#
1318while :; do
1319	if ! f_interactive; then
1320		retval=$DIALOG_OK
1321		mtag=">>> $msg_install"
1322	else
1323		dialog_menu_main
1324		retval=$?
1325		f_dialog_menutag_fetch mtag
1326	fi
1327
1328	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1329	[ $retval -eq $DIALOG_OK ] || f_die
1330
1331	case "$mtag" in
1332	">>> $msg_install")
1333		#
1334		# First, validate the user's selections
1335		#
1336
1337		# Make sure they gave us a name for the pool
1338		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1339			f_dprintf "Pool name cannot be empty."
1340			f_show_err "$msg_pool_name_cannot_be_empty"
1341			continue
1342		fi
1343
1344		# Validate vdev type against number of disks selected/scripted
1345		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1346		# NB: dialog_menu_layout supports running non-interactively
1347		dialog_menu_layout || continue
1348
1349		# Make sure each disk will be at least 50% ZFS
1350		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
1351		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize
1352		then
1353			minsize=$swapsize teeny_disks=
1354			[ "$ZFSBOOT_BOOT_POOL" ] &&
1355				minsize=$(( $minsize + $bootsize ))
1356			for disk in $ZFSBOOT_DISKS; do
1357				debug= f_device_find -1 \
1358					$disk $DEVICE_TYPE_DISK device
1359				$device get capacity disksize || continue
1360				[ ${disksize:-0} -ge 0 ] || disksize=0
1361				disksize=$(( $disksize - $minsize ))
1362				[ $disksize -lt $minsize ] &&
1363					teeny_disks="$teeny_disks $disk"
1364			done
1365			if [ "$teeny_disks" ]; then
1366				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1367				          "$ZFSBOOT_SWAP_SIZE" \
1368				          "$ZFSBOOT_BOOT_POOL_SIZE" \
1369				          "minsize=[$minsize]"
1370				f_dprintf "These disks are too small: %s" \
1371				          "$teeny_disks"
1372				f_show_err "$msg_these_disks_are_too_small" \
1373				           "$ZFSBOOT_SWAP_SIZE" \
1374				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1375				           "$teeny_disks"
1376				continue
1377			fi
1378		fi
1379
1380		#
1381		# Last Chance!
1382		#
1383		if f_interactive; then
1384			dialog_last_chance $ZFSBOOT_DISKS || continue
1385		fi
1386
1387		#
1388		# Let's do this
1389		#
1390
1391		vdev_type="$ZFSBOOT_VDEV_TYPE"
1392
1393		# Blank the vdev type for the default layout
1394		[ "$vdev_type" = "stripe" ] && vdev_type=
1395
1396		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1397		                "$vdev_type" $ZFSBOOT_DISKS || continue
1398
1399		break # to success
1400		;;
1401	?" $msg_pool_type_disks")
1402		ZFSBOOT_CONFIRM_LAYOUT=1
1403		dialog_menu_layout
1404		# User has poked settings, disable later confirmation
1405		ZFSBOOT_CONFIRM_LAYOUT=
1406		;;
1407	"- $msg_rescan_devices") f_device_rescan ;;
1408	"- $msg_disk_info") dialog_menu_diskinfo ;;
1409	?" $msg_pool_name")
1410		# Prompt the user to input/change the name for the new pool
1411		f_dialog_input input \
1412			"$msg_please_enter_a_name_for_your_zpool" \
1413			"$ZFSBOOT_POOL_NAME" &&
1414			ZFSBOOT_POOL_NAME="$input"
1415		;;
1416	?" $msg_force_4k_sectors")
1417		# Toggle the variable referenced both by the menu and later
1418		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1419			ZFSBOOT_GNOP_4K_FORCE_ALIGN=
1420		else
1421			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1422		fi
1423		;;
1424	?" $msg_encrypt_disks")
1425		# Toggle the variable referenced both by the menu and later
1426		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1427			ZFSBOOT_GELI_ENCRYPTION=
1428		else
1429			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1430			ZFSBOOT_GELI_ENCRYPTION=1
1431		fi
1432		;;
1433	?" $msg_partition_scheme")
1434		# Toggle between GPT and MBR
1435		if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
1436			ZFSBOOT_PARTITION_SCHEME=MBR
1437		else
1438			ZFSBOOT_PARTITION_SCHEME=GPT
1439		fi
1440		;;
1441	?" $msg_swap_size")
1442		# Prompt the user to input/change the swap size for each disk
1443		f_dialog_input input \
1444			"$msg_please_enter_amount_of_swap_space" \
1445			"$ZFSBOOT_SWAP_SIZE" &&
1446			ZFSBOOT_SWAP_SIZE="${input:-0}"
1447		;;
1448	esac
1449done
1450
1451return $SUCCESS
1452
1453################################################################################
1454# END
1455################################################################################
1456