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