2018-04-07 00:11:09 +03:00
|
|
|
#!/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.
|
|
|
|
|
2019-05-07 19:32:23 +03:00
|
|
|
set -e
|
2018-04-07 00:11:09 +03:00
|
|
|
|
|
|
|
FSLIST="@sysconfdir@/zfs/zfs-list.cache"
|
|
|
|
|
|
|
|
[ -d "${FSLIST}" ] || exit 0
|
|
|
|
|
|
|
|
do_fail() {
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: %s\n' "$*" > /dev/kmsg
|
2018-04-07 00:11:09 +03:00
|
|
|
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
|
2018-05-11 22:44:14 +03:00
|
|
|
# zfs list -H -t filesystem -o <properties>
|
2018-04-07 00:11:09 +03:00
|
|
|
# cached in $FSLIST, and each line is processed by the following function:
|
2018-05-11 22:44:14 +03:00
|
|
|
# See the list below for the properties and their order
|
2018-04-07 00:11:09 +03:00
|
|
|
|
|
|
|
process_line() {
|
|
|
|
|
2019-05-07 19:32:23 +03:00
|
|
|
# zfs list -H -o name,...
|
|
|
|
# fields are tab separated
|
|
|
|
IFS="$(printf '\t')"
|
|
|
|
# protect against special characters in, e.g., mountpoints
|
|
|
|
set -f
|
2020-01-11 21:14:23 +03:00
|
|
|
# shellcheck disable=SC2086
|
2019-05-07 19:32:23 +03:00
|
|
|
set -- $1
|
2018-05-11 22:44:14 +03:00
|
|
|
dataset="${1}"
|
|
|
|
p_mountpoint="${2}"
|
|
|
|
p_canmount="${3}"
|
|
|
|
p_atime="${4}"
|
|
|
|
p_relatime="${5}"
|
|
|
|
p_devices="${6}"
|
|
|
|
p_exec="${7}"
|
|
|
|
p_readonly="${8}"
|
|
|
|
p_setuid="${9}"
|
|
|
|
p_nbmand="${10}"
|
2019-06-02 15:57:10 +03:00
|
|
|
p_encroot="${11}"
|
|
|
|
p_keyloc="${12}"
|
2018-05-11 22:44:14 +03:00
|
|
|
|
2019-11-22 20:53:51 +03:00
|
|
|
# Minimal pre-requisites to mount a ZFS dataset
|
|
|
|
wants="zfs-import.target"
|
|
|
|
|
|
|
|
# Handle encryption
|
|
|
|
if [ -n "${p_encroot}" ] &&
|
|
|
|
[ "${p_encroot}" != "-" ] ; then
|
|
|
|
keyloadunit="zfs-load-key-$(systemd-escape "${p_encroot}").service"
|
|
|
|
if [ "${p_encroot}" = "${dataset}" ] ; then
|
|
|
|
pathdep=""
|
|
|
|
if [ "${p_keyloc%%://*}" = "file" ] ; then
|
|
|
|
pathdep="RequiresMountsFor='${p_keyloc#file://}'"
|
|
|
|
keyloadcmd="@sbindir@/zfs load-key '${dataset}'"
|
|
|
|
elif [ "${p_keyloc}" = "prompt" ] ; then
|
2020-01-11 21:14:23 +03:00
|
|
|
keyloadcmd="\
|
|
|
|
/bin/sh -c '\
|
|
|
|
set -eu;\
|
|
|
|
keystatus=\"\$\$(@sbindir@/zfs get -H -o value keystatus \"${dataset}\")\";\
|
|
|
|
[ \"\$\$keystatus\" = \"unavailable\" ] || exit 0;\
|
|
|
|
count=0;\
|
|
|
|
while [ \$\$count -lt 3 ];do\
|
|
|
|
systemd-ask-password --id=\"zfs:${dataset}\"\
|
|
|
|
\"Enter passphrase for ${dataset}:\"|\
|
|
|
|
@sbindir@/zfs load-key \"${dataset}\" && exit 0;\
|
|
|
|
count=\$\$((count + 1));\
|
|
|
|
done;\
|
|
|
|
exit 1'"
|
2019-11-22 20:53:51 +03:00
|
|
|
else
|
|
|
|
printf 'zfs-mount-generator: (%s) invalid keylocation\n' \
|
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Generate the key-load .service unit
|
2020-01-08 04:03:00 +03:00
|
|
|
#
|
|
|
|
# Note: It is tempting to use a `<<EOF` style here-document for this, but
|
|
|
|
# bash requires a writable /tmp or $TMPDIR for that. This is not always
|
|
|
|
# available early during boot.
|
|
|
|
#
|
|
|
|
echo \
|
|
|
|
"# Automatically generated by zfs-mount-generator
|
2019-11-22 20:53:51 +03:00
|
|
|
|
|
|
|
[Unit]
|
|
|
|
Description=Load ZFS key for ${dataset}
|
|
|
|
SourcePath=${cachefile}
|
|
|
|
Documentation=man:zfs-mount-generator(8)
|
|
|
|
DefaultDependencies=no
|
|
|
|
Wants=${wants}
|
|
|
|
After=${wants}
|
|
|
|
${pathdep}
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=oneshot
|
|
|
|
RemainAfterExit=yes
|
|
|
|
ExecStart=${keyloadcmd}
|
2020-01-08 04:03:00 +03:00
|
|
|
ExecStop=@sbindir@/zfs unload-key '${dataset}'" > "${dest_norm}/${keyloadunit}"
|
2019-11-22 20:53:51 +03:00
|
|
|
fi
|
|
|
|
# Update the dependencies for the mount file to require the
|
|
|
|
# key-loading unit.
|
|
|
|
wants="${wants} ${keyloadunit}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Prepare the .mount unit
|
|
|
|
|
2018-04-07 00:11:09 +03:00
|
|
|
# Check for canmount=off .
|
2018-05-11 22:44:14 +03:00
|
|
|
if [ "${p_canmount}" = "off" ] ; then
|
2018-04-07 00:11:09 +03:00
|
|
|
return
|
2018-05-11 22:44:14 +03:00
|
|
|
elif [ "${p_canmount}" = "noauto" ] ; then
|
2019-11-22 20:48:03 +03:00
|
|
|
# Don't let a noauto marked mountpoint block an "auto" marked mountpoint
|
2018-04-07 00:11:09 +03:00
|
|
|
return
|
2018-05-11 22:44:14 +03:00
|
|
|
elif [ "${p_canmount}" = "on" ] ; then
|
2018-04-07 00:11:09 +03:00
|
|
|
: # This is OK
|
|
|
|
else
|
|
|
|
do_fail "invalid canmount"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for legacy and blank mountpoints.
|
2018-05-11 22:44:14 +03:00
|
|
|
if [ "${p_mountpoint}" = "legacy" ] ; then
|
2018-04-07 00:11:09 +03:00
|
|
|
return
|
2018-05-11 22:44:14 +03:00
|
|
|
elif [ "${p_mountpoint}" = "none" ] ; then
|
2018-04-07 00:11:09 +03:00
|
|
|
return
|
2018-05-11 22:44:14 +03:00
|
|
|
elif [ "${p_mountpoint%"${p_mountpoint#?}"}" != "/" ] ; then
|
2018-04-07 00:11:09 +03:00
|
|
|
do_fail "invalid mountpoint $*"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Escape the mountpoint per systemd policy.
|
2020-02-13 22:55:59 +03:00
|
|
|
mountfile="$(systemd-escape --path --suffix=mount "${p_mountpoint}")"
|
2018-05-11 22:44:14 +03:00
|
|
|
|
|
|
|
# Parse options
|
|
|
|
# see lib/libzfs/libzfs_mount.c:zfs_add_options
|
|
|
|
opts=""
|
|
|
|
|
|
|
|
# atime
|
|
|
|
if [ "${p_atime}" = on ] ; then
|
|
|
|
# relatime
|
|
|
|
if [ "${p_relatime}" = on ] ; then
|
|
|
|
opts="${opts},atime,relatime"
|
|
|
|
elif [ "${p_relatime}" = off ] ; then
|
|
|
|
opts="${opts},atime,strictatime"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid relatime\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
elif [ "${p_atime}" = off ] ; then
|
|
|
|
opts="${opts},noatime"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid atime\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# devices
|
|
|
|
if [ "${p_devices}" = on ] ; then
|
|
|
|
opts="${opts},dev"
|
|
|
|
elif [ "${p_devices}" = off ] ; then
|
|
|
|
opts="${opts},nodev"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid devices\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# exec
|
|
|
|
if [ "${p_exec}" = on ] ; then
|
|
|
|
opts="${opts},exec"
|
|
|
|
elif [ "${p_exec}" = off ] ; then
|
|
|
|
opts="${opts},noexec"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid exec\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# readonly
|
|
|
|
if [ "${p_readonly}" = on ] ; then
|
|
|
|
opts="${opts},ro"
|
|
|
|
elif [ "${p_readonly}" = off ] ; then
|
|
|
|
opts="${opts},rw"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid readonly\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# setuid
|
|
|
|
if [ "${p_setuid}" = on ] ; then
|
|
|
|
opts="${opts},suid"
|
|
|
|
elif [ "${p_setuid}" = off ] ; then
|
|
|
|
opts="${opts},nosuid"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid setuid\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
|
|
|
|
|
|
|
# nbmand
|
|
|
|
if [ "${p_nbmand}" = on ] ; then
|
|
|
|
opts="${opts},mand"
|
|
|
|
elif [ "${p_nbmand}" = off ] ; then
|
|
|
|
opts="${opts},nomand"
|
|
|
|
else
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: (%s) invalid nbmand\n' \
|
2018-05-11 22:44:14 +03:00
|
|
|
"${dataset}" >/dev/kmsg
|
|
|
|
fi
|
2018-04-07 00:11:09 +03:00
|
|
|
|
|
|
|
# If the mountpoint has already been created, give it precedence.
|
|
|
|
if [ -e "${dest_norm}/${mountfile}" ] ; then
|
2018-06-07 19:38:39 +03:00
|
|
|
printf 'zfs-mount-generator: %s already exists\n' "${mountfile}" \
|
2018-04-07 00:11:09 +03:00
|
|
|
>/dev/kmsg
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2019-11-22 20:53:51 +03:00
|
|
|
# Create the .mount unit file.
|
2018-04-07 00:11:09 +03:00
|
|
|
# By ordering before zfs-mount.service, we avoid race conditions.
|
2020-01-08 04:03:00 +03:00
|
|
|
#
|
|
|
|
# (Do not use `<<EOF`-style here-documents for this, see warning above)
|
|
|
|
#
|
|
|
|
echo \
|
|
|
|
"# Automatically generated by zfs-mount-generator
|
2018-04-07 00:11:09 +03:00
|
|
|
|
|
|
|
[Unit]
|
2019-05-07 19:32:23 +03:00
|
|
|
SourcePath=${cachefile}
|
2018-04-07 00:11:09 +03:00
|
|
|
Documentation=man:zfs-mount-generator(8)
|
|
|
|
Before=local-fs.target zfs-mount.service
|
2019-06-02 15:57:10 +03:00
|
|
|
After=${wants}
|
|
|
|
Wants=${wants}
|
2018-04-07 00:11:09 +03:00
|
|
|
|
|
|
|
[Mount]
|
2018-05-11 22:44:14 +03:00
|
|
|
Where=${p_mountpoint}
|
|
|
|
What=${dataset}
|
2018-04-07 00:11:09 +03:00
|
|
|
Type=zfs
|
2020-01-08 04:03:00 +03:00
|
|
|
Options=defaults${opts},zfsutil" > "${dest_norm}/${mountfile}"
|
2018-04-07 00:11:09 +03:00
|
|
|
|
2018-05-11 22:44:14 +03:00
|
|
|
# Finally, create the appropriate dependency
|
|
|
|
ln -s "../${mountfile}" "${req_dir}"
|
2018-04-07 00:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Feed each line into process_line
|
2019-05-07 19:32:23 +03:00
|
|
|
for cachefile in "${FSLIST}/"* ; do
|
2018-04-07 00:11:09 +03:00
|
|
|
while read -r fs ; do
|
2019-05-07 19:32:23 +03:00
|
|
|
process_line "${fs}"
|
|
|
|
done < "${cachefile}"
|
2018-04-07 00:11:09 +03:00
|
|
|
done
|