3 # Copyright (c) 2013 Allan Jude
4 # Copyright (c) 2013 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 primany boot environment
60 : ${ZFSBOOT_BOOTFS_NAME:=default}
63 # Default Virtual Device (vdev) type to create
65 : ${ZFSBOOT_VDEV_TYPE:=stripe}
68 # Should we use gnop(8) to configure a transparent mapping to 4K sectors?
70 : ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=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:=GPT}
115 # How much swap to put on each block device in the boot zpool
116 # NOTE: Value passed to gpart(8); which supports SI unit suffixes.
118 : ${ZFSBOOT_SWAP_SIZE:=2g}
121 # Should we use geli(8) to encrypt the swap?
123 : ${ZFSBOOT_SWAP_ENCRYPTION=}
126 # Should we use gmirror(8) to mirror the swap?
128 : ${ZFSBOOT_SWAP_MIRROR=}
131 # Default ZFS datasets for root zpool
133 # NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
134 # NOTE: Anything after pound/hash character [#] is ignored as a comment.
136 f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
137 # DATASET OPTIONS (comma or space separated; or both)
139 # Boot Environment [BE] root and default boot dataset
140 /$ZFSBOOT_BEROOT_NAME mountpoint=none
141 /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME mountpoint=/
143 # Compress /tmp, allow exec but not setuid
144 /tmp mountpoint=/tmp,exec=on,setuid=off
146 # Don't mount /usr so that 'base' files go to the BEROOT
147 /usr mountpoint=/usr,canmount=off
149 # Home directories separated so they are common to all BEs
150 /usr/home # NB: /home is a symlink to /usr/home
153 /usr/ports setuid=off
155 # Source tree (compressed)
158 # Create /var and friends
159 /var mountpoint=/var,canmount=off
160 /var/crash exec=off,setuid=off
161 /var/log exec=off,setuid=off
167 # If interactive and the user has not explicitly chosen a vdev type or disks,
168 # make the user confirm scripted/default choices when proceeding to install.
170 : ${ZFSBOOT_CONFIRM_LAYOUT:=1}
172 ############################################################ GLOBALS
175 # Format of a line in printf(1) syntax to add to fstab(5)
177 FSTAB_FMT="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
180 # Command strings for various tasks
182 CHMOD_MODE='chmod %s "%s"'
183 DD_WITH_OPTIONS='dd if="%s" of="%s" %s'
184 ECHO_APPEND='echo "%s" >> "%s"'
185 GELI_ATTACH='geli attach -j - -k "%s" "%s"'
186 GELI_DETACH_F='geli detach -f "%s"'
187 GELI_PASSWORD_INIT='geli init -b -B "%s" -e %s -J - -K "%s" -l 256 -s 4096 "%s"'
188 GNOP_CREATE='gnop create -S 4096 "%s"'
189 GNOP_DESTROY='gnop destroy "%s"'
190 GPART_ADD='gpart add -t %s "%s"'
191 GPART_ADD_INDEX='gpart add -i %s -t %s "%s"'
192 GPART_ADD_INDEX_WITH_SIZE='gpart add -i %s -t %s -s %s "%s"'
193 GPART_ADD_LABEL='gpart add -l %s -t %s "%s"'
194 GPART_ADD_LABEL_WITH_SIZE='gpart add -l %s -t %s -s %s "%s"'
195 GPART_BOOTCODE='gpart bootcode -b "%s" "%s"'
196 GPART_BOOTCODE_PART='gpart bootcode -b "%s" -p "%s" -i %s "%s"'
197 GPART_CREATE='gpart create -s %s "%s"'
198 GPART_DESTROY_F='gpart destroy -F "%s"'
199 GPART_SET_ACTIVE='gpart set -a active -i %s "%s"'
200 GRAID_DELETE='graid delete "%s"'
201 LN_SF='ln -sf "%s" "%s"'
202 MKDIR_P='mkdir -p "%s"'
203 MOUNT_TYPE='mount -t %s "%s" "%s"'
204 PRINTF_CONF="printf '%s=\"%%s\"\\\n' %s >> \"%s\""
205 PRINTF_FSTAB='printf "$FSTAB_FMT" "%s" "%s" "%s" "%s" "%s" "%s" >> "%s"'
206 SHELL_TRUNCATE=':> "%s"'
207 SWAP_GMIRROR_LABEL='gmirror label swap %s'
209 ZFS_CREATE_WITH_OPTIONS='zfs create %s "%s"'
210 ZFS_SET='zfs set "%s" "%s"'
211 ZFS_UNMOUNT='zfs unmount "%s"'
212 ZPOOL_CREATE_WITH_OPTIONS='zpool create %s "%s" %s %s'
213 ZPOOL_DESTROY='zpool destroy "%s"'
214 ZPOOL_EXPORT='zpool export "%s"'
215 ZPOOL_IMPORT_WITH_OPTIONS='zpool import %s "%s"'
216 ZPOOL_LABELCLEAR_F='zpool labelclear -f "%s"'
217 ZPOOL_SET='zpool set %s "%s"'
220 # Strings that should be moved to an i18n file and loaded with f_include_lang()
222 hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
223 hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
224 hline_arrows_tab_enter="Press arrows, TAB or ENTER"
225 msg_an_unknown_error_occurred="An unknown error occurred"
228 msg_change_selection="Change Selection"
229 msg_configure_options="Configure Options:"
230 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"
231 msg_disk_info="Disk Info"
232 msg_disk_info_help="Get detailed information on disk device(s)"
233 msg_disk_singular="disk"
234 msg_disk_plural="disks"
235 msg_encrypt_disks="Encrypt Disks?"
236 msg_encrypt_disks_help="Use geli(8) to encrypt all data partitions"
238 msg_force_4k_sectors="Force 4K Sectors?"
239 msg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
240 msg_freebsd_installer="FreeBSD Installer"
241 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"
242 msg_geli_setup="Initializing encryption on selected disks,\n this will take several seconds per disk"
243 msg_install="Install"
244 msg_install_desc="Proceed with Installation"
245 msg_install_help="Create ZFS boot pool with displayed options"
246 msg_invalid_boot_pool_size="Invalid boot pool size \`%s'"
247 msg_invalid_disk_argument="Invalid disk argument \`%s'"
248 msg_invalid_index_argument="Invalid index argument \`%s'"
249 msg_invalid_swap_size="Invalid swap size \`%s'"
250 msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
251 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"
252 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'
253 msg_mirror_desc="Mirror - n-Way Mirroring"
254 msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
255 msg_missing_disk_arguments="missing disk arguments"
256 msg_missing_one_or_more_scripted_disks="Missing one or more scripted disks!"
258 msg_no_disks_present_to_configure="No disk(s) present to configure"
259 msg_no_disks_selected="No disks selected."
260 msg_not_enough_disks_selected="Not enough disks selected. (%u < %u minimum)"
261 msg_null_disk_argument="NULL disk argument"
262 msg_null_index_argument="NULL index argument"
263 msg_null_poolname="NULL poolname"
265 msg_partition_scheme="Partition Scheme"
266 msg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
267 msg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
268 msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
269 msg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
270 msg_pool_name="Pool Name"
271 msg_pool_name_cannot_be_empty="Pool name cannot be empty."
272 msg_pool_name_help="Customize the name of the zpool to be created (Required)"
273 msg_pool_type_disks="Pool Type/Disks:"
274 msg_pool_type_disks_help="Choose type of ZFS Virtual Device and disks to use (Required)"
275 msg_processing_selection="Processing selection..."
276 msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
277 msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
278 msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
279 msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
280 msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
281 msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
282 msg_rescan_devices="Rescan Devices"
283 msg_rescan_devices_help="Scan for device changes"
285 msg_select_a_disk_device="Select a disk device"
286 msg_select_virtual_device_type="Select Virtual Device type:"
287 msg_stripe_desc="Stripe - No Redundancy"
288 msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
289 msg_swap_encrypt="Encrypt Swap?"
290 msg_swap_encrypt_help="Encrypt swap partitions with temporary keys, discarded on reboot"
291 msg_swap_mirror="Mirror Swap?"
292 msg_swap_mirror_help="Mirror swap partitions for redundancy, breaks crash dumps"
293 msg_swap_size="Swap Size"
294 msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
295 msg_these_disks_are_too_small="These disks are too small given the amount of requested\nswap (%s) and/or geli(8) (%s) partitions, which would\ntake 50%% or more of each of the following selected disk\ndevices (not recommended):\n\n %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of devices."
296 msg_uefi_not_supported="The FreeBSD UEFI loader does not currently support booting root-on-ZFS. Your system will need to boot in legacy (CSM) mode.\nDo you want to continue?"
297 msg_unable_to_get_disk_capacity="Unable to get disk capacity of \`%s'"
298 msg_unsupported_partition_scheme="%s is an unsupported partition scheme"
299 msg_user_cancelled="User Cancelled."
301 msg_zfs_configuration="ZFS Configuration"
303 ############################################################ FUNCTIONS
307 # Display the dialog(1)-based application main menu.
311 local title="$DIALOG_TITLE"
312 local btitle="$DIALOG_BACKTITLE"
313 local prompt="$msg_configure_options"
314 local force4k="$msg_no"
315 local usegeli="$msg_no"
316 local swapgeli="$msg_no"
317 local swapmirror="$msg_no"
318 [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
319 [ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
320 [ "$ZFSBOOT_SWAP_ENCRYPTION" ] && swapgeli="$msg_yes"
321 [ "$ZFSBOOT_SWAP_MIRROR" ] && swapmirror="$msg_yes"
322 local disks n disks_grammar
323 f_count n $ZFSBOOT_DISKS
324 { [ $n -eq 1 ] && disks_grammar=$msg_disk_singular; } ||
325 disks_grammar=$msg_disk_plural # grammar
327 '>>> $msg_install' '$msg_install_desc'
329 'T $msg_pool_type_disks' '$ZFSBOOT_VDEV_TYPE: $n $disks_grammar'
330 '$msg_pool_type_disks_help'
331 '- $msg_rescan_devices' '*'
332 '$msg_rescan_devices_help'
333 '- $msg_disk_info' '*'
334 '$msg_disk_info_help'
335 'N $msg_pool_name' '$ZFSBOOT_POOL_NAME'
336 '$msg_pool_name_help'
337 '4 $msg_force_4k_sectors' '$force4k'
338 '$msg_force_4k_sectors_help'
339 'E $msg_encrypt_disks' '$usegeli'
340 '$msg_encrypt_disks_help'
341 'P $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
342 '$msg_partition_scheme_help'
343 'S $msg_swap_size' '$ZFSBOOT_SWAP_SIZE'
344 '$msg_swap_size_help'
345 'M $msg_swap_mirror' '$swapmirror'
346 '$msg_swap_mirror_help'
347 'W $msg_swap_encrypt' '$swapgeli'
348 '$msg_swap_encrypt_help'
350 local defaultitem= # Calculated below
351 local hline="$hline_alnum_arrows_punc_tab_enter"
353 local height width rows
354 eval f_dialog_menu_with_help_size height width rows \
355 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
357 # Obtain default-item from previously stored selection
358 f_dialog_default_fetch defaultitem
361 menu_choice=$( eval $DIALOG \
362 --title \"\$title\" \
363 --backtitle \"\$btitle\" \
364 --hline \"\$hline\" \
366 --ok-label \"\$msg_select\" \
367 --cancel-label \"\$msg_cancel\" \
368 --default-item \"\$defaultitem\" \
369 --menu \"\$prompt\" \
370 $height $width $rows \
372 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
375 f_dialog_data_sanitize menu_choice
376 f_dialog_menutag_store "$menu_choice"
378 # Only update default-item on success
379 [ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
384 # dialog_last_chance $disks ...
386 # Display a list of the disks that the user is about to destroy. The default
387 # action is to return error status unless the user explicitly (non-default)
388 # selects "Yes" from the noyes dialog.
392 local title="$DIALOG_TITLE"
393 local btitle="$DIALOG_BACKTITLE"
394 local prompt # Calculated below
395 local hline="$hline_arrows_tab_enter"
397 local height=8 width=50 prefix=" "
398 local plen=${#prefix} list= line=
399 local max_width=$(( $width - 3 - $plen ))
401 local yes no defaultno extra_args format
402 if [ "$USE_XDIALOG" ]; then
403 yes=ok no=cancel defaultno=default-no
404 extra_args="--wrap --left"
405 format="$msg_last_chance_are_you_sure"
407 yes=yes no=no defaultno=defaultno
408 extra_args="--colors --cr-wrap"
409 format="$msg_last_chance_are_you_sure_color"
412 local disk line_width
419 line_width=$(( $line_width + 1 + ${#disk} ))
420 # Add newline before disk if it would exceed max_width
421 if [ $line_width -gt $max_width ]; then
424 height=$(( $height + 1 ))
426 # Add the disk to the list
429 # Append the left-overs
430 if [ "${line#$prefix}" ]; then
432 height=$(( $height + 1 ))
435 # Add height for Xdialog(1)
436 [ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
438 prompt=$( printf "$format" "$list" )
439 f_dprintf "%s: Last Chance!" "$0"
442 --backtitle "$btitle" \
445 --$yes-label "$msg_yes" \
446 --$no-label "$msg_no" \
448 --yesno "$prompt" $height $width
453 # Configure Virtual Device type and disks to use for the ZFS boot pool. User
454 # must select enough disks to satisfy the chosen vdev type.
458 local funcname=dialog_menu_layout
459 local title="$DIALOG_TITLE"
460 local btitle="$DIALOG_BACKTITLE"
461 local vdev_prompt="$msg_select_virtual_device_type"
462 local disk_prompt="$msg_please_select_one_or_more_disks"
463 local vdev_menu_list="
464 'stripe' '$msg_stripe_desc' '$msg_stripe_help'
465 'mirror' '$msg_mirror_desc' '$msg_mirror_help'
466 'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
467 'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
468 'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
470 local disk_check_list= # Calculated below
471 local vdev_hline="$hline_arrows_tab_enter"
472 local disk_hline="$hline_arrows_space_tab_enter"
474 # Warn the user if vdev type is not valid
475 case "$ZFSBOOT_VDEV_TYPE" in
476 stripe|mirror|raidz1|raidz2|raidz3) : known good ;;
478 f_dprintf "%s: Invalid virtual device type \`%s'" \
479 $funcname "$ZFSBOOT_VDEV_TYPE"
480 f_show_err "$msg_invalid_virtual_device_type" \
482 f_interactive || return $FAILURE
485 # Calculate size of vdev menu once only
486 local vheight vwidth vrows
487 eval f_dialog_menu_with_help_size vheight vwidth vrows \
488 \"\$title\" \"\$btitle\" \"\$vdev_prompt\" \"\$vdev_hline\" \
491 # Get a list of probed disk devices
493 debug= f_device_find "" $DEVICE_TYPE_DISK disks
495 # Prune out mounted md(4) devices that may be part of the boot process
496 local disk name new_list=
497 for disk in $disks; do
498 debug= $disk get name name
500 md[0-9]*) f_mounted -b "/dev/$name" && continue ;;
502 new_list="$new_list $disk"
504 disks="${new_list# }"
507 if [ "$debug" ]; then
509 for disk in $disks; do
510 debug= $disk get name name
511 disk_names="$disk_names $name"
513 f_dprintf "$funcname: disks=[%s]" "${disk_names# }"
516 if [ ! "$disks" ]; then
517 f_dprintf "No disk(s) present to configure"
518 f_show_err "$msg_no_disks_present_to_configure"
522 # Lets sort the disks array to be more user friendly
523 f_device_sort_by name disks disks
526 # Operate in a loop so we can (if interactive) repeat if not enough
527 # disks are selected to satisfy the chosen vdev type or user wants to
528 # back-up to the previous menu.
530 local vardisk ndisks onoff selections vdev_choice breakout device
531 local valid_disks all_valid want_disks desc height width rows
534 # Confirm the vdev type that was selected
536 if f_interactive && [ "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
537 vdev_choice=$( eval $DIALOG \
538 --title \"\$title\" \
539 --backtitle \"\$btitle\" \
540 --hline \"\$vdev_hline\" \
541 --ok-label \"\$msg_ok\" \
542 --cancel-label \"\$msg_cancel\" \
544 --default-item \"\$ZFSBOOT_VDEV_TYPE\" \
545 --menu \"\$vdev_prompt\" \
546 $vheight $vwidth $vrows \
548 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
550 # Exit if user pressed ESC or chose Cancel/No
551 f_dialog_data_sanitize vdev_choice
553 ZFSBOOT_VDEV_TYPE="$vdev_choice"
554 f_dprintf "$funcname: ZFSBOOT_VDEV_TYPE=[%s]" \
558 # Determine the number of disks needed for this vdev type
560 case "$ZFSBOOT_VDEV_TYPE" in
561 stripe) want_disks=1 ;;
562 mirror) want_disks=2 ;;
563 raidz1) want_disks=3 ;;
564 raidz2) want_disks=4 ;;
565 raidz3) want_disks=5 ;;
569 # Warn the user if any scripted disks are invalid
571 valid_disks= all_valid=${ZFSBOOT_DISKS:+1} # optimism
572 for disk in $ZFSBOOT_DISKS; do
573 if debug= f_device_find -1 \
574 $disk $DEVICE_TYPE_DISK device
576 valid_disks="$valid_disks $disk"
579 f_dprintf "$funcname: \`%s' is not a real disk" "$disk"
582 if [ ! "$all_valid" ]; then
583 if [ "$ZFSBOOT_DISKS" ]; then
585 "$msg_missing_one_or_more_scripted_disks"
587 f_dprintf "No disks selected."
589 f_show_err "$msg_no_disks_selected"
591 f_interactive || return $FAILURE
593 ZFSBOOT_DISKS="${valid_disks# }"
596 # Short-circuit if we're running non-interactively
598 if ! f_interactive || [ ! "$ZFSBOOT_CONFIRM_LAYOUT" ]; then
599 f_count ndisks $ZFSBOOT_DISKS
600 [ $ndisks -ge $want_disks ] && break # to success
602 # Not enough disks selected
603 f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
604 "$ZFSBOOT_VDEV_TYPE" \
605 "Not enough disks selected." \
607 f_interactive || return $FAILURE
608 msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
609 f_yesno "%s: $msg_not_enough_disks_selected" \
610 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
615 # Confirm the disks that were selected
616 # Loop until the user cancels or selects enough disks
620 # Loop over list of available disks, resetting state
621 for disk in $disks; do
622 f_isset _${disk}_status && _${disk}_status=
625 # Loop over list of selected disks and create temporary
626 # locals to map statuses onto up-to-date list of disks
627 for disk in $ZFSBOOT_DISKS; do
628 debug= f_device_find -1 \
629 $disk $DEVICE_TYPE_DISK disk
630 f_isset _${disk}_status ||
631 local _${disk}_status
635 # Create the checklist menu of discovered disk devices
637 for disk in $disks; do
641 f_shell_escape "$desc" desc
642 f_getvar _${disk}_status:-off onoff
643 disk_check_list="$disk_check_list
644 $name '$desc' $onoff"
647 eval f_dialog_checklist_size height width rows \
648 \"\$title\" \"\$btitle\" \"\$prompt\" \
649 \"\$hline\" $disk_check_list
651 selections=$( eval $DIALOG \
652 --title \"\$DIALOG_TITLE\" \
653 --backtitle \"\$DIALOG_BACKTITLE\" \
655 --hline \"\$hline\" \
656 --ok-label \"\$msg_ok\" \
657 --cancel-label \"\$msg_back\" \
658 --checklist \"\$prompt\" \
659 $height $width $rows \
661 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
663 # Loop if user pressed ESC or chose Cancel/No
664 f_dialog_data_sanitize selections
666 ZFSBOOT_DISKS="$selections"
667 f_dprintf "$funcname: ZFSBOOT_DISKS=[%s]" \
670 f_count ndisks $ZFSBOOT_DISKS
671 [ $ndisks -ge $want_disks ] &&
672 breakout=break && break
674 # Not enough disks selected
675 f_dprintf "$funcname: %s: %s (%u < %u minimum)" \
676 "$ZFSBOOT_VDEV_TYPE" \
677 "Not enough disks selected." \
679 msg_yes="$msg_change_selection" msg_no="$msg_cancel" \
680 f_yesno "%s: $msg_not_enough_disks_selected" \
681 "$ZFSBOOT_VDEV_TYPE" $ndisks $want_disks ||
684 [ "$breakout" = "break" ] && break
685 [ "$ZFSBOOT_CONFIRM_LAYOUT" ] || return $FAILURE
693 # Confirm that the user wants to continue with the installation on a BIOS
694 # system when they have booted with UEFI
698 local title="$DIALOG_TITLE"
699 local btitle="$DIALOG_BACKTITLE"
700 local prompt # Calculated below
701 local hline="$hline_arrows_tab_enter"
703 local height=8 width=50 prefix=" "
704 local plen=${#prefix} list= line=
705 local max_width=$(( $width - 3 - $plen ))
707 local yes no defaultno extra_args format
708 if [ "$USE_XDIALOG" ]; then
709 yes=ok no=cancel defaultno=default-no
710 extra_args="--wrap --left"
711 format="$msg_uefi_not_supported"
713 yes=yes no=no defaultno=defaultno
714 extra_args="--cr-wrap"
715 format="$msg_uefi_not_supported"
718 # Add height for Xdialog(1)
719 [ "$USE_XDIALOG" ] && height=$(( $height + $height / 5 + 3 ))
721 prompt=$( printf "$format" )
722 f_dprintf "%s: UEFI prompt" "$0"
725 --backtitle "$btitle" \
727 --$yes-label "$msg_yes" \
728 --$no-label "$msg_no" \
730 --yesno "$prompt" $height $width
733 # zfs_create_diskpart $disk $index
735 # For each block device to be used in the zpool, rather than just create the
736 # zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
737 # so we can have some real swap. This also provides wiggle room incase your
738 # replacement drivers do not have the exact same sector counts.
740 # NOTE: $swapsize and $bootsize should be defined by the calling function.
741 # NOTE: Sets $bootpart and $targetpart for the calling function.
743 zfs_create_diskpart()
745 local funcname=zfs_create_diskpart
746 local disk="$1" index="$2"
749 if [ ! "$disk" ]; then
750 f_dprintf "$funcname: NULL disk argument"
751 msg_error="$msg_error: $funcname" \
752 f_show_err "$msg_null_disk_argument"
755 if [ "${disk#*[$IFS]}" != "$disk" ]; then
756 f_dprintf "$funcname: Invalid disk argument \`%s'" "$disk"
757 msg_error="$msg_error: $funcname" \
758 f_show_err "$msg_invalid_disk_argument" "$disk"
761 if [ ! "$index" ]; then
762 f_dprintf "$funcname: NULL index argument"
763 msg_error="$msg_error: $funcname" \
764 f_show_err "$msg_null_index_argument"
767 if ! f_isinteger "$index"; then
768 f_dprintf "$funcname: Invalid index argument \`%s'" "$index"
769 msg_error="$msg_error: $funcname" \
770 f_show_err "$msg_invalid_index_argument" "$index"
773 f_dprintf "$funcname: disk=[%s] index=[%s]" "$disk" "$index"
775 # Check for unknown partition scheme before proceeding further
776 case "$ZFSBOOT_PARTITION_SCHEME" in
777 ""|MBR|GPT) : known good ;;
779 f_dprintf "$funcname: %s is an unsupported partition scheme" \
780 "$ZFSBOOT_PARTITION_SCHEME"
781 msg_error="$msg_error: $funcname" f_show_err \
782 "$msg_unsupported_partition_scheme" \
783 "$ZFSBOOT_PARTITION_SCHEME"
788 # Destroy whatever partition layout is currently on disk.
789 # NOTE: `-F' required to destroy if partitions still exist.
790 # NOTE: Failure is ok here, blank disk will have nothing to destroy.
792 f_dprintf "$funcname: Destroying all data/layouts on \`%s'..." "$disk"
793 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
794 f_eval_catch -d $funcname graid "$GRAID_DELETE" $disk
795 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" /dev/$disk
797 # Make doubly-sure backup GPT is destroyed
798 f_eval_catch -d $funcname gpart "$GPART_CREATE" gpt $disk
799 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" $disk
802 # Enable boot pool if encryption is desired
804 [ "$ZFSBOOT_GELI_ENCRYPTION" ] && ZFSBOOT_BOOT_POOL=1
807 # Lay down the desired type of partition scheme
809 local setsize mbrindex
810 case "$ZFSBOOT_PARTITION_SCHEME" in
811 ""|GPT) f_dprintf "$funcname: Creating GPT layout..."
813 # 1. Create GPT layout using labels
815 f_eval_catch $funcname gpart "$GPART_CREATE" gpt $disk ||
819 # 2. Add small freebsd-boot partition labeled `boot#'
821 f_eval_catch $funcname gpart "$GPART_ADD_LABEL_WITH_SIZE" \
822 gptboot$index freebsd-boot 512k $disk ||
824 f_eval_catch $funcname gpart "$GPART_BOOTCODE_PART" \
825 /boot/pmbr /boot/gptzfsboot 1 $disk ||
828 # NB: zpool will use the `zfs#' GPT labels
829 bootpart=p2 swappart=p2 targetpart=p2
830 [ ${swapsize:-0} -gt 0 ] && targetpart=p3
833 # Prepare boot pool if enabled (e.g., for geli(8))
835 if [ "$ZFSBOOT_BOOT_POOL" ]; then
836 bootpart=p2 swappart=p3 targetpart=p3
837 [ ${swapsize:-0} -gt 0 ] && targetpart=p4
838 f_eval_catch $funcname gpart \
839 "$GPART_ADD_LABEL_WITH_SIZE" boot$index \
840 freebsd-zfs ${bootsize}b $disk ||
842 # Pedantically nuke any old labels
843 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
845 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
846 # Pedantically detach targetpart for later
847 f_eval_catch -d $funcname geli \
849 /dev/$disk$targetpart
854 # 3. Add freebsd-swap partition labeled `swap#'
856 if [ ${swapsize:-0} -gt 0 ]; then
857 f_eval_catch $funcname gpart \
858 "$GPART_ADD_LABEL_WITH_SIZE" swap$index \
859 freebsd-swap ${swapsize}b $disk ||
861 # Pedantically nuke any old labels on the swap
862 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
867 # 4. Add freebsd-zfs partition labeled `zfs#' for zroot
869 f_eval_catch $funcname gpart "$GPART_ADD_LABEL" \
870 zfs$index freebsd-zfs $disk || return $FAILURE
871 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
872 /dev/$disk$targetpart
875 MBR) f_dprintf "$funcname: Creating MBR layout..."
877 # 1. Create MBR layout (no labels)
879 f_eval_catch $funcname gpart "$GPART_CREATE" mbr $disk ||
881 f_eval_catch $funcname gpart "$GPART_BOOTCODE" /boot/mbr \
882 $disk || return $FAILURE
885 # 2. Add freebsd slice with all available space
887 f_eval_catch $funcname gpart "$GPART_ADD" freebsd $disk ||
889 f_eval_catch $funcname gpart "$GPART_SET_ACTIVE" 1 $disk ||
891 # Pedantically nuke any old labels
892 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
894 # Pedantically nuke any old scheme
895 f_eval_catch -d $funcname gpart "$GPART_DESTROY_F" ${disk}s1
898 # 3. Write BSD scheme to the freebsd slice
900 f_eval_catch $funcname gpart "$GPART_CREATE" BSD ${disk}s1 ||
903 # NB: zpool will use s1a (no labels)
904 bootpart=s1a swappart=s1b targetpart=s1d mbrindex=4
907 # Always prepare a boot pool on MBR
910 f_eval_catch $funcname gpart \
911 "$GPART_ADD_INDEX_WITH_SIZE" \
912 1 freebsd-zfs ${bootsize}b ${disk}s1 ||
914 # Pedantically nuke any old labels
915 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
917 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
918 # Pedantically detach targetpart for later
919 f_eval_catch -d $funcname geli \
921 /dev/$disk$targetpart
925 # 4. Add freebsd-swap partition
927 if [ ${swapsize:-0} -gt 0 ]; then
928 f_eval_catch $funcname gpart \
929 "$GPART_ADD_INDEX_WITH_SIZE" 2 \
930 freebsd-swap ${swapsize}b ${disk}s1 ||
932 # Pedantically nuke any old labels on the swap
933 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
938 # 5. Add freebsd-zfs partition for zroot
940 f_eval_catch $funcname gpart "$GPART_ADD_INDEX" \
941 $mbrindex freebsd-zfs ${disk}s1 || return $FAILURE
942 f_eval_catch -d $funcname zpool "$ZPOOL_LABELCLEAR_F" \
943 /dev/$disk$targetpart # Pedantic
944 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
945 /boot/zfsboot /dev/${disk}s1 count=1 ||
949 esac # $ZFSBOOT_PARTITION_SCHEME
952 if [ "$isswapmirror" ]; then
953 # This is not the first disk in the mirror, do nothing
954 elif [ "$ZFSBOOT_SWAP_ENCRYPTION" -a "$ZFSBOOT_SWAP_MIRROR" ]; then
955 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
956 /dev/mirror/swap.eli none swap sw 0 0 \
957 $BSDINSTALL_TMPETC/fstab ||
960 elif [ "$ZFSBOOT_SWAP_MIRROR" ]; then
961 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
962 /dev/mirror/swap none swap sw 0 0 \
963 $BSDINSTALL_TMPETC/fstab ||
966 elif [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
967 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
968 /dev/$disk${swappart}.eli none swap sw 0 0 \
969 $BSDINSTALL_TMPETC/fstab ||
972 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
973 /dev/$disk$swappart none swap sw 0 0 \
974 $BSDINSTALL_TMPETC/fstab ||
981 # zfs_create_boot $poolname $vdev_type $disks ...
983 # Creates boot pool and dataset layout. Returns error if something goes wrong.
984 # Errors are printed to stderr for collection and display.
988 local funcname=zfs_create_boot
989 local zroot_name="$1"
990 local zroot_vdevtype="$2"
991 local zroot_vdevs= # Calculated below
992 local swap_devs= # Calculated below
993 local boot_vdevs= # Used for geli(8) and/or MBR layouts
994 shift 2 # poolname vdev_type
995 local disks="$*" disk
997 local bootpart targetpart swappart # Set by zfs_create_diskpart() below
1001 # Pedantic checks; should never be seen
1003 if [ ! "$zroot_name" ]; then
1004 f_dprintf "$funcname: NULL poolname"
1005 msg_error="$msg_error: $funcname" \
1006 f_show_err "$msg_null_poolname"
1009 if [ $# -lt 1 ]; then
1010 f_dprintf "$funcname: missing disk arguments"
1011 msg_error="$msg_error: $funcname" \
1012 f_show_err "$msg_missing_disk_arguments"
1015 f_dprintf "$funcname: poolname=[%s] vdev_type=[%s]" \
1016 "$zroot_name" "$zroot_vdevtype"
1019 # Initialize fstab(5)
1021 f_dprintf "$funcname: Initializing temporary fstab(5) file..."
1022 f_eval_catch $funcname sh "$SHELL_TRUNCATE" $BSDINSTALL_TMPETC/fstab ||
1024 f_eval_catch $funcname printf "$PRINTF_FSTAB" \
1025 "# Device" Mountpoint FStype Options Dump "Pass#" \
1026 $BSDINSTALL_TMPETC/fstab || return $FAILURE
1029 # Expand SI units in desired sizes
1031 f_dprintf "$funcname: Expanding supplied size values..."
1032 local swapsize bootsize
1033 if ! f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize; then
1034 f_dprintf "$funcname: Invalid swap size \`%s'" \
1035 "$ZFSBOOT_SWAP_SIZE"
1036 f_show_err "$msg_invalid_swap_size" "$ZFSBOOT_SWAP_SIZE"
1039 if ! f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize; then
1040 f_dprintf "$funcname: Invalid boot pool size \`%s'" \
1041 "$ZFSBOOT_BOOT_POOL_SIZE"
1042 f_show_err "$msg_invalid_boot_pool_size" \
1043 "$ZFSBOOT_BOOT_POOL_SIZE"
1046 f_dprintf "$funcname: ZFSBOOT_SWAP_SIZE=[%s] swapsize=[%s]" \
1047 "$ZFSBOOT_SWAP_SIZE" "$swapsize"
1048 f_dprintf "$funcname: ZFSBOOT_BOOT_POOL_SIZE=[%s] bootsize=[%s]" \
1049 "$ZFSBOOT_BOOT_POOL_SIZE" "$bootsize"
1052 # Destroy the pool in-case this is our second time 'round (case of
1053 # failure and installer presented ``Retry'' option to come back).
1055 # NB: If we don't destroy the pool, later gpart(8) destroy commands
1056 # that try to clear existing partitions (see zfs_create_diskpart())
1057 # will fail with a `Device Busy' error, leading to `GEOM exists'.
1059 f_eval_catch -d $funcname zpool "$ZPOOL_DESTROY" "$zroot_name"
1062 # Prepare the disks and build pool device list(s)
1064 f_dprintf "$funcname: Preparing disk partitions for ZFS pool..."
1065 [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] &&
1066 f_dprintf "$funcname: With 4k alignment using gnop(8)..."
1068 for disk in $disks; do
1069 zfs_create_diskpart $disk $n || return $FAILURE
1070 # Now $bootpart, $targetpart, and $swappart are set (suffix
1073 # Forced 4k alignment support using Geom NOP (see gnop(8))
1074 if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1075 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1076 boot_vdevs="$boot_vdevs $disk$bootpart.nop"
1077 f_eval_catch $funcname gnop "$GNOP_CREATE" \
1078 $disk$bootpart || return $FAILURE
1080 # Don't gnop encrypted partition
1081 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1082 zroot_vdevs="$zroot_vdevs $disk$targetpart.eli"
1084 zroot_vdevs="$zroot_vdevs $disk$targetpart.nop"
1085 f_eval_catch $funcname gnop "$GNOP_CREATE" \
1090 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1091 boot_vdevs="$boot_vdevs $disk$bootpart"
1093 zroot_vdevs="$zroot_vdevs $disk$targetpart"
1100 # If we need/want a boot pool, create it
1102 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1103 local bootpool_vdevtype= # Calculated below
1104 local bootpool_options= # Calculated below
1105 local bootpool_name="$ZFSBOOT_BOOT_POOL_NAME"
1106 local bootpool="$BSDINSTALL_CHROOT/$bootpool_name"
1107 local zroot_key="${ZFSBOOT_GELI_KEY_FILE#/}"
1109 f_dprintf "$funcname: Setting up boot pool..."
1110 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
1111 f_dprintf "$funcname: For encrypted root disk..."
1113 # Create parent directory for boot pool
1114 f_eval_catch -d $funcname umount "$UMOUNT" /mnt
1115 f_eval_catch $funcname mount "$MOUNT_TYPE" tmpfs none \
1116 $BSDINSTALL_CHROOT || return $FAILURE
1118 # Create mirror across the boot partition on all disks
1120 f_count nvdevs $boot_vdevs
1121 [ $nvdevs -gt 1 ] && bootpool_vdevtype=mirror
1123 create_options="$ZFSBOOT_BOOT_POOL_CREATE_OPTIONS"
1124 bootpool_options="-o altroot=$BSDINSTALL_CHROOT"
1125 bootpool_options="$bootpool_options $create_options"
1126 bootpool_options="$bootpool_options -m \"/$bootpool_name\" -f"
1127 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1128 "$bootpool_options" "$bootpool_name" \
1129 "$bootpool_vdevtype" "$boot_vdevs" ||
1132 f_eval_catch $funcname mkdir "$MKDIR_P" "$bootpool/boot" ||
1135 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1136 # Generate an encryption key using random(4)
1137 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1138 /dev/random "$bootpool/$zroot_key" \
1139 "bs=4096 count=1" || return $FAILURE
1142 f_eval_catch $funcname zfs "$ZFS_UNMOUNT" \
1143 "$bootpool_name" || return $FAILURE
1144 f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1150 # Create the geli(8) GEOMS
1152 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1153 # Prompt user for password (twice)
1154 if ! msg_enter_new_password="$msg_geli_password" \
1155 f_dialog_input_password
1157 f_dprintf "$funcname: User cancelled"
1158 f_show_err "$msg_user_cancelled"
1162 # Initialize geli(8) on each of the target partitions
1163 for disk in $disks; do
1164 f_dialog_info "$msg_geli_setup" \
1165 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
1166 if ! echo "$pw_password" | f_eval_catch \
1167 $funcname geli "$GELI_PASSWORD_INIT" \
1168 "$bootpool/boot/$disk$targetpart.eli" \
1169 AES-XTS "$bootpool/$zroot_key" \
1172 f_interactive || f_die
1173 unset pw_password # Sensitive info
1176 if ! echo "$pw_password" | f_eval_catch \
1177 $funcname geli "$GELI_ATTACH" \
1178 "$bootpool/$zroot_key" $disk$targetpart
1180 f_interactive || f_die
1181 unset pw_password # Sensitive info
1185 unset pw_password # Sensitive info
1188 f_eval_catch $funcname zfs "$ZFS_UNMOUNT" "$bootpool_name" ||
1190 f_eval_catch -d $funcname umount "$UMOUNT" /mnt # tmpfs
1193 # Create the gmirror(8) GEOMS for swap
1195 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1196 for disk in $disks; do
1197 swap_devs="$swap_devs $disk$swappart"
1199 f_eval_catch $funcname gmirror "$SWAP_GMIRROR_LABEL" \
1200 "$swap_devs" || return $FAILURE
1204 # Create the ZFS root pool with desired type and disk devices
1206 f_dprintf "$funcname: Creating root pool..."
1207 create_options="$ZFSBOOT_POOL_CREATE_OPTIONS"
1208 f_eval_catch $funcname zpool "$ZPOOL_CREATE_WITH_OPTIONS" \
1209 "-o altroot=$BSDINSTALL_CHROOT $create_options -m none -f" \
1210 "$zroot_name" "$zroot_vdevtype" "$zroot_vdevs" ||
1214 # Create ZFS dataset layout within the new root pool
1216 f_dprintf "$funcname: Creating ZFS datasets..."
1217 echo "$ZFSBOOT_DATASETS" | while read dataset options; do
1218 # Skip blank lines and comments
1219 case "$dataset" in "#"*|"") continue; esac
1220 # Remove potential inline comments in options
1221 options="${options%%#*}"
1222 # Replace tabs with spaces
1223 f_replaceall "$options" " " " " options
1224 # Reduce contiguous runs of space to one single space
1226 while [ "$oldoptions" != "$options" ]; do
1227 oldoptions="$options"
1228 f_replaceall "$options" " " " " options
1230 # Replace both commas and spaces with ` -o '
1231 f_replaceall "$options" "[ ,]" " -o " options
1232 # Create the dataset with desired options
1233 f_eval_catch $funcname zfs "$ZFS_CREATE_WITH_OPTIONS" \
1234 "${options:+-o $options}" "$zroot_name$dataset" ||
1238 # Touch up permissions on the tmp directories
1239 f_dprintf "$funcname: Modifying directory permissions..."
1241 for dir in /tmp /var/tmp; do
1242 f_eval_catch $funcname chmod "$CHMOD_MODE" 1777 \
1243 $BSDINSTALL_CHROOTDIR$dir || return $FAILURE
1247 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1248 f_dprintf "$funcname: Creating /boot symlink for boot pool..."
1249 f_eval_catch $funcname ln "$LN_SF" "$bootpool_name/boot" \
1250 $BSDINSTALL_CHROOT/boot || return $FAILURE
1253 # Set bootfs property
1254 local zroot_bootfs="$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME"
1255 f_dprintf "$funcname: Setting bootfs property..."
1256 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1257 "bootfs=\"$zroot_name/$zroot_bootfs\"" "$zroot_name" ||
1260 # Export the pool(s)
1261 f_dprintf "$funcname: Temporarily exporting ZFS pool(s)..."
1262 f_eval_catch $funcname zpool "$ZPOOL_EXPORT" "$zroot_name" ||
1264 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1265 f_eval_catch $funcname zpool "$ZPOOL_EXPORT" \
1266 "$bootpool_name" || return $FAILURE
1269 # Destroy the gnop devices (if enabled)
1270 for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$disks}; do
1271 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1272 f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1275 if [ ! "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1276 f_eval_catch -d $funcname gnop "$GNOP_DESTROY" \
1277 $disk$targetpart.nop
1281 # MBR boot loader touch-up
1282 if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
1283 f_dprintf "$funcname: Updating MBR boot loader on disks..."
1284 # Stick the ZFS boot loader in the "convienient hole" after
1285 # the ZFS internal metadata
1286 for disk in $disks; do
1287 f_eval_catch $funcname dd "$DD_WITH_OPTIONS" \
1288 /boot/zfsboot /dev/$disk$bootpart \
1289 "skip=1 seek=1024" || return $FAILURE
1293 # Re-import the ZFS pool(s)
1294 f_dprintf "$funcname: Re-importing ZFS pool(s)..."
1295 f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1296 "-o altroot=\"$BSDINSTALL_CHROOT\"" "$zroot_name" ||
1298 if [ "$ZFSBOOT_BOOT_POOL" ]; then
1299 f_eval_catch $funcname zpool "$ZPOOL_IMPORT_WITH_OPTIONS" \
1300 "-o altroot=\"$BSDINSTALL_CHROOT\"" \
1301 "$bootpool_name" || return $FAILURE
1304 # While this is apparently not needed, it seems to help MBR
1305 f_dprintf "$funcname: Configuring zpool.cache for zroot..."
1306 f_eval_catch $funcname mkdir "$MKDIR_P" $BSDINSTALL_CHROOT/boot/zfs ||
1308 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1309 "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1310 "$zroot_name" || return $FAILURE
1312 # Last, but not least... required lines for rc.conf(5)/loader.conf(5)
1313 # NOTE: We later concatenate these into their destination
1314 f_dprintf "%s: Configuring rc.conf(5)/loader.conf(5) additions..." \
1316 f_eval_catch $funcname echo "$ECHO_APPEND" 'zfs_enable=\"YES\"' \
1317 $BSDINSTALL_TMPETC/rc.conf.zfs || return $FAILURE
1318 f_eval_catch $funcname echo "$ECHO_APPEND" \
1319 'kern.geom.label.gptid.enable=\"0\"' \
1320 $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
1322 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1323 f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_mirror_load=\"YES\"' \
1324 $BSDINSTALL_TMPBOOT/loader.conf.gmirror || return $FAILURE
1327 # We're all done unless we should go on for boot pool
1328 [ "$ZFSBOOT_BOOT_POOL" ] || return $SUCCESS
1330 # Set cachefile for boot pool so it auto-imports at system start
1331 f_dprintf "$funcname: Configuring zpool.cache for boot pool..."
1332 f_eval_catch $funcname zpool "$ZPOOL_SET" \
1333 "cachefile=\"$BSDINSTALL_CHROOT/boot/zfs/zpool.cache\"" \
1334 "$bootpool_name" || return $FAILURE
1336 # Some additional geli(8) requirements for loader.conf(5)
1338 'zpool_cache_load=\"YES\"' \
1339 'zpool_cache_type=\"/boot/zfs/zpool.cache\"' \
1340 'zpool_cache_name=\"/boot/zfs/zpool.cache\"' \
1342 f_eval_catch $funcname echo "$ECHO_APPEND" "$option" \
1343 $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
1346 f_eval_catch $funcname printf "$PRINTF_CONF" vfs.root.mountfrom \
1347 "\"zfs:$zroot_name/$zroot_bootfs\"" \
1348 $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
1350 # We're all done unless we should go on to do encryption
1351 [ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
1354 # Configure geli(8)-based encryption
1356 f_dprintf "$funcname: Configuring disk encryption..."
1357 f_eval_catch $funcname echo "$ECHO_APPEND" 'aesni_load=\"YES\"' \
1358 $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
1359 f_eval_catch $funcname echo "$ECHO_APPEND" 'geom_eli_load=\"YES\"' \
1360 $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
1361 for disk in $disks; do
1362 f_eval_catch $funcname printf "$PRINTF_CONF" \
1363 geli_%s_keyfile0_load "$disk$targetpart YES" \
1364 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1366 f_eval_catch $funcname printf "$PRINTF_CONF" \
1367 geli_%s_keyfile0_type \
1368 "$disk$targetpart $disk$targetpart:geli_keyfile0" \
1369 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1371 f_eval_catch $funcname printf "$PRINTF_CONF" \
1372 geli_%s_keyfile0_name \
1373 "$disk$targetpart \"$ZFSBOOT_GELI_KEY_FILE\"" \
1374 $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
1381 # dialog_menu_diskinfo
1383 # Prompt the user to select a disk and then provide detailed info on it.
1385 dialog_menu_diskinfo()
1390 # Break from loop when user cancels disk selection
1393 device=$( msg_cancel="$msg_back" f_device_menu \
1394 "$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
1395 $DEVICE_TYPE_DISK 2>&1 ) || break
1396 $device get name disk
1398 # Show gpart(8) `show' and camcontrol(8) `inquiry' data
1399 f_show_msg "$msg_detailed_disk_info" \
1400 "$disk" "$( gpart show $disk 2> /dev/null )" \
1401 "$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
1402 "$disk" "$( camcontrol identify $disk 2> /dev/null )"
1408 ############################################################ MAIN
1413 f_dialog_title "$msg_zfs_configuration"
1414 f_dialog_backtitle "$msg_freebsd_installer"
1416 # User may have specifically requested ZFS-related operations be interactive
1417 ! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
1422 f_dprintf "BSDINSTALL_CHROOT=[%s]" "$BSDINSTALL_CHROOT"
1423 f_dprintf "BSDINSTALL_TMPETC=[%s]" "$BSDINSTALL_TMPETC"
1424 f_dprintf "FSTAB_FMT=[%s]" "$FSTAB_FMT"
1427 # If the system was booted with UEFI, warn the user that FreeBSD can't do
1430 if f_interactive; then
1431 bootmethod=$(sysctl -n machdep.bootmethod)
1432 f_dprintf "machdep.bootmethod=[%s]" "$bootmethod"
1433 if [ "$bootmethod" != "BIOS" ]; then
1436 f_dprintf "uefi_prompt=[%s]" "$retval"
1437 [ $retval -eq $DIALOG_OK ] || f_die
1442 # Loop over the main menu until we've accomplished what we came here to do
1445 if ! f_interactive; then
1447 mtag=">>> $msg_install"
1451 f_dialog_menutag_fetch mtag
1454 f_dprintf "retval=%u mtag=[%s]" $retval "$mtag"
1455 [ $retval -eq $DIALOG_OK ] || f_die
1460 # First, validate the user's selections
1463 # Make sure they gave us a name for the pool
1464 if [ ! "$ZFSBOOT_POOL_NAME" ]; then
1465 f_dprintf "Pool name cannot be empty."
1466 f_show_err "$msg_pool_name_cannot_be_empty"
1470 # Validate vdev type against number of disks selected/scripted
1471 # (also validates that ZFSBOOT_DISKS are real [probed] disks)
1472 # NB: dialog_menu_layout supports running non-interactively
1473 dialog_menu_layout || continue
1475 # Make sure each disk will be at least 50% ZFS
1476 if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
1477 f_expand_number "$ZFSBOOT_BOOT_POOL_SIZE" bootsize
1479 minsize=$swapsize teeny_disks=
1480 [ "$ZFSBOOT_BOOT_POOL" ] &&
1481 minsize=$(( $minsize + $bootsize ))
1482 for disk in $ZFSBOOT_DISKS; do
1483 debug= f_device_find -1 \
1484 $disk $DEVICE_TYPE_DISK device
1485 $device get capacity disksize || continue
1486 [ ${disksize:-0} -ge 0 ] || disksize=0
1487 disksize=$(( $disksize - $minsize ))
1488 [ $disksize -lt $minsize ] &&
1489 teeny_disks="$teeny_disks $disk"
1491 if [ "$teeny_disks" ]; then
1492 f_dprintf "swapsize=[%s] bootsize[%s] %s" \
1493 "$ZFSBOOT_SWAP_SIZE" \
1494 "$ZFSBOOT_BOOT_POOL_SIZE" \
1495 "minsize=[$minsize]"
1496 f_dprintf "These disks are too small: %s" \
1498 f_show_err "$msg_these_disks_are_too_small" \
1499 "$ZFSBOOT_SWAP_SIZE" \
1500 "$ZFSBOOT_BOOT_POOL_SIZE" \
1509 if f_interactive; then
1510 dialog_last_chance $ZFSBOOT_DISKS || continue
1517 vdev_type="$ZFSBOOT_VDEV_TYPE"
1519 # Blank the vdev type for the default layout
1520 [ "$vdev_type" = "stripe" ] && vdev_type=
1522 zfs_create_boot "$ZFSBOOT_POOL_NAME" \
1523 "$vdev_type" $ZFSBOOT_DISKS || continue
1527 ?" $msg_pool_type_disks")
1528 ZFSBOOT_CONFIRM_LAYOUT=1
1530 # User has poked settings, disable later confirmation
1531 ZFSBOOT_CONFIRM_LAYOUT=
1533 "- $msg_rescan_devices") f_device_rescan ;;
1534 "- $msg_disk_info") dialog_menu_diskinfo ;;
1536 # Prompt the user to input/change the name for the new pool
1537 f_dialog_input input \
1538 "$msg_please_enter_a_name_for_your_zpool" \
1539 "$ZFSBOOT_POOL_NAME" &&
1540 ZFSBOOT_POOL_NAME="$input"
1542 ?" $msg_force_4k_sectors")
1543 # Toggle the variable referenced both by the menu and later
1544 if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1545 ZFSBOOT_GNOP_4K_FORCE_ALIGN=
1547 ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1550 ?" $msg_encrypt_disks")
1551 # Toggle the variable referenced both by the menu and later
1552 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1553 ZFSBOOT_GELI_ENCRYPTION=
1555 ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1556 ZFSBOOT_GELI_ENCRYPTION=1
1559 ?" $msg_partition_scheme")
1560 # Toggle between GPT and MBR
1561 if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
1562 ZFSBOOT_PARTITION_SCHEME=MBR
1564 ZFSBOOT_PARTITION_SCHEME=GPT
1568 # Prompt the user to input/change the swap size for each disk
1569 f_dialog_input input \
1570 "$msg_please_enter_amount_of_swap_space" \
1571 "$ZFSBOOT_SWAP_SIZE" &&
1572 ZFSBOOT_SWAP_SIZE="${input:-0}"
1574 ?" $msg_swap_mirror")
1575 # Toggle the variable referenced both by the menu and later
1576 if [ "$ZFSBOOT_SWAP_MIRROR" ]; then
1577 ZFSBOOT_SWAP_MIRROR=
1579 ZFSBOOT_SWAP_MIRROR=1
1582 ?" $msg_swap_encrypt")
1583 # Toggle the variable referenced both by the menu and later
1584 if [ "$ZFSBOOT_SWAP_ENCRYPTION" ]; then
1585 ZFSBOOT_SWAP_ENCRYPTION=
1587 ZFSBOOT_SWAP_ENCRYPTION=1
1595 ################################################################################
1597 ################################################################################