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 name for the boot environment parent dataset
50 : ${ZFSBOOT_BEROOT_NAME:=bootenv}
53 # Default name for the primany boot environment
55 : ${ZFSBOOT_BOOTFS_NAME:=default}
58 # Default Virtual Device (vdev) type to create
60 : ${ZFSBOOT_VDEV_TYPE:=stripe}
63 # Should we use gnop(8) to configure a transparent mapping to 4K sectors?
65 : ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:=1}
68 # Should we use geli(8) to encrypt the drives?
70 : ${ZFSBOOT_GELI_ENCRYPTION:=}
73 # Default name the unencrypted pool when using geli(8) to encrypt the drives
75 : ${ZFSBOOT_GELI_POOL_NAME:=bootpool}
78 # Default size for the unencrypted boot pool when using geli(8)
80 : ${ZFSBOOT_GELI_BOOT_SIZE:=2g}
83 # Default path to the geli(8) keyfile used in drive encryption
85 : ${ZFSBOOT_GELI_KEY_FILE:=/boot/encryption.key}
88 # Default disks to use (always empty unless being scripted)
93 # Default partitioning scheme to use on disks
95 : ${ZFSBOOT_PARTITION_SCHEME:=GPT}
98 # How much swap to put on each block device in the boot zpool
99 # NOTE: Value passed to gpart(8); which supports SI unit suffixes.
101 : ${ZFSBOOT_SWAP_SIZE:=2g}
104 # Default ZFS layout for root zpool
106 # NOTE: Requires /tmp, /var/tmp, /$ZFSBOOT_BOOTFS_NAME/$ZFSBOOT_BOOTFS_NAME
107 # NOTE: Anything after pound/hash character [#] is ignored as a comment.
109 f_isset ZFSBOOT_DATASETS || ZFSBOOT_DATASETS="
110 # DATASET OPTIONS (comma or space separated; or both)
112 # Boot Environment [BE] root and default boot dataset
113 /$ZFSBOOT_BEROOT_NAME mountpoint=none
114 /$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME mountpoint=/
116 # Compress /tmp, allow exec but not setuid
117 /tmp mountpoint=/tmp,compression=lz4,exec=on,setuid=off
119 # Don't mount /usr so that 'base' files go to the BEROOT
120 /usr mountpoint=/usr,canmount=off
122 /usr/local # local files (i.e. from packages) separate from base system
124 # Home directories separated so they are common to all BEs
128 /usr/ports compression=lz4,setuid=off
129 /usr/ports/distfiles compression=off,exec=off,setuid=off
130 /usr/ports/packages compression=off,exec=off,setuid=off
132 # Source tree (compressed)
133 /usr/src compression=lz4,exec=off,setuid=off
134 /usr/obj # Object files
136 # Create /var and friends
138 /var/crash compression=lz4,exec=off,setuid=off
139 /var/db exec=off,setuid=off
140 /var/db/pkg compression=lz4,exec=off,setuid=off
141 /var/empty exec=off,setuid=off
142 /var/log compression=lz4,exec=off,setuid=off
143 /var/mail compression=lz4,exec=off,setuid=off
144 /var/run exec=off,setuid=off
145 /var/tmp compression=lz4,exec=on,setuid=off
148 ############################################################ GLOBALS
151 # Strings that should be moved to an i18n file and loaded with f_include_lang()
153 hline_alnum_arrows_punc_tab_enter="Use alnum, arrows, punctuation, TAB or ENTER"
154 hline_arrows_space_tab_enter="Use arrows, SPACE, TAB or ENTER"
155 hline_arrows_tab_enter="Press arrows, TAB or ENTER"
158 msg_change="Change Selection"
159 msg_configure_options="Configure Options:"
161 msg_create_desc="Proceed with Installation"
162 msg_create_help="Create ZFS boot pool with displayed options"
163 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"
164 msg_disk_info="Disk Info"
165 msg_disk_info_help="Get detailed information on disk device(s)"
166 msg_disks_to_use="Disks To Use"
167 msg_disks_to_use_help="Choose which disks to use for the Virtual Device (Required)"
168 msg_force_4k_sectors="Force 4K Sectors?"
169 msg_force_4k_sectors_help="Use gnop(8) to configure forced 4K sector alignment"
170 msg_freebsd_installer="FreeBSD Installer"
171 msg_geli_encryption="Encrypt Disks?"
172 msg_geli_encryption_help="Use geli(8) to encrypt all data partitions"
173 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"
174 msg_geli_setup="Initializing encryption on the selected disks, this will take several seconds per disk"
175 msg_invalid_virtual_device_type="Invalid Virtual Device type \`%s'"
176 msg_invalid_virtual_device_type_help="Select another Virtual Device type or Cancel to\nreturn to the ZFS menu. From there you can select\nmore disks or rescan for additional devices."
177 msg_last_chance_are_you_sure="Last Chance! Are you sure you want to destroy the current contents of the following disks:\n%s"
178 msg_last_chance_are_you_sure_color="\\\\ZrLast Chance!\\\\ZR Are you \\\\Z1sure\\\\Zn you want to \\\\Zr\\\\Z1destroy\\\\Zn the current contents of the following disks:\n%s"
179 msg_mirror_desc="Mirror - n-Way Mirroring"
180 msg_mirror_help="[2+ Disks] Mirroring provides the best performance, but the least storage"
182 msg_no_disks_present_to_configure="No disk(s) present to configure"
183 msg_no_disks_selected="No disks selected."
184 msg_not_enough_disks_selected="Not enough disks selected. (%u < %u wanted)"
186 msg_partition_scheme="Partition Scheme"
187 msg_partition_scheme_help="Toggle between GPT and MBR partitioning schemes"
188 msg_please_enter_a_name_for_your_zpool="Please enter a name for your zpool:"
189 msg_please_enter_amount_of_swap_space="Please enter amount of swap space (SI-Unit suffixes\nrecommended; e.g., \`2g' for 2 Gigabytes):"
190 msg_please_select_one_or_more_disks="Please select one or more disks to create a zpool:"
191 msg_pool_name="Pool Name"
192 msg_pool_name_cannot_be_empty="Pool name cannot be empty."
193 msg_pool_name_help="Customize the name of the zpool to be created (Required)"
194 msg_processing_selection="Processing selection..."
195 msg_raidz1_desc="RAID-Z1 - Single Redundant RAID"
196 msg_raidz1_help="[3+ Disks] Withstand failure of 1 disk. Recommended for: 3, 5 or 9 disks"
197 msg_raidz2_desc="RAID-Z2 - Double Redundant RAID"
198 msg_raidz2_help="[4+ Disks] Withstand failure of 2 disks. Recommended for: 4, 6 or 10 disks"
199 msg_raidz3_desc="RAID-Z3 - Triple Redundant RAID"
200 msg_raidz3_help="[5+ Disks] Withstand failure of 3 disks. Recommended for: 5, 7 or 11 disks"
201 msg_rescan_devices="Rescan Devices"
202 msg_rescan_devices_help="Scan for device changes"
204 msg_select_a_disk_device="Select a disk device"
205 msg_select_virtual_device_type="Select Virtual Device type:"
206 msg_stripe_desc="Stripe - No Redundancy"
207 msg_stripe_help="[1+ Disks] Striping provides maximum storage but no redundancy"
208 msg_swap_size="Swap Size"
209 msg_swap_size_help="Customize how much swap space is allocated to each selected disk"
210 msg_these_disks_are_too_small="These disks are too small given the amount of requested\nswap (%s) and/or GELI (%s) partitions, which would take\n50%% or more (not recommended) of each of the following\nselected disk devices:\n\n %s\n\nRecommend changing partition size(s) and/or selecting a\ndifferent set of devices."
212 msg_zfs_configuration="ZFS Configuration"
213 msg_zfs_vdev_type="ZFS VDev Type"
214 msg_zfs_vdev_type_help="Select type of ZFS Virtual Device to create"
216 ############################################################ FUNCTIONS
220 # Display the dialog(1)-based application main menu.
224 local title="$DIALOG_TITLE"
225 local btitle="$DIALOG_BACKTITLE"
226 local prompt="$msg_configure_options"
227 local force4k="$msg_no"
228 local usegeli="$msg_no"
229 [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ] && force4k="$msg_yes"
230 [ "$ZFSBOOT_GELI_ENCRYPTION" ] && usegeli="$msg_yes"
232 '>>> $msg_create' '$msg_create_desc'
234 '- $msg_rescan_devices' '*'
235 '$msg_rescan_devices_help'
236 '- $msg_disk_info' '*'
237 '$msg_disk_info_help'
238 '1 $msg_pool_name' '$ZFSBOOT_POOL_NAME'
239 '$msg_pool_name_help'
240 '2 $msg_disks_to_use' '$ZFSBOOT_DISKS'
241 '$msg_disks_to_use_help'
242 '3 $msg_zfs_vdev_type' '$ZFSBOOT_VDEV_TYPE'
243 '$msg_zfs_vdev_type_help'
244 '4 $msg_force_4k_sectors' '$force4k'
245 '$msg_force_4k_sectors_help'
246 '5 $msg_geli_encryption' '$usegeli'
247 '$msg_geli_encryption_help'
248 '6 $msg_partition_scheme' '$ZFSBOOT_PARTITION_SCHEME'
249 '$msg_partition_scheme_help'
250 '7 $msg_swap_size' '$ZFSBOOT_SWAP_SIZE'
251 '$msg_swap_size_help'
253 local defaultitem= # Calculated below
254 local hline="$hline_alnum_arrows_punc_tab_enter"
256 local height width rows
257 eval f_dialog_menu_with_help_size height width rows \
258 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
260 # Obtain default-item from previously stored selection
261 f_dialog_default_fetch defaultitem
264 menu_choice=$( eval $DIALOG \
265 --title \"\$title\" \
266 --backtitle \"\$btitle\" \
267 --hline \"\$hline\" \
269 --ok-label \"\$msg_select\" \
270 --cancel-label \"\$msg_cancel\" \
271 --default-item \"\$defaultitem\" \
272 --menu \"\$prompt\" \
273 $height $width $rows \
275 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
278 f_dialog_data_sanitize menu_choice
279 f_dialog_menutag_store "$menu_choice"
281 # Only update default-item on success
282 [ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
289 # Edit the list of disks to be used by the ZFS boot pool.
293 local title="$DIALOG_TITLE"
294 local btitle="$DIALOG_BACKTITLE"
295 local prompt="$msg_please_select_one_or_more_disks"
296 local check_list= # Calculated below
297 local hline="$hline_arrows_space_tab_enter"
298 local dev vardev disks=
301 # Get a [new] list of disk devices
303 f_device_find "" $DEVICE_TYPE_DISK disks
304 if [ ! "$disks" ]; then
305 f_show_msg "$msg_no_disks_present_to_configure"
309 # Lets sort the disks array to be more user friendly
310 disks=$( echo "$disks" | tr ' ' '\n' | sort | tr '\n' ' ' )
313 # Loop through the list of selected disks and create temporary local
314 # variables mapping their status onto an up-to-date list of disks.
316 for dev in $ZFSBOOT_DISKS; do
317 f_str2varname "$dev" vardev
318 local _${vardev}_status=on
322 # Create the checklist menu of discovered disk devices
325 for dev in $disks; do
327 device_$dev get desc desc
328 f_shell_escape "$desc" desc
329 f_str2varname "$dev" vardev
330 f_getvar _${vardev}_status:-off on_off
331 check_list="$check_list '$dev' '$desc' $on_off"
335 # Prompt the user to check some disks
337 local height width rows
338 eval f_dialog_checklist_size height width rows \
339 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $check_list
340 disks=$( eval $DIALOG \
341 --title \"\$DIALOG_TITLE\" \
342 --backtitle \"\$DIALOG_BACKTITLE\" \
343 --hline \"\$hline\" \
344 --ok-label \"\$msg_ok\" \
345 --cancel-label \"\$msg_cancel\" \
346 --checklist \"\$prompt\" \
347 $height $width $rows \
349 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
351 # Exit if user either pressed ESC or chose Cancel/No
352 f_dialog_data_sanitize disks
354 ZFSBOOT_DISKS="$disks"
361 # Prompt the user to select a a Virtual Device type.
365 local title="$DIALOG_TITLE"
366 local btitle="$DIALOG_BACKTITLE"
367 local prompt="$msg_select_virtual_device_type"
369 # Make sure [potentially scripted] selections are real
371 for disk in $ZFSBOOT_DISKS; do
372 f_struct device_$disk && real_disks="$real_disks $disk"
374 # Make sure we have at least one real disk selected
375 ndisks=$( set -- $real_disks; echo $# )
378 'stripe' '$msg_stripe_desc' '$msg_stripe_help'
379 'mirror' '$msg_mirror_desc' '$msg_mirror_help'
380 'raidz1' '$msg_raidz1_desc' '$msg_raidz1_help'
381 'raidz2' '$msg_raidz2_desc' '$msg_raidz2_help'
382 'raidz3' '$msg_raidz3_desc' '$msg_raidz3_help'
385 local defaultitem="$ZFSBOOT_VDEV_TYPE"
386 local hline="$hline_arrows_tab_enter"
387 local error_msg revalidate_choice
389 local mheight mwidth mrows
390 eval f_dialog_menu_size mheight mwidth mrows \
391 \"\$title\" \"\$btitle\" \"\$prompt\" \"\$hline\" $menu_list
393 f_dialog_infobox_size iheight iwidth \
394 "$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$msg_processing_selection"
397 menu_choice=$( eval $DIALOG \
398 --title \"\$title\" \
399 --backtitle \"\$btitle\" \
400 --hline \"\$hline\" \
401 --ok-label \"\$msg_ok\" \
402 --cancel-label \"\$msg_cancel\" \
404 --default-item \"\$defaultitem\" \
405 --menu \"\$prompt\" \
406 $mheight $mwidth $mrows \
409 ${USE_XDIALOG:+--no-buttons} \
410 --infobox \"\$msg_processing_selection\" \
412 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
414 f_dialog_data_sanitize menu_choice
415 sleep 0.5 # Give time to read `--and-widget --info-box'
417 # Make sure we have enough disks for the desired vdev type
418 case "$menu_choice" in
419 stripe) want_disks=1 ;;
420 mirror) want_disks=2 ;;
421 raidz1) want_disks=3 ;;
422 raidz2) want_disks=4 ;;
423 raidz3) want_disks=5 ;;
425 f_show_msg "$msg_invalid_virtual_device_type" \
429 if [ $ndisks -lt $want_disks ]; then
430 msg_yes="$msg_change" msg_no="$msg_cancel" f_yesno \
431 "%s: $msg_not_enough_disks_selected\n%s" \
432 "$menu_choice" $ndisks $want_disks \
433 "$msg_invalid_virtual_device_type_help" ||
437 ZFSBOOT_VDEV_TYPE="$menu_choice"
441 # zfs_create_diskpart $disk $index
443 # For each block device to be used in the zpool, rather than just create the
444 # zpool with the raw block devices (e.g., da0, da1, etc.) we create partitions
445 # so we can have some real swap. This also provides wiggle room incase your
446 # replacement drivers do not have the exact same sector counts.
448 # NOTE: The MBR layout is more complicated (GPT is preferred).
450 zfs_create_diskpart()
452 local disk="$1" index="$2"
453 local funcname=zfs_create_diskpart
454 local disksize partsize
457 [ "$disk" -a "$index" ] || return $FAILURE
460 # Destroy whatever partition layout is currently on disk.
461 # NOTE: `-F' required to destroy if partitions still exist.
462 # NOTE: Failure is ok here, blank disk will have nothing to destroy.
464 f_quietly gpart destroy -F $disk
465 f_quietly zpool labelclear -f /dev/$disk # Kill it with fire
467 # Make doubly-sure backup GPT is destroyed
468 f_quietly gpart create -s gpt $disk || return $FAILURE
469 f_quietly gpart destroy -F $disk || return $FAILURE
471 # Calculate partition size given desired amount of swap
472 device_$disk get capacity disksize || return $FAILURE
473 partsize=$(( $disksize - $swapsize ))
476 # Lay down the desired type of partition scheme
478 local setsize mbrindex
479 case "$ZFSBOOT_PARTITION_SCHEME" in
482 # 1. Create GPT layout using labels
484 gpart create -s gpt $disk || return $FAILURE
487 # 2. Add small freebsd-boot partition labeled `boot#'
489 gpart add -l gptboot$index -t freebsd-boot -s 512k $disk ||
491 gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 $disk ||
494 # zpool will use the `zfs#' GPT labels
495 bootpart=p2 targetpart=p2
497 # Change things around if we are using GELI
498 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
499 bootpart=p2 targetpart=p3
500 partsize=$(( $partsize - $gelisize ))
501 gpart add -l boot$index -t freebsd-zfs \
502 -s ${gelisize}b -a 1m $disk || return $FAILURE
503 # Pedantically nuke any old labels, stop geli
504 f_quietly zpool labelclear -f /dev/$disk$bootpart
505 f_quietly geli detach -f /dev/$disk$targetpart
509 # 3. Add freebsd-zfs partition labeled `zfs#' for zpool
510 # NOTE: Using above calculated partsize to leave room for swap.
512 [ $swapsize -gt 0 ] && setsize="-s ${partsize}b"
513 gpart add -l zfs$index -t freebsd-zfs $setsize -a 1m $disk ||
515 f_quietly zpool labelclear -f /dev/$disk$targetpart # Pedantic
518 # 4. Add freebsd-swap partition labeled `swap#'
520 if [ $swapsize -gt 0 ]; then
521 gpart add -l swap$index -t freebsd-swap -a 1m $disk ||
524 printf "$fstab_fmt" \
525 /dev/gpt/swap$index none swap sw 0 0 \
526 >> $BSDINSTALL_TMPETC/fstab || return $FAILURE
532 # 1. Create MBR layout (no labels)
534 gpart create -s mbr $disk || return $FAILURE
535 gpart bootcode -b /boot/boot0 $disk || return $FAILURE
538 # 2. Add freebsd slice with all available space
540 gpart add -t freebsd $disk || return $FAILURE
541 gpart set -a active -i 1 $disk || return $FAILURE
542 f_quietly zpool labelclear -f /dev/${disk}s1 # Pedantic
543 f_quietly gpart destroy -F ${disk}s1 # Pedantic
546 # 3. Write BSD sceme to the freebsd slice
548 gpart create -s BSD ${disk}s1 || return $FAILURE
550 # zpool will use s1a (no labels)
551 bootpart=s1a targetpart=s1a mbrindex=1
553 # Change things around if we are using GELI
554 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
555 bootpart=s1a targetpart=s1d
556 partsize=$(( $partsize - $gelisize ))
557 mbrindex=4 # If this is s1a then make the zpool s1d
558 gpart add -t freebsd-zfs -i 1 -s ${gelisize}b \
559 ${disk}s1 || return $FAILURE
560 # Pedantically nuke any old labels, stop geli
561 f_quietly zpool labelclear -f /dev/$disk$bootpart
562 f_quietly geli detach -f /dev/$disk$targetpart
566 # 4. Partition the BSD slice for ZFS
567 # NOTE: Using above calculated partsize to leave room for swap.
569 [ $swapsize -gt 0 ] && setsize="-s ${partsize}b"
570 gpart add -t freebsd-zfs -i $mbrindex $setsize ${disk}s1 ||
572 f_quietly zpool labelclear -f /dev/$disk$targetpart # Pedantic
575 # 5. Add freebsd-swap partition
577 if [ $swapsize -gt 0 ]; then
578 gpart add -t freebsd-swap -i 2 ${disk}s1 ||
581 printf "$fstab_fmt" /dev/${disk}s1b none swap sw 0 0 \
582 >> $BSDINSTALL_TMPETC/fstab || return $FAILURE
587 printf "%s: %s is an unsupported partition scheme" \
588 "$funcname" "$ZFSBOOT_PARTITION_SCHEME" >&2
591 esac # $ZFSBOOT_PARTITION_SCHEME
596 # zfs_create_boot $poolname $vdev_type $real_disks ...
598 # Creates boot pool and dataset layout. Returns error if something goes wrong.
599 # Errors are printed to stderr for collection and display.
603 local poolname="$1" vdev_type="$2"
604 local fstab_fmt="%s\t\t%s\t%s\t%s\t\t%s\t%s\n"
605 local funcname=zfs_create_boot
606 local bootpart targetpart
608 shift 2 # name vdev_type
610 # We may need this later
613 # Pedantic checks; should never be seen
614 if [ ! "$poolname" ]; then
615 echo "$funcname: NULL poolname" >&2
618 if [ $# -lt 1 ]; then
619 echo "$funcname: missing disk arguments" >&2
623 # Initialize fstab(5)
624 printf "$fstab_fmt" \
625 "# Device" Mountpoint FStype Options Dump "Pass#" \
626 >> $BSDINSTALL_TMPETC/fstab || return $FAILURE
628 # Expand SI units in desired sizes
629 local swapsize gelisize
630 f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize || return $FAILURE
631 f_expand_number "$ZFSBOOT_GELI_BOOT_SIZE" gelisize || return $FAILURE
636 zfs_create_diskpart $disk $n || return $FAILURE
640 # MBR boot loader hack part 1
641 # We have to do this early because geli gets in the way later
642 if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
643 for disk in $realdisks; do
644 dd if=/boot/zfsboot of=/dev/${disk}s1 count=1 ||
649 # Forced 4k alignment support provided by Geom NOP (see gnop(8))
651 if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
654 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
655 # We don't gnop the encrypted partition
656 # because geli will do this for us
657 # gnop the unencrypted disk
658 gnop create -S 4096 $disk$bootpart ||
660 unenc_list="$unenc_list $disk$bootpart.nop"
662 gnop create -S 4096 $disk$targetpart ||
664 new_list="$new_list $disk$targetpart.nop"
671 new_list="$new_list $disk$targetpart"
672 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
673 unenc_list="$unenc_list $disk$bootpart"
679 # If encryption is enabled, we need to create the GEOMs
681 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
683 local geli_pool="$BSDINSTALL_CHROOT/$ZFSBOOT_GELI_POOL_NAME"
684 local key="$ZFSBOOT_GELI_KEY_FILE"
686 # Create the parent directories for our unencrypted pool
687 f_quietly umount /mnt
688 mount -t tmpfs none $BSDINSTALL_CHROOT || return $FAILURE
690 # Create mirror across the unencrypted partition on all disks
691 [ $( set -- $unenc_list; echo $# ) -gt 1 ] && bootvdev=mirror
693 zpool create -o altroot=$BSDINSTALL_CHROOT \
694 -m "/$ZFSBOOT_GELI_POOL_NAME" -f \
695 "$ZFSBOOT_GELI_POOL_NAME" $bootvdev $unenc_list ||
697 mkdir -p $geli_pool/boot || return $FAILURE
699 # Generate an encryption key using random(4)
700 dd if=/dev/random of="$geli_pool/$key" bs=4096 count=1 ||
703 # Create the geli(8) GEOMS
705 msg_enter_new_password="$msg_geli_password" \
706 f_dialog_input_password || return $FAILURE
707 f_dialog_info "$msg_geli_setup" \
708 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
709 for disk in $realdisks; do
710 echo "$pw_password" | geli init -b -B \
711 "$geli_pool/boot/$disk$targetpart.eli" \
712 -e AES-XTS -J - -K "$geli_pool/$key" -l 256 \
713 -s 4096 $disk$targetpart || return $FAILURE
714 echo "$pw_password" | geli attach -j - \
715 -k "$geli_pool/$key" $disk$targetpart ||
717 geli_list="$geli_list $disk$targetpart.eli"
720 zfs unmount "$ZFSBOOT_GELI_POOL_NAME" || return $FAILURE
721 f_quietly umount /mnt # done with tmpfs
725 # Create the ZFS pool with desired type and disk devices
727 zpool create -o altroot=$BSDINSTALL_CHROOT -m none -f \
728 "$poolname" $vdev_type $* || return $FAILURE
730 # Customize the zpool a bit...
731 zfs set checksum=fletcher4 "$poolname" || return $FAILURE
732 zfs set atime=off "$poolname" || return $FAILURE
735 # Create ZFS dataset layout within the new boot pool
737 echo "$ZFSBOOT_DATASETS" | while read dataset options; do
738 # Skip blank lines and comments
739 case "$dataset" in "#"*|"") continue; esac
740 # Remove potential inline comments in options
741 options="${options%%#*}"
742 # Replace tabs with spaces
743 f_replaceall "$options" " " " " options
744 # Reduce contiguous runs of space to one single space
746 while [ "$oldoptions" != "$options" ]; do
747 oldoptions="$options"
748 f_replaceall "$options" " " " " options
750 # Replace both commas and spaces with ` -o '
751 f_replaceall "$options" "[ ,]" " -o " options
752 # Create the dataset with desired options
753 zfs create ${options:+-o $options} "$poolname$dataset" ||
757 # Touch up permissions on the tmp directories
758 chmod 1777 $BSDINSTALL_CHROOT/tmp || return $FAILURE
759 chmod 1777 $BSDINSTALL_CHROOT/var/tmp || return $FAILURE
762 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
763 { ln -s $ZFSBOOT_GELI_POOL_NAME/boot $BSDINSTALL_CHROOT/boot ||
766 # Set bootfs property
767 zpool set bootfs="$poolname/$ZFSBOOT_BEROOT_NAME/$ZFSBOOT_BOOTFS_NAME" \
768 "$poolname" || return $FAILURE
771 zpool export "$poolname" || return $FAILURE
772 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
773 { zpool export "$ZFSBOOT_GELI_POOL_NAME" || return $FAILURE; }
775 # Destroy the gnop devices (if enabled)
776 for disk in ${ZFSBOOT_GNOP_4K_FORCE_ALIGN:+$realdisks}; do
777 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
778 f_quietly gnop destroy $disk$bootpart.nop
780 f_quietly gnop destroy $disk$targetpart.nop
784 # MBR boot loader hack part 2
785 if [ "$ZFSBOOT_PARTITION_SCHEME" = "MBR" ]; then
786 # Stick the ZFS boot loader in the "convienient hole" after
787 # the ZFS internal metadata
788 for disk in $realdisks; do
789 dd if=/boot/zfsboot of=/dev/$disk$bootpart \
790 skip=1 seek=1024 || return $FAILURE
794 # Re-import the ZFS pool(s)
795 zpool import -o altroot=$BSDINSTALL_CHROOT $poolname || return $FAILURE
796 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
797 { zpool import -o altroot=$BSDINSTALL_CHROOT \
798 "$ZFSBOOT_GELI_POOL_NAME" || return $FAILURE; }
800 # While this is apparently not needed, it seems to help MBR
801 mkdir -p $BSDINSTALL_CHROOT/boot/zfs || return $FAILURE
802 zpool set cachefile=$BSDINSTALL_CHROOT/boot/zfs/zpool.cache \
803 "$poolname" || return $FAILURE
805 # Last, but not least... required lines for rc.conf(5)/loader.conf(5)
806 # NOTE: We later concatenate these into their destination
807 echo 'zfs_enable="YES"' > $BSDINSTALL_TMPETC/rc.conf.zfs ||
809 echo 'zfs_load="YES"' > $BSDINSTALL_TMPBOOT/loader.conf.zfs ||
812 # We're all done unless we should go on to do encryption
813 [ "$ZFSBOOT_GELI_ENCRYPTION" ] || return $SUCCESS
815 # Some additional GELI requirements for loader.conf(5)
816 echo 'zpool_cache_load="YES"' \
817 >> $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
818 echo 'zpool_cache_type="/boot/zfs/zpool.cache"' \
819 >> $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
820 echo 'zpool_cache_name="/boot/zfs/zpool.cache"' \
821 >> $BSDINSTALL_TMPBOOT/loader.conf.zfs || return $FAILURE
824 # Configure geli(8)-based encryption
826 echo 'aesni_load="YES"' \
827 > $BSDINSTALL_TMPBOOT/loader.conf.aesni || return $FAILURE
828 echo 'geom_eli_load="YES"' \
829 > $BSDINSTALL_TMPBOOT/loader.conf.geli || return $FAILURE
830 printf 'vfs.root.mountfrom="zfs:%s/%s/%s"\n' "$poolname" \
831 "$ZFSBOOT_BEROOT_NAME" "$ZFSBOOT_BOOTFS_NAME" \
832 > $BSDINSTALL_TMPBOOT/loader.conf.root || return $FAILURE
833 for disk in $realdisks; do
834 printf 'geli_%s_keyfile0_load="YES"\n' \
836 > $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
838 printf 'geli_%s_keyfile0_type="%s:geli_keyfile0"\n' \
839 "$disk$targetpart" "$disk$targetpart" \
840 >> $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
842 printf 'geli_%s_keyfile0_name="%s"\n' \
843 "$disk$targetpart" "$ZFSBOOT_GELI_KEY_FILE" \
844 >> $BSDINSTALL_TMPBOOT/loader.conf.$disk$targetpart ||
851 # dialog_menu_diskinfo
853 # Prompt the user to select a disk and then provide detailed info on it.
855 dialog_menu_diskinfo()
860 # Break from loop when user cancels disk selection
863 disk=$( msg_cancel="$msg_back" f_device_menu \
864 "$DIALOG_TITLE" "$msg_select_a_disk_device" "" \
865 $DEVICE_TYPE_DISK 2>&1 ) || break
867 # Show gpart(8) `show' and camcontrol(8) `inquiry' data
868 f_show_msg "$msg_detailed_disk_info" \
869 "$disk" "$( gpart show $disk 2> /dev/null )" \
870 "$disk" "$( camcontrol inquiry $disk 2> /dev/null )" \
871 "$disk" "$( camcontrol identify $disk 2> /dev/null )"
877 ############################################################ MAIN
882 f_dialog_title "$msg_zfs_configuration"
883 f_dialog_backtitle "$msg_freebsd_installer"
885 # User may have specifically requested ZFS-related operations be interactive
886 ! f_interactive && f_zfsinteractive && unset $VAR_NONINTERACTIVE
889 # Loop over the main menu until we've accomplished what we came here to do
892 if ! f_interactive; then
894 mtag=">>> $msg_create"
898 f_dialog_menutag_fetch mtag
901 f_dprintf "retval=%u mtag=[%s]" $reval "$mtag"
902 [ $retval -eq $DIALOG_OK ] || f_die
907 # First, validate the user's selections
910 # Make sure they gave us a name for the pool
911 if [ ! "$ZFSBOOT_POOL_NAME" ]; then
912 f_show_msg "$msg_pool_name_cannot_be_empty"
913 f_interactive || f_die
916 # Make sure [potentially scripted] selections are real
918 for disk in $ZFSBOOT_DISKS; do
919 f_struct device_$disk && real_disks="$real_disks $disk"
921 # Make sure we have at least one real disk selected
922 ndisks=$( set -- $real_disks; echo $# )
923 if [ $ndisks -lt 1 ]; then
924 f_show_msg "$msg_no_disks_selected"
925 f_interactive || f_die
928 # Make sure we have enough disks for the desired vdev type
929 case "$ZFSBOOT_VDEV_TYPE" in
930 stripe) want_disks=1 ;;
931 mirror) want_disks=2 ;;
932 raidz1) want_disks=3 ;;
933 raidz2) want_disks=4 ;;
934 raidz3) want_disks=5 ;;
936 f_show_msg "$msg_invalid_virtual_device_type" \
938 f_interactive || f_die
941 if [ $ndisks -lt $want_disks ]; then
942 f_show_msg "%s: $msg_not_enough_disks_selected" \
943 "$ZFSBOOT_VDEV_TYPE" "$want_disks"
944 f_interactive || f_die
947 # Make sure each disk will be at least 50% ZFS
948 if f_expand_number "$ZFSBOOT_SWAP_SIZE" swapsize &&
949 f_expand_number "$ZFSBOOT_GELI_BOOT_SIZE" gelisize
951 minsize=$swapsize teeny_disks=
952 [ "$ZFSBOOT_GELI_ENCRYPTION" ] &&
953 minsize=$(( $minsize + $gelisize ))
954 for disk in $real_disks; do
955 device_$disk get capacity disksize || continue
956 disksize=$(( $disksize - $minsize ))
957 [ $disksize -lt $minsize ] &&
958 teeny_disks="$teeny_disks $disk"
960 if [ "$teeny_disks" ]; then
961 f_show_msg "$msg_these_disks_are_too_small" \
962 "$ZFSBOOT_SWAP_SIZE" \
963 "$ZFSBOOT_GELI_BOOT_SIZE" \
965 f_interactive || f_die
973 if [ ! "$USE_XDIALOG" ]; then
974 f_interactive && DIALOG="$DIALOG --colors" f_noyes \
975 "$msg_last_chance_are_you_sure_color" \
976 "$ZFSBOOT_DISKS" || continue
978 f_interactive && f_noyes \
979 "$msg_last_chance_are_you_sure" \
980 "$ZFSBOOT_DISKS" || continue
987 vdev_type="$ZFSBOOT_VDEV_TYPE"
989 # Blank the vdev type for the default layout
990 [ "$vdev_type" = "stripe" ] && vdev_type=
992 if ! error=$( zfs_create_boot "$ZFSBOOT_POOL_NAME" \
993 "$vdev_type" $real_disks 2>&1 )
995 f_dialog_msgbox "$error"
996 f_interactive || f_die
1002 "- $msg_rescan_devices") f_device_rescan ;;
1003 "- $msg_disk_info") dialog_menu_diskinfo ;;
1005 # Prompt the user to input/change the name for the new pool
1006 f_dialog_input input \
1007 "$msg_please_enter_a_name_for_your_zpool" \
1008 "$ZFSBOOT_POOL_NAME" &&
1009 ZFSBOOT_POOL_NAME="$input"
1011 ?" $msg_disks_to_use") dialog_edit_disks ;;
1012 ?" $msg_zfs_vdev_type") dialog_menu_vdev ;;
1013 ?" $msg_force_4k_sectors")
1014 # Toggle the variable referenced both by the menu and later
1015 if [ "$ZFSBOOT_GNOP_4K_FORCE_ALIGN" ]; then
1016 ZFSBOOT_GNOP_4K_FORCE_ALIGN=
1018 ZFSBOOT_GNOP_4K_FORCE_ALIGN=1
1021 ?" $msg_geli_encryption")
1022 # Toggle the variable referenced both by the menu and later
1023 if [ "$ZFSBOOT_GELI_ENCRYPTION" ]; then
1024 ZFSBOOT_GELI_ENCRYPTION=
1026 ZFSBOOT_GELI_ENCRYPTION=1
1029 ?" $msg_partition_scheme")
1030 # Toggle between GPT and MBR
1031 if [ "$ZFSBOOT_PARTITION_SCHEME" = GPT ]; then
1032 ZFSBOOT_PARTITION_SCHEME=MBR
1034 ZFSBOOT_PARTITION_SCHEME=GPT
1038 # Prompt the user to input/change the swap size for each disk
1039 f_dialog_input input \
1040 "$msg_please_enter_amount_of_swap_space" \
1041 "$ZFSBOOT_SWAP_SIZE" &&
1042 ZFSBOOT_SWAP_SIZE="$input"
1049 ################################################################################
1051 ################################################################################