dcavalca / rpms / mdadm

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