Detaching SCSI LUNs from ESXi hosts

About a month ago I experienced a production outage that related to improper removal of storage from a cluster. In this scenario, VMFS file systems were deleted from FC devices and then the FC devices were removed from the hosts. I’ve seen this done hundreds of times without issue, but as a best practice the devices should be detached from the host before they are removed. Here are some articles that discuss proper removal:

Manually detaching devices from a bunch of hosts doesn’t sound like a fun to me, so I created a quick script to help:
[cc lang=”powershell”]
#These are the temporary LUNs
$removeThese = @()
Get-Datastore *Temp |%{
$removeThese += $_.ExtensionData.info.Vmfs.Extent[0].diskName
}

#Manually delete file systems, then run the following:

Get-VMHost esxi0* | %{
$vmHost = $_
$vmHost.name
$removeThese | %{
$esxcli = Get-EsxCli -VMHost $vmHost
$esxcli.storage.core.device.set(“$_”,$null,$null,”off”)
}#end device loop
}#end host loop
[/cc]
Note: This code only works with ESXi 5.0 or later. I recently tested on an ESXi 4.1 host and this code does not work. (In 4.1 Get-EsxCli doesn’t have a storage.core namespace)

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.