]> git.proxmox.com Git - mirror_zfs.git/blob - contrib/dracut/90zfs/mount-zfs.sh.in
contrib; dracut: centralise root= parsing, actually support root=s
[mirror_zfs.git] / contrib / dracut / 90zfs / mount-zfs.sh.in
1 #!/bin/sh
2 # shellcheck disable=SC2034,SC2154
3
4 . /lib/dracut-zfs-lib.sh
5
6 decode_root_args || return 0
7
8 GENERATOR_FILE=/run/systemd/generator/sysroot.mount
9 GENERATOR_EXTENSION=/run/systemd/generator/sysroot.mount.d/zfs-enhancement.conf
10
11 if [ -e "$GENERATOR_FILE" ] && [ -e "$GENERATOR_EXTENSION" ]; then
12 # We're under systemd and dracut-zfs-generator ran to completion.
13 info "ZFS: Delegating root mount to sysroot.mount at al."
14
15 # We now prevent Dracut from running this thing again.
16 rm -f "$hookdir"/mount/*zfs*
17 return
18 fi
19
20 info "ZFS: No sysroot.mount exists or zfs-generator did not extend it."
21 info "ZFS: Mounting root with the traditional mount-zfs.sh instead."
22
23 # Delay until all required block devices are present.
24 modprobe zfs 2>/dev/null
25 udevadm settle
26
27 ZFS_DATASET=
28 ZFS_POOL=
29
30 if [ "${root}" = "zfs:AUTO" ] ; then
31 if ! ZFS_DATASET="$(find_bootfs)" ; then
32 # shellcheck disable=SC2086
33 zpool import -N -a ${ZPOOL_IMPORT_OPTS}
34 if ! ZFS_DATASET="$(find_bootfs)" ; then
35 warn "ZFS: No bootfs attribute found in importable pools."
36 zpool export -aF
37
38 rootok=0
39 return 1
40 fi
41 fi
42 info "ZFS: Using ${ZFS_DATASET} as root."
43 fi
44
45 ZFS_DATASET="${ZFS_DATASET:-${root}}"
46 ZFS_POOL="${ZFS_DATASET%%/*}"
47
48 if import_pool "${ZFS_POOL}" ; then
49 # Load keys if we can or if we need to
50 if [ "$(zpool list -H -o feature@encryption "${ZFS_POOL}")" = 'active' ]; then
51 # if the root dataset has encryption enabled
52 ENCRYPTIONROOT="$(zfs get -H -o value encryptionroot "${ZFS_DATASET}")"
53 if ! [ "${ENCRYPTIONROOT}" = "-" ]; then
54 KEYSTATUS="$(zfs get -H -o value keystatus "${ENCRYPTIONROOT}")"
55 # if the key needs to be loaded
56 if [ "$KEYSTATUS" = "unavailable" ]; then
57 # decrypt them
58 ask_for_password \
59 --tries 5 \
60 --prompt "Encrypted ZFS password for ${ENCRYPTIONROOT}: " \
61 --cmd "zfs load-key '${ENCRYPTIONROOT}'"
62 fi
63 fi
64 fi
65 # Let us tell the initrd to run on shutdown.
66 # We have a shutdown hook to run
67 # because we imported the pool.
68 info "ZFS: Mounting dataset ${ZFS_DATASET}..."
69 if mount_dataset "${ZFS_DATASET}" ; then
70 ROOTFS_MOUNTED=yes
71 return 0
72 fi
73 fi
74
75 rootok=0