-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zfs-mount-generator implements the "systemd generator" protocol, producing systemd.mount units from the (possibly cached) output of zfs list during early boot, integrating with systemd. Among other things, this allows for complex mount hierarchies. Signed-off-by: Antonio Russo <[email protected]>
- Loading branch information
Showing
9 changed files
with
202 additions
and
2 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
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,130 @@ | ||
#!/bin/sh | ||
|
||
# zfs-mount-generator - generates systemd mount units for zfs | ||
# Copyright (c) 2017 Antonio Russo <[email protected]> | ||
# | ||
# 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" | ||
|
||
do_fail() { | ||
printf '%s\n' "$*" >&2 | ||
exit 1 | ||
} | ||
|
||
# We don't use dest_late | ||
if [ $# -eq 0 ] ; then | ||
dest_norm="/tmp" | ||
#dest_late="/tmp" | ||
elif [ $# -eq 3 ] ; then | ||
dest_norm="${1}" | ||
#dest_late="${3}" | ||
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 | ||
# 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="${2#?}" | ||
mountfile="$(systemd-escape "${mountfile}").mount" | ||
|
||
# If the mountpoint has already been created, | ||
# give it precedence. | ||
[ -e "${dest_norm}/${mountfile}" ] && return | ||
|
||
# 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]" | ||
[ -n "${SOURCEPATH}" ] && out "${SOURCEPATH}" | ||
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}" | ||
} | ||
|
||
# First, try listing ZFSs directly: | ||
(@sbindir@/zfs list -H -t filesystem -oname,mountpoint,canmount 2>/dev/null || | ||
true) | while read -r fs ; do | ||
process_line $fs | ||
done | ||
|
||
# At the early stage that systemd generators are called, | ||
# pools may not be imported, and the above may fail. | ||
|
||
# $FSLIST, if available, is a cache of the output of | ||
# the above command. Process it if it exists. | ||
|
||
if [ -r "${FSLIST}" ] ; then | ||
SOURCEPATH="SourcePath=${FSLIST}" | ||
while read -r fs ; do | ||
process_line $fs | ||
done < "${FSLIST}" | ||
fi |
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,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) |
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