]> git.proxmox.com Git - mirror_zfs.git/blob - cmd/zed/zed.d/statechange-slot_off.sh
checkstyle: fix action failures
[mirror_zfs.git] / cmd / zed / zed.d / statechange-slot_off.sh
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
30 if [ ! -d /sys/class/enclosure ] ; then
31 # No JBOD enclosure or NVMe slots
32 exit 1
33 fi
34
35 if [ "${ZED_POWER_OFF_ENCLOUSRE_SLOT_ON_FAULT}" != "1" ] ; then
36 exit 2
37 fi
38
39 if [ "$ZEVENT_VDEV_STATE_STR" != "FAULTED" ] ; then
40 exit 3
41 fi
42
43 if [ ! -f "$ZEVENT_VDEV_ENC_SYSFS_PATH/power_status" ] ; then
44 exit 4
45 fi
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.
49 for 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
58 done
59
60 if [ "$(cat $ZEVENT_VDEV_ENC_SYSFS_PATH/power_status)" != "off" ] ; then
61 exit 5
62 fi
63
64 zed_log_msg "powered down slot $ZEVENT_VDEV_ENC_SYSFS_PATH for $ZEVENT_VDEV_PATH"