Tuesday, May 31, 2016

Finding Linux UUID and Identifying the VMware Hard Drive X Shown in VMware.


If you've been frustrated about converting from SDA to SCSI:X:X or Hard Drive X in VMware and want to be certain that you are picking the correct device then this Powershell script should come in use.  I've read many other blogs that try and use scsi_id and it doesn't always correlate to the correct Hard Drive # or SCSI:x:x that you can see in VMware.



# --- ps code start ---  #

if(-not (Get-PSSnapin VMware.VimAutomation.Core))
{
   Add-PSSnapin "VMware.VimAutomation.Core" | Out-Null
}

function getDiskUUIDs ([string]$vmname) {

 $vm=get-vm -name $vmname
 $vm.name 
 $vdm = get-view -id (get-view serviceinstance).content.virtualdiskmanager
 ForEach ($HardDisk in ($vm | Get-HardDisk | Sort-Object -Property Name)) {

Write-Host "Hard Disk:  " $HardDisk
    
    $filepath=$HardDisk.FileName
    Write-Host "File Path : " $filepath

    $tmp1=$vdm.queryvirtualdiskuuid($filePath, $dc.id)
$tmp2=$tmp1 -replace '\s+', ''
    $UUID=$tmp2 -replace '-', ''

    Write-Host ""
    Write-Host "oldUUID : " $tmp1
    Write-Host "UUID : " $UUID
 }
}

$vc="yourvc.yourdomain.local"
$vm="yourvmname"

Write-Host "vcenter: " $vc
Write-Host "VM:      " $vm
Connect-VIServer "$vc"

$dc=get-datacenter
write-Host "Datacenter: " $dc
write-Host "DC Name:  "   $dc.Name
getDiskUUIDs $vm


# --- ps code end ---  #


In Red Hat Linux on the VM you want to identify which devices you would want to do these steps :

cd /dev/disk

if [ RH5 ]  :   

ls -al by-id
if [ RH6 ]  :   ls -al by-id | grep scsi

You should now be able to correlate the UUID to what is shown in the VM under /dev/disk and the corresponding current Hard Drive #








                 




1 comment:

Unknown said...

Doesn't work because of this line:

$tmp1=$vdm.queryvirtualdiskuuid($filePath, $dc.id)

The variable "$dc" has not been defined yet, so "$dc.id" will always be null.