3 # Copyright (c) 2013-2016 Allan Jude
4 # Copyright (c) 2013-2018 Devin Teske
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
30 ############################################################ INCLUDES
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
40 ############################################################ CONFIGURATION
43 # Default name of the boot-pool
45 : ${ZFSBOOT_POOL_NAME:=zroot}
48 # Default options to use when creating zroot pool
50 : ${ZFSBOOT_POOL_CREATE_OPTIONS:=-O compress=lz4 -O atime=off}
53 # Default name for the boot environment parent dataset
55 : ${ZFSBOOT_BEROOT_NAME:=ROOT}
58 # Default name for the primary boot environment
60 : ${ZFSBOOT_BOOTFS_NAME:=default}
63 # Default Virtual Device (vdev) type to create
65 : ${ZFSBOOT_VDEV_TYPE:=stripe}
68 # Should we use sysctl(8) vfs.zfs.min_auto_ashift=12 to force 4K sectors?
70 : ${ZFSBOOT_FORCE_4K_SECTORS:=1}
73 # Should we use geli(8) to encrypt the drives?
74 # NB: Automatically enables ZFSBOOT_BOOT_POOL
76 : ${ZFSBOOT_GELI_ENCRYPTION=}
79 # Default path to the geli(8) keyfile used in drive encryption
81 : ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
84 # Create a separate boot pool?
85 # NB: Automatically set when using geli(8) or MBR
87 : ${ZFSBOOT_BOOT_POOL=}
90 # Options to use when creating separate boot pool (if any)
92 : ${ZFSBOOT_BOOT_POOL_CREATE_OPTIONS:=}
95 # Default name for boot pool when enabled (e.g., geli(8) or MBR)
97 : ${ZFSBOOT_BOOT_POOL_NAME:=bootpool}
100 # Default size for boot pool when enabled (e.g., geli(8) or MBR)
102 : ${ZFSBOOT_BOOT_POOL_SIZE:=2g}
105 # Default disks to use (always empty unless being scripted)
110 # Default partitioning scheme to use on disks
112 : ${ZFSBOOT_PARTITION_SCHEME:=}
115 # Default boot type to use on disks
117 : ${ZFSBOOT_BOOT_TYPE:=}
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.
123 : ${ZFSBOOT_SWAP_SIZE:=2g}
126 # Should we use geli(8) to encrypt the swap?
128 : ${ZFSBOOT_SWAP_ENCRYPTION=}
131 # Should we use gmirror(8) to mirror the swap?
133 : ${ZFSBOOT_SWAP_MIRROR=}
136 # Default ZFS datasets for root zpool
138 # NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
139 # NOTE: Anything after pound/hash character [#] is ignored as a comment.
141 f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
142 # DATASET OPTIONS (comma or space separated; or both)
144 # Boot Environment [BE] root and default boot dataset
145 /$ZFSBOOT_BEROOT_NAME mountpoint=none
146 /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME mountpoint=/
148 # Compress /tmp, allow exec but not setuid
149 /tmp mountpoint=/tmp,exec=on,setuid=off
151 # Don't mount /usr so that 'base' files go to the BEROOT
152 /usr mountpoint=/usr,canmount=off
154 # Home directories separated so they are common to all BEs
155 /usr/home # NB: /home is a symlink to /usr/home
158 /usr/ports setuid=off
160 # Source tree (compressed)
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
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.
176 : ${ZFSBOOT_CONFIRM_LAYOUT:=1}
178 ############################################################ GLOBALS
181 # Format of a line in printf(1) syntax to add to fstab(5)
183 FSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
186 # Command strings for various tasks
189 CHMOD_MODE='chmod %s "%s"'
190 DD_WITH_OPTIONS='dd if="%s" of="%s" %s'
191 ECHO_APPEND='echo "%s" >> "%s"'
192 ECHO_OVERWRITE='echo "%s" > "%s"'
193 GELI_ATTACH='geli attach -j - -k "%s" "%s"'
194 GELI_ATTACH_NOKEY='geli attach -j - "%s"'
195 GELI_DETACH_F='geli detach -f "%s"'
196 GELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
197 GELI_PASSWORD_GELIBOOT_INIT='geli init -bg -e %s -J - -l 256 -s 4096 "%s"'
198 GPART_ADD_ALIGN='gpart add %s -t %s "%s"'
199 GPART_ADD_ALIGN_INDEX='gpart add %s -i %s -t %s "%s"'
200 GPART_ADD_ALIGN_INDEX_WITH_SIZE='gpart add %s -i %s -t %s -s %s "%s"'
201 GPART_ADD_ALIGN_LABEL='gpart add %s -l %s -t %s "%s"'
202 GPART_ADD_ALIGN_LABEL_WITH_SIZE='gpart add %s -l %s -t %s -s %s "%s"'
203 GPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
204 GPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
205 GPART_BOOTCODE_PARTONLY='gpart bootcode -p "%s" -i %s "%s"'
206 GPART_CREATE='gpart create -s %s "%s"'
207 GPART_DESTROY_F='gpart destroy -F "%s"'
208 GPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
209 GPART_SET_LENOVOFIX='gpart set -a lenovofix "%s"'
210 GPART_SET_PMBR_ACTIVE='gpart set -a active "%s"'
211 GRAID_DELETE='graid delete "%s"'
213 LN_SF='ln -sf "%s" "%s"'
214 MKDIR_P='mkdir -p "%s"'
215 MOUNT_TYPE='mount -t %s "%s" "%s"'
216 PRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
217 PRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
218 SHELL_TRUNCATE=':> "%s"'
219 SWAP_GMIRROR_LABEL='gmirror label swap %s'
220 SYSCTL_ZFS_MIN_ASHIFT_12='sysctl vfs.zfs.min_auto_ashift=12'
222 ZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
223 ZFS_MOUNT='zfs mount "%s"'
224 ZFS_SET='zfs set "%s" "%s"'
225 ZFS_UNMOUNT='zfs unmount "%s"'
226 ZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
227 ZPOOL_DESTROY='zpool destroy "%s"'
228 ZPOOL_EXPORT='zpool export "%s"'
229 ZPOOL_EXPORT_F='zpool export -f "%s"'
230 ZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
231 ZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
232 ZPOOL_SET='zpool set %s "%s"'
235 # Strings that should be moved to an i18n file and loaded with f_include_lang()
237 hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
238 hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
239 hline_arrows_tab_enter="Press arrows, TAB or ENTER"
240 msg_an_unknown_error_occurred="An unknown error occurred"
243 msg_change_selection="Change Selection"
244 msg_configure_options="Configure Options:"
245 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"
246 msg_disk_info="Disk Info"
247 msg_disk_info_help="Get detailed information on disk device(s)"
248 msg_disk_plural="disks"
249 msg_disk_singular="disk"
250 msg_encrypt_disks="Encrypt Disks?"
251 msg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
253 msg_force_4k_sectors="Force 4K Sectors?"
254 msg_force_4k_sectors_help="Align partitions to 4K sector boundries and set vfs.zfs.min_auto_ashift=12"
255 msg_freebsd_installer="FreeBSD Installer"
256 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"
257 msg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
258 msg_install="Install"
259 msg_install_desc="Proceed with Installation"
260 msg_install_help="Create ZFS boot pool with displayed options"
261 msg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
262 msg_invalid_disk_argument="Invalid disk argument \`%s'"
263 msg_invalid_index_argument="Invalid index argument \`%s'"
264 msg_invalid_swap_size="Invalid swap size \`%s'"
265 msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
266 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"
267 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'
268 msg_mirror_desc="Mirror - n-Way Mirroring"
269 msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
270 msg_missing_disk_arguments="missing disk arguments"
271 msg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
273 msg_no_disks_present_to_configure="No disk(s) present to configure"
274 msg_no_disks_selected="No disks selected."
275 msg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
276 msg_null_disk_argument="NULL disk argument"
277 msg_null_index_argument="NULL index argument"
278 msg_null_poolname="NULL poolname"
279 msg_odd_disk_selected="An even number of disks must be selected to create a RAID 1+0. (%u selected)"
281 msg_partition_scheme="Partition Scheme"
282 msg_partition_scheme_help="Select partitioning scheme. GPT is recommended."
283 msg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
284 msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
285 msg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
286 msg_pool_name="Pool Name"
287 msg_pool_name_cannot_be_empty="Pool name cannot be empty."
288 msg_pool_name_help="Customize the name of the zpool to be created (Required)"
289 msg_pool_type_disks="Pool Type/Disks:"
290 msg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
291 msg_processing_selection="Processing selection..."
292 msg_raid10_desc="RAID 1+0 - n x 2-Way Mirrors"
293 msg_raid10_help="[4+ Disks] Striped Mirrors provides the best performance, but the least storage"
294 msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
295 msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
296 msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
297 msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
298 msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
299 msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
300 msg_rescan_devices="Rescan Devices"
301 msg_rescan_devices_help="Scan for device changes"
303 msg_select_a_disk_device="Select a disk device"
304 msg_select_virtual_device_type="Select Virtual Device type:"
305 msg_stripe_desc="Stripe - No Redundancy"
306 msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
307 msg_swap_encrypt="Encrypt Swap?"
308 msg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
309 msg_swap_invalid="The selected swap size (%s) is invalid. Enter a number optionally followed by units. Example: 2G"
310 msg_swap_mirror="Mirror Swap?"
311 msg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
312 msg_swap_size="Swap Size"
313 msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
314 msg_swap_toosmall="The selected swap size (%s) is to small. Please enter a value greater than 100MB or enter 0 for no swap"
315 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."
316 msg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
317 msg_unsupported_partition_scheme="%s is an unsupported partition scheme"
318 msg_user_cancelled="User Cancelled."
320 msg_zfs_configuration="ZFS Configuration"
322 ############################################################ FUNCTIONS
326 # Display the dialog(1)-based application main menu.
330 local title="$DIALOG_TITLE"
331 local btitle="$DIALOG_BACKTITLE"
332 local prompt="$msg_configure_options"
333 local force4k="$msg_no"
334 local usegeli="$msg_no"
335 local swapgeli="$msg_no"
336 local swapmirror="$msg_no"
337 [ "$ZFSBOOT_FORCE_4K_SECTORS" ] && force4k="$msg_yes"
338 [ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
339 [ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
340 [ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
341 local disks n disks_grammar
342 f_count n $ZFSBOOT_DISKS
343 { [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
344 disks_grammar=$msg_disk_plural # grammar
346 '>>> $msg_install' '$msg_install_desc'
348 'T $msg_pool_type_disks'
349 '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
350 '$msg_pool_type_disks_help'
351 '- $msg_rescan_devices' '*'
352 '$msg_rescan_devices_help'
353 '- $msg_disk_info' '*'
354 '$msg_disk_info_help'
355 'N $msg_pool_name' '$ZFSBOOT_POOL_NAME'
356 '$msg_pool_name_help'
357 '4 $msg_force_4k_sectors'
359 '$msg_force_4k_sectors_help'
360 'E $msg_encrypt_disks' '$usegeli'
361 '$msg_encrypt_disks_help'
362 'P $msg_partition_scheme'
363 '$ZFSBOOT_PARTITION_SCHEME ($ZFSBOOT_BOOT_TYPE)'
364 '$msg_partition_scheme_help'
365 'S $msg_swap_size' '$ZFSBOOT_SWAP_SIZE'
366 '$msg_swap_size_help'
367 'M $msg_swap_mirror' '$swapmirror'
368 '$msg_swap_mirror_help'
369 'W $msg_swap_encrypt' '$swapgeli'
370 '$msg_swap_encrypt_help'
372 local defaultitem= # Calculated below
373 local hline="$hline_alnum_arrows_punc_tab_enter"
375 local height width rows
376 eval f_dialog_menu_with_help_size height width rows \
377 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
379 # Obtain default-item from previously stored selection
380 f_dialog_default_fetch defaultitem
383 menu_choice=$( eval $DIALOG \
384 --title \"\$title\" \
385 --backtitle \"\$btitle\" \
386 --hline \"\$hline\" \
388 --ok-label \"\$msg_select\" \
389 --cancel-label \"\$msg_cancel\" \
390 --default-item \"\$defaultitem\" \
391 --menu \"\$prompt\" \
392 $height $width $rows \
394 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
397 f_dialog_data_sanitize menu_choice
398 f_dialog_menutag_store "$menu_choice"
400 # Only update default-item on success
401 [ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
406 # dialog_last_chance $disks ...
408 # Display a list of the disks that the user is about to destroy. The default
409 # action is to return error status unless the user explicitly (non-default)
410 # selects "Yes" from the noyes dialog.
414 local title="$DIALOG_TITLE"
415 local btitle="$DIALOG_BACKTITLE"
416 local prompt # Calculated below
417 local hline="$hline_arrows_tab_enter"
419 local height=8 width=50 prefix=" "
420 local plen=${#prefix} list= line=
421 local max_width=$(( $width - 3 - $plen ))
423 local yes no defaultno extra_args format
424 if [ "$USE_XDIALOG" ]; then
425 yes=ok no=cancel defaultno=default-no
426 extra_args="--wrap --left"
427 format="$msg_last_chance_are_you_sure"
429 yes=yes no=no defaultno=defaultno
430 extra_args="--colors --cr-wrap"
431 format="$msg_last_chance_are_you_sure_color"
434 local disk line_width
441 line_width=$(( $line_width + 1 + ${#disk} ))
442 # Add newline before disk if it would exceed max_width
443 if [ $line_width -gt $max_width ]; then
446 height=$(( $height + 1 ))
448 # Add the disk to the list
451 # Append the left-overs
452 if [ "${line#$prefix}" ]; then
454 height=$(( $height + 1 ))
457 # Add height for Xdialog(1)
458 [ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
460 prompt=$( printf "$format" "$list" )
461 f_dprintf "%s: Last Chance!" "$0"
464 --backtitle "$btitle" \
467 --$yes-label "$msg_yes" \
468 --$no-label "$msg_no" \
470 --yesno "$prompt" $height $width
475 # Configure Virtual Device type and disks to use for the ZFS boot pool. User
476 # must select enough disks to satisfy the chosen vdev type.
480 local funcname=dialog_menu_layout
481 local title="$DIALOG_TITLE"
482 local btitle="$DIALOG_BACKTITLE"
483 local vdev_prompt="$msg_select_virtual_device_type"
484 local disk_prompt="$msg_please_select_one_or_more_disks"
485 local vdev_menu_list="
486 'stripe' '$msg_stripe_desc' '$msg_stripe_help'
487 'mirror' '$msg_mirror_desc' '$msg_mirror_help'
488 'raid10' '$msg_raid10_desc' '$msg_raid10_help'
489 'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
490 'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
491 'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
493 local disk_check_list= # Calculated below
494 local vdev_hline="$hline_arrows_tab_enter"
495 local disk_hline="$hline_arrows_space_tab_enter"
497 # Warn the user if vdev type is not valid
498 case "$ZFSBOOT_VDEV_TYPE" in
499 stripe|mirror|raid10|raidz1|raidz2|raidz3) : known good ;;
501 f_dprintf "%s: Invalid virtual device type \`%s'" \
502 $funcname "$ZFSBOOT_VDEV_TYPE"
503 f_show_err "$msg_invalid_virtual_device_type" \
505 f_interactive || return $FAILURE
508 # Calculate size of vdev menu once only
509 local vheight vwidth vrows
510 eval f_dialog_menu_with_help_size vheight vwidth vrows \
511 \"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
514 # Get a list of probed disk devices
516 debug= f_device_find "" $DEVICE_TYPE_DISK disks
518 # Prune out mounted md(4) devices that may be part of the boot process
519 local disk name new_list=
520 for disk in $disks; do
521 debug= $disk get name name
523 md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
525 new_list="$new_list $disk"
527 disks="${new_list# }"
530 if [ "$debug" ]; then
532 for disk in $disks; do
533 debug= $disk get name name
534 disk_names="$disk_names $name"
536 f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
539 if [ ! "$disks" ]; then
540 f_dprintf "No disk(s) present to configure"
541 f_show_err "$msg_no_disks_present_to_configure"
545 # Lets sort the disks array to be more user friendly
546 f_device_sort_by name disks disks
549 # Operate in a loop so we can (if interactive) repeat if not enough
550 # disks are selected to satisfy the chosen vdev type or user wants to
551 # back-up to the previous menu.
553 local vardisk ndisks onoff selections vdev_choice breakout device
554 local valid_disks all_valid want_disks desc height width rows
557 # Confirm the vdev type that was selected
559 if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
560 vdev_choice=$( eval $DIALOG \
561 --title \"\$title\" \
562 --backtitle \"\$btitle\" \
563 --hline \"\$vdev_hline\" \
564 --ok-label \"\$msg_ok\" \
565 --cancel-label \"\$msg_cancel\" \
567 --default-item \"\$ZFSBOOT_VDEV_TYPE\" \
568 --menu \"\$vdev_prompt\" \
569 $vheight $vwidth $vrows \
571 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
573 # Exit if user pressed ESC or chose Cancel/No
574 f_dialog_data_sanitize vdev_choice
576 ZFSBOOT_VDEV_TYPE="$vdev_choice"
577 f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
581 # Determine the number of disks needed for this vdev type
583 case "$ZFSBOOT_VDEV_TYPE" in
584 stripe) want_disks=1 ;;
585 mirror) want_disks=2 ;;
586 raid10) want_disks=4 ;;
587 raidz1) want_disks=3 ;;
588 raidz2) want_disks=4 ;;
589 raidz3) want_disks=5 ;;
593 # Warn the user if any scripted disks are invalid
595 valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
596 for disk in $ZFSBOOT_DISKS; do
597 if debug= f_device_find -1 \
598 $disk $DEVICE_TYPE_DISK device
600 valid_disks="$valid_disks $disk"
603 f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
606 if [ ! "$all_valid" ]; then
607 if [ "$ZFSBOOT_DISKS" ]; then
609 "$msg_missing_one_or_more_scripted_disks"
611 f_dprintf "No disks selected."
613 f_show_err "$msg_no_disks_selected"
615 f_interactive || return $FAILURE
617 ZFSBOOT_DISKS="${valid_disks# }"
620 # Short-circuit if we're running non-interactively
622 if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
623 f_count ndisks $ZFSBOOT_DISKS
624 [ $ndisks -ge $want_disks ] && break # to success
626 # Not enough disks selected
627 f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
628 "$ZFSBOOT_VDEV_TYPE" \
629 "Not enough disks selected." \
631 f_interactive || return $FAILURE
632 msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
633 f_yesno "%s: $msg_not_enough_disks_selected" \
634 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
639 # Confirm the disks that were selected
640 # Loop until the user cancels or selects enough disks
644 # Loop over list of available disks, resetting state
645 for disk in $disks; do
646 f_isset _${disk}_status && _${disk}_status=
649 # Loop over list of selected disks and create temporary
650 # locals to map statuses onto up-to-date list of disks
651 for disk in $ZFSBOOT_DISKS; do
652 debug= f_device_find -1 \
653 $disk $DEVICE_TYPE_DISK disk
654 f_isset _${disk}_status ||
655 local _${disk}_status
659 # Create the checklist menu of discovered disk devices
661 for disk in $disks; do
665 f_shell_escape "$desc" desc
666 f_getvar _${disk}_status:-off onoff
667 disk_check_list="$disk_check_list
668 $name '$desc' $onoff"
671 eval f_dialog_checklist_size height width rows \
672 \"\$title\" \"\$btitle\" \"\$prompt\" \
673 \"\$hline\" $disk_check_list
675 selections=$( eval $DIALOG \
676 --title \"\$DIALOG_TITLE\" \
677 --backtitle \"\$DIALOG_BACKTITLE\" \
679 --hline \"\$hline\" \
680 --ok-label \"\$msg_ok\" \
681 --cancel-label \"\$msg_back\" \
682 --checklist \"\$prompt\" \
683 $height $width $rows \
685 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
687 # Loop if user pressed ESC or chose Cancel/No
688 f_dialog_data_sanitize selections
690 ZFSBOOT_DISKS="$selections"
691 f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
694 f_count ndisks $ZFSBOOT_DISKS
696 if [ "$ZFSBOOT_VDEV_TYPE" == "raid10" ] &&
697 [ $(( $ndisks % 2 )) -ne 0 ]; then
698 f_dprintf "$funcname: %s: %s (%u %% 2 = %u)" \
699 "$ZFSBOOT_VDEV_TYPE" \
700 "Number of disks not even:" \
701 $ndisks $(( $ndisks % 2 ))
702 msg_yes="$msg_change_selection" \
703 msg_no="$msg_cancel" \
704 f_yesno "%s: $msg_odd_disk_selected" \
705 "$ZFSBOOT_VDEV_TYPE" $ndisks ||
710 [ $ndisks -ge $want_disks ] &&
711 breakout=break && break
713 # Not enough disks selected
714 f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
715 "$ZFSBOOT_VDEV_TYPE" \
716 "Not enough disks selected." \
718 msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
719 f_yesno "%s: $msg_not_enough_disks_selected" \
720 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
723 [ "$breakout" = "break" ] && break
724 [ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
730 # zfs_create_diskpart $disk $index
732 # For each block device to be used in the zpool, rather than just create the
733 # zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
734 # so we can have some real swap. This also provides wiggle room incase your
735 # replacement drivers do not have the exact same sector counts.
737 # NOTE: $swapsize and $bootsize should be defined by the calling function.
738 # NOTE: Sets $bootpart and $targetpart for the calling function.
740 zfs_create_diskpart()
742 local funcname=zfs_create_diskpart
743 local disk="$1" index="$2"
746 if [ ! "$disk" ]; then
747 f_dprintf "$funcname: NULL disk argument"
748 msg_error="$msg_error: $funcname" \
749 f_show_err "$msg_null_disk_argument"
752 if [ "${disk#*[$IFS]}" != "$disk" ]; then
753 f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
754 msg_error="$msg_error: $funcname" \
755 f_show_err "$msg_invalid_disk_argument" "$disk"
758 if [ ! "$index" ]; then
759 f_dprintf "$funcname: NULL index argument"
760 msg_error="$msg_error: $funcname" \
761 f_show_err "$msg_null_index_argument"
764 if ! f_isinteger "$index"; then
765 f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
766 msg_error="$msg_error: $funcname" \
767 f_show_err "$msg_invalid_index_argument" "$index"
770 f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
772 # Check for unknown partition scheme before proceeding further
773 case "$ZFSBOOT_PARTITION_SCHEME" in
774 ""|MBR|GPT*) : known good ;;
776 f_dprintf "$funcname: %s is an unsupported partition scheme" \
777 "$ZFSBOOT_PARTITION_SCHEME"
778 msg_error="$msg_error: $funcname" f_show_err \
779 "$msg_unsupported_partition_scheme" \
780 "$ZFSBOOT_PARTITION_SCHEME"
785 # Destroy whatever partition layout is currently on disk.
786 # NOTE: `-F' required to destroy if partitions still exist.
787 # NOTE: Failure is ok here, blank disk will have nothing to destroy.
789 f_dprintf "$funcname: Exporting ZFS pools..."
790 zpool list -Ho name | while read z_name; do
791 f_eval_catch -d $funcname zpool "$ZPOOL_EXPORT_F" $z_name
793 f_dprintf "$funcname: Detaching all GELI providers..."
794 geli status | tail -n +2 | while read g_name g_status g_component; do
795 f_eval_catch -d $funcname geli "$GELI_DETACH_F" $g_name
797 f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
798 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
799 f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
800 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
802 # Make doubly-sure backup GPT is destroyed
803 f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
804 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
807 # Lay down the desired type of partition scheme
809 local setsize mbrindex align_small align_big
811 # If user has requested 4 K alignment, add these params to the
812 # gpart add calls. With GPT, we align large partitions to 1 M for
813 # improved performance on SSDs. MBR does not always play well with gaps
814 # between partitions, so all alignment is only 4k for that case.
815 # With MBR, we align the BSD partition that contains the MBR, otherwise
816 # the system fails to boot.
818 if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
823 case "$ZFSBOOT_PARTITION_SCHEME" in
824 ""|GPT*) f_dprintf "$funcname: Creating GPT layout..."
826 # 1. Create GPT layout using labels
828 f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
832 # Apply workarounds if requested by the user
834 if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Lenovo Fix" ]; then
835 f_eval_catch $funcname gpart "$GPART_SET_LENOVOFIX" \
836 $disk || return $FAILURE
837 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
838 f_eval_catch $funcname gpart "$GPART_SET_PMBR_ACTIVE" \
839 $disk || return $FAILURE
843 # 2. Add small freebsd-boot and/or efi partition
845 if [ "$ZFSBOOT_BOOT_TYPE" = "UEFI" -o \
846 "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
848 f_eval_catch $funcname gpart \
849 "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
850 "$align_small" efiboot$index efi 200M \
851 $disk || return $FAILURE
853 # We'll configure the ESP in bootconfig
856 if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" -o \
857 "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]
859 f_eval_catch $funcname gpart \
860 "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
861 "$align_small" gptboot$index \
862 freebsd-boot 512k $disk || return $FAILURE
863 if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]; then
864 f_eval_catch $funcname gpart \
865 "$GPART_BOOTCODE_PART" \
866 /boot/pmbr /boot/gptzfsboot 1 \
867 $disk || return $FAILURE
869 f_eval_catch $funcname gpart \
870 "$GPART_BOOTCODE_PART" \
871 /boot/pmbr /boot/gptzfsboot 2 \
872 $disk || return $FAILURE
876 # NB: zpool will use the `zfs#' GPT labels
877 if [ "$ZFSBOOT_BOOT_TYPE" = "BIOS+UEFI" ]; then
878 if [ "$ZFSBOOT_BOOT_POOL" ]; then
879 bootpart=p3 swappart=p4 targetpart=p4
880 [ ${swapsize:-0} -gt 0 ] && targetpart=p5
883 bootpart=p3 swappart=p3 targetpart=p3
884 [ ${swapsize:-0} -gt 0 ] && targetpart=p4
887 if [ "$ZFSBOOT_BOOT_POOL" ]; then
888 bootpart=p2 swappart=p3 targetpart=p3
889 [ ${swapsize:-0} -gt 0 ] && targetpart=p4
892 bootpart=p2 swappart=p2 targetpart=p2
893 [ ${swapsize:-0} -gt 0 ] && targetpart=p3
898 # Prepare boot pool if enabled (e.g., for geli(8))
900 if [ "$ZFSBOOT_BOOT_POOL" ]; then
901 f_eval_catch $funcname gpart \
902 "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
903 "$align_big" boot$index freebsd-zfs \
904 ${bootsize}b $disk || return $FAILURE
905 # Pedantically nuke any old labels
906 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
908 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
909 # Pedantically detach targetpart for later
910 f_eval_catch -d $funcname geli \
912 /dev/$disk$targetpart
917 # 3. Add freebsd-swap partition labeled `swap#'
919 if [ ${swapsize:-0} -gt 0 ]; then
920 f_eval_catch $funcname gpart \
921 "$GPART_ADD_ALIGN_LABEL_WITH_SIZE" \
922 "$align_big" swap$index freebsd-swap \
923 ${swapsize}b $disk || return $FAILURE
924 # Pedantically nuke any old labels on the swap
925 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
930 # 4. Add freebsd-zfs partition labeled `zfs#' for zroot
932 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_LABEL" \
933 "$align_big" zfs$index freebsd-zfs $disk ||
935 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
936 /dev/$disk$targetpart
939 MBR) f_dprintf "$funcname: Creating MBR layout..."
941 # Enable boot pool if encryption is desired
943 [ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
945 # 1. Create MBR layout (no labels)
947 f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
949 f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
950 $disk || return $FAILURE
953 # 2. Add freebsd slice with all available space
955 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN" \
956 "$align_small" freebsd $disk || return $FAILURE
957 f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
959 # Pedantically nuke any old labels
960 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
962 # Pedantically nuke any old scheme
963 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
966 # 3. Write BSD scheme to the freebsd slice
968 f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
971 # NB: zpool will use s1a (no labels)
972 bootpart=s1a swappart=s1b targetpart=s1d mbrindex=4
975 # Always prepare a boot pool on MBR
976 # Do not align this partition, there must not be a gap
979 f_eval_catch $funcname gpart \
980 "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
981 "" 1 freebsd-zfs ${bootsize}b ${disk}s1 ||
983 # Pedantically nuke any old labels
984 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
986 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
987 # Pedantically detach targetpart for later
988 f_eval_catch -d $funcname geli \
990 /dev/$disk$targetpart
994 # 4. Add freebsd-swap partition
996 if [ ${swapsize:-0} -gt 0 ]; then
997 f_eval_catch $funcname gpart \
998 "$GPART_ADD_ALIGN_INDEX_WITH_SIZE" \
999 "$align_small" 2 freebsd-swap \
1000 ${swapsize}b ${disk}s1 || return $FAILURE
1001 # Pedantically nuke any old labels on the swap
1002 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
1007 # 5. Add freebsd-zfs partition for zroot
1009 f_eval_catch $funcname gpart "$GPART_ADD_ALIGN_INDEX" \
1010 "$align_small" $mbrindex freebsd-zfs ${disk}s1 ||
1012 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
1013 /dev/$disk$targetpart # Pedantic
1014 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1015 /boot/zfsboot /dev/${disk}s1 count=1 ||
1019 esac # $ZFSBOOT_PARTITION_SCHEME
1023 f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1024 if [ "$isswapmirror" ]; then
1025 # This is not the first disk in the mirror, do nothing
1026 elif [ ${swapsize:-0} -eq 0 ]; then
1027 # If swap is 0 sized, don't add it to fstab
1028 elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
1029 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1030 /dev/mirror/swap.eli none swap sw 0 0 \
1031 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1033 elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1034 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1035 /dev/mirror/swap none swap sw 0 0 \
1036 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1038 elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1039 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1040 /dev/$disk${swappart}.eli none swap sw 0 0 \
1041 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1043 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1044 /dev/$disk$swappart none swap sw 0 0 \
1045 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1051 # zfs_create_boot $poolname $vdev_type $disks ...
1053 # Creates boot pool and dataset layout. Returns error if something goes wrong.
1054 # Errors are printed to stderr for collection and display.
1058 local funcname=zfs_create_boot
1059 local zroot_name="$1"
1060 local zroot_vdevtype="$2"
1061 local zroot_vdevs= # Calculated below
1062 local swap_devs= # Calculated below
1063 local boot_vdevs= # Used for geli(8) and/or MBR layouts
1064 shift 2 # poolname vdev_type
1065 local disks="$*" disk
1067 local bootpart targetpart swappart # Set by zfs_create_diskpart() below
1068 local create_options
1071 # Pedantic checks; should never be seen
1073 if [ ! "$zroot_name" ]; then
1074 f_dprintf "$funcname: NULL poolname"
1075 msg_error="$msg_error: $funcname" \
1076 f_show_err "$msg_null_poolname"
1079 if [ $# -lt 1 ]; then
1080 f_dprintf "$funcname: missing disk arguments"
1081 msg_error="$msg_error: $funcname" \
1082 f_show_err "$msg_missing_disk_arguments"
1085 f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
1086 "$zroot_name" "$zroot_vdevtype"
1089 # Initialize fstab(5)
1091 f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1092 f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1094 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1095 "# Device" Mountpoint FStype Options Dump "Pass#" \
1096 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1099 # Expand SI units in desired sizes
1101 f_dprintf "$funcname: Expanding supplied size values..."
1102 local swapsize bootsize
1103 if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1104 f_dprintf "$funcname: Invalid swap size \`%s'" \
1105 "$ZFSBOOT_SWAP_SIZE"
1106 f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1109 if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
1110 f_dprintf "$funcname: Invalid boot pool size \`%s'" \
1111 "$ZFSBOOT_BOOT_POOL_SIZE"
1112 f_show_err "$msg_invalid_boot_pool_size" \
1113 "$ZFSBOOT_BOOT_POOL_SIZE"
1116 f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1117 "$ZFSBOOT_SWAP_SIZE" "$swapsize"
1118 f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
1119 "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1122 # Destroy the pool in-case this is our second time 'round (case of
1123 # failure and installer presented ``Retry'' option to come back).
1125 # NB: If we don't destroy the pool, later gpart(8) destroy commands
1126 # that try to clear existing partitions (see zfs_create_diskpart())
1127 # will fail with a `Device Busy' error, leading to `GEOM exists'.
1129 f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1132 # Prepare the disks and build pool device list(s)
1134 f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1136 # Force 4K sectors using vfs.zfs.min_auto_ashift=12
1137 if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1138 f_dprintf "$funcname: With 4K sectors..."
1139 f_eval_catch $funcname sysctl "$SYSCTL_ZFS_MIN_ASHIFT_12" \
1141 sysctl kern.geom.part.mbr.enforce_chs=0
1144 for disk in $disks; do
1145 zfs_create_diskpart $disk $n || return $FAILURE
1146 # Now $bootpart, $targetpart, and $swappart are set (suffix
1148 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1149 boot_vdevs="$boot_vdevs $disk$bootpart"
1151 zroot_vdevs="$zroot_vdevs $disk$targetpart"
1152 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1153 zroot_vdevs="$zroot_vdevs.eli"
1160 # If we need/want a boot pool, create it
1162 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1163 local bootpool_vdevtype= # Calculated below
1164 local bootpool_options= # Calculated below
1165 local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
1166 local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
1167 local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
1169 f_dprintf "$funcname: Setting up boot pool..."
1170 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
1171 f_dprintf "$funcname: For encrypted root disk..."
1173 # Create parent directory for boot pool
1174 f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1175 f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1176 $BSDINSTALL_CHROOT || return $FAILURE
1178 # Create mirror across the boot partition on all disks
1180 f_count nvdevs $boot_vdevs
1181 [ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1183 create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
1184 bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
1185 bootpool_options="$bootpool_options $create_options"
1186 bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1187 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1188 "$bootpool_options" "$bootpool_name" \
1189 "$bootpool_vdevtype" "$boot_vdevs" ||
1192 f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1195 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1196 # Generate an encryption key using random(4)
1197 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1198 /dev/random "$bootpool/$zroot_key" \
1199 "bs=4096 count=1" || return $FAILURE
1200 f_eval_catch $funcname chmod "$CHMOD_MODE" \
1201 go-wrx "$bootpool/$zroot_key" ||
1208 # Create the geli(8) GEOMS
1210 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1212 # Load the AES-NI kernel module to accelerate encryption
1214 f_eval_catch -d $funcname kldload "$KLDLOAD" "aesni"
1215 # Prompt user for password (twice)
1216 if ! msg_enter_new_password="$msg_geli_password" \
1217 f_dialog_input_password
1219 f_dprintf "$funcname: User cancelled"
1220 f_show_err "$msg_user_cancelled"
1224 # Initialize geli(8) on each of the target partitions
1225 for disk in $disks; do
1226 f_dialog_info "$msg_geli_setup" \
1227 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1228 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1229 if ! echo "$pw_password" | f_eval_catch \
1230 $funcname geli "$GELI_PASSWORD_INIT" \
1231 "$bootpool/boot/$disk$targetpart.eli" \
1232 AES-XTS "$bootpool/$zroot_key" \
1235 f_interactive || f_die
1236 unset pw_password # Sensitive info
1239 if ! echo "$pw_password" | f_eval_catch \
1240 $funcname geli "$GELI_ATTACH" \
1241 "$bootpool/$zroot_key" $disk$targetpart
1243 f_interactive || f_die
1244 unset pw_password # Sensitive info
1248 # With no bootpool, there is no place to store
1249 # the key files, use only a password
1250 if ! echo "$pw_password" | f_eval_catch \
1252 "$GELI_PASSWORD_GELIBOOT_INIT" \
1253 AES-XTS $disk$targetpart
1255 f_interactive || f_die
1256 unset pw_password # Sensitive info
1259 if ! echo "$pw_password" | f_eval_catch \
1260 $funcname geli "$GELI_ATTACH_NOKEY" \
1263 f_interactive || f_die
1264 unset pw_password # Sensitive info
1269 unset pw_password # Sensitive info
1272 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1274 f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
1277 f_eval_catch -d $funcname umount "$UMOUNT" "$BSDINSTALL_CHROOT"
1281 # Create the gmirror(8) GEOMS for swap
1283 if [ ${swapsize:-0} -gt 0 -a "$ZFSBOOT_SWAP_MIRROR" ]; then
1284 for disk in $disks; do
1285 swap_devs="$swap_devs $disk$swappart"
1287 f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
1288 "$swap_devs" || return $FAILURE
1292 # Create the ZFS root pool with desired type and disk devices
1294 f_dprintf "$funcname: Creating root pool..."
1295 create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1296 if [ "$zroot_vdevtype" == "raid10" ]; then
1298 for vdev in $zroot_vdevs; do
1299 f_count nvdev $raid10_vdevs
1300 if [ $(( $nvdev % 3 )) -eq 0 ]; then
1301 raid10_vdevs="$raid10_vdevs mirror"
1303 raid10_vdevs="$raid10_vdevs $vdev"
1305 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1306 "-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1307 "$zroot_name" "" "$raid10_vdevs" || return $FAILURE
1309 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1310 "-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1311 "$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
1316 # Create ZFS dataset layout within the new root pool
1318 f_dprintf "$funcname: Creating ZFS datasets..."
1319 echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1320 # Skip blank lines and comments
1321 case "$dataset" in "#"*|"") continue; esac
1322 # Remove potential inline comments in options
1323 options="${options%%#*}"
1324 # Replace tabs with spaces
1325 f_replaceall "$options" " " " " options
1326 # Reduce contiguous runs of space to one single space
1328 while [ "$oldoptions" != "$options" ]; do
1329 oldoptions="$options"
1330 f_replaceall "$options" " " " " options
1332 # Replace both commas and spaces with ` -o '
1333 f_replaceall "$options" "[ ,]" " -o " options
1334 # Create the dataset with desired options
1335 f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
1336 "${options:+-o $options}" "$zroot_name$dataset" ||
1341 # Set a mountpoint for the root of the pool so newly created datasets
1342 # have a mountpoint to inherit
1344 f_dprintf "$funcname: Setting mountpoint for root of the pool..."
1345 f_eval_catch $funcname zfs "$ZFS_SET" \
1346 "mountpoint=/$zroot_name" "$zroot_name" || return $FAILURE
1348 # Touch up permissions on the tmp directories
1349 f_dprintf "$funcname: Modifying directory permissions..."
1351 for dir in /tmp /var/tmp; do
1352 f_eval_catch $funcname mkdir "$MKDIR_P" \
1353 $BSDINSTALL_CHROOT$dir || return $FAILURE
1354 f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1355 $BSDINSTALL_CHROOT$dir || return $FAILURE
1358 # Set bootfs property
1359 local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1360 f_dprintf "$funcname: Setting bootfs property..."
1361 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1362 "bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
1365 # MBR boot loader touch-up
1366 if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1367 # Export the pool(s)
1368 f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
1369 f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1371 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1372 f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
1373 "$bootpool_name" || return $FAILURE
1376 f_dprintf "$funcname: Updating MBR boot loader on disks..."
1377 # Stick the ZFS boot loader in the "convenient hole" after
1378 # the ZFS internal metadata
1379 for disk in $disks; do
1380 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1381 /boot/zfsboot /dev/$disk$bootpart \
1382 "skip=1 seek=1024" || return $FAILURE
1385 # Re-import the ZFS pool(s)
1386 f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1387 f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1388 "-o altroot=\"$BSDINSTALL_CHROOT\"" \
1389 "$zroot_name" || return $FAILURE
1390 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1391 # Import the bootpool, but do not mount it yet
1392 f_eval_catch $funcname zpool \
1393 "$ZPOOL_IMPORT_WITH_OPTIONS" \
1394 "-o altroot=\"$BSDINSTALL_CHROOT\" -N" \
1395 "$bootpool_name" || return $FAILURE
1399 # Remount bootpool and create symlink(s)
1400 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1401 f_eval_catch $funcname zfs "$ZFS_MOUNT" "$bootpool_name" ||
1403 f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1404 f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1405 $BSDINSTALL_CHROOT/boot || return $FAILURE
1408 # zpool.cache is required to mount more than one pool at boot time
1409 f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1410 f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1412 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1413 "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1414 "$zroot_name" || return $FAILURE
1416 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1417 f_eval_catch $funcname printf "$PRINTF_CONF" \
1418 vfs.root.mountfrom \
1419 "\"zfs:$zroot_name/$zroot_bootfs\"" \
1420 $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
1423 # Set canmount=noauto so that the default Boot Environment (BE) does
1424 # not get mounted if a different BE is selected from the beastie menu
1426 f_dprintf "$funcname: Set canmount=noauto for the root of the pool..."
1427 f_eval_catch $funcname zfs "$ZFS_SET" "canmount=noauto" \
1428 "$zroot_name/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1430 # Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1431 # NOTE: We later concatenate these into their destination
1432 f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1434 f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1435 $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1436 f_eval_catch $funcname echo "$ECHO_APPEND" \
1437 'kern.geom.label.disk_ident.enable=\"0\"' \
1438 $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1439 f_eval_catch $funcname echo "$ECHO_APPEND" \
1440 'kern.geom.label.gptid.enable=\"0\"' \
1441 $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1443 if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1444 f_eval_catch $funcname echo "$ECHO_APPEND" \
1445 'vfs.zfs.min_auto_ashift=12' \
1446 $BSDINSTALL_TMPETC/sysctl.conf.zfs || return $FAILURE
1449 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1450 f_eval_catch $funcname echo "$ECHO_APPEND" \
1451 'geom_mirror_load=\"YES\"' \
1452 $BSDINSTALL_TMPBOOT/loader.conf.gmirror ||
1456 # We're all done unless we should go on to do encryption
1457 [ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
1460 # Configure geli(8)-based encryption
1462 f_dprintf "$funcname: Configuring disk encryption..."
1463 f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1464 $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1465 f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1466 $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1468 # We're all done unless we should go on for boot pool
1469 [ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1471 for disk in $disks; do
1472 f_eval_catch $funcname printf "$PRINTF_CONF" \
1473 geli_%s_keyfile0_load "$disk$targetpart YES" \
1474 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1476 f_eval_catch $funcname printf "$PRINTF_CONF" \
1477 geli_%s_keyfile0_type \
1478 "$disk$targetpart $disk$targetpart:geli_keyfile0" \
1479 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1481 f_eval_catch $funcname printf "$PRINTF_CONF" \
1482 geli_%s_keyfile0_name \
1483 "$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1484 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1488 # Set cachefile for boot pool so it auto-imports at system start
1489 f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1490 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1491 "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1492 "$bootpool_name" || return $FAILURE
1494 # Some additional geli(8) requirements for loader.conf(5)
1496 'zpool_cache_load=\"YES\"' \
1497 'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1498 'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1499 'geom_eli_passphrase_prompt=\"YES\"' \
1501 f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1502 $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1508 # dialog_menu_diskinfo
1510 # Prompt the user to select a disk and then provide detailed info on it.
1512 dialog_menu_diskinfo()
1517 # Break from loop when user cancels disk selection
1520 device=$( msg_cancel="$msg_back" f_device_menu \
1521 "$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1522 $DEVICE_TYPE_DISK 2>&1 ) || break
1523 $device get name disk
1525 # Show gpart(8) `show' and camcontrol(8) `inquiry' data
1526 f_show_msg "$msg_detailed_disk_info" \
1527 "$disk" "$( gpart show $disk 2> /dev/null )" \
1528 "$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1529 "$disk" "$( camcontrol identify $disk 2> /dev/null )"
1535 ############################################################ MAIN
1540 f_dialog_title "$msg_zfs_configuration"
1541 f_dialog_backtitle "$msg_freebsd_installer"
1543 # User may have specifically requested ZFS-related operations be interactive
1544 ! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1549 f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1550 f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1551 f_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1554 # Determine default boot type
1558 # We support only UEFI boot for arm64
1559 : ${ZFSBOOT_BOOT_TYPE:=UEFI}
1560 : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1563 # If the system was booted with UEFI, set the default boot type to UEFI
1564 bootmethod=$( sysctl -n machdep.bootmethod )
1565 f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
1566 if [ "$bootmethod" = "UEFI" ]; then
1567 : ${ZFSBOOT_BOOT_TYPE:=BIOS+UEFI}
1568 : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1570 : ${ZFSBOOT_BOOT_TYPE:=BIOS}
1571 : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
1577 # Loop over the main menu until we've accomplished what we came here to do
1580 if ! f_interactive; then
1582 mtag=">>> $msg_install"
1586 f_dialog_menutag_fetch mtag
1589 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1590 [ $retval -eq $DIALOG_OK ] || f_die
1595 # First, validate the user's selections
1598 # Make sure they gave us a name for the pool
1599 if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1600 f_dprintf "Pool name cannot be empty."
1601 f_show_err "$msg_pool_name_cannot_be_empty"
1605 # Validate vdev type against number of disks selected/scripted
1606 # (also validates that ZFSBOOT_DISKS are real [probed] disks)
1607 # NB: dialog_menu_layout supports running non-interactively
1608 dialog_menu_layout || continue
1610 # Make sure each disk will have room for ZFS
1611 if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
1612 f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize &&
1613 f_expand_number "1g" zpoolmin
1615 minsize=$(( $swapsize + $zpoolmin )) teeny_disks=
1616 [ "$ZFSBOOT_BOOT_POOL" ] &&
1617 minsize=$(( $minsize + $bootsize ))
1618 for disk in $ZFSBOOT_DISKS; do
1619 debug= f_device_find -1 \
1620 $disk $DEVICE_TYPE_DISK device
1621 $device get capacity disksize || continue
1622 [ ${disksize:-0} -ge 0 ] || disksize=0
1623 [ $disksize -lt $minsize ] &&
1624 teeny_disks="$teeny_disks $disk"
1626 if [ "$teeny_disks" ]; then
1627 f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1628 "$ZFSBOOT_SWAP_SIZE" \
1629 "$ZFSBOOT_BOOT_POOL_SIZE" \
1630 "minsize=[$minsize]"
1631 f_dprintf "These disks are too small: %s" \
1633 f_show_err "$msg_these_disks_are_too_small" \
1634 "$ZFSBOOT_SWAP_SIZE" \
1635 "$ZFSBOOT_BOOT_POOL_SIZE" \
1644 if f_interactive; then
1645 dialog_last_chance $ZFSBOOT_DISKS || continue
1652 vdev_type="$ZFSBOOT_VDEV_TYPE"
1654 # Blank the vdev type for the default layout
1655 [ "$vdev_type" = "stripe" ] && vdev_type=
1657 zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1658 "$vdev_type" $ZFSBOOT_DISKS || continue
1660 # To be reused by bootconfig
1661 echo "$ZFSBOOT_DISKS" > ${TMPDIR:-"/tmp"}/bsdinstall-zfsboot
1665 ?" $msg_pool_type_disks")
1666 ZFSBOOT_CONFIRM_LAYOUT=1
1668 # User has poked settings, disable later confirmation
1669 ZFSBOOT_CONFIRM_LAYOUT=
1671 "- $msg_rescan_devices") f_device_rescan ;;
1672 "- $msg_disk_info") dialog_menu_diskinfo ;;
1674 # Prompt the user to input/change the name for the new pool
1675 f_dialog_input input \
1676 "$msg_please_enter_a_name_for_your_zpool" \
1677 "$ZFSBOOT_POOL_NAME" &&
1678 ZFSBOOT_POOL_NAME="$input"
1680 ?" $msg_force_4k_sectors")
1681 # Toggle the variable referenced both by the menu and later
1682 if [ "$ZFSBOOT_FORCE_4K_SECTORS" ]; then
1683 ZFSBOOT_FORCE_4K_SECTORS=
1685 ZFSBOOT_FORCE_4K_SECTORS=1
1688 ?" $msg_encrypt_disks")
1689 # Toggle the variable referenced both by the menu and later
1690 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1691 ZFSBOOT_GELI_ENCRYPTION=
1693 ZFSBOOT_FORCE_4K_SECTORS=1
1694 ZFSBOOT_GELI_ENCRYPTION=1
1697 ?" $msg_partition_scheme")
1698 # Toggle between GPT (BIOS), GPT (UEFI) and MBR
1699 if [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1700 "$ZFSBOOT_BOOT_TYPE" = "BIOS" ]
1702 ZFSBOOT_PARTITION_SCHEME="GPT"
1703 ZFSBOOT_BOOT_TYPE="UEFI"
1704 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" -a \
1705 "$ZFSBOOT_BOOT_TYPE" = "UEFI" ]
1707 ZFSBOOT_PARTITION_SCHEME="GPT"
1708 ZFSBOOT_BOOT_TYPE="BIOS+UEFI"
1709 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT" ]; then
1710 ZFSBOOT_PARTITION_SCHEME="MBR"
1711 ZFSBOOT_BOOT_TYPE="BIOS"
1712 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1713 ZFSBOOT_PARTITION_SCHEME="GPT + Active"
1714 ZFSBOOT_BOOT_TYPE="BIOS"
1715 elif [ "$ZFSBOOT_PARTITION_SCHEME" = "GPT + Active" ]; then
1716 ZFSBOOT_PARTITION_SCHEME="GPT + Lenovo Fix"
1717 ZFSBOOT_BOOT_TYPE="BIOS"
1719 ZFSBOOT_PARTITION_SCHEME="GPT"
1720 ZFSBOOT_BOOT_TYPE="BIOS"
1724 # Prompt the user to input/change the swap size for each disk
1726 f_dialog_input input \
1727 "$msg_please_enter_amount_of_swap_space" \
1728 "$ZFSBOOT_SWAP_SIZE" &&
1729 ZFSBOOT_SWAP_SIZE="${input:-0}"
1730 if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize
1732 if [ $swapsize -ne 0 -a $swapsize -lt 104857600 ]; then
1733 f_show_err "$msg_swap_toosmall" \
1734 "$ZFSBOOT_SWAP_SIZE"
1740 f_show_err "$msg_swap_invalid" \
1741 "$ZFSBOOT_SWAP_SIZE"
1746 ?" $msg_swap_mirror")
1747 # Toggle the variable referenced both by the menu and later
1748 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1749 ZFSBOOT_SWAP_MIRROR=
1751 ZFSBOOT_SWAP_MIRROR=1
1754 ?" $msg_swap_encrypt")
1755 # Toggle the variable referenced both by the menu and later
1756 if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1757 ZFSBOOT_SWAP_ENCRYPTION=
1759 ZFSBOOT_SWAP_ENCRYPTION=1
1767 ################################################################################
1769 ################################################################################