Init script fixes

* Fix regression - "OVERLAY_MOUNTS" should have been "DO_OVERLAY_MOUNTS".
* Fix update-rc.d commands in postinst.  Thanx to subzero79@GitHub.
* Fix make sure a filesystem exists before trying to mount in mount_fs()
* Fix local variable usage.
* Fix to read_mtab():
  * Strip control characters (space - \040) from /proc/mounts GLOBALY,
    not just first occurrence.
  * Don't replace unprintable characters ([/-. ]) for use in the variable
    name with underscore. No need, just remove them all together.
* Add check_boolean() to check if a user configure option is
  set ('yes', 'Yes', 'YES' or any combination there of) OR '1'.
  Anything else is considered 'unset'.
* Add a ZFS_POOL_IMPORT to the default config.
  * This is a semi colon separated list of pools to import ONLY.
  * This is intended for systems which have _a lot_ of pools (from
    a SAN for example) and it would be to many to put in the
    ZFS_POOL_EXCEPTIONS variable..
* Add a config option "ZPOOL_IMPORT_OPTS" for adding additional options
  to "zpool import".
* Add documentation and the chance of overriding the ZPOOL_CACHE
  variable in the config file.
* Remove "sort" from find_pools() and setup_snapshot_booting().
  Sometimes not available, and not really necessary.

Signed-off-by: Turbo Fredriksson <turbo@bayour.com>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ned Bass <bass6@llnl.gov>
Issue #3816
This commit is contained in:
Turbo Fredriksson 2015-09-22 09:56:28 +02:00 committed by Brian Behlendorf
parent 45838e3a41
commit 57732964d3
7 changed files with 88 additions and 63 deletions

View File

@ -96,12 +96,11 @@ find_rootfs()
find_pools() find_pools()
{ {
local CMD="$*" local CMD="$*"
local pools local pools pool
pools=$($CMD 2> /dev/null | \ pools=$($CMD 2> /dev/null | \
grep -E "pool:|^[a-zA-Z0-9]" | \ grep -E "pool:|^[a-zA-Z0-9]" | \
sed 's@.*: @@' | \ sed 's@.*: @@' | \
sort | \
while read pool; do \ while read pool; do \
echo -n "$pool;" echo -n "$pool;"
done) done)
@ -114,6 +113,11 @@ get_pools()
{ {
local available_pools npools local available_pools npools
if [ -n "${ZFS_POOL_IMPORT}" ]; then
echo "$ZFS_POOL_IMPORT"
return 0
fi
# Get the base list of availible pools. # Get the base list of availible pools.
available_pools=$(find_pools "$ZPOOL" import) available_pools=$(find_pools "$ZPOOL" import)
@ -185,7 +189,7 @@ get_pools()
import_pool() import_pool()
{ {
local pool="$1" local pool="$1"
local dirs dir ZFS_CMD ZFS_STDERR ZFS_ERROR local dirs dir
# Verify that the pool isn't already imported # Verify that the pool isn't already imported
# Make as sure as we can to not require '-f' to import. # Make as sure as we can to not require '-f' to import.
@ -223,7 +227,7 @@ import_pool()
[ "$quiet" != "y" ] && zfs_log_begin_msg \ [ "$quiet" != "y" ] && zfs_log_begin_msg \
"Importing pool '${pool}' using defaults" "Importing pool '${pool}' using defaults"
ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE}" ZFS_CMD="${ZPOOL} import -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)" ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
ZFS_ERROR="$?" ZFS_ERROR="$?"
if [ "${ZFS_ERROR}" != 0 ] if [ "${ZFS_ERROR}" != 0 ]
@ -235,7 +239,7 @@ import_pool()
[ "$quiet" != "y" ] && zfs_log_begin_msg \ [ "$quiet" != "y" ] && zfs_log_begin_msg \
"Importing pool '${pool}' using cachefile." "Importing pool '${pool}' using cachefile."
ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE}" ZFS_CMD="${ZPOOL} import -c ${ZPOOL_CACHE} -N ${ZPOOL_FORCE} ${ZPOOL_IMPORT_OPTS}"
ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)" ZFS_STDERR="$($ZFS_CMD "$pool" 2>&1)"
ZFS_ERROR="$?" ZFS_ERROR="$?"
fi fi
@ -308,7 +312,11 @@ load_module_initrd()
mount_fs() mount_fs()
{ {
local fs="$1" local fs="$1"
local mountpoint ZFS_CMD ZFS_STDERR ZFS_ERROR local mountpoint
# Check that the filesystem exists
"${ZFS}" list -oname -tfilesystem -H "${fs}" > /dev/null 2>&1
[ "$?" -ne 0 ] && return 1
# Need the _original_ datasets mountpoint! # Need the _original_ datasets mountpoint!
mountpoint=$(get_fs_value "$fs" mountpoint) mountpoint=$(get_fs_value "$fs" mountpoint)
@ -378,7 +386,6 @@ mount_fs()
decrypt_fs() decrypt_fs()
{ {
local fs="$1" local fs="$1"
local ZFS_CMD ZFS_STDERR ZFS_ERROR
# If the 'zfs key' command isn't availible, exit right here. # If the 'zfs key' command isn't availible, exit right here.
"${ZFS}" 2>&1 | grep -q 'key -l ' || return 0 "${ZFS}" 2>&1 | grep -q 'key -l ' || return 0
@ -449,7 +456,6 @@ decrypt_fs()
destroy_fs() destroy_fs()
{ {
local fs="$1" local fs="$1"
local ZFS_CMD ZFS_STDERR ZFS_ERROR
[ "$quiet" != "y" ] && \ [ "$quiet" != "y" ] && \
zfs_log_begin_msg "Destroying '$fs'" zfs_log_begin_msg "Destroying '$fs'"
@ -487,7 +493,6 @@ clone_snap()
local snap="$1" local snap="$1"
local destfs="$2" local destfs="$2"
local mountpoint="$3" local mountpoint="$3"
local ZFS_CMD ZFS_STDERR ZFS_ERROR
[ "$quiet" != "y" ] && zfs_log_begin_msg "Cloning '$snap' to '$destfs'" [ "$quiet" != "y" ] && zfs_log_begin_msg "Cloning '$snap' to '$destfs'"
@ -526,7 +531,6 @@ clone_snap()
rollback_snap() rollback_snap()
{ {
local snap="$1" local snap="$1"
local ZFS_CMD ZFS_STDERR ZFS_ERROR
[ "$quiet" != "y" ] && zfs_log_begin_msg "Rollback $snap" [ "$quiet" != "y" ] && zfs_log_begin_msg "Rollback $snap"
@ -616,7 +620,7 @@ setup_snapshot_booting()
# already exists, destroy it. Recursivly # already exists, destroy it. Recursivly
if [ $(get_fs_value "${rootfs}_${snapname}" type) ]; then if [ $(get_fs_value "${rootfs}_${snapname}" type) ]; then
filesystems=$("${ZFS}" list -oname -tfilesystem -H \ filesystems=$("${ZFS}" list -oname -tfilesystem -H \
-r "${ZFS_BOOTFS}" | sort -r) -r -Sname "${ZFS_BOOTFS}")
for fs in $filesystems; do for fs in $filesystems; do
destroy_fs "${fs}" destroy_fs "${fs}"
done done
@ -675,9 +679,7 @@ setup_snapshot_booting()
# This is the main function. # This is the main function.
mountroot() mountroot()
{ {
local snaporig local snaporig snapsub destfs pool POOLS
local snapsub
local destfs
# ---------------------------------------------------------------- # ----------------------------------------------------------------
# I N I T I A L S E T U P # I N I T I A L S E T U P

View File

@ -42,9 +42,9 @@ INSTALLING INIT SCRIPT LINKS
To setup the init script links in /etc/rc?.d manually on a Debian GNU/Linux To setup the init script links in /etc/rc?.d manually on a Debian GNU/Linux
(or derived) system, run the following commands (the order is important!): (or derived) system, run the following commands (the order is important!):
update-rc.d zfs-import start 07 S . stop 08 0 1 6 . update-rc.d zfs-import start 07 S . stop 07 0 1 6 .
update-rc.d zfs-mount start 02 2 3 4 5 . stop 07 0 1 6 . update-rc.d zfs-mount start 02 2 3 4 5 . stop 06 0 1 6 .
update-rc.d zfs-zed start 26 2 3 4 5 . stop 06 0 1 6 . update-rc.d zfs-zed start 07 2 3 4 5 . stop 08 0 1 6 .
update-rc.d zfs-share start 27 2 3 4 5 . stop 05 0 1 6 . update-rc.d zfs-share start 27 2 3 4 5 . stop 05 0 1 6 .
To do the same on RedHat, Fedora and/or CentOS: To do the same on RedHat, Fedora and/or CentOS:

View File

@ -310,6 +310,15 @@ get_root_pool()
[ "$5" = "zfs" ] && echo "${1%%/*}" [ "$5" = "zfs" ] && echo "${1%%/*}"
} }
# Check if a variable is 'yes' (any case) or '1'
# Returns TRUE if set.
check_boolean()
{
local var="$1"
echo "$var" | grep -Eiq "^yes$|^on$|^true$|^1$" && return 0 || return 1
}
check_module_loaded() check_module_loaded()
{ {
module="$1" module="$1"
@ -348,12 +357,12 @@ read_mtab()
# * We need to use the external echo, because the # * We need to use the external echo, because the
# internal one would interpret the backslash code # internal one would interpret the backslash code
# (incorrectly), giving us a  instead. # (incorrectly), giving us a  instead.
mntpnt=$(/bin/echo "$mntpnt" | sed "s,\\\0,\\\00,") mntpnt=$(/bin/echo "$mntpnt" | sed "s,\\\0,\\\00,g")
fs=$(/bin/echo "$fs" | sed "s,\\\0,\\\00,") fs=$(/bin/echo "$fs" | sed "s,\\\0,\\\00,")
# Replace 'unwanted' characters with underscore. # Remove 'unwanted' characters.
mntpnt=$(printf '%b\n' "$mntpnt" | sed -e 's,/,_,g' \ mntpnt=$(printf '%b\n' "$mntpnt" | sed -e 's,/,,g' \
-e 's,-,_,g' -e 's,\.,_,g' -e 's, ,_,g') -e 's,-,,g' -e 's,\.,,g' -e 's, ,,g')
fs=$(printf '%b\n' "$fs") fs=$(printf '%b\n' "$fs")
# Set the variable. # Set the variable.

View File

@ -209,7 +209,7 @@ do_import()
# Import by using ZPOOL_IMPORT_PATH (either set above or in # Import by using ZPOOL_IMPORT_PATH (either set above or in
# the config file) _or_ with the 'built in' default search # the config file) _or_ with the 'built in' default search
# paths. This is the prefered way. # paths. This is the prefered way.
"$ZPOOL" import -N "$pool" 2> /dev/null "$ZPOOL" import -N ${ZPOOL_IMPORT_OPTS} "$pool" 2> /dev/null
r="$?" ; RET=$((RET + r)) r="$?" ; RET=$((RET + r))
if [ "$r" -eq 0 ] if [ "$r" -eq 0 ]
then then
@ -224,13 +224,14 @@ do_import()
if [ "$r" -gt 0 -a -f "$ZPOOL_CACHE" ] if [ "$r" -gt 0 -a -f "$ZPOOL_CACHE" ]
then then
# Failed to import without a cache file. Try WITH... # Failed to import without a cache file. Try WITH...
if [ -z "$init" -a "$VERBOSE_MOUNT" = 'yes' ] if [ -z "$init" ] && check_boolean "$VERBOSE_MOUNT"
then then
# Interactive + Verbose = more information # Interactive + Verbose = more information
zfs_log_progress_msg " using cache file" zfs_log_progress_msg " using cache file"
fi fi
"$ZPOOL" import -c "$ZPOOL_CACHE" -N "$pool" 2> /dev/null "$ZPOOL" import -c "$ZPOOL_CACHE" -N ${ZPOOL_IMPORT_OPTS} \
"$pool" 2> /dev/null
r="$?" ; RET=$((RET + r)) r="$?" ; RET=$((RET + r))
if [ "$r" -eq 0 ] if [ "$r" -eq 0 ]
then then
@ -295,26 +296,24 @@ do_status()
do_start() do_start()
{ {
if [ "$VERBOSE_MOUNT" = 'yes' ] if check_boolean "$VERBOSE_MOUNT"
then then
zfs_log_begin_msg "Checking if ZFS userspace tools present" zfs_log_begin_msg "Checking if ZFS userspace tools present"
fi fi
if checksystem if checksystem
then then
[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
if [ "$VERBOSE_MOUNT" = 'yes' ] check_boolean "$VERBOSE_MOUNT" && \
then zfs_log_begin_msg "Loading kernel ZFS infrastructure"
zfs_log_begin_msg "Loading kernel ZFS infrastructure"
fi
if ! load_module "zfs" if ! load_module "zfs"
then then
[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 1 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 1
return 5 return 5
fi fi
[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
do_import && udev_trigger # just to make sure we get zvols. do_import && udev_trigger # just to make sure we get zvols.

View File

@ -67,8 +67,8 @@ do_mount()
{ {
local verbose overlay i mntpt val local verbose overlay i mntpt val
[ "$VERBOSE_MOUNT" = 'yes' ] && verbose=v check_boolean "$VERBOSE_MOUNT" && verbose=v
[ "$OVERLAY_MOUNTS" = 'yes' ] && overlay=O check_boolean "$DO_OVERLAY_MOUNTS" && overlay=O
zfs_action "Mounting ZFS filesystem(s)" \ zfs_action "Mounting ZFS filesystem(s)" \
"$ZFS" mount -a$verbose$overlay "$MOUNT_EXTRA_OPTIONS" "$ZFS" mount -a$verbose$overlay "$MOUNT_EXTRA_OPTIONS"
@ -78,7 +78,7 @@ do_mount()
# can get zfs-import to run sufficiently early on in the boot # can get zfs-import to run sufficiently early on in the boot
# process - before local mounts. This is just here in case/if # process - before local mounts. This is just here in case/if
# this isn't possible. # this isn't possible.
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab" zfs_log_begin_msg "Mounting volumes and filesystems registered in fstab"
read_mtab "^/dev/(zd|zvol)" read_mtab "^/dev/(zd|zvol)"
@ -90,7 +90,7 @@ do_mount()
dev=$(eval echo "$"FSTAB_dev_$i) dev=$(eval echo "$"FSTAB_dev_$i)
if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ] if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" && [ -e "$dev" ]
then then
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_progress_msg "$mntpt " zfs_log_progress_msg "$mntpt "
fsck "$dev" && mount "$mntpt" fsck "$dev" && mount "$mntpt"
fi fi
@ -107,7 +107,7 @@ do_mount()
mntpt=$(eval echo "$""$var") mntpt=$(eval echo "$""$var")
if ! in_mtab "$mntpt" && ! is_mounted "$mntpt" if ! in_mtab "$mntpt" && ! is_mounted "$mntpt"
then then
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_progress_msg "$mntpt " zfs_log_progress_msg "$mntpt "
mount "$mntpt" mount "$mntpt"
fi fi
@ -115,7 +115,7 @@ do_mount()
i=$((i + 1)) i=$((i + 1))
var=$(eval echo FSTAB_$i) var=$(eval echo FSTAB_$i)
done done
[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
return 0 return 0
} }
@ -131,7 +131,7 @@ do_unmount()
# this isn't possible. # this isn't possible.
zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a zfs_action "Unmounting ZFS filesystems" "$ZFS" unmount -a
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab" zfs_log_begin_msg "Unmounting volumes and filesystems registered in fstab"
read_mtab "^/dev/(zd|zvol)" read_mtab "^/dev/(zd|zvol)"
@ -143,7 +143,7 @@ do_unmount()
dev=$(eval echo "$"FSTAB_dev_$i) dev=$(eval echo "$"FSTAB_dev_$i)
if in_mtab "$mntpt" if in_mtab "$mntpt"
then then
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_progress_msg "$mntpt " zfs_log_progress_msg "$mntpt "
umount "$mntpt" umount "$mntpt"
fi fi
@ -159,7 +159,7 @@ do_unmount()
do do
mntpt=$(eval echo "$""$var") mntpt=$(eval echo "$""$var")
if in_mtab "$mntpt"; then if in_mtab "$mntpt"; then
[ "$VERBOSE_MOUNT" = 'yes' ] && \ check_boolean "$VERBOSE_MOUNT" && \
zfs_log_progress_msg "$mntpt " zfs_log_progress_msg "$mntpt "
umount "$mntpt" umount "$mntpt"
fi fi
@ -167,20 +167,16 @@ do_unmount()
i=$((i + 1)) i=$((i + 1))
var=$(eval echo FSTAB_$i) var=$(eval echo FSTAB_$i)
done done
[ "$VERBOSE_MOUNT" = 'yes' ] && zfs_log_end_msg 0 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
return 0 return 0
} }
do_start() do_start()
{ {
check_module_loaded "zfs" || exit 0 check_boolean "$ZFS_MOUNT" || exit 0
case "$ZFS_MOUNT" in check_module_loaded "zfs" || exit 0
[Oo][Ff][Ff]|[Nn][Oo]|''|0)
exit 3
;;
esac
# Ensure / exists in /etc/mtab, if not update mtab accordingly. # Ensure / exists in /etc/mtab, if not update mtab accordingly.
# This should be handled by rc.sysinit but lets be paranoid. # This should be handled by rc.sysinit but lets be paranoid.
@ -194,11 +190,7 @@ do_start()
do_stop() do_stop()
{ {
case "$ZFS_UNMOUNT" in check_boolean "$ZFS_UNMOUNT" || exit 0
[Oo][Ff][Ff]|[Nn][Oo]|''|0)
exit 0
;;
esac
check_module_loaded "zfs" || exit 0 check_module_loaded "zfs" || exit 0

View File

@ -40,11 +40,7 @@ do_depend()
do_start() do_start()
{ {
case "$ZFS_SHARE" in check_boolean "$ZFS_SHARE" || exit 0
[Oo][Ff][Ff]|[Nn][Oo]|''|0)
exit 0
;;
esac
check_module_loaded "zfs" || exit 0 check_module_loaded "zfs" || exit 0
@ -53,11 +49,7 @@ do_start()
do_stop() do_stop()
{ {
case "$ZFS_UNSHARE" in check_boolean "$ZFS_UNSHARE" || exit 0
[Oo][Ff][Ff]|[Nn][Oo]|''|0)
exit 0
;;
esac
check_module_loaded "zfs" || exit 0 check_module_loaded "zfs" || exit 0

View File

@ -1,5 +1,8 @@
# ZoL userland configuration. # ZoL userland configuration.
# To enable a boolean setting, set it to yes, on, true, or 1.
# Anything else will be interpreted as unset.
# Run `zfs mount -a` during system start? # Run `zfs mount -a` during system start?
ZFS_MOUNT='yes' ZFS_MOUNT='yes'
@ -29,6 +32,27 @@ VERBOSE_MOUNT='no'
# is not allowed). # is not allowed).
DO_OVERLAY_MOUNTS='no' DO_OVERLAY_MOUNTS='no'
# Any additional option to the 'zfs import' commandline?
# Include '-o' for each option wanted.
# You don't need to put '-f' in here, unless you want it ALL the time.
# Using the option 'zfsforce=1' on the grub/kernel command line will
# do the same, but on a case-to-case basis.
ZPOOL_IMPORT_OPTS=""
# Full path to the ZFS cache file?
# See "cachefile" in zpool(8).
# The default is "@sysconfdir@/zfs/zpool.cache".
#ZPOOL_CACHE="@sysconfdir@/zfs/zpool.cache"
#
# Setting ZPOOL_CACHE to an empty string ('') AND setting ZPOOL_IMPORT_OPTS to
# "-c @sysconfdir@/zfs/zpool.cache" will _enforce_ the use of a cache file.
# This is needed in some cases (extreme amounts of VDEVs, multipath etc).
# Generally, the use of a cache file is usually not recommended on Linux
# because it sometimes is more trouble than it's worth (laptops with external
# devices or when/if device nodes changes names).
#ZPOOL_IMPORT_OPTS="-c @sysconfdir@/zfs/zpool.cache"
#ZPOOL_CACHE=""
# Any additional option to the 'zfs mount' command line? # Any additional option to the 'zfs mount' command line?
# Include '-o' for each option wanted. # Include '-o' for each option wanted.
MOUNT_EXTRA_OPTIONS="" MOUNT_EXTRA_OPTIONS=""
@ -77,6 +101,13 @@ ZFS_INITRD_POST_MODPROBE_SLEEP='0'
# This is a space separated list. # This is a space separated list.
#ZFS_POOL_EXCEPTIONS="test2" #ZFS_POOL_EXCEPTIONS="test2"
# List of pools to import?
# If this variable is set, there will be NO auto-import of ANY other
# pool. In essence, there will be no auto detection of availible pools.
# This is a semi-colon separated list.
# Makes the variable ZFS_POOL_EXCEPTIONS above redundant (won't be checked).
#ZFS_POOL_IMPORT="pool1;pool2"
# Optional arguments for the ZFS Event Daemon (ZED). # Optional arguments for the ZFS Event Daemon (ZED).
# See zed(8) for more information on available options. # See zed(8) for more information on available options.
#ZED_ARGS="-M" #ZED_ARGS="-M"