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
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
how aboout the determining it from esxcfg-mpath -l ?
Sorry, neglected to say that I'm doing this all with powershell. esxcg-mpath is a shell command isn't it?
Or have a look at the script that emulates the esxcfg-mpath command
See
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A great script (as ever), but I'm not sure it helps me.
If LUN 11 is presented from my SAN via to my ESX via 2 controllers, it appears under SCSI targets 0 and 1 as vmhba1:0:11 and vmhba1:1:11. One of these is the prefered path, but I don't know which it will be. If I want to use the new-harddisk I need to specify the correct target else it will error.
Thanks
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.
The logic to determine the preferred path is present in the script.
Do you need help to extract the lines that do this ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm probably missing something; apologies if I am.
In the Viclient I can see LUN 11 listed under both SCSI target 0 and 1 with a path of vmhba1:0:11 and vmhba:1:1:11 respectively. The Canonicalpath is vmhba:1:0:11, making SCSI target 0 the prefered. If I map subsequent LUNs the prefered SCSI target varies between 0 and 1, with no obvious pattern that I can see.
In order to use the new-harddisk cmd I need to know this canoncialpath else I will get an error message when attempting to map the drive. I could try mapping using one scsi target and if this fails trying the other. It'd work, but it would be messy and I'd like to do it better if possible.
Config.StorageDevice.MultipathInfo.Lun and Config.StorageDevice.ScsiLun contains only the canonical paths on SCSI target 0. I was hoping to see an entry on SCSI target 1 as this would allow me to then read the canonicalpath. My script would take the LUN and query to see if SCSI target 0 was the canonical path or not, but I can't do this.
I guess I could generate all of the canonicalpaths and then work out which SCSI target a LUN should be using before I attempt to use the new-harddisk cmd. Or maybe I check to see if the entry in configure.storagedevice.scsilun is on target 0 or 1 and then issue the new-harddisk cmd.
Thanks
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, looking back I didn't explain that at all well.
From my perspective the LUN is the unique point as it is relates to the volume on the SAN, although I can see why it is not from the hosts pov.
Would it be possible to do the following:
Check to see if the volume/LUN is visible on SCSI target 0 (vmhba1:0:11), if it does run the new-harddisk cmdlet using vmhba1:0:11
If it does not exist check to see if it exists on SCSI target 1 (vmhba1:1:11), If it does run the new-harddisk cmdlet using the vmhba1:1:11
If it does not exist on either then it doesn't exist at all.
Thanks
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).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The problem is these volumes are created in the event of a disaster which I believe will makes the Uuid different each time.
Is there no way of working with the path? I know 2 of the 3 elements: device and lun, just not the SCSI target.
I've been experimenting with the whatif parameter on the end of the new-harddisk command. I was expecting $? to come back with false if the command would fail and true if it worked. If it failed, I would attempt the new-harddisk command again using the other value for the SCSI target.
Unfortuately the whatif command, well $?, returned true when it should have failed, i.e. I'd specific the wrong SCSI target. Curiously it returned false if I specified a completely wrong device.
Perhaps I'll just map the volumes using one controller card and not worry about the backup. This is the DR site after all..
The error mechanism and the trap mechanism with some of the PowerCLI cmdlets is quite complex.
See Yavor's explanation in this thread .
And this shows another example of trapping errors .
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) ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I'm refering to the device as the HBA, which in this case is vmhba1, and the LUN is the integer displayed as the LUN ID, 11 in this case. The only variable is the SCSI target:
The SAN volumes are replicated at SAN level to the DR SAN from the primary. In the event of a disaster, the replication is broken and the replica volume mapped to the DR ESX server. Actually, the replica is checked pointed at predetermined points in time and it is from one of these checkspoints the volume is created.
I'll take a look the error trapping, but it is a pity the simple what-if parameter doesn't work.
Many thanks
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.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I can see how the same LUNid can appear for different LUNs, but it definitely doesn't in our case. I'm struggling to see why it would be setup like this elsewhere, but I am only familiar with our setup.
Thanks for the script this should be perfect for what I need to do.
Once again, many thanks for your valuable and generous assistance.