1#!/bin/sh
2# shellcheck disable=SC2154
3#
4# Track changes to enumerated pools for use in early-boot
5set -ef
6
7FSLIST="@sysconfdir@/zfs/zfs-list.cache/${ZEVENT_POOL}"
8FSLIST_TMP="@runstatedir@/zfs-list.cache@${ZEVENT_POOL}"
9
10# If the pool specific cache file is not writeable, abort
11[ -w "${FSLIST}" ] || exit 0
12
13[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
14. "${ZED_ZEDLET_DIR}/zed-functions.sh"
15
16[ "$ZEVENT_SUBCLASS" != "history_event" ] && exit 0
17zed_check_cmd "${ZFS}" sort diff
18
19# If we are acting on a snapshot, we have nothing to do
20[ "${ZEVENT_HISTORY_DSNAME%@*}" = "${ZEVENT_HISTORY_DSNAME}" ] || exit 0
21
22# We lock the output file to avoid simultaneous writes.
23# If we run into trouble, log and drop the lock
24abort_alter() {
25  zed_log_msg "Error updating zfs-list.cache for ${ZEVENT_POOL}!"
26  zed_unlock "${FSLIST}"
27}
28
29finished() {
30  zed_unlock "${FSLIST}"
31  trap - EXIT
32  exit 0
33}
34
35case "${ZEVENT_HISTORY_INTERNAL_NAME}" in
36    create|"finish receiving"|import|destroy|rename)
37      ;;
38
39    export)
40        zed_lock "${FSLIST}"
41        trap abort_alter EXIT
42        echo > "${FSLIST}"
43        finished
44      ;;
45
46    set|inherit)
47        # Only act if one of the tracked properties is altered.
48        case "${ZEVENT_HISTORY_INTERNAL_STR%%=*}" in
49            canmount|mountpoint|atime|relatime|devices|exec|readonly| \
50              setuid|nbmand|encroot|keylocation|org.openzfs.systemd:requires| \
51              org.openzfs.systemd:requires-mounts-for| \
52              org.openzfs.systemd:before|org.openzfs.systemd:after| \
53              org.openzfs.systemd:wanted-by|org.openzfs.systemd:required-by| \
54              org.openzfs.systemd:nofail|org.openzfs.systemd:ignore \
55            ) ;;
56            *) exit 0 ;;
57        esac
58      ;;
59
60    *)
61        # Ignore all other events.
62        exit 0
63      ;;
64esac
65
66zed_lock "${FSLIST}"
67trap abort_alter EXIT
68
69PROPS="name,mountpoint,canmount,atime,relatime,devices,exec\
70,readonly,setuid,nbmand,encroot,keylocation\
71,org.openzfs.systemd:requires,org.openzfs.systemd:requires-mounts-for\
72,org.openzfs.systemd:before,org.openzfs.systemd:after\
73,org.openzfs.systemd:wanted-by,org.openzfs.systemd:required-by\
74,org.openzfs.systemd:nofail,org.openzfs.systemd:ignore"
75
76"${ZFS}" list -H -t filesystem -o "${PROPS}" -r "${ZEVENT_POOL}" > "${FSLIST_TMP}"
77
78# Sort the output so that it is stable
79sort "${FSLIST_TMP}" -o "${FSLIST_TMP}"
80
81# Don't modify the file if it hasn't changed
82diff -q "${FSLIST_TMP}" "${FSLIST}" || cat "${FSLIST_TMP}" > "${FSLIST}"
83rm -f "${FSLIST_TMP}"
84
85finished
86