Home > datastore, LUN, PowerShell > Find free SCSI LUNs

Find free SCSI LUNs

January 15th, 2013 Leave a comment Go to comments

Another post that comes from a VMTN PowerCLI Community question. Jeff wanted to find the free SCSI LUNs in his environment.
While answering that thread I was amazed there was no PowerCLI function written yet to provide this functionality. At least that was what my friend Google told me ;-)

Since there exists a SDK method that makes retrieving free SCSI LUNs quite easy, the function I came up with isn’t too complex.

But it should help you in further automating the setup of your datastores.

The Script

function Get-FreeScsiLun {

<#  
.SYNOPSIS  Find free SCSI LUNs  
.DESCRIPTION The function will find the free SCSI LUNs
  on an ESXi server
.NOTES  Author:  Luc Dekens  
.PARAMETER VMHost
    The VMHost where to look for the free SCSI LUNs  
.EXAMPLE
   PS> Get-FreeScsiLun -VMHost $esx
.EXAMPLE
   PS> Get-VMHost | Get-FreeScsiLun
#>

  param (
  [parameter(ValueFromPipeline = $true,Position=1)]
  [ValidateNotNullOrEmpty()]
  [VMware.VimAutomation.Client20.VMHostImpl]
  $VMHost
  )

  process{
    $storMgr = Get-View $VMHost.ExtensionData.ConfigManager.DatastoreSystem

    $storMgr.QueryAvailableDisksForVmfs($null) | %{
      New-Object PSObject -Property @{
        VMHost = $VMHost.Name
        CanonicalName = $_.CanonicalName
        Uuid = $_.Uuid
        CapacityGB = [Math]::Round($_.Capacity.Block * $_.Capacity.BlockSize / 1GB,2)
      }
    }
  }
}

Annotations

Line 24: The HostDatastoreSystem provides access to the method we are using

Line 26: The QueryAvailableDisksForVmfs method is called with a $null argument. That way we tell the method that we do not want to extend an existing datastore, but that we want to retrieve all free LUNs that can be used to create a datastore.

Line 27-32: The result is returned as an array of HostScsiDisk objects. With the New-Object cmdlet we pass the information that you can eventually use in the New-Datastore cmdlet.

Sample Usage

The use of this function is quite simple as the following example will show


$esx = Get-VMHost -Name MyEsx

Get-FreeScsiLun -VMHost $esx | Format-List

The result looks something like this. You can use the CanonicalName property in the Path parameter of the New-Datastore cmdlet.

freelun1

You can also use the function in a pipeline construct. For example something like this


Get-Cluster -Name MyCluster |
Get-VMHost |
Get-FreeScsiLun | Format-List

On my test cluster with 2 nodes this will return the following results. And as we can see the free SCSI LUNs are visible on both nodes.

freelun2

Enjoy !

Categories: datastore, LUN, PowerShell Tags: , ,
  1. Michael
    January 16th, 2013 at 16:33 | #1

    Nice work Luc, adding to toolbox.

  2. January 16th, 2013 at 00:31 | #2

    Nice Function, thanks LucD

  1. No trackbacks yet.