Skip to content

Commit

Permalink
systemd mount generator
Browse files Browse the repository at this point in the history
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.

zfs-list.cache is kept up-to-date by the zed event script
history_event-zfs-list-cacher.sh, which ensures that every listed
filesystem's entry in zfs-list.cache is synchronized with the
pool state. It is left to the system administrator's judgement
which ZFSs should be in this cache. ZFSs 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
aerusso committed Mar 26, 2018
1 parent 9ea6c3d commit 8778d62
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cmd/zed/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ DEFAULT_INCLUDES += \
-I$(top_srcdir)/include \
-I$(top_srcdir)/lib/libspl/include

EXTRA_DIST = zed.d/README
EXTRA_DIST = zed.d/README \
zed.d/history_event-zfs-list-cacher.sh.in

sbin_PROGRAMS = zed

Expand Down Expand Up @@ -59,6 +60,7 @@ dist_zedexec_SCRIPTS = \
zed.d/all-debug.sh \
zed.d/all-syslog.sh \
zed.d/data-notify.sh \
zed.d/history_event-zfs-list-cacher.sh \
zed.d/generic-notify.sh \
zed.d/resilver_finish-notify.sh \
zed.d/scrub_finish-notify.sh \
Expand All @@ -69,6 +71,13 @@ dist_zedexec_SCRIPTS = \
zed.d/pool_import-led.sh \
zed.d/resilver_finish-start-scrub.sh

zed.d/history_event-zfs-list-cacher.sh: %: %.in
-$(SED) -e 's,@bindir\@,$(bindir),g' \
-e 's,@runstatedir\@,$(runstatedir),g' \
-e 's,@sbindir\@,$(sbindir),g' \
-e 's,@sysconfdir\@,$(sysconfdir),g' \
$< >'$@'

zedconfdefaults = \
all-syslog.sh \
data-notify.sh \
Expand Down
1 change: 1 addition & 0 deletions cmd/zed/zed.d/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
history_event-zfs-list-cacher.sh
61 changes: 61 additions & 0 deletions cmd/zed/zed.d/history_event-zfs-list-cacher.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
#
# Track changes to specifically enumerated filesystems for system mounting.
set -e

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

# 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
}

case "${ZEVENT_HISTORY_INTERNAL_NAME}" in
destroy|rename)
# because the history event only reports the command issued, rather than
# the affected ZFS, a rename or recursive destroy can affect a child ZFS.
# Instead of trying to figure out if we're affected, we instead just
# regenerate the cache *for that pool* on every modification.
;;
set)
# only act if the mountpoint or canmount setting is altered
printf '%s' "${ZEVENT_HISTORY_INTERNAL_STR}" |
grep -q '^\(mountpoint\|canmount\)=' || exit 0
;;
*)
# ignore all other events
exit 0
;;
esac

zed_lock zfs-list
trap abort_alter EXIT

# Get the information for the affected pool
grep "^${ZEVENT_POOL}[/$(printf '\t')]" "${FSLIST}" |
grep -o '[^'"$(printf '\t')"'*' |
xargs @sbindir@/zfs list -H -oname,mountpoint,canmount \
>"${FSLIST_TMP}" 2>/dev/null || true
# Gather the old information for the unaffected pools
grep -v "^${ZEVENT_POOL}[/$(printf '\t')]" "${FSLIST}" >>"${FSLIST_TMP}"
# Sort the output so that it is stable
sort "${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}"

