aboutsummaryrefslogtreecommitdiffstats
path: root/etc/init.d/zfs-zed.in
blob: 9fa243f8f29c19315d6e5f175eda4ede31b9ec38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!@SHELL@
#
# zfs-zed
#
# chkconfig:    2345 01 99
# description:  This script will start and stop the ZFS Event Daemon.
# probe: true
#
### BEGIN INIT INFO
# Provides:          zfs-zed
# Required-Start:    mtab
# Required-Stop:     $local_fs mtab
# Default-Start:     S
# Default-Stop:      0 1 6
# X-Start-Before:    checkfs
# X-Stop-After:      zfs-import
# Short-Description: ZFS Event Daemon
# Description:       zed monitors ZFS events. When a zevent is posted, zed
#                    will run any scripts that have been enabled for the
#                    corresponding zevent class.
### END INIT INFO
#
# NOTE: Not having '$local_fs' on Required-Start but only on Required-Stop
#       is on purpose. If we have '$local_fs' in both (and X-Start-Before=checkfs)
#       we get conflicts - zed and import needs to be started extremely early,
#       but not stopped too late.
#
# Released under the 2-clause BSD license.
#
# The original script that acted as a template for this script came from
# the Debian GNU/Linux kFreeBSD ZFS packages (which did not include a
# licensing stansa) in the commit dated Mar 24, 2011:
#   https://github.com/zfsonlinux/pkg-zfs/commit/80a3ae582b59c0250d7912ba794dca9e669e605a

# Source the common init script
. @sysconfdir@/zfs/zfs-functions

ZED_NAME="zed"
ZED_PIDFILE="@runstatedir@/$ZED_NAME.pid"

# Exit if the package is not installed
[ -x "$ZED" ] || exit 0

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

do_depend()
{
	# Try to allow people to mix and match fstab with ZFS in a way that makes sense.
	if [ "$(mountinfo -s /)" = 'zfs' ]
	then
		before localmount
	else
		after localmount
	fi

	# bootmisc will log to /var which may be a different zfs than root.
	before bootmisc logger zfs-import
	after sysfs
}

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

	ZED_ARGS="$ZED_ARGS -p $ZED_PIDFILE"

	zfs_action "Starting ZFS Event Daemon" zfs_daemon_start \
	    "$ZED_PIDFILE" "$ZED" "$ZED_ARGS"
	return "$?"
}

do_stop()
{
	local pools RET
	check_module_loaded "zfs" || exit 0

	zfs_action "Stopping ZFS Event Daemon" zfs_daemon_stop \
	   "$ZED_PIDFILE" "$ZED" "$ZED_NAME"
	if [ "$?" -eq "0" ]
	then
		# Let's see if we have any pools imported
		pools=$("$ZPOOL" list -H -oname)
		if [ -z "$pools" ]
		then
			# No pools imported, it is/should be safe/possible to
			# unload modules.
			zfs_action "Unloading modules" rmmod zfs zunicode \
			    zavl zcommon znvpair spl
			return "$?"
		fi
	else
		return "$?"
	fi
}

do_status()
{
	check_module_loaded "zfs" || exit 0

	zfs_daemon_status "$ZED_PIDFILE" "$ZED" "$ZED_NAME"
	return "$?"
}

do_reload()
{
	check_module_loaded "zfs" || exit 0

	zfs_action "Reloading ZFS Event Daemon" zfs_daemon_reload \
	    "$ZED_PIDFILE" "$ZED_NAME"
	return "$?"
}

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

if [ ! -e /etc/gentoo-release ]; then
	case "$1" in
		start)
			do_start
			;;
		stop)
			do_stop
			;;
		status)
			do_status
			;;
		reload|force-reload)
			do_reload
			;;
		restart)
			do_stop
			do_start
			;;
		*)
			[ -n "$1" ] && echo "Error: Unknown command $1."
			echo "Usage: $0 {start|stop|status|reload|restart}"
			exit 1
			;;
	esac

	exit $?
else
	# Create wrapper functions since Gentoo don't use the case part.
	depend() { do_depend; }
	start() { do_start; }
	stop() { do_stop; }
	status() { do_status; }
	reload() { do_reload; }
fi