VMware Cloud Community
DavidMcKnight
Contributor
Contributor
Jump to solution

Another Datastore Scripting Question

I need to do some storage server upgrades.  That means removing all the mapped iSCSI datastores being shared from a particular storage server. Normally I would do it manually from inside of the vSphere client, just to make sure that the datastores are empty.  So after I'm confident a datastore is empty I would unmount the datastore then delete it from inside of vSphere Client.  My problem is, when I go back to the storage server it says that all my hosts are still connected to iSCSI Target.  So the question is: Can I/How do I get my VMHosts to disconnect/logout of my storage server.  I understand I don't have to, but I would still like to know if there is a way to do it.  I guess my biggest problem is, that once I manually delete a datastore from my cluster I can't find a way to reference it or its LUNs to even attempt to clean up any lingering associations between my hosts and my decommissioned storage server.

How would I get a list of LUNs that are not actively being used in datastores?  I can't think of a reason why I would have a LUN and not have it being associated to an active datastore.

In the end, I think, I would use something like:

HostView.ConfigManager.StorageSystem.DetachScsiLun($LunUUID)

to have a host disconnect/logout from an iSCSI session?

Second question.  Along the same line as the first.  When I bring online the replacement storage server I'll be adding new iSCSI datastores to my cluster.  With my recent upgrade to vCenter 6.5 and being forced to use that god awful web interface to manage my VMware infrastructure, I can't find a way to see the iSCSI IQN on the page where I add a new datastore, so if I have multiple iSCSI targets ready to add to my cluster I can't tell which datastore is which to assign it the proper name.  At present I can manually get a list of the available device UUIDs then go back to the storage adapters configuration page, list all the LUNs there, then one by one cross-reference the UUID and get the IQN.

Anyway, how can get a list of all the LUNs no being used in datastores and get the IQN of that LUN?


I hope these questions make sense, and thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this.

$report = foreach($esx in Get-VMHost){

    $dsTab = @{}

    foreach($ds in Get-Datastore -VMHost $esx){

        if($ds.ExtensionData.Info.Vmfs){

            $ds.ExtensionData.Info.Vmfs.Extent | %{

                $dsTab.Add($_.DiskName,$ds.Name)

            }

        }

    }

    if($esx.ExtensionData.Config.StorageDevice.SoftwareInternetScsiEnabled){

        $hbaTab = @{}

        $esx.ExtensionData.Config.StorageDevice.HostBusAdapter | %{

            $hbaTab.Add($_.Key,$_.Device)

        }

        $lunTab = @{}

        $esx.ExtensionData.Config.StorageDevice.ScsiLun | %{

            $lunTab.Add($_.Key,$_.CanonicalName)

        }

        $esx.ExtensionData.Config.StorageDevice.ScsiTopology | %{

            foreach($adapter in ($_.Adapter | where{$_.Adapter -match "InternetScsi"})){

                $iqnTab = @{}

                foreach($lun in $esx.ExtensionData.Config.StorageDevice.MultipathInfo.Lun){

                    $lun.Path | where{$_.Adapter -eq $adapter.Adapter} | %{

                        $iqnTab.Add($lun.lun,$_.Transport.IScsiName)

                    }

                }

                $adapter.Target | %{

                    $obj = [ordered]@{

                        VMHost = $esx.Name

                        HBA = $hbaTab[$adapter.Adapter]

                        ScsiID = $_.Lun[0].Lun

                        CanonicalName = $lunTab[$_.Lun[0].ScsiLun]

                        IQN = $iqnTab[$_.Lun[0].ScsiLun]

                        Datastore = $dsTab[$lunTab[$_.Lun[0].ScsiLun]]

                    }

                    New-Object psobject -Property $obj

                }

            }

        }

    }

}

$report | Format-Table -AutoSize

It produces a report something like this.

When the Datastore property is empty, it indicates a LUN that is not used.

iscsi.png


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.

$report = foreach($esx in Get-VMHost){

    $dsTab = @{}

    foreach($ds in Get-Datastore -VMHost $esx){

        if($ds.ExtensionData.Info.Vmfs){

            $ds.ExtensionData.Info.Vmfs.Extent | %{

                $dsTab.Add($_.DiskName,$ds.Name)

            }

        }

    }

    if($esx.ExtensionData.Config.StorageDevice.SoftwareInternetScsiEnabled){

        $hbaTab = @{}

        $esx.ExtensionData.Config.StorageDevice.HostBusAdapter | %{

            $hbaTab.Add($_.Key,$_.Device)

        }

        $lunTab = @{}

        $esx.ExtensionData.Config.StorageDevice.ScsiLun | %{

            $lunTab.Add($_.Key,$_.CanonicalName)

        }

        $esx.ExtensionData.Config.StorageDevice.ScsiTopology | %{

            foreach($adapter in ($_.Adapter | where{$_.Adapter -match "InternetScsi"})){

                $iqnTab = @{}

                foreach($lun in $esx.ExtensionData.Config.StorageDevice.MultipathInfo.Lun){

                    $lun.Path | where{$_.Adapter -eq $adapter.Adapter} | %{

                        $iqnTab.Add($lun.lun,$_.Transport.IScsiName)

                    }

                }

                $adapter.Target | %{

                    $obj = [ordered]@{

                        VMHost = $esx.Name

                        HBA = $hbaTab[$adapter.Adapter]

                        ScsiID = $_.Lun[0].Lun

                        CanonicalName = $lunTab[$_.Lun[0].ScsiLun]

                        IQN = $iqnTab[$_.Lun[0].ScsiLun]

                        Datastore = $dsTab[$lunTab[$_.Lun[0].ScsiLun]]

                    }

                    New-Object psobject -Property $obj

                }

            }

        }

    }

}

$report | Format-Table -AutoSize

It produces a report something like this.

When the Datastore property is empty, it indicates a LUN that is not used.

iscsi.png


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DavidMcKnight
Contributor
Contributor
Jump to solution

Wow, that looks a lot more complicated than I thought it would.  I don't feel as bad not being able to figure this out on my own.  That being said, I'm going to assume it's right and take a some time to chew on your code.

One last point of verification.  Given the information I now have access to.  What function do I call get a host to properly disconnect a remote iSCSI session?

Thanks again.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this

Get-VMHostHba -VMHost esx1* -Type IScsi | Get-IScsiHbaTarget | Remove-IScsiHbaTarget -WhatIf


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos