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.
|
|
|
|
|
|
|
|
set -ef
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
2018-05-11 22:44:14 +03:00
|
|
|
# -o name
|
|
|
|
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}"
|
|
|
|
|
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
|
2018-04-07 00:11:09 +03:00
|
|
|
# Don't let a noauto marked mountpoint block an "auto" market mountpoint
|
|
|
|
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.
|
2018-05-11 22:44:14 +03:00
|
|
|
mountfile="$(systemd-escape "${p_mountpoint#?}").mount"
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# By ordering before zfs-mount.service, we avoid race conditions.
|
|
|
|
cat > "${dest_norm}/${mountfile}" << EOF
|
|
|
|
# Automatically generated by zfs-mount-generator
|
|
|
|
|
|
|
|
[Unit]
|
|
|
|
SourcePath=${FSLIST}/${cachefile}
|
|
|
|
Documentation=man:zfs-mount-generator(8)
|
|
|
|
Before=local-fs.target zfs-mount.service
|
|
|
|
After=zfs-import.target
|
|
|
|
Wants=zfs-import.target
|
|
|
|
|
|
|
|
[Mount]
|
2018-05-11 22:44:14 +03:00
|
|
|
Where=${p_mountpoint}
|
|
|
|
What=${dataset}
|
2018-04-07 00:11:09 +03:00
|
|
|
Type=zfs
|
2018-05-11 22:44:14 +03:00
|
|
|
Options=defaults${opts},zfsutil
|
2018-04-07 00:11:09 +03:00
|
|
|
EOF
|
|
|
|
|
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
|
|
|
|
for cachefile in $(ls "${FSLIST}") ; do
|
|
|
|
while read -r fs ; do
|
|
|
|
process_line $fs
|
|
|
|
done < "${FSLIST}/${cachefile}"
|
|
|
|
done
|