2017-03-09 18:20:15 +00:00
|
|
|
#!/bin/sh
|
2016-10-19 19:55:59 +00:00
|
|
|
#
|
2021-04-23 20:27:50 +00:00
|
|
|
# Turn off/on vdevs' enclosure fault LEDs when their pool's state changes.
|
2016-10-19 19:55:59 +00:00
|
|
|
#
|
2021-04-23 20:27:50 +00:00
|
|
|
# Turn a vdev's fault LED on if it becomes FAULTED, DEGRADED or UNAVAIL.
|
|
|
|
# Turn its LED off when it's back ONLINE again.
|
2016-10-24 17:45:59 +00:00
|
|
|
#
|
2017-02-11 00:09:45 +00:00
|
|
|
# This script run in two basic modes:
|
|
|
|
#
|
|
|
|
# 1. If $ZEVENT_VDEV_ENC_SYSFS_PATH and $ZEVENT_VDEV_STATE_STR are set, then
|
2021-04-23 20:27:50 +00:00
|
|
|
# only set the LED for that particular vdev. This is the case for statechange
|
2017-02-11 00:09:45 +00:00
|
|
|
# events and some vdev_* events.
|
|
|
|
#
|
2021-04-23 20:27:50 +00:00
|
|
|
# 2. If those vars are not set, then check the state of all vdevs in the pool
|
2017-02-11 00:09:45 +00:00
|
|
|
# and set the LEDs accordingly. This is the case for pool_import events.
|
|
|
|
#
|
|
|
|
# Note that this script requires that your enclosure be supported by the
|
2021-04-23 20:27:50 +00:00
|
|
|
# Linux SCSI Enclosure services (SES) driver. The script will do nothing
|
2016-10-19 19:55:59 +00:00
|
|
|
# if you have no enclosure, or if your enclosure isn't supported.
|
|
|
|
#
|
|
|
|
# Exit codes:
|
|
|
|
# 0: enclosure led successfully set
|
2019-08-30 16:43:30 +00:00
|
|
|
# 1: enclosure leds not available
|
2016-10-19 19:55:59 +00:00
|
|
|
# 2: enclosure leds administratively disabled
|
2017-02-11 00:09:45 +00:00
|
|
|
# 3: The led sysfs path passed from ZFS does not exist
|
|
|
|
# 4: $ZPOOL not set
|
2017-03-09 18:20:15 +00:00
|
|
|
# 5: awk is not installed
|
2016-10-19 19:55:59 +00:00
|
|
|
|
|
|
|
[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
|
|
|
|
. "${ZED_ZEDLET_DIR}/zed-functions.sh"
|
|
|
|
|
2016-10-24 17:45:59 +00:00
|
|
|
if [ ! -d /sys/class/enclosure ] ; then
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-10-19 19:55:59 +00:00
|
|
|
|
|
|
|
if [ "${ZED_USE_ENCLOSURE_LEDS}" != "1" ] ; then
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2017-02-11 00:09:45 +00:00
|
|
|
zed_check_cmd "$ZPOOL" || exit 4
|
2017-03-09 18:20:15 +00:00
|
|
|
zed_check_cmd awk || exit 5
|
2017-02-11 00:09:45 +00:00
|
|
|
|
|
|
|
# Global used in set_led debug print
|
|
|
|
vdev=""
|
|
|
|
|
|
|
|
# check_and_set_led (file, val)
|
|
|
|
#
|
|
|
|
# Read an enclosure sysfs file, and write it if it's not already set to 'val'
|
|
|
|
#
|
|
|
|
# Arguments
|
|
|
|
# file: sysfs file to set (like /sys/class/enclosure/0:0:1:0/SLOT 10/fault)
|
|
|
|
# val: value to set it to
|
|
|
|
#
|
|
|
|
# Return
|
|
|
|
# 0 on success, 3 on missing sysfs path
|
|
|
|
#
|
2017-03-09 18:20:15 +00:00
|
|
|
check_and_set_led()
|
2016-10-19 19:55:59 +00:00
|
|
|
{
|
2017-02-11 00:09:45 +00:00
|
|
|
file="$1"
|
|
|
|
val="$2"
|
2017-02-16 21:41:48 +00:00
|
|
|
|
2021-04-23 20:27:50 +00:00
|
|
|
if [ -z "$val" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2017-02-16 21:41:48 +00:00
|
|
|
if [ ! -e "$file" ] ; then
|
2017-02-11 00:09:45 +00:00
|
|
|
return 3
|
|
|
|
fi
|
2016-10-19 19:55:59 +00:00
|
|
|
|
2017-02-16 21:41:48 +00:00
|
|
|
# If another process is accessing the LED when we attempt to update it,
|
|
|
|
# the update will be lost so retry until the LED actually changes or we
|
|
|
|
# timeout.
|
2021-04-23 20:27:50 +00:00
|
|
|
for _ in 1 2 3 4 5; do
|
2017-02-16 21:41:48 +00:00
|
|
|
# We want to check the current state first, since writing to the
|
2019-08-30 16:43:30 +00:00
|
|
|
# 'fault' entry always causes a SES command, even if the
|
2017-02-16 21:41:48 +00:00
|
|
|
# current state is already what you want.
|
2021-04-23 20:27:50 +00:00
|
|
|
read -r current < "${file}"
|
2016-10-19 19:55:59 +00:00
|
|
|
|
2017-02-16 21:41:48 +00:00
|
|
|
# On some enclosures if you write 1 to fault, and read it back,
|
|
|
|
# it will return 2. Treat all non-zero values as 1 for
|
|
|
|
# simplicity.
|
|
|
|
if [ "$current" != "0" ] ; then
|
|
|
|
current=1
|
|
|
|
fi
|
2016-10-19 19:55:59 +00:00
|
|
|
|
2017-02-16 21:41:48 +00:00
|
|
|
if [ "$current" != "$val" ] ; then
|
|
|
|
echo "$val" > "$file"
|
|
|
|
zed_log_msg "vdev $vdev set '$file' LED to $val"
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
2021-04-23 20:27:50 +00:00
|
|
|
done
|
2016-10-19 19:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:20:15 +00:00
|
|
|
state_to_val()
|
|
|
|
{
|
2017-02-11 00:09:45 +00:00
|
|
|
state="$1"
|
2021-04-23 20:27:50 +00:00
|
|
|
case "$state" in
|
|
|
|
FAULTED|DEGRADED|UNAVAIL)
|
|
|
|
echo 1
|
|
|
|
;;
|
|
|
|
ONLINE)
|
|
|
|
echo 0
|
|
|
|
;;
|
|
|
|
esac
|
2016-10-19 19:55:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 20:27:50 +00:00
|
|
|
# process_pool (pool)
|
2017-02-11 00:09:45 +00:00
|
|
|
#
|
2021-04-23 20:27:50 +00:00
|
|
|
# Iterate through a pool and set the vdevs' enclosure slot LEDs to
|
|
|
|
# those vdevs' state.
|
2017-02-11 00:09:45 +00:00
|
|
|
#
|
|
|
|
# Arguments
|
2021-04-23 20:27:50 +00:00
|
|
|
# pool: Pool name.
|
2017-02-11 00:09:45 +00:00
|
|
|
#
|
|
|
|
# Return
|
|
|
|
# 0 on success, 3 on missing sysfs path
|
|
|
|
#
|
2017-03-09 18:20:15 +00:00
|
|
|
process_pool()
|
2017-02-11 00:09:45 +00:00
|
|
|
{
|
|
|
|
pool="$1"
|
|
|
|
|
2021-04-23 20:27:50 +00:00
|
|
|
# The output will be the vdevs only (from "grep '/dev/'"):
|
|
|
|
#
|
|
|
|
# U45 ONLINE 0 0 0 /dev/sdk 0
|
|
|
|
# U46 ONLINE 0 0 0 /dev/sdm 0
|
|
|
|
# U47 ONLINE 0 0 0 /dev/sdn 0
|
|
|
|
# U50 ONLINE 0 0 0 /dev/sdbn 0
|
|
|
|
#
|
|
|
|
ZPOOL_SCRIPTS_AS_ROOT=1 $ZPOOL status -c upath,fault_led "$pool" | grep '/dev/' | (
|
|
|
|
rc=0
|
|
|
|
while read -r vdev state _ _ _ therest; do
|
2017-02-11 00:09:45 +00:00
|
|
|
# Read out current LED value and path
|
2021-04-23 20:27:50 +00:00
|
|
|
# Get dev name (like 'sda')
|
|
|
|
dev=$(basename "$(echo "$therest" | awk '{print $(NF-1)}')")
|
|
|
|
vdev_enc_sysfs_path=$(realpath "/sys/class/block/$dev/device/enclosure_device"*)
|
|
|
|
current_val=$(echo "$therest" | awk '{print $NF}')
|
2017-03-09 18:20:15 +00:00
|
|
|
|
|
|
|
if [ "$current_val" != "0" ] ; then
|
|
|
|
current_val=1
|
|
|
|
fi
|
2017-02-11 00:09:45 +00:00
|
|
|
|
|
|
|
if [ -z "$vdev_enc_sysfs_path" ] ; then
|
|
|
|
# Skip anything with no sysfs LED entries
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2017-02-16 21:41:48 +00:00
|
|
|
if [ ! -e "$vdev_enc_sysfs_path/fault" ] ; then
|
2021-04-23 20:27:50 +00:00
|
|
|
rc=3
|
2017-02-16 21:41:48 +00:00
|
|
|
zed_log_msg "vdev $vdev '$file/fault' doesn't exist"
|
2021-04-23 20:27:50 +00:00
|
|
|
continue
|
2017-02-11 00:09:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
val=$(state_to_val "$state")
|
|
|
|
|
2017-03-09 18:20:15 +00:00
|
|
|
if [ "$current_val" = "$val" ] ; then
|
2017-02-11 00:09:45 +00:00
|
|
|
# LED is already set correctly
|
2021-04-23 20:27:50 +00:00
|
|
|
continue
|
2017-02-11 00:09:45 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-09 18:20:15 +00:00
|
|
|
if ! check_and_set_led "$vdev_enc_sysfs_path/fault" "$val"; then
|
2021-04-23 20:27:50 +00:00
|
|
|
rc=3
|
2017-03-09 18:20:15 +00:00
|
|
|
fi
|
|
|
|
done
|
2021-04-23 20:27:50 +00:00
|
|
|
exit "$rc"; )
|
2017-02-11 00:09:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 17:07:19 +00:00
|
|
|
if [ -n "$ZEVENT_VDEV_ENC_SYSFS_PATH" ] && [ -n "$ZEVENT_VDEV_STATE_STR" ] ; then
|
2021-04-23 20:27:50 +00:00
|
|
|
# Got a statechange for an individual vdev
|
2017-02-11 00:09:45 +00:00
|
|
|
val=$(state_to_val "$ZEVENT_VDEV_STATE_STR")
|
2017-03-09 18:20:15 +00:00
|
|
|
vdev=$(basename "$ZEVENT_VDEV_PATH")
|
2017-02-11 00:09:45 +00:00
|
|
|
check_and_set_led "$ZEVENT_VDEV_ENC_SYSFS_PATH/fault" "$val"
|
|
|
|
else
|
|
|
|
# Process the entire pool
|
2017-03-09 18:20:15 +00:00
|
|
|
poolname=$(zed_guid_to_pool "$ZEVENT_POOL_GUID")
|
|
|
|
process_pool "$poolname"
|
2017-02-11 00:09:45 +00:00
|
|
|
fi
|