Thursday, March 20, 2014

Associating RDMs to the corresponding VMs

In order to decommission an older SAN, I had to migrate all the LUNs off. The process to migrate LUNs associated with datastores is easy - create new datastores on the new SAN, attach to the same ESXi hosts and then svMotion all the VMDKs over.

The other scenario involved LUNs that were mapped as RDMs to the VMs. While the process to move the RDMs over were just as straight forward (shutdown VM, detach an remove RDM LUN from all hosts, migrate RDMs onto new SAN, reconnect RDM into the same VM in the same SCSI bus location), I found it difficult to identify which VM the RDM was connected to given the device identifier. Fortunately, the following PowerCLI line of code will find the VM that a specific RDM is connected to given the device ID:

Get-Cluster "<cluster-name>" | Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | where { $_.ScsiCanonicalName -eq "<device id>"} | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName | fl


Wednesday, March 5, 2014

Detaching RDMs from all ESXi Hosts

After removing the RDM disks from the VMs, the LUN must be detached from every host in order to prevent an All paths down condition (APD). This is a tedious task to do manually within the vSphere client, so the following powerCLI script can be used to automatically remove it from all hosts in the cluster.

function Detach-Disk{
    param(
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
        [string]$CanonicalName    )
   
    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = (Get-ScsiLun -VmHost $VMHost | where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid
   
    $storSys.DetachScsiLun($lunUuid)
}

Connect-VIServer -Server "vcenter01"


foreach($esx in (Get-Cluster | Get-VMHost)){
  Get-ScsiLun -VMhost $esx -LunType disk | Where {$_.CanonicalName -like "<naa id>"} | %{
    Detach-Disk -VMHost $esx -CanonicalName $_.CanonicalName
  }
}

Disconnect-VIServer -Server "vcenter01" -Force -Confirm false