dcavalca / rpms / mdadm

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