VMware

This Question is Answered

2 "helpful" answers available (6 pts)
1 2 Previous Next 15 Replies Last post: Jul 6, 2009 8:34 AM by Trebormint  

SCSI Canonicalname and new-harddisk posted: Jul 1, 2009 3:40 AM

Click to view Trebormint's profile Enthusiast 34 posts since
Apr 18, 2007

When I present a LUN to an ESX host, it is seen on 2 different SCSI targets, due to the dual controller ports in our SAN. As far as I can tell, ESX assigns one as the primary and sets this as the canonicalname. If I try to map the LUN to a guest, using the new-harddisk cmdlet, I need to know this canonical name. Problem is, I can't work out how to find what the canonical name is for a particular LUN.

The only possible solution I've come up with so far is to check for the existence of the LUN using all both possible canonical names and then mapping on the correct one. Does anyone know if it is possible to find out the correct canonical name from just the LUN?

Thanks

Re: SCSI Canonicalname and new-harddisk

1. Jul 1, 2009 3:50 AM in response to: Trebormint
Click to view fyi's profile Novice 12 posts since
Aug 23, 2007

how aboout the determining it from esxcfg-mpath -l ?


Re: SCSI Canonicalname and new-harddisk

3. Jul 1, 2009 4:47 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
Or have a look at the script that emulates the esxcfg-mpath command
See scripting esxcfg-mpath -l results

Re: SCSI Canonicalname and new-harddisk

5. Jul 1, 2009 9:54 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
I'm not sure I understand the question completely.

The following screenshot shows a LUN in my test environment.
It is reachable over 2 HBAs and each HBA has 2 paths.
As you can see vmhba2:2:0 is the preferred path.

LUN-preferred.png

The logic to determine the preferred path is present in the script.
Do you need help to extract the lines that do this ?

Re: SCSI Canonicalname and new-harddisk

7. Jul 3, 2009 6:22 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
I think I finally got what you want to do.
But I'm afraid you can't find the Canonical name based only on the LUN id.
The LUNid is not a unique identifier, you will at least have to give the Target id and the name of the HBA.

Then this should return the DeviceName you can use in in the New-HardDisk cmdlet.
$esxName = <ESX-hostname>
$vmName = <VM-name>
$hbaName = <HBA-devicename>		# Can be any of the HBAs that connect to the LUN
$target = <target-number>
$lunId = <LUN-number>

$esx = Get-VMHost $esxName | Get-View
foreach($hba in $esx.Config.StorageDevice.HostBusAdapter){
	if($hba.Device -eq $hbaName){
		$hbaKey = $hba.Key
		continue
	}
}
foreach($adapter in $esx.Config.StorageDevice.ScsiTopology.Adapter){
	if($adapter.Adapter -eq $hbaKey){
		foreach($tgt in $adapter.Target){
			if($tgt.Target -eq $target){
				foreach($lun in $tgt.Lun){
					if($lun.Lun -eq $lunId){
						$lunKey = $lun.Key
						continue
					}
				}
				continue
			}
		}
		continue
	}
}
foreach($lun in $esx.Config.StorageDevice.ScsiLun){
	if($lun.Uuid -eq $lunKey){
		$path = $lun.DevicePath
		continue
	}
}

# Create raw disk
Get-VM $vmName | New-HardDisk -DiskType RawVirtual -DeviceName $path

Re: SCSI Canonicalname and new-harddisk

9. Jul 3, 2009 8:23 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
The critical point here is how you can identify the LUN you want.
The LUN ID, as it is shown in the VIC, is not a unique identifier.

Do you have for example the LUN Uuid ?
That would be a unique identifier (I think).

Re: SCSI Canonicalname and new-harddisk

12. Jul 3, 2009 11:21 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
The error mechanism and the trap mechanism with some of the PowerCLI cmdlets is quite complex.
See Yavor's explanation in this thread How to catch VimExceptions properly?.
And this shows another example of trapping errors Trap and Powershell with VI Toolkit.

To come back to your original question and to avoid all terminology confusion, what exactly do you mean with 'device' and 'lun' ?
Is 'device' the HBA name ? Something like vmhba2 or vmhba3 ?
And is 'lun' the integer number that the VIC displays as the LUN ID ?
Perhaps a screenshot with some annotations would make things clearer.

And let's recap what exactly happens in case of a DR.
You have a VC and 1 or more ESX servers running in your DR center.
You define 1 or more luns on the DR storage and they are made visible to the ESX server(s) ?
You then do a rescan to make these new luns visible to the ESX server(s) ?

Re: SCSI Canonicalname and new-harddisk

14. Jul 6, 2009 3:52 AM in response to: Trebormint
Click to view LucD's profile Champion 2,431 posts since
Oct 31, 2005
If you are sure that the same LUNid doesn't appear for different LUNs on different targets in the same HBA than the earlier script can be adapted.

$esxName = <ESX-hostname>
$vmName = <VM-name>
$hbaName = <hba-name>             # Can be any of the HBAs that connect to the LUN
$lunId = <Lunid>

$esx = Get-VMHost $esxName | Get-View
foreach($hba in $esx.Config.StorageDevice.HostBusAdapter){
	if($hba.Device -eq $hbaName){
		$hbaKey = $hba.Key
		continue
	}
}
foreach($adapter in $esx.Config.StorageDevice.ScsiTopology.Adapter){
	if($adapter.Adapter -eq $hbaKey){
		foreach($tgt in $adapter.Target){
			foreach($lun in $tgt.Lun){
				if($lun.Lun -eq $lunId){
					$lunKey = $lun.Key
					continue
				}
			}
			continue
		}
		continue
	}
}
foreach($lun in $esx.Config.StorageDevice.ScsiLun){
	if($lun.Uuid -eq $lunKey){
		$path = $lun.DevicePath
		continue
	}
}

# Create raw disk
Get-VM $vmName | New-HardDisk -DiskType RawVirtual -DeviceName $path

Fyi different LUNs with the same LUNid, albeit under different targets, is possible.
HBA-LUN.png

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities