]> git.proxmox.com Git - mirror_zfs-debian.git/blob - contrib/initramfs/scripts/zfs
4650c964655b9a6e9beff50bf25a4fd0b7ea8cd6
[mirror_zfs-debian.git] / contrib / initramfs / scripts / zfs
1 # ZFS boot stub for initramfs-tools.
2 #
3 # In the initramfs environment, the /init script sources this stub to
4 # override the default functions in the /scripts/local script.
5 #
6 # Enable this by passing boot=zfs on the kernel command line.
7 #
8
9 # Source the common init script
10 . /etc/zfs/zfs-functions
11
12 # Paths to what we need - in the initrd, these paths are hardcoded,
13 # so override the defines in zfs-functions.
14 ZFS="/sbin/zfs"
15 ZPOOL="/sbin/zpool"
16 ZPOOL_CACHE="/etc/zfs/zpool.cache"
17 export ZFS ZPOOL ZPOOL_CACHE
18
19 # This runs any scripts that should run before we start importing
20 # pools and mounting any filesystems.
21 pre_mountroot()
22 {
23 if type run_scripts > /dev/null 2>&1 && \
24 [ -f "/scripts/local-top" -o -d "/scripts/local-top" ]
25 then
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
30 fi
31
32 if type run_scripts > /dev/null 2>&1 && \
33 [ -f "/scripts/local-premount" -o -d "/scripts/local-premount" ]
34 then
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
39 fi
40 }
41
42 # If plymouth is availible, hide the splash image.
43 disable_plymouth()
44 {
45 if [ -x /bin/plymouth ] && /bin/plymouth --ping
46 then
47 /bin/plymouth hide-splash >/dev/null 2>&1
48 fi
49 }
50
51 # Get a ZFS filesystem property value.
52 get_fs_value()
53 {
54 local fs="$1"
55 local value=$2
56
57 "${ZFS}" get -H -ovalue $value "$fs" 2> /dev/null
58 }
59
60 # Find the 'bootfs' property on pool $1.
61 # If the property does not contain '/', then ignore this
62 # pool by exporting it again.
63 find_rootfs()
64 {
65 local pool="$1"
66
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
70
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
74
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")
78
79 # Make sure it's not '-' and that it starts with /.
80 if [ "${ZFS_BOOTFS}" != "-" ] && \
81 $(get_fs_value "${ZFS_BOOTFS}" mountpoint | grep -q '^/$')
82 then
83 # Keep it mounted
84 POOL_IMPORTED=1
85 return 0
86 fi
87
88 # Not boot fs here, export it and later try again..
89 "${ZPOOL}" export "$pool"
90 POOL_IMPORTED=""
91
92 return 1
93 }
94
95 # Support function to get a list of all pools, separated with ';'
96 find_pools()
97 {
98 local CMD="$*"
99 local pools pool
100
101 pools=$($CMD 2> /dev/null | \
102 grep -E "pool:|^[a-zA-Z0-9]" | \
103 sed 's@.*: @@' | \
104 while read pool; do \
105 echo -n "$pool;"
106 done)
107
108 echo "${pools%%;}" # Return without the last ';'.
109 }
110
111 # Get a list of all availible pools
112 get_pools()
113 {
114 local available_pools npools
115
116 if [ -n "${ZFS_POOL_IMPORT}" ]; then
117 echo "$ZFS_POOL_IMPORT"
118 return 0
119 fi
120
121 # Get the base list of availible pools.
122 available_pools=$(find_pools "$ZPOOL" import)
123
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" ]
128 then
129 npools=$(find_pools "$ZPOOL" import -d /dev/disk/by-id)
130 if [ -n "$npools" ]
131 then
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.
135 USE_DISK_BY_ID='yes'
136
137 if [ -n "$available_pools" ]
138 then
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,,")
143
144 # Add the list to the existing list of
145 # available pools
146 available_pools="$available_pools;$npools"
147 else
148 available_pools="$npools"
149 fi
150 fi
151 fi
152
153 # Filter out any exceptions...
154 if [ -n "$ZFS_POOL_EXCEPTIONS" ]
155 then
156 local found=""
157 local apools=""
158 local pool exception
159 OLD_IFS="$IFS" ; IFS=";"
160
161 for pool in $available_pools
162 do
163 for exception in $ZFS_POOL_EXCEPTIONS
164 do
165 [ "$pool" = "$exception" ] && continue 2
166 found="$pool"
167 done
168
169 if [ -n "$found" ]
170 then
171 if [ -n "$apools" ]
172 then
173 apools="$apools;$pool"
174 else
175 apools="$pool"
176 fi
177 fi
178 done
179
180 IFS="$OLD_IFS"
181 available_pools="$apools"
182 fi
183
184 # Return list of availible pools.
185 echo "$available_pools"
186 }
187
188 # Import given pool $1
189 import_pool()
190 {
191 local pool="$1"
192 local dirs dir
193
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
197
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
201 # exists).
202 if [ -n "$USE_DISK_BY_ID" -a -z "$ZPOOL_IMPORT_PATH" ]
203 then
204 dirs="$(for dir in $(echo /dev/disk/by-*)
205 do
206 # Ignore by-vdev here - we want it first!
207 echo "$dir" | grep -q /by-vdev && continue
208 [ ! -d "$dir" ] && continue
209
210 echo -n "$dir:"
211 done | sed 's,:$,,g')"
212
213 if [ -d "/dev/disk/by-vdev" ]
214 then
215 # Add by-vdev at the beginning.
216 ZPOOL_IMPORT_PATH="/dev/disk/by-vdev:"
217 fi
218
219 # ... and /dev at the very end, just for good measure.
220 ZPOOL_IMPORT_PATH="$ZPOOL_IMPORT_PATH$dirs:/dev"
221 fi
222
223 # Needs to be exported for "zpool" to catch it.
224 [ -n "$ZPOOL_IMPORT_PATH" ] && export ZPOOL_IMPORT_PATH
225
226
227 [ "$quiet" != "y" ] && zfs_log_begin_msg \
228 "Importing pool '${pool}' using defaults"
229
230 ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
231 ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
232 ZFS_ERROR="$?"
233 if [ "${ZFS_ERROR}" != 0 ]
234 then
235 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
236
237 if [ -f "${ZPOOL_CACHE}" ]
238 then
239 [ "$quiet" != "y" ] && zfs_log_begin_msg \
240 "Importing pool '${pool}' using cachefile."
241
242 ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
243 ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
244 ZFS_ERROR="$?"
245 fi
246
247 if [ "${ZFS_ERROR}" != 0 ]
248 then
249 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
250
251 disable_plymouth
252 echo ""
253 echo "Command: ${ZFS_CMD} '$pool'"
254 echo "Message: $ZFS_STDERR"
255 echo "Error: $ZFS_ERROR"
256 echo ""
257 echo "Failed to import pool '$pool'."
258 echo "Manually import the pool and exit."
259 /bin/sh
260 fi
261 fi
262
263 [ "$quiet" != "y" ] && zfs_log_end_msg
264
265 POOL_IMPORTED=1
266 return 0
267 }
268
269 # Load ZFS modules
270 # Loading a module in a initrd require a slightly different approach,
271 # with more logging etc.
272 load_module_initrd()
273 {
274 if [ "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP" > 0 ]
275 then
276 if [ "$quiet" != "y" ]; then
277 zfs_log_begin_msg "Sleeping for" \
278 "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP seconds..."
279 fi
280 sleep "$ZFS_INITRD_PRE_MOUNTROOT_SLEEP"
281 [ "$quiet" != "y" ] && zfs_log_end_msg
282 fi
283
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
286 wait_for_udev 10
287 elif type wait_for_dev > /dev/null 2>&1 ; then
288 wait_for_dev
289 fi
290
291 # zpool import refuse to import without a valid /proc/self/mounts
292 [ ! -f /proc/self/mounts ] && mount proc /proc
293
294 # Load the module
295 load_module "zfs" || return 1
296
297 if [ "$ZFS_INITRD_POST_MODPROBE_SLEEP" > 0 ]
298 then
299 if [ "$quiet" != "y" ]; then
300 zfs_log_begin_msg "Sleeping for" \
301 "$ZFS_INITRD_POST_MODPROBE_SLEEP seconds..."
302 fi
303 sleep "$ZFS_INITRD_POST_MODPROBE_SLEEP"
304 [ "$quiet" != "y" ] && zfs_log_end_msg
305 fi
306
307 return 0
308 }
309
310 # Mount a given filesystem
311 mount_fs()
312 {
313 local fs="$1"
314 local mountpoint
315
316 # Check that the filesystem exists
317 "${ZFS}" list -oname -tfilesystem -H "${fs}" > /dev/null 2>&1
318 [ "$?" -ne 0 ] && return 1
319
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" = "-" ]
330 then
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}"
337 else
338 # Last hail-mary: Hope 'rootmnt' is set!
339 mountpoint=""
340 fi
341 fi
342
343 if [ "$mountpoint" = "legacy" ]; then
344 ZFS_CMD="mount -t zfs"
345 else
346 # If it's not a legacy filesystem, it can only be a
347 # native one...
348 ZFS_CMD="mount -o zfsutil -t zfs"
349 fi
350 else
351 ZFS_CMD="mount -o zfsutil -t zfs"
352 fi
353
354 # Possibly decrypt a filesystem using native encryption.
355 decrypt_fs "$fs"
356
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}'"
361
362 ZFS_STDERR=$(${ZFS_CMD} "${fs}" "${rootmnt}/${mountpoint}" 2>&1)
363 ZFS_ERROR=$?
364 if [ "${ZFS_ERROR}" != 0 ]
365 then
366 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
367
368 disable_plymouth
369 echo ""
370 echo "Command: ${ZFS_CMD} ${fs} ${rootmnt}/${mountpoint}"
371 echo "Message: $ZFS_STDERR"
372 echo "Error: $ZFS_ERROR"
373 echo ""
374 echo "Failed to mount ${fs} on ${rootmnt}/${mountpoint}."
375 echo "Manually mount the filesystem and exit."
376 /bin/sh
377 else
378 [ "$quiet" != "y" ] && zfs_log_end_msg
379 fi
380
381 return 0
382 }
383
384 # Unlock a ZFS native crypted filesystem.
385 decrypt_fs()
386 {
387 local fs="$1"
388
389 # If the 'zfs key' command isn't availible, exit right here.
390 "${ZFS}" 2>&1 | grep -q 'key -l ' || return 0
391
392 # Check if filesystem is encrypted. If not, exit right here.
393 [ "$(get_fs_value "$fs" encryption)" != "off" ] || return 0
394
395 [ "$quiet" != "y" ] && \
396 zfs_log_begin_msg "Loading crypto wrapper key for $fs"
397
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
401 do
402 [ "$quiet" != "y" ] && zfs_log_progress_msg "${mod} "
403
404 ZFS_CMD="load_module $mod"
405 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
406 ZFS_ERROR="$?"
407
408 if [ "${ZFS_ERROR}" != 0 ]
409 then
410 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
411
412 disable_plymouth
413 echo ""
414 echo "Command: $ZFS_CMD"
415 echo "Message: $ZFS_STDERR"
416 echo "Error: $ZFS_ERROR"
417 echo ""
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"
422 /bin/sh
423 else
424 [ "$quiet" != "y" ] && zfs_log_end_msg
425 fi
426 done
427
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)"
431 ZFS_ERROR="$?"
432
433 if [ "${ZFS_ERROR}" != 0 ]
434 then
435 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
436
437 disable_plymouth
438 echo ""
439 echo "Command: $ZFS_CMD"
440 echo "Message: $ZFS_STDERR"
441 echo "Error: $ZFS_ERROR"
442 echo ""
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"
446 /bin/sh
447 else
448 [ "$quiet" != "y" ] && zfs_log_end_msg
449 fi
450
451 return 0
452 }
453
454 # Destroy a given filesystem.
455 destroy_fs()
456 {
457 local fs="$1"
458
459 [ "$quiet" != "y" ] && \
460 zfs_log_begin_msg "Destroying '$fs'"
461
462 ZFS_CMD="${ZFS} destroy $fs"
463 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
464 ZFS_ERROR="$?"
465 if [ "${ZFS_ERROR}" != 0 ]
466 then
467 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
468
469 disable_plymouth
470 echo ""
471 echo "Command: $ZFS_CMD"
472 echo "Message: $ZFS_STDERR"
473 echo "Error: $ZFS_ERROR"
474 echo ""
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."
478 /bin/sh
479 else
480 [ "$quiet" != "y" ] && zfs_log_end_msg
481 fi
482
483 return 0
484 }
485
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).
490 clone_snap()
491 {
492 local snap="$1"
493 local destfs="$2"
494 local mountpoint="$3"
495
496 [ "$quiet" != "y" ] && zfs_log_begin_msg "Cloning '$snap' to '$destfs'"
497
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)"
507 ZFS_ERROR="$?"
508 if [ "${ZFS_ERROR}" != 0 ]
509 then
510 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
511
512 disable_plymouth
513 echo ""
514 echo "Command: $ZFS_CMD"
515 echo "Message: $ZFS_STDERR"
516 echo "Error: $ZFS_ERROR"
517 echo ""
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."
521 /bin/sh
522 else
523 [ "$quiet" != "y" ] && zfs_log_end_msg
524 fi
525
526 return 0
527 }
528
529 # Rollback a given snapshot.
530 rollback_snap()
531 {
532 local snap="$1"
533
534 [ "$quiet" != "y" ] && zfs_log_begin_msg "Rollback $snap"
535
536 ZFS_CMD="${ZFS} rollback -Rf $snap"
537 ZFS_STDERR="$(${ZFS_CMD} 2>&1)"
538 ZFS_ERROR="$?"
539 if [ "${ZFS_ERROR}" != 0 ]
540 then
541 [ "$quiet" != "y" ] && zfs_log_failure_msg "${ZFS_ERROR}"
542
543 disable_plymouth
544 echo ""
545 echo "Command: $ZFS_CMD"
546 echo "Message: $ZFS_STDERR"
547 echo "Error: $ZFS_ERROR"
548 echo ""
549 echo "Failed to rollback snapshot."
550 /bin/sh
551 else
552 [ "$quiet" != "y" ] && zfs_log_end_msg
553 fi
554
555 return 0
556 }
557
558 # Get a list of snapshots, give them as a numbered list
559 # to the user to choose from.
560 ask_user_snap()
561 {
562 local fs="$1"
563 local i=1
564 local SNAP snapnr snap debug
565
566 # We need to temporarily disable debugging. Set 'debug' so we
567 # remember to enabled it again.
568 if [ -n "${ZFS_DEBUG}" ]; then
569 unset ZFS_DEBUG
570 set +x
571 debug=1
572 fi
573
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
577 while read snap; do
578 echo " $i: ${snap}" > /dev/stderr
579 eval `echo SNAP_$i=$snap`
580 i=$((i + 1))
581 done <<EOT
582 $("${ZFS}" list -H -oname -tsnapshot -r "${fs}")
583 EOT
584
585 echo -n " Snap nr [1-$((i-1))]? " > /dev/stderr
586 read snapnr
587
588 # Re-enable debugging.
589 if [ -n "${debug}" ]; then
590 ZFS_DEBUG=1
591 set -x
592 fi
593
594 echo "$(eval echo "$"SNAP_$snapnr)"
595 }
596
597 setup_snapshot_booting()
598 {
599 local snap="$1"
600 local s destfs subfs mountpoint retval=0 filesystems fs
601
602 # Make sure that the snapshot specified actually exist.
603 if [ ! $(get_fs_value "${snap}" type) ]
604 then
605 # Snapshot does not exist (...@<null> ?)
606 # ask the user for a snapshot to use.
607 snap="$(ask_user_snap "${snap%%@*}")"
608 fi
609
610 # Separate the full snapshot ('$snap') into it's filesystem and
611 # snapshot names. Would have been nice with a split() function..
612 rootfs="${snap%%@*}"
613 snapname="${snap##*@}"
614 ZFS_BOOTFS="${rootfs}_${snapname}"
615
616 if ! grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
617 then
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
624 destroy_fs "${fs}"
625 done
626 fi
627 fi
628
629 # Get all snapshots, recursivly (might need to clone /usr, /var etc
630 # as well).
631 for s in $("${ZFS}" list -H -oname -tsnapshot -r "${rootfs}" | \
632 grep "${snapname}")
633 do
634 if grep -qiE '(^|[^\\](\\\\)* )(rollback)=(on|yes|1)( |$)' /proc/cmdline
635 then
636 # Rollback snapshot
637 rollback_snap "$s" || retval=$((retval + 1))
638 else
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}"
647
648 destfs="${rootfs}_${snapname}" # base fs.
649 [ -n "$subfs" ] && destfs="${destfs}$subfs" # + sub fs.
650
651 # Get the mountpoint of the filesystem, to be used
652 # with clone_snap(). If legacy or none, then use
653 # the sub fs value.
654 mountpoint=$(get_fs_value "${s%%@*}" mountpoint)
655 if [ "$mountpoint" = "legacy" -o \
656 "$mountpoint" = "none" ]
657 then
658 if [ -n "${subfs}" ]; then
659 mountpoint="${subfs}"
660 else
661 mountpoint="/"
662 fi
663 fi
664
665 # Clone the snapshot into its own
666 # filesystem
667 clone_snap "$s" "${destfs}" "${mountpoint}" || \
668 retval=$((retval + 1))
669 fi
670 done
671
672 # If we haven't return yet, we have a problem...
673 return "${retval}"
674 }
675
676 # ================================================================
677
678 # This is the main function.
679 mountroot()
680 {
681 local snaporig snapsub destfs pool POOLS
682
683 # ----------------------------------------------------------------
684 # I N I T I A L S E T U P
685
686 # ------------
687 # Run the pre-mount scripts from /scripts/local-top.
688 pre_mountroot
689
690 # ------------
691 # Source the default setup variables.
692 [ -r '/etc/default/zfs' ] && . /etc/default/zfs
693
694 # ------------
695 # Support debug option
696 if grep -qiE '(^|[^\\](\\\\)* )(zfs_debug|zfs\.debug|zfsdebug)=(on|yes|1)( |$)' /proc/cmdline
697 then
698 ZFS_DEBUG=1
699 mkdir /var/log
700 #exec 2> /var/log/boot.debug
701 set -x
702 fi
703
704 # ------------
705 # Load ZFS module etc.
706 if ! load_module_initrd; then
707 disable_plymouth
708 echo ""
709 echo "Failed to load ZFS modules."
710 echo "Manually load the modules and exit."
711 /bin/sh
712 fi
713
714 # ------------
715 # Look for the cache file (if any).
716 [ ! -f ${ZPOOL_CACHE} ] && unset ZPOOL_CACHE
717
718 # ------------
719 # Compatibility: 'ROOT' is for Debian GNU/Linux (etc),
720 # 'root' is for Redhat/Fedora (etc),
721 # 'REAL_ROOT' is for Gentoo
722 if [ -z "$ROOT" ]
723 then
724 [ -n "$root" ] && ROOT=${root}
725
726 [ -n "$REAL_ROOT" ] && ROOT=${REAL_ROOT}
727 fi
728
729 # ------------
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
734 if [ -z "$rootmnt" ]
735 then
736 [ -n "$NEWROOT" ] && rootmnt=${NEWROOT}
737
738 [ -n "$NEW_ROOT" ] && rootmnt=${NEW_ROOT}
739 fi
740
741 # ------------
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' ] && \
746 unset USE_DISK_BY_ID
747
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
750
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.
754 #
755 # Supports the following kernel command line argument combinations
756 # (in this order - first match win):
757 #
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:')
767 #
768 # Option <dataset> could also be <snapshot>
769
770 # ------------
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
775 # completeness.
776 ZPOOL_FORCE=""
777 if grep -qiE '(^|[^\\](\\\\)* )(zfs_force|zfs\.force|zfsforce)=(on|yes|1)( |$)' /proc/cmdline
778 then
779 ZPOOL_FORCE="-f"
780 fi
781
782 # ------------
783 # Look for 'rpool' and 'bootfs' parameter
784 [ -n "$rpool" ] && ZFS_RPOOL="${rpool#rpool=}"
785 [ -n "$bootfs" ] && ZFS_BOOTFS="${bootfs#bootfs=}"
786
787 # ------------
788 # If we have 'ROOT' (see above), but not 'ZFS_BOOTFS', then use
789 # 'ROOT'
790 [ -n "$ROOT" -a -z "${ZFS_BOOTFS}" ] && ZFS_BOOTFS="$ROOT"
791
792 # ------------
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" ]
797 then
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
801 # hyphen.
802 #
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')
807 fi
808
809 # ------------
810 # No root fs or pool specified - do auto detect.
811 if [ -z "$ZFS_RPOOL" -a -z "${ZFS_BOOTFS}" ]
812 then
813 # Do auto detect. Do this by 'cheating' - set 'root=zfs:AUTO'
814 # which will be caught later
815 ROOT=zfs:AUTO
816 fi
817
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
820
821 # ------------
822 if [ "$ROOT" = "zfs:AUTO" ]
823 then
824 # Try to detect both pool and root fs.
825
826 [ "$quiet" != "y" ] && \
827 zfs_log_begin_msg "Attempting to import additional pools."
828
829 # Get a list of pools available for import
830 if [ -n "$ZFS_RPOOL" ]
831 then
832 # We've specified a pool - check only that
833 POOLS=$ZFS_RPOOL
834 else
835 POOLS=$(get_pools)
836 fi
837
838 OLD_IFS="$IFS" ; IFS=";"
839 for pool in $POOLS
840 do
841 [ -z "$pool" ] && continue
842
843 import_pool "$pool"
844 find_rootfs "$pool"
845 done
846 IFS="$OLD_IFS"
847
848 [ "$quiet" != "y" ] && zfs_log_end_msg $ZFS_ERROR
849 else
850 # No auto - use value from the command line option.
851
852 # Strip 'zfs:' and 'ZFS='.
853 ZFS_BOOTFS="${ROOT#*[:=]}"
854
855 # Stip everything after the first slash.
856 ZFS_RPOOL="${ZFS_BOOTFS%%/*}"
857 fi
858
859 # Import the pool (if not already done so in the AUTO check above).
860 if [ -n "$ZFS_RPOOL" -a -z "${POOL_IMPORTED}" ]
861 then
862 [ "$quiet" != "y" ] && \
863 zfs_log_begin_msg "Importing ZFS root pool '$ZFS_RPOOL'"
864
865 import_pool "${ZFS_RPOOL}"
866 find_rootfs "${ZFS_RPOOL}"
867
868 [ "$quiet" != "y" ] && zfs_log_end_msg
869 fi
870
871 if [ -z "${POOL_IMPORTED}" ]
872 then
873 # No pool imported, this is serious!
874 disable_plymouth
875 echo ""
876 echo "Command: $ZFS_CMD"
877 echo "Message: $ZFS_STDERR"
878 echo "Error: $ZFS_ERROR"
879 echo ""
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}"
883 /bin/sh
884 fi
885
886 # ----------------------------------------------------------------
887 # P R E P A R E R O O T F I L E S Y S T E M
888
889 if [ -n "${ZFS_BOOTFS}" ]
890 then
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}"
896 fi
897
898 if [ -z "${ZFS_BOOTFS}" ]
899 then
900 # Still nothing! Let the user sort this out.
901 disable_plymouth
902 echo ""
903 echo "Error: Unknown root filesystem - no 'bootfs' pool property and"
904 echo " not specified on the kernel command line."
905 echo ""
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"
908 /bin/sh
909 fi
910
911 # ----------------------------------------------------------------
912 # M O U N T F I L E S Y S T E M S
913
914 # * Ideally, the root filesystem would be mounted like this:
915 #
916 # zpool import -R "$rootmnt" -N "$ZFS_RPOOL"
917 # zfs mount -o mountpoint=/ "${ZFS_BOOTFS}"
918 #
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.
922 #
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!
927
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
932 do
933 mount_fs "$fs"
934 done
935
936 # ------------
937 # Debugging information
938 if [ -n "${ZFS_DEBUG}" ]
939 then
940 #exec 2>&1-
941
942 echo "DEBUG: imported pools:"
943 "${ZPOOL}" list -H
944 echo
945
946 echo "DEBUG: mounted ZFS filesystems:"
947 mount | grep zfs
948 echo
949
950 echo "=> waiting for ENTER before continuing because of 'zfsdebug=1'. "
951 echo -n " 'c' for shell, 'r' for reboot, 'ENTER' to continue. "
952 read b
953
954 [ "$b" = "c" ] && /bin/sh
955 [ "$b" = "r" ] && reboot -f
956
957 set +x
958 fi
959
960 # ------------
961 # Run local bottom script
962 if type run_scripts > /dev/null 2>&1 && \
963 [ -f "/scripts/local-bottom" -o -d "/scripts/local-bottom" ]
964 then
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
969 fi
970 }