xref: /freebsd/usr.sbin/bsdinstall/scripts/zfsboot (revision 16045420e7f83489ecd5e2163aa9bb11236962f5)
1cd88b886SDevin Teske#!/bin/sh
2cd88b886SDevin Teske#-
3a1b8f55cSAllan Jude# Copyright (c) 2013-2016 Allan Jude
4ee21b0efSDevin Teske# Copyright (c) 2013-2018 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#
29cd88b886SDevin Teske############################################################ INCLUDES
30cd88b886SDevin Teske
31cd88b886SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32cd88b886SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
33cd88b886SDevin Teskef_dprintf "%s: loading includes..." "$0"
34cd88b886SDevin Teskef_include $BSDCFG_SHARE/device.subr
35cd88b886SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
36cd88b886SDevin Teskef_include $BSDCFG_SHARE/password/password.subr
37cd88b886SDevin Teskef_include $BSDCFG_SHARE/variable.subr
38cd88b886SDevin Teske
39cd88b886SDevin Teske############################################################ CONFIGURATION
40cd88b886SDevin Teske
41cd88b886SDevin Teske#
42cd88b886SDevin Teske# Default name of the boot-pool
43cd88b886SDevin Teske#
44cd88b886SDevin Teske: ${ZFSBOOT_POOL_NAME:=zroot}
45cd88b886SDevin Teske
46cd88b886SDevin Teske#
477ef92163SJohn Ko# Default pool size is optional
487ef92163SJohn Ko#
497ef92163SJohn Ko: ${ZFSBOOT_POOL_SIZE=}
507ef92163SJohn Ko
517ef92163SJohn Ko#
5247206692SDevin Teske# Default options to use when creating zroot pool
5347206692SDevin Teske#
54*16045420SLexi Winter: ${ZFSBOOT_POOL_CREATE_OPTIONS:=-O compression=on -O atime=off}
5547206692SDevin Teske
5647206692SDevin Teske#
57cd88b886SDevin Teske# Default name for the boot environment parent dataset
58cd88b886SDevin Teske#
5967635c19SDevin Teske: ${ZFSBOOT_BEROOT_NAME:=ROOT}
60cd88b886SDevin Teske
61cd88b886SDevin Teske#
625e38260cSBaptiste Daroussin# Default name for the primary boot environment
63cd88b886SDevin Teske#
64cd88b886SDevin Teske: ${ZFSBOOT_BOOTFS_NAME:=default}
65cd88b886SDevin Teske
66cd88b886SDevin Teske#
67cd88b886SDevin Teske# Default Virtual Device (vdev) type to create
68cd88b886SDevin Teske#
69cd88b886SDevin Teske: ${ZFSBOOT_VDEV_TYPE:=stripe}
70cd88b886SDevin Teske
71cd88b886SDevin Teske#
72b4843bd6SSteven Hartland# Should we use sysctl(8) vfs.zfs.min_auto_ashift=12 to force 4K sectors?
73cd88b886SDevin Teske#
74de82aed1SJohn Baldwin: ${ZFSBOOT_FORCE_4K_SECTORS=1}
75cd88b886SDevin Teske
76cd88b886SDevin Teske#
77cd88b886SDevin Teske# Should we use geli(8) to encrypt the drives?
787cae6aabSDevin Teske# NB: Automatically enables ZFSBOOT_BOOT_POOL
79cd88b886SDevin Teske#
80bc4a673fSDevin Teske: ${ZFSBOOT_GELI_ENCRYPTION=}
81cd88b886SDevin Teske
82cd88b886SDevin Teske#
83cd88b886SDevin Teske# Default path to the geli(8) keyfile used in drive encryption
84cd88b886SDevin Teske#
85cd88b886SDevin Teske: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
86cd88b886SDevin Teske
87cd88b886SDevin Teske#
887cae6aabSDevin Teske# Create a separate boot pool?
89ee110941SEd Maste# NB: Automatically set when using geli(8)
907cae6aabSDevin Teske#
917cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL=}
927cae6aabSDevin Teske
937cae6aabSDevin Teske#
9447206692SDevin Teske# Options to use when creating separate boot pool (if any)
9547206692SDevin Teske#
9647206692SDevin Teske: ${ZFSBOOT_BOOT_POOL_CREATE_OPTIONS:=}
9747206692SDevin Teske
9847206692SDevin Teske#
99ee110941SEd Maste# Default name for boot pool when enabled (e.g., geli(8))
1007cae6aabSDevin Teske#
1017cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
1027cae6aabSDevin Teske
1037cae6aabSDevin Teske#
104ee110941SEd Maste# Default size for boot pool when enabled (e.g., geli(8))
1057cae6aabSDevin Teske#
1067cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
1077cae6aabSDevin Teske
1087cae6aabSDevin Teske#
109cd88b886SDevin Teske# Default disks to use (always empty unless being scripted)
110cd88b886SDevin Teske#
111cd88b886SDevin Teske: ${ZFSBOOT_DISKS:=}
112cd88b886SDevin Teske
113cd88b886SDevin Teske#
114cd88b886SDevin Teske# Default partitioning scheme to use on disks
115cd88b886SDevin Teske#
1160edc8cc8SAllan Jude: ${ZFSBOOT_PARTITION_SCHEME:=}
1170edc8cc8SAllan Jude
1180edc8cc8SAllan Jude#
1194d41f472SAllan Jude# Default boot type to use on disks
1200edc8cc8SAllan Jude#
1210edc8cc8SAllan Jude: ${ZFSBOOT_BOOT_TYPE:=}
122cd88b886SDevin Teske
123cd88b886SDevin Teske#
124f69afe42SGraham Perrin# How much swap to put on each block device in the boot pool
125cd88b886SDevin Teske# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
126cd88b886SDevin Teske#
127cd88b886SDevin Teske: ${ZFSBOOT_SWAP_SIZE:=2g}
128cd88b886SDevin Teske
129cd88b886SDevin Teske#
1302875e59fSOllivier Robert# Should we use geli(8) to encrypt the swap?
1312875e59fSOllivier Robert#
1322875e59fSOllivier Robert: ${ZFSBOOT_SWAP_ENCRYPTION=}
1332875e59fSOllivier Robert
1342875e59fSOllivier Robert#
1352875e59fSOllivier Robert# Should we use gmirror(8) to mirror the swap?
1362875e59fSOllivier Robert#
1372875e59fSOllivier Robert: ${ZFSBOOT_SWAP_MIRROR=}
1382875e59fSOllivier Robert
1392875e59fSOllivier Robert#
140f69afe42SGraham Perrin# Default ZFS datasets for root pool
141cd88b886SDevin Teske#
1425bf3ac7aSMark Johnston# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME
143cd88b886SDevin Teske# NOTE: Anything after pound/hash character [#] is ignored as a comment.
144cd88b886SDevin Teske#
145cd88b886SDevin Teskef_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
146cd88b886SDevin Teske	# DATASET	OPTIONS (comma or space separated; or both)
147cd88b886SDevin Teske
148cd88b886SDevin Teske	# Boot Environment [BE] root and default boot dataset
149cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
150dcf36219SAllan Jude	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
151cd88b886SDevin Teske
1523bb92304SMike Karels	# Home directories separated so they are common to all BEs
1533bb92304SMike Karels	/home		mountpoint=/home
1543bb92304SMike Karels
155cd88b886SDevin Teske	# Compress /tmp, allow exec but not setuid
156ed8690e3SOllivier Robert	/tmp		mountpoint=/tmp,exec=on,setuid=off
157cd88b886SDevin Teske
158cd88b886SDevin Teske	# Don't mount /usr so that 'base' files go to the BEROOT
159cd88b886SDevin Teske	/usr		mountpoint=/usr,canmount=off
160cd88b886SDevin Teske
161cd88b886SDevin Teske	# Ports tree
162ed8690e3SOllivier Robert	/usr/ports	setuid=off
163cd88b886SDevin Teske
164cd88b886SDevin Teske	# Source tree (compressed)
165ed8690e3SOllivier Robert	/usr/src
166cd88b886SDevin Teske
167cd88b886SDevin Teske	# Create /var and friends
1681aec0f4fSAllan Jude	/var		mountpoint=/var,canmount=off
1691bc08043SAllan Jude	/var/audit	exec=off,setuid=off
170ed8690e3SOllivier Robert	/var/crash	exec=off,setuid=off
171ed8690e3SOllivier Robert	/var/log	exec=off,setuid=off
172ed8690e3SOllivier Robert	/var/mail	atime=on
173ed8690e3SOllivier Robert	/var/tmp	setuid=off
174cd88b886SDevin Teske" # END-QUOTE
175cd88b886SDevin Teske
176bc4a673fSDevin Teske#
177bc4a673fSDevin Teske# If interactive and the user has not explicitly chosen a vdev type or disks,
178bc4a673fSDevin Teske# make the user confirm scripted/default choices when proceeding to install.
179bc4a673fSDevin Teske#
180bc4a673fSDevin Teske: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
181bc4a673fSDevin Teske
182cd88b886SDevin Teske############################################################ GLOBALS
183cd88b886SDevin Teske
184cd88b886SDevin Teske#
185bc4a673fSDevin Teske# Format of a line in printf(1) syntax to add to fstab(5)
186bc4a673fSDevin Teske#
187bc4a673fSDevin TeskeFSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
188bc4a673fSDevin Teske
189bc4a673fSDevin Teske#
190bc4a673fSDevin Teske# Command strings for various tasks
191bc4a673fSDevin Teske#
192aa2a0e0fSAllan JudeCOPY='cp "%s" "%s"'
193bc4a673fSDevin TeskeCHMOD_MODE='chmod %s "%s"'
194bc4a673fSDevin TeskeDD_WITH_OPTIONS='dd if="%s" of="%s" %s'
195bc4a673fSDevin TeskeECHO_APPEND='echo "%s" >> "%s"'
196aa2a0e0fSAllan JudeECHO_OVERWRITE='echo "%s" > "%s"'
197bc4a673fSDevin TeskeGELI_ATTACH='geli attach -j - -k "%s" "%s"'
198a1b8f55cSAllan JudeGELI_ATTACH_NOKEY='geli attach -j - "%s"'
199a622223fSDevin TeskeGELI_DETACH_F='geli detach -f "%s"'
200bc4a673fSDevin TeskeGELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
201a1b8f55cSAllan JudeGELI_PASSWORD_GELIBOOT_INIT='geli init -bg -e %s -J - -l 256 -s 4096 "%s"'
20269e4b249SAllan JudeGPART_ADD_ALIGN='gpart add %s -t %s "%s"'
20369e4b249SAllan JudeGPART_ADD_ALIGN_INDEX='gpart add %s -i %s -t %s "%s"'
20469e4b249SAllan JudeGPART_ADD_ALIGN_INDEX_WITH_SIZE='gpart add %s -i %s -t %s -s %s "%s"'
20569e4b249SAllan JudeGPART_ADD_ALIGN_LABEL='gpart add %s -l %s -t %s "%s"'
20669e4b249SAllan JudeGPART_ADD_ALIGN_LABEL_WITH_SIZE='gpart add %s -l %s -t %s -s %s "%s"'
207bc4a673fSDevin TeskeGPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
208bc4a673fSDevin TeskeGPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
209bc4a673fSDevin TeskeGPART_CREATE='gpart create -s %s "%s"'
210a622223fSDevin TeskeGPART_DESTROY_F='gpart destroy -F "%s"'
211bc4a673fSDevin TeskeGPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
2127059fa6fSAllan JudeGPART_SET_LENOVOFIX='gpart set -a lenovofix "%s"'
2137059fa6fSAllan JudeGPART_SET_PMBR_ACTIVE='gpart set -a active "%s"'
214a622223fSDevin TeskeGRAID_DELETE='graid delete "%s"'
215a1b8f55cSAllan JudeKLDLOAD='kldload %s'
216bc4a673fSDevin TeskeLN_SF='ln -sf "%s" "%s"'
217bc4a673fSDevin TeskeMKDIR_P='mkdir -p "%s"'
218bc4a673fSDevin TeskeMOUNT_TYPE='mount -t %s "%s" "%s"'
2190b7472b3SNathan WhitehornNEWFS_ESP='newfs_msdos "%s"'
220bc4a673fSDevin TeskePRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
221bc4a673fSDevin TeskePRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
222bc4a673fSDevin TeskeSHELL_TRUNCATE=':> "%s"'
2232875e59fSOllivier RobertSWAP_GMIRROR_LABEL='gmirror label swap %s'
224b4843bd6SSteven HartlandSYSCTL_ZFS_MIN_ASHIFT_12='sysctl vfs.zfs.min_auto_ashift=12'
225a622223fSDevin TeskeUMOUNT='umount "%s"'
226bc4a673fSDevin TeskeZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
227ca13d5d2SAllan JudeZFS_MOUNT='zfs mount "%s"'
228bc4a673fSDevin TeskeZFS_SET='zfs set "%s" "%s"'
229bc4a673fSDevin TeskeZFS_UNMOUNT='zfs unmount "%s"'
230bc4a673fSDevin TeskeZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
231a88393ceSDevin TeskeZPOOL_DESTROY='zpool destroy "%s"'
232bc4a673fSDevin TeskeZPOOL_EXPORT='zpool export "%s"'
2331b63cafbSAllan JudeZPOOL_EXPORT_F='zpool export -f "%s"'
234bc4a673fSDevin TeskeZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
235a622223fSDevin TeskeZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
236bc4a673fSDevin TeskeZPOOL_SET='zpool set %s "%s"'
237bc4a673fSDevin Teske
238bc4a673fSDevin Teske#
239cd88b886SDevin Teske# Strings that should be moved to an i18n file and loaded with f_include_lang()
240cd88b886SDevin Teske#
241cd88b886SDevin Teskehline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
242cd88b886SDevin Teskehline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
243cd88b886SDevin Teskehline_arrows_tab_enter="Press arrows, TAB or ENTER"
244bc4a673fSDevin Teskemsg_an_unknown_error_occurred="An unknown error occurred"
245cd88b886SDevin Teskemsg_back="Back"
246cd88b886SDevin Teskemsg_cancel="Cancel"
247bc4a673fSDevin Teskemsg_change_selection="Change Selection"
248cd88b886SDevin Teskemsg_configure_options="Configure Options:"
249cd88b886SDevin 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"
250cd88b886SDevin Teskemsg_disk_info="Disk Info"
251cd88b886SDevin Teskemsg_disk_info_help="Get detailed information on disk device(s)"
252695a612fSOllivier Robertmsg_disk_plural="disks"
2531b41374fSDevin Teskemsg_disk_singular="disk"
254bc4a673fSDevin Teskemsg_encrypt_disks="Encrypt Disks?"
255bc4a673fSDevin Teskemsg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
256bc4a673fSDevin Teskemsg_error="Error"
257cd88b886SDevin Teskemsg_force_4k_sectors="Force 4K Sectors?"
25869e4b249SAllan Judemsg_force_4k_sectors_help="Align partitions to 4K sector boundries and set vfs.zfs.min_auto_ashift=12"
259cc42ef53SBrad Davismsg_freebsd_installer="$OSNAME Installer"
260cd88b886SDevin 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"
2617a434c5cSDevin Teskemsg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
262bc4a673fSDevin Teskemsg_install="Install"
263bc4a673fSDevin Teskemsg_install_desc="Proceed with Installation"
264bc4a673fSDevin Teskemsg_install_help="Create ZFS boot pool with displayed options"
2657cae6aabSDevin Teskemsg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
266bc4a673fSDevin Teskemsg_invalid_disk_argument="Invalid disk argument \`%s'"
267bc4a673fSDevin Teskemsg_invalid_index_argument="Invalid index argument \`%s'"
2687ef92163SJohn Komsg_invalid_pool_size="Invalid pool size \`%s'"
269bc4a673fSDevin Teskemsg_invalid_swap_size="Invalid swap size \`%s'"
270cd88b886SDevin Teskemsg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
271bc4a673fSDevin 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"
272bc4a673fSDevin 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'
273cd88b886SDevin Teskemsg_mirror_desc="Mirror - n-Way Mirroring"
274cd88b886SDevin Teskemsg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
275bc4a673fSDevin Teskemsg_missing_disk_arguments="missing disk arguments"
276bc4a673fSDevin Teskemsg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
277cd88b886SDevin Teskemsg_no="NO"
278cd88b886SDevin Teskemsg_no_disks_present_to_configure="No disk(s) present to configure"
279cd88b886SDevin Teskemsg_no_disks_selected="No disks selected."
280bc4a673fSDevin Teskemsg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
281bc4a673fSDevin Teskemsg_null_disk_argument="NULL disk argument"
282bc4a673fSDevin Teskemsg_null_index_argument="NULL index argument"
283bc4a673fSDevin Teskemsg_null_poolname="NULL poolname"
284bb4fd0a0SAllan Judemsg_odd_disk_selected="An even number of disks must be selected to create a RAID 1+0. (%u selected)"
285cd88b886SDevin Teskemsg_ok="OK"
286cd88b886SDevin Teskemsg_partition_scheme="Partition Scheme"
2877059fa6fSAllan Judemsg_partition_scheme_help="Select partitioning scheme. GPT is recommended."
288f69afe42SGraham Perrinmsg_please_enter_a_name_for_your_pool="Please enter a name for your pool:"
289cd88b886SDevin Teskemsg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
290f69afe42SGraham Perrinmsg_please_select_one_or_more_disks="Please select one or more disks to create a pool:"
291cd88b886SDevin Teskemsg_pool_name="Pool Name"
292cd88b886SDevin Teskemsg_pool_name_cannot_be_empty="Pool name cannot be empty."
293f69afe42SGraham Perrinmsg_pool_name_help="Customize the name of the pool to be created (Required)"
294bc4a673fSDevin Teskemsg_pool_type_disks="Pool Type/Disks:"
295bc4a673fSDevin Teskemsg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
296cd88b886SDevin Teskemsg_processing_selection="Processing selection..."
297bb4fd0a0SAllan Judemsg_raid10_desc="RAID 1+0 - n x 2-Way Mirrors"
298bb4fd0a0SAllan Judemsg_raid10_help="[4+ Disks] Striped Mirrors provides the best performance, but the least storage"
299cd88b886SDevin Teskemsg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
300cd88b886SDevin Teskemsg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
301cd88b886SDevin Teskemsg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
302cd88b886SDevin Teskemsg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
303cd88b886SDevin Teskemsg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
304cd88b886SDevin Teskemsg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
305cd88b886SDevin Teskemsg_rescan_devices="Rescan Devices"
306cd88b886SDevin Teskemsg_rescan_devices_help="Scan for device changes"
307cd88b886SDevin Teskemsg_select="Select"
308cd88b886SDevin Teskemsg_select_a_disk_device="Select a disk device"
309cd88b886SDevin Teskemsg_select_virtual_device_type="Select Virtual Device type:"
310cd88b886SDevin Teskemsg_stripe_desc="Stripe - No Redundancy"
311cd88b886SDevin Teskemsg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
3122875e59fSOllivier Robertmsg_swap_encrypt="Encrypt Swap?"
3132875e59fSOllivier Robertmsg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
314169ff110SAllan Judemsg_swap_invalid="The selected swap size (%s) is invalid. Enter a number optionally followed by units. Example: 2G"
3152875e59fSOllivier Robertmsg_swap_mirror="Mirror Swap?"
3162875e59fSOllivier Robertmsg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
317cd88b886SDevin Teskemsg_swap_size="Swap Size"
318cd88b886SDevin Teskemsg_swap_size_help="Customize how much swap space is allocated to each selected disk"
31965308195SMateusz Guzikmsg_swap_toosmall="The selected swap size (%s) is too small. Please enter a value greater than 100MB or enter 0 for no swap"
3206203e46bSAllan Judemsg_these_disks_are_too_small="These disks are smaller than the amount of requested\nswap (%s) and/or geli(8) (%s) partitions, which would\ntake 100%% or more of each of the following selected disks:\n\n  %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of disks."
321bc4a673fSDevin Teskemsg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
322bc4a673fSDevin Teskemsg_unsupported_partition_scheme="%s is an unsupported partition scheme"
323bc4a673fSDevin Teskemsg_user_cancelled="User Cancelled."
324cd88b886SDevin Teskemsg_yes="YES"
325cd88b886SDevin Teskemsg_zfs_configuration="ZFS Configuration"
326f69afe42SGraham Perrinmsg_please_enter_options_for_your_pool="Please enter options for your pool"
327ca7e12f7SCraig Leresmsg_zfs_options_name="ZFS Pool Options"
328f69afe42SGraham Perrinmsg_zfs_options_name_help="Customize ZFS options for the pool to be created"
329cd88b886SDevin Teske
330cd88b886SDevin Teske############################################################ FUNCTIONS
331cd88b886SDevin Teske
332cd88b886SDevin Teske# dialog_menu_main
333cd88b886SDevin Teske#
334cd88b886SDevin Teske# Display the dialog(1)-based application main menu.
335cd88b886SDevin Teske#
336cd88b886SDevin Teskedialog_menu_main()
337cd88b886SDevin Teske{
338cd88b886SDevin Teske	local title="$DIALOG_TITLE"
339cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
340cd88b886SDevin Teske	local prompt="$msg_configure_options"
341cd88b886SDevin Teske	local force4k="$msg_no"
342cd88b886SDevin Teske	local usegeli="$msg_no"
3432875e59fSOllivier Robert	local swapgeli="$msg_no"
3442875e59fSOllivier Robert	local swapmirror="$msg_no"
345b4843bd6SSteven Hartland	[ "$ZFSBOOT_FORCE_4K_SECTORS" ] && force4k="$msg_yes"
346cd88b886SDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
3472875e59fSOllivier Robert	[ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
3482875e59fSOllivier Robert	[ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
349695a612fSOllivier Robert	local disks n disks_grammar
350a88393ceSDevin Teske	f_count n $ZFSBOOT_DISKS
351695a612fSOllivier Robert	{ [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
352695a612fSOllivier Robert		disks_grammar=$msg_disk_plural # grammar
353cd88b886SDevin Teske	local menu_list="
354bc4a673fSDevin Teske		'>>> $msg_install'      '$msg_install_desc'
355bc4a673fSDevin Teske		                        '$msg_install_help'
35633112d9eSDevin Teske		'T $msg_pool_type_disks'
35733112d9eSDevin Teske		               '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
358bc4a673fSDevin Teske		               '$msg_pool_type_disks_help'
359cd88b886SDevin Teske		'- $msg_rescan_devices' '*'
360cd88b886SDevin Teske		                        '$msg_rescan_devices_help'
361cd88b886SDevin Teske		'- $msg_disk_info'      '*'
362cd88b886SDevin Teske		                        '$msg_disk_info_help'
363bc4a673fSDevin Teske		'N $msg_pool_name'      '$ZFSBOOT_POOL_NAME'
364cd88b886SDevin Teske		                        '$msg_pool_name_help'
36533112d9eSDevin Teske		'4 $msg_force_4k_sectors'
36633112d9eSDevin Teske		                        '$force4k'
367cd88b886SDevin Teske		                        '$msg_force_4k_sectors_help'
368bc4a673fSDevin Teske		'E $msg_encrypt_disks'  '$usegeli'
369bc4a673fSDevin Teske		                        '$msg_encrypt_disks_help'
37033112d9eSDevin Teske		'P $msg_partition_scheme'
3710edc8cc8SAllan Jude		               '$ZFSBOOT_PARTITION_SCHEME ($ZFSBOOT_BOOT_TYPE)'
372cd88b886SDevin Teske		               '$msg_partition_scheme_help'
373bc4a673fSDevin Teske		'S $msg_swap_size'      '$ZFSBOOT_SWAP_SIZE'
374cd88b886SDevin Teske		                        '$msg_swap_size_help'
3752875e59fSOllivier Robert		'M $msg_swap_mirror'    '$swapmirror'
3762875e59fSOllivier Robert		                        '$msg_swap_mirror_help'
3772875e59fSOllivier Robert		'W $msg_swap_encrypt'   '$swapgeli'
3782875e59fSOllivier Robert		                        '$msg_swap_encrypt_help'
379ca7e12f7SCraig Leres		'O $msg_zfs_options_name'   '$ZFSBOOT_POOL_CREATE_OPTIONS'
380ca7e12f7SCraig Leres		                        '$msg_zfs_options_name_help'
381cd88b886SDevin Teske	" # END-QUOTE
382cd88b886SDevin Teske	local defaultitem= # Calculated below
383cd88b886SDevin Teske	local hline="$hline_alnum_arrows_punc_tab_enter"
384cd88b886SDevin Teske
385cd88b886SDevin Teske	local height width rows
386cd88b886SDevin Teske	eval f_dialog_menu_with_help_size height width rows \
387cd88b886SDevin Teske		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
388cd88b886SDevin Teske
389cd88b886SDevin Teske	# Obtain default-item from previously stored selection
390cd88b886SDevin Teske	f_dialog_default_fetch defaultitem
391cd88b886SDevin Teske
392cd88b886SDevin Teske	local menu_choice
393cd88b886SDevin Teske	menu_choice=$( eval $DIALOG \
394cd88b886SDevin Teske		--title \"\$title\"              \
395cd88b886SDevin Teske		--backtitle \"\$btitle\"         \
396cd88b886SDevin Teske		--hline \"\$hline\"              \
397cd88b886SDevin Teske		--item-help                      \
398cd88b886SDevin Teske		--ok-label \"\$msg_select\"      \
399cd88b886SDevin Teske		--cancel-label \"\$msg_cancel\"  \
400cd88b886SDevin Teske		--default-item \"\$defaultitem\" \
401cd88b886SDevin Teske		--menu \"\$prompt\"              \
402cd88b886SDevin Teske		$height $width $rows             \
4038df9efe8SAlfonso S. Siciliano		${USE_DIALOG:+--} $menu_list     \
404cd88b886SDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
405cd88b886SDevin Teske	)
406cd88b886SDevin Teske	local retval=$?
407cd88b886SDevin Teske	f_dialog_data_sanitize menu_choice
408cd88b886SDevin Teske	f_dialog_menutag_store "$menu_choice"
409cd88b886SDevin Teske
410cd88b886SDevin Teske	# Only update default-item on success
411cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
412cd88b886SDevin Teske
413cd88b886SDevin Teske	return $retval
414cd88b886SDevin Teske}
415cd88b886SDevin Teske
416bc4a673fSDevin Teske# dialog_last_chance $disks ...
417cd88b886SDevin Teske#
418bc4a673fSDevin Teske# Display a list of the disks that the user is about to destroy. The default
419bc4a673fSDevin Teske# action is to return error status unless the user explicitly (non-default)
420bc4a673fSDevin Teske# selects "Yes" from the noyes dialog.
421cd88b886SDevin Teske#
422bc4a673fSDevin Teskedialog_last_chance()
423cd88b886SDevin Teske{
424cd88b886SDevin Teske	local title="$DIALOG_TITLE"
425cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
426bc4a673fSDevin Teske	local prompt # Calculated below
427bc4a673fSDevin Teske	local hline="$hline_arrows_tab_enter"
428cd88b886SDevin Teske
429bc4a673fSDevin Teske	local height=8 width=50 prefix="   "
430bc4a673fSDevin Teske	local plen=${#prefix} list= line=
431bc4a673fSDevin Teske	local max_width=$(( $width - 3 - $plen ))
432bc4a673fSDevin Teske
433bc4a673fSDevin Teske	local yes no defaultno extra_args format
434bc4a673fSDevin Teske	if [ "$USE_XDIALOG" ]; then
435bc4a673fSDevin Teske		yes=ok no=cancel defaultno=default-no
436bc4a673fSDevin Teske		extra_args="--wrap --left"
437bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure"
438bc4a673fSDevin Teske	else
439bc4a673fSDevin Teske		yes=yes no=no defaultno=defaultno
440bc4a673fSDevin Teske		extra_args="--colors --cr-wrap"
441bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure_color"
442bc4a673fSDevin Teske	fi
443bc4a673fSDevin Teske
444bc4a673fSDevin Teske	local disk line_width
445bc4a673fSDevin Teske	for disk in $*; do
446bc4a673fSDevin Teske		if [ "$line" ]; then
447bc4a673fSDevin Teske			line_width=${#line}
448bc4a673fSDevin Teske		else
449bc4a673fSDevin Teske			line_width=$plen
450bc4a673fSDevin Teske		fi
451bc4a673fSDevin Teske		line_width=$(( $line_width + 1 + ${#disk} ))
452bc4a673fSDevin Teske		# Add newline before disk if it would exceed max_width
453bc4a673fSDevin Teske		if [ $line_width -gt $max_width ]; then
454bc4a673fSDevin Teske			list="$list$line\n"
455bc4a673fSDevin Teske			line="$prefix"
456bc4a673fSDevin Teske			height=$(( $height + 1 ))
457bc4a673fSDevin Teske		fi
458bc4a673fSDevin Teske		# Add the disk to the list
459bc4a673fSDevin Teske		line="$line $disk"
460bc4a673fSDevin Teske	done
461bc4a673fSDevin Teske	# Append the left-overs
462bc4a673fSDevin Teske	if [ "${line#$prefix}" ]; then
463bc4a673fSDevin Teske		list="$list$line"
464bc4a673fSDevin Teske		height=$(( $height + 1 ))
465bc4a673fSDevin Teske	fi
466bc4a673fSDevin Teske
467bc4a673fSDevin Teske	# Add height for Xdialog(1)
468bc4a673fSDevin Teske	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
469bc4a673fSDevin Teske
470bc4a673fSDevin Teske	prompt=$( printf "$format" "$list" )
471bc4a673fSDevin Teske	f_dprintf "%s: Last Chance!" "$0"
472bc4a673fSDevin Teske	$DIALOG \
473bc4a673fSDevin Teske		--title "$title"        \
474bc4a673fSDevin Teske		--backtitle "$btitle"   \
475bc4a673fSDevin Teske		--hline "$hline"        \
476bc4a673fSDevin Teske		--$defaultno            \
477bc4a673fSDevin Teske		--$yes-label "$msg_yes" \
478bc4a673fSDevin Teske		--$no-label "$msg_no"   \
479bc4a673fSDevin Teske		$extra_args             \
480bc4a673fSDevin Teske		--yesno "$prompt" $height $width
481bc4a673fSDevin Teske}
482bc4a673fSDevin Teske
483bc4a673fSDevin Teske# dialog_menu_layout
484cd88b886SDevin Teske#
485bc4a673fSDevin Teske# Configure Virtual Device type and disks to use for the ZFS boot pool. User
486bc4a673fSDevin Teske# must select enough disks to satisfy the chosen vdev type.
487cd88b886SDevin Teske#
488bc4a673fSDevin Teskedialog_menu_layout()
489bc4a673fSDevin Teske{
490bc4a673fSDevin Teske	local funcname=dialog_menu_layout
491bc4a673fSDevin Teske	local title="$DIALOG_TITLE"
492bc4a673fSDevin Teske	local btitle="$DIALOG_BACKTITLE"
493bc4a673fSDevin Teske	local vdev_prompt="$msg_select_virtual_device_type"
494bc4a673fSDevin Teske	local disk_prompt="$msg_please_select_one_or_more_disks"
495bc4a673fSDevin Teske	local vdev_menu_list="
496bc4a673fSDevin Teske		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
497bc4a673fSDevin Teske		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
498bb4fd0a0SAllan Jude		'raid10' '$msg_raid10_desc' '$msg_raid10_help'
499bc4a673fSDevin Teske		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
500bc4a673fSDevin Teske		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
501bc4a673fSDevin Teske		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
502bc4a673fSDevin Teske	" # END-QUOTE
503bc4a673fSDevin Teske	local disk_check_list= # Calculated below
504bc4a673fSDevin Teske	local vdev_hline="$hline_arrows_tab_enter"
505bc4a673fSDevin Teske	local disk_hline="$hline_arrows_space_tab_enter"
506bc4a673fSDevin Teske
507bc4a673fSDevin Teske	# Warn the user if vdev type is not valid
508bc4a673fSDevin Teske	case "$ZFSBOOT_VDEV_TYPE" in
509bb4fd0a0SAllan Jude	stripe|mirror|raid10|raidz1|raidz2|raidz3) : known good ;;
510bc4a673fSDevin Teske	*)
511bc4a673fSDevin Teske		f_dprintf "%s: Invalid virtual device type \`%s'" \
512bc4a673fSDevin Teske			  $funcname "$ZFSBOOT_VDEV_TYPE"
513bc4a673fSDevin Teske		f_show_err "$msg_invalid_virtual_device_type" \
514bc4a673fSDevin Teske			   "$ZFSBOOT_VDEV_TYPE"
515bc4a673fSDevin Teske		f_interactive || return $FAILURE
516bc4a673fSDevin Teske	esac
517bc4a673fSDevin Teske
518bc4a673fSDevin Teske	# Calculate size of vdev menu once only
519bc4a673fSDevin Teske	local vheight vwidth vrows
520bc4a673fSDevin Teske	eval f_dialog_menu_with_help_size vheight vwidth vrows \
521bc4a673fSDevin Teske		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
522bc4a673fSDevin Teske		$vdev_menu_list
523bc4a673fSDevin Teske
524bc4a673fSDevin Teske	# Get a list of probed disk devices
525bc4a673fSDevin Teske	local disks=
526a88393ceSDevin Teske	debug= f_device_find "" $DEVICE_TYPE_DISK disks
527a88393ceSDevin Teske
528a88393ceSDevin Teske	# Prune out mounted md(4) devices that may be part of the boot process
529a88393ceSDevin Teske	local disk name new_list=
530a88393ceSDevin Teske	for disk in $disks; do
531a88393ceSDevin Teske		debug= $disk get name name
532a88393ceSDevin Teske		case "$name" in
533a88393ceSDevin Teske		md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
534a88393ceSDevin Teske		esac
535a88393ceSDevin Teske		new_list="$new_list $disk"
536a88393ceSDevin Teske	done
537a88393ceSDevin Teske	disks="${new_list# }"
538a88393ceSDevin Teske
539be0d16b0SBrad Davis	# Prune out disks that are not available to install to
540be0d16b0SBrad Davis	local avail_disks=
541be0d16b0SBrad Davis	for disk in $disks; do
542be0d16b0SBrad Davis		debug= $disk get name name
543be0d16b0SBrad Davis		geom disk list $name | awk '$1 == "Mode:" && $2 != "r0w0e0" { exit 1 }'
544be0d16b0SBrad Davis		[ $? -eq 0 ] && avail_disks="$avail_disks $disk"
545be0d16b0SBrad Davis	done
546be0d16b0SBrad Davis	disks="${avail_disks# }"
547be0d16b0SBrad Davis
548a88393ceSDevin Teske	# Debugging
549a88393ceSDevin Teske	if [ "$debug" ]; then
550a88393ceSDevin Teske		local disk_names=
551a88393ceSDevin Teske		for disk in $disks; do
552a88393ceSDevin Teske			debug= $disk get name name
553a88393ceSDevin Teske			disk_names="$disk_names $name"
554a88393ceSDevin Teske		done
555a88393ceSDevin Teske		f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
556a88393ceSDevin Teske	fi
557a88393ceSDevin Teske
558cd88b886SDevin Teske	if [ ! "$disks" ]; then
559bc4a673fSDevin Teske		f_dprintf "No disk(s) present to configure"
560bc4a673fSDevin Teske		f_show_err "$msg_no_disks_present_to_configure"
561cd88b886SDevin Teske		return $FAILURE
562cd88b886SDevin Teske	fi
563cd88b886SDevin Teske
564cd88b886SDevin Teske	# Lets sort the disks array to be more user friendly
565a88393ceSDevin Teske	f_device_sort_by name disks disks
566cd88b886SDevin Teske
567cd88b886SDevin Teske	#
568bc4a673fSDevin Teske	# Operate in a loop so we can (if interactive) repeat if not enough
569bc4a673fSDevin Teske	# disks are selected to satisfy the chosen vdev type or user wants to
570bc4a673fSDevin Teske	# back-up to the previous menu.
571cd88b886SDevin Teske	#
572a88393ceSDevin Teske	local vardisk ndisks onoff selections vdev_choice breakout device
573a88393ceSDevin Teske	local valid_disks all_valid want_disks desc height width rows
574bc4a673fSDevin Teske	while :; do
575cd88b886SDevin Teske		#
576bc4a673fSDevin Teske		# Confirm the vdev type that was selected
577cd88b886SDevin Teske		#
578bc4a673fSDevin Teske		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
579bc4a673fSDevin Teske			vdev_choice=$( eval $DIALOG \
580cd88b886SDevin Teske				--title \"\$title\"              \
581cd88b886SDevin Teske				--backtitle \"\$btitle\"         \
582bc4a673fSDevin Teske				--hline \"\$vdev_hline\"         \
583cd88b886SDevin Teske				--ok-label \"\$msg_ok\"          \
584cd88b886SDevin Teske				--cancel-label \"\$msg_cancel\"  \
585cd88b886SDevin Teske				--item-help                      \
586bc4a673fSDevin Teske				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
587bc4a673fSDevin Teske				--menu \"\$vdev_prompt\"         \
588bc4a673fSDevin Teske				$vheight $vwidth $vrows          \
589bc4a673fSDevin Teske				$vdev_menu_list                  \
590cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
591bc4a673fSDevin Teske			) || return $?
592bc4a673fSDevin Teske				# Exit if user pressed ESC or chose Cancel/No
593bc4a673fSDevin Teske			f_dialog_data_sanitize vdev_choice
594cd88b886SDevin Teske
595bc4a673fSDevin Teske			ZFSBOOT_VDEV_TYPE="$vdev_choice"
596bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
597bc4a673fSDevin Teske			          "$ZFSBOOT_VDEV_TYPE"
598bc4a673fSDevin Teske		fi
599bc4a673fSDevin Teske
600bc4a673fSDevin Teske		# Determine the number of disks needed for this vdev type
601a88393ceSDevin Teske		want_disks=0
602bc4a673fSDevin Teske		case "$ZFSBOOT_VDEV_TYPE" in
603cd88b886SDevin Teske		stripe) want_disks=1 ;;
604cd88b886SDevin Teske		mirror) want_disks=2 ;;
605bb4fd0a0SAllan Jude		raid10) want_disks=4 ;;
606cd88b886SDevin Teske		raidz1) want_disks=3 ;;
607cd88b886SDevin Teske		raidz2) want_disks=4 ;;
608cd88b886SDevin Teske		raidz3) want_disks=5 ;;
609cd88b886SDevin Teske		esac
610bc4a673fSDevin Teske
611a88393ceSDevin Teske		#
612bc4a673fSDevin Teske		# Warn the user if any scripted disks are invalid
613a88393ceSDevin Teske		#
614a88393ceSDevin Teske		valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
615bc4a673fSDevin Teske		for disk in $ZFSBOOT_DISKS; do
616a88393ceSDevin Teske			if debug= f_device_find -1 \
617a88393ceSDevin Teske				$disk $DEVICE_TYPE_DISK device
618a88393ceSDevin Teske			then
619bc4a673fSDevin Teske				valid_disks="$valid_disks $disk"
620bc4a673fSDevin Teske				continue
621cd88b886SDevin Teske			fi
622bc4a673fSDevin Teske			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
623bc4a673fSDevin Teske			all_valid=
624bc4a673fSDevin Teske		done
625bc4a673fSDevin Teske		if [ ! "$all_valid" ]; then
626bc4a673fSDevin Teske			if [ "$ZFSBOOT_DISKS" ]; then
627bc4a673fSDevin Teske				f_show_err \
628bc4a673fSDevin Teske				    "$msg_missing_one_or_more_scripted_disks"
629bc4a673fSDevin Teske			else
630bc4a673fSDevin Teske				f_dprintf "No disks selected."
631bc4a673fSDevin Teske				f_interactive ||
632bc4a673fSDevin Teske					f_show_err "$msg_no_disks_selected"
633bc4a673fSDevin Teske			fi
634bc4a673fSDevin Teske			f_interactive || return $FAILURE
635bc4a673fSDevin Teske		fi
636bc4a673fSDevin Teske		ZFSBOOT_DISKS="${valid_disks# }"
637bc4a673fSDevin Teske
638bc4a673fSDevin Teske		#
639bc4a673fSDevin Teske		# Short-circuit if we're running non-interactively
640bc4a673fSDevin Teske		#
641bc4a673fSDevin Teske		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
642a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
643bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] && break # to success
644bc4a673fSDevin Teske
645bc4a673fSDevin Teske			# Not enough disks selected
646bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
647bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
648bc4a673fSDevin Teske				  "Not enough disks selected." \
649bc4a673fSDevin Teske				  $ndisks $want_disks
650bc4a673fSDevin Teske			f_interactive || return $FAILURE
651bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
652bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
653bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
654bc4a673fSDevin Teske				return $FAILURE
655bc4a673fSDevin Teske		fi
656bc4a673fSDevin Teske
657bc4a673fSDevin Teske		#
658bc4a673fSDevin Teske		# Confirm the disks that were selected
659bc4a673fSDevin Teske		# Loop until the user cancels or selects enough disks
660bc4a673fSDevin Teske		#
661a88393ceSDevin Teske		breakout=
662bc4a673fSDevin Teske		while :; do
663bc4a673fSDevin Teske			# Loop over list of available disks, resetting state
664a88393ceSDevin Teske			for disk in $disks; do
665caf73e58SAlan Somers				f_isset _${disk}_status && setvar _${disk}_status
666a88393ceSDevin Teske			done
667bc4a673fSDevin Teske
668bc4a673fSDevin Teske			# Loop over list of selected disks and create temporary
669bc4a673fSDevin Teske			# locals to map statuses onto up-to-date list of disks
670bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
671a88393ceSDevin Teske				debug= f_device_find -1 \
672a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK disk
67377d678b7SAlan Somers				f_isset _${disk}_status ||
674a88393ceSDevin Teske					local _${disk}_status
675caf73e58SAlan Somers				setvar _${disk}_status on
676bc4a673fSDevin Teske			done
677bc4a673fSDevin Teske
678bc4a673fSDevin Teske			# Create the checklist menu of discovered disk devices
679bc4a673fSDevin Teske			disk_check_list=
680bc4a673fSDevin Teske			for disk in $disks; do
681a88393ceSDevin Teske				desc=
682a88393ceSDevin Teske				$disk get name name
683a88393ceSDevin Teske				$disk get desc desc
684bc4a673fSDevin Teske				f_shell_escape "$desc" desc
685bc4a673fSDevin Teske				f_getvar _${disk}_status:-off onoff
686bc4a673fSDevin Teske				disk_check_list="$disk_check_list
687a88393ceSDevin Teske					$name '$desc' $onoff"
688bc4a673fSDevin Teske			done
689bc4a673fSDevin Teske
690bc4a673fSDevin Teske			eval f_dialog_checklist_size height width rows \
691d726bc28SFranco Fichtner				\"\$title\" \"\$btitle\" \"\$disk_prompt\" \
692d726bc28SFranco Fichtner				\"\$disk_hline\" $disk_check_list
693bc4a673fSDevin Teske
694bc4a673fSDevin Teske			selections=$( eval $DIALOG \
695bc4a673fSDevin Teske				--title \"\$DIALOG_TITLE\"         \
696bc4a673fSDevin Teske				--backtitle \"\$DIALOG_BACKTITLE\" \
697751952aeSDevin Teske				--separate-output                  \
698d726bc28SFranco Fichtner				--hline \"\$disk_hline\"           \
699bc4a673fSDevin Teske				--ok-label \"\$msg_ok\"            \
700bc4a673fSDevin Teske				--cancel-label \"\$msg_back\"      \
701d726bc28SFranco Fichtner				--checklist \"\$disk_prompt\"      \
702bc4a673fSDevin Teske				$height $width $rows               \
703bc4a673fSDevin Teske				$disk_check_list                   \
704bc4a673fSDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
705bc4a673fSDevin Teske			) || break
706bc4a673fSDevin Teske				# Loop if user pressed ESC or chose Cancel/No
707bc4a673fSDevin Teske			f_dialog_data_sanitize selections
708bc4a673fSDevin Teske
709bc4a673fSDevin Teske			ZFSBOOT_DISKS="$selections"
710bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
711bc4a673fSDevin Teske			          "$ZFSBOOT_DISKS"
712bc4a673fSDevin Teske
713a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
714bb4fd0a0SAllan Jude
715bb4fd0a0SAllan Jude			if [ "$ZFSBOOT_VDEV_TYPE" == "raid10" ] &&
716bb4fd0a0SAllan Jude			    [ $(( $ndisks % 2 )) -ne 0 ]; then
717bb4fd0a0SAllan Jude				f_dprintf "$funcname: %s: %s (%u %% 2 = %u)" \
718bb4fd0a0SAllan Jude					  "$ZFSBOOT_VDEV_TYPE" \
719bb4fd0a0SAllan Jude					  "Number of disks not even:" \
720bb4fd0a0SAllan Jude					  $ndisks $(( $ndisks % 2 ))
721bb4fd0a0SAllan Jude				msg_yes="$msg_change_selection" \
722bb4fd0a0SAllan Jude					msg_no="$msg_cancel" \
723bb4fd0a0SAllan Jude					f_yesno "%s: $msg_odd_disk_selected" \
724bb4fd0a0SAllan Jude						"$ZFSBOOT_VDEV_TYPE" $ndisks ||
725bb4fd0a0SAllan Jude						break
726bb4fd0a0SAllan Jude				continue
727bb4fd0a0SAllan Jude			fi
728bb4fd0a0SAllan Jude
729bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] &&
730bc4a673fSDevin Teske				breakout=break && break
731bc4a673fSDevin Teske
732bc4a673fSDevin Teske			# Not enough disks selected
733bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
734bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
735bc4a673fSDevin Teske			          "Not enough disks selected." \
736bc4a673fSDevin Teske			          $ndisks $want_disks
737bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
738bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
739bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
740bc4a673fSDevin Teske				break
741bc4a673fSDevin Teske		done
742bc4a673fSDevin Teske		[ "$breakout" = "break" ] && break
743bc4a673fSDevin Teske		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
744bc4a673fSDevin Teske	done
745bc4a673fSDevin Teske
746bc4a673fSDevin Teske	return $DIALOG_OK
747cd88b886SDevin Teske}
748cd88b886SDevin Teske
749cd88b886SDevin Teske# zfs_create_diskpart $disk $index
750cd88b886SDevin Teske#
751f69afe42SGraham Perrin# For each block device to be used in the pool, rather than just create the
752f69afe42SGraham Perrin# pool with the raw block devices (e.g., da0, da1, etc.) we create partitions
753cd88b886SDevin Teske# so we can have some real swap. This also provides wiggle room incase your
754cd88b886SDevin Teske# replacement drivers do not have the exact same sector counts.
755cd88b886SDevin Teske#
7567cae6aabSDevin Teske# NOTE: $swapsize and $bootsize should be defined by the calling function.
757a622223fSDevin Teske# NOTE: Sets $bootpart and $targetpart for the calling function.
758cd88b886SDevin Teske#
759cd88b886SDevin Teskezfs_create_diskpart()
760cd88b886SDevin Teske{
761cd88b886SDevin Teske	local funcname=zfs_create_diskpart
762bc4a673fSDevin Teske	local disk="$1" index="$2"
763cd88b886SDevin Teske
764cd88b886SDevin Teske	# Check arguments
765bc4a673fSDevin Teske	if [ ! "$disk" ]; then
766bc4a673fSDevin Teske		f_dprintf "$funcname: NULL disk argument"
767bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
768bc4a673fSDevin Teske			f_show_err "$msg_null_disk_argument"
769bc4a673fSDevin Teske		return $FAILURE
770bc4a673fSDevin Teske	fi
771bc4a673fSDevin Teske	if [ "${disk#*[$IFS]}" != "$disk" ]; then
772bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
773bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
774bc4a673fSDevin Teske			f_show_err "$msg_invalid_disk_argument" "$disk"
775bc4a673fSDevin Teske		return $FAILURE
776bc4a673fSDevin Teske	fi
777bc4a673fSDevin Teske	if [ ! "$index" ]; then
778bc4a673fSDevin Teske		f_dprintf "$funcname: NULL index argument"
779bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
780bc4a673fSDevin Teske			f_show_err "$msg_null_index_argument"
781bc4a673fSDevin Teske		return $FAILURE
782bc4a673fSDevin Teske	fi
783bc4a673fSDevin Teske	if ! f_isinteger "$index"; then
784bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
785bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
786bc4a673fSDevin Teske			f_show_err "$msg_invalid_index_argument" "$index"
787bc4a673fSDevin Teske		return $FAILURE
788bc4a673fSDevin Teske	fi
789bc4a673fSDevin Teske	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
790cd88b886SDevin Teske
791d4d729e4SDevin Teske	# Check for unknown partition scheme before proceeding further
792d4d729e4SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
793ee110941SEd Maste	""|GPT*) : known good ;;
794d4d729e4SDevin Teske	*)
795d4d729e4SDevin Teske		f_dprintf "$funcname: %s is an unsupported partition scheme" \
796d4d729e4SDevin Teske		          "$ZFSBOOT_PARTITION_SCHEME"
797d4d729e4SDevin Teske		msg_error="$msg_error: $funcname" f_show_err \
798d4d729e4SDevin Teske			"$msg_unsupported_partition_scheme" \
799d4d729e4SDevin Teske			"$ZFSBOOT_PARTITION_SCHEME"
800d4d729e4SDevin Teske		return $FAILURE
801d4d729e4SDevin Teske	esac
802d4d729e4SDevin Teske
803cd88b886SDevin Teske	#
804cd88b886SDevin Teske	# Destroy whatever partition layout is currently on disk.
805cd88b886SDevin Teske	# NOTE: `-F' required to destroy if partitions still exist.
806cd88b886SDevin Teske	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
807cd88b886SDevin Teske	#
8081b63cafbSAllan Jude	f_dprintf "$funcname: Exporting ZFS pools..."
8091b63cafbSAllan Jude	zpool list -Ho name | while read z_name; do
8101b63cafbSAllan Jude		f_eval_catch -d $funcname zpool "$ZPOOL_EXPORT_F" $z_name
8111b63cafbSAllan Jude	done
8121b63cafbSAllan Jude	f_dprintf "$funcname: Detaching all GELI providers..."
8131b63cafbSAllan Jude	geli status | tail -n +2 | while read g_name g_status g_component; do
8141b63cafbSAllan Jude		f_eval_catch -d $funcname geli "$GELI_DETACH_F" $g_name
8151b63cafbSAllan Jude	done
816bc4a673fSDevin Teske	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
817a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
818a622223fSDevin Teske	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
819a622223fSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
820cd88b886SDevin Teske
821cd88b886SDevin Teske	# Make doubly-sure backup GPT is destroyed
822a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
823a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
824cd88b886SDevin Teske
8257cae6aabSDevin Teske	#
826cd88b886SDevin Teske	# Lay down the desired type of partition scheme
827cd88b886SDevin Teske	#
828ee110941SEd Maste	local setsize align_small align_big
82969e4b249SAllan Jude	#
83069e4b249SAllan Jude	# If user has requested 4 K alignment, add these params to the
83169e4b249SAllan Jude	# gpart add calls. With GPT, we align large partitions to 1 M for
832ee110941SEd Maste	# improved performance on SSDs.
83369e4b249SAllan Jude	#
83469e4b249SAllan Jude	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
83569e4b249SAllan Jude		align_small="-a 4k"
83669e4b249SAllan Jude		align_big="-a 1m"
83769e4b249SAllan Jude	fi
83869e4b249SAllan Jude
839cd88b886SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
8407059fa6fSAllan Jude	""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
841cd88b886SDevin Teske		#
842cd88b886SDevin Teske		# 1. Create GPT layout using labels
843cd88b886SDevin Teske		#
844a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
845bc4a673fSDevin Teske		             return $FAILURE
846cd88b886SDevin Teske
847cd88b886SDevin Teske		#
8487059fa6fSAllan Jude		# Apply workarounds if requested by the user
8497059fa6fSAllan Jude		#
8507059fa6fSAllan Jude		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Lenovo Fix" ]; then
8517059fa6fSAllan Jude			f_eval_catch $funcname gpart "$GPART_SET_LENOVOFIX" \
8527059fa6fSAllan Jude			             $disk || return $FAILURE
8537059fa6fSAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
8547059fa6fSAllan Jude			f_eval_catch $funcname gpart "$GPART_SET_PMBR_ACTIVE" \
8557059fa6fSAllan Jude			             $disk || return $FAILURE
8567059fa6fSAllan Jude		fi
8577059fa6fSAllan Jude
8587059fa6fSAllan Jude		#
859a1b8f55cSAllan Jude		# 2. Add small freebsd-boot and/or efi partition
860cd88b886SDevin Teske		#
861ee21b0efSDevin Teske		if [ "$ZFSBOOT_BOOT_TYPE" = "UEFI" -o \
862ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
863ee21b0efSDevin Teske		then
8640b7472b3SNathan Whitehorn			f_eval_catch -k justaddedpart $funcname gpart \
8650edc8cc8SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
8660b7472b3SNathan Whitehorn			             "$align_small" efiboot$index efi 260M \
867ee21b0efSDevin Teske			             $disk || return $FAILURE
868aa2a0e0fSAllan Jude
869db8b5613SRebecca Cran			# We'll configure the ESP in bootconfig
8700b7472b3SNathan Whitehorn			if [ -z "$efibootpart" ]; then
8717919c76dSAlan Somers				efibootpart="/dev/gpt/efiboot$index"
8720b7472b3SNathan Whitehorn				f_dprintf "$funcname: configuring ESP at [%s]" \
8730b7472b3SNathan Whitehorn				          "${efibootpart}"
8740b7472b3SNathan Whitehorn
8750b7472b3SNathan Whitehorn				f_eval_catch $funcname newfs_msdos "$NEWFS_ESP"\
8760b7472b3SNathan Whitehorn				             "$efibootpart" \
8770b7472b3SNathan Whitehorn				             || return $FAILURE
8780b7472b3SNathan Whitehorn				f_eval_catch $funcname printf "$PRINTF_FSTAB" \
8790b7472b3SNathan Whitehorn					     $efibootpart /boot/efi msdosfs \
8800b7472b3SNathan Whitehorn				             rw 2 2 "$BSDINSTALL_TMPETC/fstab" \
8810b7472b3SNathan Whitehorn				             || return $FAILURE
8820b7472b3SNathan Whitehorn			fi
8834d41f472SAllan Jude		fi
8844d41f472SAllan Jude
885ee21b0efSDevin Teske		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" -o \
886ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
887ee21b0efSDevin Teske		then
8880edc8cc8SAllan Jude			f_eval_catch $funcname gpart \
8890edc8cc8SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
890ee21b0efSDevin Teske			             "$align_small" gptboot$index \
891ee21b0efSDevin Teske			             freebsd-boot 512k $disk || return $FAILURE
8924d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]; then
893ee21b0efSDevin Teske				f_eval_catch $funcname gpart \
894ee21b0efSDevin Teske				             "$GPART_BOOTCODE_PART" \
895ee21b0efSDevin Teske				             /boot/pmbr /boot/gptzfsboot 1 \
896ee21b0efSDevin Teske				             $disk || return $FAILURE
8974d41f472SAllan Jude			else
898ee21b0efSDevin Teske				f_eval_catch $funcname gpart \
899ee21b0efSDevin Teske				             "$GPART_BOOTCODE_PART" \
900ee21b0efSDevin Teske				             /boot/pmbr /boot/gptzfsboot 2 \
901ee21b0efSDevin Teske				             $disk || return $FAILURE
9024d41f472SAllan Jude			fi
9030edc8cc8SAllan Jude		fi
904cd88b886SDevin Teske
905f69afe42SGraham Perrin		# NB: ZFS pools will use `zfs#' GPT labels
9064d41f472SAllan Jude		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]; then
9074d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9084d41f472SAllan Jude				bootpart=p3 swappart=p4 targetpart=p4
9094d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p5
9104d41f472SAllan Jude			else
9114d41f472SAllan Jude				# Bootpart unused
9124d41f472SAllan Jude				bootpart=p3 swappart=p3 targetpart=p3
9134d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
9144d41f472SAllan Jude			fi
9154d41f472SAllan Jude		else
9164d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9174d41f472SAllan Jude				bootpart=p2 swappart=p3 targetpart=p3
9184d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
9194d41f472SAllan Jude			else
9204d41f472SAllan Jude				# Bootpart unused
9212875e59fSOllivier Robert				bootpart=p2 swappart=p2 targetpart=p2
9227cae6aabSDevin Teske				[ ${swapsize:-0} -gt 0 ] && targetpart=p3
9234d41f472SAllan Jude			fi
9244d41f472SAllan Jude		fi
925cd88b886SDevin Teske
9267cae6aabSDevin Teske		#
9277cae6aabSDevin Teske		# Prepare boot pool if enabled (e.g., for geli(8))
9287cae6aabSDevin Teske		#
9297cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
930bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
93169e4b249SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
93269e4b249SAllan Jude			             "$align_big" boot$index freebsd-zfs \
933ee21b0efSDevin Teske			             ${bootsize}b $disk || return $FAILURE
9347cae6aabSDevin Teske			# Pedantically nuke any old labels
935a622223fSDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
936a622223fSDevin Teske			                /dev/$disk$bootpart
9377cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
9387cae6aabSDevin Teske				# Pedantically detach targetpart for later
9397cae6aabSDevin Teske				f_eval_catch -d $funcname geli \
9407cae6aabSDevin Teske				                "$GELI_DETACH_F" \
941a622223fSDevin Teske				                /dev/$disk$targetpart
942cd88b886SDevin Teske			fi
9437cae6aabSDevin Teske		fi
944cd88b886SDevin Teske
945cd88b886SDevin Teske		#
9467cae6aabSDevin Teske		# 3. Add freebsd-swap partition labeled `swap#'
947cd88b886SDevin Teske		#
948bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
949bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
95069e4b249SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
95169e4b249SAllan Jude			             "$align_big" swap$index freebsd-swap \
952ee21b0efSDevin Teske			             ${swapsize}b $disk || return $FAILURE
953cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
954cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
9552875e59fSOllivier Robert			                /dev/$disk$swappart
956cd88b886SDevin Teske		fi
9577cae6aabSDevin Teske
9587cae6aabSDevin Teske		#
9597cae6aabSDevin Teske		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
9607cae6aabSDevin Teske		#
9617ef92163SJohn Ko		if [ "$ZFSBOOT_POOL_SIZE" ]; then
9627ef92163SJohn Ko			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
9637ef92163SJohn Ko					"$align_big" zfs$index freebsd-zfs $ZFSBOOT_POOL_SIZE $disk ||
9647ef92163SJohn Ko					return $FAILURE
9657ef92163SJohn Ko		else
96669e4b249SAllan Jude			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL" \
96769e4b249SAllan Jude					"$align_big" zfs$index freebsd-zfs $disk ||
96869e4b249SAllan Jude					return $FAILURE
9697ef92163SJohn Ko		fi
9707cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
9717cae6aabSDevin Teske		                /dev/$disk$targetpart
972cd88b886SDevin Teske		;;
973cd88b886SDevin Teske
974cd88b886SDevin Teske	esac # $ZFSBOOT_PARTITION_SCHEME
975cd88b886SDevin Teske
9762875e59fSOllivier Robert	# Update fstab(5)
977caa3817dSAllan Jude	local swapsize
978caa3817dSAllan Jude	f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
9792875e59fSOllivier Robert	if [ "$isswapmirror" ]; then
9802875e59fSOllivier Robert		# This is not the first disk in the mirror, do nothing
9816b5e33baSAllan Jude	elif [ ${swapsize:-0} -eq 0 ]; then
9826b5e33baSAllan Jude		# If swap is 0 sized, don't add it to fstab
9832875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
9842875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
9852875e59fSOllivier Robert		             /dev/mirror/swap.eli none swap sw 0 0 \
986ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
9872875e59fSOllivier Robert		isswapmirror=1
9882875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
9892875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
9902875e59fSOllivier Robert		             /dev/mirror/swap none swap sw 0 0 \
991ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
9922875e59fSOllivier Robert		isswapmirror=1
9932875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
9942875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
9952875e59fSOllivier Robert		             /dev/$disk${swappart}.eli none swap sw 0 0 \
996ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
9972875e59fSOllivier Robert	else
9982875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
9992875e59fSOllivier Robert		             /dev/$disk$swappart none swap sw 0 0 \
1000ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
10012875e59fSOllivier Robert	fi
10022875e59fSOllivier Robert
1003cd88b886SDevin Teske	return $SUCCESS
1004cd88b886SDevin Teske}
1005cd88b886SDevin Teske
1006bc4a673fSDevin Teske# zfs_create_boot $poolname $vdev_type $disks ...
1007cd88b886SDevin Teske#
1008cd88b886SDevin Teske# Creates boot pool and dataset layout. Returns error if something goes wrong.
1009cd88b886SDevin Teske# Errors are printed to stderr for collection and display.
1010cd88b886SDevin Teske#
1011cd88b886SDevin Teskezfs_create_boot()
1012cd88b886SDevin Teske{
1013cd88b886SDevin Teske	local funcname=zfs_create_boot
10147cae6aabSDevin Teske	local zroot_name="$1"
10157cae6aabSDevin Teske	local zroot_vdevtype="$2"
10167cae6aabSDevin Teske	local zroot_vdevs= # Calculated below
10172875e59fSOllivier Robert	local swap_devs= # Calculated below
1018ee110941SEd Maste	local boot_vdevs= # Used for geli(8) layouts
1019a622223fSDevin Teske	shift 2 # poolname vdev_type
1020a622223fSDevin Teske	local disks="$*" disk
10212875e59fSOllivier Robert	local isswapmirror
10222875e59fSOllivier Robert	local bootpart targetpart swappart # Set by zfs_create_diskpart() below
102347206692SDevin Teske	local create_options
10240b7472b3SNathan Whitehorn	local efibootpart
1025cd88b886SDevin Teske
1026a622223fSDevin Teske	#
1027cd88b886SDevin Teske	# Pedantic checks; should never be seen
1028a622223fSDevin Teske	#
10297cae6aabSDevin Teske	if [ ! "$zroot_name" ]; then
1030bc4a673fSDevin Teske		f_dprintf "$funcname: NULL poolname"
1031bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
1032bc4a673fSDevin Teske			f_show_err "$msg_null_poolname"
1033cd88b886SDevin Teske		return $FAILURE
1034cd88b886SDevin Teske	fi
1035cd88b886SDevin Teske	if [ $# -lt 1 ]; then
1036bc4a673fSDevin Teske		f_dprintf "$funcname: missing disk arguments"
1037bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
1038bc4a673fSDevin Teske			f_show_err "$msg_missing_disk_arguments"
1039cd88b886SDevin Teske		return $FAILURE
1040cd88b886SDevin Teske	fi
1041bc4a673fSDevin Teske	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
10427cae6aabSDevin Teske	          "$zroot_name" "$zroot_vdevtype"
1043cd88b886SDevin Teske
1044a622223fSDevin Teske	#
1045cd88b886SDevin Teske	# Initialize fstab(5)
1046a622223fSDevin Teske	#
1047bc4a673fSDevin Teske	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1048a622223fSDevin Teske	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1049bc4a673fSDevin Teske	             return $FAILURE
1050bc4a673fSDevin Teske	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1051cd88b886SDevin Teske	             "# Device" Mountpoint FStype Options Dump "Pass#" \
1052a622223fSDevin Teske	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1053cd88b886SDevin Teske
1054a622223fSDevin Teske	#
1055cd88b886SDevin Teske	# Expand SI units in desired sizes
1056a622223fSDevin Teske	#
10577cae6aabSDevin Teske	f_dprintf "$funcname: Expanding supplied size values..."
10587ef92163SJohn Ko	local swapsize bootsize poolsize
1059bc4a673fSDevin Teske	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1060bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid swap size \`%s'" \
1061bc4a673fSDevin Teske		          "$ZFSBOOT_SWAP_SIZE"
1062bc4a673fSDevin Teske		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1063bc4a673fSDevin Teske		return $FAILURE
1064bc4a673fSDevin Teske	fi
10657cae6aabSDevin Teske	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
10667cae6aabSDevin Teske		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
10677cae6aabSDevin Teske		          "$ZFSBOOT_BOOT_POOL_SIZE"
10687cae6aabSDevin Teske		f_show_err "$msg_invalid_boot_pool_size" \
10697cae6aabSDevin Teske		           "$ZFSBOOT_BOOT_POOL_SIZE"
1070bc4a673fSDevin Teske		return $FAILURE
1071bc4a673fSDevin Teske	fi
10727ef92163SJohn Ko	if [ "$ZFSBOOT_POOL_SIZE" ]; then
10737ef92163SJohn Ko		if ! f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize; then
10747ef92163SJohn Ko			f_dprintf "$funcname: Invalid pool size \`%s'" \
10757ef92163SJohn Ko				  "$ZFSBOOT_POOL_SIZE"
10767ef92163SJohn Ko			f_show_err "$msg_invalid_pool_size" \
10777ef92163SJohn Ko				   "$ZFSBOOT_POOL_SIZE"
10787ef92163SJohn Ko		fi
10797ef92163SJohn Ko		f_dprintf "$funcname: ZFSBOOT_POOL_SIZE=[%s] poolsize=[%s]" \
10807ef92163SJohn Ko			  "$ZFSBOOT_POOL_SIZE" "$poolsize"
10817ef92163SJohn Ko	fi
1082bc4a673fSDevin Teske	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1083bc4a673fSDevin Teske	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
10847cae6aabSDevin Teske	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
10857cae6aabSDevin Teske	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1086cd88b886SDevin Teske
1087a622223fSDevin Teske	#
1088a88393ceSDevin Teske	# Destroy the pool in-case this is our second time 'round (case of
1089a88393ceSDevin Teske	# failure and installer presented ``Retry'' option to come back).
1090a88393ceSDevin Teske	#
1091a88393ceSDevin Teske	# NB: If we don't destroy the pool, later gpart(8) destroy commands
1092a88393ceSDevin Teske	# that try to clear existing partitions (see zfs_create_diskpart())
1093a88393ceSDevin Teske	# will fail with a `Device Busy' error, leading to `GEOM exists'.
1094a88393ceSDevin Teske	#
1095a88393ceSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1096a88393ceSDevin Teske
1097a88393ceSDevin Teske	#
10987cae6aabSDevin Teske	# Prepare the disks and build pool device list(s)
1099a622223fSDevin Teske	#
1100bc4a673fSDevin Teske	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1101b4843bd6SSteven Hartland
1102b4843bd6SSteven Hartland	# Force 4K sectors using vfs.zfs.min_auto_ashift=12
1103b4843bd6SSteven Hartland	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1104b4843bd6SSteven Hartland		f_dprintf "$funcname: With 4K sectors..."
1105b4843bd6SSteven Hartland		f_eval_catch $funcname sysctl "$SYSCTL_ZFS_MIN_ASHIFT_12" \
1106b4843bd6SSteven Hartland		    || return $FAILURE
1107b4843bd6SSteven Hartland	fi
1108cd88b886SDevin Teske	local n=0
1109a622223fSDevin Teske	for disk in $disks; do
1110cd88b886SDevin Teske		zfs_create_diskpart $disk $n || return $FAILURE
11112875e59fSOllivier Robert		# Now $bootpart, $targetpart, and $swappart are set (suffix
11122875e59fSOllivier Robert		# for $disk)
11137cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
11147cae6aabSDevin Teske			boot_vdevs="$boot_vdevs $disk$bootpart"
11157cae6aabSDevin Teske		fi
11167cae6aabSDevin Teske		zroot_vdevs="$zroot_vdevs $disk$targetpart"
1117e751d176SSteven Hartland		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1118e751d176SSteven Hartland			zroot_vdevs="$zroot_vdevs.eli"
1119e751d176SSteven Hartland		fi
11207cae6aabSDevin Teske
11217cae6aabSDevin Teske		n=$(( $n + 1 ))
11227cae6aabSDevin Teske	done # disks
11237cae6aabSDevin Teske
11247cae6aabSDevin Teske	#
11257cae6aabSDevin Teske	# If we need/want a boot pool, create it
11267cae6aabSDevin Teske	#
11277cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
11287cae6aabSDevin Teske		local bootpool_vdevtype= # Calculated below
11297cae6aabSDevin Teske		local bootpool_options= # Calculated below
11307cae6aabSDevin Teske		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
11317cae6aabSDevin Teske		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
11327cae6aabSDevin Teske		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
11337cae6aabSDevin Teske
11347cae6aabSDevin Teske		f_dprintf "$funcname: Setting up boot pool..."
1135cd88b886SDevin Teske		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
11367cae6aabSDevin Teske			f_dprintf "$funcname: For encrypted root disk..."
1137cd88b886SDevin Teske
11387cae6aabSDevin Teske		# Create parent directory for boot pool
1139a107ddbbSRebecca Cran		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1140bc4a673fSDevin Teske		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1141a622223fSDevin Teske		             $BSDINSTALL_CHROOT || return $FAILURE
1142cd88b886SDevin Teske
11437cae6aabSDevin Teske		# Create mirror across the boot partition on all disks
1144a88393ceSDevin Teske		local nvdevs
1145a88393ceSDevin Teske		f_count nvdevs $boot_vdevs
1146a88393ceSDevin Teske		[ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1147cd88b886SDevin Teske
114847206692SDevin Teske		create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
11497cae6aabSDevin Teske		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
115047206692SDevin Teske		bootpool_options="$bootpool_options $create_options"
11517cae6aabSDevin Teske		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1152bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
11537cae6aabSDevin Teske		             "$bootpool_options" "$bootpool_name" \
11544b4b90d5SDevin Teske		             "$bootpool_vdevtype" "$boot_vdevs" ||
11554b4b90d5SDevin Teske		             return $FAILURE
115645df402eSDevin Teske
11577cae6aabSDevin Teske		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1158cd88b886SDevin Teske		             return $FAILURE
1159cd88b886SDevin Teske
11607cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1161cd88b886SDevin Teske			# Generate an encryption key using random(4)
1162bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
11637cae6aabSDevin Teske			             /dev/random "$bootpool/$zroot_key" \
11647cae6aabSDevin Teske			             "bs=4096 count=1" || return $FAILURE
11651ebd9bd4SDevin Teske			f_eval_catch $funcname chmod "$CHMOD_MODE" \
11664ef40262SDevin Teske			             go-wrx "$bootpool/$zroot_key" ||
11674ef40262SDevin Teske			             return $FAILURE
11687cae6aabSDevin Teske		fi
11697cae6aabSDevin Teske
11707cae6aabSDevin Teske	fi
1171cd88b886SDevin Teske
1172a622223fSDevin Teske	#
1173cd88b886SDevin Teske	# Create the geli(8) GEOMS
1174a622223fSDevin Teske	#
11757cae6aabSDevin Teske	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1176a1b8f55cSAllan Jude		#
1177a1b8f55cSAllan Jude		# Load the AES-NI kernel module to accelerate encryption
1178a1b8f55cSAllan Jude		#
1179a1b8f55cSAllan Jude		f_eval_catch -d $funcname kldload "$KLDLOAD" "aesni"
11807cae6aabSDevin Teske		# Prompt user for password (twice)
1181bc4a673fSDevin Teske		if ! msg_enter_new_password="$msg_geli_password" \
1182bc4a673fSDevin Teske			f_dialog_input_password
1183bc4a673fSDevin Teske		then
1184bc4a673fSDevin Teske			f_dprintf "$funcname: User cancelled"
1185bc4a673fSDevin Teske			f_show_err "$msg_user_cancelled"
1186bc4a673fSDevin Teske			return $FAILURE
1187bc4a673fSDevin Teske		fi
1188a622223fSDevin Teske
1189a622223fSDevin Teske		# Initialize geli(8) on each of the target partitions
1190bc4a673fSDevin Teske		for disk in $disks; do
1191cd88b886SDevin Teske			f_dialog_info "$msg_geli_setup" \
1192cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1193a1b8f55cSAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
1194a622223fSDevin Teske				if ! echo "$pw_password" | f_eval_catch \
1195a622223fSDevin Teske					$funcname geli "$GELI_PASSWORD_INIT" \
11967cae6aabSDevin Teske					"$bootpool/boot/$disk$targetpart.eli" \
11977cae6aabSDevin Teske					AES-XTS "$bootpool/$zroot_key" \
1198a622223fSDevin Teske					$disk$targetpart
1199bc4a673fSDevin Teske				then
1200bc4a673fSDevin Teske					f_interactive || f_die
1201a622223fSDevin Teske					unset pw_password # Sensitive info
1202cd88b886SDevin Teske					return $FAILURE
1203bc4a673fSDevin Teske				fi
1204a622223fSDevin Teske				if ! echo "$pw_password" | f_eval_catch \
1205a622223fSDevin Teske					$funcname geli "$GELI_ATTACH" \
12067cae6aabSDevin Teske					"$bootpool/$zroot_key" $disk$targetpart
1207bc4a673fSDevin Teske				then
1208bc4a673fSDevin Teske					f_interactive || f_die
1209a622223fSDevin Teske					unset pw_password # Sensitive info
1210bc4a673fSDevin Teske					return $FAILURE
1211bc4a673fSDevin Teske				fi
1212a1b8f55cSAllan Jude			else
1213a1b8f55cSAllan Jude				# With no bootpool, there is no place to store
1214a1b8f55cSAllan Jude				# the key files, use only a password
1215a1b8f55cSAllan Jude				if ! echo "$pw_password" | f_eval_catch \
1216a1b8f55cSAllan Jude					$funcname geli \
1217ee21b0efSDevin Teske					"$GELI_PASSWORD_GELIBOOT_INIT" \
1218ee21b0efSDevin Teske					AES-XTS $disk$targetpart
1219a1b8f55cSAllan Jude				then
1220a1b8f55cSAllan Jude					f_interactive || f_die
1221a1b8f55cSAllan Jude					unset pw_password # Sensitive info
1222a1b8f55cSAllan Jude					return $FAILURE
1223a1b8f55cSAllan Jude				fi
1224a1b8f55cSAllan Jude				if ! echo "$pw_password" | f_eval_catch \
1225a1b8f55cSAllan Jude					$funcname geli "$GELI_ATTACH_NOKEY" \
1226a1b8f55cSAllan Jude					$disk$targetpart
1227a1b8f55cSAllan Jude				then
1228a1b8f55cSAllan Jude					f_interactive || f_die
1229a1b8f55cSAllan Jude					unset pw_password # Sensitive info
1230a1b8f55cSAllan Jude					return $FAILURE
1231a1b8f55cSAllan Jude				fi
1232a1b8f55cSAllan Jude			fi
1233cd88b886SDevin Teske		done
1234a622223fSDevin Teske		unset pw_password # Sensitive info
1235a1b8f55cSAllan Jude	fi
1236a622223fSDevin Teske
1237a1b8f55cSAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1238a622223fSDevin Teske		# Clean up
12397cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
12407cae6aabSDevin Teske			return $FAILURE
1241a107ddbbSRebecca Cran		# tmpfs
1242a107ddbbSRebecca Cran		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1243cd88b886SDevin Teske	fi
124433112d9eSDevin Teske
12452875e59fSOllivier Robert	#
12462875e59fSOllivier Robert	# Create the gmirror(8) GEOMS for swap
12472875e59fSOllivier Robert	#
124806445fc0SAllan Jude	if [ ${swapsize:-0} -gt 0 -a "$ZFSBOOT_SWAP_MIRROR" ]; then
12492875e59fSOllivier Robert		for disk in $disks; do
12502875e59fSOllivier Robert			swap_devs="$swap_devs $disk$swappart"
12512875e59fSOllivier Robert		done
12522875e59fSOllivier Robert		f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
12532875e59fSOllivier Robert			"$swap_devs" || return $FAILURE
12542875e59fSOllivier Robert	fi
1255cd88b886SDevin Teske
1256cd88b886SDevin Teske	#
1257a622223fSDevin Teske	# Create the ZFS root pool with desired type and disk devices
1258cd88b886SDevin Teske	#
12597cae6aabSDevin Teske	f_dprintf "$funcname: Creating root pool..."
126047206692SDevin Teske	create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1261bb4fd0a0SAllan Jude	if [ "$zroot_vdevtype" == "raid10" ]; then
1262bb4fd0a0SAllan Jude		raid10_vdevs=""
1263bb4fd0a0SAllan Jude		for vdev in $zroot_vdevs; do
1264bb4fd0a0SAllan Jude			f_count nvdev $raid10_vdevs
1265bb4fd0a0SAllan Jude			if [ $(( $nvdev % 3 )) -eq 0 ]; then
1266bb4fd0a0SAllan Jude				raid10_vdevs="$raid10_vdevs mirror"
1267bb4fd0a0SAllan Jude			fi
1268bb4fd0a0SAllan Jude			raid10_vdevs="$raid10_vdevs $vdev"
1269bb4fd0a0SAllan Jude		done
1270bb4fd0a0SAllan Jude		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1271bb4fd0a0SAllan Jude			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1272ee21b0efSDevin Teske			"$zroot_name" "" "$raid10_vdevs" || return $FAILURE
1273bb4fd0a0SAllan Jude	else
1274bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
127547206692SDevin Teske			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
12764b4b90d5SDevin Teske			"$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
12774b4b90d5SDevin Teske			return $FAILURE
1278bb4fd0a0SAllan Jude	fi
1279cd88b886SDevin Teske
1280cd88b886SDevin Teske	#
12817cae6aabSDevin Teske	# Create ZFS dataset layout within the new root pool
1282cd88b886SDevin Teske	#
1283bc4a673fSDevin Teske	f_dprintf "$funcname: Creating ZFS datasets..."
1284cd88b886SDevin Teske	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1285cd88b886SDevin Teske		# Skip blank lines and comments
1286cd88b886SDevin Teske		case "$dataset" in "#"*|"") continue; esac
1287cd88b886SDevin Teske		# Remove potential inline comments in options
1288cd88b886SDevin Teske		options="${options%%#*}"
1289cd88b886SDevin Teske		# Replace tabs with spaces
1290cd88b886SDevin Teske		f_replaceall "$options" "	" " " options
1291cd88b886SDevin Teske		# Reduce contiguous runs of space to one single space
1292cd88b886SDevin Teske		oldoptions=
1293cd88b886SDevin Teske		while [ "$oldoptions" != "$options" ]; do
1294cd88b886SDevin Teske			oldoptions="$options"
1295cd88b886SDevin Teske			f_replaceall "$options" "  " " " options
1296cd88b886SDevin Teske		done
1297cd88b886SDevin Teske		# Replace both commas and spaces with ` -o '
1298cd88b886SDevin Teske		f_replaceall "$options" "[ ,]" " -o " options
1299cd88b886SDevin Teske		# Create the dataset with desired options
1300bc4a673fSDevin Teske		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
13017cae6aabSDevin Teske		             "${options:+-o $options}" "$zroot_name$dataset" ||
1302cd88b886SDevin Teske		             return $FAILURE
1303cd88b886SDevin Teske	done
1304cd88b886SDevin Teske
13057e3ebc5bSAllan Jude	#
13067e3ebc5bSAllan Jude	# Set a mountpoint for the root of the pool so newly created datasets
13077e3ebc5bSAllan Jude	# have a mountpoint to inherit
13087e3ebc5bSAllan Jude	#
13097e3ebc5bSAllan Jude	f_dprintf "$funcname: Setting mountpoint for root of the pool..."
13107e3ebc5bSAllan Jude	f_eval_catch $funcname zfs "$ZFS_SET" \
1311ee21b0efSDevin Teske		"mountpoint=/$zroot_name" "$zroot_name" || return $FAILURE
13127e3ebc5bSAllan Jude
1313cd88b886SDevin Teske	# Touch up permissions on the tmp directories
1314bc4a673fSDevin Teske	f_dprintf "$funcname: Modifying directory permissions..."
1315bc4a673fSDevin Teske	local dir
1316bc4a673fSDevin Teske	for dir in /tmp /var/tmp; do
1317b3f7b139SAllan Jude		f_eval_catch $funcname mkdir "$MKDIR_P" \
1318b3f7b139SAllan Jude		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1319bc4a673fSDevin Teske		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
13204ffa3da5SAllan Jude		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1321bc4a673fSDevin Teske	done
1322cd88b886SDevin Teske
1323cd88b886SDevin Teske	# Set bootfs property
13247cae6aabSDevin Teske	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1325bc4a673fSDevin Teske	f_dprintf "$funcname: Setting bootfs property..."
1326bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
13277cae6aabSDevin Teske		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
13287cae6aabSDevin Teske		return $FAILURE
1329cd88b886SDevin Teske
1330ca13d5d2SAllan Jude	# Remount bootpool and create symlink(s)
1331ca13d5d2SAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1332ca13d5d2SAllan Jude		f_eval_catch $funcname zfs "$ZFS_MOUNT" "$bootpool_name" ||
1333ca13d5d2SAllan Jude			return $FAILURE
1334ca13d5d2SAllan Jude		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1335ca13d5d2SAllan Jude		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1336ca13d5d2SAllan Jude		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1337ca13d5d2SAllan Jude	fi
1338ca13d5d2SAllan Jude
1339ca13d5d2SAllan Jude	# zpool.cache is required to mount more than one pool at boot time
13406311cc9eSDevin Teske	f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1341a622223fSDevin Teske	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1342bc4a673fSDevin Teske	             return $FAILURE
1343bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1344a622223fSDevin Teske	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
13457cae6aabSDevin Teske	             "$zroot_name" || return $FAILURE
1346cd88b886SDevin Teske
134752fffe93SAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
134852fffe93SAllan Jude		f_eval_catch $funcname printf "$PRINTF_CONF" \
1349ee21b0efSDevin Teske			vfs.root.mountfrom \
1350ee21b0efSDevin Teske			"\"zfs:$zroot_name/$zroot_bootfs\"" \
135152fffe93SAllan Jude			$BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
135252fffe93SAllan Jude	fi
1353b06bc65eSAllan Jude	#
1354ee21b0efSDevin Teske	# Set canmount=noauto so that the default Boot Environment (BE) does
1355ee21b0efSDevin Teske	# not get mounted if a different BE is selected from the beastie menu
1356b06bc65eSAllan Jude	#
1357a320e9ddSBrad Davis	f_dprintf "$funcname: Set canmount=noauto for any datasets under the BE..."
1358a320e9ddSBrad Davis	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1359a320e9ddSBrad Davis		# Skip blank lines and comments
1360a320e9ddSBrad Davis		case "$dataset" in "#"*|"") continue; esac
1361a320e9ddSBrad Davis		options="${options%%#*}"
1362a320e9ddSBrad Davis		#
1363a320e9ddSBrad Davis		case "$dataset" in "/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"*)
1364b06bc65eSAllan Jude			f_eval_catch $funcname zfs "$ZFS_SET" "canmount=noauto" \
1365a320e9ddSBrad Davis				"$zroot_name$dataset" || return $FAILURE ;;
1366a320e9ddSBrad Davis		*) continue ;;
1367a320e9ddSBrad Davis		esac
1368a320e9ddSBrad Davis	done
1369b06bc65eSAllan Jude
137013d8e1ceSDevin Teske	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1371cd88b886SDevin Teske	# NOTE: We later concatenate these into their destination
1372bc4a673fSDevin Teske	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1373bc4a673fSDevin Teske	          "$funcname"
1374bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1375a622223fSDevin Teske	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1376ee482f2cSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" \
1377ee482f2cSDevin Teske	             'kern.geom.label.disk_ident.enable=\"0\"' \
1378ee482f2cSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
13792875e59fSOllivier Robert	f_eval_catch $funcname echo "$ECHO_APPEND" \
13802875e59fSOllivier Robert	             'kern.geom.label.gptid.enable=\"0\"' \
13812875e59fSOllivier Robert	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
13822875e59fSOllivier Robert
138346c380e6SAllan Jude	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
138446c380e6SAllan Jude		f_eval_catch $funcname echo "$ECHO_APPEND" \
138546c380e6SAllan Jude	             'vfs.zfs.min_auto_ashift=12' \
138698b9d384SSteven Hartland	             $BSDINSTALL_TMPETC/sysctl.conf.zfs || return $FAILURE
138746c380e6SAllan Jude	fi
138846c380e6SAllan Jude
13892875e59fSOllivier Robert	if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
139033112d9eSDevin Teske		f_eval_catch $funcname echo "$ECHO_APPEND" \
139133112d9eSDevin Teske		             'geom_mirror_load=\"YES\"' \
139233112d9eSDevin Teske		             $BSDINSTALL_TMPBOOT/loader.conf.gmirror ||
139333112d9eSDevin Teske		             return $FAILURE
13942875e59fSOllivier Robert	fi
1395cd88b886SDevin Teske
13967cae6aabSDevin Teske	# We're all done unless we should go on to do encryption
13977cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
139813d8e1ceSDevin Teske
1399cd88b886SDevin Teske	#
1400cd88b886SDevin Teske	# Configure geli(8)-based encryption
1401cd88b886SDevin Teske	#
1402bc4a673fSDevin Teske	f_dprintf "$funcname: Configuring disk encryption..."
1403bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1404a622223fSDevin Teske		$BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1405bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1406a622223fSDevin Teske		$BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1407a1b8f55cSAllan Jude
1408a1b8f55cSAllan Jude	# We're all done unless we should go on for boot pool
1409a1b8f55cSAllan Jude	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1410a1b8f55cSAllan Jude
1411bc4a673fSDevin Teske	for disk in $disks; do
1412bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1413a622223fSDevin Teske			geli_%s_keyfile0_load "$disk$targetpart YES" \
1414a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1415cd88b886SDevin Teske			return $FAILURE
1416bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1417bc4a673fSDevin Teske			geli_%s_keyfile0_type \
1418a622223fSDevin Teske			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1419a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1420cd88b886SDevin Teske			return $FAILURE
1421bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1422bc4a673fSDevin Teske			geli_%s_keyfile0_name \
1423a622223fSDevin Teske			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1424a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1425cd88b886SDevin Teske			return $FAILURE
1426cd88b886SDevin Teske	done
1427cd88b886SDevin Teske
1428a1b8f55cSAllan Jude	# Set cachefile for boot pool so it auto-imports at system start
1429a1b8f55cSAllan Jude	f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1430a1b8f55cSAllan Jude	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1431a1b8f55cSAllan Jude	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1432a1b8f55cSAllan Jude	             "$bootpool_name" || return $FAILURE
1433a1b8f55cSAllan Jude
1434a1b8f55cSAllan Jude	# Some additional geli(8) requirements for loader.conf(5)
1435a1b8f55cSAllan Jude	for option in \
1436a1b8f55cSAllan Jude		'zpool_cache_load=\"YES\"' \
1437a1b8f55cSAllan Jude		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1438a1b8f55cSAllan Jude		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1439a1b8f55cSAllan Jude		'geom_eli_passphrase_prompt=\"YES\"' \
1440a1b8f55cSAllan Jude	; do
1441a1b8f55cSAllan Jude		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1442a1b8f55cSAllan Jude		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1443a1b8f55cSAllan Jude		             return $FAILURE
1444a1b8f55cSAllan Jude	done
1445cd88b886SDevin Teske	return $SUCCESS
1446cd88b886SDevin Teske}
1447cd88b886SDevin Teske
1448cd88b886SDevin Teske# dialog_menu_diskinfo
1449cd88b886SDevin Teske#
1450cd88b886SDevin Teske# Prompt the user to select a disk and then provide detailed info on it.
1451cd88b886SDevin Teske#
1452cd88b886SDevin Teskedialog_menu_diskinfo()
1453cd88b886SDevin Teske{
1454a88393ceSDevin Teske	local device disk
1455cd88b886SDevin Teske
1456cd88b886SDevin Teske	#
1457cd88b886SDevin Teske	# Break from loop when user cancels disk selection
1458cd88b886SDevin Teske	#
1459cd88b886SDevin Teske	while :; do
1460a88393ceSDevin Teske		device=$( msg_cancel="$msg_back" f_device_menu \
1461cd88b886SDevin Teske			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1462cd88b886SDevin Teske			$DEVICE_TYPE_DISK 2>&1 ) || break
1463a88393ceSDevin Teske		$device get name disk
1464cd88b886SDevin Teske
1465cd88b886SDevin Teske		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1466cd88b886SDevin Teske		f_show_msg "$msg_detailed_disk_info" \
1467cd88b886SDevin Teske			"$disk" "$( gpart show $disk 2> /dev/null )" \
1468cd88b886SDevin Teske			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1469cd88b886SDevin Teske			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1470cd88b886SDevin Teske	done
1471cd88b886SDevin Teske
1472cd88b886SDevin Teske	return $SUCCESS
1473cd88b886SDevin Teske}
1474cd88b886SDevin Teske
1475f69afe42SGraham Perrindialog_pool_name()
14769935b0e8SEmmanuel Vadot{
14779935b0e8SEmmanuel Vadot	local prompt="$* is already taken, please enter a name for the ZFS pool \
14789935b0e8SEmmanuel Vadot	(Or confirm using the same name by just pressing enter)"
14799935b0e8SEmmanuel Vadot	local hline=
14809935b0e8SEmmanuel Vadot	local value="$*"
14819935b0e8SEmmanuel Vadot	local height width
14829935b0e8SEmmanuel Vadot
14839935b0e8SEmmanuel Vadot	f_dialog_inputbox_size height width \
14849935b0e8SEmmanuel Vadot		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
14859935b0e8SEmmanuel Vadot
14869935b0e8SEmmanuel Vadot	$DIALOG \
14879935b0e8SEmmanuel Vadot		--title "$DIALOG_TITLE"         \
14889935b0e8SEmmanuel Vadot		--backtitle "$DIALOG_BACKTITLE" \
14899935b0e8SEmmanuel Vadot		--hline "$hline"                \
14909935b0e8SEmmanuel Vadot		--ok-label "Ok"                 \
14919935b0e8SEmmanuel Vadot		--no-cancel                     \
14929935b0e8SEmmanuel Vadot		--inputbox "$prompt"            \
14939935b0e8SEmmanuel Vadot		$height $width "$value"         \
14949935b0e8SEmmanuel Vadot		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
14959935b0e8SEmmanuel Vadot}
14969935b0e8SEmmanuel Vadot
1497cd88b886SDevin Teske############################################################ MAIN
1498cd88b886SDevin Teske
1499cd88b886SDevin Teske#
1500cd88b886SDevin Teske# Initialize
1501cd88b886SDevin Teske#
1502cd88b886SDevin Teskef_dialog_title "$msg_zfs_configuration"
1503cd88b886SDevin Teskef_dialog_backtitle "$msg_freebsd_installer"
1504cd88b886SDevin Teske
1505cd88b886SDevin Teske# User may have specifically requested ZFS-related operations be interactive
1506cd88b886SDevin Teske! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1507cd88b886SDevin Teske
1508cd88b886SDevin Teske#
1509bc4a673fSDevin Teske# Debugging
1510bc4a673fSDevin Teske#
1511bc4a673fSDevin Teskef_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1512bc4a673fSDevin Teskef_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1513a622223fSDevin Teskef_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1514bc4a673fSDevin Teske
15159935b0e8SEmmanuel Vadotpools=$(zpool import | awk '$1 == "pool:" { print $2}')
15169935b0e8SEmmanuel Vadotfor pool in ${pools}; do
15179935b0e8SEmmanuel Vadot	f_dprintf "Checking ${pool} against ${ZFSBOOT_POOL_NAME}"
15189935b0e8SEmmanuel Vadot	if [ "${pool}" = "${ZFSBOOT_POOL_NAME}" ]; then
15199935b0e8SEmmanuel Vadot		f_dprintf "Pool ${pool} already taken"
1520f69afe42SGraham Perrin		ZFSBOOT_POOL_NAME=$(dialog_pool_name "${ZFSBOOT_POOL_NAME}")
1521e50daba0SJens Schweikhardt		break
15229935b0e8SEmmanuel Vadot	fi
15239935b0e8SEmmanuel Vadotdone
15249935b0e8SEmmanuel Vadot
1525bc4a673fSDevin Teske#
1526e39a9678SEd Maste# Determine default boot type
152712307018SAllan Jude#
1528e39a9678SEd Mastecase $(uname -m) in
15297b08a307SMitchell Hornearm64|riscv)
15307b08a307SMitchell Horne	# We support only UEFI boot for arm64 and riscv.
1531e39a9678SEd Maste	: ${ZFSBOOT_BOOT_TYPE:=UEFI}
1532e39a9678SEd Maste	: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1533e39a9678SEd Maste	;;
1534e39a9678SEd Maste*)
1535e39a9678SEd Maste	# If the system was booted with UEFI, set the default boot type to UEFI
153612307018SAllan Jude	bootmethod=$( sysctl -n machdep.bootmethod )
153712307018SAllan Jude	f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
15380edc8cc8SAllan Jude	if [ "$bootmethod" = "UEFI" ]; then
15394d41f472SAllan Jude		: ${ZFSBOOT_BOOT_TYPE:=BIOS+UEFI}
15400edc8cc8SAllan Jude		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
15410edc8cc8SAllan Jude	else
15420edc8cc8SAllan Jude		: ${ZFSBOOT_BOOT_TYPE:=BIOS}
15430edc8cc8SAllan Jude		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
154412307018SAllan Jude	fi
1545e39a9678SEd Maste	;;
1546e39a9678SEd Masteesac
154712307018SAllan Jude
154812307018SAllan Jude#
1549cd88b886SDevin Teske# Loop over the main menu until we've accomplished what we came here to do
1550cd88b886SDevin Teske#
1551cd88b886SDevin Teskewhile :; do
1552cd88b886SDevin Teske	if ! f_interactive; then
1553cd88b886SDevin Teske		retval=$DIALOG_OK
1554bc4a673fSDevin Teske		mtag=">>> $msg_install"
1555cd88b886SDevin Teske	else
1556cd88b886SDevin Teske		dialog_menu_main
1557cd88b886SDevin Teske		retval=$?
1558cd88b886SDevin Teske		f_dialog_menutag_fetch mtag
1559cd88b886SDevin Teske	fi
1560cd88b886SDevin Teske
1561bc4a673fSDevin Teske	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1562cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] || f_die
1563cd88b886SDevin Teske
1564cd88b886SDevin Teske	case "$mtag" in
1565bc4a673fSDevin Teske	">>> $msg_install")
1566cd88b886SDevin Teske		#
1567cd88b886SDevin Teske		# First, validate the user's selections
1568cd88b886SDevin Teske		#
1569cd88b886SDevin Teske
1570cd88b886SDevin Teske		# Make sure they gave us a name for the pool
1571cd88b886SDevin Teske		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1572bc4a673fSDevin Teske			f_dprintf "Pool name cannot be empty."
1573bc4a673fSDevin Teske			f_show_err "$msg_pool_name_cannot_be_empty"
1574cd88b886SDevin Teske			continue
1575cd88b886SDevin Teske		fi
1576bc4a673fSDevin Teske
1577bc4a673fSDevin Teske		# Validate vdev type against number of disks selected/scripted
1578bc4a673fSDevin Teske		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1579bc4a673fSDevin Teske		# NB: dialog_menu_layout supports running non-interactively
1580bc4a673fSDevin Teske		dialog_menu_layout || continue
1581bc4a673fSDevin Teske
15826203e46bSAllan Jude		# Make sure each disk will have room for ZFS
1583cd88b886SDevin Teske		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
15846203e46bSAllan Jude		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize &&
15856203e46bSAllan Jude		   f_expand_number "1g" zpoolmin
1586cd88b886SDevin Teske		then
15877ef92163SJohn Ko			minsize=$swapsize teeny_disks=
15887ef92163SJohn Ko			if [ "$ZFSBOOT_POOL_SIZE" ]; then
15897ef92163SJohn Ko				f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize
15907ef92163SJohn Ko				minsize=$(( $minsize + $poolsize ))
15917ef92163SJohn Ko			fi
15927cae6aabSDevin Teske			[ "$ZFSBOOT_BOOT_POOL" ] &&
15937cae6aabSDevin Teske				minsize=$(( $minsize + $bootsize ))
1594bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
1595a88393ceSDevin Teske				debug= f_device_find -1 \
1596a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK device
1597a88393ceSDevin Teske				$device get capacity disksize || continue
1598a88393ceSDevin Teske				[ ${disksize:-0} -ge 0 ] || disksize=0
1599cd88b886SDevin Teske				[ $disksize -lt $minsize ] &&
1600cd88b886SDevin Teske					teeny_disks="$teeny_disks $disk"
1601cd88b886SDevin Teske			done
1602cd88b886SDevin Teske			if [ "$teeny_disks" ]; then
16037cae6aabSDevin Teske				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1604bc4a673fSDevin Teske				          "$ZFSBOOT_SWAP_SIZE" \
16057cae6aabSDevin Teske				          "$ZFSBOOT_BOOT_POOL_SIZE" \
16067cae6aabSDevin Teske				          "minsize=[$minsize]"
1607bc4a673fSDevin Teske				f_dprintf "These disks are too small: %s" \
1608bc4a673fSDevin Teske				          "$teeny_disks"
1609bc4a673fSDevin Teske				f_show_err "$msg_these_disks_are_too_small" \
1610cd88b886SDevin Teske				           "$ZFSBOOT_SWAP_SIZE" \
16117cae6aabSDevin Teske				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1612cd88b886SDevin Teske				           "$teeny_disks"
1613cd88b886SDevin Teske				continue
1614cd88b886SDevin Teske			fi
1615cd88b886SDevin Teske		fi
1616cd88b886SDevin Teske
1617cd88b886SDevin Teske		#
1618cd88b886SDevin Teske		# Last Chance!
1619cd88b886SDevin Teske		#
1620bc4a673fSDevin Teske		if f_interactive; then
1621bc4a673fSDevin Teske			dialog_last_chance $ZFSBOOT_DISKS || continue
1622cd88b886SDevin Teske		fi
1623cd88b886SDevin Teske
1624cd88b886SDevin Teske		#
1625cd88b886SDevin Teske		# Let's do this
1626cd88b886SDevin Teske		#
1627cd88b886SDevin Teske
1628cd88b886SDevin Teske		vdev_type="$ZFSBOOT_VDEV_TYPE"
1629cd88b886SDevin Teske
1630cd88b886SDevin Teske		# Blank the vdev type for the default layout
1631cd88b886SDevin Teske		[ "$vdev_type" = "stripe" ] && vdev_type=
1632cd88b886SDevin Teske
1633bc4a673fSDevin Teske		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1634bc4a673fSDevin Teske		                "$vdev_type" $ZFSBOOT_DISKS || continue
1635cd88b886SDevin Teske
1636a89559c2SYuri Pankov		# To be reused by bootconfig
1637a107ddbbSRebecca Cran		echo "$ZFSBOOT_DISKS" > ${TMPDIR:-"/tmp"}/bsdinstall-zfsboot
1638a89559c2SYuri Pankov
1639cd88b886SDevin Teske		break # to success
1640cd88b886SDevin Teske		;;
1641bc4a673fSDevin Teske	?" $msg_pool_type_disks")
1642bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=1
1643bc4a673fSDevin Teske		dialog_menu_layout
1644bc4a673fSDevin Teske		# User has poked settings, disable later confirmation
1645bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=
1646bc4a673fSDevin Teske		;;
1647cd88b886SDevin Teske	"- $msg_rescan_devices") f_device_rescan ;;
1648cd88b886SDevin Teske	"- $msg_disk_info") dialog_menu_diskinfo ;;
1649cd88b886SDevin Teske	?" $msg_pool_name")
1650cd88b886SDevin Teske		# Prompt the user to input/change the name for the new pool
1651cd88b886SDevin Teske		f_dialog_input input \
1652f69afe42SGraham Perrin			"$msg_please_enter_a_name_for_your_pool" \
1653cd88b886SDevin Teske			"$ZFSBOOT_POOL_NAME" &&
1654cd88b886SDevin Teske			ZFSBOOT_POOL_NAME="$input"
1655cd88b886SDevin Teske		;;
1656cd88b886SDevin Teske	?" $msg_force_4k_sectors")
1657cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1658b4843bd6SSteven Hartland		if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1659b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=
1660cd88b886SDevin Teske		else
1661b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=1
1662cd88b886SDevin Teske		fi
1663cd88b886SDevin Teske		;;
1664bc4a673fSDevin Teske	?" $msg_encrypt_disks")
1665cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1666cd88b886SDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1667cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=
1668cd88b886SDevin Teske		else
1669b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=1
1670cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=1
1671cd88b886SDevin Teske		fi
1672cd88b886SDevin Teske		;;
1673cd88b886SDevin Teske	?" $msg_partition_scheme")
1674ee110941SEd Maste		# Toggle between partition schemes
1675ee21b0efSDevin Teske		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1676ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]
1677ee21b0efSDevin Teske		then
16780edc8cc8SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
16790edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="UEFI"
1680ee21b0efSDevin Teske		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1681ee21b0efSDevin Teske		       "$ZFSBOOT_BOOT_TYPE" = "UEFI" ]
1682ee21b0efSDevin Teske		then
16834d41f472SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
16844d41f472SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS+UEFI"
16850edc8cc8SAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" ]; then
16867059fa6fSAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT + Active"
16870edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
16887059fa6fSAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
16897059fa6fSAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
16900edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
1691cd88b886SDevin Teske		else
16920edc8cc8SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
16930edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
1694cd88b886SDevin Teske		fi
1695cd88b886SDevin Teske		;;
1696cd88b886SDevin Teske	?" $msg_swap_size")
1697cd88b886SDevin Teske		# Prompt the user to input/change the swap size for each disk
1698169ff110SAllan Jude		while :; do
1699cd88b886SDevin Teske		    f_dialog_input input \
1700cd88b886SDevin Teske			    "$msg_please_enter_amount_of_swap_space" \
1701cd88b886SDevin Teske			    "$ZFSBOOT_SWAP_SIZE" &&
170230c8ebe9SDevin Teske			    ZFSBOOT_SWAP_SIZE="${input:-0}"
1703169ff110SAllan Jude		    if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1704169ff110SAllan Jude		    then
1705169ff110SAllan Jude			if [ $swapsize -ne 0 -a $swapsize -lt 104857600 ]; then
1706169ff110SAllan Jude			    f_show_err "$msg_swap_toosmall" \
1707169ff110SAllan Jude				       "$ZFSBOOT_SWAP_SIZE"
1708e50daba0SJens Schweikhardt			    continue
1709169ff110SAllan Jude			else
1710e50daba0SJens Schweikhardt			    break
1711169ff110SAllan Jude			fi
1712169ff110SAllan Jude		    else
1713169ff110SAllan Jude			f_show_err "$msg_swap_invalid" \
1714169ff110SAllan Jude				   "$ZFSBOOT_SWAP_SIZE"
1715e50daba0SJens Schweikhardt			continue
1716169ff110SAllan Jude		    fi
1717169ff110SAllan Jude		done
1718cd88b886SDevin Teske		;;
17192875e59fSOllivier Robert	?" $msg_swap_mirror")
17202875e59fSOllivier Robert		# Toggle the variable referenced both by the menu and later
17212875e59fSOllivier Robert		if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
17222875e59fSOllivier Robert			ZFSBOOT_SWAP_MIRROR=
17232875e59fSOllivier Robert		else
17242875e59fSOllivier Robert			ZFSBOOT_SWAP_MIRROR=1
17252875e59fSOllivier Robert		fi
17262875e59fSOllivier Robert		;;
17272875e59fSOllivier Robert	?" $msg_swap_encrypt")
17282875e59fSOllivier Robert		# Toggle the variable referenced both by the menu and later
17292875e59fSOllivier Robert		if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
17302875e59fSOllivier Robert			ZFSBOOT_SWAP_ENCRYPTION=
17312875e59fSOllivier Robert		else
17322875e59fSOllivier Robert			ZFSBOOT_SWAP_ENCRYPTION=1
17332875e59fSOllivier Robert		fi
17342875e59fSOllivier Robert		;;
1735ca7e12f7SCraig Leres	?" $msg_zfs_options_name")
1736ca7e12f7SCraig Leres		# Prompt the user to input/change the pool options
1737ca7e12f7SCraig Leres		f_dialog_input input \
1738f69afe42SGraham Perrin			"$msg_please_enter_options_for_your_pool" \
1739ca7e12f7SCraig Leres			"$ZFSBOOT_POOL_CREATE_OPTIONS" &&
1740ca7e12f7SCraig Leres			ZFSBOOT_POOL_CREATE_OPTIONS="$input"
1741ca7e12f7SCraig Leres		;;
1742cd88b886SDevin Teske	esac
1743cd88b886SDevin Teskedone
1744cd88b886SDevin Teske
174565f36140SDevin Teskeexit $SUCCESS
1746cd88b886SDevin Teske
1747cd88b886SDevin Teske################################################################################
1748cd88b886SDevin Teske# END
1749cd88b886SDevin Teske################################################################################
1750