#!/bin/bash
. bsfl

DEVICES="/dev/sdb /dev/sdc /dev/sdd /dev/sdi /dev/sdj /dev/sdk"
NO_OF_DEVICES=6
ARRAY=/dev/md0
CHUNKS="64"
IODEPTHS="01 02 04 08 16 32 64"
MOUNT=/server
LOG=/var/log/raid-test.log
LEVEL="0 5 6 10"
export RUNTIME=60

test_array () {

	RAID_LEVEL="$1"
	CHUNK_SIZE="$2"
	MODE="$3"
	#TESTFILESIZE=`df -m | grep $ARRAY | awk '{ print $4 }'`
	#TESTFILESIZE="$TESTFILESIZE"M
	TESTFILESIZE=10G
	msg "* Test file size = $TESTFILESIZE" "$RED"
	export BLOCKSIZE=4k,4k
	export SIZE="$TESTFILESIZE"
	export DIRECTORY=$MOUNT

	RESULT_DIR=/fio/results2/raid-$RAID_LEVEL-chunk-$CHUNK_SIZE
	mkdir -p "$RESULT_DIR/$MODE"
	cd "$RESULT_DIR/$MODE"

	msg "* Performing $MODE test..." "$BLUE"

	if [ "$MODE" == "write" ]
	then
		msg "* Disabling write-back caching" "$ORANGE" 
		for q in $DEVICES
		do 
			hdparm -W 0 $q >> /dev/null 2>&1
		done
		umount $ARRAY >> /dev/null 2>&1
		mount -o sync $ARRAY $MOUNT >> /dev/null 2>&1
	fi 

	for a in $IODEPTHS
	do
		export IODEPTH=$a
		fio /fio/random-$MODE-template.fio >> /tmp/log.txt
	done

	if [ "$MODE" == "write" ]
	then
		msg "* Enabling write-back caching" "$ORANGE" 
		for q in $DEVICES
		do 
			hdparm -W 1 $q >> /dev/null 2>&1
		done
		umount $ARRAY >> /dev/null 2>&1
		mount $ARRAY $MOUNT >> /dev/null 2>&1
	fi 
}

for x in $CHUNKS
do
    for y in $LEVEL
    do
	    msg "==============================="
	    msg "* RAID $y - Chunk $x" "$YELLOW"
	    msg "* Umounting existing file system on $MOUNT" "$GREEN"
            umount $MOUNT >> /dev/null 2>&1
	    msg "* Stopping array $ARRAY" "$GREEN" 
	    sleep 2
            mdadm -S $ARRAY >> /dev/null 2>&1
	    sleep 2
	    msg "* Zero all MDADM devices" "$GREEN"
            mdadm --zero $DEVICES >> /dev/null 2>&1
	    sleep 2
	    msg "* Create new array $ARRAY" "$GREEN"
            yes | mdadm --create $ARRAY --assume-clean --level $y --chunk $x --raid-devices $NO_OF_DEVICES $DEVICES >> /dev/null 2>&1
            sleep 5
	    msg "* Create new file system" "$GREEN"
            mkfs.xfs $ARRAY -f 2>&1 >> /dev/null
	    sleep 5
	    msg "* Mounting file system on $MOUNT" "$GREEN"
            mount $ARRAY $MOUNT
	    sleep 2
	    test_array $y $x read
	    test_array $y $x write
            printf "%8.0f %7.0f\n" $x $y >> $LOG
    done
done



