xref: /freebsd/usr.sbin/bsdinstall/scripts/zfsboot (revision cf9c3e5697bc69f08820d7e3b7a4c21f989a7f64)
1cd88b886SDevin Teske#!/bin/sh
2cd88b886SDevin Teske#-
3cd88b886SDevin Teske# Copyright (c) 2013 Allan Jude
4cd88b886SDevin Teske# Copyright (c) 2013 Devin Teske
5cd88b886SDevin Teske# All rights reserved.
6cd88b886SDevin Teske#
7cd88b886SDevin Teske# Redistribution and use in source and binary forms, with or without
8cd88b886SDevin Teske# modification, are permitted provided that the following conditions
9cd88b886SDevin Teske# are met:
10cd88b886SDevin Teske# 1. Redistributions of source code must retain the above copyright
11cd88b886SDevin Teske#    notice, this list of conditions and the following disclaimer.
12cd88b886SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
13cd88b886SDevin Teske#    notice, this list of conditions and the following disclaimer in the
14cd88b886SDevin Teske#    documentation and/or other materials provided with the distribution.
15cd88b886SDevin Teske#
16cd88b886SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17cd88b886SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18cd88b886SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19cd88b886SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20cd88b886SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21cd88b886SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22cd88b886SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23cd88b886SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24cd88b886SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25cd88b886SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26cd88b886SDevin Teske# SUCH DAMAGE.
27cd88b886SDevin Teske#
28cd88b886SDevin Teske# $FreeBSD$
29cd88b886SDevin Teske#
30cd88b886SDevin Teske############################################################ INCLUDES
31cd88b886SDevin Teske
32cd88b886SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
33cd88b886SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
34cd88b886SDevin Teskef_dprintf "%s: loading includes..." "$0"
35cd88b886SDevin Teskef_include $BSDCFG_SHARE/device.subr
36cd88b886SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
37cd88b886SDevin Teskef_include $BSDCFG_SHARE/password/password.subr
38cd88b886SDevin Teskef_include $BSDCFG_SHARE/variable.subr
39cd88b886SDevin Teske
40cd88b886SDevin Teske############################################################ CONFIGURATION
41cd88b886SDevin Teske
42cd88b886SDevin Teske#
43cd88b886SDevin Teske# Default name of the boot-pool
44cd88b886SDevin Teske#
45cd88b886SDevin Teske: ${ZFSBOOT_POOL_NAME:=zroot}
46cd88b886SDevin Teske
47cd88b886SDevin Teske#
48cd88b886SDevin Teske# Default name for the boot environment parent dataset
49cd88b886SDevin Teske#
5067635c19SDevin Teske: ${ZFSBOOT_BEROOT_NAME:=ROOT}
51cd88b886SDevin Teske
52cd88b886SDevin Teske#
53cd88b886SDevin Teske# Default name for the primany boot environment
54cd88b886SDevin Teske#
55cd88b886SDevin Teske: ${ZFSBOOT_BOOTFS_NAME:=default}
56cd88b886SDevin Teske
57cd88b886SDevin Teske#
58cd88b886SDevin Teske# Default Virtual Device (vdev) type to create
59cd88b886SDevin Teske#
60cd88b886SDevin Teske: ${ZFSBOOT_VDEV_TYPE:=stripe}
61cd88b886SDevin Teske
62cd88b886SDevin Teske#
63cd88b886SDevin Teske# Should we use gnop(8) to configure a transparent mapping to 4K sectors?
64cd88b886SDevin Teske#
65cd88b886SDevin Teske: ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=1}
66cd88b886SDevin Teske
67cd88b886SDevin Teske#
68cd88b886SDevin Teske# Should we use geli(8) to encrypt the drives?
697cae6aabSDevin Teske# NB: Automatically enables ZFSBOOT_BOOT_POOL
70cd88b886SDevin Teske#
71bc4a673fSDevin Teske: ${ZFSBOOT_GELI_ENCRYPTION=}
72cd88b886SDevin Teske
73cd88b886SDevin Teske#
74cd88b886SDevin Teske# Default path to the geli(8) keyfile used in drive encryption
75cd88b886SDevin Teske#
76cd88b886SDevin Teske: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
77cd88b886SDevin Teske
78cd88b886SDevin Teske#
797cae6aabSDevin Teske# Create a separate boot pool?
807cae6aabSDevin Teske# NB: Automatically set when using geli(8) or MBR
817cae6aabSDevin Teske#
827cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL=}
837cae6aabSDevin Teske
847cae6aabSDevin Teske#
857cae6aabSDevin Teske# Default name for boot pool when enabled (e.g., geli(8) or MBR)
867cae6aabSDevin Teske#
877cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
887cae6aabSDevin Teske
897cae6aabSDevin Teske#
907cae6aabSDevin Teske# Default size for boot pool when enabled (e.g., geli(8) or MBR)
917cae6aabSDevin Teske#
927cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
937cae6aabSDevin Teske
947cae6aabSDevin Teske#
95cd88b886SDevin Teske# Default disks to use (always empty unless being scripted)
96cd88b886SDevin Teske#
97cd88b886SDevin Teske: ${ZFSBOOT_DISKS:=}
98cd88b886SDevin Teske
99cd88b886SDevin Teske#
100cd88b886SDevin Teske# Default partitioning scheme to use on disks
101cd88b886SDevin Teske#
102cd88b886SDevin Teske: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
103cd88b886SDevin Teske
104cd88b886SDevin Teske#
105cd88b886SDevin Teske# How much swap to put on each block device in the boot zpool
106cd88b886SDevin Teske# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
107cd88b886SDevin Teske#
108cd88b886SDevin Teske: ${ZFSBOOT_SWAP_SIZE:=2g}
109cd88b886SDevin Teske
110cd88b886SDevin Teske#
111bc4a673fSDevin Teske# Default ZFS datasets for root zpool
112cd88b886SDevin Teske#
113cd88b886SDevin Teske# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
114cd88b886SDevin Teske# NOTE: Anything after pound/hash character [#] is ignored as a comment.
115cd88b886SDevin Teske#
116cd88b886SDevin Teskef_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
117cd88b886SDevin Teske	# DATASET	OPTIONS (comma or space separated; or both)
118cd88b886SDevin Teske
119cd88b886SDevin Teske	# Boot Environment [BE] root and default boot dataset
120cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
121cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
122cd88b886SDevin Teske
123cd88b886SDevin Teske	# Compress /tmp, allow exec but not setuid
124cd88b886SDevin Teske	/tmp		mountpoint=/tmp,compression=lz4,exec=on,setuid=off
125cd88b886SDevin Teske
126cd88b886SDevin Teske	# Don't mount /usr so that 'base' files go to the BEROOT
127cd88b886SDevin Teske	/usr		mountpoint=/usr,canmount=off
128cd88b886SDevin Teske
129cd88b886SDevin Teske	# Home directories separated so they are common to all BEs
130bc4a673fSDevin Teske	/usr/home	# NB: /home is a symlink to /usr/home
131cd88b886SDevin Teske
132cd88b886SDevin Teske	# Ports tree
133cd88b886SDevin Teske	/usr/ports		compression=lz4,setuid=off
134cd88b886SDevin Teske
135cd88b886SDevin Teske	# Source tree (compressed)
136cd88b886SDevin Teske	/usr/src	compression=lz4,exec=off,setuid=off
137cd88b886SDevin Teske
138cd88b886SDevin Teske	# Create /var and friends
139cd88b886SDevin Teske	/var		mountpoint=/var
140cd88b886SDevin Teske	/var/crash	compression=lz4,exec=off,setuid=off
141cd88b886SDevin Teske	/var/log	compression=lz4,exec=off,setuid=off
142a6c8532cSDevin Teske	/var/mail	compression=lz4,atime=on
143cd88b886SDevin Teske	/var/tmp	compression=lz4,exec=on,setuid=off
144cd88b886SDevin Teske" # END-QUOTE
145cd88b886SDevin Teske
146bc4a673fSDevin Teske#
147bc4a673fSDevin Teske# If interactive and the user has not explicitly chosen a vdev type or disks,
148bc4a673fSDevin Teske# make the user confirm scripted/default choices when proceeding to install.
149bc4a673fSDevin Teske#
150bc4a673fSDevin Teske: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
151bc4a673fSDevin Teske
152cd88b886SDevin Teske############################################################ GLOBALS
153cd88b886SDevin Teske
154cd88b886SDevin Teske#
155bc4a673fSDevin Teske# Format of a line in printf(1) syntax to add to fstab(5)
156bc4a673fSDevin Teske#
157bc4a673fSDevin TeskeFSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
158bc4a673fSDevin Teske
159bc4a673fSDevin Teske#
160bc4a673fSDevin Teske# Command strings for various tasks
161bc4a673fSDevin Teske#
162bc4a673fSDevin TeskeCHMOD_MODE='chmod %s "%s"'
163bc4a673fSDevin TeskeDD_WITH_OPTIONS='dd if="%s" of="%s" %s'
164bc4a673fSDevin TeskeECHO_APPEND='echo "%s" >> "%s"'
165bc4a673fSDevin TeskeGELI_ATTACH='geli attach -j - -k "%s" "%s"'
166a622223fSDevin TeskeGELI_DETACH_F='geli detach -f "%s"'
167bc4a673fSDevin TeskeGELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
168bc4a673fSDevin TeskeGNOP_CREATE='gnop create -S 4096 "%s"'
169a622223fSDevin TeskeGNOP_DESTROY='gnop destroy "%s"'
170bc4a673fSDevin TeskeGPART_ADD='gpart add -t %s "%s"'
171bc4a673fSDevin TeskeGPART_ADD_INDEX='gpart add -i %s -t %s "%s"'
172bc4a673fSDevin TeskeGPART_ADD_INDEX_WITH_SIZE='gpart add -i %s -t %s -s %s "%s"'
173bc4a673fSDevin TeskeGPART_ADD_LABEL='gpart add -l %s -t %s "%s"'
174bc4a673fSDevin TeskeGPART_ADD_LABEL_WITH_SIZE='gpart add -l %s -t %s -s %s "%s"'
175bc4a673fSDevin TeskeGPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
176bc4a673fSDevin TeskeGPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
177bc4a673fSDevin TeskeGPART_CREATE='gpart create -s %s "%s"'
178a622223fSDevin TeskeGPART_DESTROY_F='gpart destroy -F "%s"'
179bc4a673fSDevin TeskeGPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
180a622223fSDevin TeskeGRAID_DELETE='graid delete "%s"'
181bc4a673fSDevin TeskeLN_SF='ln -sf "%s" "%s"'
182bc4a673fSDevin TeskeMKDIR_P='mkdir -p "%s"'
183bc4a673fSDevin TeskeMOUNT_TYPE='mount -t %s "%s" "%s"'
184bc4a673fSDevin TeskePRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
185bc4a673fSDevin TeskePRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
186bc4a673fSDevin TeskeSHELL_TRUNCATE=':> "%s"'
187a622223fSDevin TeskeUMOUNT='umount "%s"'
188bc4a673fSDevin TeskeZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
189bc4a673fSDevin TeskeZFS_SET='zfs set "%s" "%s"'
190bc4a673fSDevin TeskeZFS_UNMOUNT='zfs unmount "%s"'
191bc4a673fSDevin TeskeZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
192bc4a673fSDevin TeskeZPOOL_EXPORT='zpool export "%s"'
193bc4a673fSDevin TeskeZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
194a622223fSDevin TeskeZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
195bc4a673fSDevin TeskeZPOOL_SET='zpool set %s "%s"'
196bc4a673fSDevin Teske
197bc4a673fSDevin Teske#
198cd88b886SDevin Teske# Strings that should be moved to an i18n file and loaded with f_include_lang()
199cd88b886SDevin Teske#
200cd88b886SDevin Teskehline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
201cd88b886SDevin Teskehline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
202cd88b886SDevin Teskehline_arrows_tab_enter="Press arrows, TAB or ENTER"
203bc4a673fSDevin Teskemsg_an_unknown_error_occurred="An unknown error occurred"
204cd88b886SDevin Teskemsg_back="Back"
205cd88b886SDevin Teskemsg_cancel="Cancel"
206bc4a673fSDevin Teskemsg_change_selection="Change Selection"
207cd88b886SDevin Teskemsg_configure_options="Configure Options:"
208cd88b886SDevin Teskemsg_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"
209cd88b886SDevin Teskemsg_disk_info="Disk Info"
210cd88b886SDevin Teskemsg_disk_info_help="Get detailed information on disk device(s)"
211bc4a673fSDevin Teskemsg_encrypt_disks="Encrypt Disks?"
212bc4a673fSDevin Teskemsg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
213bc4a673fSDevin Teskemsg_error="Error"
214cd88b886SDevin Teskemsg_force_4k_sectors="Force 4K Sectors?"
215cd88b886SDevin Teskemsg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
216cd88b886SDevin Teskemsg_freebsd_installer="FreeBSD Installer"
217cd88b886SDevin Teskemsg_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"
2187a434c5cSDevin Teskemsg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
219bc4a673fSDevin Teskemsg_install="Install"
220bc4a673fSDevin Teskemsg_install_desc="Proceed with Installation"
221bc4a673fSDevin Teskemsg_install_help="Create ZFS boot pool with displayed options"
2227cae6aabSDevin Teskemsg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
223bc4a673fSDevin Teskemsg_invalid_disk_argument="Invalid disk argument \`%s'"
224bc4a673fSDevin Teskemsg_invalid_index_argument="Invalid index argument \`%s'"
225bc4a673fSDevin Teskemsg_invalid_swap_size="Invalid swap size \`%s'"
226cd88b886SDevin Teskemsg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
227bc4a673fSDevin Teskemsg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy\nthe current contents of the following disks:\n\n   %s"
228bc4a673fSDevin Teskemsg_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'
229cd88b886SDevin Teskemsg_mirror_desc="Mirror - n-Way Mirroring"
230cd88b886SDevin Teskemsg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
231bc4a673fSDevin Teskemsg_missing_disk_arguments="missing disk arguments"
232bc4a673fSDevin Teskemsg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
233cd88b886SDevin Teskemsg_no="NO"
234cd88b886SDevin Teskemsg_no_disks_present_to_configure="No disk(s) present to configure"
235cd88b886SDevin Teskemsg_no_disks_selected="No disks selected."
236bc4a673fSDevin Teskemsg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
237bc4a673fSDevin Teskemsg_null_disk_argument="NULL disk argument"
238bc4a673fSDevin Teskemsg_null_index_argument="NULL index argument"
239bc4a673fSDevin Teskemsg_null_poolname="NULL poolname"
240cd88b886SDevin Teskemsg_ok="OK"
241cd88b886SDevin Teskemsg_partition_scheme="Partition Scheme"
242cd88b886SDevin Teskemsg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
243cd88b886SDevin Teskemsg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
244cd88b886SDevin Teskemsg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
245cd88b886SDevin Teskemsg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
246cd88b886SDevin Teskemsg_pool_name="Pool Name"
247cd88b886SDevin Teskemsg_pool_name_cannot_be_empty="Pool name cannot be empty."
248cd88b886SDevin Teskemsg_pool_name_help="Customize the name of the zpool to be created (Required)"
249bc4a673fSDevin Teskemsg_pool_type_disks="Pool Type/Disks:"
250bc4a673fSDevin Teskemsg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
251cd88b886SDevin Teskemsg_processing_selection="Processing selection..."
252cd88b886SDevin Teskemsg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
253cd88b886SDevin Teskemsg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
254cd88b886SDevin Teskemsg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
255cd88b886SDevin Teskemsg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
256cd88b886SDevin Teskemsg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
257cd88b886SDevin Teskemsg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
258cd88b886SDevin Teskemsg_rescan_devices="Rescan Devices"
259cd88b886SDevin Teskemsg_rescan_devices_help="Scan for device changes"
260cd88b886SDevin Teskemsg_select="Select"
261cd88b886SDevin Teskemsg_select_a_disk_device="Select a disk device"
262cd88b886SDevin Teskemsg_select_virtual_device_type="Select Virtual Device type:"
263cd88b886SDevin Teskemsg_stripe_desc="Stripe - No Redundancy"
264cd88b886SDevin Teskemsg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
265cd88b886SDevin Teskemsg_swap_size="Swap Size"
266cd88b886SDevin Teskemsg_swap_size_help="Customize how much swap space is allocated to each selected disk"
267bc4a673fSDevin Teskemsg_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."
268bc4a673fSDevin Teskemsg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
269bc4a673fSDevin Teskemsg_unsupported_partition_scheme="%s is an unsupported partition scheme"
270bc4a673fSDevin Teskemsg_user_cancelled="User Cancelled."
271cd88b886SDevin Teskemsg_yes="YES"
272cd88b886SDevin Teskemsg_zfs_configuration="ZFS Configuration"
273cd88b886SDevin Teske
274cd88b886SDevin Teske############################################################ FUNCTIONS
275cd88b886SDevin Teske
276cd88b886SDevin Teske# dialog_menu_main
277cd88b886SDevin Teske#
278cd88b886SDevin Teske# Display the dialog(1)-based application main menu.
279cd88b886SDevin Teske#
280cd88b886SDevin Teskedialog_menu_main()
281cd88b886SDevin Teske{
282cd88b886SDevin Teske	local title="$DIALOG_TITLE"
283cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
284cd88b886SDevin Teske	local prompt="$msg_configure_options"
285cd88b886SDevin Teske	local force4k="$msg_no"
286cd88b886SDevin Teske	local usegeli="$msg_no"
287cd88b886SDevin Teske	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
288cd88b886SDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
289bc4a673fSDevin Teske	local disks n=$( set -- $ZFSBOOT_DISKS; echo $# )
290bc4a673fSDevin Teske	{ [ $n -eq 1 ] && disks=disk; } || disks=disks # grammar
291cd88b886SDevin Teske	local menu_list="
292bc4a673fSDevin Teske		'>>> $msg_install'        '$msg_install_desc'
293bc4a673fSDevin Teske		                          '$msg_install_help'
294bc4a673fSDevin Teske		'T $msg_pool_type_disks'  '$ZFSBOOT_VDEV_TYPE: $n $disks'
295bc4a673fSDevin Teske                                          '$msg_pool_type_disks_help'
296cd88b886SDevin Teske		'- $msg_rescan_devices'   '*'
297cd88b886SDevin Teske		                          '$msg_rescan_devices_help'
298cd88b886SDevin Teske		'- $msg_disk_info'        '*'
299cd88b886SDevin Teske		                          '$msg_disk_info_help'
300bc4a673fSDevin Teske		'N $msg_pool_name'        '$ZFSBOOT_POOL_NAME'
301cd88b886SDevin Teske		                          '$msg_pool_name_help'
302cd88b886SDevin Teske		'4 $msg_force_4k_sectors' '$force4k'
303cd88b886SDevin Teske		                          '$msg_force_4k_sectors_help'
304bc4a673fSDevin Teske		'E $msg_encrypt_disks'    '$usegeli'
305bc4a673fSDevin Teske		                          '$msg_encrypt_disks_help'
306bc4a673fSDevin Teske		'P $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
307cd88b886SDevin Teske		                          '$msg_partition_scheme_help'
308bc4a673fSDevin Teske		'S $msg_swap_size'        '$ZFSBOOT_SWAP_SIZE'
309cd88b886SDevin Teske		                          '$msg_swap_size_help'
310cd88b886SDevin Teske	" # END-QUOTE
311cd88b886SDevin Teske	local defaultitem= # Calculated below
312cd88b886SDevin Teske	local hline="$hline_alnum_arrows_punc_tab_enter"
313cd88b886SDevin Teske
314cd88b886SDevin Teske	local height width rows
315cd88b886SDevin Teske	eval f_dialog_menu_with_help_size height width rows \
316cd88b886SDevin Teske		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
317cd88b886SDevin Teske
318cd88b886SDevin Teske	# Obtain default-item from previously stored selection
319cd88b886SDevin Teske	f_dialog_default_fetch defaultitem
320cd88b886SDevin Teske
321cd88b886SDevin Teske	local menu_choice
322cd88b886SDevin Teske	menu_choice=$( eval $DIALOG \
323cd88b886SDevin Teske		--title \"\$title\"              \
324cd88b886SDevin Teske		--backtitle \"\$btitle\"         \
325cd88b886SDevin Teske		--hline \"\$hline\"              \
326cd88b886SDevin Teske		--item-help                      \
327cd88b886SDevin Teske		--ok-label \"\$msg_select\"      \
328cd88b886SDevin Teske		--cancel-label \"\$msg_cancel\"  \
329cd88b886SDevin Teske		--default-item \"\$defaultitem\" \
330cd88b886SDevin Teske		--menu \"\$prompt\"              \
331cd88b886SDevin Teske		$height $width $rows             \
332cd88b886SDevin Teske		$menu_list                       \
333cd88b886SDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
334cd88b886SDevin Teske	)
335cd88b886SDevin Teske	local retval=$?
336cd88b886SDevin Teske	f_dialog_data_sanitize menu_choice
337cd88b886SDevin Teske	f_dialog_menutag_store "$menu_choice"
338cd88b886SDevin Teske
339cd88b886SDevin Teske	# Only update default-item on success
340cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
341cd88b886SDevin Teske
342cd88b886SDevin Teske	return $retval
343cd88b886SDevin Teske}
344cd88b886SDevin Teske
345bc4a673fSDevin Teske# dialog_last_chance $disks ...
346cd88b886SDevin Teske#
347bc4a673fSDevin Teske# Display a list of the disks that the user is about to destroy. The default
348bc4a673fSDevin Teske# action is to return error status unless the user explicitly (non-default)
349bc4a673fSDevin Teske# selects "Yes" from the noyes dialog.
350cd88b886SDevin Teske#
351bc4a673fSDevin Teskedialog_last_chance()
352cd88b886SDevin Teske{
353cd88b886SDevin Teske	local title="$DIALOG_TITLE"
354cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
355bc4a673fSDevin Teske	local prompt # Calculated below
356bc4a673fSDevin Teske	local hline="$hline_arrows_tab_enter"
357cd88b886SDevin Teske
358bc4a673fSDevin Teske	local height=8 width=50 prefix="   "
359bc4a673fSDevin Teske	local plen=${#prefix} list= line=
360bc4a673fSDevin Teske	local max_width=$(( $width - 3 - $plen ))
361bc4a673fSDevin Teske
362bc4a673fSDevin Teske	local yes no defaultno extra_args format
363bc4a673fSDevin Teske	if [ "$USE_XDIALOG" ]; then
364bc4a673fSDevin Teske		yes=ok no=cancel defaultno=default-no
365bc4a673fSDevin Teske		extra_args="--wrap --left"
366bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure"
367bc4a673fSDevin Teske	else
368bc4a673fSDevin Teske		yes=yes no=no defaultno=defaultno
369bc4a673fSDevin Teske		extra_args="--colors --cr-wrap"
370bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure_color"
371bc4a673fSDevin Teske	fi
372bc4a673fSDevin Teske
373bc4a673fSDevin Teske	local disk line_width
374bc4a673fSDevin Teske	for disk in $*; do
375bc4a673fSDevin Teske		if [ "$line" ]; then
376bc4a673fSDevin Teske			line_width=${#line}
377bc4a673fSDevin Teske		else
378bc4a673fSDevin Teske			line_width=$plen
379bc4a673fSDevin Teske		fi
380bc4a673fSDevin Teske		line_width=$(( $line_width + 1 + ${#disk} ))
381bc4a673fSDevin Teske		# Add newline before disk if it would exceed max_width
382bc4a673fSDevin Teske		if [ $line_width -gt $max_width ]; then
383bc4a673fSDevin Teske			list="$list$line\n"
384bc4a673fSDevin Teske			line="$prefix"
385bc4a673fSDevin Teske			height=$(( $height + 1 ))
386bc4a673fSDevin Teske		fi
387bc4a673fSDevin Teske		# Add the disk to the list
388bc4a673fSDevin Teske		line="$line $disk"
389bc4a673fSDevin Teske	done
390bc4a673fSDevin Teske	# Append the left-overs
391bc4a673fSDevin Teske	if [ "${line#$prefix}" ]; then
392bc4a673fSDevin Teske		list="$list$line"
393bc4a673fSDevin Teske		height=$(( $height + 1 ))
394bc4a673fSDevin Teske	fi
395bc4a673fSDevin Teske
396bc4a673fSDevin Teske	# Add height for Xdialog(1)
397bc4a673fSDevin Teske	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
398bc4a673fSDevin Teske
399bc4a673fSDevin Teske	prompt=$( printf "$format" "$list" )
400bc4a673fSDevin Teske	f_dprintf "%s: Last Chance!" "$0"
401bc4a673fSDevin Teske	$DIALOG \
402bc4a673fSDevin Teske		--title "$title"        \
403bc4a673fSDevin Teske		--backtitle "$btitle"   \
404bc4a673fSDevin Teske		--hline "$hline"        \
405bc4a673fSDevin Teske		--$defaultno            \
406bc4a673fSDevin Teske		--$yes-label "$msg_yes" \
407bc4a673fSDevin Teske		--$no-label "$msg_no"   \
408bc4a673fSDevin Teske		$extra_args             \
409bc4a673fSDevin Teske		--yesno "$prompt" $height $width
410bc4a673fSDevin Teske}
411bc4a673fSDevin Teske
412bc4a673fSDevin Teske# dialog_menu_layout
413cd88b886SDevin Teske#
414bc4a673fSDevin Teske# Configure Virtual Device type and disks to use for the ZFS boot pool. User
415bc4a673fSDevin Teske# must select enough disks to satisfy the chosen vdev type.
416cd88b886SDevin Teske#
417bc4a673fSDevin Teskedialog_menu_layout()
418bc4a673fSDevin Teske{
419bc4a673fSDevin Teske	local funcname=dialog_menu_layout
420bc4a673fSDevin Teske	local title="$DIALOG_TITLE"
421bc4a673fSDevin Teske	local btitle="$DIALOG_BACKTITLE"
422bc4a673fSDevin Teske	local vdev_prompt="$msg_select_virtual_device_type"
423bc4a673fSDevin Teske	local disk_prompt="$msg_please_select_one_or_more_disks"
424bc4a673fSDevin Teske	local vdev_menu_list="
425bc4a673fSDevin Teske		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
426bc4a673fSDevin Teske		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
427bc4a673fSDevin Teske		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
428bc4a673fSDevin Teske		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
429bc4a673fSDevin Teske		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
430bc4a673fSDevin Teske	" # END-QUOTE
431bc4a673fSDevin Teske	local disk_check_list= # Calculated below
432bc4a673fSDevin Teske	local vdev_hline="$hline_arrows_tab_enter"
433bc4a673fSDevin Teske	local disk_hline="$hline_arrows_space_tab_enter"
434bc4a673fSDevin Teske
435bc4a673fSDevin Teske	# Warn the user if vdev type is not valid
436bc4a673fSDevin Teske	case "$ZFSBOOT_VDEV_TYPE" in
437bc4a673fSDevin Teske	stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
438bc4a673fSDevin Teske	*)
439bc4a673fSDevin Teske		f_dprintf "%s: Invalid virtual device type \`%s'" \
440bc4a673fSDevin Teske			  $funcname "$ZFSBOOT_VDEV_TYPE"
441bc4a673fSDevin Teske		f_show_err "$msg_invalid_virtual_device_type" \
442bc4a673fSDevin Teske			   "$ZFSBOOT_VDEV_TYPE"
443bc4a673fSDevin Teske		f_interactive || return $FAILURE
444bc4a673fSDevin Teske	esac
445bc4a673fSDevin Teske
446bc4a673fSDevin Teske	# Calculate size of vdev menu once only
447bc4a673fSDevin Teske	local vheight vwidth vrows
448bc4a673fSDevin Teske	eval f_dialog_menu_with_help_size vheight vwidth vrows \
449bc4a673fSDevin Teske		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
450bc4a673fSDevin Teske		$vdev_menu_list
451bc4a673fSDevin Teske
452bc4a673fSDevin Teske	# Get a list of probed disk devices
453bc4a673fSDevin Teske	local disks=
454cd88b886SDevin Teske	f_device_find "" $DEVICE_TYPE_DISK disks
455bc4a673fSDevin Teske	f_dprintf "$funcname: disks=[%s]" "$disks"
456cd88b886SDevin Teske	if [ ! "$disks" ]; then
457bc4a673fSDevin Teske		f_dprintf "No disk(s) present to configure"
458bc4a673fSDevin Teske		f_show_err "$msg_no_disks_present_to_configure"
459cd88b886SDevin Teske		return $FAILURE
460cd88b886SDevin Teske	fi
461cd88b886SDevin Teske
462cd88b886SDevin Teske	# Lets sort the disks array to be more user friendly
463cd88b886SDevin Teske	disks=$( echo "$disks" | tr ' ' '\n' | sort | tr '\n' ' ' )
464cd88b886SDevin Teske
465cd88b886SDevin Teske	#
466bc4a673fSDevin Teske	# Operate in a loop so we can (if interactive) repeat if not enough
467bc4a673fSDevin Teske	# disks are selected to satisfy the chosen vdev type or user wants to
468bc4a673fSDevin Teske	# back-up to the previous menu.
469cd88b886SDevin Teske	#
470bc4a673fSDevin Teske	local vardisk ndisks onoff selections vdev_choice
471bc4a673fSDevin Teske	while :; do
472cd88b886SDevin Teske		#
473bc4a673fSDevin Teske		# Confirm the vdev type that was selected
474cd88b886SDevin Teske		#
475bc4a673fSDevin Teske		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
476bc4a673fSDevin Teske			vdev_choice=$( eval $DIALOG \
477cd88b886SDevin Teske				--title \"\$title\"              \
478cd88b886SDevin Teske				--backtitle \"\$btitle\"         \
479bc4a673fSDevin Teske				--hline \"\$vdev_hline\"         \
480cd88b886SDevin Teske				--ok-label \"\$msg_ok\"          \
481cd88b886SDevin Teske				--cancel-label \"\$msg_cancel\"  \
482cd88b886SDevin Teske				--item-help                      \
483bc4a673fSDevin Teske				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
484bc4a673fSDevin Teske				--menu \"\$vdev_prompt\"         \
485bc4a673fSDevin Teske				$vheight $vwidth $vrows          \
486bc4a673fSDevin Teske				$vdev_menu_list                  \
487cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
488bc4a673fSDevin Teske			) || return $?
489bc4a673fSDevin Teske				# Exit if user pressed ESC or chose Cancel/No
490bc4a673fSDevin Teske			f_dialog_data_sanitize vdev_choice
491cd88b886SDevin Teske
492bc4a673fSDevin Teske			ZFSBOOT_VDEV_TYPE="$vdev_choice"
493bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
494bc4a673fSDevin Teske			          "$ZFSBOOT_VDEV_TYPE"
495bc4a673fSDevin Teske		fi
496bc4a673fSDevin Teske
497bc4a673fSDevin Teske		# Determine the number of disks needed for this vdev type
498bc4a673fSDevin Teske		local want_disks=0
499bc4a673fSDevin Teske		case "$ZFSBOOT_VDEV_TYPE" in
500cd88b886SDevin Teske		stripe) want_disks=1 ;;
501cd88b886SDevin Teske		mirror) want_disks=2 ;;
502cd88b886SDevin Teske		raidz1) want_disks=3 ;;
503cd88b886SDevin Teske		raidz2) want_disks=4 ;;
504cd88b886SDevin Teske		raidz3) want_disks=5 ;;
505cd88b886SDevin Teske		esac
506bc4a673fSDevin Teske
507bc4a673fSDevin Teske		# Warn the user if any scripted disks are invalid
508bc4a673fSDevin Teske		local disk valid_disks=
509bc4a673fSDevin Teske		local all_valid=${ZFSBOOT_DISKS:+1} # optimism
510bc4a673fSDevin Teske		for disk in $ZFSBOOT_DISKS; do
511bc4a673fSDevin Teske			if f_struct device_$disk; then
512bc4a673fSDevin Teske				valid_disks="$valid_disks $disk"
513bc4a673fSDevin Teske				continue
514cd88b886SDevin Teske			fi
515bc4a673fSDevin Teske			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
516bc4a673fSDevin Teske			all_valid=
517bc4a673fSDevin Teske		done
518bc4a673fSDevin Teske		if [ ! "$all_valid" ]; then
519bc4a673fSDevin Teske			if [ "$ZFSBOOT_DISKS" ]; then
520bc4a673fSDevin Teske				f_show_err \
521bc4a673fSDevin Teske				    "$msg_missing_one_or_more_scripted_disks"
522bc4a673fSDevin Teske			else
523bc4a673fSDevin Teske				f_dprintf "No disks selected."
524bc4a673fSDevin Teske				f_interactive ||
525bc4a673fSDevin Teske					f_show_err "$msg_no_disks_selected"
526bc4a673fSDevin Teske			fi
527bc4a673fSDevin Teske			f_interactive || return $FAILURE
528bc4a673fSDevin Teske		fi
529bc4a673fSDevin Teske		ZFSBOOT_DISKS="${valid_disks# }"
530bc4a673fSDevin Teske
531bc4a673fSDevin Teske		#
532bc4a673fSDevin Teske		# Short-circuit if we're running non-interactively
533bc4a673fSDevin Teske		#
534bc4a673fSDevin Teske		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
535bc4a673fSDevin Teske			ndisks=$( set -- $ZFSBOOT_DISKS; echo $# )
536bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] && break # to success
537bc4a673fSDevin Teske
538bc4a673fSDevin Teske			# Not enough disks selected
539bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
540bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
541bc4a673fSDevin Teske			          "Not enough disks selected." \
542bc4a673fSDevin Teske				  $ndisks $want_disks
543bc4a673fSDevin Teske			f_interactive || return $FAILURE
544bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
545bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
546bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
547bc4a673fSDevin Teske				return $FAILURE
548bc4a673fSDevin Teske		fi
549bc4a673fSDevin Teske
550bc4a673fSDevin Teske		#
551bc4a673fSDevin Teske		# Confirm the disks that were selected
552bc4a673fSDevin Teske		# Loop until the user cancels or selects enough disks
553bc4a673fSDevin Teske		#
554bc4a673fSDevin Teske		local breakout=
555bc4a673fSDevin Teske		while :; do
556bc4a673fSDevin Teske			# Loop over list of available disks, resetting state
557bc4a673fSDevin Teske			for disk in $disks; do unset _${disk}_status; done
558bc4a673fSDevin Teske
559bc4a673fSDevin Teske			# Loop over list of selected disks and create temporary
560bc4a673fSDevin Teske			# locals to map statuses onto up-to-date list of disks
561bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
562bc4a673fSDevin Teske				local _${disk}_status=on
563bc4a673fSDevin Teske			done
564bc4a673fSDevin Teske
565bc4a673fSDevin Teske			# Create the checklist menu of discovered disk devices
566bc4a673fSDevin Teske			disk_check_list=
567bc4a673fSDevin Teske			for disk in $disks; do
568bc4a673fSDevin Teske				local desc=
569bc4a673fSDevin Teske				device_$disk get desc desc
570bc4a673fSDevin Teske				f_shell_escape "$desc" desc
571bc4a673fSDevin Teske				f_getvar _${disk}_status:-off onoff
572bc4a673fSDevin Teske				disk_check_list="$disk_check_list
573bc4a673fSDevin Teske					$disk '$desc' $onoff"
574bc4a673fSDevin Teske			done
575bc4a673fSDevin Teske
576bc4a673fSDevin Teske			local height width rows
577bc4a673fSDevin Teske			eval f_dialog_checklist_size height width rows \
578bc4a673fSDevin Teske				\"\$title\" \"\$btitle\" \"\$prompt\" \
579bc4a673fSDevin Teske				\"\$hline\" $disk_check_list
580bc4a673fSDevin Teske
581bc4a673fSDevin Teske			selections=$( eval $DIALOG \
582bc4a673fSDevin Teske				--title \"\$DIALOG_TITLE\"         \
583bc4a673fSDevin Teske				--backtitle \"\$DIALOG_BACKTITLE\" \
584bc4a673fSDevin Teske				--hline \"\$hline\"                \
585bc4a673fSDevin Teske				--ok-label \"\$msg_ok\"            \
586bc4a673fSDevin Teske				--cancel-label \"\$msg_back\"      \
587bc4a673fSDevin Teske				--checklist \"\$prompt\"           \
588bc4a673fSDevin Teske				$height $width $rows               \
589bc4a673fSDevin Teske				$disk_check_list                   \
590bc4a673fSDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
591bc4a673fSDevin Teske			) || break
592bc4a673fSDevin Teske				# Loop if user pressed ESC or chose Cancel/No
593bc4a673fSDevin Teske			f_dialog_data_sanitize selections
594bc4a673fSDevin Teske
595bc4a673fSDevin Teske			ZFSBOOT_DISKS="$selections"
596bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
597bc4a673fSDevin Teske			          "$ZFSBOOT_DISKS"
598bc4a673fSDevin Teske
599bc4a673fSDevin Teske			ndisks=$( set -- $ZFSBOOT_DISKS; echo $# )
600bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] &&
601bc4a673fSDevin Teske				breakout=break && break
602bc4a673fSDevin Teske
603bc4a673fSDevin Teske			# Not enough disks selected
604bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
605bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
606bc4a673fSDevin Teske			          "Not enough disks selected." \
607bc4a673fSDevin Teske			          $ndisks $want_disks
608bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
609bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
610bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
611bc4a673fSDevin Teske				break
612bc4a673fSDevin Teske		done
613bc4a673fSDevin Teske		[ "$breakout" = "break" ] && break
614bc4a673fSDevin Teske		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
615bc4a673fSDevin Teske	done
616bc4a673fSDevin Teske
617bc4a673fSDevin Teske	return $DIALOG_OK
618cd88b886SDevin Teske}
619cd88b886SDevin Teske
620cd88b886SDevin Teske# zfs_create_diskpart $disk $index
621cd88b886SDevin Teske#
622cd88b886SDevin Teske# For each block device to be used in the zpool, rather than just create the
623cd88b886SDevin Teske# zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
624cd88b886SDevin Teske# so we can have some real swap. This also provides wiggle room incase your
625cd88b886SDevin Teske# replacement drivers do not have the exact same sector counts.
626cd88b886SDevin Teske#
6277cae6aabSDevin Teske# NOTE: $swapsize and $bootsize should be defined by the calling function.
628a622223fSDevin Teske# NOTE: Sets $bootpart and $targetpart for the calling function.
629cd88b886SDevin Teske#
630cd88b886SDevin Teskezfs_create_diskpart()
631cd88b886SDevin Teske{
632cd88b886SDevin Teske	local funcname=zfs_create_diskpart
633bc4a673fSDevin Teske	local disk="$1" index="$2"
634cd88b886SDevin Teske
635cd88b886SDevin Teske	# Check arguments
636bc4a673fSDevin Teske	if [ ! "$disk" ]; then
637bc4a673fSDevin Teske		f_dprintf "$funcname: NULL disk argument"
638bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
639bc4a673fSDevin Teske			f_show_err "$msg_null_disk_argument"
640bc4a673fSDevin Teske		return $FAILURE
641bc4a673fSDevin Teske	fi
642bc4a673fSDevin Teske	if [ "${disk#*[$IFS]}" != "$disk" ]; then
643bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
644bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
645bc4a673fSDevin Teske			f_show_err "$msg_invalid_disk_argument" "$disk"
646bc4a673fSDevin Teske		return $FAILURE
647bc4a673fSDevin Teske	fi
648bc4a673fSDevin Teske	if [ ! "$index" ]; then
649bc4a673fSDevin Teske		f_dprintf "$funcname: NULL index argument"
650bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
651bc4a673fSDevin Teske			f_show_err "$msg_null_index_argument"
652bc4a673fSDevin Teske		return $FAILURE
653bc4a673fSDevin Teske	fi
654bc4a673fSDevin Teske	if ! f_isinteger "$index"; then
655bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
656bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
657bc4a673fSDevin Teske			f_show_err "$msg_invalid_index_argument" "$index"
658bc4a673fSDevin Teske		return $FAILURE
659bc4a673fSDevin Teske	fi
660bc4a673fSDevin Teske	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
661cd88b886SDevin Teske
662d4d729e4SDevin Teske	# Check for unknown partition scheme before proceeding further
663d4d729e4SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
664d4d729e4SDevin Teske	""|MBR|GPT) : known good ;;
665d4d729e4SDevin Teske	*)
666d4d729e4SDevin Teske		f_dprintf "$funcname: %s is an unsupported partition scheme" \
667d4d729e4SDevin Teske		          "$ZFSBOOT_PARTITION_SCHEME"
668d4d729e4SDevin Teske		msg_error="$msg_error: $funcname" f_show_err \
669d4d729e4SDevin Teske			"$msg_unsupported_partition_scheme" \
670d4d729e4SDevin Teske			"$ZFSBOOT_PARTITION_SCHEME"
671d4d729e4SDevin Teske		return $FAILURE
672d4d729e4SDevin Teske	esac
673d4d729e4SDevin Teske
674cd88b886SDevin Teske	#
675cd88b886SDevin Teske	# Destroy whatever partition layout is currently on disk.
676cd88b886SDevin Teske	# NOTE: `-F' required to destroy if partitions still exist.
677cd88b886SDevin Teske	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
678cd88b886SDevin Teske	#
679bc4a673fSDevin Teske	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
680a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
681a622223fSDevin Teske	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
682a622223fSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
683cd88b886SDevin Teske
684cd88b886SDevin Teske	# Make doubly-sure backup GPT is destroyed
685a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
686a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
687cd88b886SDevin Teske
6887cae6aabSDevin Teske	#
6897cae6aabSDevin Teske	# Enable boot pool if encryption is desired
6907cae6aabSDevin Teske	#
6917cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
692cd88b886SDevin Teske
693cd88b886SDevin Teske	#
694cd88b886SDevin Teske	# Lay down the desired type of partition scheme
695cd88b886SDevin Teske	#
696cd88b886SDevin Teske	local setsize mbrindex
697cd88b886SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
698bc4a673fSDevin Teske	""|GPT) f_dprintf "$funcname: Creating GPT layout..."
699cd88b886SDevin Teske		#
700cd88b886SDevin Teske		# 1. Create GPT layout using labels
701cd88b886SDevin Teske		#
702a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
703bc4a673fSDevin Teske		             return $FAILURE
704cd88b886SDevin Teske
705cd88b886SDevin Teske		#
706cd88b886SDevin Teske		# 2. Add small freebsd-boot partition labeled `boot#'
707cd88b886SDevin Teske		#
708bc4a673fSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_LABEL_WITH_SIZE" \
709a622223fSDevin Teske		             gptboot$index freebsd-boot 512k $disk ||
710cd88b886SDevin Teske		             return $FAILURE
711bc4a673fSDevin Teske		f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
712a622223fSDevin Teske		             /boot/pmbr /boot/gptzfsboot 1 $disk ||
713cd88b886SDevin Teske		             return $FAILURE
714cd88b886SDevin Teske
715a622223fSDevin Teske		# NB: zpool will use the `zfs#' GPT labels
716cd88b886SDevin Teske		bootpart=p2 targetpart=p2
7177cae6aabSDevin Teske		[ ${swapsize:-0} -gt 0 ] && targetpart=p3
718cd88b886SDevin Teske
7197cae6aabSDevin Teske		#
7207cae6aabSDevin Teske		# Prepare boot pool if enabled (e.g., for geli(8))
7217cae6aabSDevin Teske		#
7227cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
723cd88b886SDevin Teske			bootpart=p2 targetpart=p3
7247cae6aabSDevin Teske			[ ${swapsize:-0} -gt 0 ] && targetpart=p4
725bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
726a622223fSDevin Teske			             "$GPART_ADD_LABEL_WITH_SIZE" boot$index \
7277cae6aabSDevin Teske			             freebsd-zfs ${bootsize}b $disk ||
728bc4a673fSDevin Teske			             return $FAILURE
7297cae6aabSDevin Teske			# Pedantically nuke any old labels
730a622223fSDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
731a622223fSDevin Teske			                /dev/$disk$bootpart
7327cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
7337cae6aabSDevin Teske				# Pedantically detach targetpart for later
7347cae6aabSDevin Teske				f_eval_catch -d $funcname geli \
7357cae6aabSDevin Teske				                "$GELI_DETACH_F" \
736a622223fSDevin Teske				                /dev/$disk$targetpart
737cd88b886SDevin Teske			fi
7387cae6aabSDevin Teske		fi
739cd88b886SDevin Teske
740cd88b886SDevin Teske		#
7417cae6aabSDevin Teske		# 3. Add freebsd-swap partition labeled `swap#'
742cd88b886SDevin Teske		#
743bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
744bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
7457cae6aabSDevin Teske			             "$GPART_ADD_LABEL_WITH_SIZE" swap$index \
7467cae6aabSDevin Teske			             freebsd-swap ${swapsize}b $disk ||
747cd88b886SDevin Teske			             return $FAILURE
748*cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
749*cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
750*cf9c3e56SDevin Teske			                /dev/gpt/swap$index
751cd88b886SDevin Teske			# Update fstab(5)
752bc4a673fSDevin Teske			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
753a622223fSDevin Teske			             /dev/gpt/swap$index none swap sw 0 0 \
754a622223fSDevin Teske			             $BSDINSTALL_TMPETC/fstab ||
755bc4a673fSDevin Teske			             return $FAILURE
756cd88b886SDevin Teske		fi
7577cae6aabSDevin Teske
7587cae6aabSDevin Teske		#
7597cae6aabSDevin Teske		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
7607cae6aabSDevin Teske		#
7617cae6aabSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
7627cae6aabSDevin Teske		             zfs$index freebsd-zfs $disk || return $FAILURE
7637cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
7647cae6aabSDevin Teske		                /dev/$disk$targetpart
765cd88b886SDevin Teske		;;
766cd88b886SDevin Teske
767bc4a673fSDevin Teske	MBR) f_dprintf "$funcname: Creating MBR layout..."
768cd88b886SDevin Teske		#
769cd88b886SDevin Teske		# 1. Create MBR layout (no labels)
770cd88b886SDevin Teske		#
771a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
772bc4a673fSDevin Teske		             return $FAILURE
7732925848dSDevin Teske		f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
774a622223fSDevin Teske		             $disk || return $FAILURE
775cd88b886SDevin Teske
776cd88b886SDevin Teske		#
777cd88b886SDevin Teske		# 2. Add freebsd slice with all available space
778cd88b886SDevin Teske		#
779a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD" freebsd $disk ||
780bc4a673fSDevin Teske		             return $FAILURE
781a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
782bc4a673fSDevin Teske		             return $FAILURE
783a622223fSDevin Teske		# Pedantically nuke any old labels
784a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
785a622223fSDevin Teske		                /dev/${disk}s1
786a622223fSDevin Teske		# Pedantically nuke any old scheme
787a622223fSDevin Teske		f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
788cd88b886SDevin Teske
789cd88b886SDevin Teske		#
790bc4a673fSDevin Teske		# 3. Write BSD scheme to the freebsd slice
791cd88b886SDevin Teske		#
792a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
793bc4a673fSDevin Teske		             return $FAILURE
794cd88b886SDevin Teske
7957cae6aabSDevin Teske		# NB: zpool will use s1a (no labels)
7967cae6aabSDevin Teske		bootpart=s1a targetpart=s1d mbrindex=4
797cd88b886SDevin Teske
7987cae6aabSDevin Teske		#
7997cae6aabSDevin Teske		# Always prepare a boot pool on MBR
8007cae6aabSDevin Teske		#
8017cae6aabSDevin Teske		ZFSBOOT_BOOT_POOL=1
802bc4a673fSDevin Teske		f_eval_catch $funcname gpart \
803bc4a673fSDevin Teske		             "$GPART_ADD_INDEX_WITH_SIZE" \
8047cae6aabSDevin Teske		             1 freebsd-zfs ${bootsize}b ${disk}s1 ||
805bc4a673fSDevin Teske		             return $FAILURE
806a622223fSDevin Teske		# Pedantically nuke any old labels
807a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
808a622223fSDevin Teske		                /dev/$disk$bootpart
8097cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
810a622223fSDevin Teske			# Pedantically detach targetpart for later
811a622223fSDevin Teske			f_eval_catch -d $funcname geli \
812a622223fSDevin Teske			                "$GELI_DETACH_F" \
813a622223fSDevin Teske					/dev/$disk$targetpart
814cd88b886SDevin Teske		fi
815cd88b886SDevin Teske
816cd88b886SDevin Teske		#
8177cae6aabSDevin Teske		# 4. Add freebsd-swap partition
818cd88b886SDevin Teske		#
819bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
820bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
8217cae6aabSDevin Teske			             "$GPART_ADD_INDEX_WITH_SIZE" 2 \
8227cae6aabSDevin Teske			             freebsd-swap ${swapsize}b ${disk}s1 ||
823cd88b886SDevin Teske			             return $FAILURE
824*cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
825*cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
826*cf9c3e56SDevin Teske			                /dev/${disk}s1b
827cd88b886SDevin Teske			# Update fstab(5)
828bc4a673fSDevin Teske			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
829a622223fSDevin Teske			             /dev/${disk}s1b none swap sw 0 0 \
830a622223fSDevin Teske			             $BSDINSTALL_TMPETC/fstab ||
831bc4a673fSDevin Teske			             return $FAILURE
832cd88b886SDevin Teske		fi
8337cae6aabSDevin Teske
8347cae6aabSDevin Teske		#
8357cae6aabSDevin Teske		# 5. Add freebsd-zfs partition for zroot
8367cae6aabSDevin Teske		#
8377cae6aabSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
8387cae6aabSDevin Teske			     $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
8397cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
8407cae6aabSDevin Teske				/dev/$disk$targetpart # Pedantic
8417cae6aabSDevin Teske		f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
8427cae6aabSDevin Teske			     /boot/zfsboot /dev/${disk}s1 count=1 ||
8437cae6aabSDevin Teske			     return $FAILURE
844cd88b886SDevin Teske		;;
845cd88b886SDevin Teske
846cd88b886SDevin Teske	esac # $ZFSBOOT_PARTITION_SCHEME
847cd88b886SDevin Teske
848cd88b886SDevin Teske	return $SUCCESS
849cd88b886SDevin Teske}
850cd88b886SDevin Teske
851bc4a673fSDevin Teske# zfs_create_boot $poolname $vdev_type $disks ...
852cd88b886SDevin Teske#
853cd88b886SDevin Teske# Creates boot pool and dataset layout. Returns error if something goes wrong.
854cd88b886SDevin Teske# Errors are printed to stderr for collection and display.
855cd88b886SDevin Teske#
856cd88b886SDevin Teskezfs_create_boot()
857cd88b886SDevin Teske{
858cd88b886SDevin Teske	local funcname=zfs_create_boot
8597cae6aabSDevin Teske	local zroot_name="$1"
8607cae6aabSDevin Teske	local zroot_vdevtype="$2"
8617cae6aabSDevin Teske	local zroot_vdevs= # Calculated below
8627cae6aabSDevin Teske	local boot_vdevs= # Used for geli(8) and/or MBR layouts
863a622223fSDevin Teske	shift 2 # poolname vdev_type
864a622223fSDevin Teske	local disks="$*" disk
865a622223fSDevin Teske	local bootpart targetpart # Set by zfs_create_diskpart() below
866cd88b886SDevin Teske
867a622223fSDevin Teske	#
868cd88b886SDevin Teske	# Pedantic checks; should never be seen
869a622223fSDevin Teske	#
8707cae6aabSDevin Teske	if [ ! "$zroot_name" ]; then
871bc4a673fSDevin Teske		f_dprintf "$funcname: NULL poolname"
872bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
873bc4a673fSDevin Teske			f_show_err "$msg_null_poolname"
874cd88b886SDevin Teske		return $FAILURE
875cd88b886SDevin Teske	fi
876cd88b886SDevin Teske	if [ $# -lt 1 ]; then
877bc4a673fSDevin Teske		f_dprintf "$funcname: missing disk arguments"
878bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
879bc4a673fSDevin Teske			f_show_err "$msg_missing_disk_arguments"
880cd88b886SDevin Teske		return $FAILURE
881cd88b886SDevin Teske	fi
882bc4a673fSDevin Teske	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
8837cae6aabSDevin Teske	          "$zroot_name" "$zroot_vdevtype"
884cd88b886SDevin Teske
885a622223fSDevin Teske	#
886cd88b886SDevin Teske	# Initialize fstab(5)
887a622223fSDevin Teske	#
888bc4a673fSDevin Teske	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
889a622223fSDevin Teske	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
890bc4a673fSDevin Teske	             return $FAILURE
891bc4a673fSDevin Teske	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
892cd88b886SDevin Teske	             "# Device" Mountpoint FStype Options Dump "Pass#" \
893a622223fSDevin Teske	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
894cd88b886SDevin Teske
895a622223fSDevin Teske	#
896cd88b886SDevin Teske	# Expand SI units in desired sizes
897a622223fSDevin Teske	#
8987cae6aabSDevin Teske	f_dprintf "$funcname: Expanding supplied size values..."
8997cae6aabSDevin Teske	local swapsize bootsize
900bc4a673fSDevin Teske	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
901bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid swap size \`%s'" \
902bc4a673fSDevin Teske		          "$ZFSBOOT_SWAP_SIZE"
903bc4a673fSDevin Teske		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
904bc4a673fSDevin Teske		return $FAILURE
905bc4a673fSDevin Teske	fi
9067cae6aabSDevin Teske	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
9077cae6aabSDevin Teske		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
9087cae6aabSDevin Teske		          "$ZFSBOOT_BOOT_POOL_SIZE"
9097cae6aabSDevin Teske		f_show_err "$msg_invalid_boot_pool_size" \
9107cae6aabSDevin Teske		           "$ZFSBOOT_BOOT_POOL_SIZE"
911bc4a673fSDevin Teske		return $FAILURE
912bc4a673fSDevin Teske	fi
913bc4a673fSDevin Teske	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
914bc4a673fSDevin Teske	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
9157cae6aabSDevin Teske	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
9167cae6aabSDevin Teske	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
917cd88b886SDevin Teske
918a622223fSDevin Teske	#
9197cae6aabSDevin Teske	# Prepare the disks and build pool device list(s)
920a622223fSDevin Teske	#
921bc4a673fSDevin Teske	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
9227cae6aabSDevin Teske	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] &&
9237cae6aabSDevin Teske		f_dprintf "$funcname: With 4k alignment using gnop(8)..."
924cd88b886SDevin Teske	local n=0
925a622223fSDevin Teske	for disk in $disks; do
926cd88b886SDevin Teske		zfs_create_diskpart $disk $n || return $FAILURE
927a622223fSDevin Teske		# Now $bootpart and $targetpart are set (suffix for $disk)
928cd88b886SDevin Teske
9297cae6aabSDevin Teske		# Forced 4k alignment support using Geom NOP (see gnop(8))
930cd88b886SDevin Teske		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
9317cae6aabSDevin Teske			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9327cae6aabSDevin Teske				boot_vdevs="$boot_vdevs $disk$bootpart.nop"
933bc4a673fSDevin Teske				f_eval_catch $funcname gnop "$GNOP_CREATE" \
9347cae6aabSDevin Teske				             $disk$bootpart || return $FAILURE
935cd88b886SDevin Teske			fi
9367cae6aabSDevin Teske			# Don't gnop encrypted partition
9377cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
9387cae6aabSDevin Teske				zroot_vdevs="$zroot_vdevs $disk$targetpart.eli"
939cd88b886SDevin Teske			else
9407cae6aabSDevin Teske				zroot_vdevs="$zroot_vdevs $disk$targetpart.nop"
9417cae6aabSDevin Teske				f_eval_catch $funcname gnop "$GNOP_CREATE" \
9427cae6aabSDevin Teske					     $disk$targetpart ||
9437cae6aabSDevin Teske				             return $FAILURE
9447cae6aabSDevin Teske			fi
9457cae6aabSDevin Teske		else
9467cae6aabSDevin Teske			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9477cae6aabSDevin Teske				boot_vdevs="$boot_vdevs $disk$bootpart"
9487cae6aabSDevin Teske			fi
9497cae6aabSDevin Teske			zroot_vdevs="$zroot_vdevs $disk$targetpart"
9507cae6aabSDevin Teske		fi
9517cae6aabSDevin Teske
9527cae6aabSDevin Teske		n=$(( $n + 1 ))
9537cae6aabSDevin Teske	done # disks
9547cae6aabSDevin Teske
9557cae6aabSDevin Teske	#
9567cae6aabSDevin Teske	# If we need/want a boot pool, create it
9577cae6aabSDevin Teske	#
9587cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
9597cae6aabSDevin Teske		local bootpool_vdevtype= # Calculated below
9607cae6aabSDevin Teske		local bootpool_options= # Calculated below
9617cae6aabSDevin Teske		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
9627cae6aabSDevin Teske		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
9637cae6aabSDevin Teske		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
9647cae6aabSDevin Teske
9657cae6aabSDevin Teske		f_dprintf "$funcname: Setting up boot pool..."
966cd88b886SDevin Teske		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
9677cae6aabSDevin Teske			f_dprintf "$funcname: For encrypted root disk..."
968cd88b886SDevin Teske
9697cae6aabSDevin Teske		# Create parent directory for boot pool
970a622223fSDevin Teske		f_eval_catch -d $funcname umount "$UMOUNT" /mnt
971bc4a673fSDevin Teske		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
972a622223fSDevin Teske		             $BSDINSTALL_CHROOT || return $FAILURE
973cd88b886SDevin Teske
9747cae6aabSDevin Teske		# Create mirror across the boot partition on all disks
9757cae6aabSDevin Teske		[ $( set -- $boot_vdevs; echo $# ) -gt 1 ] &&
9767cae6aabSDevin Teske			bootpool_vdevtype=mirror
977cd88b886SDevin Teske
9787cae6aabSDevin Teske		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
9797cae6aabSDevin Teske		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
980bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
9817cae6aabSDevin Teske		             "$bootpool_options" "$bootpool_name" \
9827cae6aabSDevin Teske		             $bootpool_vdevtype $boot_vdevs || return $FAILURE
98345df402eSDevin Teske
9847cae6aabSDevin Teske		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
985cd88b886SDevin Teske		             return $FAILURE
986cd88b886SDevin Teske
9877cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
988cd88b886SDevin Teske			# Generate an encryption key using random(4)
989bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
9907cae6aabSDevin Teske				     /dev/random "$bootpool/$zroot_key" \
9917cae6aabSDevin Teske			             "bs=4096 count=1" || return $FAILURE
9927cae6aabSDevin Teske		else
9937cae6aabSDevin Teske			# Clean up
9947cae6aabSDevin Teske			f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
9957cae6aabSDevin Teske			             "$bootpool_name" || return $FAILURE
9967cae6aabSDevin Teske			f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
9977cae6aabSDevin Teske		fi
9987cae6aabSDevin Teske
9997cae6aabSDevin Teske	fi
1000cd88b886SDevin Teske
1001a622223fSDevin Teske	#
1002cd88b886SDevin Teske	# Create the geli(8) GEOMS
1003a622223fSDevin Teske	#
10047cae6aabSDevin Teske	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
10057cae6aabSDevin Teske		# Prompt user for password (twice)
1006bc4a673fSDevin Teske		if ! msg_enter_new_password="$msg_geli_password" \
1007bc4a673fSDevin Teske			f_dialog_input_password
1008bc4a673fSDevin Teske		then
1009bc4a673fSDevin Teske			f_dprintf "$funcname: User cancelled"
1010bc4a673fSDevin Teske			f_show_err "$msg_user_cancelled"
1011bc4a673fSDevin Teske			return $FAILURE
1012bc4a673fSDevin Teske		fi
1013a622223fSDevin Teske
1014a622223fSDevin Teske		# Initialize geli(8) on each of the target partitions
1015bc4a673fSDevin Teske		for disk in $disks; do
1016cd88b886SDevin Teske			f_dialog_info "$msg_geli_setup" \
1017cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1018a622223fSDevin Teske			if ! echo "$pw_password" | f_eval_catch \
1019a622223fSDevin Teske				$funcname geli "$GELI_PASSWORD_INIT" \
10207cae6aabSDevin Teske				"$bootpool/boot/$disk$targetpart.eli" \
10217cae6aabSDevin Teske				AES-XTS "$bootpool/$zroot_key" \
1022a622223fSDevin Teske				$disk$targetpart
1023bc4a673fSDevin Teske			then
1024bc4a673fSDevin Teske				f_interactive || f_die
1025a622223fSDevin Teske				unset pw_password # Sensitive info
1026cd88b886SDevin Teske				return $FAILURE
1027bc4a673fSDevin Teske			fi
1028a622223fSDevin Teske			if ! echo "$pw_password" | f_eval_catch \
1029a622223fSDevin Teske				$funcname geli "$GELI_ATTACH" \
10307cae6aabSDevin Teske				"$bootpool/$zroot_key" $disk$targetpart
1031bc4a673fSDevin Teske			then
1032bc4a673fSDevin Teske				f_interactive || f_die
1033a622223fSDevin Teske				unset pw_password # Sensitive info
1034bc4a673fSDevin Teske				return $FAILURE
1035bc4a673fSDevin Teske			fi
1036cd88b886SDevin Teske		done
1037a622223fSDevin Teske		unset pw_password # Sensitive info
1038a622223fSDevin Teske
1039a622223fSDevin Teske		# Clean up
10407cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
10417cae6aabSDevin Teske			return $FAILURE
1042a622223fSDevin Teske		f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1043cd88b886SDevin Teske	fi
1044cd88b886SDevin Teske
1045cd88b886SDevin Teske	#
1046a622223fSDevin Teske	# Create the ZFS root pool with desired type and disk devices
1047cd88b886SDevin Teske	#
10487cae6aabSDevin Teske	f_dprintf "$funcname: Creating root pool..."
1049bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1050a622223fSDevin Teske	             "-o altroot=$BSDINSTALL_CHROOT -m none -f" \
10517cae6aabSDevin Teske	             "$zroot_name" $zroot_vdevtype $zroot_vdevs
1052cd88b886SDevin Teske
10537cae6aabSDevin Teske	# Customize the zroot a bit...
1054bc4a673fSDevin Teske	local option
10557cae6aabSDevin Teske	f_dprintf "$funcname: Setting miscellaneous options on root pool..."
1056ca2d97c2SDevin Teske	for option in atime=off; do
10577cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_SET" $option "$zroot_name" ||
1058bc4a673fSDevin Teske			return $FAILURE
1059bc4a673fSDevin Teske	done
1060cd88b886SDevin Teske
1061cd88b886SDevin Teske	#
10627cae6aabSDevin Teske	# Create ZFS dataset layout within the new root pool
1063cd88b886SDevin Teske	#
1064bc4a673fSDevin Teske	f_dprintf "$funcname: Creating ZFS datasets..."
1065cd88b886SDevin Teske	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1066cd88b886SDevin Teske		# Skip blank lines and comments
1067cd88b886SDevin Teske		case "$dataset" in "#"*|"") continue; esac
1068cd88b886SDevin Teske		# Remove potential inline comments in options
1069cd88b886SDevin Teske		options="${options%%#*}"
1070cd88b886SDevin Teske		# Replace tabs with spaces
1071cd88b886SDevin Teske		f_replaceall "$options" "	" " " options
1072cd88b886SDevin Teske		# Reduce contiguous runs of space to one single space
1073cd88b886SDevin Teske		oldoptions=
1074cd88b886SDevin Teske		while [ "$oldoptions" != "$options" ]; do
1075cd88b886SDevin Teske			oldoptions="$options"
1076cd88b886SDevin Teske			f_replaceall "$options" "  " " " options
1077cd88b886SDevin Teske		done
1078cd88b886SDevin Teske		# Replace both commas and spaces with ` -o '
1079cd88b886SDevin Teske		f_replaceall "$options" "[ ,]" " -o " options
1080cd88b886SDevin Teske		# Create the dataset with desired options
1081bc4a673fSDevin Teske		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
10827cae6aabSDevin Teske		             "${options:+-o $options}" "$zroot_name$dataset" ||
1083cd88b886SDevin Teske		             return $FAILURE
1084cd88b886SDevin Teske	done
1085cd88b886SDevin Teske
1086cd88b886SDevin Teske	# Touch up permissions on the tmp directories
1087bc4a673fSDevin Teske	f_dprintf "$funcname: Modifying directory permissions..."
1088bc4a673fSDevin Teske	local dir
1089bc4a673fSDevin Teske	for dir in /tmp /var/tmp; do
1090bc4a673fSDevin Teske		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1091a622223fSDevin Teske		             $BSDINSTALL_CHROOTDIR$dir || return $FAILURE
1092bc4a673fSDevin Teske	done
1093cd88b886SDevin Teske
1094cd88b886SDevin Teske	# Create symlink(s)
10957cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
10967cae6aabSDevin Teske		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
10977cae6aabSDevin Teske		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1098a622223fSDevin Teske		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1099bc4a673fSDevin Teske	fi
1100cd88b886SDevin Teske
1101cd88b886SDevin Teske	# Set bootfs property
11027cae6aabSDevin Teske	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1103bc4a673fSDevin Teske	f_dprintf "$funcname: Setting bootfs property..."
1104bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
11057cae6aabSDevin Teske		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
11067cae6aabSDevin Teske		return $FAILURE
1107cd88b886SDevin Teske
1108cd88b886SDevin Teske	# Export the pool(s)
1109bc4a673fSDevin Teske	f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
11107cae6aabSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1111bc4a673fSDevin Teske	             return $FAILURE
11127cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1113bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
11147cae6aabSDevin Teske		             "$bootpool_name" || return $FAILURE
1115bc4a673fSDevin Teske	fi
1116cd88b886SDevin Teske
1117cd88b886SDevin Teske	# Destroy the gnop devices (if enabled)
1118bc4a673fSDevin Teske	for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$disks}; do
11197cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
1120a622223fSDevin Teske			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1121a622223fSDevin Teske			                $disk$bootpart.nop
11227cae6aabSDevin Teske		fi
11237cae6aabSDevin Teske		if [ ! "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1124a622223fSDevin Teske			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1125a622223fSDevin Teske			                $disk$targetpart.nop
1126cd88b886SDevin Teske		fi
1127cd88b886SDevin Teske	done
1128cd88b886SDevin Teske
11297cae6aabSDevin Teske	# MBR boot loader touch-up
1130cd88b886SDevin Teske	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1131bc4a673fSDevin Teske		f_dprintf "$funcname: Updating MBR boot loader on disks..."
1132cd88b886SDevin Teske		# Stick the ZFS boot loader in the "convienient hole" after
1133cd88b886SDevin Teske		# the ZFS internal metadata
1134bc4a673fSDevin Teske		for disk in $disks; do
1135bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1136a622223fSDevin Teske			             /boot/zfsboot /dev/$disk$bootpart \
1137bc4a673fSDevin Teske			             "skip=1 seek=1024" || return $FAILURE
1138cd88b886SDevin Teske		done
1139cd88b886SDevin Teske	fi
1140cd88b886SDevin Teske
1141cd88b886SDevin Teske	# Re-import the ZFS pool(s)
1142bc4a673fSDevin Teske	f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1143bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
11447cae6aabSDevin Teske	             "-o altroot=\"$BSDINSTALL_CHROOT\"" "$zroot_name" ||
1145bc4a673fSDevin Teske	             return $FAILURE
11467cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1147bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
11487cae6aabSDevin Teske		             "-o altroot=\"$BSDINSTALL_CHROOT\"" \
11497cae6aabSDevin Teske		             "$bootpool_name" || return $FAILURE
1150bc4a673fSDevin Teske	fi
1151cd88b886SDevin Teske
1152cd88b886SDevin Teske	# While this is apparently not needed, it seems to help MBR
1153bc4a673fSDevin Teske	f_dprintf "$funcname: Configuring zpool.cache..."
1154a622223fSDevin Teske	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1155bc4a673fSDevin Teske	             return $FAILURE
1156bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1157a622223fSDevin Teske	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
11587cae6aabSDevin Teske	             "$zroot_name" || return $FAILURE
1159cd88b886SDevin Teske
116013d8e1ceSDevin Teske	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1161cd88b886SDevin Teske	# NOTE: We later concatenate these into their destination
1162bc4a673fSDevin Teske	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1163bc4a673fSDevin Teske	          "$funcname"
1164bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1165a622223fSDevin Teske	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1166bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_load=\"YES\"' \
1167a622223fSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1168ee482f2cSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" \
1169ee482f2cSDevin Teske	             'kern.geom.label.disk_ident.enable=\"0\"' \
1170ee482f2cSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1171cd88b886SDevin Teske
11727cae6aabSDevin Teske	# We're all done unless we should go on for boot pool
11737cae6aabSDevin Teske	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1174cd88b886SDevin Teske
1175bc4a673fSDevin Teske	# Some additional geli(8) requirements for loader.conf(5)
1176bc4a673fSDevin Teske	for option in \
1177a622223fSDevin Teske		'zpool_cache_load=\"YES\"' \
1178a622223fSDevin Teske		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1179a622223fSDevin Teske		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1180bc4a673fSDevin Teske	; do
1181a622223fSDevin Teske		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1182a622223fSDevin Teske		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1183bc4a673fSDevin Teske		             return $FAILURE
1184bc4a673fSDevin Teske	done
11857cae6aabSDevin Teske	f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
11867cae6aabSDevin Teske	    "\"zfs:$zroot_name/$zroot_bootfs\"" \
11877cae6aabSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
11887cae6aabSDevin Teske
11897cae6aabSDevin Teske	# We're all done unless we should go on to do encryption
11907cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
119113d8e1ceSDevin Teske
1192cd88b886SDevin Teske	#
1193cd88b886SDevin Teske	# Configure geli(8)-based encryption
1194cd88b886SDevin Teske	#
1195bc4a673fSDevin Teske	f_dprintf "$funcname: Configuring disk encryption..."
1196bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1197a622223fSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1198bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1199a622223fSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1200bc4a673fSDevin Teske	for disk in $disks; do
1201bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1202a622223fSDevin Teske			geli_%s_keyfile0_load "$disk$targetpart YES" \
1203a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1204cd88b886SDevin Teske			return $FAILURE
1205bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1206bc4a673fSDevin Teske			geli_%s_keyfile0_type \
1207a622223fSDevin Teske			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1208a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1209cd88b886SDevin Teske			return $FAILURE
1210bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1211bc4a673fSDevin Teske			geli_%s_keyfile0_name \
1212a622223fSDevin Teske			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1213a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1214cd88b886SDevin Teske			return $FAILURE
1215cd88b886SDevin Teske	done
1216cd88b886SDevin Teske
1217cd88b886SDevin Teske	return $SUCCESS
1218cd88b886SDevin Teske}
1219cd88b886SDevin Teske
1220cd88b886SDevin Teske# dialog_menu_diskinfo
1221cd88b886SDevin Teske#
1222cd88b886SDevin Teske# Prompt the user to select a disk and then provide detailed info on it.
1223cd88b886SDevin Teske#
1224cd88b886SDevin Teskedialog_menu_diskinfo()
1225cd88b886SDevin Teske{
1226cd88b886SDevin Teske	local disk
1227cd88b886SDevin Teske
1228cd88b886SDevin Teske	#
1229cd88b886SDevin Teske	# Break from loop when user cancels disk selection
1230cd88b886SDevin Teske	#
1231cd88b886SDevin Teske	while :; do
1232cd88b886SDevin Teske		disk=$( msg_cancel="$msg_back" f_device_menu \
1233cd88b886SDevin Teske			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1234cd88b886SDevin Teske			$DEVICE_TYPE_DISK 2>&1 ) || break
1235cd88b886SDevin Teske
1236cd88b886SDevin Teske		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1237cd88b886SDevin Teske		f_show_msg "$msg_detailed_disk_info" \
1238cd88b886SDevin Teske			"$disk" "$( gpart show $disk 2> /dev/null )" \
1239cd88b886SDevin Teske			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1240cd88b886SDevin Teske			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1241cd88b886SDevin Teske	done
1242cd88b886SDevin Teske
1243cd88b886SDevin Teske	return $SUCCESS
1244cd88b886SDevin Teske}
1245cd88b886SDevin Teske
1246cd88b886SDevin Teske############################################################ MAIN
1247cd88b886SDevin Teske
1248cd88b886SDevin Teske#
1249cd88b886SDevin Teske# Initialize
1250cd88b886SDevin Teske#
1251cd88b886SDevin Teskef_dialog_title "$msg_zfs_configuration"
1252cd88b886SDevin Teskef_dialog_backtitle "$msg_freebsd_installer"
1253cd88b886SDevin Teske
1254cd88b886SDevin Teske# User may have specifically requested ZFS-related operations be interactive
1255cd88b886SDevin Teske! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1256cd88b886SDevin Teske
1257cd88b886SDevin Teske#
1258bc4a673fSDevin Teske# Debugging
1259bc4a673fSDevin Teske#
1260bc4a673fSDevin Teskef_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1261bc4a673fSDevin Teskef_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1262a622223fSDevin Teskef_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1263bc4a673fSDevin Teske
1264bc4a673fSDevin Teske#
1265cd88b886SDevin Teske# Loop over the main menu until we've accomplished what we came here to do
1266cd88b886SDevin Teske#
1267cd88b886SDevin Teskewhile :; do
1268cd88b886SDevin Teske	if ! f_interactive; then
1269cd88b886SDevin Teske		retval=$DIALOG_OK
1270bc4a673fSDevin Teske		mtag=">>> $msg_install"
1271cd88b886SDevin Teske	else
1272cd88b886SDevin Teske		dialog_menu_main
1273cd88b886SDevin Teske		retval=$?
1274cd88b886SDevin Teske		f_dialog_menutag_fetch mtag
1275cd88b886SDevin Teske	fi
1276cd88b886SDevin Teske
1277bc4a673fSDevin Teske	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1278cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] || f_die
1279cd88b886SDevin Teske
1280cd88b886SDevin Teske	case "$mtag" in
1281bc4a673fSDevin Teske	">>> $msg_install")
1282cd88b886SDevin Teske		#
1283cd88b886SDevin Teske		# First, validate the user's selections
1284cd88b886SDevin Teske		#
1285cd88b886SDevin Teske
1286cd88b886SDevin Teske		# Make sure they gave us a name for the pool
1287cd88b886SDevin Teske		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1288bc4a673fSDevin Teske			f_dprintf "Pool name cannot be empty."
1289bc4a673fSDevin Teske			f_show_err "$msg_pool_name_cannot_be_empty"
1290cd88b886SDevin Teske			continue
1291cd88b886SDevin Teske		fi
1292bc4a673fSDevin Teske
1293bc4a673fSDevin Teske		# Validate vdev type against number of disks selected/scripted
1294bc4a673fSDevin Teske		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1295bc4a673fSDevin Teske		# NB: dialog_menu_layout supports running non-interactively
1296bc4a673fSDevin Teske		dialog_menu_layout || continue
1297bc4a673fSDevin Teske
1298cd88b886SDevin Teske		# Make sure each disk will be at least 50% ZFS
1299cd88b886SDevin Teske		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
13007cae6aabSDevin Teske		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize
1301cd88b886SDevin Teske		then
1302cd88b886SDevin Teske			minsize=$swapsize teeny_disks=
13037cae6aabSDevin Teske			[ "$ZFSBOOT_BOOT_POOL" ] &&
13047cae6aabSDevin Teske				minsize=$(( $minsize + $bootsize ))
1305bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
1306cd88b886SDevin Teske				device_$disk get capacity disksize || continue
1307cd88b886SDevin Teske				disksize=$(( $disksize - $minsize ))
1308cd88b886SDevin Teske				[ $disksize -lt $minsize ] &&
1309cd88b886SDevin Teske					teeny_disks="$teeny_disks $disk"
1310cd88b886SDevin Teske			done
1311cd88b886SDevin Teske			if [ "$teeny_disks" ]; then
13127cae6aabSDevin Teske				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1313bc4a673fSDevin Teske				          "$ZFSBOOT_SWAP_SIZE" \
13147cae6aabSDevin Teske				          "$ZFSBOOT_BOOT_POOL_SIZE" \
13157cae6aabSDevin Teske				          "minsize=[$minsize]"
1316bc4a673fSDevin Teske				f_dprintf "These disks are too small: %s" \
1317bc4a673fSDevin Teske				          "$teeny_disks"
1318bc4a673fSDevin Teske				f_show_err "$msg_these_disks_are_too_small" \
1319cd88b886SDevin Teske				           "$ZFSBOOT_SWAP_SIZE" \
13207cae6aabSDevin Teske				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1321cd88b886SDevin Teske				           "$teeny_disks"
1322cd88b886SDevin Teske				continue
1323cd88b886SDevin Teske			fi
1324cd88b886SDevin Teske		fi
1325cd88b886SDevin Teske
1326cd88b886SDevin Teske		#
1327cd88b886SDevin Teske		# Last Chance!
1328cd88b886SDevin Teske		#
1329bc4a673fSDevin Teske		if f_interactive; then
1330bc4a673fSDevin Teske			dialog_last_chance $ZFSBOOT_DISKS || continue
1331cd88b886SDevin Teske		fi
1332cd88b886SDevin Teske
1333cd88b886SDevin Teske		#
1334cd88b886SDevin Teske		# Let's do this
1335cd88b886SDevin Teske		#
1336cd88b886SDevin Teske
1337cd88b886SDevin Teske		vdev_type="$ZFSBOOT_VDEV_TYPE"
1338cd88b886SDevin Teske
1339cd88b886SDevin Teske		# Blank the vdev type for the default layout
1340cd88b886SDevin Teske		[ "$vdev_type" = "stripe" ] && vdev_type=
1341cd88b886SDevin Teske
1342bc4a673fSDevin Teske		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1343bc4a673fSDevin Teske		                "$vdev_type" $ZFSBOOT_DISKS || continue
1344cd88b886SDevin Teske
1345cd88b886SDevin Teske		break # to success
1346cd88b886SDevin Teske		;;
1347bc4a673fSDevin Teske	?" $msg_pool_type_disks")
1348bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=1
1349bc4a673fSDevin Teske		dialog_menu_layout
1350bc4a673fSDevin Teske		# User has poked settings, disable later confirmation
1351bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=
1352bc4a673fSDevin Teske		;;
1353cd88b886SDevin Teske	"- $msg_rescan_devices") f_device_rescan ;;
1354cd88b886SDevin Teske	"- $msg_disk_info") dialog_menu_diskinfo ;;
1355cd88b886SDevin Teske	?" $msg_pool_name")
1356cd88b886SDevin Teske		# Prompt the user to input/change the name for the new pool
1357cd88b886SDevin Teske		f_dialog_input input \
1358cd88b886SDevin Teske			"$msg_please_enter_a_name_for_your_zpool" \
1359cd88b886SDevin Teske			"$ZFSBOOT_POOL_NAME" &&
1360cd88b886SDevin Teske			ZFSBOOT_POOL_NAME="$input"
1361cd88b886SDevin Teske		;;
1362cd88b886SDevin Teske	?" $msg_force_4k_sectors")
1363cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1364cd88b886SDevin Teske		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1365cd88b886SDevin Teske			ZFSBOOT_GNOP_4K_FORCE_ALIGN=
1366cd88b886SDevin Teske		else
1367cd88b886SDevin Teske			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1368cd88b886SDevin Teske		fi
1369cd88b886SDevin Teske		;;
1370bc4a673fSDevin Teske	?" $msg_encrypt_disks")
1371cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1372cd88b886SDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1373cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=
1374cd88b886SDevin Teske		else
1375cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=1
1376cd88b886SDevin Teske		fi
1377cd88b886SDevin Teske		;;
1378cd88b886SDevin Teske	?" $msg_partition_scheme")
1379cd88b886SDevin Teske		# Toggle between GPT and MBR
1380cd88b886SDevin Teske		if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
1381cd88b886SDevin Teske			ZFSBOOT_PARTITION_SCHEME=MBR
1382cd88b886SDevin Teske		else
1383cd88b886SDevin Teske			ZFSBOOT_PARTITION_SCHEME=GPT
1384cd88b886SDevin Teske		fi
1385cd88b886SDevin Teske		;;
1386cd88b886SDevin Teske	?" $msg_swap_size")
1387cd88b886SDevin Teske		# Prompt the user to input/change the swap size for each disk
1388cd88b886SDevin Teske		f_dialog_input input \
1389cd88b886SDevin Teske			"$msg_please_enter_amount_of_swap_space" \
1390cd88b886SDevin Teske			"$ZFSBOOT_SWAP_SIZE" &&
139130c8ebe9SDevin Teske			ZFSBOOT_SWAP_SIZE="${input:-0}"
1392cd88b886SDevin Teske		;;
1393cd88b886SDevin Teske	esac
1394cd88b886SDevin Teskedone
1395cd88b886SDevin Teske
1396cd88b886SDevin Teskereturn $SUCCESS
1397cd88b886SDevin Teske
1398cd88b886SDevin Teske################################################################################
1399cd88b886SDevin Teske# END
1400cd88b886SDevin Teske################################################################################
1401