xref: /freebsd/usr.sbin/bsdinstall/scripts/zfsboot (revision a88393ce0bddc4e074ed3f65380e456418979ad4)
1cd88b886SDevin Teske#!/bin/sh
2cd88b886SDevin Teske#-
3cd88b886SDevin Teske# Copyright (c) 2013 Allan Jude
4cd88b886SDevin Teske# Copyright (c) 2013 Devin Teske
5cd88b886SDevin Teske# All rights reserved.
6cd88b886SDevin Teske#
7cd88b886SDevin Teske# Redistribution and use in source and binary forms, with or without
8cd88b886SDevin Teske# modification, are permitted provided that the following conditions
9cd88b886SDevin Teske# are met:
10cd88b886SDevin Teske# 1. Redistributions of source code must retain the above copyright
11cd88b886SDevin Teske#    notice, this list of conditions and the following disclaimer.
12cd88b886SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
13cd88b886SDevin Teske#    notice, this list of conditions and the following disclaimer in the
14cd88b886SDevin Teske#    documentation and/or other materials provided with the distribution.
15cd88b886SDevin Teske#
16cd88b886SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17cd88b886SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18cd88b886SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19cd88b886SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20cd88b886SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21cd88b886SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22cd88b886SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23cd88b886SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24cd88b886SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25cd88b886SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26cd88b886SDevin Teske# SUCH DAMAGE.
27cd88b886SDevin Teske#
28cd88b886SDevin Teske# $FreeBSD$
29cd88b886SDevin Teske#
30cd88b886SDevin Teske############################################################ INCLUDES
31cd88b886SDevin Teske
32cd88b886SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
33cd88b886SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
34cd88b886SDevin Teskef_dprintf "%s: loading includes..." "$0"
35cd88b886SDevin Teskef_include $BSDCFG_SHARE/device.subr
36cd88b886SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
37cd88b886SDevin Teskef_include $BSDCFG_SHARE/password/password.subr
38cd88b886SDevin Teskef_include $BSDCFG_SHARE/variable.subr
39cd88b886SDevin Teske
40cd88b886SDevin Teske############################################################ CONFIGURATION
41cd88b886SDevin Teske
42cd88b886SDevin Teske#
43cd88b886SDevin Teske# Default name of the boot-pool
44cd88b886SDevin Teske#
45cd88b886SDevin Teske: ${ZFSBOOT_POOL_NAME:=zroot}
46cd88b886SDevin Teske
47cd88b886SDevin Teske#
48cd88b886SDevin Teske# Default name for the boot environment parent dataset
49cd88b886SDevin Teske#
5067635c19SDevin Teske: ${ZFSBOOT_BEROOT_NAME:=ROOT}
51cd88b886SDevin Teske
52cd88b886SDevin Teske#
53cd88b886SDevin Teske# Default name for the primany boot environment
54cd88b886SDevin Teske#
55cd88b886SDevin Teske: ${ZFSBOOT_BOOTFS_NAME:=default}
56cd88b886SDevin Teske
57cd88b886SDevin Teske#
58cd88b886SDevin Teske# Default Virtual Device (vdev) type to create
59cd88b886SDevin Teske#
60cd88b886SDevin Teske: ${ZFSBOOT_VDEV_TYPE:=stripe}
61cd88b886SDevin Teske
62cd88b886SDevin Teske#
63cd88b886SDevin Teske# Should we use gnop(8) to configure a transparent mapping to 4K sectors?
64cd88b886SDevin Teske#
65cd88b886SDevin Teske: ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=1}
66cd88b886SDevin Teske
67cd88b886SDevin Teske#
68cd88b886SDevin Teske# Should we use geli(8) to encrypt the drives?
697cae6aabSDevin Teske# NB: Automatically enables ZFSBOOT_BOOT_POOL
70cd88b886SDevin Teske#
71bc4a673fSDevin Teske: ${ZFSBOOT_GELI_ENCRYPTION=}
72cd88b886SDevin Teske
73cd88b886SDevin Teske#
74cd88b886SDevin Teske# Default path to the geli(8) keyfile used in drive encryption
75cd88b886SDevin Teske#
76cd88b886SDevin Teske: ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
77cd88b886SDevin Teske
78cd88b886SDevin Teske#
797cae6aabSDevin Teske# Create a separate boot pool?
807cae6aabSDevin Teske# NB: Automatically set when using geli(8) or MBR
817cae6aabSDevin Teske#
827cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL=}
837cae6aabSDevin Teske
847cae6aabSDevin Teske#
857cae6aabSDevin Teske# Default name for boot pool when enabled (e.g., geli(8) or MBR)
867cae6aabSDevin Teske#
877cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
887cae6aabSDevin Teske
897cae6aabSDevin Teske#
907cae6aabSDevin Teske# Default size for boot pool when enabled (e.g., geli(8) or MBR)
917cae6aabSDevin Teske#
927cae6aabSDevin Teske: ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
937cae6aabSDevin Teske
947cae6aabSDevin Teske#
95cd88b886SDevin Teske# Default disks to use (always empty unless being scripted)
96cd88b886SDevin Teske#
97cd88b886SDevin Teske: ${ZFSBOOT_DISKS:=}
98cd88b886SDevin Teske
99cd88b886SDevin Teske#
100cd88b886SDevin Teske# Default partitioning scheme to use on disks
101cd88b886SDevin Teske#
102cd88b886SDevin Teske: ${ZFSBOOT_PARTITION_SCHEME:=GPT}
103cd88b886SDevin Teske
104cd88b886SDevin Teske#
105cd88b886SDevin Teske# How much swap to put on each block device in the boot zpool
106cd88b886SDevin Teske# NOTE: Value passed to gpart(8); which supports SI unit suffixes.
107cd88b886SDevin Teske#
108cd88b886SDevin Teske: ${ZFSBOOT_SWAP_SIZE:=2g}
109cd88b886SDevin Teske
110cd88b886SDevin Teske#
111bc4a673fSDevin Teske# Default ZFS datasets for root zpool
112cd88b886SDevin Teske#
113cd88b886SDevin Teske# NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
114cd88b886SDevin Teske# NOTE: Anything after pound/hash character [#] is ignored as a comment.
115cd88b886SDevin Teske#
116cd88b886SDevin Teskef_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
117cd88b886SDevin Teske	# DATASET	OPTIONS (comma or space separated; or both)
118cd88b886SDevin Teske
119cd88b886SDevin Teske	# Boot Environment [BE] root and default boot dataset
120cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME				mountpoint=none
121cd88b886SDevin Teske	/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME	mountpoint=/
122cd88b886SDevin Teske
123cd88b886SDevin Teske	# Compress /tmp, allow exec but not setuid
124cd88b886SDevin Teske	/tmp		mountpoint=/tmp,compression=lz4,exec=on,setuid=off
125cd88b886SDevin Teske
126cd88b886SDevin Teske	# Don't mount /usr so that 'base' files go to the BEROOT
127cd88b886SDevin Teske	/usr		mountpoint=/usr,canmount=off
128cd88b886SDevin Teske
129cd88b886SDevin Teske	# Home directories separated so they are common to all BEs
130bc4a673fSDevin Teske	/usr/home	# NB: /home is a symlink to /usr/home
131cd88b886SDevin Teske
132cd88b886SDevin Teske	# Ports tree
133cd88b886SDevin Teske	/usr/ports		compression=lz4,setuid=off
134cd88b886SDevin Teske
135cd88b886SDevin Teske	# Source tree (compressed)
136cd88b886SDevin Teske	/usr/src	compression=lz4,exec=off,setuid=off
137cd88b886SDevin Teske
138cd88b886SDevin Teske	# Create /var and friends
139cd88b886SDevin Teske	/var		mountpoint=/var
140cd88b886SDevin Teske	/var/crash	compression=lz4,exec=off,setuid=off
141cd88b886SDevin Teske	/var/log	compression=lz4,exec=off,setuid=off
142a6c8532cSDevin Teske	/var/mail	compression=lz4,atime=on
143cd88b886SDevin Teske	/var/tmp	compression=lz4,exec=on,setuid=off
144cd88b886SDevin Teske" # END-QUOTE
145cd88b886SDevin Teske
146bc4a673fSDevin Teske#
147bc4a673fSDevin Teske# If interactive and the user has not explicitly chosen a vdev type or disks,
148bc4a673fSDevin Teske# make the user confirm scripted/default choices when proceeding to install.
149bc4a673fSDevin Teske#
150bc4a673fSDevin Teske: ${ZFSBOOT_CONFIRM_LAYOUT:=1}
151bc4a673fSDevin Teske
152cd88b886SDevin Teske############################################################ GLOBALS
153cd88b886SDevin Teske
154cd88b886SDevin Teske#
155bc4a673fSDevin Teske# Format of a line in printf(1) syntax to add to fstab(5)
156bc4a673fSDevin Teske#
157bc4a673fSDevin TeskeFSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
158bc4a673fSDevin Teske
159bc4a673fSDevin Teske#
160bc4a673fSDevin Teske# Command strings for various tasks
161bc4a673fSDevin Teske#
162bc4a673fSDevin TeskeCHMOD_MODE='chmod %s "%s"'
163bc4a673fSDevin TeskeDD_WITH_OPTIONS='dd if="%s" of="%s" %s'
164bc4a673fSDevin TeskeECHO_APPEND='echo "%s" >> "%s"'
165bc4a673fSDevin TeskeGELI_ATTACH='geli attach -j - -k "%s" "%s"'
166a622223fSDevin TeskeGELI_DETACH_F='geli detach -f "%s"'
167bc4a673fSDevin TeskeGELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
168bc4a673fSDevin TeskeGNOP_CREATE='gnop create -S 4096 "%s"'
169a622223fSDevin TeskeGNOP_DESTROY='gnop destroy "%s"'
170bc4a673fSDevin TeskeGPART_ADD='gpart add -t %s "%s"'
171bc4a673fSDevin TeskeGPART_ADD_INDEX='gpart add -i %s -t %s "%s"'
172bc4a673fSDevin TeskeGPART_ADD_INDEX_WITH_SIZE='gpart add -i %s -t %s -s %s "%s"'
173bc4a673fSDevin TeskeGPART_ADD_LABEL='gpart add -l %s -t %s "%s"'
174bc4a673fSDevin TeskeGPART_ADD_LABEL_WITH_SIZE='gpart add -l %s -t %s -s %s "%s"'
175bc4a673fSDevin TeskeGPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
176bc4a673fSDevin TeskeGPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
177bc4a673fSDevin TeskeGPART_CREATE='gpart create -s %s "%s"'
178a622223fSDevin TeskeGPART_DESTROY_F='gpart destroy -F "%s"'
179bc4a673fSDevin TeskeGPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
180a622223fSDevin TeskeGRAID_DELETE='graid delete "%s"'
181bc4a673fSDevin TeskeLN_SF='ln -sf "%s" "%s"'
182bc4a673fSDevin TeskeMKDIR_P='mkdir -p "%s"'
183bc4a673fSDevin TeskeMOUNT_TYPE='mount -t %s "%s" "%s"'
184bc4a673fSDevin TeskePRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
185bc4a673fSDevin TeskePRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
186bc4a673fSDevin TeskeSHELL_TRUNCATE=':> "%s"'
187a622223fSDevin TeskeUMOUNT='umount "%s"'
188bc4a673fSDevin TeskeZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
189bc4a673fSDevin TeskeZFS_SET='zfs set "%s" "%s"'
190bc4a673fSDevin TeskeZFS_UNMOUNT='zfs unmount "%s"'
191bc4a673fSDevin TeskeZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
192*a88393ceSDevin TeskeZPOOL_DESTROY='zpool destroy "%s"'
193bc4a673fSDevin TeskeZPOOL_EXPORT='zpool export "%s"'
194bc4a673fSDevin TeskeZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
195a622223fSDevin TeskeZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
196bc4a673fSDevin TeskeZPOOL_SET='zpool set %s "%s"'
197bc4a673fSDevin Teske
198bc4a673fSDevin Teske#
199cd88b886SDevin Teske# Strings that should be moved to an i18n file and loaded with f_include_lang()
200cd88b886SDevin Teske#
201cd88b886SDevin Teskehline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
202cd88b886SDevin Teskehline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
203cd88b886SDevin Teskehline_arrows_tab_enter="Press arrows, TAB or ENTER"
204bc4a673fSDevin Teskemsg_an_unknown_error_occurred="An unknown error occurred"
205cd88b886SDevin Teskemsg_back="Back"
206cd88b886SDevin Teskemsg_cancel="Cancel"
207bc4a673fSDevin Teskemsg_change_selection="Change Selection"
208cd88b886SDevin Teskemsg_configure_options="Configure Options:"
209cd88b886SDevin 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"
210cd88b886SDevin Teskemsg_disk_info="Disk Info"
211cd88b886SDevin Teskemsg_disk_info_help="Get detailed information on disk device(s)"
212bc4a673fSDevin Teskemsg_encrypt_disks="Encrypt Disks?"
213bc4a673fSDevin Teskemsg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
214bc4a673fSDevin Teskemsg_error="Error"
215cd88b886SDevin Teskemsg_force_4k_sectors="Force 4K Sectors?"
216cd88b886SDevin Teskemsg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
217cd88b886SDevin Teskemsg_freebsd_installer="FreeBSD Installer"
218cd88b886SDevin 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"
2197a434c5cSDevin Teskemsg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
220bc4a673fSDevin Teskemsg_install="Install"
221bc4a673fSDevin Teskemsg_install_desc="Proceed with Installation"
222bc4a673fSDevin Teskemsg_install_help="Create ZFS boot pool with displayed options"
2237cae6aabSDevin Teskemsg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
224bc4a673fSDevin Teskemsg_invalid_disk_argument="Invalid disk argument \`%s'"
225bc4a673fSDevin Teskemsg_invalid_index_argument="Invalid index argument \`%s'"
226bc4a673fSDevin Teskemsg_invalid_swap_size="Invalid swap size \`%s'"
227cd88b886SDevin Teskemsg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
228bc4a673fSDevin 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"
229bc4a673fSDevin 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'
230cd88b886SDevin Teskemsg_mirror_desc="Mirror - n-Way Mirroring"
231cd88b886SDevin Teskemsg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
232bc4a673fSDevin Teskemsg_missing_disk_arguments="missing disk arguments"
233bc4a673fSDevin Teskemsg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
234cd88b886SDevin Teskemsg_no="NO"
235cd88b886SDevin Teskemsg_no_disks_present_to_configure="No disk(s) present to configure"
236cd88b886SDevin Teskemsg_no_disks_selected="No disks selected."
237bc4a673fSDevin Teskemsg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
238bc4a673fSDevin Teskemsg_null_disk_argument="NULL disk argument"
239bc4a673fSDevin Teskemsg_null_index_argument="NULL index argument"
240bc4a673fSDevin Teskemsg_null_poolname="NULL poolname"
241cd88b886SDevin Teskemsg_ok="OK"
242cd88b886SDevin Teskemsg_partition_scheme="Partition Scheme"
243cd88b886SDevin Teskemsg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
244cd88b886SDevin Teskemsg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
245cd88b886SDevin Teskemsg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
246cd88b886SDevin Teskemsg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
247cd88b886SDevin Teskemsg_pool_name="Pool Name"
248cd88b886SDevin Teskemsg_pool_name_cannot_be_empty="Pool name cannot be empty."
249cd88b886SDevin Teskemsg_pool_name_help="Customize the name of the zpool to be created (Required)"
250bc4a673fSDevin Teskemsg_pool_type_disks="Pool Type/Disks:"
251bc4a673fSDevin Teskemsg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
252cd88b886SDevin Teskemsg_processing_selection="Processing selection..."
253cd88b886SDevin Teskemsg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
254cd88b886SDevin Teskemsg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
255cd88b886SDevin Teskemsg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
256cd88b886SDevin Teskemsg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
257cd88b886SDevin Teskemsg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
258cd88b886SDevin Teskemsg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
259cd88b886SDevin Teskemsg_rescan_devices="Rescan Devices"
260cd88b886SDevin Teskemsg_rescan_devices_help="Scan for device changes"
261cd88b886SDevin Teskemsg_select="Select"
262cd88b886SDevin Teskemsg_select_a_disk_device="Select a disk device"
263cd88b886SDevin Teskemsg_select_virtual_device_type="Select Virtual Device type:"
264cd88b886SDevin Teskemsg_stripe_desc="Stripe - No Redundancy"
265cd88b886SDevin Teskemsg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
266cd88b886SDevin Teskemsg_swap_size="Swap Size"
267cd88b886SDevin Teskemsg_swap_size_help="Customize how much swap space is allocated to each selected disk"
268bc4a673fSDevin Teskemsg_these_disks_are_too_small="These disks are too small given the amount of requested\nswap (%s) and/or geli(8) (%s) partitions, which would\ntake 50%% or more of each of the following selected disk\ndevices (not recommended):\n\n  %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of devices."
269bc4a673fSDevin Teskemsg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
270bc4a673fSDevin Teskemsg_unsupported_partition_scheme="%s is an unsupported partition scheme"
271bc4a673fSDevin Teskemsg_user_cancelled="User Cancelled."
272cd88b886SDevin Teskemsg_yes="YES"
273cd88b886SDevin Teskemsg_zfs_configuration="ZFS Configuration"
274cd88b886SDevin Teske
275cd88b886SDevin Teske############################################################ FUNCTIONS
276cd88b886SDevin Teske
277cd88b886SDevin Teske# dialog_menu_main
278cd88b886SDevin Teske#
279cd88b886SDevin Teske# Display the dialog(1)-based application main menu.
280cd88b886SDevin Teske#
281cd88b886SDevin Teskedialog_menu_main()
282cd88b886SDevin Teske{
283cd88b886SDevin Teske	local title="$DIALOG_TITLE"
284cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
285cd88b886SDevin Teske	local prompt="$msg_configure_options"
286cd88b886SDevin Teske	local force4k="$msg_no"
287cd88b886SDevin Teske	local usegeli="$msg_no"
288cd88b886SDevin Teske	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
289cd88b886SDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
290*a88393ceSDevin Teske	local disks n
291*a88393ceSDevin Teske	f_count n $ZFSBOOT_DISKS
292bc4a673fSDevin Teske	{ [ $n -eq 1 ] && disks=disk; } || disks=disks # grammar
293cd88b886SDevin Teske	local menu_list="
294bc4a673fSDevin Teske		'>>> $msg_install'        '$msg_install_desc'
295bc4a673fSDevin Teske		                          '$msg_install_help'
296bc4a673fSDevin Teske		'T $msg_pool_type_disks'  '$ZFSBOOT_VDEV_TYPE: $n $disks'
297bc4a673fSDevin Teske                                          '$msg_pool_type_disks_help'
298cd88b886SDevin Teske		'- $msg_rescan_devices'   '*'
299cd88b886SDevin Teske		                          '$msg_rescan_devices_help'
300cd88b886SDevin Teske		'- $msg_disk_info'        '*'
301cd88b886SDevin Teske		                          '$msg_disk_info_help'
302bc4a673fSDevin Teske		'N $msg_pool_name'        '$ZFSBOOT_POOL_NAME'
303cd88b886SDevin Teske		                          '$msg_pool_name_help'
304cd88b886SDevin Teske		'4 $msg_force_4k_sectors' '$force4k'
305cd88b886SDevin Teske		                          '$msg_force_4k_sectors_help'
306bc4a673fSDevin Teske		'E $msg_encrypt_disks'    '$usegeli'
307bc4a673fSDevin Teske		                          '$msg_encrypt_disks_help'
308bc4a673fSDevin Teske		'P $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
309cd88b886SDevin Teske		                          '$msg_partition_scheme_help'
310bc4a673fSDevin Teske		'S $msg_swap_size'        '$ZFSBOOT_SWAP_SIZE'
311cd88b886SDevin Teske		                          '$msg_swap_size_help'
312cd88b886SDevin Teske	" # END-QUOTE
313cd88b886SDevin Teske	local defaultitem= # Calculated below
314cd88b886SDevin Teske	local hline="$hline_alnum_arrows_punc_tab_enter"
315cd88b886SDevin Teske
316cd88b886SDevin Teske	local height width rows
317cd88b886SDevin Teske	eval f_dialog_menu_with_help_size height width rows \
318cd88b886SDevin Teske		\"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
319cd88b886SDevin Teske
320cd88b886SDevin Teske	# Obtain default-item from previously stored selection
321cd88b886SDevin Teske	f_dialog_default_fetch defaultitem
322cd88b886SDevin Teske
323cd88b886SDevin Teske	local menu_choice
324cd88b886SDevin Teske	menu_choice=$( eval $DIALOG \
325cd88b886SDevin Teske		--title \"\$title\"              \
326cd88b886SDevin Teske		--backtitle \"\$btitle\"         \
327cd88b886SDevin Teske		--hline \"\$hline\"              \
328cd88b886SDevin Teske		--item-help                      \
329cd88b886SDevin Teske		--ok-label \"\$msg_select\"      \
330cd88b886SDevin Teske		--cancel-label \"\$msg_cancel\"  \
331cd88b886SDevin Teske		--default-item \"\$defaultitem\" \
332cd88b886SDevin Teske		--menu \"\$prompt\"              \
333cd88b886SDevin Teske		$height $width $rows             \
334cd88b886SDevin Teske		$menu_list                       \
335cd88b886SDevin Teske		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
336cd88b886SDevin Teske	)
337cd88b886SDevin Teske	local retval=$?
338cd88b886SDevin Teske	f_dialog_data_sanitize menu_choice
339cd88b886SDevin Teske	f_dialog_menutag_store "$menu_choice"
340cd88b886SDevin Teske
341cd88b886SDevin Teske	# Only update default-item on success
342cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
343cd88b886SDevin Teske
344cd88b886SDevin Teske	return $retval
345cd88b886SDevin Teske}
346cd88b886SDevin Teske
347bc4a673fSDevin Teske# dialog_last_chance $disks ...
348cd88b886SDevin Teske#
349bc4a673fSDevin Teske# Display a list of the disks that the user is about to destroy. The default
350bc4a673fSDevin Teske# action is to return error status unless the user explicitly (non-default)
351bc4a673fSDevin Teske# selects "Yes" from the noyes dialog.
352cd88b886SDevin Teske#
353bc4a673fSDevin Teskedialog_last_chance()
354cd88b886SDevin Teske{
355cd88b886SDevin Teske	local title="$DIALOG_TITLE"
356cd88b886SDevin Teske	local btitle="$DIALOG_BACKTITLE"
357bc4a673fSDevin Teske	local prompt # Calculated below
358bc4a673fSDevin Teske	local hline="$hline_arrows_tab_enter"
359cd88b886SDevin Teske
360bc4a673fSDevin Teske	local height=8 width=50 prefix="   "
361bc4a673fSDevin Teske	local plen=${#prefix} list= line=
362bc4a673fSDevin Teske	local max_width=$(( $width - 3 - $plen ))
363bc4a673fSDevin Teske
364bc4a673fSDevin Teske	local yes no defaultno extra_args format
365bc4a673fSDevin Teske	if [ "$USE_XDIALOG" ]; then
366bc4a673fSDevin Teske		yes=ok no=cancel defaultno=default-no
367bc4a673fSDevin Teske		extra_args="--wrap --left"
368bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure"
369bc4a673fSDevin Teske	else
370bc4a673fSDevin Teske		yes=yes no=no defaultno=defaultno
371bc4a673fSDevin Teske		extra_args="--colors --cr-wrap"
372bc4a673fSDevin Teske		format="$msg_last_chance_are_you_sure_color"
373bc4a673fSDevin Teske	fi
374bc4a673fSDevin Teske
375bc4a673fSDevin Teske	local disk line_width
376bc4a673fSDevin Teske	for disk in $*; do
377bc4a673fSDevin Teske		if [ "$line" ]; then
378bc4a673fSDevin Teske			line_width=${#line}
379bc4a673fSDevin Teske		else
380bc4a673fSDevin Teske			line_width=$plen
381bc4a673fSDevin Teske		fi
382bc4a673fSDevin Teske		line_width=$(( $line_width + 1 + ${#disk} ))
383bc4a673fSDevin Teske		# Add newline before disk if it would exceed max_width
384bc4a673fSDevin Teske		if [ $line_width -gt $max_width ]; then
385bc4a673fSDevin Teske			list="$list$line\n"
386bc4a673fSDevin Teske			line="$prefix"
387bc4a673fSDevin Teske			height=$(( $height + 1 ))
388bc4a673fSDevin Teske		fi
389bc4a673fSDevin Teske		# Add the disk to the list
390bc4a673fSDevin Teske		line="$line $disk"
391bc4a673fSDevin Teske	done
392bc4a673fSDevin Teske	# Append the left-overs
393bc4a673fSDevin Teske	if [ "${line#$prefix}" ]; then
394bc4a673fSDevin Teske		list="$list$line"
395bc4a673fSDevin Teske		height=$(( $height + 1 ))
396bc4a673fSDevin Teske	fi
397bc4a673fSDevin Teske
398bc4a673fSDevin Teske	# Add height for Xdialog(1)
399bc4a673fSDevin Teske	[ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
400bc4a673fSDevin Teske
401bc4a673fSDevin Teske	prompt=$( printf "$format" "$list" )
402bc4a673fSDevin Teske	f_dprintf "%s: Last Chance!" "$0"
403bc4a673fSDevin Teske	$DIALOG \
404bc4a673fSDevin Teske		--title "$title"        \
405bc4a673fSDevin Teske		--backtitle "$btitle"   \
406bc4a673fSDevin Teske		--hline "$hline"        \
407bc4a673fSDevin Teske		--$defaultno            \
408bc4a673fSDevin Teske		--$yes-label "$msg_yes" \
409bc4a673fSDevin Teske		--$no-label "$msg_no"   \
410bc4a673fSDevin Teske		$extra_args             \
411bc4a673fSDevin Teske		--yesno "$prompt" $height $width
412bc4a673fSDevin Teske}
413bc4a673fSDevin Teske
414bc4a673fSDevin Teske# dialog_menu_layout
415cd88b886SDevin Teske#
416bc4a673fSDevin Teske# Configure Virtual Device type and disks to use for the ZFS boot pool. User
417bc4a673fSDevin Teske# must select enough disks to satisfy the chosen vdev type.
418cd88b886SDevin Teske#
419bc4a673fSDevin Teskedialog_menu_layout()
420bc4a673fSDevin Teske{
421bc4a673fSDevin Teske	local funcname=dialog_menu_layout
422bc4a673fSDevin Teske	local title="$DIALOG_TITLE"
423bc4a673fSDevin Teske	local btitle="$DIALOG_BACKTITLE"
424bc4a673fSDevin Teske	local vdev_prompt="$msg_select_virtual_device_type"
425bc4a673fSDevin Teske	local disk_prompt="$msg_please_select_one_or_more_disks"
426bc4a673fSDevin Teske	local vdev_menu_list="
427bc4a673fSDevin Teske		'stripe' '$msg_stripe_desc' '$msg_stripe_help'
428bc4a673fSDevin Teske		'mirror' '$msg_mirror_desc' '$msg_mirror_help'
429bc4a673fSDevin Teske		'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
430bc4a673fSDevin Teske		'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
431bc4a673fSDevin Teske		'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
432bc4a673fSDevin Teske	" # END-QUOTE
433bc4a673fSDevin Teske	local disk_check_list= # Calculated below
434bc4a673fSDevin Teske	local vdev_hline="$hline_arrows_tab_enter"
435bc4a673fSDevin Teske	local disk_hline="$hline_arrows_space_tab_enter"
436bc4a673fSDevin Teske
437bc4a673fSDevin Teske	# Warn the user if vdev type is not valid
438bc4a673fSDevin Teske	case "$ZFSBOOT_VDEV_TYPE" in
439bc4a673fSDevin Teske	stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
440bc4a673fSDevin Teske	*)
441bc4a673fSDevin Teske		f_dprintf "%s: Invalid virtual device type \`%s'" \
442bc4a673fSDevin Teske			  $funcname "$ZFSBOOT_VDEV_TYPE"
443bc4a673fSDevin Teske		f_show_err "$msg_invalid_virtual_device_type" \
444bc4a673fSDevin Teske			   "$ZFSBOOT_VDEV_TYPE"
445bc4a673fSDevin Teske		f_interactive || return $FAILURE
446bc4a673fSDevin Teske	esac
447bc4a673fSDevin Teske
448bc4a673fSDevin Teske	# Calculate size of vdev menu once only
449bc4a673fSDevin Teske	local vheight vwidth vrows
450bc4a673fSDevin Teske	eval f_dialog_menu_with_help_size vheight vwidth vrows \
451bc4a673fSDevin Teske		\"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
452bc4a673fSDevin Teske		$vdev_menu_list
453bc4a673fSDevin Teske
454bc4a673fSDevin Teske	# Get a list of probed disk devices
455bc4a673fSDevin Teske	local disks=
456*a88393ceSDevin Teske	debug= f_device_find "" $DEVICE_TYPE_DISK disks
457*a88393ceSDevin Teske
458*a88393ceSDevin Teske	# Prune out mounted md(4) devices that may be part of the boot process
459*a88393ceSDevin Teske	local disk name new_list=
460*a88393ceSDevin Teske	for disk in $disks; do
461*a88393ceSDevin Teske		debug= $disk get name name
462*a88393ceSDevin Teske		case "$name" in
463*a88393ceSDevin Teske		md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
464*a88393ceSDevin Teske		esac
465*a88393ceSDevin Teske		new_list="$new_list $disk"
466*a88393ceSDevin Teske	done
467*a88393ceSDevin Teske	disks="${new_list# }"
468*a88393ceSDevin Teske
469*a88393ceSDevin Teske	# Debugging
470*a88393ceSDevin Teske	if [ "$debug" ]; then
471*a88393ceSDevin Teske		local disk_names=
472*a88393ceSDevin Teske		for disk in $disks; do
473*a88393ceSDevin Teske			debug= $disk get name name
474*a88393ceSDevin Teske			disk_names="$disk_names $name"
475*a88393ceSDevin Teske		done
476*a88393ceSDevin Teske		f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
477*a88393ceSDevin Teske	fi
478*a88393ceSDevin Teske
479cd88b886SDevin Teske	if [ ! "$disks" ]; then
480bc4a673fSDevin Teske		f_dprintf "No disk(s) present to configure"
481bc4a673fSDevin Teske		f_show_err "$msg_no_disks_present_to_configure"
482cd88b886SDevin Teske		return $FAILURE
483cd88b886SDevin Teske	fi
484cd88b886SDevin Teske
485cd88b886SDevin Teske	# Lets sort the disks array to be more user friendly
486*a88393ceSDevin Teske	f_device_sort_by name disks disks
487cd88b886SDevin Teske
488cd88b886SDevin Teske	#
489bc4a673fSDevin Teske	# Operate in a loop so we can (if interactive) repeat if not enough
490bc4a673fSDevin Teske	# disks are selected to satisfy the chosen vdev type or user wants to
491bc4a673fSDevin Teske	# back-up to the previous menu.
492cd88b886SDevin Teske	#
493*a88393ceSDevin Teske	local vardisk ndisks onoff selections vdev_choice breakout device
494*a88393ceSDevin Teske	local valid_disks all_valid want_disks desc height width rows
495bc4a673fSDevin Teske	while :; do
496cd88b886SDevin Teske		#
497bc4a673fSDevin Teske		# Confirm the vdev type that was selected
498cd88b886SDevin Teske		#
499bc4a673fSDevin Teske		if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
500bc4a673fSDevin Teske			vdev_choice=$( eval $DIALOG \
501cd88b886SDevin Teske				--title \"\$title\"              \
502cd88b886SDevin Teske				--backtitle \"\$btitle\"         \
503bc4a673fSDevin Teske				--hline \"\$vdev_hline\"         \
504cd88b886SDevin Teske				--ok-label \"\$msg_ok\"          \
505cd88b886SDevin Teske				--cancel-label \"\$msg_cancel\"  \
506cd88b886SDevin Teske				--item-help                      \
507bc4a673fSDevin Teske				--default-item \"\$ZFSBOOT_VDEV_TYPE\" \
508bc4a673fSDevin Teske				--menu \"\$vdev_prompt\"         \
509bc4a673fSDevin Teske				$vheight $vwidth $vrows          \
510bc4a673fSDevin Teske				$vdev_menu_list                  \
511cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
512bc4a673fSDevin Teske			) || return $?
513bc4a673fSDevin Teske				# Exit if user pressed ESC or chose Cancel/No
514bc4a673fSDevin Teske			f_dialog_data_sanitize vdev_choice
515cd88b886SDevin Teske
516bc4a673fSDevin Teske			ZFSBOOT_VDEV_TYPE="$vdev_choice"
517bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
518bc4a673fSDevin Teske			          "$ZFSBOOT_VDEV_TYPE"
519bc4a673fSDevin Teske		fi
520bc4a673fSDevin Teske
521bc4a673fSDevin Teske		# Determine the number of disks needed for this vdev type
522*a88393ceSDevin Teske		want_disks=0
523bc4a673fSDevin Teske		case "$ZFSBOOT_VDEV_TYPE" in
524cd88b886SDevin Teske		stripe) want_disks=1 ;;
525cd88b886SDevin Teske		mirror) want_disks=2 ;;
526cd88b886SDevin Teske		raidz1) want_disks=3 ;;
527cd88b886SDevin Teske		raidz2) want_disks=4 ;;
528cd88b886SDevin Teske		raidz3) want_disks=5 ;;
529cd88b886SDevin Teske		esac
530bc4a673fSDevin Teske
531*a88393ceSDevin Teske		#
532bc4a673fSDevin Teske		# Warn the user if any scripted disks are invalid
533*a88393ceSDevin Teske		#
534*a88393ceSDevin Teske		valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
535bc4a673fSDevin Teske		for disk in $ZFSBOOT_DISKS; do
536*a88393ceSDevin Teske			if debug= f_device_find -1 \
537*a88393ceSDevin Teske				$disk $DEVICE_TYPE_DISK device
538*a88393ceSDevin Teske			then
539bc4a673fSDevin Teske				valid_disks="$valid_disks $disk"
540bc4a673fSDevin Teske				continue
541cd88b886SDevin Teske			fi
542bc4a673fSDevin Teske			f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
543bc4a673fSDevin Teske			all_valid=
544bc4a673fSDevin Teske		done
545bc4a673fSDevin Teske		if [ ! "$all_valid" ]; then
546bc4a673fSDevin Teske			if [ "$ZFSBOOT_DISKS" ]; then
547bc4a673fSDevin Teske				f_show_err \
548bc4a673fSDevin Teske				    "$msg_missing_one_or_more_scripted_disks"
549bc4a673fSDevin Teske			else
550bc4a673fSDevin Teske				f_dprintf "No disks selected."
551bc4a673fSDevin Teske				f_interactive ||
552bc4a673fSDevin Teske					f_show_err "$msg_no_disks_selected"
553bc4a673fSDevin Teske			fi
554bc4a673fSDevin Teske			f_interactive || return $FAILURE
555bc4a673fSDevin Teske		fi
556bc4a673fSDevin Teske		ZFSBOOT_DISKS="${valid_disks# }"
557bc4a673fSDevin Teske
558bc4a673fSDevin Teske		#
559bc4a673fSDevin Teske		# Short-circuit if we're running non-interactively
560bc4a673fSDevin Teske		#
561bc4a673fSDevin Teske		if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
562*a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
563bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] && break # to success
564bc4a673fSDevin Teske
565bc4a673fSDevin Teske			# Not enough disks selected
566bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
567bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
568bc4a673fSDevin Teske			          "Not enough disks selected." \
569bc4a673fSDevin Teske				  $ndisks $want_disks
570bc4a673fSDevin Teske			f_interactive || return $FAILURE
571bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
572bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
573bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
574bc4a673fSDevin Teske				return $FAILURE
575bc4a673fSDevin Teske		fi
576bc4a673fSDevin Teske
577bc4a673fSDevin Teske		#
578bc4a673fSDevin Teske		# Confirm the disks that were selected
579bc4a673fSDevin Teske		# Loop until the user cancels or selects enough disks
580bc4a673fSDevin Teske		#
581*a88393ceSDevin Teske		breakout=
582bc4a673fSDevin Teske		while :; do
583bc4a673fSDevin Teske			# Loop over list of available disks, resetting state
584*a88393ceSDevin Teske			for disk in $disks; do
585*a88393ceSDevin Teske				f_isset _${disk}_status && _${disk}_status=
586*a88393ceSDevin Teske			done
587bc4a673fSDevin Teske
588bc4a673fSDevin Teske			# Loop over list of selected disks and create temporary
589bc4a673fSDevin Teske			# locals to map statuses onto up-to-date list of disks
590bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
591*a88393ceSDevin Teske				debug= f_device_find -1 \
592*a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK disk
593*a88393ceSDevin Teske				f_isset _${disk}_status ||
594*a88393ceSDevin Teske					local _${disk}_status
595*a88393ceSDevin Teske				_${disk}_status=on
596bc4a673fSDevin Teske			done
597bc4a673fSDevin Teske
598bc4a673fSDevin Teske			# Create the checklist menu of discovered disk devices
599bc4a673fSDevin Teske			disk_check_list=
600bc4a673fSDevin Teske			for disk in $disks; do
601*a88393ceSDevin Teske				desc=
602*a88393ceSDevin Teske				$disk get name name
603*a88393ceSDevin Teske				$disk get desc desc
604bc4a673fSDevin Teske				f_shell_escape "$desc" desc
605bc4a673fSDevin Teske				f_getvar _${disk}_status:-off onoff
606bc4a673fSDevin Teske				disk_check_list="$disk_check_list
607*a88393ceSDevin Teske					$name '$desc' $onoff"
608bc4a673fSDevin Teske			done
609bc4a673fSDevin Teske
610bc4a673fSDevin Teske			eval f_dialog_checklist_size height width rows \
611bc4a673fSDevin Teske				\"\$title\" \"\$btitle\" \"\$prompt\" \
612bc4a673fSDevin Teske				\"\$hline\" $disk_check_list
613bc4a673fSDevin Teske
614bc4a673fSDevin Teske			selections=$( eval $DIALOG \
615bc4a673fSDevin Teske				--title \"\$DIALOG_TITLE\"         \
616bc4a673fSDevin Teske				--backtitle \"\$DIALOG_BACKTITLE\" \
617751952aeSDevin Teske				--separate-output                  \
618bc4a673fSDevin Teske				--hline \"\$hline\"                \
619bc4a673fSDevin Teske				--ok-label \"\$msg_ok\"            \
620bc4a673fSDevin Teske				--cancel-label \"\$msg_back\"      \
621bc4a673fSDevin Teske				--checklist \"\$prompt\"           \
622bc4a673fSDevin Teske				$height $width $rows               \
623bc4a673fSDevin Teske				$disk_check_list                   \
624bc4a673fSDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
625bc4a673fSDevin Teske			) || break
626bc4a673fSDevin Teske				# Loop if user pressed ESC or chose Cancel/No
627bc4a673fSDevin Teske			f_dialog_data_sanitize selections
628bc4a673fSDevin Teske
629bc4a673fSDevin Teske			ZFSBOOT_DISKS="$selections"
630bc4a673fSDevin Teske			f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
631bc4a673fSDevin Teske			          "$ZFSBOOT_DISKS"
632bc4a673fSDevin Teske
633*a88393ceSDevin Teske			f_count ndisks $ZFSBOOT_DISKS
634bc4a673fSDevin Teske			[ $ndisks -ge $want_disks ] &&
635bc4a673fSDevin Teske				breakout=break && break
636bc4a673fSDevin Teske
637bc4a673fSDevin Teske			# Not enough disks selected
638bc4a673fSDevin Teske			f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
639bc4a673fSDevin Teske				  "$ZFSBOOT_VDEV_TYPE" \
640bc4a673fSDevin Teske			          "Not enough disks selected." \
641bc4a673fSDevin Teske			          $ndisks $want_disks
642bc4a673fSDevin Teske			msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
643bc4a673fSDevin Teske				f_yesno "%s: $msg_not_enough_disks_selected" \
644bc4a673fSDevin Teske				"$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
645bc4a673fSDevin Teske				break
646bc4a673fSDevin Teske		done
647bc4a673fSDevin Teske		[ "$breakout" = "break" ] && break
648bc4a673fSDevin Teske		[ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
649bc4a673fSDevin Teske	done
650bc4a673fSDevin Teske
651bc4a673fSDevin Teske	return $DIALOG_OK
652cd88b886SDevin Teske}
653cd88b886SDevin Teske
654cd88b886SDevin Teske# zfs_create_diskpart $disk $index
655cd88b886SDevin Teske#
656cd88b886SDevin Teske# For each block device to be used in the zpool, rather than just create the
657cd88b886SDevin Teske# zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
658cd88b886SDevin Teske# so we can have some real swap. This also provides wiggle room incase your
659cd88b886SDevin Teske# replacement drivers do not have the exact same sector counts.
660cd88b886SDevin Teske#
6617cae6aabSDevin Teske# NOTE: $swapsize and $bootsize should be defined by the calling function.
662a622223fSDevin Teske# NOTE: Sets $bootpart and $targetpart for the calling function.
663cd88b886SDevin Teske#
664cd88b886SDevin Teskezfs_create_diskpart()
665cd88b886SDevin Teske{
666cd88b886SDevin Teske	local funcname=zfs_create_diskpart
667bc4a673fSDevin Teske	local disk="$1" index="$2"
668cd88b886SDevin Teske
669cd88b886SDevin Teske	# Check arguments
670bc4a673fSDevin Teske	if [ ! "$disk" ]; then
671bc4a673fSDevin Teske		f_dprintf "$funcname: NULL disk argument"
672bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
673bc4a673fSDevin Teske			f_show_err "$msg_null_disk_argument"
674bc4a673fSDevin Teske		return $FAILURE
675bc4a673fSDevin Teske	fi
676bc4a673fSDevin Teske	if [ "${disk#*[$IFS]}" != "$disk" ]; then
677bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
678bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
679bc4a673fSDevin Teske			f_show_err "$msg_invalid_disk_argument" "$disk"
680bc4a673fSDevin Teske		return $FAILURE
681bc4a673fSDevin Teske	fi
682bc4a673fSDevin Teske	if [ ! "$index" ]; then
683bc4a673fSDevin Teske		f_dprintf "$funcname: NULL index argument"
684bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
685bc4a673fSDevin Teske			f_show_err "$msg_null_index_argument"
686bc4a673fSDevin Teske		return $FAILURE
687bc4a673fSDevin Teske	fi
688bc4a673fSDevin Teske	if ! f_isinteger "$index"; then
689bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
690bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
691bc4a673fSDevin Teske			f_show_err "$msg_invalid_index_argument" "$index"
692bc4a673fSDevin Teske		return $FAILURE
693bc4a673fSDevin Teske	fi
694bc4a673fSDevin Teske	f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
695cd88b886SDevin Teske
696d4d729e4SDevin Teske	# Check for unknown partition scheme before proceeding further
697d4d729e4SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
698d4d729e4SDevin Teske	""|MBR|GPT) : known good ;;
699d4d729e4SDevin Teske	*)
700d4d729e4SDevin Teske		f_dprintf "$funcname: %s is an unsupported partition scheme" \
701d4d729e4SDevin Teske		          "$ZFSBOOT_PARTITION_SCHEME"
702d4d729e4SDevin Teske		msg_error="$msg_error: $funcname" f_show_err \
703d4d729e4SDevin Teske			"$msg_unsupported_partition_scheme" \
704d4d729e4SDevin Teske			"$ZFSBOOT_PARTITION_SCHEME"
705d4d729e4SDevin Teske		return $FAILURE
706d4d729e4SDevin Teske	esac
707d4d729e4SDevin Teske
708cd88b886SDevin Teske	#
709cd88b886SDevin Teske	# Destroy whatever partition layout is currently on disk.
710cd88b886SDevin Teske	# NOTE: `-F' required to destroy if partitions still exist.
711cd88b886SDevin Teske	# NOTE: Failure is ok here, blank disk will have nothing to destroy.
712cd88b886SDevin Teske	#
713bc4a673fSDevin Teske	f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
714a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
715a622223fSDevin Teske	f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
716a622223fSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
717cd88b886SDevin Teske
718cd88b886SDevin Teske	# Make doubly-sure backup GPT is destroyed
719a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
720a622223fSDevin Teske	f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
721cd88b886SDevin Teske
7227cae6aabSDevin Teske	#
7237cae6aabSDevin Teske	# Enable boot pool if encryption is desired
7247cae6aabSDevin Teske	#
7257cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
726cd88b886SDevin Teske
727cd88b886SDevin Teske	#
728cd88b886SDevin Teske	# Lay down the desired type of partition scheme
729cd88b886SDevin Teske	#
730cd88b886SDevin Teske	local setsize mbrindex
731cd88b886SDevin Teske	case "$ZFSBOOT_PARTITION_SCHEME" in
732bc4a673fSDevin Teske	""|GPT) f_dprintf "$funcname: Creating GPT layout..."
733cd88b886SDevin Teske		#
734cd88b886SDevin Teske		# 1. Create GPT layout using labels
735cd88b886SDevin Teske		#
736a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
737bc4a673fSDevin Teske		             return $FAILURE
738cd88b886SDevin Teske
739cd88b886SDevin Teske		#
740cd88b886SDevin Teske		# 2. Add small freebsd-boot partition labeled `boot#'
741cd88b886SDevin Teske		#
742bc4a673fSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_LABEL_WITH_SIZE" \
743a622223fSDevin Teske		             gptboot$index freebsd-boot 512k $disk ||
744cd88b886SDevin Teske		             return $FAILURE
745bc4a673fSDevin Teske		f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
746a622223fSDevin Teske		             /boot/pmbr /boot/gptzfsboot 1 $disk ||
747cd88b886SDevin Teske		             return $FAILURE
748cd88b886SDevin Teske
749a622223fSDevin Teske		# NB: zpool will use the `zfs#' GPT labels
750cd88b886SDevin Teske		bootpart=p2 targetpart=p2
7517cae6aabSDevin Teske		[ ${swapsize:-0} -gt 0 ] && targetpart=p3
752cd88b886SDevin Teske
7537cae6aabSDevin Teske		#
7547cae6aabSDevin Teske		# Prepare boot pool if enabled (e.g., for geli(8))
7557cae6aabSDevin Teske		#
7567cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
757cd88b886SDevin Teske			bootpart=p2 targetpart=p3
7587cae6aabSDevin Teske			[ ${swapsize:-0} -gt 0 ] && targetpart=p4
759bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
760a622223fSDevin Teske			             "$GPART_ADD_LABEL_WITH_SIZE" boot$index \
7617cae6aabSDevin Teske			             freebsd-zfs ${bootsize}b $disk ||
762bc4a673fSDevin Teske			             return $FAILURE
7637cae6aabSDevin Teske			# Pedantically nuke any old labels
764a622223fSDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
765a622223fSDevin Teske			                /dev/$disk$bootpart
7667cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
7677cae6aabSDevin Teske				# Pedantically detach targetpart for later
7687cae6aabSDevin Teske				f_eval_catch -d $funcname geli \
7697cae6aabSDevin Teske				                "$GELI_DETACH_F" \
770a622223fSDevin Teske				                /dev/$disk$targetpart
771cd88b886SDevin Teske			fi
7727cae6aabSDevin Teske		fi
773cd88b886SDevin Teske
774cd88b886SDevin Teske		#
7757cae6aabSDevin Teske		# 3. Add freebsd-swap partition labeled `swap#'
776cd88b886SDevin Teske		#
777bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
778bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
7797cae6aabSDevin Teske			             "$GPART_ADD_LABEL_WITH_SIZE" swap$index \
7807cae6aabSDevin Teske			             freebsd-swap ${swapsize}b $disk ||
781cd88b886SDevin Teske			             return $FAILURE
782cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
783cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
784cf9c3e56SDevin Teske			                /dev/gpt/swap$index
785cd88b886SDevin Teske			# Update fstab(5)
786bc4a673fSDevin Teske			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
787a622223fSDevin Teske			             /dev/gpt/swap$index none swap sw 0 0 \
788a622223fSDevin Teske			             $BSDINSTALL_TMPETC/fstab ||
789bc4a673fSDevin Teske			             return $FAILURE
790cd88b886SDevin Teske		fi
7917cae6aabSDevin Teske
7927cae6aabSDevin Teske		#
7937cae6aabSDevin Teske		# 4. Add freebsd-zfs partition labeled `zfs#' for zroot
7947cae6aabSDevin Teske		#
7957cae6aabSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
7967cae6aabSDevin Teske		             zfs$index freebsd-zfs $disk || return $FAILURE
7977cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
7987cae6aabSDevin Teske		                /dev/$disk$targetpart
799cd88b886SDevin Teske		;;
800cd88b886SDevin Teske
801bc4a673fSDevin Teske	MBR) f_dprintf "$funcname: Creating MBR layout..."
802cd88b886SDevin Teske		#
803cd88b886SDevin Teske		# 1. Create MBR layout (no labels)
804cd88b886SDevin Teske		#
805a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
806bc4a673fSDevin Teske		             return $FAILURE
8072925848dSDevin Teske		f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
808a622223fSDevin Teske		             $disk || return $FAILURE
809cd88b886SDevin Teske
810cd88b886SDevin Teske		#
811cd88b886SDevin Teske		# 2. Add freebsd slice with all available space
812cd88b886SDevin Teske		#
813a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD" freebsd $disk ||
814bc4a673fSDevin Teske		             return $FAILURE
815a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
816bc4a673fSDevin Teske		             return $FAILURE
817a622223fSDevin Teske		# Pedantically nuke any old labels
818a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
819a622223fSDevin Teske		                /dev/${disk}s1
820a622223fSDevin Teske		# Pedantically nuke any old scheme
821a622223fSDevin Teske		f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
822cd88b886SDevin Teske
823cd88b886SDevin Teske		#
824bc4a673fSDevin Teske		# 3. Write BSD scheme to the freebsd slice
825cd88b886SDevin Teske		#
826a622223fSDevin Teske		f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
827bc4a673fSDevin Teske		             return $FAILURE
828cd88b886SDevin Teske
8297cae6aabSDevin Teske		# NB: zpool will use s1a (no labels)
8307cae6aabSDevin Teske		bootpart=s1a targetpart=s1d mbrindex=4
831cd88b886SDevin Teske
8327cae6aabSDevin Teske		#
8337cae6aabSDevin Teske		# Always prepare a boot pool on MBR
8347cae6aabSDevin Teske		#
8357cae6aabSDevin Teske		ZFSBOOT_BOOT_POOL=1
836bc4a673fSDevin Teske		f_eval_catch $funcname gpart \
837bc4a673fSDevin Teske		             "$GPART_ADD_INDEX_WITH_SIZE" \
8387cae6aabSDevin Teske		             1 freebsd-zfs ${bootsize}b ${disk}s1 ||
839bc4a673fSDevin Teske		             return $FAILURE
840a622223fSDevin Teske		# Pedantically nuke any old labels
841a622223fSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
842a622223fSDevin Teske		                /dev/$disk$bootpart
8437cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
844a622223fSDevin Teske			# Pedantically detach targetpart for later
845a622223fSDevin Teske			f_eval_catch -d $funcname geli \
846a622223fSDevin Teske			                "$GELI_DETACH_F" \
847a622223fSDevin Teske					/dev/$disk$targetpart
848cd88b886SDevin Teske		fi
849cd88b886SDevin Teske
850cd88b886SDevin Teske		#
8517cae6aabSDevin Teske		# 4. Add freebsd-swap partition
852cd88b886SDevin Teske		#
853bc4a673fSDevin Teske		if [ ${swapsize:-0} -gt 0 ]; then
854bc4a673fSDevin Teske			f_eval_catch $funcname gpart \
8557cae6aabSDevin Teske			             "$GPART_ADD_INDEX_WITH_SIZE" 2 \
8567cae6aabSDevin Teske			             freebsd-swap ${swapsize}b ${disk}s1 ||
857cd88b886SDevin Teske			             return $FAILURE
858cf9c3e56SDevin Teske			# Pedantically nuke any old labels on the swap
859cf9c3e56SDevin Teske			f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
860cf9c3e56SDevin Teske			                /dev/${disk}s1b
861cd88b886SDevin Teske			# Update fstab(5)
862bc4a673fSDevin Teske			f_eval_catch $funcname printf "$PRINTF_FSTAB" \
863a622223fSDevin Teske			             /dev/${disk}s1b none swap sw 0 0 \
864a622223fSDevin Teske			             $BSDINSTALL_TMPETC/fstab ||
865bc4a673fSDevin Teske			             return $FAILURE
866cd88b886SDevin Teske		fi
8677cae6aabSDevin Teske
8687cae6aabSDevin Teske		#
8697cae6aabSDevin Teske		# 5. Add freebsd-zfs partition for zroot
8707cae6aabSDevin Teske		#
8717cae6aabSDevin Teske		f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
8727cae6aabSDevin Teske			     $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
8737cae6aabSDevin Teske		f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
8747cae6aabSDevin Teske				/dev/$disk$targetpart # Pedantic
8757cae6aabSDevin Teske		f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
8767cae6aabSDevin Teske			     /boot/zfsboot /dev/${disk}s1 count=1 ||
8777cae6aabSDevin Teske			     return $FAILURE
878cd88b886SDevin Teske		;;
879cd88b886SDevin Teske
880cd88b886SDevin Teske	esac # $ZFSBOOT_PARTITION_SCHEME
881cd88b886SDevin Teske
882cd88b886SDevin Teske	return $SUCCESS
883cd88b886SDevin Teske}
884cd88b886SDevin Teske
885bc4a673fSDevin Teske# zfs_create_boot $poolname $vdev_type $disks ...
886cd88b886SDevin Teske#
887cd88b886SDevin Teske# Creates boot pool and dataset layout. Returns error if something goes wrong.
888cd88b886SDevin Teske# Errors are printed to stderr for collection and display.
889cd88b886SDevin Teske#
890cd88b886SDevin Teskezfs_create_boot()
891cd88b886SDevin Teske{
892cd88b886SDevin Teske	local funcname=zfs_create_boot
8937cae6aabSDevin Teske	local zroot_name="$1"
8947cae6aabSDevin Teske	local zroot_vdevtype="$2"
8957cae6aabSDevin Teske	local zroot_vdevs= # Calculated below
8967cae6aabSDevin Teske	local boot_vdevs= # Used for geli(8) and/or MBR layouts
897a622223fSDevin Teske	shift 2 # poolname vdev_type
898a622223fSDevin Teske	local disks="$*" disk
899a622223fSDevin Teske	local bootpart targetpart # Set by zfs_create_diskpart() below
900cd88b886SDevin Teske
901a622223fSDevin Teske	#
902cd88b886SDevin Teske	# Pedantic checks; should never be seen
903a622223fSDevin Teske	#
9047cae6aabSDevin Teske	if [ ! "$zroot_name" ]; then
905bc4a673fSDevin Teske		f_dprintf "$funcname: NULL poolname"
906bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
907bc4a673fSDevin Teske			f_show_err "$msg_null_poolname"
908cd88b886SDevin Teske		return $FAILURE
909cd88b886SDevin Teske	fi
910cd88b886SDevin Teske	if [ $# -lt 1 ]; then
911bc4a673fSDevin Teske		f_dprintf "$funcname: missing disk arguments"
912bc4a673fSDevin Teske		msg_error="$msg_error: $funcname" \
913bc4a673fSDevin Teske			f_show_err "$msg_missing_disk_arguments"
914cd88b886SDevin Teske		return $FAILURE
915cd88b886SDevin Teske	fi
916bc4a673fSDevin Teske	f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
9177cae6aabSDevin Teske	          "$zroot_name" "$zroot_vdevtype"
918cd88b886SDevin Teske
919a622223fSDevin Teske	#
920cd88b886SDevin Teske	# Initialize fstab(5)
921a622223fSDevin Teske	#
922bc4a673fSDevin Teske	f_dprintf "$funcname: Initializing temporary fstab(5) file..."
923a622223fSDevin Teske	f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
924bc4a673fSDevin Teske	             return $FAILURE
925bc4a673fSDevin Teske	f_eval_catch $funcname printf "$PRINTF_FSTAB" \
926cd88b886SDevin Teske	             "# Device" Mountpoint FStype Options Dump "Pass#" \
927a622223fSDevin Teske	             $BSDINSTALL_TMPETC/fstab || return $FAILURE
928cd88b886SDevin Teske
929a622223fSDevin Teske	#
930cd88b886SDevin Teske	# Expand SI units in desired sizes
931a622223fSDevin Teske	#
9327cae6aabSDevin Teske	f_dprintf "$funcname: Expanding supplied size values..."
9337cae6aabSDevin Teske	local swapsize bootsize
934bc4a673fSDevin Teske	if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
935bc4a673fSDevin Teske		f_dprintf "$funcname: Invalid swap size \`%s'" \
936bc4a673fSDevin Teske		          "$ZFSBOOT_SWAP_SIZE"
937bc4a673fSDevin Teske		f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
938bc4a673fSDevin Teske		return $FAILURE
939bc4a673fSDevin Teske	fi
9407cae6aabSDevin Teske	if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
9417cae6aabSDevin Teske		f_dprintf "$funcname: Invalid boot pool size \`%s'" \
9427cae6aabSDevin Teske		          "$ZFSBOOT_BOOT_POOL_SIZE"
9437cae6aabSDevin Teske		f_show_err "$msg_invalid_boot_pool_size" \
9447cae6aabSDevin Teske		           "$ZFSBOOT_BOOT_POOL_SIZE"
945bc4a673fSDevin Teske		return $FAILURE
946bc4a673fSDevin Teske	fi
947bc4a673fSDevin Teske	f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
948bc4a673fSDevin Teske	          "$ZFSBOOT_SWAP_SIZE" "$swapsize"
9497cae6aabSDevin Teske	f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
9507cae6aabSDevin Teske	          "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
951cd88b886SDevin Teske
952a622223fSDevin Teske	#
953*a88393ceSDevin Teske	# Destroy the pool in-case this is our second time 'round (case of
954*a88393ceSDevin Teske	# failure and installer presented ``Retry'' option to come back).
955*a88393ceSDevin Teske	#
956*a88393ceSDevin Teske	# NB: If we don't destroy the pool, later gpart(8) destroy commands
957*a88393ceSDevin Teske	# that try to clear existing partitions (see zfs_create_diskpart())
958*a88393ceSDevin Teske	# will fail with a `Device Busy' error, leading to `GEOM exists'.
959*a88393ceSDevin Teske	#
960*a88393ceSDevin Teske	f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
961*a88393ceSDevin Teske
962*a88393ceSDevin Teske	#
9637cae6aabSDevin Teske	# Prepare the disks and build pool device list(s)
964a622223fSDevin Teske	#
965bc4a673fSDevin Teske	f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
9667cae6aabSDevin Teske	[ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] &&
9677cae6aabSDevin Teske		f_dprintf "$funcname: With 4k alignment using gnop(8)..."
968cd88b886SDevin Teske	local n=0
969a622223fSDevin Teske	for disk in $disks; do
970cd88b886SDevin Teske		zfs_create_diskpart $disk $n || return $FAILURE
971a622223fSDevin Teske		# Now $bootpart and $targetpart are set (suffix for $disk)
972cd88b886SDevin Teske
9737cae6aabSDevin Teske		# Forced 4k alignment support using Geom NOP (see gnop(8))
974cd88b886SDevin Teske		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
9757cae6aabSDevin Teske			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9767cae6aabSDevin Teske				boot_vdevs="$boot_vdevs $disk$bootpart.nop"
977bc4a673fSDevin Teske				f_eval_catch $funcname gnop "$GNOP_CREATE" \
9787cae6aabSDevin Teske				             $disk$bootpart || return $FAILURE
979cd88b886SDevin Teske			fi
9807cae6aabSDevin Teske			# Don't gnop encrypted partition
9817cae6aabSDevin Teske			if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
9827cae6aabSDevin Teske				zroot_vdevs="$zroot_vdevs $disk$targetpart.eli"
983cd88b886SDevin Teske			else
9847cae6aabSDevin Teske				zroot_vdevs="$zroot_vdevs $disk$targetpart.nop"
9857cae6aabSDevin Teske				f_eval_catch $funcname gnop "$GNOP_CREATE" \
9867cae6aabSDevin Teske					     $disk$targetpart ||
9877cae6aabSDevin Teske				             return $FAILURE
9887cae6aabSDevin Teske			fi
9897cae6aabSDevin Teske		else
9907cae6aabSDevin Teske			if [ "$ZFSBOOT_BOOT_POOL" ]; then
9917cae6aabSDevin Teske				boot_vdevs="$boot_vdevs $disk$bootpart"
9927cae6aabSDevin Teske			fi
9937cae6aabSDevin Teske			zroot_vdevs="$zroot_vdevs $disk$targetpart"
9947cae6aabSDevin Teske		fi
9957cae6aabSDevin Teske
9967cae6aabSDevin Teske		n=$(( $n + 1 ))
9977cae6aabSDevin Teske	done # disks
9987cae6aabSDevin Teske
9997cae6aabSDevin Teske	#
10007cae6aabSDevin Teske	# If we need/want a boot pool, create it
10017cae6aabSDevin Teske	#
10027cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
10037cae6aabSDevin Teske		local bootpool_vdevtype= # Calculated below
10047cae6aabSDevin Teske		local bootpool_options= # Calculated below
10057cae6aabSDevin Teske		local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
10067cae6aabSDevin Teske		local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
10077cae6aabSDevin Teske		local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
10087cae6aabSDevin Teske
10097cae6aabSDevin Teske		f_dprintf "$funcname: Setting up boot pool..."
1010cd88b886SDevin Teske		[ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
10117cae6aabSDevin Teske			f_dprintf "$funcname: For encrypted root disk..."
1012cd88b886SDevin Teske
10137cae6aabSDevin Teske		# Create parent directory for boot pool
1014a622223fSDevin Teske		f_eval_catch -d $funcname umount "$UMOUNT" /mnt
1015bc4a673fSDevin Teske		f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1016a622223fSDevin Teske		             $BSDINSTALL_CHROOT || return $FAILURE
1017cd88b886SDevin Teske
10187cae6aabSDevin Teske		# Create mirror across the boot partition on all disks
1019*a88393ceSDevin Teske		local nvdevs
1020*a88393ceSDevin Teske		f_count nvdevs $boot_vdevs
1021*a88393ceSDevin Teske		[ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1022cd88b886SDevin Teske
10237cae6aabSDevin Teske		bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
10247cae6aabSDevin Teske		bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1025bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
10267cae6aabSDevin Teske		             "$bootpool_options" "$bootpool_name" \
10274b4b90d5SDevin Teske		             "$bootpool_vdevtype" "$boot_vdevs" ||
10284b4b90d5SDevin Teske		             return $FAILURE
102945df402eSDevin Teske
10307cae6aabSDevin Teske		f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1031cd88b886SDevin Teske		             return $FAILURE
1032cd88b886SDevin Teske
10337cae6aabSDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1034cd88b886SDevin Teske			# Generate an encryption key using random(4)
1035bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
10367cae6aabSDevin Teske				     /dev/random "$bootpool/$zroot_key" \
10377cae6aabSDevin Teske			             "bs=4096 count=1" || return $FAILURE
10387cae6aabSDevin Teske		else
10397cae6aabSDevin Teske			# Clean up
10407cae6aabSDevin Teske			f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
10417cae6aabSDevin Teske			             "$bootpool_name" || return $FAILURE
10427cae6aabSDevin Teske			f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
10437cae6aabSDevin Teske		fi
10447cae6aabSDevin Teske
10457cae6aabSDevin Teske	fi
1046cd88b886SDevin Teske
1047a622223fSDevin Teske	#
1048cd88b886SDevin Teske	# Create the geli(8) GEOMS
1049a622223fSDevin Teske	#
10507cae6aabSDevin Teske	if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
10517cae6aabSDevin Teske		# Prompt user for password (twice)
1052bc4a673fSDevin Teske		if ! msg_enter_new_password="$msg_geli_password" \
1053bc4a673fSDevin Teske			f_dialog_input_password
1054bc4a673fSDevin Teske		then
1055bc4a673fSDevin Teske			f_dprintf "$funcname: User cancelled"
1056bc4a673fSDevin Teske			f_show_err "$msg_user_cancelled"
1057bc4a673fSDevin Teske			return $FAILURE
1058bc4a673fSDevin Teske		fi
1059a622223fSDevin Teske
1060a622223fSDevin Teske		# Initialize geli(8) on each of the target partitions
1061bc4a673fSDevin Teske		for disk in $disks; do
1062cd88b886SDevin Teske			f_dialog_info "$msg_geli_setup" \
1063cd88b886SDevin Teske				2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1064a622223fSDevin Teske			if ! echo "$pw_password" | f_eval_catch \
1065a622223fSDevin Teske				$funcname geli "$GELI_PASSWORD_INIT" \
10667cae6aabSDevin Teske				"$bootpool/boot/$disk$targetpart.eli" \
10677cae6aabSDevin Teske				AES-XTS "$bootpool/$zroot_key" \
1068a622223fSDevin Teske				$disk$targetpart
1069bc4a673fSDevin Teske			then
1070bc4a673fSDevin Teske				f_interactive || f_die
1071a622223fSDevin Teske				unset pw_password # Sensitive info
1072cd88b886SDevin Teske				return $FAILURE
1073bc4a673fSDevin Teske			fi
1074a622223fSDevin Teske			if ! echo "$pw_password" | f_eval_catch \
1075a622223fSDevin Teske				$funcname geli "$GELI_ATTACH" \
10767cae6aabSDevin Teske				"$bootpool/$zroot_key" $disk$targetpart
1077bc4a673fSDevin Teske			then
1078bc4a673fSDevin Teske				f_interactive || f_die
1079a622223fSDevin Teske				unset pw_password # Sensitive info
1080bc4a673fSDevin Teske				return $FAILURE
1081bc4a673fSDevin Teske			fi
1082cd88b886SDevin Teske		done
1083a622223fSDevin Teske		unset pw_password # Sensitive info
1084a622223fSDevin Teske
1085a622223fSDevin Teske		# Clean up
10867cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
10877cae6aabSDevin Teske			return $FAILURE
1088a622223fSDevin Teske		f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1089cd88b886SDevin Teske	fi
1090cd88b886SDevin Teske
1091cd88b886SDevin Teske	#
1092a622223fSDevin Teske	# Create the ZFS root pool with desired type and disk devices
1093cd88b886SDevin Teske	#
10947cae6aabSDevin Teske	f_dprintf "$funcname: Creating root pool..."
1095bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1096a622223fSDevin Teske	             "-o altroot=$BSDINSTALL_CHROOT -m none -f" \
10974b4b90d5SDevin Teske	             "$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
10984b4b90d5SDevin Teske	             return $FAILURE
1099cd88b886SDevin Teske
11007cae6aabSDevin Teske	# Customize the zroot a bit...
1101bc4a673fSDevin Teske	local option
11027cae6aabSDevin Teske	f_dprintf "$funcname: Setting miscellaneous options on root pool..."
1103ca2d97c2SDevin Teske	for option in atime=off; do
11047cae6aabSDevin Teske		f_eval_catch $funcname zfs "$ZFS_SET" $option "$zroot_name" ||
1105bc4a673fSDevin Teske			return $FAILURE
1106bc4a673fSDevin Teske	done
1107cd88b886SDevin Teske
1108cd88b886SDevin Teske	#
11097cae6aabSDevin Teske	# Create ZFS dataset layout within the new root pool
1110cd88b886SDevin Teske	#
1111bc4a673fSDevin Teske	f_dprintf "$funcname: Creating ZFS datasets..."
1112cd88b886SDevin Teske	echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1113cd88b886SDevin Teske		# Skip blank lines and comments
1114cd88b886SDevin Teske		case "$dataset" in "#"*|"") continue; esac
1115cd88b886SDevin Teske		# Remove potential inline comments in options
1116cd88b886SDevin Teske		options="${options%%#*}"
1117cd88b886SDevin Teske		# Replace tabs with spaces
1118cd88b886SDevin Teske		f_replaceall "$options" "	" " " options
1119cd88b886SDevin Teske		# Reduce contiguous runs of space to one single space
1120cd88b886SDevin Teske		oldoptions=
1121cd88b886SDevin Teske		while [ "$oldoptions" != "$options" ]; do
1122cd88b886SDevin Teske			oldoptions="$options"
1123cd88b886SDevin Teske			f_replaceall "$options" "  " " " options
1124cd88b886SDevin Teske		done
1125cd88b886SDevin Teske		# Replace both commas and spaces with ` -o '
1126cd88b886SDevin Teske		f_replaceall "$options" "[ ,]" " -o " options
1127cd88b886SDevin Teske		# Create the dataset with desired options
1128bc4a673fSDevin Teske		f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
11297cae6aabSDevin Teske		             "${options:+-o $options}" "$zroot_name$dataset" ||
1130cd88b886SDevin Teske		             return $FAILURE
1131cd88b886SDevin Teske	done
1132cd88b886SDevin Teske
1133cd88b886SDevin Teske	# Touch up permissions on the tmp directories
1134bc4a673fSDevin Teske	f_dprintf "$funcname: Modifying directory permissions..."
1135bc4a673fSDevin Teske	local dir
1136bc4a673fSDevin Teske	for dir in /tmp /var/tmp; do
1137bc4a673fSDevin Teske		f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1138a622223fSDevin Teske		             $BSDINSTALL_CHROOTDIR$dir || return $FAILURE
1139bc4a673fSDevin Teske	done
1140cd88b886SDevin Teske
1141cd88b886SDevin Teske	# Create symlink(s)
11427cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
11437cae6aabSDevin Teske		f_dprintf "$funcname: Creating /boot symlink for boot pool..."
11447cae6aabSDevin Teske		f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1145a622223fSDevin Teske		             $BSDINSTALL_CHROOT/boot || return $FAILURE
1146bc4a673fSDevin Teske	fi
1147cd88b886SDevin Teske
1148cd88b886SDevin Teske	# Set bootfs property
11497cae6aabSDevin Teske	local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1150bc4a673fSDevin Teske	f_dprintf "$funcname: Setting bootfs property..."
1151bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
11527cae6aabSDevin Teske		"bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
11537cae6aabSDevin Teske		return $FAILURE
1154cd88b886SDevin Teske
1155cd88b886SDevin Teske	# Export the pool(s)
1156bc4a673fSDevin Teske	f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
11577cae6aabSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1158bc4a673fSDevin Teske	             return $FAILURE
11597cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1160bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
11617cae6aabSDevin Teske		             "$bootpool_name" || return $FAILURE
1162bc4a673fSDevin Teske	fi
1163cd88b886SDevin Teske
1164cd88b886SDevin Teske	# Destroy the gnop devices (if enabled)
1165bc4a673fSDevin Teske	for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$disks}; do
11667cae6aabSDevin Teske		if [ "$ZFSBOOT_BOOT_POOL" ]; then
1167a622223fSDevin Teske			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1168a622223fSDevin Teske			                $disk$bootpart.nop
11697cae6aabSDevin Teske		fi
11707cae6aabSDevin Teske		if [ ! "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1171a622223fSDevin Teske			f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1172a622223fSDevin Teske			                $disk$targetpart.nop
1173cd88b886SDevin Teske		fi
1174cd88b886SDevin Teske	done
1175cd88b886SDevin Teske
11767cae6aabSDevin Teske	# MBR boot loader touch-up
1177cd88b886SDevin Teske	if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1178bc4a673fSDevin Teske		f_dprintf "$funcname: Updating MBR boot loader on disks..."
1179cd88b886SDevin Teske		# Stick the ZFS boot loader in the "convienient hole" after
1180cd88b886SDevin Teske		# the ZFS internal metadata
1181bc4a673fSDevin Teske		for disk in $disks; do
1182bc4a673fSDevin Teske			f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1183a622223fSDevin Teske			             /boot/zfsboot /dev/$disk$bootpart \
1184bc4a673fSDevin Teske			             "skip=1 seek=1024" || return $FAILURE
1185cd88b886SDevin Teske		done
1186cd88b886SDevin Teske	fi
1187cd88b886SDevin Teske
1188cd88b886SDevin Teske	# Re-import the ZFS pool(s)
1189bc4a673fSDevin Teske	f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1190bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
11917cae6aabSDevin Teske	             "-o altroot=\"$BSDINSTALL_CHROOT\"" "$zroot_name" ||
1192bc4a673fSDevin Teske	             return $FAILURE
11937cae6aabSDevin Teske	if [ "$ZFSBOOT_BOOT_POOL" ]; then
1194bc4a673fSDevin Teske		f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
11957cae6aabSDevin Teske		             "-o altroot=\"$BSDINSTALL_CHROOT\"" \
11967cae6aabSDevin Teske		             "$bootpool_name" || return $FAILURE
1197bc4a673fSDevin Teske	fi
1198cd88b886SDevin Teske
1199cd88b886SDevin Teske	# While this is apparently not needed, it seems to help MBR
12006311cc9eSDevin Teske	f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1201a622223fSDevin Teske	f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1202bc4a673fSDevin Teske	             return $FAILURE
1203bc4a673fSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
1204a622223fSDevin Teske	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
12057cae6aabSDevin Teske	             "$zroot_name" || return $FAILURE
1206cd88b886SDevin Teske
120713d8e1ceSDevin Teske	# Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1208cd88b886SDevin Teske	# NOTE: We later concatenate these into their destination
1209bc4a673fSDevin Teske	f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1210bc4a673fSDevin Teske	          "$funcname"
1211bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1212a622223fSDevin Teske	             $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1213bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_load=\"YES\"' \
1214a622223fSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1215ee482f2cSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" \
1216ee482f2cSDevin Teske	             'kern.geom.label.disk_ident.enable=\"0\"' \
1217ee482f2cSDevin Teske	             $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1218cd88b886SDevin Teske
12197cae6aabSDevin Teske	# We're all done unless we should go on for boot pool
12207cae6aabSDevin Teske	[ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1221cd88b886SDevin Teske
12226311cc9eSDevin Teske	# Set cachefile for boot pool so it auto-imports at system start
12236311cc9eSDevin Teske	f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
12246311cc9eSDevin Teske	f_eval_catch $funcname zpool "$ZPOOL_SET" \
12256311cc9eSDevin Teske	             "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
12266311cc9eSDevin Teske	             "$bootpool_name" || return $FAILURE
12276311cc9eSDevin Teske
1228bc4a673fSDevin Teske	# Some additional geli(8) requirements for loader.conf(5)
1229bc4a673fSDevin Teske	for option in \
1230a622223fSDevin Teske		'zpool_cache_load=\"YES\"' \
1231a622223fSDevin Teske		'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1232a622223fSDevin Teske		'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1233bc4a673fSDevin Teske	; do
1234a622223fSDevin Teske		f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1235a622223fSDevin Teske		             $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1236bc4a673fSDevin Teske		             return $FAILURE
1237bc4a673fSDevin Teske	done
12387cae6aabSDevin Teske	f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
12397cae6aabSDevin Teske	    "\"zfs:$zroot_name/$zroot_bootfs\"" \
12407cae6aabSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
12417cae6aabSDevin Teske
12427cae6aabSDevin Teske	# We're all done unless we should go on to do encryption
12437cae6aabSDevin Teske	[ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
124413d8e1ceSDevin Teske
1245cd88b886SDevin Teske	#
1246cd88b886SDevin Teske	# Configure geli(8)-based encryption
1247cd88b886SDevin Teske	#
1248bc4a673fSDevin Teske	f_dprintf "$funcname: Configuring disk encryption..."
1249bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1250a622223fSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1251bc4a673fSDevin Teske	f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1252a622223fSDevin Teske	    $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1253bc4a673fSDevin Teske	for disk in $disks; do
1254bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1255a622223fSDevin Teske			geli_%s_keyfile0_load "$disk$targetpart YES" \
1256a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1257cd88b886SDevin Teske			return $FAILURE
1258bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1259bc4a673fSDevin Teske			geli_%s_keyfile0_type \
1260a622223fSDevin Teske			"$disk$targetpart $disk$targetpart:geli_keyfile0" \
1261a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1262cd88b886SDevin Teske			return $FAILURE
1263bc4a673fSDevin Teske		f_eval_catch $funcname printf "$PRINTF_CONF" \
1264bc4a673fSDevin Teske			geli_%s_keyfile0_name \
1265a622223fSDevin Teske			"$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1266a622223fSDevin Teske			$BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1267cd88b886SDevin Teske			return $FAILURE
1268cd88b886SDevin Teske	done
1269cd88b886SDevin Teske
1270cd88b886SDevin Teske	return $SUCCESS
1271cd88b886SDevin Teske}
1272cd88b886SDevin Teske
1273cd88b886SDevin Teske# dialog_menu_diskinfo
1274cd88b886SDevin Teske#
1275cd88b886SDevin Teske# Prompt the user to select a disk and then provide detailed info on it.
1276cd88b886SDevin Teske#
1277cd88b886SDevin Teskedialog_menu_diskinfo()
1278cd88b886SDevin Teske{
1279*a88393ceSDevin Teske	local device disk
1280cd88b886SDevin Teske
1281cd88b886SDevin Teske	#
1282cd88b886SDevin Teske	# Break from loop when user cancels disk selection
1283cd88b886SDevin Teske	#
1284cd88b886SDevin Teske	while :; do
1285*a88393ceSDevin Teske		device=$( msg_cancel="$msg_back" f_device_menu \
1286cd88b886SDevin Teske			"$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1287cd88b886SDevin Teske			$DEVICE_TYPE_DISK 2>&1 ) || break
1288*a88393ceSDevin Teske		$device get name disk
1289cd88b886SDevin Teske
1290cd88b886SDevin Teske		# Show gpart(8) `show' and camcontrol(8) `inquiry' data
1291cd88b886SDevin Teske		f_show_msg "$msg_detailed_disk_info" \
1292cd88b886SDevin Teske			"$disk" "$( gpart show $disk 2> /dev/null )" \
1293cd88b886SDevin Teske			"$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1294cd88b886SDevin Teske			"$disk" "$( camcontrol identify $disk 2> /dev/null )"
1295cd88b886SDevin Teske	done
1296cd88b886SDevin Teske
1297cd88b886SDevin Teske	return $SUCCESS
1298cd88b886SDevin Teske}
1299cd88b886SDevin Teske
1300cd88b886SDevin Teske############################################################ MAIN
1301cd88b886SDevin Teske
1302cd88b886SDevin Teske#
1303cd88b886SDevin Teske# Initialize
1304cd88b886SDevin Teske#
1305cd88b886SDevin Teskef_dialog_title "$msg_zfs_configuration"
1306cd88b886SDevin Teskef_dialog_backtitle "$msg_freebsd_installer"
1307cd88b886SDevin Teske
1308cd88b886SDevin Teske# User may have specifically requested ZFS-related operations be interactive
1309cd88b886SDevin Teske! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1310cd88b886SDevin Teske
1311cd88b886SDevin Teske#
1312bc4a673fSDevin Teske# Debugging
1313bc4a673fSDevin Teske#
1314bc4a673fSDevin Teskef_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1315bc4a673fSDevin Teskef_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1316a622223fSDevin Teskef_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1317bc4a673fSDevin Teske
1318bc4a673fSDevin Teske#
1319cd88b886SDevin Teske# Loop over the main menu until we've accomplished what we came here to do
1320cd88b886SDevin Teske#
1321cd88b886SDevin Teskewhile :; do
1322cd88b886SDevin Teske	if ! f_interactive; then
1323cd88b886SDevin Teske		retval=$DIALOG_OK
1324bc4a673fSDevin Teske		mtag=">>> $msg_install"
1325cd88b886SDevin Teske	else
1326cd88b886SDevin Teske		dialog_menu_main
1327cd88b886SDevin Teske		retval=$?
1328cd88b886SDevin Teske		f_dialog_menutag_fetch mtag
1329cd88b886SDevin Teske	fi
1330cd88b886SDevin Teske
1331bc4a673fSDevin Teske	f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1332cd88b886SDevin Teske	[ $retval -eq $DIALOG_OK ] || f_die
1333cd88b886SDevin Teske
1334cd88b886SDevin Teske	case "$mtag" in
1335bc4a673fSDevin Teske	">>> $msg_install")
1336cd88b886SDevin Teske		#
1337cd88b886SDevin Teske		# First, validate the user's selections
1338cd88b886SDevin Teske		#
1339cd88b886SDevin Teske
1340cd88b886SDevin Teske		# Make sure they gave us a name for the pool
1341cd88b886SDevin Teske		if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1342bc4a673fSDevin Teske			f_dprintf "Pool name cannot be empty."
1343bc4a673fSDevin Teske			f_show_err "$msg_pool_name_cannot_be_empty"
1344cd88b886SDevin Teske			continue
1345cd88b886SDevin Teske		fi
1346bc4a673fSDevin Teske
1347bc4a673fSDevin Teske		# Validate vdev type against number of disks selected/scripted
1348bc4a673fSDevin Teske		# (also validates that ZFSBOOT_DISKS are real [probed] disks)
1349bc4a673fSDevin Teske		# NB: dialog_menu_layout supports running non-interactively
1350bc4a673fSDevin Teske		dialog_menu_layout || continue
1351bc4a673fSDevin Teske
1352cd88b886SDevin Teske		# Make sure each disk will be at least 50% ZFS
1353cd88b886SDevin Teske		if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
13547cae6aabSDevin Teske		   f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize
1355cd88b886SDevin Teske		then
1356cd88b886SDevin Teske			minsize=$swapsize teeny_disks=
13577cae6aabSDevin Teske			[ "$ZFSBOOT_BOOT_POOL" ] &&
13587cae6aabSDevin Teske				minsize=$(( $minsize + $bootsize ))
1359bc4a673fSDevin Teske			for disk in $ZFSBOOT_DISKS; do
1360*a88393ceSDevin Teske				debug= f_device_find -1 \
1361*a88393ceSDevin Teske					$disk $DEVICE_TYPE_DISK device
1362*a88393ceSDevin Teske				$device get capacity disksize || continue
1363*a88393ceSDevin Teske				[ ${disksize:-0} -ge 0 ] || disksize=0
1364cd88b886SDevin Teske				disksize=$(( $disksize - $minsize ))
1365cd88b886SDevin Teske				[ $disksize -lt $minsize ] &&
1366cd88b886SDevin Teske					teeny_disks="$teeny_disks $disk"
1367cd88b886SDevin Teske			done
1368cd88b886SDevin Teske			if [ "$teeny_disks" ]; then
13697cae6aabSDevin Teske				f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1370bc4a673fSDevin Teske				          "$ZFSBOOT_SWAP_SIZE" \
13717cae6aabSDevin Teske				          "$ZFSBOOT_BOOT_POOL_SIZE" \
13727cae6aabSDevin Teske				          "minsize=[$minsize]"
1373bc4a673fSDevin Teske				f_dprintf "These disks are too small: %s" \
1374bc4a673fSDevin Teske				          "$teeny_disks"
1375bc4a673fSDevin Teske				f_show_err "$msg_these_disks_are_too_small" \
1376cd88b886SDevin Teske				           "$ZFSBOOT_SWAP_SIZE" \
13777cae6aabSDevin Teske				           "$ZFSBOOT_BOOT_POOL_SIZE" \
1378cd88b886SDevin Teske				           "$teeny_disks"
1379cd88b886SDevin Teske				continue
1380cd88b886SDevin Teske			fi
1381cd88b886SDevin Teske		fi
1382cd88b886SDevin Teske
1383cd88b886SDevin Teske		#
1384cd88b886SDevin Teske		# Last Chance!
1385cd88b886SDevin Teske		#
1386bc4a673fSDevin Teske		if f_interactive; then
1387bc4a673fSDevin Teske			dialog_last_chance $ZFSBOOT_DISKS || continue
1388cd88b886SDevin Teske		fi
1389cd88b886SDevin Teske
1390cd88b886SDevin Teske		#
1391cd88b886SDevin Teske		# Let's do this
1392cd88b886SDevin Teske		#
1393cd88b886SDevin Teske
1394cd88b886SDevin Teske		vdev_type="$ZFSBOOT_VDEV_TYPE"
1395cd88b886SDevin Teske
1396cd88b886SDevin Teske		# Blank the vdev type for the default layout
1397cd88b886SDevin Teske		[ "$vdev_type" = "stripe" ] && vdev_type=
1398cd88b886SDevin Teske
1399bc4a673fSDevin Teske		zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1400bc4a673fSDevin Teske		                "$vdev_type" $ZFSBOOT_DISKS || continue
1401cd88b886SDevin Teske
1402cd88b886SDevin Teske		break # to success
1403cd88b886SDevin Teske		;;
1404bc4a673fSDevin Teske	?" $msg_pool_type_disks")
1405bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=1
1406bc4a673fSDevin Teske		dialog_menu_layout
1407bc4a673fSDevin Teske		# User has poked settings, disable later confirmation
1408bc4a673fSDevin Teske		ZFSBOOT_CONFIRM_LAYOUT=
1409bc4a673fSDevin Teske		;;
1410cd88b886SDevin Teske	"- $msg_rescan_devices") f_device_rescan ;;
1411cd88b886SDevin Teske	"- $msg_disk_info") dialog_menu_diskinfo ;;
1412cd88b886SDevin Teske	?" $msg_pool_name")
1413cd88b886SDevin Teske		# Prompt the user to input/change the name for the new pool
1414cd88b886SDevin Teske		f_dialog_input input \
1415cd88b886SDevin Teske			"$msg_please_enter_a_name_for_your_zpool" \
1416cd88b886SDevin Teske			"$ZFSBOOT_POOL_NAME" &&
1417cd88b886SDevin Teske			ZFSBOOT_POOL_NAME="$input"
1418cd88b886SDevin Teske		;;
1419cd88b886SDevin Teske	?" $msg_force_4k_sectors")
1420cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1421cd88b886SDevin Teske		if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1422cd88b886SDevin Teske			ZFSBOOT_GNOP_4K_FORCE_ALIGN=
1423cd88b886SDevin Teske		else
1424cd88b886SDevin Teske			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1425cd88b886SDevin Teske		fi
1426cd88b886SDevin Teske		;;
1427bc4a673fSDevin Teske	?" $msg_encrypt_disks")
1428cd88b886SDevin Teske		# Toggle the variable referenced both by the menu and later
1429cd88b886SDevin Teske		if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1430cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=
1431cd88b886SDevin Teske		else
1432926ec73fSDevin Teske			ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1433cd88b886SDevin Teske			ZFSBOOT_GELI_ENCRYPTION=1
1434cd88b886SDevin Teske		fi
1435cd88b886SDevin Teske		;;
1436cd88b886SDevin Teske	?" $msg_partition_scheme")
1437cd88b886SDevin Teske		# Toggle between GPT and MBR
1438cd88b886SDevin Teske		if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
1439cd88b886SDevin Teske			ZFSBOOT_PARTITION_SCHEME=MBR
1440cd88b886SDevin Teske		else
1441cd88b886SDevin Teske			ZFSBOOT_PARTITION_SCHEME=GPT
1442cd88b886SDevin Teske		fi
1443cd88b886SDevin Teske		;;
1444cd88b886SDevin Teske	?" $msg_swap_size")
1445cd88b886SDevin Teske		# Prompt the user to input/change the swap size for each disk
1446cd88b886SDevin Teske		f_dialog_input input \
1447cd88b886SDevin Teske			"$msg_please_enter_amount_of_swap_space" \
1448cd88b886SDevin Teske			"$ZFSBOOT_SWAP_SIZE" &&
144930c8ebe9SDevin Teske			ZFSBOOT_SWAP_SIZE="${input:-0}"
1450cd88b886SDevin Teske		;;
1451cd88b886SDevin Teske	esac
1452cd88b886SDevin Teskedone
1453cd88b886SDevin Teske
1454cd88b886SDevin Teskereturn $SUCCESS
1455cd88b886SDevin Teske
1456cd88b886SDevin Teske################################################################################
1457cd88b886SDevin Teske# END
1458cd88b886SDevin Teske################################################################################
1459