zed_unlock zfs-list
trap - EXIT
8 changes: 7 additions & 1 deletion config/user-systemd.m4
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ AC_DEFUN([ZFS_AC_CONFIG_USER_SYSTEMD], [
[install systemd module load files into dir [[/usr/lib/modules-load.d]]]),
systemdmoduleloaddir=$withval,systemdmodulesloaddir=/usr/lib/modules-load.d)
AC_ARG_WITH(systemdgeneratordir,
AC_HELP_STRING([--with-systemdgeneratordir=DIR],
[install systemd generators in dir [[/usr/lib/systemd/system-generators]]]),
systemdgeneratordir=$withval,systemdgeneratordir=/usr/lib/systemd/system-generators)
AS_IF([test "x$enable_systemd" = xcheck], [
AS_IF([systemctl --version >/dev/null 2>&1],
[enable_systemd=yes],
Expand All @@ -32,7 +37,7 @@ AC_DEFUN([ZFS_AC_CONFIG_USER_SYSTEMD], [
AS_IF([test "x$enable_systemd" = xyes], [
ZFS_INIT_SYSTEMD=systemd
ZFS_MODULE_LOAD=modules-load.d
DEFINE_SYSTEMD='--with systemd --define "_unitdir $(systemdunitdir)" --define "_presetdir $(systemdpresetdir)"'
DEFINE_SYSTEMD='--with systemd --define "_unitdir $(systemdunitdir)" --define "_presetdir $(systemdpresetdir)" --define "_generatordir $(systemdgeneratordir)"'
modulesloaddir=$systemdmodulesloaddir
],[
DEFINE_SYSTEMD='--without systemd'
Expand All @@ -43,5 +48,6 @@ AC_DEFUN([ZFS_AC_CONFIG_USER_SYSTEMD], [
AC_SUBST(DEFINE_SYSTEMD)
AC_SUBST(systemdunitdir)
AC_SUBST(systemdpresetdir)
AC_SUBST(systemdgeneratordir)
AC_SUBST(modulesloaddir)
])
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ AC_CONFIG_FILES([
etc/zfs/Makefile
etc/systemd/Makefile
etc/systemd/system/Makefile
etc/systemd/system-generators/Makefile
etc/sudoers.d/Makefile
etc/modules-load.d/Makefile
man/Makefile
Expand Down
2 changes: 1 addition & 1 deletion etc/systemd/Makefile.am
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUBDIRS = system
SUBDIRS = system system-generators
1 change: 1 addition & 0 deletions etc/systemd/system-generators/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zfs-mount-generator
15 changes: 15 additions & 0 deletions etc/systemd/system-generators/Makefile.am
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)
115 changes: 115 additions & 0 deletions etc/systemd/system-generators/zfs-mount-generator.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/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 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 [ "$(printf '%.1s' "${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}"
1 change: 1 addition & 0 deletions man/man8/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist_man_MANS = \
vdev_id.8 \
zdb.8 \
zfs.8 \
zfs-mount-generator.8 \
zfs-program.8 \
zgenhostid.8 \
zinject.8 \
Expand Down
45 changes: 45 additions & 0 deletions man/man8/zfs-mount-generator.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.TH "ZFS\-MOUNT\-GENERATOR" "8" "ZFS" "zfs-mount-generator" "\""
.SH "NAME"
zfs\-mount\-generator \- generates systemd mount units for zfs
.SH SYNOPSIS
.B /lib/systemd/system-generators/zfs\-mount\-generator
.sp
.SH DESCRIPTION
The zfs\-mount\-generator implements the \fBGenerators Specification\fP
of
.BR systemd (1),
and is called during early boot to generate
.BR systemd.mount (5)
units for ZFS filesystem. If pools are not imported before
systemd generators are run, information on ZFS mountpoints should
be stored: the output of the command
.PP
.RS 4
zfs list -H -t filesystem -oname,mountpoint,canmount
.RE
.PP
should be kept separate from the pool, at
.PP
.RS 4
@sysconfdir@/zfs/zfs-list.cache .
.RE
.PP
If this file exists, it will supplement (but not override) information
available at run-time about the ZFS mount points. Mount ordering and
dependencies are created for all listed filesystems. If a filesystem has
.BR canmount=on
and
.BR mountpoint ,
the
.BR auto
mount option will be set, and a dependency for
.BR local-fs.target
on the mount will be created.
.sp
.SH SEE ALSO
.BR zfs (5)
.BR zpool (5)
.BR systemd (1)
.BR systemd.target (5)
.BR systemd.special (7)
.BR systemd.mount (7)
1 change: 1 addition & 0 deletions rpm/generic/zfs.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ systemctl --system daemon-reload >/dev/null || true
/usr/lib/modules-load.d/*
%{_unitdir}/*
%{_presetdir}/*
%{_generatordir}/*
%else
%config(noreplace) %{_sysconfdir}/init.d/*
%config(noreplace) %{_initconfdir}/zfs
Expand Down

0 comments on commit 8778d62

Please sign in to comment.