xref: /freebsd/sys/contrib/openzfs/cmd/zed/zed.d/statechange-slot_off.sh (revision 4f8f43b06ed07e96a250855488cc531799d5b78f)
1#!/bin/sh
2# shellcheck disable=SC3014,SC2154,SC2086,SC2034
3#
4# Turn off disk's enclosure slot if it becomes FAULTED.
5#
6# Bad SCSI disks can often "disappear and reappear" causing all sorts of chaos
7# as they flip between FAULTED and ONLINE.  If
8# ZED_POWER_OFF_ENCLOUSRE_SLOT_ON_FAULT is set in zed.rc, and the disk gets
9# FAULTED, then power down the slot via sysfs:
10#
11# /sys/class/enclosure/<enclosure>/<slot>/power_status
12#
13# We assume the user will be responsible for turning the slot back on again.
14#
15# Note that this script requires that your enclosure be supported by the
16# Linux SCSI Enclosure services (SES) driver.  The script will do nothing
17# if you have no enclosure, or if your enclosure isn't supported.
18#
19# Exit codes:
20#   0: slot successfully powered off
21#   1: enclosure not available
22#   2: ZED_POWER_OFF_ENCLOUSRE_SLOT_ON_FAULT disabled
23#   3: vdev was not FAULTED
24#   4: The enclosure sysfs path passed from ZFS does not exist
25#   5: Enclosure slot didn't actually turn off after we told it to
26
27[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
28. "${ZED_ZEDLET_DIR}/zed-functions.sh"
29
30if [ ! -d /sys/class/enclosure ] ; then
31	# No JBOD enclosure or NVMe slots
32	exit 1
33fi
34
35if [ "${ZED_POWER_OFF_ENCLOUSRE_SLOT_ON_FAULT}" != "1" ] ; then
36	exit 2
37fi
38
39if [ "$ZEVENT_VDEV_STATE_STR" != "FAULTED" ] ; then
40	exit 3
41fi
42
43if [ ! -f "$ZEVENT_VDEV_ENC_SYSFS_PATH/power_status" ] ; then
44	exit 4
45fi
46
47# Turn off the slot and wait for sysfs to report that the slot is off.
48# It can take ~400ms on some enclosures and multiple retries may be needed.
49for i in $(seq 1 20) ; do
50	echo "off" | tee "$ZEVENT_VDEV_ENC_SYSFS_PATH/power_status"
51
52	for j in $(seq 1 5) ; do
53		if [ "$(cat $ZEVENT_VDEV_ENC_SYSFS_PATH/power_status)" == "off" ] ; then
54			break 2
55		fi
56		sleep 0.1
57	done
58done
59
60if [ "$(cat $ZEVENT_VDEV_ENC_SYSFS_PATH/power_status)" != "off" ] ; then
61	exit 5
62fi
63
64zed_log_msg "powered down slot $ZEVENT_VDEV_ENC_SYSFS_PATH for $ZEVENT_VDEV_PATH"
65