xref: /freebsd/usr.sbin/bsdinstall/scripts/zfsboot (revision e50daba0e496c271cdd1056ffcbab513348b3de9)
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# $FreeBSD$
29cd88b886SDevin Teske#
30cd88b886SDevin Teske############################################################ INCLUDES
31cd88b886SDevin Teske
32cd88b886SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
33cd88b886SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
34cd88b886SDevin Teskef_dprintf "%s: loading includes..." "$0"
35cd88b886SDevin Teskef_include $BSDCFG_SHARE/device.subr
36cd88b886SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
37cd88b886SDevin Teskef_include $BSDCFG_SHARE/password/password.subr
38cd88b886SDevin Teskef_include $BSDCFG_SHARE/variable.subr
39cd88b886SDevin Teske
40cd88b886SDevin Teske############################################################ CONFIGURATION
41cd88b886SDevin Teske
42cd88b886SDevin Teske#
43cd88b886SDevin Teske# Default name of the boot-pool
44cd88b886SDevin Teske#
45cd88b886SDevin Teske: ${ZFSBOOT_POOL_NAME:=zroot}
46cd88b886SDevin Teske
47cd88b886SDevin Teske#
487ef92163SJohn Ko# Default pool size is optional
497ef92163SJohn Ko#
507ef92163SJohn Ko: ${ZFSBOOT_POOL_SIZE=}
517ef92163SJohn Ko
527ef92163SJohn Ko#
5347206692SDevin Teske# Default options to use when creating zroot pool
5447206692SDevin Teske#
5547206692SDevin Teske: ${ZFSBOOT_POOL_CREATE_OPTIONS:=-O compress=lz4 -O atime=off}
5647206692SDevin Teske
5747206692SDevin Teske#
58cd88b886SDevin Teske# Default name for the boot environment parent dataset
59cd88b886SDevin Teske#
6067635c19SDevin Teske: ${ZFSBOOT_BEROOT_NAME:=ROOT}
61cd88b886SDevin Teske
62cd88b886SDevin Teske#
635e38260cSBaptiste Daroussin# Default name for the primary boot environment
64cd88b886SDevin Teske#
65cd88b886SDevin Teske: ${ZFSBOOT_BOOTFS_NAME:=default}
66cd88b886SDevin Teske
67cd88b886SDevin Teske#
68cd88b886SDevin Teske# Default Virtual Device (vdev) type to create
69cd88b886SDevin Teske#
70cd88b886SDevin Teske: ${ZFSBOOT_VDEV_TYPE:=stripe}
71cd88b886SDevin Teske
72cd88b886SDevin Teske#
73b4843bd6SSteven Hartland# Should we use sysctl(8) vfs.zfs.min_auto_ashift=12 to force 4K sectors?
74cd88b886SDevin Teske#
75b4843bd6SSteven Hartland: ${ZFSBOOT_FORCE_4K_SECTORS:=1}
76cd88b886SDevin Teske
77cd88b886SDevin Teske#
78cd88b886SDevin Teske# Should we use geli(8) to encrypt the drives?
797cae6aabSDevin Teske# NB: Automatically enables ZFSBOOT_BOOT_POOL
80cd88b886SDevin Teske#
81bc4a673fSDevin Teske: ${ZFSBOOT_GELI_ENCRYPTION=}
82cd88b886SDevin Teske
83cd88b886SDevin Teske#
84cd88b886SDevin Teske# Default path to the geli(8) keyfile used in drive encryption
85cd88b886SDevin Teske#
86cd88b886SDevin Teske: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
87cd88b886SDevin Teske
88cd88b886SDevin Teske#
897cae6aabSDevin Teske# Create a separate boot pool?
907cae6aabSDevin Teske# NB: Automatically set when using geli(8) or MBR
917cae6aabSDevin Teske#
927cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL=}
937cae6aabSDevin Teske
947cae6aabSDevin Teske#
9547206692SDevin Teske# Options to use when creating separate boot pool (if any)
9647206692SDevin Teske#
9747206692SDevin Teske: ${ZFSBOOT_BOOT_POOL_CREATE_OPTIONS:=}
9847206692SDevin Teske
9947206692SDevin Teske#
1007cae6aabSDevin Teske# Default name for boot pool when enabled (e.g., geli(8) or MBR)
1017cae6aabSDevin Teske#
1027cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
1037cae6aabSDevin Teske
1047cae6aabSDevin Teske#
1057cae6aabSDevin Teske# Default size for boot pool when enabled (e.g., geli(8) or MBR)
1067cae6aabSDevin Teske#
1077cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
1087cae6aabSDevin Teske
1097cae6aabSDevin Teske#
110cd88b886SDevin Teske# Default disks to use (always empty unless being scripted)
111cd88b886SDevin Teske#
112cd88b886SDevin Teske: ${ZFSBOOT_DISKS:=}
113cd88b886SDevin Teske
114cd88b886SDevin Teske#
115cd88b886SDevin Teske# Default partitioning scheme to use on disks
116cd88b886SDevin Teske#
1170edc8cc8SAllan Jude: ${ZFSBOOT_PARTITION_SCHEME:=}
1180edc8cc8SAllan Jude
1190edc8cc8SAllan Jude#
1204d41f472SAllan Jude# Default boot type to use on disks
1210edc8cc8SAllan Jude#
1220edc8cc8SAllan Jude: ${ZFSBOOT_BOOT_TYPE:=}
123cd88b886SDevin Teske
124cd88b886SDevin Teske#
125cd88b886SDevin Teske# How much swap to put on each block device in the boot zpool
126cd88b886SDevin Teske# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
127cd88b886SDevin Teske#
128cd88b886SDevin Teske: ${ZFSBOOT_SWAP_SIZE:=2g}
129cd88b886SDevin Teske
130cd88b886SDevin Teske#
1312875e59fSOllivier Robert# Should we use geli(8) to encrypt the swap?
1322875e59fSOllivier Robert#
1332875e59fSOllivier Robert: ${ZFSBOOT_SWAP_ENCRYPTION=}
1342875e59fSOllivier Robert
1352875e59fSOllivier Robert#
1362875e59fSOllivier Robert# Should we use gmirror(8) to mirror the swap?
1372875e59fSOllivier Robert#
1382875e59fSOllivier Robert: ${ZFSBOOT_SWAP_MIRROR=}
1392875e59fSOllivier Robert
1402875e59fSOllivier Robert#
141bc4a673fSDevin Teske# Default ZFS datasets for root zpool
142cd88b886SDevin Teske#
143cd88b886SDevin Teske# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
144cd88b886SDevin Teske# NOTE: Anything after pound/hash character [#] is ignored as a comment.
145cd88b886SDevin Teske#
146cd88b886SDevin Teskef_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
147cd88b886SDevin Teske	# DATASET	OPTIONS (comma or space separated; or both)
148cd88b886SDevin Teske
149cd88b886SDevin Teske	# Boot Environment [BE] root and default boot dataset
150cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
151dcf36219SAllan Jude	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
152cd88b886SDevin Teske
153cd88b886SDevin Teske	# Compress /tmp, allow exec but not setuid
154ed8690e3SOllivier Robert	/tmp		mountpoint=/tmp,exec=on,setuid=off
155cd88b886SDevin Teske
156cd88b886SDevin Teske	# Don't mount /usr so that 'base' files go to the BEROOT
157cd88b886SDevin Teske	/usr		mountpoint=/usr,canmount=off
158cd88b886SDevin Teske
159cd88b886SDevin Teske	# Home directories separated so they are common to all BEs
160bc4a673fSDevin Teske	/usr/home	# NB: /home is a symlink to /usr/home
161cd88b886SDevin Teske
162cd88b886SDevin Teske	# Ports tree
163ed8690e3SOllivier Robert	/usr/ports	setuid=off
164cd88b886SDevin Teske
165cd88b886SDevin Teske	# Source tree (compressed)
166ed8690e3SOllivier Robert	/usr/src
167cd88b886SDevin Teske
168cd88b886SDevin Teske	# Create /var and friends
1691aec0f4fSAllan Jude	/var		mountpoint=/var,canmount=off
1701bc08043SAllan Jude	/var/audit	exec=off,setuid=off
171ed8690e3SOllivier Robert	/var/crash	exec=off,setuid=off
172ed8690e3SOllivier Robert	/var/log	exec=off,setuid=off
173ed8690e3SOllivier Robert	/var/mail	atime=on
174ed8690e3SOllivier Robert	/var/tmp	setuid=off
175cd88b886SDevin Teske" # END-QUOTE
176cd88b886SDevin Teske
177bc4a673fSDevin Teske#
178bc4a673fSDevin Teske# If interactive and the user has not explicitly chosen a vdev type or disks,
179bc4a673fSDevin Teske# make the user confirm scripted/default choices when proceeding to install.
180bc4a673fSDevin Teske#
181bc4a673fSDevin Teske: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
182bc4a673fSDevin Teske
183cd88b886SDevin Teske############################################################ GLOBALS
184cd88b886SDevin Teske
185cd88b886SDevin Teske#
186bc4a673fSDevin Teske# Format of a line in printf(1) syntax to add to fstab(5)
187bc4a673fSDevin Teske#
188bc4a673fSDevin TeskeFSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
189bc4a673fSDevin Teske
190bc4a673fSDevin Teske#
191bc4a673fSDevin Teske# Command strings for various tasks
192bc4a673fSDevin Teske#
193aa2a0e0fSAllan JudeCOPY='cp "%s" "%s"'
194bc4a673fSDevin TeskeCHMOD_MODE='chmod %s "%s"'
195bc4a673fSDevin TeskeDD_WITH_OPTIONS='dd if="%s" of="%s" %s'
196bc4a673fSDevin TeskeECHO_APPEND='echo "%s" >> "%s"'
197aa2a0e0fSAllan JudeECHO_OVERWRITE='echo "%s" > "%s"'
198bc4a673fSDevin TeskeGELI_ATTACH='geli attach -j - -k "%s" "%s"'
199a1b8f55cSAllan JudeGELI_ATTACH_NOKEY='geli attach -j - "%s"'
200a622223fSDevin TeskeGELI_DETACH_F='geli detach -f "%s"'
201bc4a673fSDevin TeskeGELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
202a1b8f55cSAllan JudeGELI_PASSWORD_GELIBOOT_INIT='geli init -bg -e %s -J - -l 256 -s 4096 "%s"'
20369e4b249SAllan JudeGPART_ADD_ALIGN='gpart add %s -t %s "%s"'
20469e4b249SAllan JudeGPART_ADD_ALIGN_INDEX='gpart add %s -i %s -t %s "%s"'
20569e4b249SAllan JudeGPART_ADD_ALIGN_INDEX_WITH_SIZE='gpart add %s -i %s -t %s -s %s "%s"'
20669e4b249SAllan JudeGPART_ADD_ALIGN_LABEL='gpart add %s -l %s -t %s "%s"'
20769e4b249SAllan JudeGPART_ADD_ALIGN_LABEL_WITH_SIZE='gpart add %s -l %s -t %s -s %s "%s"'
208bc4a673fSDevin TeskeGPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
209bc4a673fSDevin TeskeGPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
2100edc8cc8SAllan JudeGPART_BOOTCODE_PARTONLY='gpart bootcode -p "%s" -i %s "%s"'
211bc4a673fSDevin TeskeGPART_CREATE='gpart create -s %s "%s"'
212a622223fSDevin TeskeGPART_DESTROY_F='gpart destroy -F "%s"'
213bc4a673fSDevin TeskeGPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
2147059fa6fSAllan JudeGPART_SET_LENOVOFIX='gpart set -a lenovofix "%s"'
2157059fa6fSAllan JudeGPART_SET_PMBR_ACTIVE='gpart set -a active "%s"'
216a622223fSDevin TeskeGRAID_DELETE='graid delete "%s"'
217a1b8f55cSAllan JudeKLDLOAD='kldload %s'
218bc4a673fSDevin TeskeLN_SF='ln -sf "%s" "%s"'
219bc4a673fSDevin TeskeMKDIR_P='mkdir -p "%s"'
220bc4a673fSDevin TeskeMOUNT_TYPE='mount -t %s "%s" "%s"'
2210b7472b3SNathan WhitehornNEWFS_ESP='newfs_msdos "%s"'
222bc4a673fSDevin TeskePRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
223bc4a673fSDevin TeskePRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
224bc4a673fSDevin TeskeSHELL_TRUNCATE=':> "%s"'
2252875e59fSOllivier RobertSWAP_GMIRROR_LABEL='gmirror label swap %s'
226b4843bd6SSteven HartlandSYSCTL_ZFS_MIN_ASHIFT_12='sysctl vfs.zfs.min_auto_ashift=12'
227a622223fSDevin TeskeUMOUNT='umount "%s"'
228bc4a673fSDevin TeskeZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
229ca13d5d2SAllan JudeZFS_MOUNT='zfs mount "%s"'
230bc4a673fSDevin TeskeZFS_SET='zfs set "%s" "%s"'
231bc4a673fSDevin TeskeZFS_UNMOUNT='zfs unmount "%s"'
232bc4a673fSDevin TeskeZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
233a88393ceSDevin TeskeZPOOL_DESTROY='zpool destroy "%s"'
234bc4a673fSDevin TeskeZPOOL_EXPORT='zpool export "%s"'
2351b63cafbSAllan JudeZPOOL_EXPORT_F='zpool export -f "%s"'
236bc4a673fSDevin TeskeZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
237a622223fSDevin TeskeZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
238bc4a673fSDevin TeskeZPOOL_SET='zpool set %s "%s"'
239bc4a673fSDevin Teske
240bc4a673fSDevin Teske#
241cd88b886SDevin Teske# Strings that should be moved to an i18n file and loaded with f_include_lang()
242cd88b886SDevin Teske#
243cd88b886SDevin Teskehline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
244cd88b886SDevin Teskehline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
245cd88b886SDevin Teskehline_arrows_tab_enter="Press arrows, TAB or ENTER"
246bc4a673fSDevin Teskemsg_an_unknown_error_occurred="An unknown error occurred"
247cd88b886SDevin Teskemsg_back="Back"
248cd88b886SDevin Teskemsg_cancel="Cancel"
249bc4a673fSDevin Teskemsg_change_selection="Change Selection"
250cd88b886SDevin Teskemsg_configure_options="Configure Options:"
251cd88b886SDevin 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"
252cd88b886SDevin Teskemsg_disk_info="Disk Info"
253cd88b886SDevin Teskemsg_disk_info_help="Get detailed information on disk device(s)"
254695a612fSOllivier Robertmsg_disk_plural="disks"
2551b41374fSDevin Teskemsg_disk_singular="disk"
256bc4a673fSDevin Teskemsg_encrypt_disks="Encrypt Disks?"
257bc4a673fSDevin Teskemsg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
258bc4a673fSDevin Teskemsg_error="Error"
259cd88b886SDevin Teskemsg_force_4k_sectors="Force 4K Sectors?"
26069e4b249SAllan Judemsg_force_4k_sectors_help="Align partitions to 4K sector boundries and set vfs.zfs.min_auto_ashift=12"
261cd88b886SDevin Teskemsg_freebsd_installer="FreeBSD Installer"
262cd88b886SDevin 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"
2637a434c5cSDevin Teskemsg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
264bc4a673fSDevin Teskemsg_install="Install"
265bc4a673fSDevin Teskemsg_install_desc="Proceed with Installation"
266bc4a673fSDevin Teskemsg_install_help="Create ZFS boot pool with displayed options"
2677cae6aabSDevin Teskemsg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
268bc4a673fSDevin Teskemsg_invalid_disk_argument="Invalid disk argument \`%s'"
269bc4a673fSDevin Teskemsg_invalid_index_argument="Invalid index argument \`%s'"
2707ef92163SJohn Komsg_invalid_pool_size="Invalid pool size \`%s'"
271bc4a673fSDevin Teskemsg_invalid_swap_size="Invalid swap size \`%s'"
272cd88b886SDevin Teskemsg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
273bc4a673fSDevin 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"
274bc4a673fSDevin 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'
275cd88b886SDevin Teskemsg_mirror_desc="Mirror - n-Way Mirroring"
276cd88b886SDevin Teskemsg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
277bc4a673fSDevin Teskemsg_missing_disk_arguments="missing disk arguments"
278bc4a673fSDevin Teskemsg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
279cd88b886SDevin Teskemsg_no="NO"
280cd88b886SDevin Teskemsg_no_disks_present_to_configure="No disk(s) present to configure"
281cd88b886SDevin Teskemsg_no_disks_selected="No disks selected."
282bc4a673fSDevin Teskemsg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
283bc4a673fSDevin Teskemsg_null_disk_argument="NULL disk argument"
284bc4a673fSDevin Teskemsg_null_index_argument="NULL index argument"
285bc4a673fSDevin Teskemsg_null_poolname="NULL poolname"
286bb4fd0a0SAllan Judemsg_odd_disk_selected="An even number of disks must be selected to create a RAID 1+0. (%u selected)"
287cd88b886SDevin Teskemsg_ok="OK"
288cd88b886SDevin Teskemsg_partition_scheme="Partition Scheme"
2897059fa6fSAllan Judemsg_partition_scheme_help="Select partitioning scheme. GPT is recommended."
290cd88b886SDevin Teskemsg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
291cd88b886SDevin Teskemsg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
292cd88b886SDevin Teskemsg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
293cd88b886SDevin Teskemsg_pool_name="Pool Name"
294cd88b886SDevin Teskemsg_pool_name_cannot_be_empty="Pool name cannot be empty."
295cd88b886SDevin Teskemsg_pool_name_help="Customize the name of the zpool to be created (Required)"
296bc4a673fSDevin Teskemsg_pool_type_disks="Pool Type/Disks:"
297bc4a673fSDevin Teskemsg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
298cd88b886SDevin Teskemsg_processing_selection="Processing selection..."
299bb4fd0a0SAllan Judemsg_raid10_desc="RAID 1+0 - n x 2-Way Mirrors"
300bb4fd0a0SAllan Judemsg_raid10_help="[4+ Disks] Striped Mirrors provides the best performance, but the least storage"
301cd88b886SDevin Teskemsg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
302cd88b886SDevin Teskemsg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
303cd88b886SDevin Teskemsg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
304cd88b886SDevin Teskemsg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
305cd88b886SDevin Teskemsg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
306cd88b886SDevin Teskemsg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
307cd88b886SDevin Teskemsg_rescan_devices="Rescan Devices"
308cd88b886SDevin Teskemsg_rescan_devices_help="Scan for device changes"
309cd88b886SDevin Teskemsg_select="Select"
310cd88b886SDevin Teskemsg_select_a_disk_device="Select a disk device"
311cd88b886SDevin Teskemsg_select_virtual_device_type="Select Virtual Device type:"
312cd88b886SDevin Teskemsg_stripe_desc="Stripe - No Redundancy"
313cd88b886SDevin Teskemsg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
3142875e59fSOllivier Robertmsg_swap_encrypt="Encrypt Swap?"
3152875e59fSOllivier Robertmsg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
316169ff110SAllan Judemsg_swap_invalid="The selected swap size (%s) is invalid. Enter a number optionally followed by units. Example: 2G"
3172875e59fSOllivier Robertmsg_swap_mirror="Mirror Swap?"
3182875e59fSOllivier Robertmsg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
319cd88b886SDevin Teskemsg_swap_size="Swap Size"
320cd88b886SDevin Teskemsg_swap_size_help="Customize how much swap space is allocated to each selected disk"
321169ff110SAllan Judemsg_swap_toosmall="The selected swap size (%s) is to small. Please enter a value greater than 100MB or enter 0 for no swap"
3226203e46bSAllan 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."
323bc4a673fSDevin Teskemsg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
324bc4a673fSDevin Teskemsg_unsupported_partition_scheme="%s is an unsupported partition scheme"
325bc4a673fSDevin Teskemsg_user_cancelled="User Cancelled."
326cd88b886SDevin Teskemsg_yes="YES"
327cd88b886SDevin Teskemsg_zfs_configuration="ZFS Configuration"
328cd88b886SDevin Teske
329cd88b886SDevin Teske############################################################ FUNCTIONS
330cd88b886SDevin Teske
331cd88b886SDevin Teske# dialog_menu_main
332cd88b886SDevin Teske#
333cd88b886SDevin Teske# Display the dialog(1)-based application main menu.
334cd88b886SDevin Teske#
335cd88b886SDevin Teskedialog_menu_main()
336cd88b886SDevin Teske{
337cd88b886SDevin Teske	local title="$DIALOG_TITLE"
338cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
339cd88b886SDevin Teske	local prompt="$msg_configure_options"
340cd88b886SDevin Teske	local force4k="$msg_no"
341cd88b886SDevin Teske	local usegeli="$msg_no"
3422875e59fSOllivier Robert	local swapgeli="$msg_no"
3432875e59fSOllivier Robert	local swapmirror="$msg_no"
344b4843bd6SSteven Hartland	[ "$ZFSBOOT_FORCE_4K_SECTORS" ] && force4k="$msg_yes"
345cd88b886SDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
3462875e59fSOllivier Robert	[ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
3472875e59fSOllivier Robert	[ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
348695a612fSOllivier Robert	local disks n disks_grammar
349a88393ceSDevin Teske	f_count n $ZFSBOOT_DISKS
350695a612fSOllivier Robert	{ [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
351695a612fSOllivier Robert		disks_grammar=$msg_disk_plural # grammar
352cd88b886SDevin Teske	local menu_list="
353bc4a673fSDevin Teske		'>>> $msg_install'      '$msg_install_desc'
354bc4a673fSDevin Teske		                        '$msg_install_help'
35533112d9eSDevin Teske		'T $msg_pool_type_disks'
35633112d9eSDevin Teske		               '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
357bc4a673fSDevin Teske		               '$msg_pool_type_disks_help'
358cd88b886SDevin Teske		'- $msg_rescan_devices' '*'
359cd88b886SDevin Teske		                        '$msg_rescan_devices_help'
360cd88b886SDevin Teske		'- $msg_disk_info'      '*'
361cd88b886SDevin Teske		                        '$msg_disk_info_help'
362bc4a673fSDevin Teske		'N $msg_pool_name'      '$ZFSBOOT_POOL_NAME'
363cd88b886SDevin Teske		                        '$msg_pool_name_help'
36433112d9eSDevin Teske		'4 $msg_force_4k_sectors'
36533112d9eSDevin Teske		                        '$force4k'
366cd88b886SDevin Teske		                        '$msg_force_4k_sectors_help'
367bc4a673fSDevin Teske		'E $msg_encrypt_disks'  '$usegeli'
368bc4a673fSDevin Teske		                        '$msg_encrypt_disks_help'
36933112d9eSDevin Teske		'P $msg_partition_scheme'
3700edc8cc8SAllan Jude		               '$ZFSBOOT_PARTITION_SCHEME ($ZFSBOOT_BOOT_TYPE)'
371cd88b886SDevin Teske		               '$msg_partition_scheme_help'
372bc4a673fSDevin Teske		'S $msg_swap_size'      '$ZFSBOOT_SWAP_SIZE'
373cd88b886SDevin Teske		                        '$msg_swap_size_help'
3742875e59fSOllivier Robert		'M $msg_swap_mirror'    '$swapmirror'
3752875e59fSOllivier Robert		                        '$msg_swap_mirror_help'
3762875e59fSOllivier Robert		'W $msg_swap_encrypt'   '$swapgeli'
3772875e59fSOllivier Robert		                        '$msg_swap_encrypt_help'
378cd88b886SDevin Teske	" # END-QUOTE
379cd88b886SDevin Teske	local defaultitem= # Calculated below
380cd88b886SDevin Teske	local hline="$hline_alnum_arrows_punc_tab_enter"
381cd88b886SDevin Teske
382cd88b886SDevin Teske	local height width rows
383cd88b886SDevin Teske	eval f_dialog_menu_with_help_size height width rows \
384cd88b886SDevin Teske		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
385cd88b886SDevin Teske
386cd88b886SDevin Teske	# Obtain default-item from previously stored selection
387cd88b886SDevin Teske	f_dialog_default_fetch defaultitem
388cd88b886SDevin Teske
389cd88b886SDevin Teske	local menu_choice
390cd88b886SDevin Teske	menu_choice=$( eval $DIALOG \
391cd88b886SDevin Teske		--title \"\$title\"              \
392cd88b886SDevin Teske		--backtitle \"\$btitle\"         \
393cd88b886SDevin Teske		--hline \"\$hline\"              \
394cd88b886SDevin Teske		--item-help                      \
395cd88b886SDevin Teske		--ok-label \"\$msg_select\"      \
396cd88b886SDevin Teske		--cancel-label \"\$msg_cancel\"  \
397cd88b886SDevin Teske		--default-item \"\$defaultitem\" \
398cd88b886SDevin Teske		--menu \"\$prompt\"              \
399cd88b886SDevin Teske		$height $width $rows             \
400cd88b886SDevin Teske		$menu_list                       \
401cd88b886SDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
402cd88b886SDevin Teske	)
403cd88b886SDevin Teske	local retval=$?
404cd88b886SDevin Teske	f_dialog_data_sanitize menu_choice
405cd88b886SDevin Teske	f_dialog_menutag_store "$menu_choice"
406cd88b886SDevin Teske
407cd88b886SDevin Teske	# Only update default-item on success
408cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
409cd88b886SDevin Teske
410cd88b886SDevin Teske	return $retval
411cd88b886SDevin Teske}
412cd88b886SDevin Teske
413bc4a673fSDevin Teske# dialog_last_chance $disks ...
414cd88b886SDevin Teske#
415bc4a673fSDevin Teske# Display a list of the disks that the user is about to destroy. The default
416bc4a673fSDevin Teske# action is to return error status unless the user explicitly (non-default)
417bc4a673fSDevin Teske# selects "Yes" from the noyes dialog.
418cd88b886SDevin Teske#
419bc4a673fSDevin Teskedialog_last_chance()
420cd88b886SDevin Teske{
421cd88b886SDevin Teske	local title="$DIALOG_TITLE"
422cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
423bc4a673fSDevin Teske	local prompt # Calculated below
424bc4a673fSDevin Teske	local hline="$hline_arrows_tab_enter"
425cd88b886SDevin Teske
426bc4a673fSDevin Teske	local height=8 width=50 prefix="   "
427bc4a673fSDevin Teske	local plen=${#prefix} list= line=
428bc4a673fSDevin Teske	local max_width=$(( $width - 3 - $plen ))
429bc4a673fSDevin Teske
430bc4a673fSDevin Teske	local yes no defaultno extra_args format
431bc4a673fSDevin Teske	if [ "$USE_XDIALOG" ]; then
432bc4a673fSDevin Teske		yes=ok no=cancel defaultno=default-no
433bc4a673fSDevin Teske		extra_args="--wrap --left"
434bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure"
435bc4a673fSDevin Teske	else
436bc4a673fSDevin Teske		yes=yes no=no defaultno=defaultno
437bc4a673fSDevin Teske		extra_args="--colors --cr-wrap"
438bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure_color"
439bc4a673fSDevin Teske	fi
440bc4a673fSDevin Teske
441bc4a673fSDevin Teske	local disk line_width
442bc4a673fSDevin Teske	for disk in $*; do
443bc4a673fSDevin Teske		if [ "$line" ]; then
444bc4a673fSDevin Teske			line_width=${#line}
445bc4a673fSDevin Teske		else
446bc4a673fSDevin Teske			line_width=$plen
447bc4a673fSDevin Teske		fi
448bc4a673fSDevin Teske		line_width=$(( $line_width + 1 + ${#disk} ))
449bc4a673fSDevin Teske		# Add newline before disk if it would exceed max_width
450bc4a673fSDevin Teske		if [ $line_width -gt $max_width ]; then
451bc4a673fSDevin Teske			list="$list$line\n"
452bc4a673fSDevin Teske			line="$prefix"
453bc4a673fSDevin Teske			height=$(( $height + 1 ))
454bc4a673fSDevin Teske		fi
455bc4a673fSDevin Teske		# Add the disk to the list
456bc4a673fSDevin Teske		line="$line $disk"
457bc4a673fSDevin Teske	done
458bc4a673fSDevin Teske	# Append the left-overs
459bc4a673fSDevin Teske	if [ "${line#$prefix}" ]; then
460bc4a673fSDevin Teske		list="$list$line"
461bc4a673fSDevin Teske		height=$(( $height + 1 ))
462bc4a673fSDevin Teske	fi
463bc4a673fSDevin Teske
464bc4a673fSDevin Teske	# Add height for Xdialog(1)
465bc4a673fSDevin Teske	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
466bc4a673fSDevin Teske
467bc4a673fSDevin Teske	prompt=$( printf "$format" "$list" )
468bc4a673fSDevin Teske	f_dprintf "%s: Last Chance!" "$0"
469bc4a673fSDevin Teske	$DIALOG \
470bc4a673fSDevin Teske		--title "$title"        \
471bc4a673fSDevin Teske		--backtitle "$btitle"   \
472bc4a673fSDevin Teske		--hline "$hline"        \
473bc4a673fSDevin Teske		--$defaultno            \
474bc4a673fSDevin Teske		--$yes-label "$msg_yes" \
475bc4a673fSDevin Teske		--$no-label "$msg_no"   \
476bc4a673fSDevin Teske		$extra_args             \
477bc4a673fSDevin Teske		--yesno "$prompt" $height $width
478bc4a673fSDevin Teske}
479bc4a673fSDevin Teske
480bc4a673fSDevin Teske# dialog_menu_layout
481cd88b886SDevin Teske#
482bc4a673fSDevin Teske# Configure Virtual Device type and disks to use for the ZFS boot pool. User
483bc4a673fSDevin Teske# must select enough disks to satisfy the chosen vdev type.
484cd88b886SDevin Teske#
485bc4a673fSDevin Teskedialog_menu_layout()
486bc4a673fSDevin Teske{
487bc4a673fSDevin Teske	local funcname=dialog_menu_layout
488bc4a673fSDevin Teske	local title="$DIALOG_TITLE"
489bc4a673fSDevin Teske	local btitle="$DIALOG_BACKTITLE"
490bc4a673fSDevin Teske	local vdev_prompt="$msg_select_virtual_device_type"
491bc4a673fSDevin Teske	local disk_prompt="$msg_please_select_one_or_more_disks"
492bc4a673fSDevin Teske	local vdev_menu_list="
493bc4a673fSDevin Teske		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
494bc4a673fSDevin Teske		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
495bb4fd0a0SAllan Jude		'raid10' '$msg_raid10_desc' '$msg_raid10_help'
496bc4a673fSDevin Teske		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
497bc4a673fSDevin Teske		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
498bc4a673fSDevin Teske		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
499bc4a673fSDevin Teske	" # END-QUOTE
500bc4a673fSDevin Teske	local disk_check_list= # Calculated below
501bc4a673fSDevin Teske	local vdev_hline="$hline_arrows_tab_enter"
502bc4a673fSDevin Teske	local disk_hline="$hline_arrows_space_tab_enter"
503bc4a673fSDevin Teske
504bc4a673fSDevin Teske	# Warn the user if vdev type is not valid
505bc4a673fSDevin Teske	case "$ZFSBOOT_VDEV_TYPE" in
506bb4fd0a0SAllan Jude	stripe|mirror|raid10|raidz1|raidz2|raidz3) : known good ;;
507bc4a673fSDevin Teske	*)
508bc4a673fSDevin Teske		f_dprintf "%s: Invalid virtual device type \`%s'" \
509bc4a673fSDevin Teske			  $funcname "$ZFSBOOT_VDEV_TYPE"
510bc4a673fSDevin Teske		f_show_err "$msg_invalid_virtual_device_type" \
511bc4a673fSDevin Teske			   "$ZFSBOOT_VDEV_TYPE"
512bc4a673fSDevin Teske		f_interactive || return $FAILURE
513bc4a673fSDevin Teske	esac
514bc4a673fSDevin Teske
515bc4a673fSDevin Teske	# Calculate size of vdev menu once only
516bc4a673fSDevin Teske	local vheight vwidth vrows
517bc4a673fSDevin Teske	eval f_dialog_menu_with_help_size vheight vwidth vrows \
518bc4a673fSDevin Teske		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
519bc4a673fSDevin Teske		$vdev_menu_list
520bc4a673fSDevin Teske
521bc4a673fSDevin Teske	# Get a list of probed disk devices
522bc4a673fSDevin Teske	local disks=
523a88393ceSDevin Teske	debug= f_device_find "" $DEVICE_TYPE_DISK disks
524a88393ceSDevin Teske
525a88393ceSDevin Teske	# Prune out mounted md(4) devices that may be part of the boot process
526a88393ceSDevin Teske	local disk name new_list=
527a88393ceSDevin Teske	for disk in $disks; do
528a88393ceSDevin Teske		debug= $disk get name name
529a88393ceSDevin Teske		case "$name" in
530a88393ceSDevin Teske		md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
531a88393ceSDevin Teske		esac
532a88393ceSDevin Teske		new_list="$new_list $disk"
533a88393ceSDevin Teske	done
534a88393ceSDevin Teske	disks="${new_list# }"
535a88393ceSDevin Teske
536a88393ceSDevin Teske	# Debugging
537a88393ceSDevin Teske	if [ "$debug" ]; then
538a88393ceSDevin Teske		local disk_names=
539a88393ceSDevin Teske		for disk in $disks; do
540a88393ceSDevin Teske			debug= $disk get name name
541a88393ceSDevin Teske			disk_names="$disk_names $name"
542a88393ceSDevin Teske		done
543a88393ceSDevin Teske		f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
544a88393ceSDevin Teske	fi
545a88393ceSDevin Teske
546cd88b886SDevin Teske	if [ ! "$disks" ]; then
547bc4a673fSDevin Teske		f_dprintf "No disk(s) present to configure"
548bc4a673fSDevin Teske		f_show_err "$msg_no_disks_present_to_configure"
549cd88b886SDevin Teske		return $FAILURE
550cd88b886SDevin Teske	fi
551cd88b886SDevin Teske
552cd88b886SDevin Teske	# Lets sort the disks array to be more user friendly
553a88393ceSDevin Teske	f_device_sort_by name disks disks
554cd88b886SDevin Teske
555cd88b886SDevin Teske	#
556bc4a673fSDevin Teske	# Operate in a loop so we can (if interactive) repeat if not enough
557bc4a673fSDevin Teske	# disks are selected to satisfy the chosen vdev type or user wants to
558bc4a673fSDevin Teske	# back-up to the previous menu.
559cd88b886SDevin Teske	#
560a88393ceSDevin Teske	local vardisk ndisks onoff selections vdev_choice breakout device
561a88393ceSDevin Teske	local valid_disks all_valid want_disks desc height width rows
562bc4a673fSDevin Teske	while :; do
563cd88b886SDevin Teske		#
564bc4a673fSDevin Teske		# Confirm the vdev type that was selected
565cd88b886SDevin Teske		#
566bc4a673fSDevin Teske		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
567bc4a673fSDevin Teske			vdev_choice=$( eval $DIALOG \
568cd88b886SDevin Teske				--title \"\$title\"              \
569cd88b886SDevin Teske				--backtitle \"\$btitle\"         \
570bc4a673fSDevin Teske				--hline \"\$vdev_hline\"         \
571cd88b886SDevin Teske				--ok-label \"\$msg_ok\"          \
572cd88b886SDevin Teske				--cancel-label \"\$msg_cancel\"  \
573cd88b886SDevin Teske				--item-help                      \
574bc4a673fSDevin Teske				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
575bc4a673fSDevin Teske				--menu \"\$vdev_prompt\"         \
576bc4a673fSDevin Teske				$vheight $vwidth $vrows          \
577bc4a673fSDevin Teske				$vdev_menu_list                  \
578cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
579bc4a673fSDevin Teske			) || return $?
580bc4a673fSDevin Teske				# Exit if user pressed ESC or chose Cancel/No
581bc4a673fSDevin Teske			f_dialog_data_sanitize vdev_choice
582cd88b886SDevin Teske
583bc4a673fSDevin Teske			ZFSBOOT_VDEV_TYPE="$vdev_choice"
584bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
585bc4a673fSDevin Teske			          "$ZFSBOOT_VDEV_TYPE"
586bc4a673fSDevin Teske		fi
587bc4a673fSDevin Teske
588bc4a673fSDevin Teske		# Determine the number of disks needed for this vdev type
589a88393ceSDevin Teske		want_disks=0
590bc4a673fSDevin Teske		case "$ZFSBOOT_VDEV_TYPE" in
591cd88b886SDevin Teske		stripe) want_disks=1 ;;
592cd88b886SDevin Teske		mirror) want_disks=2 ;;
593bb4fd0a0SAllan Jude		raid10) want_disks=4 ;;
594cd88b886SDevin Teske		raidz1) want_disks=3 ;;
595cd88b886SDevin Teske		raidz2) want_disks=4 ;;
596cd88b886SDevin Teske		raidz3) want_disks=5 ;;
597cd88b886SDevin Teske		esac
598bc4a673fSDevin Teske
599a88393ceSDevin Teske		#
600bc4a673fSDevin Teske		# Warn the user if any scripted disks are invalid
601a88393ceSDevin Teske		#
602a88393ceSDevin Teske		valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
603bc4a673fSDevin Teske		for disk in $ZFSBOOT_DISKS; do
604a88393ceSDevin Teske			if debug= f_device_find -1 \
605a88393ceSDevin Teske				$disk $DEVICE_TYPE_DISK device
606a88393ceSDevin Teske			then
607bc4a673fSDevin Teske				valid_disks="$valid_disks $disk"
608bc4a673fSDevin Teske				continue
609cd88b886SDevin Teske			fi
610bc4a673fSDevin Teske			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
611bc4a673fSDevin Teske			all_valid=
612bc4a673fSDevin Teske		done
613bc4a673fSDevin Teske		if [ ! "$all_valid" ]; then
614bc4a673fSDevin Teske			if [ "$ZFSBOOT_DISKS" ]; then
615bc4a673fSDevin Teske				f_show_err \
616bc4a673fSDevin Teske				    "$msg_missing_one_or_more_scripted_disks"
617bc4a673fSDevin Teske			else
618bc4a673fSDevin Teske				f_dprintf "No disks selected."
619bc4a673fSDevin Teske				f_interactive ||
620bc4a673fSDevin Teske					f_show_err "$msg_no_disks_selected"
621bc4a673fSDevin Teske			fi
622bc4a673fSDevin Teske			f_interactive || return $FAILURE
623bc4a673fSDevin Teske		fi
624bc4a673fSDevin Teske		ZFSBOOT_DISKS="${valid_disks# }"
625bc4a673fSDevin Teske
626bc4a673fSDevin Teske		#
627bc4a673fSDevin Teske		# Short-circuit if we're running non-interactively
628bc4a673fSDevin Teske		#
629bc4a673fSDevin Teske		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
630a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
631bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] && break # to success
632bc4a673fSDevin Teske
633bc4a673fSDevin Teske			# Not enough disks selected
634bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
635bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
636bc4a673fSDevin Teske				  "Not enough disks selected." \
637bc4a673fSDevin Teske				  $ndisks $want_disks
638bc4a673fSDevin Teske			f_interactive || return $FAILURE
639bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
640bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
641bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
642bc4a673fSDevin Teske				return $FAILURE
643bc4a673fSDevin Teske		fi
644bc4a673fSDevin Teske
645bc4a673fSDevin Teske		#
646bc4a673fSDevin Teske		# Confirm the disks that were selected
647bc4a673fSDevin Teske		# Loop until the user cancels or selects enough disks
648bc4a673fSDevin Teske		#
649a88393ceSDevin Teske		breakout=
650bc4a673fSDevin Teske		while :; do
651bc4a673fSDevin Teske			# Loop over list of available disks, resetting state
652a88393ceSDevin Teske			for disk in $disks; do
653a88393ceSDevin Teske				f_isset _${disk}_status && _${disk}_status=
654a88393ceSDevin Teske			done
655bc4a673fSDevin Teske
656bc4a673fSDevin Teske			# Loop over list of selected disks and create temporary
657bc4a673fSDevin Teske			# locals to map statuses onto up-to-date list of disks
658bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
659a88393ceSDevin Teske				debug= f_device_find -1 \
660a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK disk
661a88393ceSDevin Teske				f_isset _${disk}_status ||
662a88393ceSDevin Teske					local _${disk}_status
663a88393ceSDevin Teske				_${disk}_status=on
664bc4a673fSDevin Teske			done
665bc4a673fSDevin Teske
666bc4a673fSDevin Teske			# Create the checklist menu of discovered disk devices
667bc4a673fSDevin Teske			disk_check_list=
668bc4a673fSDevin Teske			for disk in $disks; do
669a88393ceSDevin Teske				desc=
670a88393ceSDevin Teske				$disk get name name
671a88393ceSDevin Teske				$disk get desc desc
672bc4a673fSDevin Teske				f_shell_escape "$desc" desc
673bc4a673fSDevin Teske				f_getvar _${disk}_status:-off onoff
674bc4a673fSDevin Teske				disk_check_list="$disk_check_list
675a88393ceSDevin Teske					$name '$desc' $onoff"
676bc4a673fSDevin Teske			done
677bc4a673fSDevin Teske
678bc4a673fSDevin Teske			eval f_dialog_checklist_size height width rows \
679bc4a673fSDevin Teske				\"\$title\" \"\$btitle\" \"\$prompt\" \
680bc4a673fSDevin Teske				\"\$hline\" $disk_check_list
681bc4a673fSDevin Teske
682bc4a673fSDevin Teske			selections=$( eval $DIALOG \
683bc4a673fSDevin Teske				--title \"\$DIALOG_TITLE\"         \
684bc4a673fSDevin Teske				--backtitle \"\$DIALOG_BACKTITLE\" \
685751952aeSDevin Teske				--separate-output                  \
686bc4a673fSDevin Teske				--hline \"\$hline\"                \
687bc4a673fSDevin Teske				--ok-label \"\$msg_ok\"            \
688bc4a673fSDevin Teske				--cancel-label \"\$msg_back\"      \
689bc4a673fSDevin Teske				--checklist \"\$prompt\"           \
690bc4a673fSDevin Teske				$height $width $rows               \
691bc4a673fSDevin Teske				$disk_check_list                   \
692bc4a673fSDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
693bc4a673fSDevin Teske			) || break
694bc4a673fSDevin Teske				# Loop if user pressed ESC or chose Cancel/No
695bc4a673fSDevin Teske			f_dialog_data_sanitize selections
696bc4a673fSDevin Teske
697bc4a673fSDevin Teske			ZFSBOOT_DISKS="$selections"
698bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
699bc4a673fSDevin Teske			          "$ZFSBOOT_DISKS"
700bc4a673fSDevin Teske
701a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
702bb4fd0a0SAllan Jude
703bb4fd0a0SAllan Jude			if [ "$ZFSBOOT_VDEV_TYPE" == "raid10" ] &&
704bb4fd0a0SAllan Jude			    [ $(( $ndisks % 2 )) -ne 0 ]; then
705bb4fd0a0SAllan Jude				f_dprintf "$funcname: %s: %s (%u %% 2 = %u)" \
706bb4fd0a0SAllan Jude					  "$ZFSBOOT_VDEV_TYPE" \
707bb4fd0a0SAllan Jude					  "Number of disks not even:" \
708bb4fd0a0SAllan Jude					  $ndisks $(( $ndisks % 2 ))
709bb4fd0a0SAllan Jude				msg_yes="$msg_change_selection" \
710bb4fd0a0SAllan Jude					msg_no="$msg_cancel" \
711bb4fd0a0SAllan Jude					f_yesno "%s: $msg_odd_disk_selected" \
712bb4fd0a0SAllan Jude						"$ZFSBOOT_VDEV_TYPE" $ndisks ||
713bb4fd0a0SAllan Jude						break
714bb4fd0a0SAllan Jude				continue
715bb4fd0a0SAllan Jude			fi
716bb4fd0a0SAllan Jude
717bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] &&
718bc4a673fSDevin Teske				breakout=break && break
719bc4a673fSDevin Teske
720bc4a673fSDevin Teske			# Not enough disks selected
721bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
722bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
723bc4a673fSDevin Teske			          "Not enough disks selected." \
724bc4a673fSDevin Teske			          $ndisks $want_disks
725bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
726bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
727bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
728bc4a673fSDevin Teske				break
729bc4a673fSDevin Teske		done
730bc4a673fSDevin Teske		[ "$breakout" = "break" ] && break
731bc4a673fSDevin Teske		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
732bc4a673fSDevin Teske	done
733bc4a673fSDevin Teske
734bc4a673fSDevin Teske	return $DIALOG_OK
735cd88b886SDevin Teske}
736cd88b886SDevin Teske
737cd88b886SDevin Teske# zfs_create_diskpart $disk $index
738cd88b886SDevin Teske#
739cd88b886SDevin Teske# For each block device to be used in the zpool, rather than just create the
740cd88b886SDevin Teske# zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
741cd88b886SDevin Teske# so we can have some real swap. This also provides wiggle room incase your
742cd88b886SDevin Teske# replacement drivers do not have the exact same sector counts.
743cd88b886SDevin Teske#
7447cae6aabSDevin Teske# NOTE: $swapsize and $bootsize should be defined by the calling function.
745a622223fSDevin Teske# NOTE: Sets $bootpart and $targetpart for the calling function.
746cd88b886SDevin Teske#
747cd88b886SDevin Teskezfs_create_diskpart()
748cd88b886SDevin Teske{
749cd88b886SDevin Teske	local funcname=zfs_create_diskpart
750bc4a673fSDevin Teske	local disk="$1" index="$2"
751cd88b886SDevin Teske
752cd88b886SDevin Teske	# Check arguments
753bc4a673fSDevin Teske	if [ ! "$disk" ]; then
754bc4a673fSDevin Teske		f_dprintf "$funcname: NULL disk argument"
755bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
756bc4a673fSDevin Teske			f_show_err "$msg_null_disk_argument"
757bc4a673fSDevin Teske		return $FAILURE
758bc4a673fSDevin Teske	fi
759bc4a673fSDevin Teske	if [ "${disk#*[$IFS]}" != "$disk" ]; then
760bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
761bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
762bc4a673fSDevin Teske			f_show_err "$msg_invalid_disk_argument" "$disk"
763bc4a673fSDevin Teske		return $FAILURE
764bc4a673fSDevin Teske	fi
765bc4a673fSDevin Teske	if [ ! "$index" ]; then
766bc4a673fSDevin Teske		f_dprintf "$funcname: NULL index argument"
767bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
768bc4a673fSDevin Teske			f_show_err "$msg_null_index_argument"
769bc4a673fSDevin Teske		return $FAILURE
770bc4a673fSDevin Teske	fi
771bc4a673fSDevin Teske	if ! f_isinteger "$index"; then
772bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
773bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
774bc4a673fSDevin Teske			f_show_err "$msg_invalid_index_argument" "$index"
775bc4a673fSDevin Teske		return $FAILURE
776bc4a673fSDevin Teske	fi
777bc4a673fSDevin Teske	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
778cd88b886SDevin Teske
779d4d729e4SDevin Teske	# Check for unknown partition scheme before proceeding further
780d4d729e4SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
7817059fa6fSAllan Jude	""|MBR|GPT*) : known good ;;
782d4d729e4SDevin Teske	*)
783d4d729e4SDevin Teske		f_dprintf "$funcname: %s is an unsupported partition scheme" \
784d4d729e4SDevin Teske		          "$ZFSBOOT_PARTITION_SCHEME"
785d4d729e4SDevin Teske		msg_error="$msg_error: $funcname" f_show_err \
786d4d729e4SDevin Teske			"$msg_unsupported_partition_scheme" \
787d4d729e4SDevin Teske			"$ZFSBOOT_PARTITION_SCHEME"
788d4d729e4SDevin Teske		return $FAILURE
789d4d729e4SDevin Teske	esac
790d4d729e4SDevin Teske
791cd88b886SDevin Teske	#
792cd88b886SDevin Teske	# Destroy whatever partition layout is currently on disk.
793cd88b886SDevin Teske	# NOTE: `-F' required to destroy if partitions still exist.
794cd88b886SDevin Teske	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
795cd88b886SDevin Teske	#
7961b63cafbSAllan Jude	f_dprintf "$funcname: Exporting ZFS pools..."
7971b63cafbSAllan Jude	zpool list -Ho name | while read z_name; do
7981b63cafbSAllan Jude		f_eval_catch -d $funcname zpool "$ZPOOL_EXPORT_F" $z_name
7991b63cafbSAllan Jude	done
8001b63cafbSAllan Jude	f_dprintf "$funcname: Detaching all GELI providers..."
8011b63cafbSAllan Jude	geli status | tail -n +2 | while read g_name g_status g_component; do
8021b63cafbSAllan Jude		f_eval_catch -d $funcname geli "$GELI_DETACH_F" $g_name
8031b63cafbSAllan Jude	done
804bc4a673fSDevin Teske	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
805a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
806a622223fSDevin Teske	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
807a622223fSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
808cd88b886SDevin Teske
809cd88b886SDevin Teske	# Make doubly-sure backup GPT is destroyed
810a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
811a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
812cd88b886SDevin Teske
8137cae6aabSDevin Teske	#
814cd88b886SDevin Teske	# Lay down the desired type of partition scheme
815cd88b886SDevin Teske	#
81669e4b249SAllan Jude	local setsize mbrindex align_small align_big
81769e4b249SAllan Jude	#
81869e4b249SAllan Jude	# If user has requested 4 K alignment, add these params to the
81969e4b249SAllan Jude	# gpart add calls. With GPT, we align large partitions to 1 M for
82069e4b249SAllan Jude	# improved performance on SSDs. MBR does not always play well with gaps
82169e4b249SAllan Jude	# between partitions, so all alignment is only 4k for that case.
82269e4b249SAllan Jude	# With MBR, we align the BSD partition that contains the MBR, otherwise
82369e4b249SAllan Jude	# the system fails to boot.
82469e4b249SAllan Jude	#
82569e4b249SAllan Jude	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
82669e4b249SAllan Jude		align_small="-a 4k"
82769e4b249SAllan Jude		align_big="-a 1m"
82869e4b249SAllan Jude	fi
82969e4b249SAllan Jude
830cd88b886SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
8317059fa6fSAllan Jude	""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
832cd88b886SDevin Teske		#
833cd88b886SDevin Teske		# 1. Create GPT layout using labels
834cd88b886SDevin Teske		#
835a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
836bc4a673fSDevin Teske		             return $FAILURE
837cd88b886SDevin Teske
838cd88b886SDevin Teske		#
8397059fa6fSAllan Jude		# Apply workarounds if requested by the user
8407059fa6fSAllan Jude		#
8417059fa6fSAllan Jude		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Lenovo Fix" ]; then
8427059fa6fSAllan Jude			f_eval_catch $funcname gpart "$GPART_SET_LENOVOFIX" \
8437059fa6fSAllan Jude			             $disk || return $FAILURE
8447059fa6fSAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
8457059fa6fSAllan Jude			f_eval_catch $funcname gpart "$GPART_SET_PMBR_ACTIVE" \
8467059fa6fSAllan Jude			             $disk || return $FAILURE
8477059fa6fSAllan Jude		fi
8487059fa6fSAllan Jude
8497059fa6fSAllan Jude		#
850a1b8f55cSAllan Jude		# 2. Add small freebsd-boot and/or efi partition
851cd88b886SDevin Teske		#
852ee21b0efSDevin Teske		if [ "$ZFSBOOT_BOOT_TYPE" = "UEFI" -o \
853ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
854ee21b0efSDevin Teske		then
8550b7472b3SNathan Whitehorn			f_eval_catch -k justaddedpart $funcname gpart \
8560edc8cc8SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
8570b7472b3SNathan Whitehorn			             "$align_small" efiboot$index efi 260M \
858ee21b0efSDevin Teske			             $disk || return $FAILURE
859aa2a0e0fSAllan Jude
860db8b5613SRebecca Cran			# We'll configure the ESP in bootconfig
8610b7472b3SNathan Whitehorn			if [ -z "$efibootpart" ]; then
8620b7472b3SNathan Whitehorn				efibootpart="/dev/$(echo $justaddedpart | cut -f 1 -d ' ')"
8630b7472b3SNathan Whitehorn				f_dprintf "$funcname: configuring ESP at [%s]" \
8640b7472b3SNathan Whitehorn				          "${efibootpart}"
8650b7472b3SNathan Whitehorn
8660b7472b3SNathan Whitehorn				f_eval_catch $funcname newfs_msdos "$NEWFS_ESP"\
8670b7472b3SNathan Whitehorn				             "$efibootpart" \
8680b7472b3SNathan Whitehorn				             || return $FAILURE
8690b7472b3SNathan Whitehorn				f_eval_catch $funcname printf "$PRINTF_FSTAB" \
8700b7472b3SNathan Whitehorn					     $efibootpart /boot/efi msdosfs \
8710b7472b3SNathan Whitehorn				             rw 2 2 "$BSDINSTALL_TMPETC/fstab" \
8720b7472b3SNathan Whitehorn				             || return $FAILURE
8730b7472b3SNathan Whitehorn			fi
8744d41f472SAllan Jude		fi
8754d41f472SAllan Jude
876ee21b0efSDevin Teske		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" -o \
877ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
878ee21b0efSDevin Teske		then
8790edc8cc8SAllan Jude			f_eval_catch $funcname gpart \
8800edc8cc8SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
881ee21b0efSDevin Teske			             "$align_small" gptboot$index \
882ee21b0efSDevin Teske			             freebsd-boot 512k $disk || return $FAILURE
8834d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]; then
884ee21b0efSDevin Teske				f_eval_catch $funcname gpart \
885ee21b0efSDevin Teske				             "$GPART_BOOTCODE_PART" \
886ee21b0efSDevin Teske				             /boot/pmbr /boot/gptzfsboot 1 \
887ee21b0efSDevin Teske				             $disk || return $FAILURE
8884d41f472SAllan Jude			else
889ee21b0efSDevin Teske				f_eval_catch $funcname gpart \
890ee21b0efSDevin Teske				             "$GPART_BOOTCODE_PART" \
891ee21b0efSDevin Teske				             /boot/pmbr /boot/gptzfsboot 2 \
892ee21b0efSDevin Teske				             $disk || return $FAILURE
8934d41f472SAllan Jude			fi
8940edc8cc8SAllan Jude		fi
895cd88b886SDevin Teske
896a622223fSDevin Teske		# NB: zpool will use the `zfs#' GPT labels
8974d41f472SAllan Jude		if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]; then
8984d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
8994d41f472SAllan Jude				bootpart=p3 swappart=p4 targetpart=p4
9004d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p5
9014d41f472SAllan Jude			else
9024d41f472SAllan Jude				# Bootpart unused
9034d41f472SAllan Jude				bootpart=p3 swappart=p3 targetpart=p3
9044d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
9054d41f472SAllan Jude			fi
9064d41f472SAllan Jude		else
9074d41f472SAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9084d41f472SAllan Jude				bootpart=p2 swappart=p3 targetpart=p3
9094d41f472SAllan Jude				[ ${swapsize:-0} -gt 0 ] && targetpart=p4
9104d41f472SAllan Jude			else
9114d41f472SAllan Jude				# Bootpart unused
9122875e59fSOllivier Robert				bootpart=p2 swappart=p2 targetpart=p2
9137cae6aabSDevin Teske				[ ${swapsize:-0} -gt 0 ] && targetpart=p3
9144d41f472SAllan Jude			fi
9154d41f472SAllan Jude		fi
916cd88b886SDevin Teske
9177cae6aabSDevin Teske		#
9187cae6aabSDevin Teske		# Prepare boot pool if enabled (e.g., for geli(8))
9197cae6aabSDevin Teske		#
9207cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
921bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
92269e4b249SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
92369e4b249SAllan Jude			             "$align_big" boot$index freebsd-zfs \
924ee21b0efSDevin Teske			             ${bootsize}b $disk || return $FAILURE
9257cae6aabSDevin Teske			# Pedantically nuke any old labels
926a622223fSDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
927a622223fSDevin Teske			                /dev/$disk$bootpart
9287cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
9297cae6aabSDevin Teske				# Pedantically detach targetpart for later
9307cae6aabSDevin Teske				f_eval_catch -d $funcname geli \
9317cae6aabSDevin Teske				                "$GELI_DETACH_F" \
932a622223fSDevin Teske				                /dev/$disk$targetpart
933cd88b886SDevin Teske			fi
9347cae6aabSDevin Teske		fi
935cd88b886SDevin Teske
936cd88b886SDevin Teske		#
9377cae6aabSDevin Teske		# 3. Add freebsd-swap partition labeled `swap#'
938cd88b886SDevin Teske		#
939bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
940bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
94169e4b249SAllan Jude			             "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
94269e4b249SAllan Jude			             "$align_big" swap$index freebsd-swap \
943ee21b0efSDevin Teske			             ${swapsize}b $disk || return $FAILURE
944cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
945cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
9462875e59fSOllivier Robert			                /dev/$disk$swappart
947cd88b886SDevin Teske		fi
9487cae6aabSDevin Teske
9497cae6aabSDevin Teske		#
9507cae6aabSDevin Teske		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
9517cae6aabSDevin Teske		#
9527ef92163SJohn Ko		if [ "$ZFSBOOT_POOL_SIZE" ]; then
9537ef92163SJohn Ko			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
9547ef92163SJohn Ko					"$align_big" zfs$index freebsd-zfs $ZFSBOOT_POOL_SIZE $disk ||
9557ef92163SJohn Ko					return $FAILURE
9567ef92163SJohn Ko		else
95769e4b249SAllan Jude			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL" \
95869e4b249SAllan Jude					"$align_big" zfs$index freebsd-zfs $disk ||
95969e4b249SAllan Jude					return $FAILURE
9607ef92163SJohn Ko		fi
9617cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
9627cae6aabSDevin Teske		                /dev/$disk$targetpart
963cd88b886SDevin Teske		;;
964cd88b886SDevin Teske
965bc4a673fSDevin Teske	MBR) f_dprintf "$funcname: Creating MBR layout..."
966cd88b886SDevin Teske		#
967a1b8f55cSAllan Jude		# Enable boot pool if encryption is desired
968a1b8f55cSAllan Jude		#
969a1b8f55cSAllan Jude		[ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
970a1b8f55cSAllan Jude		#
971cd88b886SDevin Teske		# 1. Create MBR layout (no labels)
972cd88b886SDevin Teske		#
973a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
974bc4a673fSDevin Teske		             return $FAILURE
9752925848dSDevin Teske		f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
976a622223fSDevin Teske		             $disk || return $FAILURE
977cd88b886SDevin Teske
978cd88b886SDevin Teske		#
979cd88b886SDevin Teske		# 2. Add freebsd slice with all available space
980cd88b886SDevin Teske		#
981ee21b0efSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_ALIGN" \
982ee21b0efSDevin Teske		             "$align_small" freebsd $disk || return $FAILURE
983a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
984bc4a673fSDevin Teske		             return $FAILURE
985a622223fSDevin Teske		# Pedantically nuke any old labels
986a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
987a622223fSDevin Teske		                /dev/${disk}s1
988a622223fSDevin Teske		# Pedantically nuke any old scheme
989a622223fSDevin Teske		f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
990cd88b886SDevin Teske
991cd88b886SDevin Teske		#
992bc4a673fSDevin Teske		# 3. Write BSD scheme to the freebsd slice
993cd88b886SDevin Teske		#
994a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
995bc4a673fSDevin Teske		             return $FAILURE
996cd88b886SDevin Teske
9977cae6aabSDevin Teske		# NB: zpool will use s1a (no labels)
9982875e59fSOllivier Robert		bootpart=s1a swappart=s1b targetpart=s1d mbrindex=4
999cd88b886SDevin Teske
10007cae6aabSDevin Teske		#
10017cae6aabSDevin Teske		# Always prepare a boot pool on MBR
1002a94af954SAllan Jude		# Do not align this partition, there must not be a gap
10037cae6aabSDevin Teske		#
10047cae6aabSDevin Teske		ZFSBOOT_BOOT_POOL=1
1005bc4a673fSDevin Teske		f_eval_catch $funcname gpart \
100669e4b249SAllan Jude		             "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
1007a94af954SAllan Jude		             "" 1 freebsd-zfs ${bootsize}b ${disk}s1 ||
1008bc4a673fSDevin Teske		             return $FAILURE
1009a622223fSDevin Teske		# Pedantically nuke any old labels
1010a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
1011a622223fSDevin Teske		                /dev/$disk$bootpart
10127cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1013a622223fSDevin Teske			# Pedantically detach targetpart for later
1014a622223fSDevin Teske			f_eval_catch -d $funcname geli \
1015a622223fSDevin Teske			                "$GELI_DETACH_F" \
1016a622223fSDevin Teske					/dev/$disk$targetpart
1017cd88b886SDevin Teske		fi
1018cd88b886SDevin Teske
1019cd88b886SDevin Teske		#
10207cae6aabSDevin Teske		# 4. Add freebsd-swap partition
1021cd88b886SDevin Teske		#
1022bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
1023bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
102469e4b249SAllan Jude			             "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
1025ee21b0efSDevin Teske			             "$align_small" 2 freebsd-swap \
1026ee21b0efSDevin Teske			             ${swapsize}b ${disk}s1 || return $FAILURE
1027cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
1028cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
1029cf9c3e56SDevin Teske			                /dev/${disk}s1b
1030cd88b886SDevin Teske		fi
10317cae6aabSDevin Teske
10327cae6aabSDevin Teske		#
10337cae6aabSDevin Teske		# 5. Add freebsd-zfs partition for zroot
10347cae6aabSDevin Teske		#
10357ef92163SJohn Ko		if [ "$ZFSBOOT_POOL_SIZE" ]; then
10367ef92163SJohn Ko			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
10377ef92163SJohn Ko					"$align_small" $mbrindex freebsd-zfs $ZFSBOOT_POOL_SIZE ${disk}s1 || return $FAILURE
10387ef92163SJohn Ko		else
103969e4b249SAllan Jude			f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_INDEX" \
10407ef92163SJohn Ko					"$align_small" $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
10417ef92163SJohn Ko		fi
10427cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
10437cae6aabSDevin Teske		                /dev/$disk$targetpart # Pedantic
10447cae6aabSDevin Teske		f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
10457cae6aabSDevin Teske		             /boot/zfsboot /dev/${disk}s1 count=1 ||
10467cae6aabSDevin Teske		             return $FAILURE
1047cd88b886SDevin Teske		;;
1048cd88b886SDevin Teske
1049cd88b886SDevin Teske	esac # $ZFSBOOT_PARTITION_SCHEME
1050cd88b886SDevin Teske
10512875e59fSOllivier Robert	# Update fstab(5)
1052caa3817dSAllan Jude	local swapsize
1053caa3817dSAllan Jude	f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
10542875e59fSOllivier Robert	if [ "$isswapmirror" ]; then
10552875e59fSOllivier Robert		# This is not the first disk in the mirror, do nothing
10566b5e33baSAllan Jude	elif [ ${swapsize:-0} -eq 0 ]; then
10576b5e33baSAllan Jude		# If swap is 0 sized, don't add it to fstab
10582875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
10592875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
10602875e59fSOllivier Robert		             /dev/mirror/swap.eli none swap sw 0 0 \
1061ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
10622875e59fSOllivier Robert		isswapmirror=1
10632875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
10642875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
10652875e59fSOllivier Robert		             /dev/mirror/swap none swap sw 0 0 \
1066ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
10672875e59fSOllivier Robert		isswapmirror=1
10682875e59fSOllivier Robert	elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
10692875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
10702875e59fSOllivier Robert		             /dev/$disk${swappart}.eli none swap sw 0 0 \
1071ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
10722875e59fSOllivier Robert	else
10732875e59fSOllivier Robert		f_eval_catch $funcname printf "$PRINTF_FSTAB" \
10742875e59fSOllivier Robert		             /dev/$disk$swappart none swap sw 0 0 \
1075ee21b0efSDevin Teske		             $BSDINSTALL_TMPETC/fstab || return $FAILURE
10762875e59fSOllivier Robert	fi
10772875e59fSOllivier Robert
1078cd88b886SDevin Teske	return $SUCCESS
1079cd88b886SDevin Teske}
1080cd88b886SDevin Teske
1081bc4a673fSDevin Teske# zfs_create_boot $poolname $vdev_type $disks ...
1082cd88b886SDevin Teske#
1083cd88b886SDevin Teske# Creates boot pool and dataset layout. Returns error if something goes wrong.
1084cd88b886SDevin Teske# Errors are printed to stderr for collection and display.
1085cd88b886SDevin Teske#
1086cd88b886SDevin Teskezfs_create_boot()
1087cd88b886SDevin Teske{
1088cd88b886SDevin Teske	local funcname=zfs_create_boot
10897cae6aabSDevin Teske	local zroot_name="$1"
10907cae6aabSDevin Teske	local zroot_vdevtype="$2"
10917cae6aabSDevin Teske	local zroot_vdevs= # Calculated below
10922875e59fSOllivier Robert	local swap_devs= # Calculated below
10937cae6aabSDevin Teske	local boot_vdevs= # Used for geli(8) and/or MBR layouts
1094a622223fSDevin Teske	shift 2 # poolname vdev_type
1095a622223fSDevin Teske	local disks="$*" disk
10962875e59fSOllivier Robert	local isswapmirror
10972875e59fSOllivier Robert	local bootpart targetpart swappart # Set by zfs_create_diskpart() below
109847206692SDevin Teske	local create_options
10990b7472b3SNathan Whitehorn	local efibootpart
1100cd88b886SDevin Teske
1101a622223fSDevin Teske	#
1102cd88b886SDevin Teske	# Pedantic checks; should never be seen
1103a622223fSDevin Teske	#
11047cae6aabSDevin Teske	if [ ! "$zroot_name" ]; then
1105bc4a673fSDevin Teske		f_dprintf "$funcname: NULL poolname"
1106bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
1107bc4a673fSDevin Teske			f_show_err "$msg_null_poolname"
1108cd88b886SDevin Teske		return $FAILURE
1109cd88b886SDevin Teske	fi
1110cd88b886SDevin Teske	if [ $# -lt 1 ]; then
1111bc4a673fSDevin Teske		f_dprintf "$funcname: missing disk arguments"
1112bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
1113bc4a673fSDevin Teske			f_show_err "$msg_missing_disk_arguments"
1114cd88b886SDevin Teske		return $FAILURE
1115cd88b886SDevin Teske	fi
1116bc4a673fSDevin Teske	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
11177cae6aabSDevin Teske	          "$zroot_name" "$zroot_vdevtype"
1118cd88b886SDevin Teske
1119a622223fSDevin Teske	#
1120cd88b886SDevin Teske	# Initialize fstab(5)
1121a622223fSDevin Teske	#
1122bc4a673fSDevin Teske	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1123a622223fSDevin Teske	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1124bc4a673fSDevin Teske	             return $FAILURE
1125bc4a673fSDevin Teske	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1126cd88b886SDevin Teske	             "# Device" Mountpoint FStype Options Dump "Pass#" \
1127a622223fSDevin Teske	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
1128cd88b886SDevin Teske
1129a622223fSDevin Teske	#
1130cd88b886SDevin Teske	# Expand SI units in desired sizes
1131a622223fSDevin Teske	#
11327cae6aabSDevin Teske	f_dprintf "$funcname: Expanding supplied size values..."
11337ef92163SJohn Ko	local swapsize bootsize poolsize
1134bc4a673fSDevin Teske	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1135bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid swap size \`%s'" \
1136bc4a673fSDevin Teske		          "$ZFSBOOT_SWAP_SIZE"
1137bc4a673fSDevin Teske		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1138bc4a673fSDevin Teske		return $FAILURE
1139bc4a673fSDevin Teske	fi
11407cae6aabSDevin Teske	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
11417cae6aabSDevin Teske		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
11427cae6aabSDevin Teske		          "$ZFSBOOT_BOOT_POOL_SIZE"
11437cae6aabSDevin Teske		f_show_err "$msg_invalid_boot_pool_size" \
11447cae6aabSDevin Teske		           "$ZFSBOOT_BOOT_POOL_SIZE"
1145bc4a673fSDevin Teske		return $FAILURE
1146bc4a673fSDevin Teske	fi
11477ef92163SJohn Ko	if [ "$ZFSBOOT_POOL_SIZE" ]; then
11487ef92163SJohn Ko		if ! f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize; then
11497ef92163SJohn Ko			f_dprintf "$funcname: Invalid pool size \`%s'" \
11507ef92163SJohn Ko				  "$ZFSBOOT_POOL_SIZE"
11517ef92163SJohn Ko			f_show_err "$msg_invalid_pool_size" \
11527ef92163SJohn Ko				   "$ZFSBOOT_POOL_SIZE"
11537ef92163SJohn Ko		fi
11547ef92163SJohn Ko		f_dprintf "$funcname: ZFSBOOT_POOL_SIZE=[%s] poolsize=[%s]" \
11557ef92163SJohn Ko			  "$ZFSBOOT_POOL_SIZE" "$poolsize"
11567ef92163SJohn Ko	fi
1157bc4a673fSDevin Teske	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1158bc4a673fSDevin Teske	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
11597cae6aabSDevin Teske	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
11607cae6aabSDevin Teske	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1161cd88b886SDevin Teske
1162a622223fSDevin Teske	#
1163a88393ceSDevin Teske	# Destroy the pool in-case this is our second time 'round (case of
1164a88393ceSDevin Teske	# failure and installer presented ``Retry'' option to come back).
1165a88393ceSDevin Teske	#
1166a88393ceSDevin Teske	# NB: If we don't destroy the pool, later gpart(8) destroy commands
1167a88393ceSDevin Teske	# that try to clear existing partitions (see zfs_create_diskpart())
1168a88393ceSDevin Teske	# will fail with a `Device Busy' error, leading to `GEOM exists'.
1169a88393ceSDevin Teske	#
1170a88393ceSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1171a88393ceSDevin Teske
1172a88393ceSDevin Teske	#
11737cae6aabSDevin Teske	# Prepare the disks and build pool device list(s)
1174a622223fSDevin Teske	#
1175bc4a673fSDevin Teske	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1176b4843bd6SSteven Hartland
1177b4843bd6SSteven Hartland	# Force 4K sectors using vfs.zfs.min_auto_ashift=12
1178b4843bd6SSteven Hartland	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1179b4843bd6SSteven Hartland		f_dprintf "$funcname: With 4K sectors..."
1180b4843bd6SSteven Hartland		f_eval_catch $funcname sysctl "$SYSCTL_ZFS_MIN_ASHIFT_12" \
1181b4843bd6SSteven Hartland		    || return $FAILURE
118224d17f1eSAllan Jude		sysctl kern.geom.part.mbr.enforce_chs=0
1183b4843bd6SSteven Hartland	fi
1184cd88b886SDevin Teske	local n=0
1185a622223fSDevin Teske	for disk in $disks; do
1186cd88b886SDevin Teske		zfs_create_diskpart $disk $n || return $FAILURE
11872875e59fSOllivier Robert		# Now $bootpart, $targetpart, and $swappart are set (suffix
11882875e59fSOllivier Robert		# for $disk)
11897cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
11907cae6aabSDevin Teske			boot_vdevs="$boot_vdevs $disk$bootpart"
11917cae6aabSDevin Teske		fi
11927cae6aabSDevin Teske		zroot_vdevs="$zroot_vdevs $disk$targetpart"
1193e751d176SSteven Hartland		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1194e751d176SSteven Hartland			zroot_vdevs="$zroot_vdevs.eli"
1195e751d176SSteven Hartland		fi
11967cae6aabSDevin Teske
11977cae6aabSDevin Teske		n=$(( $n + 1 ))
11987cae6aabSDevin Teske	done # disks
11997cae6aabSDevin Teske
12007cae6aabSDevin Teske	#
12017cae6aabSDevin Teske	# If we need/want a boot pool, create it
12027cae6aabSDevin Teske	#
12037cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
12047cae6aabSDevin Teske		local bootpool_vdevtype= # Calculated below
12057cae6aabSDevin Teske		local bootpool_options= # Calculated below
12067cae6aabSDevin Teske		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
12077cae6aabSDevin Teske		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
12087cae6aabSDevin Teske		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
12097cae6aabSDevin Teske
12107cae6aabSDevin Teske		f_dprintf "$funcname: Setting up boot pool..."
1211cd88b886SDevin Teske		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
12127cae6aabSDevin Teske			f_dprintf "$funcname: For encrypted root disk..."
1213cd88b886SDevin Teske
12147cae6aabSDevin Teske		# Create parent directory for boot pool
1215a107ddbbSRebecca Cran		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1216bc4a673fSDevin Teske		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1217a622223fSDevin Teske		             $BSDINSTALL_CHROOT || return $FAILURE
1218cd88b886SDevin Teske
12197cae6aabSDevin Teske		# Create mirror across the boot partition on all disks
1220a88393ceSDevin Teske		local nvdevs
1221a88393ceSDevin Teske		f_count nvdevs $boot_vdevs
1222a88393ceSDevin Teske		[ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1223cd88b886SDevin Teske
122447206692SDevin Teske		create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
12257cae6aabSDevin Teske		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
122647206692SDevin Teske		bootpool_options="$bootpool_options $create_options"
12277cae6aabSDevin Teske		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1228bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
12297cae6aabSDevin Teske		             "$bootpool_options" "$bootpool_name" \
12304b4b90d5SDevin Teske		             "$bootpool_vdevtype" "$boot_vdevs" ||
12314b4b90d5SDevin Teske		             return $FAILURE
123245df402eSDevin Teske
12337cae6aabSDevin Teske		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1234cd88b886SDevin Teske		             return $FAILURE
1235cd88b886SDevin Teske
12367cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1237cd88b886SDevin Teske			# Generate an encryption key using random(4)
1238bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
12397cae6aabSDevin Teske			             /dev/random "$bootpool/$zroot_key" \
12407cae6aabSDevin Teske			             "bs=4096 count=1" || return $FAILURE
12411ebd9bd4SDevin Teske			f_eval_catch $funcname chmod "$CHMOD_MODE" \
12424ef40262SDevin Teske			             go-wrx "$bootpool/$zroot_key" ||
12434ef40262SDevin Teske			             return $FAILURE
12447cae6aabSDevin Teske		fi
12457cae6aabSDevin Teske
12467cae6aabSDevin Teske	fi
1247cd88b886SDevin Teske
1248a622223fSDevin Teske	#
1249cd88b886SDevin Teske	# Create the geli(8) GEOMS
1250a622223fSDevin Teske	#
12517cae6aabSDevin Teske	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1252a1b8f55cSAllan Jude		#
1253a1b8f55cSAllan Jude		# Load the AES-NI kernel module to accelerate encryption
1254a1b8f55cSAllan Jude		#
1255a1b8f55cSAllan Jude		f_eval_catch -d $funcname kldload "$KLDLOAD" "aesni"
12567cae6aabSDevin Teske		# Prompt user for password (twice)
1257bc4a673fSDevin Teske		if ! msg_enter_new_password="$msg_geli_password" \
1258bc4a673fSDevin Teske			f_dialog_input_password
1259bc4a673fSDevin Teske		then
1260bc4a673fSDevin Teske			f_dprintf "$funcname: User cancelled"
1261bc4a673fSDevin Teske			f_show_err "$msg_user_cancelled"
1262bc4a673fSDevin Teske			return $FAILURE
1263bc4a673fSDevin Teske		fi
1264a622223fSDevin Teske
1265a622223fSDevin Teske		# Initialize geli(8) on each of the target partitions
1266bc4a673fSDevin Teske		for disk in $disks; do
1267cd88b886SDevin Teske			f_dialog_info "$msg_geli_setup" \
1268cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1269a1b8f55cSAllan Jude			if [ "$ZFSBOOT_BOOT_POOL" ]; then
1270a622223fSDevin Teske				if ! echo "$pw_password" | f_eval_catch \
1271a622223fSDevin Teske					$funcname geli "$GELI_PASSWORD_INIT" \
12727cae6aabSDevin Teske					"$bootpool/boot/$disk$targetpart.eli" \
12737cae6aabSDevin Teske					AES-XTS "$bootpool/$zroot_key" \
1274a622223fSDevin Teske					$disk$targetpart
1275bc4a673fSDevin Teske				then
1276bc4a673fSDevin Teske					f_interactive || f_die
1277a622223fSDevin Teske					unset pw_password # Sensitive info
1278cd88b886SDevin Teske					return $FAILURE
1279bc4a673fSDevin Teske				fi
1280a622223fSDevin Teske				if ! echo "$pw_password" | f_eval_catch \
1281a622223fSDevin Teske					$funcname geli "$GELI_ATTACH" \
12827cae6aabSDevin Teske					"$bootpool/$zroot_key" $disk$targetpart
1283bc4a673fSDevin Teske				then
1284bc4a673fSDevin Teske					f_interactive || f_die
1285a622223fSDevin Teske					unset pw_password # Sensitive info
1286bc4a673fSDevin Teske					return $FAILURE
1287bc4a673fSDevin Teske				fi
1288a1b8f55cSAllan Jude			else
1289a1b8f55cSAllan Jude				# With no bootpool, there is no place to store
1290a1b8f55cSAllan Jude				# the key files, use only a password
1291a1b8f55cSAllan Jude				if ! echo "$pw_password" | f_eval_catch \
1292a1b8f55cSAllan Jude					$funcname geli \
1293ee21b0efSDevin Teske					"$GELI_PASSWORD_GELIBOOT_INIT" \
1294ee21b0efSDevin Teske					AES-XTS $disk$targetpart
1295a1b8f55cSAllan Jude				then
1296a1b8f55cSAllan Jude					f_interactive || f_die
1297a1b8f55cSAllan Jude					unset pw_password # Sensitive info
1298a1b8f55cSAllan Jude					return $FAILURE
1299a1b8f55cSAllan Jude				fi
1300a1b8f55cSAllan Jude				if ! echo "$pw_password" | f_eval_catch \
1301a1b8f55cSAllan Jude					$funcname geli "$GELI_ATTACH_NOKEY" \
1302a1b8f55cSAllan Jude					$disk$targetpart
1303a1b8f55cSAllan Jude				then
1304a1b8f55cSAllan Jude					f_interactive || f_die
1305a1b8f55cSAllan Jude					unset pw_password # Sensitive info
1306a1b8f55cSAllan Jude					return $FAILURE
1307a1b8f55cSAllan Jude				fi
1308a1b8f55cSAllan Jude			fi
1309cd88b886SDevin Teske		done
1310a622223fSDevin Teske		unset pw_password # Sensitive info
1311a1b8f55cSAllan Jude	fi
1312a622223fSDevin Teske
1313a1b8f55cSAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1314a622223fSDevin Teske		# Clean up
13157cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
13167cae6aabSDevin Teske			return $FAILURE
1317a107ddbbSRebecca Cran		# tmpfs
1318a107ddbbSRebecca Cran		f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1319cd88b886SDevin Teske	fi
132033112d9eSDevin Teske
13212875e59fSOllivier Robert	#
13222875e59fSOllivier Robert	# Create the gmirror(8) GEOMS for swap
13232875e59fSOllivier Robert	#
132406445fc0SAllan Jude	if [ ${swapsize:-0} -gt 0 -a "$ZFSBOOT_SWAP_MIRROR" ]; then
13252875e59fSOllivier Robert		for disk in $disks; do
13262875e59fSOllivier Robert			swap_devs="$swap_devs $disk$swappart"
13272875e59fSOllivier Robert		done
13282875e59fSOllivier Robert		f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
13292875e59fSOllivier Robert			"$swap_devs" || return $FAILURE
13302875e59fSOllivier Robert	fi
1331cd88b886SDevin Teske
1332cd88b886SDevin Teske	#
1333a622223fSDevin Teske	# Create the ZFS root pool with desired type and disk devices
1334cd88b886SDevin Teske	#
13357cae6aabSDevin Teske	f_dprintf "$funcname: Creating root pool..."
133647206692SDevin Teske	create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1337bb4fd0a0SAllan Jude	if [ "$zroot_vdevtype" == "raid10" ]; then
1338bb4fd0a0SAllan Jude		raid10_vdevs=""
1339bb4fd0a0SAllan Jude		for vdev in $zroot_vdevs; do
1340bb4fd0a0SAllan Jude			f_count nvdev $raid10_vdevs
1341bb4fd0a0SAllan Jude			if [ $(( $nvdev % 3 )) -eq 0 ]; then
1342bb4fd0a0SAllan Jude				raid10_vdevs="$raid10_vdevs mirror"
1343bb4fd0a0SAllan Jude			fi
1344bb4fd0a0SAllan Jude			raid10_vdevs="$raid10_vdevs $vdev"
1345bb4fd0a0SAllan Jude		done
1346bb4fd0a0SAllan Jude		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1347bb4fd0a0SAllan Jude			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1348ee21b0efSDevin Teske			"$zroot_name" "" "$raid10_vdevs" || return $FAILURE
1349bb4fd0a0SAllan Jude	else
1350bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
135147206692SDevin Teske			"-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
13524b4b90d5SDevin Teske			"$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
13534b4b90d5SDevin Teske			return $FAILURE
1354bb4fd0a0SAllan Jude	fi
1355cd88b886SDevin Teske
1356cd88b886SDevin Teske	#
13577cae6aabSDevin Teske	# Create ZFS dataset layout within the new root pool
1358cd88b886SDevin Teske	#
1359bc4a673fSDevin Teske	f_dprintf "$funcname: Creating ZFS datasets..."
1360cd88b886SDevin Teske	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1361cd88b886SDevin Teske		# Skip blank lines and comments
1362cd88b886SDevin Teske		case "$dataset" in "#"*|"") continue; esac
1363cd88b886SDevin Teske		# Remove potential inline comments in options
1364cd88b886SDevin Teske		options="${options%%#*}"
1365cd88b886SDevin Teske		# Replace tabs with spaces
1366cd88b886SDevin Teske		f_replaceall "$options" "	" " " options
1367cd88b886SDevin Teske		# Reduce contiguous runs of space to one single space
1368cd88b886SDevin Teske		oldoptions=
1369cd88b886SDevin Teske		while [ "$oldoptions" != "$options" ]; do
1370cd88b886SDevin Teske			oldoptions="$options"
1371cd88b886SDevin Teske			f_replaceall "$options" "  " " " options
1372cd88b886SDevin Teske		done
1373cd88b886SDevin Teske		# Replace both commas and spaces with ` -o '
1374cd88b886SDevin Teske		f_replaceall "$options" "[ ,]" " -o " options
1375cd88b886SDevin Teske		# Create the dataset with desired options
1376bc4a673fSDevin Teske		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
13777cae6aabSDevin Teske		             "${options:+-o $options}" "$zroot_name$dataset" ||
1378cd88b886SDevin Teske		             return $FAILURE
1379cd88b886SDevin Teske	done
1380cd88b886SDevin Teske
13817e3ebc5bSAllan Jude	#
13827e3ebc5bSAllan Jude	# Set a mountpoint for the root of the pool so newly created datasets
13837e3ebc5bSAllan Jude	# have a mountpoint to inherit
13847e3ebc5bSAllan Jude	#
13857e3ebc5bSAllan Jude	f_dprintf "$funcname: Setting mountpoint for root of the pool..."
13867e3ebc5bSAllan Jude	f_eval_catch $funcname zfs "$ZFS_SET" \
1387ee21b0efSDevin Teske		"mountpoint=/$zroot_name" "$zroot_name" || return $FAILURE
13887e3ebc5bSAllan Jude
1389cd88b886SDevin Teske	# Touch up permissions on the tmp directories
1390bc4a673fSDevin Teske	f_dprintf "$funcname: Modifying directory permissions..."
1391bc4a673fSDevin Teske	local dir
1392bc4a673fSDevin Teske	for dir in /tmp /var/tmp; do
1393b3f7b139SAllan Jude		f_eval_catch $funcname mkdir "$MKDIR_P" \
1394b3f7b139SAllan Jude		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1395bc4a673fSDevin Teske		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
13964ffa3da5SAllan Jude		             $BSDINSTALL_CHROOT$dir || return $FAILURE
1397bc4a673fSDevin Teske	done
1398cd88b886SDevin Teske
1399cd88b886SDevin Teske	# Set bootfs property
14007cae6aabSDevin Teske	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1401bc4a673fSDevin Teske	f_dprintf "$funcname: Setting bootfs property..."
1402bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
14037cae6aabSDevin Teske		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
14047cae6aabSDevin Teske		return $FAILURE
1405cd88b886SDevin Teske
1406b06bc65eSAllan Jude	# MBR boot loader touch-up
1407b06bc65eSAllan Jude	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1408cd88b886SDevin Teske		# Export the pool(s)
1409bc4a673fSDevin Teske		f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
14107cae6aabSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1411bc4a673fSDevin Teske			     return $FAILURE
14127cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
1413bc4a673fSDevin Teske			f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
14147cae6aabSDevin Teske				     "$bootpool_name" || return $FAILURE
1415bc4a673fSDevin Teske		fi
1416cd88b886SDevin Teske
1417bc4a673fSDevin Teske		f_dprintf "$funcname: Updating MBR boot loader on disks..."
1418ec65e4f8SPedro F. Giffuni		# Stick the ZFS boot loader in the "convenient hole" after
1419cd88b886SDevin Teske		# the ZFS internal metadata
1420bc4a673fSDevin Teske		for disk in $disks; do
1421bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1422a622223fSDevin Teske			             /boot/zfsboot /dev/$disk$bootpart \
1423bc4a673fSDevin Teske			             "skip=1 seek=1024" || return $FAILURE
1424cd88b886SDevin Teske		done
1425cd88b886SDevin Teske
1426cd88b886SDevin Teske		# Re-import the ZFS pool(s)
1427bc4a673fSDevin Teske		f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1428bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1429ee21b0efSDevin Teske			     "-o altroot=\"$BSDINSTALL_CHROOT\"" \
1430ee21b0efSDevin Teske			     "$zroot_name" || return $FAILURE
14317cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
14324487af02SAllan Jude			# Import the bootpool, but do not mount it yet
1433ee21b0efSDevin Teske			f_eval_catch $funcname zpool \
1434ee21b0efSDevin Teske				     "$ZPOOL_IMPORT_WITH_OPTIONS" \
14354487af02SAllan Jude				     "-o altroot=\"$BSDINSTALL_CHROOT\" -N" \
14367cae6aabSDevin Teske				     "$bootpool_name" || return $FAILURE
1437bc4a673fSDevin Teske		fi
1438b06bc65eSAllan Jude	fi
1439cd88b886SDevin Teske
1440ca13d5d2SAllan Jude	# Remount bootpool and create symlink(s)
1441ca13d5d2SAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1442ca13d5d2SAllan Jude		f_eval_catch $funcname zfs "$ZFS_MOUNT" "$bootpool_name" ||
1443ca13d5d2SAllan Jude			return $FAILURE
1444ca13d5d2SAllan Jude		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1445ca13d5d2SAllan Jude		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1446ca13d5d2SAllan Jude		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1447ca13d5d2SAllan Jude	fi
1448ca13d5d2SAllan Jude
1449ca13d5d2SAllan Jude	# zpool.cache is required to mount more than one pool at boot time
14506311cc9eSDevin Teske	f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1451a622223fSDevin Teske	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1452bc4a673fSDevin Teske	             return $FAILURE
1453bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1454a622223fSDevin Teske	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
14557cae6aabSDevin Teske	             "$zroot_name" || return $FAILURE
1456cd88b886SDevin Teske
145752fffe93SAllan Jude	if [ "$ZFSBOOT_BOOT_POOL" ]; then
145852fffe93SAllan Jude		f_eval_catch $funcname printf "$PRINTF_CONF" \
1459ee21b0efSDevin Teske			vfs.root.mountfrom \
1460ee21b0efSDevin Teske			"\"zfs:$zroot_name/$zroot_bootfs\"" \
146152fffe93SAllan Jude			$BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
146252fffe93SAllan Jude	fi
1463b06bc65eSAllan Jude	#
1464ee21b0efSDevin Teske	# Set canmount=noauto so that the default Boot Environment (BE) does
1465ee21b0efSDevin Teske	# not get mounted if a different BE is selected from the beastie menu
1466b06bc65eSAllan Jude	#
1467b06bc65eSAllan Jude	f_dprintf "$funcname: Set canmount=noauto for the root of the pool..."
1468b06bc65eSAllan Jude	f_eval_catch $funcname zfs "$ZFS_SET" "canmount=noauto" \
1469b06bc65eSAllan Jude		"$zroot_name/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1470b06bc65eSAllan Jude
147113d8e1ceSDevin Teske	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1472cd88b886SDevin Teske	# NOTE: We later concatenate these into their destination
1473bc4a673fSDevin Teske	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1474bc4a673fSDevin Teske	          "$funcname"
1475bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1476a622223fSDevin Teske	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1477ee482f2cSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" \
1478ee482f2cSDevin Teske	             'kern.geom.label.disk_ident.enable=\"0\"' \
1479ee482f2cSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
14802875e59fSOllivier Robert	f_eval_catch $funcname echo "$ECHO_APPEND" \
14812875e59fSOllivier Robert	             'kern.geom.label.gptid.enable=\"0\"' \
14822875e59fSOllivier Robert	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
14832875e59fSOllivier Robert
148446c380e6SAllan Jude	if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
148546c380e6SAllan Jude		f_eval_catch $funcname echo "$ECHO_APPEND" \
148646c380e6SAllan Jude	             'vfs.zfs.min_auto_ashift=12' \
148798b9d384SSteven Hartland	             $BSDINSTALL_TMPETC/sysctl.conf.zfs || return $FAILURE
148846c380e6SAllan Jude	fi
148946c380e6SAllan Jude
14902875e59fSOllivier Robert	if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
149133112d9eSDevin Teske		f_eval_catch $funcname echo "$ECHO_APPEND" \
149233112d9eSDevin Teske		             'geom_mirror_load=\"YES\"' \
149333112d9eSDevin Teske		             $BSDINSTALL_TMPBOOT/loader.conf.gmirror ||
149433112d9eSDevin Teske		             return $FAILURE
14952875e59fSOllivier Robert	fi
1496cd88b886SDevin Teske
14977cae6aabSDevin Teske	# We're all done unless we should go on to do encryption
14987cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
149913d8e1ceSDevin Teske
1500cd88b886SDevin Teske	#
1501cd88b886SDevin Teske	# Configure geli(8)-based encryption
1502cd88b886SDevin Teske	#
1503bc4a673fSDevin Teske	f_dprintf "$funcname: Configuring disk encryption..."
1504bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1505a622223fSDevin Teske		$BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1506bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1507a622223fSDevin Teske		$BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1508a1b8f55cSAllan Jude
1509a1b8f55cSAllan Jude	# We're all done unless we should go on for boot pool
1510a1b8f55cSAllan Jude	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1511a1b8f55cSAllan Jude
1512bc4a673fSDevin Teske	for disk in $disks; do
1513bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1514a622223fSDevin Teske			geli_%s_keyfile0_load "$disk$targetpart YES" \
1515a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1516cd88b886SDevin Teske			return $FAILURE
1517bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1518bc4a673fSDevin Teske			geli_%s_keyfile0_type \
1519a622223fSDevin Teske			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1520a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1521cd88b886SDevin Teske			return $FAILURE
1522bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1523bc4a673fSDevin Teske			geli_%s_keyfile0_name \
1524a622223fSDevin Teske			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1525a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1526cd88b886SDevin Teske			return $FAILURE
1527cd88b886SDevin Teske	done
1528cd88b886SDevin Teske
1529a1b8f55cSAllan Jude	# Set cachefile for boot pool so it auto-imports at system start
1530a1b8f55cSAllan Jude	f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1531a1b8f55cSAllan Jude	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1532a1b8f55cSAllan Jude	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1533a1b8f55cSAllan Jude	             "$bootpool_name" || return $FAILURE
1534a1b8f55cSAllan Jude
1535a1b8f55cSAllan Jude	# Some additional geli(8) requirements for loader.conf(5)
1536a1b8f55cSAllan Jude	for option in \
1537a1b8f55cSAllan Jude		'zpool_cache_load=\"YES\"' \
1538a1b8f55cSAllan Jude		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1539a1b8f55cSAllan Jude		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1540a1b8f55cSAllan Jude		'geom_eli_passphrase_prompt=\"YES\"' \
1541a1b8f55cSAllan Jude	; do
1542a1b8f55cSAllan Jude		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1543a1b8f55cSAllan Jude		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1544a1b8f55cSAllan Jude		             return $FAILURE
1545a1b8f55cSAllan Jude	done
1546cd88b886SDevin Teske	return $SUCCESS
1547cd88b886SDevin Teske}
1548cd88b886SDevin Teske
1549cd88b886SDevin Teske# dialog_menu_diskinfo
1550cd88b886SDevin Teske#
1551cd88b886SDevin Teske# Prompt the user to select a disk and then provide detailed info on it.
1552cd88b886SDevin Teske#
1553cd88b886SDevin Teskedialog_menu_diskinfo()
1554cd88b886SDevin Teske{
1555a88393ceSDevin Teske	local device disk
1556cd88b886SDevin Teske
1557cd88b886SDevin Teske	#
1558cd88b886SDevin Teske	# Break from loop when user cancels disk selection
1559cd88b886SDevin Teske	#
1560cd88b886SDevin Teske	while :; do
1561a88393ceSDevin Teske		device=$( msg_cancel="$msg_back" f_device_menu \
1562cd88b886SDevin Teske			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1563cd88b886SDevin Teske			$DEVICE_TYPE_DISK 2>&1 ) || break
1564a88393ceSDevin Teske		$device get name disk
1565cd88b886SDevin Teske
1566cd88b886SDevin Teske		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1567cd88b886SDevin Teske		f_show_msg "$msg_detailed_disk_info" \
1568cd88b886SDevin Teske			"$disk" "$( gpart show $disk 2> /dev/null )" \
1569cd88b886SDevin Teske			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1570cd88b886SDevin Teske			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1571cd88b886SDevin Teske	done
1572cd88b886SDevin Teske
1573cd88b886SDevin Teske	return $SUCCESS
1574cd88b886SDevin Teske}
1575cd88b886SDevin Teske
15769935b0e8SEmmanuel Vadotdialog_zpool_name()
15779935b0e8SEmmanuel Vadot{
15789935b0e8SEmmanuel Vadot	local prompt="$* is already taken, please enter a name for the ZFS pool \
15799935b0e8SEmmanuel Vadot	(Or confirm using the same name by just pressing enter)"
15809935b0e8SEmmanuel Vadot	local hline=
15819935b0e8SEmmanuel Vadot	local value="$*"
15829935b0e8SEmmanuel Vadot	local height width
15839935b0e8SEmmanuel Vadot
15849935b0e8SEmmanuel Vadot	f_dialog_inputbox_size height width \
15859935b0e8SEmmanuel Vadot		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
15869935b0e8SEmmanuel Vadot
15879935b0e8SEmmanuel Vadot	$DIALOG \
15889935b0e8SEmmanuel Vadot		--title "$DIALOG_TITLE"         \
15899935b0e8SEmmanuel Vadot		--backtitle "$DIALOG_BACKTITLE" \
15909935b0e8SEmmanuel Vadot		--hline "$hline"                \
15919935b0e8SEmmanuel Vadot		--ok-label "Ok"                 \
15929935b0e8SEmmanuel Vadot		--no-cancel                     \
15939935b0e8SEmmanuel Vadot		--inputbox "$prompt"            \
15949935b0e8SEmmanuel Vadot		$height $width "$value"         \
15959935b0e8SEmmanuel Vadot		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
15969935b0e8SEmmanuel Vadot}
15979935b0e8SEmmanuel Vadot
1598cd88b886SDevin Teske############################################################ MAIN
1599cd88b886SDevin Teske
1600cd88b886SDevin Teske#
1601cd88b886SDevin Teske# Initialize
1602cd88b886SDevin Teske#
1603cd88b886SDevin Teskef_dialog_title "$msg_zfs_configuration"
1604cd88b886SDevin Teskef_dialog_backtitle "$msg_freebsd_installer"
1605cd88b886SDevin Teske
1606cd88b886SDevin Teske# User may have specifically requested ZFS-related operations be interactive
1607cd88b886SDevin Teske! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1608cd88b886SDevin Teske
1609cd88b886SDevin Teske#
1610bc4a673fSDevin Teske# Debugging
1611bc4a673fSDevin Teske#
1612bc4a673fSDevin Teskef_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1613bc4a673fSDevin Teskef_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1614a622223fSDevin Teskef_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1615bc4a673fSDevin Teske
16169935b0e8SEmmanuel Vadotpools=$(zpool import | awk '$1 == "pool:" { print $2}')
16179935b0e8SEmmanuel Vadotfor pool in ${pools}; do
16189935b0e8SEmmanuel Vadot	f_dprintf "Checking ${pool} against ${ZFSBOOT_POOL_NAME}"
16199935b0e8SEmmanuel Vadot	if [ "${pool}" = "${ZFSBOOT_POOL_NAME}" ]; then
16209935b0e8SEmmanuel Vadot		f_dprintf "Pool ${pool} already taken"
16219935b0e8SEmmanuel Vadot		ZFSBOOT_POOL_NAME=$(dialog_zpool_name "${ZFSBOOT_POOL_NAME}")
1622*e50daba0SJens Schweikhardt		break
16239935b0e8SEmmanuel Vadot	fi
16249935b0e8SEmmanuel Vadotdone
16259935b0e8SEmmanuel Vadot
1626bc4a673fSDevin Teske#
1627e39a9678SEd Maste# Determine default boot type
162812307018SAllan Jude#
1629e39a9678SEd Mastecase $(uname -m) in
16307b08a307SMitchell Hornearm64|riscv)
16317b08a307SMitchell Horne	# We support only UEFI boot for arm64 and riscv.
1632e39a9678SEd Maste	: ${ZFSBOOT_BOOT_TYPE:=UEFI}
1633e39a9678SEd Maste	: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1634e39a9678SEd Maste	;;
1635e39a9678SEd Maste*)
1636e39a9678SEd Maste	# If the system was booted with UEFI, set the default boot type to UEFI
163712307018SAllan Jude	bootmethod=$( sysctl -n machdep.bootmethod )
163812307018SAllan Jude	f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
16390edc8cc8SAllan Jude	if [ "$bootmethod" = "UEFI" ]; then
16404d41f472SAllan Jude		: ${ZFSBOOT_BOOT_TYPE:=BIOS+UEFI}
16410edc8cc8SAllan Jude		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
16420edc8cc8SAllan Jude	else
16430edc8cc8SAllan Jude		: ${ZFSBOOT_BOOT_TYPE:=BIOS}
16440edc8cc8SAllan Jude		: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
164512307018SAllan Jude	fi
1646e39a9678SEd Maste	;;
1647e39a9678SEd Masteesac
164812307018SAllan Jude
164912307018SAllan Jude#
1650cd88b886SDevin Teske# Loop over the main menu until we've accomplished what we came here to do
1651cd88b886SDevin Teske#
1652cd88b886SDevin Teskewhile :; do
1653cd88b886SDevin Teske	if ! f_interactive; then
1654cd88b886SDevin Teske		retval=$DIALOG_OK
1655bc4a673fSDevin Teske		mtag=">>> $msg_install"
1656cd88b886SDevin Teske	else
1657cd88b886SDevin Teske		dialog_menu_main
1658cd88b886SDevin Teske		retval=$?
1659cd88b886SDevin Teske		f_dialog_menutag_fetch mtag
1660cd88b886SDevin Teske	fi
1661cd88b886SDevin Teske
1662bc4a673fSDevin Teske	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1663cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] || f_die
1664cd88b886SDevin Teske
1665cd88b886SDevin Teske	case "$mtag" in
1666bc4a673fSDevin Teske	">>> $msg_install")
1667cd88b886SDevin Teske		#
1668cd88b886SDevin Teske		# First, validate the user's selections
1669cd88b886SDevin Teske		#
1670cd88b886SDevin Teske
1671cd88b886SDevin Teske		# Make sure they gave us a name for the pool
1672cd88b886SDevin Teske		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1673bc4a673fSDevin Teske			f_dprintf "Pool name cannot be empty."
1674bc4a673fSDevin Teske			f_show_err "$msg_pool_name_cannot_be_empty"
1675cd88b886SDevin Teske			continue
1676cd88b886SDevin Teske		fi
1677bc4a673fSDevin Teske
1678bc4a673fSDevin Teske		# Validate vdev type against number of disks selected/scripted
1679bc4a673fSDevin Teske		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1680bc4a673fSDevin Teske		# NB: dialog_menu_layout supports running non-interactively
1681bc4a673fSDevin Teske		dialog_menu_layout || continue
1682bc4a673fSDevin Teske
16836203e46bSAllan Jude		# Make sure each disk will have room for ZFS
1684cd88b886SDevin Teske		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
16856203e46bSAllan Jude		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize &&
16866203e46bSAllan Jude		   f_expand_number "1g" zpoolmin
1687cd88b886SDevin Teske		then
16887ef92163SJohn Ko			minsize=$swapsize teeny_disks=
16897ef92163SJohn Ko			if [ "$ZFSBOOT_POOL_SIZE" ]; then
16907ef92163SJohn Ko				f_expand_number "$ZFSBOOT_POOL_SIZE" poolsize
16917ef92163SJohn Ko				minsize=$(( $minsize + $poolsize ))
16927ef92163SJohn Ko			fi
16937cae6aabSDevin Teske			[ "$ZFSBOOT_BOOT_POOL" ] &&
16947cae6aabSDevin Teske				minsize=$(( $minsize + $bootsize ))
1695bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
1696a88393ceSDevin Teske				debug= f_device_find -1 \
1697a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK device
1698a88393ceSDevin Teske				$device get capacity disksize || continue
1699a88393ceSDevin Teske				[ ${disksize:-0} -ge 0 ] || disksize=0
1700cd88b886SDevin Teske				[ $disksize -lt $minsize ] &&
1701cd88b886SDevin Teske					teeny_disks="$teeny_disks $disk"
1702cd88b886SDevin Teske			done
1703cd88b886SDevin Teske			if [ "$teeny_disks" ]; then
17047cae6aabSDevin Teske				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1705bc4a673fSDevin Teske				          "$ZFSBOOT_SWAP_SIZE" \
17067cae6aabSDevin Teske				          "$ZFSBOOT_BOOT_POOL_SIZE" \
17077cae6aabSDevin Teske				          "minsize=[$minsize]"
1708bc4a673fSDevin Teske				f_dprintf "These disks are too small: %s" \
1709bc4a673fSDevin Teske				          "$teeny_disks"
1710bc4a673fSDevin Teske				f_show_err "$msg_these_disks_are_too_small" \
1711cd88b886SDevin Teske				           "$ZFSBOOT_SWAP_SIZE" \
17127cae6aabSDevin Teske				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1713cd88b886SDevin Teske				           "$teeny_disks"
1714cd88b886SDevin Teske				continue
1715cd88b886SDevin Teske			fi
1716cd88b886SDevin Teske		fi
1717cd88b886SDevin Teske
1718cd88b886SDevin Teske		#
1719cd88b886SDevin Teske		# Last Chance!
1720cd88b886SDevin Teske		#
1721bc4a673fSDevin Teske		if f_interactive; then
1722bc4a673fSDevin Teske			dialog_last_chance $ZFSBOOT_DISKS || continue
1723cd88b886SDevin Teske		fi
1724cd88b886SDevin Teske
1725cd88b886SDevin Teske		#
1726cd88b886SDevin Teske		# Let's do this
1727cd88b886SDevin Teske		#
1728cd88b886SDevin Teske
1729cd88b886SDevin Teske		vdev_type="$ZFSBOOT_VDEV_TYPE"
1730cd88b886SDevin Teske
1731cd88b886SDevin Teske		# Blank the vdev type for the default layout
1732cd88b886SDevin Teske		[ "$vdev_type" = "stripe" ] && vdev_type=
1733cd88b886SDevin Teske
1734bc4a673fSDevin Teske		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1735bc4a673fSDevin Teske		                "$vdev_type" $ZFSBOOT_DISKS || continue
1736cd88b886SDevin Teske
1737a89559c2SYuri Pankov		# To be reused by bootconfig
1738a107ddbbSRebecca Cran		echo "$ZFSBOOT_DISKS" > ${TMPDIR:-"/tmp"}/bsdinstall-zfsboot
1739a89559c2SYuri Pankov
1740cd88b886SDevin Teske		break # to success
1741cd88b886SDevin Teske		;;
1742bc4a673fSDevin Teske	?" $msg_pool_type_disks")
1743bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=1
1744bc4a673fSDevin Teske		dialog_menu_layout
1745bc4a673fSDevin Teske		# User has poked settings, disable later confirmation
1746bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=
1747bc4a673fSDevin Teske		;;
1748cd88b886SDevin Teske	"- $msg_rescan_devices") f_device_rescan ;;
1749cd88b886SDevin Teske	"- $msg_disk_info") dialog_menu_diskinfo ;;
1750cd88b886SDevin Teske	?" $msg_pool_name")
1751cd88b886SDevin Teske		# Prompt the user to input/change the name for the new pool
1752cd88b886SDevin Teske		f_dialog_input input \
1753cd88b886SDevin Teske			"$msg_please_enter_a_name_for_your_zpool" \
1754cd88b886SDevin Teske			"$ZFSBOOT_POOL_NAME" &&
1755cd88b886SDevin Teske			ZFSBOOT_POOL_NAME="$input"
1756cd88b886SDevin Teske		;;
1757cd88b886SDevin Teske	?" $msg_force_4k_sectors")
1758cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1759b4843bd6SSteven Hartland		if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1760b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=
1761cd88b886SDevin Teske		else
1762b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=1
1763cd88b886SDevin Teske		fi
1764cd88b886SDevin Teske		;;
1765bc4a673fSDevin Teske	?" $msg_encrypt_disks")
1766cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1767cd88b886SDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1768cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=
1769cd88b886SDevin Teske		else
1770b4843bd6SSteven Hartland			ZFSBOOT_FORCE_4K_SECTORS=1
1771cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=1
1772cd88b886SDevin Teske		fi
1773cd88b886SDevin Teske		;;
1774cd88b886SDevin Teske	?" $msg_partition_scheme")
17750edc8cc8SAllan Jude		# Toggle between GPT (BIOS), GPT (UEFI) and MBR
1776ee21b0efSDevin Teske		if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1777ee21b0efSDevin Teske		     "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]
1778ee21b0efSDevin Teske		then
17790edc8cc8SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
17800edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="UEFI"
1781ee21b0efSDevin Teske		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1782ee21b0efSDevin Teske		       "$ZFSBOOT_BOOT_TYPE" = "UEFI" ]
1783ee21b0efSDevin Teske		then
17844d41f472SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
17854d41f472SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS+UEFI"
17860edc8cc8SAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" ]; then
17870edc8cc8SAllan Jude			ZFSBOOT_PARTITION_SCHEME="MBR"
17880edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
17890edc8cc8SAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
17907059fa6fSAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT + Active"
17910edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
17927059fa6fSAllan Jude		elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
17937059fa6fSAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
17940edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
1795cd88b886SDevin Teske		else
17960edc8cc8SAllan Jude			ZFSBOOT_PARTITION_SCHEME="GPT"
17970edc8cc8SAllan Jude			ZFSBOOT_BOOT_TYPE="BIOS"
1798cd88b886SDevin Teske		fi
1799cd88b886SDevin Teske		;;
1800cd88b886SDevin Teske	?" $msg_swap_size")
1801cd88b886SDevin Teske		# Prompt the user to input/change the swap size for each disk
1802169ff110SAllan Jude		while :; do
1803cd88b886SDevin Teske		    f_dialog_input input \
1804cd88b886SDevin Teske			    "$msg_please_enter_amount_of_swap_space" \
1805cd88b886SDevin Teske			    "$ZFSBOOT_SWAP_SIZE" &&
180630c8ebe9SDevin Teske			    ZFSBOOT_SWAP_SIZE="${input:-0}"
1807169ff110SAllan Jude		    if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1808169ff110SAllan Jude		    then
1809169ff110SAllan Jude			if [ $swapsize -ne 0 -a $swapsize -lt 104857600 ]; then
1810169ff110SAllan Jude			    f_show_err "$msg_swap_toosmall" \
1811169ff110SAllan Jude				       "$ZFSBOOT_SWAP_SIZE"
1812*e50daba0SJens Schweikhardt			    continue
1813169ff110SAllan Jude			else
1814*e50daba0SJens Schweikhardt			    break
1815169ff110SAllan Jude			fi
1816169ff110SAllan Jude		    else
1817169ff110SAllan Jude			f_show_err "$msg_swap_invalid" \
1818169ff110SAllan Jude				   "$ZFSBOOT_SWAP_SIZE"
1819*e50daba0SJens Schweikhardt			continue
1820169ff110SAllan Jude		    fi
1821169ff110SAllan Jude		done
1822cd88b886SDevin Teske		;;
18232875e59fSOllivier Robert	?" $msg_swap_mirror")
18242875e59fSOllivier Robert		# Toggle the variable referenced both by the menu and later
18252875e59fSOllivier Robert		if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
18262875e59fSOllivier Robert			ZFSBOOT_SWAP_MIRROR=
18272875e59fSOllivier Robert		else
18282875e59fSOllivier Robert			ZFSBOOT_SWAP_MIRROR=1
18292875e59fSOllivier Robert		fi
18302875e59fSOllivier Robert		;;
18312875e59fSOllivier Robert	?" $msg_swap_encrypt")
18322875e59fSOllivier Robert		# Toggle the variable referenced both by the menu and later
18332875e59fSOllivier Robert		if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
18342875e59fSOllivier Robert			ZFSBOOT_SWAP_ENCRYPTION=
18352875e59fSOllivier Robert		else
18362875e59fSOllivier Robert			ZFSBOOT_SWAP_ENCRYPTION=1
18372875e59fSOllivier Robert		fi
18382875e59fSOllivier Robert		;;
1839cd88b886SDevin Teske	esac
1840cd88b886SDevin Teskedone
1841cd88b886SDevin Teske
184265f36140SDevin Teskeexit $SUCCESS
1843cd88b886SDevin Teske
1844cd88b886SDevin Teske################################################################################
1845cd88b886SDevin Teske# END
1846cd88b886SDevin Teske################################################################################
1847