Files
mirror_zfs/cmd/zed/zed.d/statechange-led.sh
T

178 lines
4.5 KiB
Bash
Raw Normal View History

2017-03-09 10:20:15 -08:00
#!/bin/sh
#
# Turn off/on the VDEV's enclosure fault LEDs when the pool's state changes.
#
2017-02-10 16:09:45 -08:00
# Turn the VDEV's fault LED on if it becomes FAULTED, DEGRADED or UNAVAIL.
# Turn the LED off when it's back ONLINE again.
#
2017-02-10 16:09:45 -08:00
# This script run in two basic modes:
#
# 1. If $ZEVENT_VDEV_ENC_SYSFS_PATH and $ZEVENT_VDEV_STATE_STR are set, then
# only set the LED for that particular VDEV. This is the case for statechange
# events and some vdev_* events.
#
2017-02-16 15:41:48 -06:00
# 2. If those vars are not set, then check the state of all VDEVs in the pool
2017-02-10 16:09:45 -08: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
# Linux SCSI enclosure services (ses) driver. The script will do nothing
# if you have no enclosure, or if your enclosure isn't supported.
#
# Exit codes:
# 0: enclosure led successfully set
2019-08-30 18:43:30 +02:00
# 1: enclosure leds not available
# 2: enclosure leds administratively disabled
2017-02-10 16:09:45 -08:00
# 3: The led sysfs path passed from ZFS does not exist
# 4: $ZPOOL not set
2017-03-09 10:20:15 -08:00
# 5: awk is not installed
[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
. "${ZED_ZEDLET_DIR}/zed-functions.sh"
if [ ! -d /sys/class/enclosure ] ; then
exit 1
fi
if [ "${ZED_USE_ENCLOSURE_LEDS}" != "1" ] ; then
exit 2
fi
2017-02-10 16:09:45 -08:00
zed_check_cmd "$ZPOOL" || exit 4
2017-03-09 10:20:15 -08:00
zed_check_cmd awk || exit 5
2017-02-10 16:09:45 -08: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 10:20:15 -08:00
check_and_set_led()
{
2017-02-10 16:09:45 -08:00
file="$1"
val="$2"
2017-02-16 15:41:48 -06:00
if [ ! -e "$file" ] ; then
2017-02-10 16:09:45 -08:00
return 3
fi
2017-02-16 15:41:48 -06: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.
for _ in $(seq 1 5); do
# We want to check the current state first, since writing to the
2019-08-30 18:43:30 +02:00
# 'fault' entry always causes a SES command, even if the
2017-02-16 15:41:48 -06:00
# current state is already what you want.
current=$(cat "${file}")
2017-02-16 15:41:48 -06: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
2017-02-16 15:41:48 -06:00
if [ "$current" != "$val" ] ; then
echo "$val" > "$file"
zed_log_msg "vdev $vdev set '$file' LED to $val"
else
break
fi
done
}
2017-03-09 10:20:15 -08:00
state_to_val()
{
2017-02-10 16:09:45 -08:00
state="$1"
2017-03-09 10:20:15 -08:00
if [ "$state" = "FAULTED" ] || [ "$state" = "DEGRADED" ] || \
[ "$state" = "UNAVAIL" ] ; then
2017-02-10 16:09:45 -08:00
echo 1
2017-03-09 10:20:15 -08:00
elif [ "$state" = "ONLINE" ] ; then
2017-02-10 16:09:45 -08:00
echo 0
fi
}
2017-02-10 16:09:45 -08:00
# process_pool ([pool])
#
# Iterate through a pool (or pools) and set the VDEV's enclosure slot LEDs to
# the VDEV's state.
#
# Arguments
# pool: Optional pool name. If not specified, iterate though all pools.
#
# Return
# 0 on success, 3 on missing sysfs path
#
2017-03-09 10:20:15 -08:00
process_pool()
2017-02-10 16:09:45 -08:00
{
pool="$1"
rc=0
# Lookup all the current LED values and paths in parallel
2017-03-09 10:20:15 -08:00
#shellcheck disable=SC2016
2017-02-10 16:09:45 -08:00
cmd='echo led_token=$(cat "$VDEV_ENC_SYSFS_PATH/fault"),"$VDEV_ENC_SYSFS_PATH",'
2017-02-16 15:41:48 -06:00
out=$($ZPOOL status -vc "$cmd" "$pool" | grep 'led_token=')
2017-02-10 16:09:45 -08:00
2017-03-09 10:20:15 -08:00
#shellcheck disable=SC2034
echo "$out" | while read -r vdev state read write chksum therest; do
2017-02-10 16:09:45 -08:00
# Read out current LED value and path
tmp=$(echo "$therest" | sed 's/^.*led_token=//g')
2017-03-09 10:20:15 -08:00
vdev_enc_sysfs_path=$(echo "$tmp" | awk -F ',' '{print $2}')
current_val=$(echo "$tmp" | awk -F ',' '{print $1}')
if [ "$current_val" != "0" ] ; then
current_val=1
fi
2017-02-10 16:09:45 -08:00
if [ -z "$vdev_enc_sysfs_path" ] ; then
# Skip anything with no sysfs LED entries
continue
fi
2017-02-16 15:41:48 -06:00
if [ ! -e "$vdev_enc_sysfs_path/fault" ] ; then
2017-03-09 10:20:15 -08:00
#shellcheck disable=SC2030
2017-02-16 15:41:48 -06:00
rc=1
zed_log_msg "vdev $vdev '$file/fault' doesn't exist"
continue;
2017-02-10 16:09:45 -08:00
fi
val=$(state_to_val "$state")
2017-03-09 10:20:15 -08:00
if [ "$current_val" = "$val" ] ; then
2017-02-10 16:09:45 -08:00
# LED is already set correctly
continue;
fi
2017-03-09 10:20:15 -08:00
if ! check_and_set_led "$vdev_enc_sysfs_path/fault" "$val"; then
rc=1
fi
2017-02-10 16:09:45 -08:00
2017-03-09 10:20:15 -08:00
done
2017-02-16 15:41:48 -06:00
2017-03-09 10:20:15 -08:00
#shellcheck disable=SC2031
if [ "$rc" = "0" ] ; then
2017-02-10 16:09:45 -08:00
return 0
else
# We didn't see a sysfs entry that we wanted to set
return 3
fi
}
2019-02-04 12:07:19 -05:00
if [ -n "$ZEVENT_VDEV_ENC_SYSFS_PATH" ] && [ -n "$ZEVENT_VDEV_STATE_STR" ] ; then
2017-02-10 16:09:45 -08:00
# Got a statechange for an individual VDEV
val=$(state_to_val "$ZEVENT_VDEV_STATE_STR")
2017-03-09 10:20:15 -08:00
vdev=$(basename "$ZEVENT_VDEV_PATH")
2017-02-10 16:09:45 -08:00
check_and_set_led "$ZEVENT_VDEV_ENC_SYSFS_PATH/fault" "$val"
else
# Process the entire pool
2017-03-09 10:20:15 -08:00
poolname=$(zed_guid_to_pool "$ZEVENT_POOL_GUID")
process_pool "$poolname"
2017-02-10 16:09:45 -08:00
fi