check_dd_partially 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/bin/bash

usage="$(basename "$0") [-v] [-q] n src des -- verifies per sha256sum of n(>=2) equidistant distributed blocks of size 4096kB, that src was correctly written to des
$(basename "$0") -h

where:
    -v  verbose: prints the sha256sums
    -q  quite: suppresses the output of the result,
        the return-Code remains as reponse
    -h  prints this help message"

quite=false;
verbose=false;
printHelp=false;
for opt in "$@"
do
	case $opt in
		-v) verbose=true ; shift 1 ;;
		-q) quite=true ; shift 1 ;;
		-h) printHelp=true ; shift 1 ;;
		-?) printHelp=true ; shift 1 ;;
	esac
done

if [ "$printHelp" = true ] || [ $# -ne 3 ] || [ $1 -le 1 ]
then
	echo "$usage";
	exit -1;
fi

n=$1
src=$2;
des=$3;
lengthSrc=$(($(stat -c '%s' $src) / 4096)); # length of the source (in 4096kB)
if [ "$verbose" = true ]
then
	echo "length of the source (in 4096kB): $lengthSrc"
fi
failed=false;
for i in $(seq 0 $(($n-1)))
do
	offset=$(echo "$i*($lengthSrc-1)/($n-1)" | bc );
	if [ "$verbose" = true ]
	then
		echo "comparing blocks at offset $offset"
	fi
	sha256sumSrc=$(dd if=$src skip=$offset bs=4096 count=1 status=none | sha256sum)
	if [ "$verbose" = true ]
	then
		echo "sha256sum of the source:      $sha256sumSrc"
	fi
	sha256sumDes=$(dd if=$des skip=$offset bs=4096 count=1 status=none | sha256sum)
	if [ "$verbose" = true ]
	then
		echo "sha256sum of the destination: $sha256sumDes"
	fi
	if [ "$sha256sumSrc" != "$sha256sumDes" ]
	then
		failed=true;
		break;
	fi
done
if [ "$failed" = false ]
then
	if [ "$quite" = false ]
	then
		echo "OK!";
	fi
	exit 0;
else
	if [ "$quite" = false ]
	then
		echo "FAILED!";
	fi
	exit 1;
fi