Blame SOURCES/raid-check

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