dcavalca / rpms / mdadm

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