1 # ZFS boot stub for initramfs-tools.
3 # In the initramfs environment, the /init script sources this stub to
4 # override the default functions in the /scripts/local script.
6 # Enable this by passing boot=zfs on the kernel command line.
9 # Source the common init script
10 . /etc/zfs/zfs-functions
12 # Paths to what we need - in the initrd, these paths are hardcoded,
13 # so override the defines in zfs-functions.
16 ZPOOL_CACHE="/etc/zfs/zpool.cache"
17 export ZFS ZPOOL ZPOOL_CACHE
19 # This runs any scripts that should run before we start importing
20 # pools and mounting any filesystems.
23 if type run_scripts > /dev/null 2>&1 && \
24 [ -f "/scripts/local-top" -o -d "/scripts/local-top" ]
26 [ "$quiet" != "y" ] && \
27 zfs_log_begin_msg "Running /scripts/local-top"
28 run_scripts /scripts/local-top
29 [ "$quiet" != "y" ] && zfs_log_end_msg
32 if type run_scripts > /dev/null 2>&1 && \
33 [ -f "/scripts/local-premount" -o -d "/scripts/local-premount" ]
35 [ "$quiet" != "y" ] && \
36 zfs_log_begin_msg "Running /scripts/local-premount"
37 run_scripts /scripts/local-premount
38 [ "$quiet" != "y" ] && zfs_log_end_msg
42 # If plymouth is availible, hide the splash image.
45 if [ -x /bin/plymouth ] && /bin/plymouth --ping
47 /bin/plymouth hide-splash >/dev/null 2>&1
51 # Get a ZFS filesystem property value.
57 "${ZFS}" get -H -ovalue $value "$fs" 2> /dev/null
60 # Find the 'bootfs' property on pool $1.
61 # If the property does not contain '/', then ignore this
62 # pool by exporting it again.
67 # If 'POOL_IMPORTED' isn't set, no pool imported and therefor
68 # we won't be able to find a root fs.
69 [ -z "${POOL_IMPORTED}" ] && return 1
71 # If it's already specified, just keep it mounted and exit
72 # User (kernel command line) must be correct.
73 [ -n "${ZFS_BOOTFS}" ] && return 0
75 # Not set, try to find it in the 'bootfs' property of the pool.
76 # NOTE: zpool does not support 'get -H -ovalue bootfs'...
77 ZFS_BOOTFS=$("${ZPOOL}" list -H -obootfs "$pool")
79 # Make sure it's not '-' and that it starts with /.
80 if [ "${ZFS_BOOTFS}" != "-" ] && \
81 $(get_fs_value "${ZFS_BOOTFS}" mountpoint | grep -q '^/$')
88 # Not boot fs here, export it and later try again..
89 "${ZPOOL}" export "$pool"
95 # Support function to get a list of all pools, separated with ';'
101 pools=$($CMD 2> /dev/null | \
102 grep -E "pool:|^[a-zA-Z0-9]" | \
104 while read pool; do \
108 echo "${pools%%;}" # Return without the last ';'.
111 # Get a list of all availible pools
114 local available_pools npools
116 if [ -n "${ZFS_POOL_IMPORT}" ]; then
117 echo "$ZFS_POOL_IMPORT"
121 # Get the base list of availible pools.
122 available_pools=$(find_pools "$ZPOOL" import)
124 # Just in case - seen it happen (that a pool isn't visable/found
125 # with a simple "zpool import" but only when using the "-d"
126 # option or setting ZPOOL_IMPORT_PATH).
127 if [ -d "/dev/disk/by-id" ]
129 npools=$(find_pools "$ZPOOL" import -d /dev/disk/by-id)
132 # Because we have found extra pool(s) here, which wasn't
133 # found 'normally', we need to force USE_DISK_BY_ID to
134 # make sure we're able to actually import it/them later.
137 if [ -n "$available_pools" ]
139 # Filter out duplicates (pools found with the simple
140 # "zpool import" but which is also found with the
141 # "zpool import -d ...").
142 npools=$(echo "$npools" | sed "s,$available_pools,,")
144 # Add the list to the existing list of
146 available_pools="$available_pools;$npools"
148 available_pools="$npools"
153 # Filter out any exceptions...
154 if [ -n "$ZFS_POOL_EXCEPTIONS" ]
159 OLD_IFS="$IFS" ; IFS=";"
161 for pool in $available_pools
163 for exception in $ZFS_POOL_EXCEPTIONS
165 [ "$pool" = "$exception" ] && continue 2
173 apools="$apools;$pool"
181 available_pools="$apools"
184 # Return list of availible pools.
185 echo "$available_pools"
188 # Import given pool $1
194 # Verify that the pool isn't already imported
195 # Make as sure as we can to not require '-f' to import.
196 "${ZPOOL}" status "$pool" > /dev/null 2>&1 && return 0
198 # For backwards compatibility, make sure that ZPOOL_IMPORT_PATH is set
199 # to something we can use later with the real import(s). We want to
200 # make sure we find all by* dirs, BUT by-vdev should be first (if it
202 if [ -n "$USE_DISK_BY_ID" -a -z "$ZPOOL_IMPORT_PATH" ]
204 dirs="$(for dir in $(echo /dev/disk/by-*)
206 # Ignore by-vdev here - we want it first!
207 echo "$dir" | grep -q /by-vdev && continue
208 [ ! -d "$dir" ] && continue
211 done | sed 's,:$,,g')"
213 if [ -d "/dev/disk/by-vdev" ]
215 # Add by-vdev at the beginning.
216 ZPOOL_IMPORT_PATH="/dev/disk/by-vdev:"
219 # ... and /dev at the very end, just for good measure.
220 ZPOOL_IMPORT_PATH="$ZPOOL_IMPORT_PATH$dirs:/dev"
223 # Needs to be exported for "zpool" to catch it.
224 [ -n "$ZPOOL_IMPORT_PATH" ] && export ZPOOL_IMPORT_PATH
227 [ "$quiet" != "y" ] && zfs_log_begin_msg \
228 "Importing pool '${pool}' using defaults"
230 ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
231 ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
233 if [ "${ZFS_ERROR}" != 0 ]
235 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
237 if [ -f "${ZPOOL_CACHE}" ]
239 [ "$quiet" != "y" ] && zfs_log_begin_msg \
240 "Importing pool '${pool}' using cachefile."
242 ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
243 ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
247 if [ "${ZFS_ERROR}" != 0 ]
249 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
253 echo "Command: ${ZFS_CMD} '$pool'"
254 echo "Message: $ZFS_STDERR"
255 echo "Error: $ZFS_ERROR"
257 echo "Failed to import pool '$pool'."
258 echo "Manually import the pool and exit."
263 [ "$quiet" != "y" ] && zfs_log_end_msg
270 # Loading a module in a initrd require a slightly different approach,
271 # with more logging etc.
274 if [ "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP" > 0 ]
276 if [ "$quiet" != "y" ]; then
277 zfs_log_begin_msg "Sleeping for" \
278 "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP seconds..."
280 sleep "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP"
281 [ "$quiet" != "y" ] && zfs_log_end_msg
284 # Wait for all of the /dev/{hd,sd}[a-z] device nodes to appear.
285 if type wait_for_udev > /dev/null 2>&1 ; then
287 elif type wait_for_dev > /dev/null 2>&1 ; then
291 # zpool import refuse to import without a valid /proc/self/mounts
292 [ ! -f /proc/self/mounts ] && mount proc /proc
295 load_module "zfs" || return 1
297 if [ "$ZFS_INITRD_POST_MODPROBE_SLEEP" > 0 ]
299 if [ "$quiet" != "y" ]; then
300 zfs_log_begin_msg "Sleeping for" \
301 "$ZFS_INITRD_POST_MODPROBE_SLEEP seconds..."
303 sleep "$ZFS_INITRD_POST_MODPROBE_SLEEP"
304 [ "$quiet" != "y" ] && zfs_log_end_msg
310 # Mount a given filesystem
316 # Check that the filesystem exists
317 "${ZFS}" list -oname -tfilesystem -H "${fs}" > /dev/null 2>&1
318 [ "$?" -ne 0 ] && return 1
320 # Need the _original_ datasets mountpoint!
321 mountpoint=$(get_fs_value "$fs" mountpoint)
322 if [ "$mountpoint" = "legacy" -o "$mountpoint" = "none" ]; then
323 # Can't use the mountpoint property. Might be one of our
324 # clones. Check the 'org.zol:mountpoint' property set in
325 # clone_snap() if that's usable.
326 mountpoint=$(get_fs_value "$fs" org.zol:mountpoint)
327 if [ "$mountpoint" = "legacy" -o \
328 "$mountpoint" = "none" -o \
329 "$mountpoint" = "-" ]
331 if [ "$fs" != "${ZFS_BOOTFS}" ]; then
332 # We don't have a proper mountpoint, this
333 # isn't the root fs. So extract the root fs
334 # value from the filesystem, and we should
335 # (hopefully!) have a mountpoint we can use.
336 mountpoint="${fs##$ZFS_BOOTFS}"
338 # Last hail-mary: Hope 'rootmnt' is set!
343 if [ "$mountpoint" = "legacy" ]; then
344 ZFS_CMD="mount -t zfs"
346 # If it's not a legacy filesystem, it can only be a
348 ZFS_CMD="mount -o zfsutil -t zfs"
351 ZFS_CMD="mount -o zfsutil -t zfs"
354 # Possibly decrypt a filesystem using native encryption.
357 [ "$quiet" != "y" ] && \
358 zfs_log_begin_msg "Mounting '${fs}' on '${rootmnt}/${mountpoint}'"
359 [ -n "${ZFS_DEBUG}" ] && \
360 zfs_log_begin_msg "CMD: '$ZFS_CMD ${fs} ${rootmnt}/${mountpoint}'"
362 ZFS_STDERR=$(${ZFS_CMD} "${fs}" "${rootmnt}/${mountpoint}" 2>&1)
364 if [ "${ZFS_ERROR}" != 0 ]
366 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
370 echo "Command: ${ZFS_CMD} ${fs} ${rootmnt}/${mountpoint}"
371 echo "Message: $ZFS_STDERR"
372 echo "Error: $ZFS_ERROR"
374 echo "Failed to mount ${fs} on ${rootmnt}/${mountpoint}."
375 echo "Manually mount the filesystem and exit."
378 [ "$quiet" != "y" ] && zfs_log_end_msg
384 # Unlock a ZFS native crypted filesystem.
389 # If the 'zfs key' command isn't availible, exit right here.
390 "${ZFS}" 2>&1 | grep -q 'key -l ' || return 0
392 # Check if filesystem is encrypted. If not, exit right here.
393 [ "$(get_fs_value "$fs" encryption)" != "off" ] || return 0
395 [ "$quiet" != "y" ] && \
396 zfs_log_begin_msg "Loading crypto wrapper key for $fs"
398 # Just make sure that ALL crypto modules module is loaded.
399 # Simplest just to load all...
400 for mod in sun-ccm sun-gcm sun-ctr
402 [ "$quiet" != "y" ] && zfs_log_progress_msg "${mod} "
404 ZFS_CMD="load_module $mod"
405 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
408 if [ "${ZFS_ERROR}" != 0 ]
410 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
414 echo "Command: $ZFS_CMD"
415 echo "Message: $ZFS_STDERR"
416 echo "Error: $ZFS_ERROR"
418 echo "Failed to load $mod module."
419 echo "Please verify that it is availible on the initrd image"
420 echo "(without it it won't be possible to unlock the filesystem)"
421 echo "and rerun: $ZFS_CMD"
424 [ "$quiet" != "y" ] && zfs_log_end_msg
428 # If the key isn't availible, then this will fail!
429 ZFS_CMD="${ZFS} key -l -r $fs"
430 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
433 if [ "${ZFS_ERROR}" != 0 ]
435 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
439 echo "Command: $ZFS_CMD"
440 echo "Message: $ZFS_STDERR"
441 echo "Error: $ZFS_ERROR"
443 echo "Failed to load zfs encryption wrapper key (s)."
444 echo "Please verify dataset property 'keysource' for datasets"
445 echo "and rerun: $ZFS_CMD"
448 [ "$quiet" != "y" ] && zfs_log_end_msg
454 # Destroy a given filesystem.
459 [ "$quiet" != "y" ] && \
460 zfs_log_begin_msg "Destroying '$fs'"
462 ZFS_CMD="${ZFS} destroy $fs"
463 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
465 if [ "${ZFS_ERROR}" != 0 ]
467 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
471 echo "Command: $ZFS_CMD"
472 echo "Message: $ZFS_STDERR"
473 echo "Error: $ZFS_ERROR"
475 echo "Failed to destroy '$fs'. Please make sure that '$fs' is not availible."
476 echo "Hint: Try: zfs destroy -Rfn $fs"
477 echo "If this dryrun looks good, then remove the 'n' from '-Rfn' and try again."
480 [ "$quiet" != "y" ] && zfs_log_end_msg
486 # Clone snapshot $1 to destination filesystem $2
487 # Set 'canmount=noauto' and 'mountpoint=none' so that we get to keep
488 # manual control over it's mounting (i.e., make sure it's not automatically
489 # mounted with a 'zfs mount -a' in the init/systemd scripts).
494 local mountpoint="$3"
496 [ "$quiet" != "y" ] && zfs_log_begin_msg "Cloning '$snap' to '$destfs'"
498 # Clone the snapshot into a dataset we can boot from
499 # + We don't want this filesystem to be automatically mounted, we
500 # want control over this here and nowhere else.
501 # + We don't need any mountpoint set for the same reason.
502 # We use the 'org.zol:mountpoint' property to remember the mountpoint.
503 ZFS_CMD="${ZFS} clone -o canmount=noauto -o mountpoint=none"
504 ZFS_CMD="${ZFS_CMD} -o org.zol:mountpoint=${mountpoint}"
505 ZFS_CMD="${ZFS_CMD} $snap $destfs"
506 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
508 if [ "${ZFS_ERROR}" != 0 ]
510 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
514 echo "Command: $ZFS_CMD"
515 echo "Message: $ZFS_STDERR"
516 echo "Error: $ZFS_ERROR"
518 echo "Failed to clone snapshot."
519 echo "Make sure that the any problems are corrected and then make sure"
520 echo "that the dataset '$destfs' exists and is bootable."
523 [ "$quiet" != "y" ] && zfs_log_end_msg
529 # Rollback a given snapshot.
534 [ "$quiet" != "y" ] && zfs_log_begin_msg "Rollback $snap"
536 ZFS_CMD="${ZFS} rollback -Rf $snap"
537 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
539 if [ "${ZFS_ERROR}" != 0 ]
541 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
545 echo "Command: $ZFS_CMD"
546 echo "Message: $ZFS_STDERR"
547 echo "Error: $ZFS_ERROR"
549 echo "Failed to rollback snapshot."
552 [ "$quiet" != "y" ] && zfs_log_end_msg
558 # Get a list of snapshots, give them as a numbered list
559 # to the user to choose from.
564 local SNAP snapnr snap debug
566 # We need to temporarily disable debugging. Set 'debug' so we
567 # remember to enabled it again.
568 if [ -n "${ZFS_DEBUG}" ]; then
574 # Because we need the resulting snapshot, which is sent on
575 # stdout to the caller, we use stderr for our questions.
576 echo "What snapshot do you want to boot from?" > /dev/stderr
578 echo " $i: ${snap}" > /dev/stderr
579 eval `echo SNAP_$i=$snap`
582 $("${ZFS}" list -H -oname -tsnapshot -r "${fs}")
585 echo -n " Snap nr [1-$((i-1))]? " > /dev/stderr
588 # Re-enable debugging.
589 if [ -n "${debug}" ]; then
594 echo "$(eval echo "$"SNAP_$snapnr)"
597 setup_snapshot_booting()
600 local s destfs subfs mountpoint retval=0 filesystems fs
602 # Make sure that the snapshot specified actually exist.
603 if [ ! $(get_fs_value "${snap}" type) ]
605 # Snapshot does not exist (...@<null> ?)
606 # ask the user for a snapshot to use.
607 snap="$(ask_user_snap "${snap%%@*}")"
610 # Separate the full snapshot ('$snap') into it's filesystem and
611 # snapshot names. Would have been nice with a split() function..
613 snapname="${snap##*@}"
614 ZFS_BOOTFS="${rootfs}_${snapname}"
616 if ! grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
618 # If the destination dataset for the clone
619 # already exists, destroy it. Recursivly
620 if [ $(get_fs_value "${rootfs}_${snapname}" type) ]; then
621 filesystems=$("${ZFS}" list -oname -tfilesystem -H \
622 -r -Sname "${ZFS_BOOTFS}")
623 for fs in $filesystems; do
629 # Get all snapshots, recursivly (might need to clone /usr, /var etc
631 for s in $("${ZFS}" list -H -oname -tsnapshot -r "${rootfs}" | \
634 if grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
637 rollback_snap "$s" || retval=$((retval + 1))
639 # Setup a destination filesystem name.
640 # Ex: Called with 'rpool/ROOT/debian@snap2'
641 # rpool/ROOT/debian@snap2 => rpool/ROOT/debian_snap2
642 # rpool/ROOT/debian/boot@snap2 => rpool/ROOT/debian_snap2/boot
643 # rpool/ROOT/debian/usr@snap2 => rpool/ROOT/debian_snap2/usr
644 # rpool/ROOT/debian/var@snap2 => rpool/ROOT/debian_snap2/var
645 subfs="${s##$rootfs}"
646 subfs="${subfs%%@$snapname}"
648 destfs="${rootfs}_${snapname}" # base fs.
649 [ -n "$subfs" ] && destfs="${destfs}$subfs" # + sub fs.
651 # Get the mountpoint of the filesystem, to be used
652 # with clone_snap(). If legacy or none, then use
654 mountpoint=$(get_fs_value "${s%%@*}" mountpoint)
655 if [ "$mountpoint" = "legacy" -o \
656 "$mountpoint" = "none" ]
658 if [ -n "${subfs}" ]; then
659 mountpoint="${subfs}"
665 # Clone the snapshot into its own
667 clone_snap "$s" "${destfs}" "${mountpoint}" || \
668 retval=$((retval + 1))
672 # If we haven't return yet, we have a problem...
676 # ================================================================
678 # This is the main function.
681 local snaporig snapsub destfs pool POOLS
683 # ----------------------------------------------------------------
684 # I N I T I A L S E T U P
687 # Run the pre-mount scripts from /scripts/local-top.
691 # Source the default setup variables.
692 [ -r '/etc/default/zfs' ] && . /etc/default/zfs
695 # Support debug option
696 if grep -qiE '(^|[^\\](\\\\)* )(zfs_debug|zfs\.debug|zfsdebug)=(on|yes|1)( |$)' /proc/cmdline
700 #exec 2> /var/log/boot.debug
705 # Load ZFS module etc.
706 if ! load_module_initrd; then
709 echo "Failed to load ZFS modules."
710 echo "Manually load the modules and exit."
715 # Look for the cache file (if any).
716 [ ! -f ${ZPOOL_CACHE} ] && unset ZPOOL_CACHE
719 # Compatibility: 'ROOT' is for Debian GNU/Linux (etc),
720 # 'root' is for Redhat/Fedora (etc),
721 # 'REAL_ROOT' is for Gentoo
724 [ -n "$root" ] && ROOT=${root}
726 [ -n "$REAL_ROOT" ] && ROOT=${REAL_ROOT}
730 # Where to mount the root fs in the initrd - set outside this script
731 # Compatibility: 'rootmnt' is for Debian GNU/Linux (etc),
732 # 'NEWROOT' is for RedHat/Fedora (etc),
733 # 'NEW_ROOT' is for Gentoo
736 [ -n "$NEWROOT" ] && rootmnt=${NEWROOT}
738 [ -n "$NEW_ROOT" ] && rootmnt=${NEW_ROOT}
742 # No longer set in the defaults file, but it could have been set in
743 # get_pools() in some circumstances. If it's something, but not 'yes',
744 # it's no good to us.
745 [ -n "$USE_DISK_BY_ID" -a "$USE_DISK_BY_ID" != 'yes' ] && \
748 # ----------------------------------------------------------------
749 # P A R S E C O M M A N D L I N E O P T I O N S
751 # This part is the really ugly part - there's so many options and permutations
752 # 'out there', and if we should make this the 'primary' source for ZFS initrd
753 # scripting, we need/should support them all.
755 # Supports the following kernel command line argument combinations
756 # (in this order - first match win):
758 # rpool=<pool> (tries to finds bootfs automatically)
759 # bootfs=<pool>/<dataset> (uses this for rpool - first part)
760 # rpool=<pool> bootfs=<pool>/<dataset>
761 # -B zfs-bootfs=<pool>/<fs> (uses this for rpool - first part)
762 # rpool=rpool (default if none of the above is used)
763 # root=<pool>/<dataset> (uses this for rpool - first part)
764 # root=ZFS=<pool>/<dataset> (uses this for rpool - first part, without 'ZFS=')
765 # root=zfs:AUTO (tries to detect both pool and rootfs
766 # root=zfs:<pool>/<dataset> (uses this for rpool - first part, without 'zfs:')
768 # Option <dataset> could also be <snapshot>
771 # Support force option
772 # In addition, setting one of zfs_force, zfs.force or zfsforce to
773 # 'yes', 'on' or '1' will make sure we force import the pool.
774 # This should (almost) never be needed, but it's here for
777 if grep -qiE '(^|[^\\](\\\\)* )(zfs_force|zfs\.force|zfsforce)=(on|yes|1)( |$)' /proc/cmdline
783 # Look for 'rpool' and 'bootfs' parameter
784 [ -n "$rpool" ] && ZFS_RPOOL="${rpool#rpool=}"
785 [ -n "$bootfs" ] && ZFS_BOOTFS="${bootfs#bootfs=}"
788 # If we have 'ROOT' (see above), but not 'ZFS_BOOTFS', then use
790 [ -n "$ROOT" -a -z "${ZFS_BOOTFS}" ] && ZFS_BOOTFS="$ROOT"
793 # Check for the `-B zfs-bootfs=%s/%u,...` kind of parameter.
794 # NOTE: Only use the pool name and dataset. The rest is not
795 # supported by ZoL (whatever it's for).
796 if [ -z "$ZFS_RPOOL" ]
798 # The ${zfs-bootfs} variable is set at the kernel command
799 # line, usually by GRUB, but it cannot be referenced here
800 # directly because bourne variable names cannot contain a
803 # Reassign the variable by dumping the environment and
804 # stripping the zfs-bootfs= prefix. Let the shell handle
805 # quoting through the eval command.
806 eval ZFS_RPOOL=$(set | sed -n -e 's,^zfs-bootfs=,,p')
810 # No root fs or pool specified - do auto detect.
811 if [ -z "$ZFS_RPOOL" -a -z "${ZFS_BOOTFS}" ]
813 # Do auto detect. Do this by 'cheating' - set 'root=zfs:AUTO'
814 # which will be caught later
818 # ----------------------------------------------------------------
819 # F I N D A N D I M P O R T C O R R E C T P O O L
822 if [ "$ROOT" = "zfs:AUTO" ]
824 # Try to detect both pool and root fs.
826 [ "$quiet" != "y" ] && \
827 zfs_log_begin_msg "Attempting to import additional pools."
829 # Get a list of pools available for import
830 if [ -n "$ZFS_RPOOL" ]
832 # We've specified a pool - check only that
838 OLD_IFS="$IFS" ; IFS=";"
841 [ -z "$pool" ] && continue
848 [ "$quiet" != "y" ] && zfs_log_end_msg $ZFS_ERROR
850 # No auto - use value from the command line option.
852 # Strip 'zfs:' and 'ZFS='.
853 ZFS_BOOTFS="${ROOT#*[:=]}"
855 # Stip everything after the first slash.
856 ZFS_RPOOL="${ZFS_BOOTFS%%/*}"
859 # Import the pool (if not already done so in the AUTO check above).
860 if [ -n "$ZFS_RPOOL" -a -z "${POOL_IMPORTED}" ]
862 [ "$quiet" != "y" ] && \
863 zfs_log_begin_msg "Importing ZFS root pool '$ZFS_RPOOL'"
865 import_pool "${ZFS_RPOOL}"
866 find_rootfs "${ZFS_RPOOL}"
868 [ "$quiet" != "y" ] && zfs_log_end_msg
871 if [ -z "${POOL_IMPORTED}" ]
873 # No pool imported, this is serious!
876 echo "Command: $ZFS_CMD"
877 echo "Message: $ZFS_STDERR"
878 echo "Error: $ZFS_ERROR"
880 echo "No pool imported. Manually import the root pool"
881 echo "at the command prompt and then exit."
882 echo "Hint: Try: zpool import -R ${rootmnt} -N ${ZFS_RPOOL}"
886 # ----------------------------------------------------------------
887 # P R E P A R E R O O T F I L E S Y S T E M
889 if [ -n "${ZFS_BOOTFS}" ]
891 # Booting from a snapshot?
892 # Will overwrite the ZFS_BOOTFS variable like so:
893 # rpool/ROOT/debian@snap2 => rpool/ROOT/debian_snap2
894 echo "${ZFS_BOOTFS}" | grep -q '@' && \
895 setup_snapshot_booting "${ZFS_BOOTFS}"
898 if [ -z "${ZFS_BOOTFS}" ]
900 # Still nothing! Let the user sort this out.
903 echo "Error: Unknown root filesystem - no 'bootfs' pool property and"
904 echo " not specified on the kernel command line."
906 echo "Manually mount the root filesystem on $rootmnt and then exit."
907 echo "Hint: Try: mount -o zfsutil -t zfs ${ZFS_RPOOL-rpool}/ROOT/system $rootmnt"
911 # ----------------------------------------------------------------
912 # M O U N T F I L E S Y S T E M S
914 # * Ideally, the root filesystem would be mounted like this:
916 # zpool import -R "$rootmnt" -N "$ZFS_RPOOL"
917 # zfs mount -o mountpoint=/ "${ZFS_BOOTFS}"
919 # but the MOUNTPOINT prefix is preserved on descendent filesystem
920 # after the pivot into the regular root, which later breaks things
921 # like `zfs mount -a` and the /proc/self/mounts refresh.
923 # * Mount additional filesystems required
924 # Such as /usr, /var, /usr/local etc.
925 # NOTE: Mounted in the order specified in the
926 # ZFS_INITRD_ADDITIONAL_DATASETS variable so take care!
928 # Go through the complete list (recursivly) of all filesystems below
929 # the real root dataset
930 filesystems=$("${ZFS}" list -oname -tfilesystem -H -r "${ZFS_BOOTFS}")
931 for fs in $filesystems $ZFS_INITRD_ADDITIONAL_DATASETS
937 # Debugging information
938 if [ -n "${ZFS_DEBUG}" ]
942 echo "DEBUG: imported pools:"
946 echo "DEBUG: mounted ZFS filesystems:"
950 echo "=> waiting for ENTER before continuing because of 'zfsdebug=1'. "
951 echo -n " 'c' for shell, 'r' for reboot, 'ENTER' to continue. "
954 [ "$b" = "c" ] && /bin/sh
955 [ "$b" = "r" ] && reboot -f
961 # Run local bottom script
962 if type run_scripts > /dev/null 2>&1 && \
963 [ -f "/scripts/local-bottom" -o -d "/scripts/local-bottom" ]
965 [ "$quiet" != "y" ] && \
966 zfs_log_begin_msg "Running /scripts/local-bottom"
967 run_scripts /scripts/local-bottom
968 [ "$quiet" != "y" ] && zfs_log_end_msg