Detaching Devices from a Cluster

By | August 14, 2017

When working with block storage we occasionally need to remove a storage device from the hosts it is connected to. When using the Web Client we can detach devices on a per-host basis, but there is nothing to detach from all its attached hosts.

What we do have is a helpful KB article on how to remove/detach a datastore – https://kb.vmware.com/kb/2004605 and a PowerCLI file that contains some functions. However the functions revolve around managing actions on datastores as opposed to devices that are connected and could be used for RDMs.

Most LUNs are masked to all the hosts in a cluster so that, whether the device is an RDM or has a VMFS datastore on it, VMs can run on any host in the cluster. To deal with this use case, I use a script that will detach a device from every host in a cluster. If the device does not exist on  a host then an error is displayed and the script moves on to the next device/host.

Before we can detach a device we need to ensure the following:

  • It is not used for a datastore.
  • It is not in use as a RDM.
  • The perennially reserved flag is not set for the device on the host.

The last point comes from the KB article above – if the flag is set (and it should be for any RDMs) then it could prevent the device from being unmounted successfully. So although clearing the flag is not required, clearing it ensures the best chance of success.

So, our script will start off with the following – we will accept a cluster to unmount/detach the device(s) from, an array of device identifiers, and a switch that will allow us to do checks without actually detaching anything.

For the sake of time, we will make the assumption that all devices are masked to all hosts in the cluster – so when checking what the device is used for we will only check on the first host in the cluster. The following code will get the VMs in the cluster (for checking RDM usage in the next step) and the host that we will use for checking.

Finally we will use the Get-ESXCLI cmdlet to check if the perennially reserved flag is set.

When all our checks are complete, we can loop through the hosts in the cluster and attempt to detach the LUN.

The entire script is available from my github here. As always, feel free to fork and make pull requests if you find a mistake.