]>
Commit | Line | Data |
---|---|---|
ae26d046 MAR |
1 | #!/bin/sh |
2 | ||
eda3d4e1 | 3 | . /lib/dracut-zfs-lib.sh |
ae26d046 | 4 | |
eda3d4e1 ST |
5 | ZFS_DATASET="" |
6 | ZFS_POOL="" | |
a4719e54 | 7 | |
eda3d4e1 ST |
8 | case "${root}" in |
9 | zfs:*) ;; | |
10 | *) return ;; | |
11 | esac | |
fde4ce99 | 12 | |
b0f578a8 R |
13 | GENERATOR_FILE=/run/systemd/generator/sysroot.mount |
14 | GENERATOR_EXTENSION=/run/systemd/generator/sysroot.mount.d/zfs-enhancement.conf | |
15 | ||
16 | if [ -e "$GENERATOR_FILE" -a -e "$GENERATOR_EXTENSION" ] ; then | |
17 | # If the ZFS sysroot.mount flag exists, the initial RAM disk configured | |
18 | # it to mount ZFS on root. In that case, we bail early. This flag | |
19 | # file gets created by the zfs-generator program upon successful run. | |
20 | info "ZFS: There is a sysroot.mount and zfs-generator has extended it." | |
21 | info "ZFS: Delegating root mount to sysroot.mount." | |
22 | # Let us tell the initrd to run on shutdown. | |
23 | # We have a shutdown hook to run | |
24 | # because we imported the pool. | |
b0f578a8 R |
25 | # We now prevent Dracut from running this thing again. |
26 | for zfsmounthook in "$hookdir"/mount/*zfs* ; do | |
27 | if [ -f "$zfsmounthook" ] ; then | |
28 | rm -f "$zfsmounthook" | |
29 | fi | |
30 | done | |
31 | return | |
d402c18d | 32 | fi |
b0f578a8 R |
33 | info "ZFS: No sysroot.mount exists or zfs-generator did not extend it." |
34 | info "ZFS: Mounting root with the traditional mount-zfs.sh instead." | |
d402c18d | 35 | |
7b4536c7 GB |
36 | # Delay until all required block devices are present. |
37 | udevadm settle | |
38 | ||
eda3d4e1 ST |
39 | if [ "${root}" = "zfs:AUTO" ] ; then |
40 | ZFS_DATASET="$(find_bootfs)" | |
41 | if [ $? -ne 0 ] ; then | |
42 | zpool import -N -a ${ZPOOL_IMPORT_OPTS} | |
43 | ZFS_DATASET="$(find_bootfs)" | |
44 | if [ $? -ne 0 ] ; then | |
45 | warn "ZFS: No bootfs attribute found in importable pools." | |
7e8a2d0b | 46 | export_all -F |
eda3d4e1 ST |
47 | |
48 | rootok=0 | |
49 | return 1 | |
1ef5e829 | 50 | fi |
eda3d4e1 ST |
51 | fi |
52 | info "ZFS: Using ${ZFS_DATASET} as root." | |
53 | fi | |
fde4ce99 | 54 | |
eda3d4e1 ST |
55 | ZFS_DATASET="${ZFS_DATASET:-${root#zfs:}}" |
56 | ZFS_POOL="${ZFS_DATASET%%/*}" | |
07a3312f | 57 | |
eda3d4e1 | 58 | if import_pool "${ZFS_POOL}" ; then |
7da8f8d8 MT |
59 | # Load keys if we can or if we need to |
60 | if [ $(zpool list -H -o feature@encryption $(echo "${ZFS_POOL}" | awk -F\/ '{print $1}')) == 'active' ]; then | |
61 | # if the root dataset has encryption enabled | |
62 | if $(zfs list -H -o encryption "${ZFS_DATASET}" | grep -q -v off); then | |
63 | # figure out where the root dataset has its key, the keylocation should not be none | |
64 | while true; do | |
65 | if [[ $(zfs list -H -o keylocation "${ZFS_DATASET}") == 'none' ]]; then | |
66 | ZFS_DATASET=$(echo -n "${ZFS_DATASET}" | awk 'BEGIN{FS=OFS="/"}{NF--; print}') | |
67 | if [[ "${ZFS_DATASET}" == '' ]]; then | |
68 | rootok=0 | |
69 | break | |
70 | fi | |
71 | else | |
72 | rootok=1 | |
73 | break | |
74 | fi | |
75 | done | |
76 | [[ "${rootok}" -eq 0 ]]&& return 1 | |
77 | # decrypt them | |
78 | TRY_COUNT=5 | |
79 | while [ $TRY_COUNT != 0 ]; do | |
80 | zfs load-key "${ZFS_DATASET}" | |
81 | [ $? == 0 ] && break | |
82 | ((TRY_COUNT-=1)) | |
83 | done | |
84 | fi | |
85 | fi | |
b0f578a8 R |
86 | # Let us tell the initrd to run on shutdown. |
87 | # We have a shutdown hook to run | |
88 | # because we imported the pool. | |
eda3d4e1 ST |
89 | info "ZFS: Mounting dataset ${ZFS_DATASET}..." |
90 | if mount_dataset "${ZFS_DATASET}" ; then | |
91 | ROOTFS_MOUNTED=yes | |
92 | return 0 | |
93 | fi | |
94 | fi | |
95 | ||
96 | rootok=0 |