dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

Blame SOURCES/raid-check

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