Blame SOURCES/mdcheck

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