dcavalca / rpms / mdadm

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