-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
systemd mount generator and tracking ZEDLET
zfs-mount-generator implements the "systemd generator" protocol, producing systemd.mount units from the cached output of zfs list, zfs-list.cache, during early boot, integrating with systemd. This files contains two kinds of lines: (1) the output of zfs list -H -oname,mountpoint,canmount and (2) lines beginning with "+ " and then a (grep) regular expression matching the dataset names that should be added, if they are created, received, or imported. The file is kept up-to-date by the zed event script history_event-zfs-list-cacher.sh, which ensures that every listed dataset in zfs-list.cache is synchronized with the pool, in addition to adding new datasets matching the aforementioned regular expressions, removing destroyed datasets, and tracking renamed datasets. Datasets not in this cache will be loaded later in the boot process by zfs-mount.service. Among other things, this allows for complex mount hierarchies. Signed-off-by: Antonio Russo <antonio.e.russo@gmail.com>
- Loading branch information
Showing
12 changed files
with
354 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
history_event-zfs-list-cacher.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#!/bin/sh | ||
# | ||
# Track changes to specifically enumerated filesystems for system mounting. | ||
set -ef | ||
|
||
FSLIST="@sysconfdir@/zfs/zfs-list.cache" | ||
FSLIST_TMP="@runstatedir@/zfs-list.cache.new" | ||
|
||
# If it is not writeable, then do not attempt to modify | ||
[ -w "${FSLIST}" ] || exit 0 | ||
# If it is empty, abort | ||
[ "$(wc -l < "${FSLIST}")" -eq 0 ] && exit 0 | ||
|
||
[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc" | ||
. "${ZED_ZEDLET_DIR}/zed-functions.sh" | ||
|
||
zed_exit_if_ignoring_this_event | ||
zed_check_cmd "${ZFS}" wc sed sort diff grep cut xargs | ||
|
||
# If we are acting on a snapshot, we have nothing to do | ||
printf '%s' "${ZEVENT_HISTORY_DSNAME}" | grep '@' && ecit 0 | ||
|
||
# We obtain a lock on zfs-list to avoid any simultaneous writes. | ||
# If we run into trouble, log and drop the lock | ||
abort_alter() { | ||
zed_log_msg "Error updating zfs-list.cache!" | ||
zed_unlock zfs-list | ||
} | ||
|
||
zed_lock zfs-list | ||
trap abort_alter EXIT | ||
|
||
finished() { | ||
zed_unlock zfs-list | ||
trap - EXIT | ||
exit 0 | ||
} | ||
|
||
RECURSE_DS="^${ZEVENT_HISTORY_DSNAME}[/$(printf '\t')]" | ||
|
||
# Get all affected entries | ||
affected_ents() { | ||
grep "${RECURSE_DS}" "${FSLIST}" || true | ||
} | ||
|
||
# Regenerate entries for listed datasets | ||
regenerate() { | ||
xargs "${ZFS}" list -H -oname,mountpoint,canmount > "${FSLIST_TMP}" | ||
} | ||
|
||
case "${ZEVENT_HISTORY_INTERNAL_NAME}" in | ||
create|"finish receiving"|import) | ||
# We handle filesystem creation, receiving, and pool import | ||
# slightly differently than other cases. First, preserve all existing | ||
# entries: | ||
cp "${FSLIST}" "${FSLIST_TMP}" | ||
{ | ||
# Gather all new candidate dataset entries | ||
if [ "${ZEVENT_HISTORY_INTERNAL_NAME}" = "import" ] ; then | ||
"${ZFS}" list -H -tfilesystem -oname,mountpoint,canmount -r "${ZEVENT_POOL}" | ||
else | ||
"${ZFS}" list -H -oname,mountpoint,canmount "${ZEVENT_HISTORY_DSNAME}" | ||
fi | ||
} | while read -r ENTRY ; do | ||
# For each entry in the cache file beginning with a +, check if | ||
# the rest of that entry, treated as a regular expression, matches | ||
# the candidate dataset name | ||
grep "^+ " "${FSLIST}" | cut -d' ' -f2 | while read -r pattern ; do | ||
printf '%s' "${ENTRY%% *}" | grep -q "${pattern}" || continue | ||
# If it does, include it | ||
printf '%s' "${ENTRY}" >> "${FSLIST_TMP}" | ||
break | ||
done | ||
done | ||
;; | ||
|
||
export) | ||
# Handle pool export by simply dropping all references to that pool. | ||
grep -v "^${ZEVENT_POOL}[/$(printf '\t')]" "${FSLIST}" \ | ||
>> "${FSLIST_TMP}" || true | ||
;; | ||
|
||
*) | ||
# The remaining cases do not involve the creation of a new entry; rather, | ||
# they involve maintaining the reference to an already tracked entry. | ||
case "${ZEVENT_HISTORY_INTERNAL_NAME}" in | ||
destroy) | ||
rm -f "${FSLIST_TMP}" | ||
# We discard the affected entries. | ||
;; | ||
|
||
rename) | ||
# Regenerate all entries for the pool, tracking the changed dataset name. | ||
# To avoid escaping slashes for sed, all slashes are converted to commas. | ||
# **Notice that, even if this entry was initially tracked because it matched | ||
# a regular expression, that is NOT a requirement to remain tracked.** | ||
DSNAME="$(printf '%s' "${ZEVENT_HISTORY_DSNAME}" | tr / , )" | ||
NEW="$(printf '%s' "${ZEVENT_HISTORY_INTERNAL_STR}" | cut -c4- | tr / ,)" | ||
affected_ents | cut -f1 | tr / , | sed "s/${DSNAME}/${NEW}/" | tr , / | | ||
regenerate | ||
|
||
;; | ||
|
||
set|inherit) | ||
# Only act if the mountpoint or canmount setting is altered. | ||
case "${ZEVENT_HISTORY_INTERNAL_STR}" in | ||
canmount=*|mountpoint=*) ;; | ||
*) finished | ||
esac | ||
|
||
affected_ents | cut -f1 | regenerate_ents | ||
|
||
;; | ||
|
||
*) | ||
# Ignore all other events. | ||
finished | ||
;; | ||
|
||
esac | ||
|
||
# Keep all the unaffected entries | ||
grep -v "${RECURSE_DS}" "${FSLIST}" >> "${FSLIST_TMP}" || true | ||
;; | ||
esac | ||
|
||
# Sort the output so that it is stable | ||
sort "${FSLIST_TMP}" -o "${FSLIST_TMP}" | ||
|
||
# Don't modify the file if it hasn't changed | ||
diff -q "${FSLIST_TMP}" "${FSLIST}" || mv "${FSLIST_TMP}" "${FSLIST}" | ||
rm -f "${FSLIST_TMP}" | ||
|
||
finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
SUBDIRS = system | ||
SUBDIRS = system system-generators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
zfs-mount-generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
systemdgenerator_SCRIPTS = \ | ||
zfs-mount-generator | ||
|
||
EXTRA_DIST = \ | ||
$(top_srcdir)/etc/systemd/system-generators/zfs-mount-generator.in | ||
|
||
$(systemdgenerator_SCRIPTS): %: %.in | ||
-$(SED) -e 's,@bindir\@,$(bindir),g' \ | ||
-e 's,@runstatedir\@,$(runstatedir),g' \ | ||
-e 's,@sbindir\@,$(sbindir),g' \ | ||
-e 's,@sysconfdir\@,$(sysconfdir),g' \ | ||
$< >'$@' | ||
|
||
distclean-local:: | ||
-$(RM) $(systemdgenerator_SCRIPTS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/bin/sh | ||
|
||
# zfs-mount-generator - generates systemd mount units for zfs | ||
# Copyright (c) 2017 Antonio Russo <antonio.e.russo@gmail.com> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
set -ef | ||
|
||
FSLIST="@sysconfdir@/zfs/zfs-list.cache" | ||
|
||
[ -r "${FSLIST}" ] || exit 0 | ||
|
||
do_fail() { | ||
printf 'zfs-mount-generator.sh: %s\n' "$*" > /dev/kmsg | ||
exit 1 | ||
} | ||
|
||
# see systemd.generator | ||
if [ $# -eq 0 ] ; then | ||
dest_norm="/tmp" | ||
elif [ $# -eq 3 ] ; then | ||
dest_norm="${1}" | ||
else | ||
do_fail "zero or three arguments required" | ||
fi | ||
|
||
# For ZFSs marked "auto", a dependency is created for local-fs.target. To | ||
# avoid regressions, this dependency is reduced to "wants" rather than | ||
# "requires". **THIS MAY CHANGE** | ||
req_dir="${dest_norm}/local-fs.target.wants/" | ||
mkdir -p "${req_dir}" | ||
|
||
# All needed information about each ZFS is available from | ||
# zfs list -H -t filesystem -oname,mountpoint,canmount | ||
# cached in $FSLIST, and each line is processed by the following function: | ||
|
||
process_line() { | ||
# Check if we're looking at a tracking directive | ||
[ "${1}" = '+' ] && return | ||
# I'm putting this here in case someone in the future thinks that it is a | ||
# good idea to put a "negative" matching regular expression or something | ||
# like that, and won't have to worry about breaking people's init systems | ||
[ "${1}" = '-' ] && return | ||
|
||
# Check for canmount=off . | ||
if [ "${3}" = "off" ] ; then | ||
return | ||
elif [ "${3}" = "on" ] ; then | ||
auto="auto" | ||
elif [ "${3}" = "noauto" ] ; then | ||
auto="noauto" | ||
else | ||
do_fail "invalid canmount" | ||
fi | ||
|
||
# Check for legacy and blank mountpoints. | ||
if [ "${2}" = "legacy" ] ; then | ||
return | ||
elif [ "${2}" = "none" ] ; then | ||
return | ||
elif [ "${2%"${2#?}"}" != "/" ] ; then | ||
do_fail "invalid mountpoint $*" | ||
fi | ||
|
||
# Escape the mountpoint per systemd policy. | ||
mountfile="$(systemd-escape "${2#?}").mount" | ||
|
||
# If the mountpoint has already been created, give it precedence. | ||
if [ -e "${dest_norm}/${mountfile}" ] ; then | ||
printf 'zfs-mount-generator.sh: %s.mount already exists\n' "${2}" \ | ||
>/dev/kmsg | ||
return | ||
fi | ||
|
||
# heredocs may not work in very early boot, so we'll use printf instead. | ||
out() { | ||
printf '%s\n' "$*" >> "${dest_norm}/${mountfile}" | ||
} | ||
|
||
# dump the mount unit | ||
out "# Automatically generated by zfs-mount-generator" | ||
out "" | ||
out "[Unit]" | ||
out "SourcePath=${FSLIST}" | ||
out "Documentation=man:zfs-mount-generator(8)" | ||
# By ordering before zfs-mount.service, we avoid race conditions. | ||
out "Before=local-fs.target zfs-mount.service" | ||
out "After=zfs-import.target" | ||
out "Wants=zfs-import.target" | ||
out "" | ||
out "[Mount]" | ||
out "Where=${2}" | ||
out "What=${1}" | ||
out "Type=zfs" | ||
out "Options=zfsutil,${auto}" | ||
|
||
# Finally, create the appropriate dependencies based on the ZFS properties. | ||
[ "$3" = "on" ] & ln -s "../${mountfile}" "${req_dir}" | ||
} | ||
|
||
# Feed each line into process_line | ||
while read -r fs ; do | ||
process_line $fs | ||
done < "${FSLIST}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.