#!/bin/sh
#
# zfs-import    This script will import ZFS pools
#
# chkconfig:    2345 02 98
# description:  This script will perform a verbatim import of ZFS pools
#               during system boot.
# probe: true
#
### BEGIN INIT INFO
# Provides:          zfs-import
# Required-Start:    mtab
# Required-Stop:     $local_fs mtab
# Default-Start:     S
# Default-Stop:      0 1 6
# X-Start-Before:    checkfs
# X-Stop-After:      zfs-mount
# Short-Description: Import ZFS pools
# Description: Run the `zpool import` command.

# Source the common init script
. /etc/zfs/zfs-functions

# ----------------------------------------------------
do_import()
{
log_info_msg "Importing ZFS pool(s)"
"$ZPOOL" import -c "$ZPOOL_CACHE" -N -a 2> /dev/null
evaluate_retval
}

# Output the status and list of pools
do_status()
{
check_module_loaded "zfs" || exit 0
"$ZPOOL" status && echo "" && "$ZPOOL" list
}

do_start()
{
depmod -a
log_info_msg "Loading kernel ZFS infrastructure"
load_module "zfs"
RETVAL=$?
evaluate_retval
if [ $RETVAL -eq 0 -a -n "$ZFS_DELAY" ] ; then
  log_info_msg "Waiting $ZFS_DELAY seconds to have zfs module activated"
  sleep $ZFS_DELAY
  evaluate_retval
  fi
if [ $RETVAL -eq 0 ] ; then
  do_import		# Importing the already known pool
  RETVAL=$?
  fi
return $RETVAL
}

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