Category Archives: VMWare

Creating DataStores From EMC Storage for your VMWare ESX environment with PowerCLI


I recently built my VMWare ESX environment out and we’ve got a VNX 5700 for our SAN. At some point we’ll be implimenting Site Recovery Manager and it was recommended that we use 1TB LUNs which is fine, but I’ve ended up with a lot of LUNs to map. Doing this in the client would just suck and the CLI is ok, but its a lot of tracking down to give them meaningful names.

How I prefer to name my DataStores is to use something that lets me know where it lives, like this VNX_P100_L15. This indicates the storage array (only have one VNX) and then the Pool that the LUN lives in (to indicate its performance level) and then the LUN number.

If you were to try and do this by hand its rather painful, so I wrote a script to do this for me.

Things you’ll need:

  • NaviSecCLI in your system path and security configured (would be easy to add perms to this if needed).
  • PowerCLI installed and connected (Connect-VIServer).
  • The IP of an SP on your VNX (any EMC array should work here).
  • The Storage Group you use for your VNX.
  • The Storage accessible to your environment (Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs will make sure all hosts see the storage)
## EMC info
$sg = "<your Storage Group Name, Case matters"
$ip = "IP of an SP"
## VM info
$vmhost = Get-VMHost "Any ESX Hostname" ##any host since all should see storage

###EMC data gathering
## get ALU/HLU
$rtn = (naviseccli -h $ip storagegroup -list -gname $sg) -match "\s+\d+\s+\d+"

$luns = @()
$rtn | %{$luns += ,(-split $_)}

##[0] = host(HLU), [1] = storage(ALU)

##Find what pool the LUN belongs to.
for($i=0;$i -lt $luns.count; $i++)
{
    $pool = ((naviseccli -h $ip lun -list -l $luns[$i][1]) -match "pool name")[0].substring(10).trim()
    ## in my case remove "pool" in pool names are something like "Pool 100", you're might be different
    $pool = $pool.replace("Pool ","")

    ##Build Lun Names
    $luns[$i] += "VNX_P$($pool)_L$($luns[$i][1])"
}

## VNX part
##get host lun numbers with the lun itself.
$VMLUNS = $VMHost | Get-SCSILun |?{$_.vendor -eq "DGC" -and $_.capacitymb  -gt 1024} | select *,HOSTLUN
$VMLUNKey = (get-view $vmhost).Config.StorageDevice.ScsiTopology | ForEach {$_.Adapter} | ForEach {$_.Target} | ForEach {$_.Lun}
$VMLUNS | %{$key = $_.key; $_.HOSTLUN = $VMLUNKey |?{$_.scsilun -eq $key} | select -exp lun -un}

##currently used canonicals to make sure you dont try to create a used datastore
$inuse = Get-Datastore | where {$_.type -eq 'vmfs'} | get-view | %{$_.info.vmfs.extent} | select -ExpandProperty diskname
$vmluns = $VMLUNS | ? {$inuse -notcontains $_.canonicalname}

##Create the DataStores
foreach($vmlun in $vmluns)
{
    $hostlun = $vmlun.HOSTLUN
    $lun = $luns | where { $_[0] -eq $hostlun}

    New-Datastore -VMHost $vmhost -Name $($lun[2]) -Path $($vmlun.canonicalname) -BlockSizeMB 2 -Vmfs
}

## rescan so all hosts see new DataStores
Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs

You’ll notice I set the BlockSizeMB to be 2, which limits VM disk to 512GB, which is generally enough (good middle ground for SRM)

Im new to ESX so any feedback would be welcomed.

Hope it helps!