Path: blob/main/sys/contrib/openzfs/cmd/zed/zed.d/statechange-sync-slot_off.sh
48529 views
#!/bin/sh1# shellcheck disable=SC3014,SC2154,SC2086,SC20342#3# Turn off disk's enclosure slot if it becomes FAULTED.4#5# Bad SCSI disks can often "disappear and reappear" causing all sorts of chaos6# as they flip between FAULTED and ONLINE. If7# ZED_POWER_OFF_ENCLOSURE_SLOT_ON_FAULT is set in zed.rc, and the disk gets8# FAULTED, then power down the slot via sysfs:9#10# /sys/class/enclosure/<enclosure>/<slot>/power_status11#12# We assume the user will be responsible for turning the slot back on again.13#14# Note that this script requires that your enclosure be supported by the15# Linux SCSI Enclosure services (SES) driver. The script will do nothing16# if you have no enclosure, or if your enclosure isn't supported.17#18# Exit codes:19# 0: slot successfully powered off20# 1: enclosure not available21# 2: ZED_POWER_OFF_ENCLOSURE_SLOT_ON_FAULT disabled22# 3: vdev was not FAULTED23# 4: The enclosure sysfs path passed from ZFS does not exist24# 5: Enclosure slot didn't actually turn off after we told it to2526[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"27. "${ZED_ZEDLET_DIR}/zed-functions.sh"2829if [ ! -d /sys/class/enclosure ] ; then30# No JBOD enclosure or NVMe slots31exit 132fi3334if [ "${ZED_POWER_OFF_ENCLOSURE_SLOT_ON_FAULT}" != "1" ] ; then35exit 236fi3738if [ "$ZEVENT_VDEV_STATE_STR" != "FAULTED" ] ; then39exit 340fi4142if [ ! -f "$ZEVENT_VDEV_ENC_SYSFS_PATH/power_status" ] ; then43exit 444fi4546# Turn off the slot and wait for sysfs to report that the slot is off.47# It can take ~400ms on some enclosures and multiple retries may be needed.48for i in $(seq 1 20) ; do49echo "off" | tee "$ZEVENT_VDEV_ENC_SYSFS_PATH/power_status"5051for j in $(seq 1 5) ; do52if [ "$(cat $ZEVENT_VDEV_ENC_SYSFS_PATH/power_status)" == "off" ] ; then53break 254fi55sleep 0.156done57done5859if [ "$(cat $ZEVENT_VDEV_ENC_SYSFS_PATH/power_status)" != "off" ] ; then60exit 561fi6263zed_log_msg "powered down slot $ZEVENT_VDEV_ENC_SYSFS_PATH for $ZEVENT_VDEV_PATH"646566