UberGeek1
Contributor
Contributor

Remove Datastore Non-Distructively

I have an interesting situation where I have several datastores that are mounted to hosts in different datacenters, but only Datacenter-A is actually running the VMs, Datacenter-B is not executing anything.

    I've tried using the ConfigManager.StorageSystem.DetachScsiLun to no avail.  What I need to do is remove the LUNs from Datacenter-B without destroying any VM that is running on Datacenter-A.  Maybe there's some API call I'm missing, but I can't seem to figure it out using PowerCLI.

Sincerely, Jody L. Whitlock
Tags (1)
0 Kudos
8 Replies
LucD
Leadership
Leadership

You would need to do an unmount and then a detach for the datastore.

You can use my function from Test If The Datastore Can Be Unmounted to check if it can be unmounted.

And then you can use Alan's functions from Datastore Mount/Unmount Detach/Attach functions


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

0 Kudos
UberGeek1
Contributor
Contributor

I was thinking along those lines and shamelessly used your code to perform the detach on every host in the Datacenter-B, but even with no errors returned the datastore did not detach.  That's where my confusion is coming in, so I will review my code but here's what I have.  I added in an optional Datacenter parameter so that I can ensure only hosts in a perticular datacenter are detached instead of everything, that could be interesting to do in production.

function Remove-SANDatastore

{

   [CmdletBinding()]

   param

   (

      [Param(ValueFromPipeline = $true, Mandatory = $true)]

      [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.Datastore]$Datastore,

      [Param(Mandatory = $false)]

      [string]$Datacenter

   )

  foreach($ds in $Datastore)

  {

  $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname

  if($ds.ExtensionData.Host)

  {

  $attachedHosts = $ds.ExtensionData.host

  foreach($VMHost in $attachedHosts)

  {

                                $dataCenterObj = get-vmhost -Id ("HostSystem-$($VMHost.key.value)") | Get-Datacenter

  [bool]$Proceed = $true

  if($Datacenter)

  {

  if($dataCenterObj.Name -ne $Datacenter)

  {

  $Proceed = $false

  }

  }

  if($Proceed)

  {

  $hostView = Get-View $vmHost.Key

  $StorageSys = Get-View $hostView.ConfigManager.StorageSystem

  $devices = $StorageSys.StorageDeviceInfo.ScsiLun

  foreach($device in $devices)

  {

  if($device.canonicalName -eq $hostviewDSDiskName)

  {

  $LunUUID = $Device.Uuid

                                                        Write-Verbose "Detaching LUN $($Device.canonicalName) from host $($hostview.Name)..."

  $StorageSys.DetachScsiLun($LunUUID);

  }

  }

  }

  }

  }

  }

}

Sincerely, Jody L. Whitlock
0 Kudos
LucD
Leadership
Leadership

You did rescan the HBA and refresh the storage after the detach?


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

0 Kudos
UberGeek1
Contributor
Contributor

Yes sir, even rebooted several of the hosts.  I'm a little baffled to be honest.
Sincerely, Jody L. Whitlock
0 Kudos
LucD
Leadership
Leadership

So unmount works, but detach doesn't seem to do anything?
What state is the datastore, and the LUN behind the datastore, in after that?
Could that datastore still be in use for something on one of the ESXi nodes on which is visible?
Did you run my function on all the ESXi nodes that have the datastore?


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

0 Kudos
UberGeek1
Contributor
Contributor

Ok, so now I'm getting something that I can work with, an error.  [Exception calling "DetachScsiLun" with "1" argument(s): "The operation is not allowed in the current state."]  So, now I can at least look at logs, hopefully that is.
Sincerely, Jody L. Whitlock
0 Kudos
LucD
Leadership
Leadership

I had similar before, it came down to a lock on a VM's file.

KB2110152 might help investigating that path.


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

0 Kudos
UberGeek1
Contributor
Contributor

Never even thought of that, I will ahve to give that a try in the morning.
Sincerely, Jody L. Whitlock
0 Kudos