8fbece
#!/bin/bash
8fbece
#
8fbece
# This script reads it's configuration from /etc/sysconfig/raid-check
8fbece
# Please use that file to enable/disable this script or to set the
8fbece
# type of check you wish performed.
8fbece
8fbece
# We might be on a kernel with no raid support at all, exit if so
8fbece
[ -f /proc/mdstat ] || exit 0
8fbece
8fbece
# and exit if we haven't been set up properly
8fbece
[ -f /etc/sysconfig/raid-check ] || exit 0
8fbece
. /etc/sysconfig/raid-check
8fbece
8fbece
# Wait until no more than arg1 arrays in arg2 list are busy
8fbece
waitbusy() {
8fbece
    local threshold=$(($1 + 1))
8fbece
    local dev_list="$2"
8fbece
    while true
8fbece
    do
8fbece
	local busy=0
8fbece
	local dev=""
8fbece
	for dev in $dev_list; do
8fbece
	    local sync_action=`cat /sys/block/$dev/md/sync_action`
8fbece
	    if [ "$sync_action" != "idle" ]; then
8fbece
		let busy++
8fbece
	    fi
8fbece
	done
8fbece
        [ $busy -lt $threshold ] && break
8fbece
	sleep 60
8fbece
    done
8fbece
}
8fbece
8fbece
[ "$ENABLED" != "yes" ] && exit 0
8fbece
8fbece
case "$CHECK" in
8fbece
    check) ;;
8fbece
    repair) ;;
8fbece
    *) exit 0;;
8fbece
esac
8fbece
8fbece
ionice=""
8fbece
renice=""
8fbece
case $NICE in
8fbece
    high)
8fbece
	renice="-n -5"
8fbece
	;;
8fbece
    low)
8fbece
	renice="-n 5"
8fbece
	ionice="-c2 -n7"
8fbece
	;;
8fbece
    idle)
8fbece
	renice="-n 15"
8fbece
	ionice="-c3"
8fbece
	;;
8fbece
    *)
8fbece
	;;
8fbece
esac
8fbece
8fbece
active_list=`grep "^md.*: active" /proc/mdstat | cut -f 1 -d ' '`
8fbece
[ -z "$active_list" ] && exit 0
8fbece
8fbece
declare -A check
8fbece
dev_list=""
8fbece
check_list=""
8fbece
for dev in $active_list; do
8fbece
    echo $SKIP_DEVS | grep -w $dev >&/dev/null && continue
8fbece
    if [ -f /sys/block/$dev/md/sync_action ]; then
8fbece
	# Only perform the checks on idle, healthy arrays, but delay
8fbece
	# actually writing the check field until the next loop so we
8fbece
	# don't switch currently idle arrays to active, which happens
8fbece
	# when two or more arrays are on the same physical disk
8fbece
	array_state=`cat /sys/block/$dev/md/array_state`
8fbece
	if [ "$array_state" != "clean" -a "$array_state" != "active" ]; then
8fbece
	    continue
8fbece
	fi
8fbece
	sync_action=`cat /sys/block/$dev/md/sync_action`
8fbece
	if [ "$sync_action" != idle ]; then
8fbece
	    continue
8fbece
	fi
8fbece
	ck=""
8fbece
	echo $REPAIR_DEVS | grep -w $dev >&/dev/null && ck="repair"
8fbece
	echo $CHECK_DEVS | grep -w $dev >&/dev/null && ck="check"
8fbece
	[ -z "$ck" ] && ck=$CHECK
8fbece
	dev_list="$dev_list $dev"
8fbece
	check[$dev]=$ck
8fbece
	[ "$ck" = "check" ] && check_list="$check_list $dev"
8fbece
    fi
8fbece
done
8fbece
[ -z "$dev_list" ] && exit 0
8fbece
8fbece
for dev in $dev_list; do
8fbece
    #Only run $MAXCONCURRENT checks at a time
8fbece
    if [ -n "$MAXCONCURRENT" ]; then
8fbece
	waitbusy $((MAXCONCURRENT - 1)) "$dev_list"
8fbece
    fi
8fbece
    echo "${check[$dev]}" > /sys/block/$dev/md/sync_action
8fbece
8fbece
    resync_pid=""
8fbece
    wait=10
8fbece
    while [ $wait -gt 0 -a -z "$resync_pid" ]; do
8fbece
	sleep 6
8fbece
	let wait--
8fbece
	resync_pid=$(ps -ef | awk -v mddev=$dev 'BEGIN { pattern = "^\\[" mddev "_resync]$" } $8 ~ pattern { print $2 }')
8fbece
    done
8fbece
    [ -n "$resync_pid" -a -n "$renice" ] &&
8fbece
    	renice $renice -p $resync_pid >&/dev/null
8fbece
    [ -n "$resync_pid" -a -n "$ionice" ] &&
8fbece
    	ionice $ionice -p $resync_pid >&/dev/null
8fbece
done
8fbece
[ -z "$check_list" ] && exit 0
8fbece
8fbece
waitbusy 0 "$check_list"
8fbece
8fbece
for dev in $check_list; do
8fbece
	mismatch_cnt=`cat /sys/block/$dev/md/mismatch_cnt`
8fbece
	# Due to the fact that raid1/10 writes in the kernel are unbuffered,
8fbece
	# a raid1 array can have non-0 mismatch counts even when the
8fbece
	# array is healthy.  These non-0 counts will only exist in
8fbece
	# transient data areas where they don't pose a problem.  However,
8fbece
	# since we can't tell the difference between a non-0 count that
8fbece
	# is just in transient data or a non-0 count that signifies a
8fbece
	# real problem, simply don't check the mismatch_cnt on raid1
8fbece
	# devices as it's providing far too many false positives.  But by
8fbece
	# leaving the raid1 device in the check list and performing the
8fbece
	# check, we still catch and correct any bad sectors there might
8fbece
	# be in the device.
8fbece
	raid_lvl=`cat /sys/block/$dev/md/level`
8fbece
	if [ "$raid_lvl" = "raid1" -o "$raid_lvl" = "raid10" ]; then
8fbece
	    continue
8fbece
	fi
8fbece
	if [ "$mismatch_cnt" -ne 0 ]; then
8fbece
		echo "WARNING: mismatch_cnt is not 0 on /dev/$dev"
8fbece
	fi
8fbece
done
8fbece