Blame SOURCES/mdcheck

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