1#!/bin/sh
2#
3# Track changes to enumerated pools for use in early-boot
4set -ef
5
6FSLIST_DIR="@sysconfdir@/zfs/zfs-list.cache"
7FSLIST_TMP="@runstatedir@/zfs-list.cache.new"
8FSLIST="${FSLIST_DIR}/${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
16zed_exit_if_ignoring_this_event
17zed_check_cmd "${ZFS}" sort diff grep
18
19# If we are acting on a snapshot, we have nothing to do
20printf '%s' "${ZEVENT_HISTORY_DSNAME}" | grep '@' && exit 0
21
22# We obtain a lock on zfs-list to avoid any simultaneous writes.
23# If we run into trouble, log and drop the lock
24abort_alter() {
25  zed_log_msg "Error updating zfs-list.cache!"
26  zed_unlock zfs-list
27}
28
29finished() {
30  zed_unlock zfs-list
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 zfs-list
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 zfs-list
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}" || mv "${FSLIST_TMP}" "${FSLIST}"
83rm -f "${FSLIST_TMP}"
84
85finished
86