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