xref: /freebsd/sys/contrib/openzfs/etc/init.d/zfs-zed.in (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1#!@DEFAULT_INIT_SHELL@
2# shellcheck disable=SC2154
3#
4# zfs-zed
5#
6# chkconfig:    2345 29 99
7# description:  This script will start and stop the ZFS Event Daemon.
8# probe: true
9#
10### BEGIN INIT INFO
11# Provides:          zfs-zed
12# Required-Start:    zfs-mount
13# Required-Stop:     zfs-mount
14# Default-Start:     2 3 4 5
15# Default-Stop:      0 1 6
16# X-Stop-After:      zfs-share
17# Short-Description: ZFS Event Daemon
18# Description:       zed monitors ZFS events. When a zevent is posted, zed
19#                    will run any scripts that have been enabled for the
20#                    corresponding zevent class.
21### END INIT INFO
22#
23# Released under the 2-clause BSD license.
24#
25# This script is based on debian/zfsutils.zfs.init from the
26# Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
27
28# Source the common init script
29. @sysconfdir@/zfs/zfs-functions
30
31ZED_NAME="zed"
32ZED_PIDFILE="@runstatedir@/$ZED_NAME.pid"
33
34# shellcheck disable=SC2034
35extra_started_commands="reload"
36
37# Exit if the package is not installed
38[ -x "$ZED" ] || exit 0
39
40# ----------------------------------------------------
41
42do_depend()
43{
44	after zfs-mount localmount
45}
46
47do_start()
48{
49	check_module_loaded "zfs" || exit 0
50
51	ZED_ARGS="$ZED_ARGS -p $ZED_PIDFILE"
52
53	zfs_action "Starting ZFS Event Daemon" zfs_daemon_start \
54	    "$ZED_PIDFILE" "$ZED" "$ZED_ARGS"
55	return "$?"
56}
57
58do_stop()
59{
60	local pools
61	check_module_loaded "zfs" || exit 0
62
63	zfs_action "Stopping ZFS Event Daemon" zfs_daemon_stop \
64	   "$ZED_PIDFILE" "$ZED" "$ZED_NAME" || return "$?"
65
66	# Let's see if we have any pools imported
67	pools=$("$ZPOOL" list -H -oname)
68	if [ -z "$pools" ]
69	then
70		# No pools imported, it is/should be safe/possible to
71		# unload modules.
72		zfs_action "Unloading modules" rmmod zfs spl
73		return "$?"
74	fi
75}
76
77do_status()
78{
79	check_module_loaded "zfs" || exit 0
80
81	zfs_daemon_status "$ZED_PIDFILE" "$ZED" "$ZED_NAME"
82	return "$?"
83}
84
85do_reload()
86{
87	check_module_loaded "zfs" || exit 0
88
89	zfs_action "Reloading ZFS Event Daemon" zfs_daemon_reload \
90	    "$ZED_PIDFILE" "$ZED_NAME"
91	return "$?"
92}
93
94# ----------------------------------------------------
95
96if [ ! -e /sbin/openrc-run ]; then
97	case "$1" in
98		start)
99			do_start
100			;;
101		stop)
102			do_stop
103			;;
104		status)
105			do_status
106			;;
107		reload|force-reload)
108			do_reload
109			;;
110		restart)
111			do_stop
112			do_start
113			;;
114		*)
115			[ -n "$1" ] && echo "Error: Unknown command $1."
116			echo "Usage: $0 {start|stop|status|reload|restart}"
117			exit 1
118			;;
119	esac
120
121	exit $?
122else
123	# Create wrapper functions since Gentoo don't use the case part.
124	depend() { do_depend; }
125	start() { do_start; }
126	stop() { do_stop; }
127	status() { do_status; }
128	reload() { do_reload; }
129fi
130