]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bsdinstall/scripts/zfsboot
MFC r302145: bsdinstall: increase EFI partition size to 200MB
[FreeBSD/stable/10.git] / usr.sbin / bsdinstall / scripts / zfsboot
1 #!/bin/sh
2 #-
3 # Copyright (c) 2013-2015 Allan Jude
4 # Copyright (c) 2013-2015 Devin Teske
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29 #
30 ############################################################ INCLUDES
31
32 BSDCFG_SHARE="/usr/share/bsdconfig"
33 . $BSDCFG_SHARE/common.subr || exit 1
34 f_dprintf "%s: loading includes..." "$0"
35 f_include $BSDCFG_SHARE/device.subr
36 f_include $BSDCFG_SHARE/dialog.subr
37 f_include $BSDCFG_SHARE/password/password.subr
38 f_include $BSDCFG_SHARE/variable.subr
39
40 ############################################################ CONFIGURATION
41
42 #
43 # Default name of the boot-pool
44 #
45 : ${ZFSBOOT_POOL_NAME:=zroot}
46
47 #
48 # Default options to use when creating zroot pool
49 #
50 : ${ZFSBOOT_POOL_CREATE_OPTIONS:=-O compress=lz4 -O atime=off}
51
52 #
53 # Default name for the boot environment parent dataset
54 #
55 : ${ZFSBOOT_BEROOT_NAME:=ROOT}
56
57 #
58 # Default name for the primany boot environment
59 #
60 : ${ZFSBOOT_BOOTFS_NAME:=default}
61
62 #
63 # Default Virtual Device (vdev) type to create
64 #
65 : ${ZFSBOOT_VDEV_TYPE:=stripe}
66
67 #
68 # Should we use sysctl(8) vfs.zfs.min_auto_ashift=12 to force 4K sectors?
69 #
70 : ${ZFSBOOT_FORCE_4K_SECTORS:=1}
71
72 #
73 # Should we use geli(8) to encrypt the drives?
74 # NB: Automatically enables ZFSBOOT_BOOT_POOL
75 #
76 : ${ZFSBOOT_GELI_ENCRYPTION=}
77
78 #
79 # Default path to the geli(8) keyfile used in drive encryption
80 #
81 : ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
82
83 #
84 # Create a separate boot pool?
85 # NB: Automatically set when using geli(8) or MBR
86 #
87 : ${ZFSBOOT_BOOT_POOL=}
88
89 #
90 # Options to use when creating separate boot pool (if any)
91 #
92 : ${ZFSBOOT_BOOT_POOL_CREATE_OPTIONS:=}
93
94 #
95 # Default name for boot pool when enabled (e.g., geli(8) or MBR)
96 #
97 : ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
98
99 #
100 # Default size for boot pool when enabled (e.g., geli(8) or MBR)
101 #
102 : ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
103
104 #
105 # Default disks to use (always empty unless being scripted)
106 #
107 : ${ZFSBOOT_DISKS:=}
108
109 #
110 # Default partitioning scheme to use on disks
111 #
112 : ${ZFSBOOT_PARTITION_SCHEME:=}
113
114 #
115 # Default partitioning scheme to use on disks
116 #
117 : ${ZFSBOOT_BOOT_TYPE:=}
118
119 #
120 # How much swap to put on each block device in the boot zpool
121 # NOTE: Value passed to gpart(8); which supports SI unit suffixes.
122 #
123 : ${ZFSBOOT_SWAP_SIZE:=2g}
124
125 #
126 # Should we use geli(8) to encrypt the swap?
127 #
128 : ${ZFSBOOT_SWAP_ENCRYPTION=}
129
130 #
131 # Should we use gmirror(8) to mirror the swap?
132 #
133 : ${ZFSBOOT_SWAP_MIRROR=}
134
135 #
136 # Default ZFS datasets for root zpool
137 #
138 # NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
139 # NOTE: Anything after pound/hash character [#] is ignored as a comment.
140 #
141 f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
142         # DATASET       OPTIONS (comma or space separated; or both)
143
144         # Boot Environment [BE] root and default boot dataset
145         /$ZFSBOOT_BEROOT_NAME                           mountpoint=none
146         /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME      mountpoint=/
147
148         # Compress /tmp, allow exec but not setuid
149         /tmp            mountpoint=/tmp,exec=on,setuid=off
150
151         # Don't mount /usr so that 'base' files go to the BEROOT
152         /usr            mountpoint=/usr,canmount=off
153
154         # Home directories separated so they are common to all BEs
155         /usr/home       # NB: /home is a symlink to /usr/home
156
157         # Ports tree
158         /usr/ports      setuid=off
159
160         # Source tree (compressed)
161         /usr/src
162
163         # Create /var and friends
164         /var            mountpoint=/var,canmount=off
165         /var/audit      exec=off,setuid=off
166         /var/crash      exec=off,setuid=off
167         /var/log        exec=off,setuid=off
168         /var/mail       atime=on
169         /var/tmp        setuid=off
170 " # END-QUOTE
171
172 #
173 # If interactive and the user has not explicitly chosen a vdev type or disks,
174 # make the user confirm scripted/default choices when proceeding to install.
175 #
176 : ${ZFSBOOT_CONFIRM_LAYOUT:=1}
177
178 ############################################################ GLOBALS
179
180 #
181 # Format of a line in printf(1) syntax to add to fstab(5)
182 #
183 FSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
184
185 #
186 # Command strings for various tasks
187 #
188 CHMOD_MODE='chmod %s "%s"'
189 DD_WITH_OPTIONS='dd if="%s" of="%s" %s'
190 ECHO_APPEND='echo "%s" >> "%s"'
191 GELI_ATTACH='geli attach -j - -k "%s" "%s"'
192 GELI_DETACH_F='geli detach -f "%s"'
193 GELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
194 GPART_ADD_ALIGN='gpart add %s -t %s "%s"'
195 GPART_ADD_ALIGN_INDEX='gpart add %s -i %s -t %s "%s"'
196 GPART_ADD_ALIGN_INDEX_WITH_SIZE='gpart add %s -i %s -t %s -s %s "%s"'
197 GPART_ADD_ALIGN_LABEL='gpart add %s -l %s -t %s "%s"'
198 GPART_ADD_ALIGN_LABEL_WITH_SIZE='gpart add %s -l %s -t %s -s %s "%s"'
199 GPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
200 GPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
201 GPART_BOOTCODE_PARTONLY='gpart bootcode -p "%s" -i %s "%s"'
202 GPART_CREATE='gpart create -s %s "%s"'
203 GPART_DESTROY_F='gpart destroy -F "%s"'
204 GPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
205 GPART_SET_LENOVOFIX='gpart set -a lenovofix "%s"'
206 GPART_SET_PMBR_ACTIVE='gpart set -a active "%s"'
207 GRAID_DELETE='graid delete "%s"'
208 LN_SF='ln -sf "%s" "%s"'
209 MKDIR_P='mkdir -p "%s"'
210 MOUNT_TYPE='mount -t %s "%s" "%s"'
211 PRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
212 PRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
213 SHELL_TRUNCATE=':> "%s"'
214 SWAP_GMIRROR_LABEL='gmirror label swap %s'
215 SYSCTL_ZFS_MIN_ASHIFT_12='sysctl vfs.zfs.min_auto_ashift=12'
216 UMOUNT='umount "%s"'
217 ZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
218 ZFS_SET='zfs set "%s" "%s"'
219 ZFS_UNMOUNT='zfs unmount "%s"'
220 ZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
221 ZPOOL_DESTROY='zpool destroy "%s"'
222 ZPOOL_EXPORT='zpool export "%s"'
223 ZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
224 ZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
225 ZPOOL_SET='zpool set %s "%s"'
226
227 #
228 # Strings that should be moved to an i18n file and loaded with f_include_lang()
229 #
230 hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
231 hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
232 hline_arrows_tab_enter="Press arrows, TAB or ENTER"
233 msg_an_unknown_error_occurred="An unknown error occurred"
234 msg_back="Back"
235 msg_cancel="Cancel"
236 msg_change_selection="Change Selection"
237 msg_configure_options="Configure Options:"
238 msg_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"
239 msg_disk_info="Disk Info"
240 msg_disk_info_help="Get detailed information on disk device(s)"
241 msg_disk_singular="disk"
242 msg_disk_plural="disks"
243 msg_encrypt_disks="Encrypt Disks?"
244 msg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
245 msg_error="Error"
246 msg_force_4k_sectors="Force 4K Sectors?"
247 msg_force_4k_sectors_help="Align partitions to 4K sector boundries and set vfs.zfs.min_auto_ashift=12"
248 msg_freebsd_installer="FreeBSD Installer"
249 msg_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"
250 msg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
251 msg_install="Install"
252 msg_install_desc="Proceed with Installation"
253 msg_install_help="Create ZFS boot pool with displayed options"
254 msg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
255 msg_invalid_disk_argument="Invalid disk argument \`%s'"
256 msg_invalid_index_argument="Invalid index argument \`%s'"
257 msg_invalid_swap_size="Invalid swap size \`%s'"
258 msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
259 msg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy\nthe current contents of the following disks:\n\n   %s"
260 msg_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'
261 msg_mirror_desc="Mirror - n-Way Mirroring"
262 msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
263 msg_missing_disk_arguments="missing disk arguments"
264 msg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
265 msg_no="NO"
266 msg_no_disks_present_to_configure="No disk(s) present to configure"
267 msg_no_disks_selected="No disks selected."
268 msg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
269 msg_null_disk_argument="NULL disk argument"
270 msg_null_index_argument="NULL index argument"
271 msg_null_poolname="NULL poolname"
272 msg_ok="OK"
273 msg_partition_scheme="Partition Scheme"
274 msg_partition_scheme_help="Select partitioning scheme. GPT is recommended."
275 msg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
276 msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
277 msg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
278 msg_pool_name="Pool Name"
279 msg_pool_name_cannot_be_empty="Pool name cannot be empty."
280 msg_pool_name_help="Customize the name of the zpool to be created (Required)"
281 msg_pool_type_disks="Pool Type/Disks:"
282 msg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
283 msg_processing_selection="Processing selection..."
284 msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
285 msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
286 msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
287 msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
288 msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
289 msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
290 msg_rescan_devices="Rescan Devices"
291 msg_rescan_devices_help="Scan for device changes"
292 msg_select="Select"
293 msg_select_a_disk_device="Select a disk device"
294 msg_select_virtual_device_type="Select Virtual Device type:"
295 msg_stripe_desc="Stripe - No Redundancy"
296 msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
297 msg_swap_encrypt="Encrypt Swap?"
298 msg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
299 msg_swap_invalid="The selected swap size (%s) is invalid. Enter a number optionally followed by units. Example: 2G"
300 msg_swap_mirror="Mirror Swap?"
301 msg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
302 msg_swap_size="Swap Size"
303 msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
304 msg_swap_toosmall="The selected swap size (%s) is to small. Please enter a value greater than 100MB or enter 0 for no swap"
305 msg_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."
306 msg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
307 msg_unsupported_partition_scheme="%s is an unsupported partition scheme"
308 msg_user_cancelled="User Cancelled."
309 msg_yes="YES"
310 msg_zfs_configuration="ZFS Configuration"
311
312 ############################################################ FUNCTIONS
313
314 # dialog_menu_main
315 #
316 # Display the dialog(1)-based application main menu.
317 #
318 dialog_menu_main()
319 {
320         local title="$DIALOG_TITLE"
321         local btitle="$DIALOG_BACKTITLE"
322         local prompt="$msg_configure_options"
323         local force4k="$msg_no"
324         local usegeli="$msg_no"
325         local swapgeli="$msg_no"
326         local swapmirror="$msg_no"
327         [ "$ZFSBOOT_FORCE_4K_SECTORS" ] && force4k="$msg_yes"
328         [ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
329         [ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
330         [ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
331         local disks n disks_grammar
332         f_count n $ZFSBOOT_DISKS
333         { [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
334                 disks_grammar=$msg_disk_plural # grammar
335         local menu_list="
336                 '>>> $msg_install'      '$msg_install_desc'
337                                         '$msg_install_help'
338                 'T $msg_pool_type_disks'
339                                         '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
340                                         '$msg_pool_type_disks_help'
341                 '- $msg_rescan_devices' '*'
342                                         '$msg_rescan_devices_help'
343                 '- $msg_disk_info'      '*'
344                                         '$msg_disk_info_help'
345                 'N $msg_pool_name'      '$ZFSBOOT_POOL_NAME'
346                                         '$msg_pool_name_help'
347                 '4 $msg_force_4k_sectors'
348                                         '$force4k'
349                                         '$msg_force_4k_sectors_help'
350                 'E $msg_encrypt_disks'  '$usegeli'
351                                         '$msg_encrypt_disks_help'
352                 'P $msg_partition_scheme'
353                                         '$ZFSBOOT_PARTITION_SCHEME ($ZFSBOOT_BOOT_TYPE)'
354                                         '$msg_partition_scheme_help'
355                 'S $msg_swap_size'      '$ZFSBOOT_SWAP_SIZE'
356                                         '$msg_swap_size_help'
357                 'M $msg_swap_mirror'    '$swapmirror'
358                                         '$msg_swap_mirror_help'
359                 'W $msg_swap_encrypt'   '$swapgeli'
360                                         '$msg_swap_encrypt_help'
361         " # END-QUOTE
362         local defaultitem= # Calculated below
363         local hline="$hline_alnum_arrows_punc_tab_enter"
364
365         local height width rows
366         eval f_dialog_menu_with_help_size height width rows \
367                 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
368
369         # Obtain default-item from previously stored selection
370         f_dialog_default_fetch defaultitem
371
372         local menu_choice
373         menu_choice=$( eval $DIALOG \
374                 --title \"\$title\"              \
375                 --backtitle \"\$btitle\"         \
376                 --hline \"\$hline\"              \
377                 --item-help                      \
378                 --ok-label \"\$msg_select\"      \
379                 --cancel-label \"\$msg_cancel\"  \
380                 --default-item \"\$defaultitem\" \
381                 --menu \"\$prompt\"              \
382                 $height $width $rows             \
383                 $menu_list                       \
384                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
385         )
386         local retval=$?
387         f_dialog_data_sanitize menu_choice
388         f_dialog_menutag_store "$menu_choice"
389
390         # Only update default-item on success
391         [ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
392
393         return $retval
394 }
395
396 # dialog_last_chance $disks ...
397 #
398 # Display a list of the disks that the user is about to destroy. The default
399 # action is to return error status unless the user explicitly (non-default)
400 # selects "Yes" from the noyes dialog.
401 #
402 dialog_last_chance()
403 {
404         local title="$DIALOG_TITLE"
405         local btitle="$DIALOG_BACKTITLE"
406         local prompt # Calculated below
407         local hline="$hline_arrows_tab_enter"
408
409         local height=8 width=50 prefix="   "
410         local plen=${#prefix} list= line=
411         local max_width=$(( $width - 3 - $plen ))
412
413         local yes no defaultno extra_args format
414         if [ "$USE_XDIALOG" ]; then
415                 yes=ok no=cancel defaultno=default-no
416                 extra_args="--wrap --left"
417                 format="$msg_last_chance_are_you_sure"
418         else
419                 yes=yes no=no defaultno=defaultno
420                 extra_args="--colors --cr-wrap"
421                 format="$msg_last_chance_are_you_sure_color"
422         fi
423
424         local disk line_width
425         for disk in $*; do
426                 if [ "$line" ]; then
427                         line_width=${#line}
428                 else
429                         line_width=$plen
430                 fi
431                 line_width=$(( $line_width + 1 + ${#disk} ))
432                 # Add newline before disk if it would exceed max_width
433                 if [ $line_width -gt $max_width ]; then
434                         list="$list$line\n"
435                         line="$prefix"
436                         height=$(( $height + 1 ))
437                 fi
438                 # Add the disk to the list
439                 line="$line $disk"
440         done
441         # Append the left-overs
442         if [ "${line#$prefix}" ]; then
443                 list="$list$line"
444                 height=$(( $height + 1 ))
445         fi
446
447         # Add height for Xdialog(1)
448         [ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
449
450         prompt=$( printf "$format" "$list" )
451         f_dprintf "%s: Last Chance!" "$0"
452         $DIALOG \
453                 --title "$title"        \
454                 --backtitle "$btitle"   \
455                 --hline "$hline"        \
456                 --$defaultno            \
457                 --$yes-label "$msg_yes" \
458                 --$no-label "$msg_no"   \
459                 $extra_args             \
460                 --yesno "$prompt" $height $width
461 }
462
463 # dialog_menu_layout
464 #
465 # Configure Virtual Device type and disks to use for the ZFS boot pool. User
466 # must select enough disks to satisfy the chosen vdev type.
467 #
468 dialog_menu_layout()
469 {
470         local funcname=dialog_menu_layout
471         local title="$DIALOG_TITLE"
472         local btitle="$DIALOG_BACKTITLE"
473         local vdev_prompt="$msg_select_virtual_device_type"
474         local disk_prompt="$msg_please_select_one_or_more_disks"
475         local vdev_menu_list="
476                 'stripe' '$msg_stripe_desc' '$msg_stripe_help'
477                 'mirror' '$msg_mirror_desc' '$msg_mirror_help'
478                 'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
479                 'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
480                 'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
481         " # END-QUOTE
482         local disk_check_list= # Calculated below
483         local vdev_hline="$hline_arrows_tab_enter"
484         local disk_hline="$hline_arrows_space_tab_enter"
485
486         # Warn the user if vdev type is not valid
487         case "$ZFSBOOT_VDEV_TYPE" in
488         stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
489         *)
490                 f_dprintf "%s: Invalid virtual device type \`%s'" \
491                           $funcname "$ZFSBOOT_VDEV_TYPE"
492                 f_show_err "$msg_invalid_virtual_device_type" \
493                            "$ZFSBOOT_VDEV_TYPE"
494                 f_interactive || return $FAILURE
495         esac
496
497         # Calculate size of vdev menu once only
498         local vheight vwidth vrows
499         eval f_dialog_menu_with_help_size vheight vwidth vrows \
500                 \"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
501                 $vdev_menu_list
502
503         # Get a list of probed disk devices
504         local disks=
505         debug= f_device_find "" $DEVICE_TYPE_DISK disks
506
507         # Prune out mounted md(4) devices that may be part of the boot process
508         local disk name new_list=
509         for disk in $disks; do
510                 debug= $disk get name name
511                 case "$name" in
512                 md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
513                 esac
514                 new_list="$new_list $disk"
515         done
516         disks="${new_list# }"
517
518         # Debugging
519         if [ "$debug" ]; then
520                 local disk_names=
521                 for disk in $disks; do
522                         debug= $disk get name name
523                         disk_names="$disk_names $name"
524                 done
525                 f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
526         fi
527
528         if [ ! "$disks" ]; then
529                 f_dprintf "No disk(s) present to configure"
530                 f_show_err "$msg_no_disks_present_to_configure"
531                 return $FAILURE
532         fi
533
534         # Lets sort the disks array to be more user friendly
535         f_device_sort_by name disks disks
536
537         #
538         # Operate in a loop so we can (if interactive) repeat if not enough
539         # disks are selected to satisfy the chosen vdev type or user wants to
540         # back-up to the previous menu.
541         #
542         local vardisk ndisks onoff selections vdev_choice breakout device
543         local valid_disks all_valid want_disks desc height width rows
544         while :; do
545                 #
546                 # Confirm the vdev type that was selected
547                 #
548                 if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
549                         vdev_choice=$( eval $DIALOG \
550                                 --title \"\$title\"              \
551                                 --backtitle \"\$btitle\"         \
552                                 --hline \"\$vdev_hline\"         \
553                                 --ok-label \"\$msg_ok\"          \
554                                 --cancel-label \"\$msg_cancel\"  \
555                                 --item-help                      \
556                                 --default-item \"\$ZFSBOOT_VDEV_TYPE\" \
557                                 --menu \"\$vdev_prompt\"         \
558                                 $vheight $vwidth $vrows          \
559                                 $vdev_menu_list                  \
560                                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
561                         ) || return $?
562                                 # Exit if user pressed ESC or chose Cancel/No
563                         f_dialog_data_sanitize vdev_choice
564
565                         ZFSBOOT_VDEV_TYPE="$vdev_choice"
566                         f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
567                                   "$ZFSBOOT_VDEV_TYPE"
568                 fi
569
570                 # Determine the number of disks needed for this vdev type
571                 want_disks=0
572                 case "$ZFSBOOT_VDEV_TYPE" in
573                 stripe) want_disks=1 ;;
574                 mirror) want_disks=2 ;;
575                 raidz1) want_disks=3 ;;
576                 raidz2) want_disks=4 ;;
577                 raidz3) want_disks=5 ;;
578                 esac
579
580                 #
581                 # Warn the user if any scripted disks are invalid
582                 #
583                 valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
584                 for disk in $ZFSBOOT_DISKS; do
585                         if debug= f_device_find -1 \
586                                 $disk $DEVICE_TYPE_DISK device
587                         then
588                                 valid_disks="$valid_disks $disk"
589                                 continue
590                         fi
591                         f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
592                         all_valid=
593                 done
594                 if [ ! "$all_valid" ]; then
595                         if [ "$ZFSBOOT_DISKS" ]; then
596                                 f_show_err \
597                                     "$msg_missing_one_or_more_scripted_disks"
598                         else
599                                 f_dprintf "No disks selected."
600                                 f_interactive ||
601                                         f_show_err "$msg_no_disks_selected"
602                         fi
603                         f_interactive || return $FAILURE
604                 fi
605                 ZFSBOOT_DISKS="${valid_disks# }"
606
607                 #
608                 # Short-circuit if we're running non-interactively
609                 #
610                 if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
611                         f_count ndisks $ZFSBOOT_DISKS
612                         [ $ndisks -ge $want_disks ] && break # to success
613
614                         # Not enough disks selected
615                         f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
616                                   "$ZFSBOOT_VDEV_TYPE" \
617                                   "Not enough disks selected." \
618                                   $ndisks $want_disks
619                         f_interactive || return $FAILURE
620                         msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
621                                 f_yesno "%s: $msg_not_enough_disks_selected" \
622                                 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
623                                 return $FAILURE
624                 fi
625
626                 #
627                 # Confirm the disks that were selected
628                 # Loop until the user cancels or selects enough disks
629                 #
630                 breakout=
631                 while :; do
632                         # Loop over list of available disks, resetting state
633                         for disk in $disks; do
634                                 f_isset _${disk}_status && _${disk}_status=
635                         done
636
637                         # Loop over list of selected disks and create temporary
638                         # locals to map statuses onto up-to-date list of disks
639                         for disk in $ZFSBOOT_DISKS; do
640                                 debug= f_device_find -1 \
641                                         $disk $DEVICE_TYPE_DISK disk
642                                 f_isset _${disk}_status ||
643                                         local _${disk}_status
644                                 _${disk}_status=on
645                         done
646
647                         # Create the checklist menu of discovered disk devices
648                         disk_check_list=
649                         for disk in $disks; do
650                                 desc=
651                                 $disk get name name
652                                 $disk get desc desc
653                                 f_shell_escape "$desc" desc
654                                 f_getvar _${disk}_status:-off onoff
655                                 disk_check_list="$disk_check_list
656                                         $name '$desc' $onoff"
657                         done
658
659                         eval f_dialog_checklist_size height width rows \
660                                 \"\$title\" \"\$btitle\" \"\$prompt\" \
661                                 \"\$hline\" $disk_check_list
662
663                         selections=$( eval $DIALOG \
664                                 --title \"\$DIALOG_TITLE\"         \
665                                 --backtitle \"\$DIALOG_BACKTITLE\" \
666                                 --separate-output                  \
667                                 --hline \"\$hline\"                \
668                                 --ok-label \"\$msg_ok\"            \
669                                 --cancel-label \"\$msg_back\"      \
670                                 --checklist \"\$prompt\"           \
671                                 $height $width $rows               \
672                                 $disk_check_list                   \
673                                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
674                         ) || break
675                                 # Loop if user pressed ESC or chose Cancel/No
676                         f_dialog_data_sanitize selections
677
678                         ZFSBOOT_DISKS="$selections"
679                         f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
680                                   "$ZFSBOOT_DISKS"
681
682                         f_count ndisks $ZFSBOOT_DISKS
683                         [ $ndisks -ge $want_disks ] &&
684                                 breakout=break && break
685
686                         # Not enough disks selected
687                         f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
688                                   "$ZFSBOOT_VDEV_TYPE" \
689                                   "Not enough disks selected." \
690                                   $ndisks $want_disks
691                         msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
692                                 f_yesno "%s: $msg_not_enough_disks_selected" \
693                                 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
694                                 break
695                 done
696                 [ "$breakout" = "break" ] && break
697                 [ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
698         done
699
700         return $DIALOG_OK
701 }
702
703 # zfs_create_diskpart $disk $index
704 #
705 # For each block device to be used in the zpool, rather than just create the
706 # zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
707 # so we can have some real swap. This also provides wiggle room incase your
708 # replacement drivers do not have the exact same sector counts.
709 #
710 # NOTE: $swapsize and $bootsize should be defined by the calling function.
711 # NOTE: Sets $bootpart and $targetpart for the calling function.
712 #
713 zfs_create_diskpart()
714 {
715         local funcname=zfs_create_diskpart
716         local disk="$1" index="$2"
717
718         # Check arguments
719         if [ ! "$disk" ]; then
720                 f_dprintf "$funcname: NULL disk argument"
721                 msg_error="$msg_error: $funcname" \
722                         f_show_err "$msg_null_disk_argument"
723                 return $FAILURE
724         fi
725         if [ "${disk#*[$IFS]}" != "$disk" ]; then
726                 f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
727                 msg_error="$msg_error: $funcname" \
728                         f_show_err "$msg_invalid_disk_argument" "$disk"
729                 return $FAILURE
730         fi
731         if [ ! "$index" ]; then
732                 f_dprintf "$funcname: NULL index argument"
733                 msg_error="$msg_error: $funcname" \
734                         f_show_err "$msg_null_index_argument"
735                 return $FAILURE
736         fi
737         if ! f_isinteger "$index"; then
738                 f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
739                 msg_error="$msg_error: $funcname" \
740                         f_show_err "$msg_invalid_index_argument" "$index"
741                 return $FAILURE
742         fi
743         f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
744
745         # Check for unknown partition scheme before proceeding further
746         case "$ZFSBOOT_PARTITION_SCHEME" in
747         ""|MBR|GPT*) : known good ;;
748         *)
749                 f_dprintf "$funcname: %s is an unsupported partition scheme" \
750                           "$ZFSBOOT_PARTITION_SCHEME"
751                 msg_error="$msg_error: $funcname" f_show_err \
752                         "$msg_unsupported_partition_scheme" \
753                         "$ZFSBOOT_PARTITION_SCHEME"
754                 return $FAILURE
755         esac
756
757         #
758         # Destroy whatever partition layout is currently on disk.
759         # NOTE: `-F' required to destroy if partitions still exist.
760         # NOTE: Failure is ok here, blank disk will have nothing to destroy.
761         #
762         f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
763         f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
764         f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
765         f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
766
767         # Make doubly-sure backup GPT is destroyed
768         f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
769         f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
770
771         #
772         # Enable boot pool if encryption is desired
773         #
774         [ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
775
776         #
777         # Lay down the desired type of partition scheme
778         #
779         local setsize mbrindex align_small align_big
780         #
781         # If user has requested 4 K alignment, add these params to the
782         # gpart add calls. With GPT, we align large partitions to 1 M for
783         # improved performance on SSDs. MBR does not always play well with gaps
784         # between partitions, so all alignment is only 4k for that case.
785         # With MBR, we align the BSD partition that contains the MBR, otherwise
786         # the system fails to boot.
787         #
788         if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
789                 align_small="-a 4k"
790                 align_big="-a 1m"
791                 sysctl kern.geom.part.mbr.enforce_chs=0
792         fi
793
794         case "$ZFSBOOT_PARTITION_SCHEME" in
795         ""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
796                 #
797                 # 1. Create GPT layout using labels
798                 #
799                 f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
800                              return $FAILURE
801
802                 #
803                 # Apply workarounds if requested by the user
804                 #
805                 if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Lenovo Fix" ]; then
806                         f_eval_catch $funcname gpart "$GPART_SET_LENOVOFIX" \
807                                      $disk || return $FAILURE
808                 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
809                         f_eval_catch $funcname gpart "$GPART_SET_PMBR_ACTIVE" \
810                                      $disk || return $FAILURE
811                 fi
812
813                 #
814                 # 2. Add small freebsd-boot or efi partition
815                 #
816                 if [ "$ZFSBOOT_BOOT_TYPE" = "UEFI" ]; then
817                         f_eval_catch $funcname gpart \
818                                      "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
819                                      "$align_small" efiboot$index efi 200M $disk ||
820                                      return $FAILURE
821                         f_eval_catch $funcname gpart "$GPART_BOOTCODE_PARTONLY" \
822                                      /boot/boot1.efifat 1 $disk ||
823                                      return $FAILURE
824                 else
825                         f_eval_catch $funcname gpart \
826                                      "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
827                                      "$align_small" gptboot$index freebsd-boot \
828                                      512k $disk || return $FAILURE
829                         f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
830                                      /boot/pmbr /boot/gptzfsboot 1 $disk ||
831                                      return $FAILURE
832                 fi
833
834                 # NB: zpool will use the `zfs#' GPT labels
835                 bootpart=p2 swappart=p2 targetpart=p2
836                 [ ${swapsize:-0} -gt 0 ] && targetpart=p3
837
838                 #
839                 # Prepare boot pool if enabled (e.g., for geli(8))
840                 #
841                 if [ "$ZFSBOOT_BOOT_POOL" ]; then
842                         bootpart=p2 swappart=p3 targetpart=p3
843                         [ ${swapsize:-0} -gt 0 ] && targetpart=p4
844                         f_eval_catch $funcname gpart \
845                                      "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
846                                      "$align_big" boot$index freebsd-zfs \
847                                      ${bootsize}b $disk ||
848                                      return $FAILURE
849                         # Pedantically nuke any old labels
850                         f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
851                                         /dev/$disk$bootpart
852                         if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
853                                 # Pedantically detach targetpart for later
854                                 f_eval_catch -d $funcname geli \
855                                                 "$GELI_DETACH_F" \
856                                                 /dev/$disk$targetpart
857                         fi
858                 fi
859
860                 #
861                 # 3. Add freebsd-swap partition labeled `swap#'
862                 #
863                 if [ ${swapsize:-0} -gt 0 ]; then
864                         f_eval_catch $funcname gpart \
865                                      "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
866                                      "$align_big" swap$index freebsd-swap \
867                                      ${swapsize}b $disk ||
868                                      return $FAILURE
869                         # Pedantically nuke any old labels on the swap
870                         f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
871                                         /dev/$disk$swappart
872                 fi
873
874                 #
875                 # 4. Add freebsd-zfs partition labeled `zfs#' for zroot
876                 #
877                 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL" \
878                              "$align_big" zfs$index freebsd-zfs $disk ||
879                              return $FAILURE
880                 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
881                                 /dev/$disk$targetpart
882                 ;;
883
884         MBR) f_dprintf "$funcname: Creating MBR layout..."
885                 #
886                 # 1. Create MBR layout (no labels)
887                 #
888                 f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
889                              return $FAILURE
890                 f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
891                              $disk || return $FAILURE
892
893                 #
894                 # 2. Add freebsd slice with all available space
895                 #
896                 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN" "$align_small" \
897                              freebsd $disk ||
898                              return $FAILURE
899                 f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
900                              return $FAILURE
901                 # Pedantically nuke any old labels
902                 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
903                                 /dev/${disk}s1
904                 # Pedantically nuke any old scheme
905                 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
906
907                 #
908                 # 3. Write BSD scheme to the freebsd slice
909                 #
910                 f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
911                              return $FAILURE
912
913                 # NB: zpool will use s1a (no labels)
914                 bootpart=s1a swappart=s1b targetpart=s1d mbrindex=4
915
916                 #
917                 # Always prepare a boot pool on MBR
918                 # Do not align this partition, there must not be a gap
919                 #
920                 ZFSBOOT_BOOT_POOL=1
921                 f_eval_catch $funcname gpart \
922                              "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
923                              "" 1 freebsd-zfs ${bootsize}b ${disk}s1 ||
924                              return $FAILURE
925                 # Pedantically nuke any old labels
926                 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
927                                 /dev/$disk$bootpart
928                 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
929                         # Pedantically detach targetpart for later
930                         f_eval_catch -d $funcname geli \
931                                         "$GELI_DETACH_F" \
932                                         /dev/$disk$targetpart
933                 fi
934
935                 #
936                 # 4. Add freebsd-swap partition
937                 #
938                 if [ ${swapsize:-0} -gt 0 ]; then
939                         f_eval_catch $funcname gpart \
940                                      "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
941                                      "$align_small" 2 freebsd-swap ${swapsize}b ${disk}s1 ||
942                                      return $FAILURE
943                         # Pedantically nuke any old labels on the swap
944                         f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
945                                         /dev/${disk}s1b
946                 fi
947
948                 #
949                 # 5. Add freebsd-zfs partition for zroot
950                 #
951                 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_INDEX" \
952                              "$align_small" $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
953                 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
954                                 /dev/$disk$targetpart # Pedantic
955                 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
956                              /boot/zfsboot /dev/${disk}s1 count=1 ||
957                              return $FAILURE
958                 ;;
959
960         esac # $ZFSBOOT_PARTITION_SCHEME
961
962         # Update fstab(5)
963         local swapsize
964         f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
965         if [ "$isswapmirror" ]; then
966                 # This is not the first disk in the mirror, do nothing
967         elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
968                 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
969                              /dev/mirror/swap.eli none swap sw 0 0 \
970                              $BSDINSTALL_TMPETC/fstab ||
971                              return $FAILURE
972                 isswapmirror=1
973         elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
974                 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
975                              /dev/mirror/swap none swap sw 0 0 \
976                              $BSDINSTALL_TMPETC/fstab ||
977                              return $FAILURE
978                 isswapmirror=1
979         elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
980                 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
981                              /dev/$disk${swappart}.eli none swap sw 0 0 \
982                              $BSDINSTALL_TMPETC/fstab ||
983                              return $FAILURE
984         elif [ ${swapsize:-0} -eq 0 ]; then
985                 # If swap is 0 sized, don't add it to fstab
986         else
987                 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
988                              /dev/$disk$swappart none swap sw 0 0 \
989                              $BSDINSTALL_TMPETC/fstab ||
990                              return $FAILURE
991         fi
992
993         return $SUCCESS
994 }
995
996 # zfs_create_boot $poolname $vdev_type $disks ...
997 #
998 # Creates boot pool and dataset layout. Returns error if something goes wrong.
999 # Errors are printed to stderr for collection and display.
1000 #
1001 zfs_create_boot()
1002 {
1003         local funcname=zfs_create_boot
1004         local zroot_name="$1"
1005         local zroot_vdevtype="$2"
1006         local zroot_vdevs= # Calculated below
1007         local swap_devs= # Calculated below
1008         local boot_vdevs= # Used for geli(8) and/or MBR layouts
1009         shift 2 # poolname vdev_type
1010         local disks="$*" disk
1011         local isswapmirror
1012         local bootpart targetpart swappart # Set by zfs_create_diskpart() below
1013         local create_options
1014
1015         #
1016         # Pedantic checks; should never be seen
1017         #
1018         if [ ! "$zroot_name" ]; then
1019                 f_dprintf "$funcname: NULL poolname"
1020                 msg_error="$msg_error: $funcname" \
1021                         f_show_err "$msg_null_poolname"
1022                 return $FAILURE
1023         fi
1024         if [ $# -lt 1 ]; then
1025                 f_dprintf "$funcname: missing disk arguments"
1026                 msg_error="$msg_error: $funcname" \
1027                         f_show_err "$msg_missing_disk_arguments"
1028                 return $FAILURE
1029         fi
1030         f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
1031                   "$zroot_name" "$zroot_vdevtype"
1032
1033         #
1034         # Initialize fstab(5)
1035         #
1036         f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1037         f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1038                      return $FAILURE
1039         f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1040                      "# Device" Mountpoint FStype Options Dump "Pass#" \
1041                      $BSDINSTALL_TMPETC/fstab || return $FAILURE
1042
1043         #
1044         # Expand SI units in desired sizes
1045         #
1046         f_dprintf "$funcname: Expanding supplied size values..."
1047         local swapsize bootsize
1048         if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1049                 f_dprintf "$funcname: Invalid swap size \`%s'" \
1050                           "$ZFSBOOT_SWAP_SIZE"
1051                 f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1052                 return $FAILURE
1053         fi
1054         if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
1055                 f_dprintf "$funcname: Invalid boot pool size \`%s'" \
1056                           "$ZFSBOOT_BOOT_POOL_SIZE"
1057                 f_show_err "$msg_invalid_boot_pool_size" \
1058                            "$ZFSBOOT_BOOT_POOL_SIZE"
1059                 return $FAILURE
1060         fi
1061         f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1062                   "$ZFSBOOT_SWAP_SIZE" "$swapsize"
1063         f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
1064                   "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1065
1066         #
1067         # Destroy the pool in-case this is our second time 'round (case of
1068         # failure and installer presented ``Retry'' option to come back).
1069         #
1070         # NB: If we don't destroy the pool, later gpart(8) destroy commands
1071         # that try to clear existing partitions (see zfs_create_diskpart())
1072         # will fail with a `Device Busy' error, leading to `GEOM exists'.
1073         #
1074         f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1075
1076         #
1077         # Prepare the disks and build pool device list(s)
1078         #
1079         f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1080
1081         # Force 4K sectors using vfs.zfs.min_auto_ashift=12
1082         if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1083                 f_dprintf "$funcname: With 4K sectors..."
1084                 f_eval_catch $funcname sysctl "$SYSCTL_ZFS_MIN_ASHIFT_12" \
1085                     || return $FAILURE
1086         fi
1087         local n=0
1088         for disk in $disks; do
1089                 zfs_create_diskpart $disk $n || return $FAILURE
1090                 # Now $bootpart, $targetpart, and $swappart are set (suffix
1091                 # for $disk)
1092                 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1093                         boot_vdevs="$boot_vdevs $disk$bootpart"
1094                 fi
1095                 zroot_vdevs="$zroot_vdevs $disk$targetpart"
1096                 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1097                         zroot_vdevs="$zroot_vdevs.eli"
1098                 fi
1099
1100                 n=$(( $n + 1 ))
1101         done # disks
1102
1103         #
1104         # If we need/want a boot pool, create it
1105         #
1106         if [ "$ZFSBOOT_BOOT_POOL" ]; then
1107                 local bootpool_vdevtype= # Calculated below
1108                 local bootpool_options= # Calculated below
1109                 local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
1110                 local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
1111                 local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
1112
1113                 f_dprintf "$funcname: Setting up boot pool..."
1114                 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
1115                         f_dprintf "$funcname: For encrypted root disk..."
1116
1117                 # Create parent directory for boot pool
1118                 f_eval_catch -d $funcname umount "$UMOUNT" /mnt
1119                 f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1120                              $BSDINSTALL_CHROOT || return $FAILURE
1121
1122                 # Create mirror across the boot partition on all disks
1123                 local nvdevs
1124                 f_count nvdevs $boot_vdevs
1125                 [ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1126
1127                 create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
1128                 bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
1129                 bootpool_options="$bootpool_options $create_options"
1130                 bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1131                 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1132                              "$bootpool_options" "$bootpool_name" \
1133                              "$bootpool_vdevtype" "$boot_vdevs" ||
1134                              return $FAILURE
1135
1136                 f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1137                              return $FAILURE
1138
1139                 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1140                         # Generate an encryption key using random(4)
1141                         f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1142                                      /dev/random "$bootpool/$zroot_key" \
1143                                      "bs=4096 count=1" || return $FAILURE
1144                         f_eval_catch $funcname chmod "$CHMOD_MODE" \
1145                                      go-wrx "$bootpool/$zroot_key" ||
1146                                      return $FAILURE
1147                 else
1148                         # Clean up
1149                         f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
1150                                      "$bootpool_name" || return $FAILURE
1151                         f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1152                 fi
1153
1154         fi
1155
1156         #
1157         # Create the geli(8) GEOMS
1158         #
1159         if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1160                 # Prompt user for password (twice)
1161                 if ! msg_enter_new_password="$msg_geli_password" \
1162                         f_dialog_input_password
1163                 then
1164                         f_dprintf "$funcname: User cancelled"
1165                         f_show_err "$msg_user_cancelled"
1166                         return $FAILURE
1167                 fi
1168
1169                 # Initialize geli(8) on each of the target partitions
1170                 for disk in $disks; do
1171                         f_dialog_info "$msg_geli_setup" \
1172                                 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1173                         if ! echo "$pw_password" | f_eval_catch \
1174                                 $funcname geli "$GELI_PASSWORD_INIT" \
1175                                 "$bootpool/boot/$disk$targetpart.eli" \
1176                                 AES-XTS "$bootpool/$zroot_key" \
1177                                 $disk$targetpart
1178                         then
1179                                 f_interactive || f_die
1180                                 unset pw_password # Sensitive info
1181                                 return $FAILURE
1182                         fi
1183                         if ! echo "$pw_password" | f_eval_catch \
1184                                 $funcname geli "$GELI_ATTACH" \
1185                                 "$bootpool/$zroot_key" $disk$targetpart
1186                         then
1187                                 f_interactive || f_die
1188                                 unset pw_password # Sensitive info
1189                                 return $FAILURE
1190                         fi
1191                 done
1192                 unset pw_password # Sensitive info
1193
1194                 # Clean up
1195                 f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
1196                         return $FAILURE
1197                 f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1198         fi
1199
1200         #
1201         # Create the gmirror(8) GEOMS for swap
1202         #
1203         if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1204                 for disk in $disks; do
1205                         swap_devs="$swap_devs $disk$swappart"
1206                 done
1207                 f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
1208                         "$swap_devs" || return $FAILURE
1209         fi
1210
1211         #
1212         # Create the ZFS root pool with desired type and disk devices
1213         #
1214         f_dprintf "$funcname: Creating root pool..."
1215         create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1216         f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1217                 "-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1218                 "$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
1219                 return $FAILURE
1220
1221         #
1222         # Create ZFS dataset layout within the new root pool
1223         #
1224         f_dprintf "$funcname: Creating ZFS datasets..."
1225         echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1226                 # Skip blank lines and comments
1227                 case "$dataset" in "#"*|"") continue; esac
1228                 # Remove potential inline comments in options
1229                 options="${options%%#*}"
1230                 # Replace tabs with spaces
1231                 f_replaceall "$options" "       " " " options
1232                 # Reduce contiguous runs of space to one single space
1233                 oldoptions=
1234                 while [ "$oldoptions" != "$options" ]; do
1235                         oldoptions="$options"
1236                         f_replaceall "$options" "  " " " options
1237                 done
1238                 # Replace both commas and spaces with ` -o '
1239                 f_replaceall "$options" "[ ,]" " -o " options
1240                 # Create the dataset with desired options
1241                 f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
1242                              "${options:+-o $options}" "$zroot_name$dataset" ||
1243                              return $FAILURE
1244         done
1245
1246         #
1247         # Set a mountpoint for the root of the pool so newly created datasets
1248         # have a mountpoint to inherit
1249         #
1250         f_dprintf "$funcname: Setting mountpoint for root of the pool..."
1251         f_eval_catch $funcname zfs "$ZFS_SET" \
1252                 "mountpoint=/$zroot_name" "$zroot_name" ||
1253                 return $FAILURE
1254
1255         # Touch up permissions on the tmp directories
1256         f_dprintf "$funcname: Modifying directory permissions..."
1257         local dir
1258         for dir in /tmp /var/tmp; do
1259                 f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1260                              $BSDINSTALL_CHROOT$dir || return $FAILURE
1261         done
1262
1263         # Create symlink(s)
1264         if [ "$ZFSBOOT_BOOT_POOL" ]; then
1265                 f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1266                 f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1267                              $BSDINSTALL_CHROOT/boot || return $FAILURE
1268         fi
1269
1270         # Set bootfs property
1271         local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1272         f_dprintf "$funcname: Setting bootfs property..."
1273         f_eval_catch $funcname zpool "$ZPOOL_SET" \
1274                 "bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
1275                 return $FAILURE
1276
1277         # Export the pool(s)
1278         f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
1279         f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1280                      return $FAILURE
1281         if [ "$ZFSBOOT_BOOT_POOL" ]; then
1282                 f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
1283                              "$bootpool_name" || return $FAILURE
1284         fi
1285
1286         # MBR boot loader touch-up
1287         if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1288                 f_dprintf "$funcname: Updating MBR boot loader on disks..."
1289                 # Stick the ZFS boot loader in the "convienient hole" after
1290                 # the ZFS internal metadata
1291                 for disk in $disks; do
1292                         f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1293                                      /boot/zfsboot /dev/$disk$bootpart \
1294                                      "skip=1 seek=1024" || return $FAILURE
1295                 done
1296         fi
1297
1298         # Re-import the ZFS pool(s)
1299         f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1300         f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1301                      "-o altroot=\"$BSDINSTALL_CHROOT\"" "$zroot_name" ||
1302                      return $FAILURE
1303         if [ "$ZFSBOOT_BOOT_POOL" ]; then
1304                 f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1305                              "-o altroot=\"$BSDINSTALL_CHROOT\"" \
1306                              "$bootpool_name" || return $FAILURE
1307         fi
1308
1309         # While this is apparently not needed, it seems to help MBR
1310         f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1311         f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1312                      return $FAILURE
1313         f_eval_catch $funcname zpool "$ZPOOL_SET" \
1314                      "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1315                      "$zroot_name" || return $FAILURE
1316
1317         # Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1318         # NOTE: We later concatenate these into their destination
1319         f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1320                   "$funcname"
1321         f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1322                      $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1323         f_eval_catch $funcname echo "$ECHO_APPEND" \
1324                      'kern.geom.label.gptid.enable=\"0\"' \
1325                      $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1326
1327         if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1328                 f_eval_catch $funcname echo "$ECHO_APPEND" \
1329                              'geom_mirror_load=\"YES\"' \
1330                              $BSDINSTALL_TMPBOOT/loader.conf.gmirror ||
1331                              return $FAILURE
1332         fi
1333
1334         # We're all done unless we should go on for boot pool
1335         [ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1336
1337         # Set cachefile for boot pool so it auto-imports at system start
1338         f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1339         f_eval_catch $funcname zpool "$ZPOOL_SET" \
1340                      "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1341                      "$bootpool_name" || return $FAILURE
1342
1343         # Some additional geli(8) requirements for loader.conf(5)
1344         for option in \
1345                 'zpool_cache_load=\"YES\"' \
1346                 'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1347                 'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1348         ; do
1349                 f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1350                              $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1351                              return $FAILURE
1352         done
1353         f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
1354                 "\"zfs:$zroot_name/$zroot_bootfs\"" \
1355                 $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
1356
1357         # We're all done unless we should go on to do encryption
1358         [ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
1359
1360         #
1361         # Configure geli(8)-based encryption
1362         #
1363         f_dprintf "$funcname: Configuring disk encryption..."
1364         f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1365                 $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1366         f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1367                 $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1368         f_eval_catch $funcname echo "$ECHO_APPEND" \
1369                 'geom_eli_passphrase_prompt=\"YES\"' \
1370                 $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1371         for disk in $disks; do
1372                 f_eval_catch $funcname printf "$PRINTF_CONF" \
1373                         geli_%s_keyfile0_load "$disk$targetpart YES" \
1374                         $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1375                         return $FAILURE
1376                 f_eval_catch $funcname printf "$PRINTF_CONF" \
1377                         geli_%s_keyfile0_type \
1378                         "$disk$targetpart $disk$targetpart:geli_keyfile0" \
1379                         $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1380                         return $FAILURE
1381                 f_eval_catch $funcname printf "$PRINTF_CONF" \
1382                         geli_%s_keyfile0_name \
1383                         "$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1384                         $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1385                         return $FAILURE
1386         done
1387
1388         return $SUCCESS
1389 }
1390
1391 # dialog_menu_diskinfo
1392 #
1393 # Prompt the user to select a disk and then provide detailed info on it.
1394 #
1395 dialog_menu_diskinfo()
1396 {
1397         local device disk
1398
1399         #
1400         # Break from loop when user cancels disk selection
1401         #
1402         while :; do
1403                 device=$( msg_cancel="$msg_back" f_device_menu \
1404                         "$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1405                         $DEVICE_TYPE_DISK 2>&1 ) || break
1406                 $device get name disk
1407
1408                 # Show gpart(8) `show' and camcontrol(8) `inquiry' data
1409                 f_show_msg "$msg_detailed_disk_info" \
1410                         "$disk" "$( gpart show $disk 2> /dev/null )" \
1411                         "$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1412                         "$disk" "$( camcontrol identify $disk 2> /dev/null )"
1413         done
1414
1415         return $SUCCESS
1416 }
1417
1418 ############################################################ MAIN
1419
1420 #
1421 # Initialize
1422 #
1423 f_dialog_title "$msg_zfs_configuration"
1424 f_dialog_backtitle "$msg_freebsd_installer"
1425
1426 # User may have specifically requested ZFS-related operations be interactive
1427 ! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1428
1429 #
1430 # Debugging
1431 #
1432 f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1433 f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1434 f_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1435
1436 #
1437 # If the system was booted with UEFI, set the default boot type to UEFI
1438 #
1439 bootmethod=$( sysctl -n machdep.bootmethod )
1440 f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
1441 if [ "$bootmethod" = "UEFI" ]; then
1442         : ${ZFSBOOT_BOOT_TYPE:=UEFI}
1443         : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1444 else
1445         : ${ZFSBOOT_BOOT_TYPE:=BIOS}
1446         : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1447 fi
1448
1449 #
1450 # Loop over the main menu until we've accomplished what we came here to do
1451 #
1452 while :; do
1453         if ! f_interactive; then
1454                 retval=$DIALOG_OK
1455                 mtag=">>> $msg_install"
1456         else
1457                 dialog_menu_main
1458                 retval=$?
1459                 f_dialog_menutag_fetch mtag
1460         fi
1461
1462         f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1463         [ $retval -eq $DIALOG_OK ] || f_die
1464
1465         case "$mtag" in
1466         ">>> $msg_install")
1467                 #
1468                 # First, validate the user's selections
1469                 #
1470
1471                 # Make sure they gave us a name for the pool
1472                 if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1473                         f_dprintf "Pool name cannot be empty."
1474                         f_show_err "$msg_pool_name_cannot_be_empty"
1475                         continue
1476                 fi
1477
1478                 # Validate vdev type against number of disks selected/scripted
1479                 # (also validates that ZFSBOOT_DISKS are real [probed] disks)
1480                 # NB: dialog_menu_layout supports running non-interactively
1481                 dialog_menu_layout || continue
1482
1483                 # Make sure each disk will have room for ZFS
1484                 if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
1485                    f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize &&
1486                    f_expand_number "1g" zpoolmin
1487                 then
1488                         minsize=$(( $swapsize + $zpoolmin )) teeny_disks=
1489                         [ "$ZFSBOOT_BOOT_POOL" ] &&
1490                                 minsize=$(( $minsize + $bootsize ))
1491                         for disk in $ZFSBOOT_DISKS; do
1492                                 debug= f_device_find -1 \
1493                                         $disk $DEVICE_TYPE_DISK device
1494                                 $device get capacity disksize || continue
1495                                 [ ${disksize:-0} -ge 0 ] || disksize=0
1496                                 [ $disksize -lt $minsize ] &&
1497                                         teeny_disks="$teeny_disks $disk"
1498                         done
1499                         if [ "$teeny_disks" ]; then
1500                                 f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1501                                           "$ZFSBOOT_SWAP_SIZE" \
1502                                           "$ZFSBOOT_BOOT_POOL_SIZE" \
1503                                           "minsize=[$minsize]"
1504                                 f_dprintf "These disks are too small: %s" \
1505                                           "$teeny_disks"
1506                                 f_show_err "$msg_these_disks_are_too_small" \
1507                                            "$ZFSBOOT_SWAP_SIZE" \
1508                                            "$ZFSBOOT_BOOT_POOL_SIZE" \
1509                                            "$teeny_disks"
1510                                 continue
1511                         fi
1512                 fi
1513
1514                 #
1515                 # Last Chance!
1516                 #
1517                 if f_interactive; then
1518                         dialog_last_chance $ZFSBOOT_DISKS || continue
1519                 fi
1520
1521                 #
1522                 # Let's do this
1523                 #
1524
1525                 vdev_type="$ZFSBOOT_VDEV_TYPE"
1526
1527                 # Blank the vdev type for the default layout
1528                 [ "$vdev_type" = "stripe" ] && vdev_type=
1529
1530                 zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1531                                 "$vdev_type" $ZFSBOOT_DISKS || continue
1532
1533                 break # to success
1534                 ;;
1535         ?" $msg_pool_type_disks")
1536                 ZFSBOOT_CONFIRM_LAYOUT=1
1537                 dialog_menu_layout
1538                 # User has poked settings, disable later confirmation
1539                 ZFSBOOT_CONFIRM_LAYOUT=
1540                 ;;
1541         "- $msg_rescan_devices") f_device_rescan ;;
1542         "- $msg_disk_info") dialog_menu_diskinfo ;;
1543         ?" $msg_pool_name")
1544                 # Prompt the user to input/change the name for the new pool
1545                 f_dialog_input input \
1546                         "$msg_please_enter_a_name_for_your_zpool" \
1547                         "$ZFSBOOT_POOL_NAME" &&
1548                         ZFSBOOT_POOL_NAME="$input"
1549                 ;;
1550         ?" $msg_force_4k_sectors")
1551                 # Toggle the variable referenced both by the menu and later
1552                 if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1553                         ZFSBOOT_FORCE_4K_SECTORS=
1554                 else
1555                         ZFSBOOT_FORCE_4K_SECTORS=1
1556                 fi
1557                 ;;
1558         ?" $msg_encrypt_disks")
1559                 # Toggle the variable referenced both by the menu and later
1560                 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1561                         ZFSBOOT_GELI_ENCRYPTION=
1562                 else
1563                         ZFSBOOT_FORCE_4K_SECTORS=1
1564                         ZFSBOOT_GELI_ENCRYPTION=1
1565                 fi
1566                 ;;
1567         ?" $msg_partition_scheme")
1568                 # Toggle between GPT (BIOS), GPT (UEFI) and MBR
1569                 if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]; then
1570                         ZFSBOOT_PARTITION_SCHEME="GPT"
1571                         ZFSBOOT_BOOT_TYPE="UEFI"
1572                 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" ]; then
1573                         ZFSBOOT_PARTITION_SCHEME="MBR"
1574                         ZFSBOOT_BOOT_TYPE="BIOS"
1575                 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1576                         ZFSBOOT_PARTITION_SCHEME="GPT + Active"
1577                         ZFSBOOT_BOOT_TYPE="BIOS"
1578                 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
1579                         ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
1580                         ZFSBOOT_BOOT_TYPE="BIOS"
1581                 else
1582                         ZFSBOOT_PARTITION_SCHEME="GPT"
1583                         ZFSBOOT_BOOT_TYPE="BIOS"
1584                 fi
1585                 ;;
1586         ?" $msg_swap_size")
1587                 # Prompt the user to input/change the swap size for each disk
1588                 while :; do
1589                     f_dialog_input input \
1590                             "$msg_please_enter_amount_of_swap_space" \
1591                             "$ZFSBOOT_SWAP_SIZE" &&
1592                             ZFSBOOT_SWAP_SIZE="${input:-0}"
1593                     if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1594                     then
1595                         if [ $swapsize -ne 0 -a $swapsize -lt 104857600 ]; then
1596                             f_show_err "$msg_swap_toosmall" \
1597                                        "$ZFSBOOT_SWAP_SIZE"
1598                             continue;
1599                         else
1600                             break;
1601                         fi
1602                     else
1603                         f_show_err "$msg_swap_invalid" \
1604                                    "$ZFSBOOT_SWAP_SIZE"
1605                         continue;
1606                     fi
1607                 done
1608                 ;;
1609         ?" $msg_swap_mirror")
1610                 # Toggle the variable referenced both by the menu and later
1611                 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1612                         ZFSBOOT_SWAP_MIRROR=
1613                 else
1614                         ZFSBOOT_SWAP_MIRROR=1
1615                 fi
1616                 ;;
1617         ?" $msg_swap_encrypt")
1618                 # Toggle the variable referenced both by the menu and later
1619                 if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1620                         ZFSBOOT_SWAP_ENCRYPTION=
1621                 else
1622                         ZFSBOOT_SWAP_ENCRYPTION=1
1623                 fi
1624                 ;;
1625         esac
1626 done
1627
1628 exit $SUCCESS
1629
1630 ################################################################################
1631 # END
1632 ################################################################################