Blame SOURCES/mdcheck

2910d5
#!/bin/bash
2910d5
2910d5
# Copyright (C) 2014-2017 Neil Brown <neilb@suse.de>
2910d5
#
2910d5
#
2910d5
#    This program is free software; you can redistribute it and/or modify
2910d5
#    it under the terms of the GNU General Public License as published by
2910d5
#    the Free Software Foundation; either version 2 of the License, or
2910d5
#    (at your option) any later version.
2910d5
#
2910d5
#    This program is distributed in the hope that it will be useful,
2910d5
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
2910d5
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2910d5
#    GNU General Public License for more details.
2910d5
#
2910d5
#    Author: Neil Brown
2910d5
#    Email: <neilb@suse.com>
2910d5
2910d5
# This script should be run periodically to automatically
2910d5
# perform a 'check' on any md arrays.
2910d5
#
2910d5
# It supports a 'time budget' such that any incomplete 'check'
2910d5
# will be checkpointed when that time has expired.
2910d5
# A subsequent invocation can allow the 'check' to continue.
2910d5
#
2910d5
# Options are:
2910d5
#   --continue    Don't start new checks, only continue old ones.
2910d5
#   --duration    This is passed to "date --date=$duration" to find out
2910d5
#		  when to finish
2910d5
#
2910d5
# To support '--continue', arrays are identified by UUID and the 'sync_completed'
2910d5
# value is stored  in /var/lib/mdcheck/$UUID
2910d5
2910d5
# convert a /dev/md name into /sys/.../md equivalent
2910d5
sysname() {
2910d5
	set `ls -lLd $1`
2910d5
	maj=${5%,}
2910d5
	min=$6
2910d5
	readlink -f /sys/dev/block/$maj:$min
2910d5
}
2910d5
2910d5
args=$(getopt -o hcd: -l help,continue,duration: -n mdcheck -- "$@")
2910d5
rv=$?
2910d5
if [ $rv -ne 0 ]; then exit $rv; fi
2910d5
2910d5
eval set -- $args
2910d5
2910d5
cont=
2910d5
endtime=
2910d5
while [ " $1" != " --" ]
2910d5
do
2910d5
    case $1 in
2910d5
	--help )
2910d5
		echo >&2 'Usage: mdcheck [--continue] [--duration time-offset]'
2910d5
		echo >&2 '  time-offset must be understood by "date --date"'
2910d5
		exit 0
2910d5
		;;
2910d5
	--continue ) cont=yes ;;
2910d5
	--duration ) shift; dur=$1
2910d5
		endtime=$(date --date "$dur" "+%s")
2910d5
		;;
2910d5
    esac
2910d5
    shift
2910d5
done
2910d5
shift
2910d5
2910d5
# We need a temp file occasionally...
2910d5
tmp=/var/lib/mdcheck/.md-check-$$
2910d5
trap 'rm -f "$tmp"' 0 2 3 15
2910d5
2910d5
2910d5
# firstly, clean out really old state files
2910d5
mkdir -p /var/lib/mdcheck
2910d5
find /var/lib/mdcheck -name "MD_UUID*" -type f -mtime +180 -exec rm {} \;
2910d5
2910d5
# Now look at each md device.
2910d5
cnt=0
2910d5
for dev in /dev/md?*
2910d5
do
2910d5
	[ -e "$dev" ] || continue
2910d5
	sys=`sysname $dev`
2910d5
	if [ ! -f "$sys/md/sync_action" ]
2910d5
	then # cannot check this array
2910d5
		continue
2910d5
	fi
2910d5
	if [ "`cat $sys/md/sync_action`" != 'idle' ]
2910d5
	then # This array is busy
2910d5
		continue
2910d5
	fi
2910d5
2910d5
	mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp || continue
2910d5
	source $tmp
2910d5
	fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
2910d5
	if [ -z "$cont" ]
2910d5
	then
2910d5
		start=0
2910d5
		logger -p daemon.info mdcheck start checking $dev
2910d5
	elif [ -z "$MD_UUID" -o ! -f "$fl" ]
2910d5
	then
2910d5
		# Nothing to continue here
2910d5
		continue
2910d5
	else
2910d5
		start=`cat "$fl"`
2910d5
		logger -p daemon.info mdcheck continue checking $dev from $start
2910d5
	fi
2910d5
2910d5
	cnt=$[cnt+1]
2910d5
	eval MD_${cnt}_fl=\$fl
2910d5
	eval MD_${cnt}_sys=\$sys
2910d5
	eval MD_${cnt}_dev=\$dev
2910d5
	echo $start > $fl
2910d5
	echo $start > $sys/md/sync_min
2910d5
	echo check > $sys/md/sync_action
2910d5
done
2910d5
2910d5
if [ -z "$endtime" ]
2910d5
then
2910d5
	exit 0
2910d5
fi
2910d5
2910d5
while [ `date +%s` -lt $endtime ]
2910d5
do
2910d5
	any=
2910d5
	for i in `eval echo {1..$cnt}`
2910d5
	do
2910d5
		eval fl=\$MD_${i}_fl
2910d5
		eval sys=\$MD_${i}_sys
2910d5
2910d5
		if [ -z "$fl" ]; then continue; fi
2910d5
2910d5
		if [ "`cat $sys/md/sync_action`" != 'check' ]
2910d5
		then
2910d5
			eval MD_${i}_fl=
2910d5
			rm -f $fl
2910d5
			continue;
2910d5
		fi
2910d5
		read a rest < $sys/md/sync_completed
2910d5
		echo $a > $fl
2910d5
		any=yes
2910d5
	done
2910d5
	if [ -z "$any" ]; then exit 0; fi
2910d5
	sleep 120
2910d5
done
2910d5
2910d5
# We've waited, and there are still checks running.
2910d5
# Time to stop them.
2910d5
for i in `eval echo {1..$cnt}`
2910d5
do
2910d5
	eval fl=\$MD_${i}_fl
2910d5
	eval sys=\$MD_${i}_sys
2910d5
	eval dev=\$MD_${i}_dev
2910d5
2910d5
	if [ -z "$fl" ]; then continue; fi
2910d5
2910d5
	if [ "`cat $sys/md/sync_action`" != 'check' ]
2910d5
	then
2910d5
		eval MD_${i}_fl=
2910d5
		rm -f $fl
2910d5
		continue;
2910d5
	fi
2910d5
	echo idle > $sys/md/sync_action
2910d5
	cat $sys/md/sync_min > $fl
2910d5
	logger -p daemon.info pause checking $dev at `cat $fl`
2910d5
done