The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 15 Jul 2011

PowerCLI, PowerShell, VMware

Tags: , , , , ,


Its bizarrely difficult to be able to map VMware presented datastore names to the underlying LUN’s in the PowerCLI, despite the fact that its readily available via the VI Client.  It’s even more difficult if you have to worry about both VI3 and VI4 vSphere installations as they handle storage in differing ways, hence the two different approaches below. The ESX3 method attacks the problem by starting its search at an ESX, whereas for ESX4 we start the attack at the cluster…

Both of the below gives an output a bit like this…

HP       HSV200           vmhba1:0:20   VMFS-DS-08
HP       HSV200           vmhba1:0:7    VMFS-DS-01
HP       HSV200           vmhba1:0:22   VMFS-DS-10
HP       HSV200           vmhba1:0:8    VMFS-DS-02
HP       HSV200           vmhba1:0:10   VMFS-DS-03
HP       HSV200           vmhba1:0:18   VMFS-DS-07
HP       HSV200           vmhba1:0:21   VMFS-DS-09
HP       HSV200           vmhba1:0:12   VMFS-DS-05
HP       HSV200           vmhba1:0:11   VMFS-DS-04
HP       HSV200           vmhba1:0:3    VMFS-DS-Templates
HP       HSV200           vmhba1:0:2    VMFS-DS-ISOs
HP       HSV200           vmhba1:0:30   VMFS-DS-SCRATCH
HP       HSV200           vmhba1:0:13   VMFS-DS-06

ESX3

The following was adapted from the work of Catman, which can be found in this forum thread – http://communities.vmware.com/thread/240466#240466

$objESX = Get-VMHost "My_ESX*"
 
# Get .NET views for host and storage system
$objViewESX = Get-View -id $objESX.id
$objViewESXstorageSys = Get-View -id $objViewESX.ConfigManager.StorageSystem
 
# Get FC HBAs
$HBAs = $objViewESXstorageSys.StorageDeviceInfo.HostBusAdapter | Where-Object {$_.Key -like "*FibreChannelHba*"}
 
foreach ($hba in $HBAs) {
    # Enumerate LUNs
    $LUNcount = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun.Length
 
    for ($LUNidx = 0; $LUNidx -lt $LUNcount; $LUNidx++ ) {
        $objScsiLUN = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun[$LUNidx]
 
        # Enumerate paths on LUN
        $PathCount = $objScsiLUN.Path.Length
 
        for ($PathIdx = 0; $PathIdx -lt $PathCount; $PathIdx++) {
            $objSCSIpath = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun[$LUNidx].Path[$PathIdx]
 
            # Only care about one path, active on current HBA
            if (($objSCSIpath.PathState -eq "active") -and ($objSCSIpath.Adapter -eq $hba.Key)) {
                # Now get the disk that we want
                $objSCSIdisk = $objViewESXstorageSys.StorageDeviceInfo.ScsiLun | Where-Object{ ($_.CanonicalName -eq $objScsiLUN.Id) -and ($_.DeviceType -eq "disk") }
 
                # Now get the datastore info for disk
                $MountCount = $objViewESXstorageSys.FileSystemVolumeInfo.MountInfo.Length
 
                for ($MountIdx = 0; $MountIdx -lt $MountCount; $MountIdx++ ) {
                    if ($objViewESXstorageSys.FileSystemVolumeInfo.MountInfo[$MountIdx].Volume.Type -eq "VMFS" ) {
                        $objVolume = $objViewESXstorageSys.FileSystemVolumeInfo.MountInfo[$MountIdx].Volume
 
                        $ExtentCount = $objVolume.Extent.Length
 
                        for ($ExtentIdx = 0; $ExtentIdx -lt $ExtentCount; $ExtentIdx++ ) {
                            $objExtent = $objVolume.Extent[$ExtentIdx]
 
                            # Match extent name to disk name
                            if ($objExtent.DiskName -eq $objSCSIdisk.CanonicalName) {
                                Write-Host($objSCSIdisk.Vendor + " " + $objSCSIdisk.Model + " " + $objSCSIdisk.CanonicalName + "`t" + $objVolume.Name)
                            }
                        }
                    }
                }
            }
        }
    }
}

ESX4

foreach ($cluster in (Get-View -ViewType "ClusterComputeResource")) {
    $vmhostsview = $cluster.host | % { Get-View $_ }
    $vmhostview  = $vmhostsview | Select -first 1
    $ScsiLuns    = $vmhostsview | % { $_.Config.StorageDevice.ScsiLun } | Select -unique *
    $UUIDs       = $ScsiLuns | Select -unique UUID
    $Datastores  = $vmhostsview | % { $_.Config.FileSystemVolume.MountInfo } | % { $_.Volume } | Select -Unique *
    $HostLUN     = $vmhostsview | % { $_.Config.StorageDevice.ScsiTopology.Adapter } | % { $_.Target | % { $_.LUN } } | Select -unique *
    foreach ($UUID in $UUIDs) {
        $Lun = $ScsiLuns | ? { $_.UUID -eq $UUID.UUID } | Select -first 1
        $objVolume               = "" | Select Datastore, Make, Model, LUN
 
        $objVolume.LUN       = ($HostLUN | ? { $_.ScsiLun -eq $Lun.Key } | select -unique LUN).LUN
        $objVolume.Make        = $Lun.Vendor
        $objVolume.Model         = $Lun.Model
        foreach ($vol in $Datastores) {
            if ($vol.extent | % { $_.diskname -eq $Lun.CanonicalName}) {
                $objVolume.Datastore  = $vol.Name
            }
        }
        $objVolume
    }
}

 


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.