#!/bin/sh
#
errlevel=0
ADDLOG=addLogRecord
# ADDLOG=echo

queryProperty IntelBIOSID BoardFamilyID S5000
if [ $? = 0 ];
then 
    echo This script only works on a S5000 system
    $ADDLOG $0 ERROR  "This script only works on a S5000 system"
    exit 1 
fi
queryProperty BIOSRAID HWRAID Enabled
if [ $? = 0 ];
then 
    queryProperty BIOSRAID SWRAID Enabled
    if [ $? = 0 ];
    then 
        chassis=`queryProperty FRU "Chassis OEM Field" -`
        if [ "$chassis" = "NSW1U" ]
        then
            echo setBIOS4RAID enable SATA
            setBIOS4RAID enable SATA
            $ADDLOG $0 INFO "setBIOS4RAID enable SATA"
        else
            echo setBIOS4RAID enable SAS
            setBIOS4RAID enable SAS
            $ADDLOG $0 INFO "setBIOS4RAID enable SAS"
        fi
        #after setting the BIOS for RAID a reboot is required
        exit 9 
    fi
fi
queryProperty BIOSRAID Vendor LSI
if [ $? = 0 ];
then 
        chassis=`queryProperty FRU "Chassis OEM Field" -`
        if [ "$chassis" = "NSW1U" ]
        then
            echo setBIOS4RAID enable SATA
            setBIOS4RAID enable SATA
            $ADDLOG $0 INFO "setBIOS4RAID enable SATA"
        else
            echo setBIOS4RAID enable SAS
            setBIOS4RAID enable SAS
            $ADDLOG $0 INFO "setBIOS4RAID enable SAS"
        fi
        #after setting the BIOS for RAID a reboot is required
        exit 9
fi

$ADDLOG $0 INFO "Inquire if the RAID driver is running"
CmdTool2 -adpAllInfo -aALL -silent
if [ $? = 1 ];
then
    $ADDLOG $0 ERROR "RAID driver is NOT installed."
	exit 1 
fi
#set -x


$ADDLOG $0 INFO "Clear previous RAID volumes and configuration information:"
CmdTool2 -CfgClr -aALL  #clears the entire configuration
$ADDLOG $0 INFO "CmdTool2 -CfgClr -aALL"
#CmdTool2 -CfgLdDel -L0 -a0 #clears Logical Drive 0 on Adapter 0 
#No error handling hear just in case there are no volumes to clear.
 CmdTool2 -CfgForeign -Clear -aALL
 $ADDLOG $0 INFO "CmdTool2 -CfgForeign -Clear -aALL"
# find 2 disks:
DiskA=none
DiskB=none
 
for N in 0 1 2 3 4 5 6; do
    queryProperty Disk device - slot="$N"
    if [ $? = 0 ];
    then
        if [ "$DiskA" = "none" ];
        then
            DiskA=$N
        else
            DiskB=$N
        fi
    fi
done

if [ "$DiskA" = "none" ];
then
    echo ERROR: 2 disks not found for RAID
    $ADDLOG $0 ERROR "2 disks not found for RAID"
    exit 1
fi
if [ "$DiskB" = "none" ];
then
    echo ERROR: 2 disks not found for RAID
    $ADDLOG $0 ERROR "2 disks not found for RAID"
    exit 1
fi

$ADDLOG $0 INFO "Two diska found...  DiskA: $DiskA   DiskB: $DiskB"
echo DiskA: $DiskA
echo DiskB: $DiskB

$ADDLOG $0 INFO "Get Enclosure Device ID"
queryProperty BIOSRAID HWRAID Enabled
if [ $? = 1 ];
then
    EID="`CmdTool2 -EncInfo -a0 |grep "Device ID"| awk '{print $4}'`"
    if [ $? = 1 ];
    then
    	exit 1
    fi
    echo Obtained Enclosure Device ID: $EID
    $ADDLOG $0 INFO "Obtained Enclosure Device ID: $EID"
else
    EID=
fi

$ADDLOG $0 INFO "Create RAID 0 Volume using the first 2 disks:"
CmdTool2 -CfgLdAdd -R1[$EID:$DiskA,$EID:$DiskB] -a0
if [ $? = 1 ];
then
    $ADDLOG $0 ERROR "CmdTool2 -CfgLdAdd -R1[$EID:$DiskA,$EID:$DiskB] -a0"
	exit 1
fi

$ADDLOG $0 INFO "Initialize the New Volume"
CmdTool2 -LDInit -Start -L0 -a0
if [ $? = 1 ];
then
    $ADDLOG $0 ERROR "CmdTool2 -LDInit -Start -L0 -a0"
	exit 1
fi

$ADDLOG $0 INFO "Display the initialization progress"
CmdTool2 -LDInit -ProgDsply -L0 -a0
if [ $? = 1 ];
then
    $ADDLOG $0 ERROR "CmdTool2 -LDInit -ProgDsply -L0 -a0"
	exit 1
fi

$ADDLOG $0 INFO "Set Disk Cache"
CmdTool2 -LDSetProp WB -L0 -a0
if [ $? = 1 ];
then
    $ADDLOG $0 ERROR "CmdTool2 -LDSetProp WB -L0 -a0"
	exit 1 
fi

echo A reboot is needed for the driver to recognize the RAID as a single DISK.  
echo Once rebooted, run fdisk to create partition and mkfs -t ext3 /dev/sdxN where /dev/sdxN is the name of the partition to format.
$ADDLOG $0 INFO "RAID configured successfully."
exit $errlevel 
