#!/bin/bash

# There are 4 ways to call this script
#
# 1) "start clean"
#    - makes new disk files
#    - binds them to loop devices
#    - creates a new raid device
#    - makes a file system on the device
#    - mounts the file system
#    - copies random data to the device
#    - takes the md5sum of the data
# 2) "stop clean"
#    - unmounts the file system
#    - stops the raid device
#    - unbinds the loop devices
#    - removes the disk files
# 3) "start"
#    - starts the raid device
#    - mounts the filesystem
#    - takes the md5sum of the data
# 4) "stop"
#    - unmounts the file system
#    - stops the raid device


# These things are variable
export TESTDIR=/tmp/raid_test_drives;
export COMPONENTS_COUNT=8;
export COMPONENT_SIZE=5M;
export ARRAY_NAME=/dev/md0;

# Make sure we got an argument
if [ "$1" == "" ]; then
    echo "You must use either 'stop' or 'start' as an argument";
    exit;
fi

# See if we are stopping
if [ "$1" == "stop" ]; then
    echo "Stopping...";
    export STARTING=0;

else
    echo "Starting...";
    export STARTING=1;
fi

# See if we are supposed to do things cleanly
if [ "$2" == "clean" ]; then
    echo "    Cleaning on shutdown...";
    export CLEANING=1;
else
    export CLEANING=0;
fi


# If we're stopping, we need to stop the array first
if [ $STARTING == 0 ]; then

    echo "    Unmounting array...";
    umount "$TESTDIR/array";
    rmdir "$TESTDIR/array";

    echo "    Stopping kernel md device...";

    # Stop the raid array so we can deconstruct the components
    /sbin/mdadm --manage --stop $ARRAY_NAME;

else

    # Start building the component list
    export COMPONENT_LIST="";
fi



# For each component that's supposed to be in the array
for ((DRIVE=0; $DRIVE < $COMPONENTS_COUNT; DRIVE = $DRIVE+1)); do

    COMPONENT="$TESTDIR/disk$DRIVE";

    if [ $STARTING == 1 ]; then

	if [ $CLEANING == 1 ]; then
	    echo "    Creating disk component file $COMPONENT.img...";
	    echo "dd if=/dev/zero of=$COMPONENT.img bs=$COMPONENT_SIZE count=1";
	    dd if=/dev/zero of="$COMPONENT.img" bs="$COMPONENT_SIZE" count=1;

	    echo "    Creating loop device for disk component $COMPONENT.img...";
	    echo "losetup /dev/loop$DRIVE $COMPONENT.img";
	    losetup "/dev/loop$DRIVE" "$COMPONENT.img";
	fi

	export COMPONENT_LIST="$COMPONENT_LIST /dev/loop$DRIVE";
	
    else
	
	if [ $CLEANING == 1 ]; then
	    echo "    Removing loop device for disk component $COMPONENT.img ...";
	    echo "losetup -d /dev/loop$DRIVE";
	    losetup -d "/dev/loop$DRIVE";

	    echo "    Removing disk compoment $COMPONENT.img ...";
	    rm -f "$COMPONENT.img";
	fi

    fi

done


# If we're starting, create the kernel array, make a file system, and put data in it
if [ $STARTING == 1 ]; then

    if [ $CLEANING == 1 ]; then
	echo "    Creating kernel-level array...";
	/sbin/mdadm --create -l5 -n$COMPONENTS_COUNT $ARRAY_NAME $COMPONENT_LIST;

	echo "    Making file system...";
	/sbin/mke2fs -j -m1 $ARRAY_NAME;
    else
	echo "    Starting kernel-level array...";
	/sbin/mdadm --assemble $ARRAY_NAME $COMPONENT_LIST;
    fi

    echo "    Mounting file system...";
    mkdir "$TESTDIR/array"
    mount -t ext3 $ARRAY_NAME "$TESTDIR/array"

    if [ $CLEANING == 1 ]; then
	echo "    Copying random data to the array..."
	dd if=/dev/urandom of=$TESTDIR/array/testdata.dat
    fi

    echo "    Calculating checksum of random data..."
    md5sum $TESTDIR/array/testdata.dat > $TESTDIR/`date +%s`.md5sum

fi


echo "done.";
