VMware Cloud Community
steve_waugh
Contributor
Contributor

Detach Multiple LUNs from some esxi host - PowerCli

I have a cluster of 40 esxi hosts, need to decomission 5 esxi hosts from this cluster.

Till now I have migrated all vms out of these 5 hosts, put in maintenance, and host profile is detached, also I have ummounted around 86 datastores manually.

Now I need to detach storage devices/LUNs from these 5 hosts. But there 100 devices and its a huge manual task.

Can I detach LUNs only from these 5 hosts safely, with POwerCli ?

ESXi 6.0 and vcenter 6.0

Please share a script if anyone has one.

++ Looping the Master "LucD"

Tags (1)
11 Replies
conyards
Expert
Expert

Steve,

Automating Datastore/Storage Device Detachment in vSphere 5 - VMware vSphere Blog

The above article is probably a good starting point, if you haven't already read it.  The article links to scripts to target vCenter or ESXi hosts...  Well in it's own words;

"...we have a PowerCLI and vSphere SDK for Perl script that allows a user to connect either to a vCenter Server and/or directly to an ESX(i) host to easily perform a “unmount” and “detach” operation for unpresenting a LUN."

Blog references vSphere 5 but is still linked to from, How to unmount a LUN or detach a datastore device from ESXi hosts (2004605) | VMware KB - which was updated August 21st 2017 and lists coverage for 6.0 and 6.5.

Thanks

Simon

https://virtual-simon.co.uk/
0 Kudos
LucD
Leadership
Leadership

You can do something like this.

After the unmount and detach you can safely remove the LUN from the ESXi node.

You might first want to check if the datastore can be unmounted, see for example this function Test If The Datastore Can Be Unmounted

$esxName = 'MyESx'

$datastoreName = 'Datastore'

$esx = Get-VMHost -Name $esxName

$ds = Get-Datastore -Name $datastoreName

$canonicalName = $ds.ExtensionData.Info.Vmfs.Extent[0].DiskName

$storSys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem

$device = $storsys.StorageDeviceInfo.ScsiLun | where {$_.CanonicalName -eq $canonicalName}

if($device.OperationalState[0] -eq 'ok'){

# unmount disk

    $StorSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)

}

# Detach disk

$storSys.DetachScsiLun($device.Uuid)


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

0 Kudos
steve_waugh
Contributor
Contributor

Do I need to provide all the names of DataStore ? Can I opt of something like "All DataStores", ?
Also, What's the safest option to detach LUNs ( through powercli connect vcenter and they pass the esx name in this script, or Connect the individual esxi host only) ?

0 Kudos
LucD
Leadership
Leadership

You can place the code above in two nested ForEach loops and run through all the ESXi nodes and datastores.

The $esx and $ds can be your ForEach index variables.

Something like this

Note that the Get-VMHost and Get-Datastore cmdlets most probably need to adapted, since you want to do this for as limited set of ESXi nodes and datastores.

Do you have that info for example in CSV files?

foreach($esx in Get-VMHost){

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

        $canonicalName = $ds.ExtensionData.Info.Vmfs.Extent[0].DiskName

        

        $storSys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem

        

        $device = $storsys.StorageDeviceInfo.ScsiLun | where {$_.CanonicalName -eq $canonicalName}

        

        if($device.OperationalState[0] -eq 'ok'){

        # unmount disk

            $StorSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)

        }

        

        # Detach disk

        $storSys.DetachScsiLun($device.Uuid)

    }

}

You need to connect to the vCenter,

The code would then do actions on each ESXi node for each datastore.


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

0 Kudos
steve_waugh
Contributor
Contributor

I have only 4 esxi which is being retired, out of 80. so I would not risk "Get-Vmhost", instead, I want to pass the esxi name "manually" to avoid any unwanted esxi getting wiped out.

I have just pulled out the name of dataStore manually, so I can pass them as reference as an import from csv/txt. But then do I need to pass the naa.id as well ?

If that's the case then I would prefer your using your second script to Get-Datastore.

Can You tweak it considering I have only 5 esxi, arguments to be passed manually, and datstore + naa.id selected automatically for all.

0 Kudos
steve_waugh
Contributor
Contributor

In summary, I wish to avoid "For each" when selecting esxi hostname. Because I dont any accidental case where unwanted esxi are wiped out.

While I wish to use "for each", when selecting LUNs, so I don't to manually provide hundreds of LUN id or datastore name.

Can this be tweaked like above.

0 Kudos
LucD
Leadership
Leadership

You could do something like this

foreach($esx in Get-VMHost -Name esx1,esx2,esx3,esx4){

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


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

0 Kudos
madey83
Contributor
Contributor

Hello LucD,

is it possible to modify your code for something like this:

put in txt file identifier for luns which should be unmounted and detached from all ESX hosts in cluster?

this aproach will give 100% sure that script will remove correct luns.

Thank you for your help.

0 Kudos
LucD
Leadership
Leadership

Try something like this.
Note that this only works for datastores that only have 1 extent.

$clusterName = 'MyCluster'

$lunNames = Get-Content -Path .\lunnames.txt

foreach($esx in (Get-Cluster -Name $clusterName | Get-VMHost)){

    foreach($ds in (Get-Datastore -RelatedObject $esx | where{$lunNames -contains $_.ExtensionData.Info.Vmfs.Extent[0].DiskName)){

        $canonicalName = $ds.ExtensionData.Info.Vmfs.Extent[0].DiskName

        $storSys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem

        $device = $storsys.StorageDeviceInfo.ScsiLun | where {$_.CanonicalName -eq $canonicalName}

        if($device.OperationalState[0] -eq 'ok'){

        # unmount disk

            $StorSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)

        }

        # Detach disk

        $storSys.DetachScsiLun($device.Uuid)

    }

}


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

quankenyu
Contributor
Contributor

what does the lunnames.txt look like? is it as same as datastorename? 

I am trying to unmount and detach a few datastores in a active cluster, while leaving other vmfs datastores untouched. these datastores to be unmounted/detached also are connected to vmhosts in other live clusters. 

Thanks.

0 Kudos
LucD
Leadership
Leadership

No, the LUN identifiers (Canonical name)
See the requestors description "put in txt file identifier for luns which should be unmounted and detached"


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

0 Kudos