#!/bin/sh
#
# zfs-mount     This script will mount/umount the zfs filesystems.
#
# chkconfig:    2345 03 97
# description:  This script will mount/umount the zfs filesystems during
#               system boot/shutdown. Configuration of which filesystems
#               should be mounted is handled by the zfs 'mountpoint' and
#               'canmount' properties. See the zfs(8) man page for details.
#               It is also responsible for all userspace zfs services.
# probe: true
#
### BEGIN INIT INFO
# Provides:          zfs-mount
# Required-Start:    $local_fs zfs-import
# Required-Stop:     $local_fs zfs-import
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Stop-After:      zfs-zed
# Short-Description: Mount ZFS filesystems and volumes
# Description: Run the `zfs mount -a` or `zfs umount -a` commands.
### END INIT INFO
#
# Source the common init script
. /etc/zfs/zfs-functions

# Mount all datasets/filesystems
do_mount()
{
log_info_msg "Mounting ZFS filesystem(s)" 
"$ZFS" mount -a
evaluate_retval

for pool in `zpool list -H -o name | cut -f1`
  do
  log_info_msg "Set sync to disabled on "$pool
  zfs set sync=disabled $pool
  evaluate_retval
  done
}

# Unmount all filesystems
do_unmount()
{
#to make sure to pause scrub if there is one
for pool in `zpool list -H | cut -f1`
  do
  zpool scrub -p $pool 2> /dev/null
  done

log_info_msg "Unmounting ZFS filesystems"
"$ZFS" unmount -a
evaluate_retval
}

do_start()
{
check_module_loaded "zfs" || exit 0
do_mount
}

do_stop()
{
check_module_loaded "zfs" || exit 0
do_unmount
}

# ----------------------------------------------------

case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  force-reload|condrestart|reload|restart|status)
    # no-op
    ;;
  *)
    [ -n "$1" ] && echo "Error: Unknown command $1."
    echo "Usage: $0 {start|stop}"
    exit 3
    ;;
  esac

exit $?
