@echo off
rem Creates a volume using LSI's cmdtool2 utility
rem assumes BIOS option for RAID is enabled
rem assumes the driver have been loaded if RAID has been configured in BIOS

set errlevel=0
set logfile=%DeploymentStore%\cmdtool2_createvolume.log

echo cmdtool2_createvolume.cmd started. > %logfile%

call queryProperty IntelBIOSID BoardFamilyID S5000
if %errorlevel% EQU 0 (
    echo This script only works on a S5000 system
    set errorlevel=1
    GOTO ERROR
)
call queryProperty BIOSRAID HWRAID Enabled
if %errorlevel% EQU 1 goto READY
call queryProperty BIOSRAID SWRAID Enabled
if %errorlevel% EQU 0 (
    rem setup BIOS for RAID
    call setBIOS4RAID enable SAS
    GOTO ERROR
)

call queryProperty BIOSRAID Vendor LSI
if %errorlevel% EQU 0 (
    rem setup BIOS for RAID
    call setBIOS4RAID enable SAS
    GOTO ERROR
)
:READY
rem Clear previous RAID volumes and configuration information:

CmdTool2 -CfgClr -aALL
CmdTool2 -CfgForeign -clear -aALL
rem No error handling done here just in case there are no volumes to clear.

rem find 2 disks:
setlocal enabledelayedexpansion
set DiskA=none
set DiskB=none
for /L %%N IN (0,1,6) DO (
    for /F %%D IN ('queryProperty Disk device - slot="%%N"') DO (
        if !errorlevel! EQU 0 (
            if !DiskA!==none (
                set DiskA=%%N
            ) else (
                set DiskB=%%N
                GOTO DISKS
            )
        )
    )
)
echo 2 disks not found for RAID
goto ERROR

:DISKS
echo DiskA: %DiskA%
echo DiskB: %DiskB%

rem Get Enclosure ID
call queryProperty BIOSRAID HWRAID Enabled
if %errorlevel% EQU 0 (
    set DeviceID=
    GOTO CREATE
)
for /F "tokens=2 delims=:" %%A IN ('"cmdtool2 -EncInfo -a0|findstr /C:"Device ID""') DO set DeviceID=%%A
if %errorlevel% geq 1 goto ERROR
ECHO Obtained Enclosure Device ID: %DeviceID%
:CREATE
rem Create RAID 0 Volume using 2 disks:
CmdTool2 -CfgLDAdd -R0[%DeviceID%:%DiskA%,%DeviceID%:%DiskB%] -a0
if %errorlevel% geq 1 goto ERROR
ECHO RAID 0 created successfully.

rem Initialize the New Volume
CmdTool2 -LDInit -Start Full -L0 -a0
if %errorlevel% geq 1 goto ERROR
ECHO Initialization started ...

rem Display the initialization progress
CmdTool2 -LDInit -ProgDsply -L0 -a0
if %errorlevel% geq 1 goto ERROR

rem Set Adapter Properties
CmdTool2 -LDSetProp WB -L0 -a0
if %errorlevel% geq 1 goto ERROR
ECHO Adapter properties are set.

rem create partiton
echo list disk > disk.txt
for /f "tokens=1,2 delims= " %%A in ('diskpart /s disk.txt^|findstr "Disk"') do echo select %%A %%B > disk.txt
echo create partition primary >> disk.txt
echo assign >> disk.txt
diskpart /s disk.txt
rem get volume drive letter
echo list volume > disk.txt
for /f "tokens=3 delims= " %%A in ('diskpart /s disk.txt^|findstr "RAW"') do echo.|echo Y|format %%A: /FS:NTFS /V:ESRT2Vol /Q
del disk.txt
goto OK


:ERROR
set errlevel=%errorlevel%
ECHO cmdtool2_createvolume completed with errors.
goto DONE

:OK
set errlevel=0
ECHO cmdtool2_createvolume completed successfully.
goto DONE

:DONE
ECHO cmdtool2_createvolume completed with status %errlevel% >> %logfile%
exit /B %errlevel%
