VMware Cloud Community
bug_juice
Contributor
Contributor
Jump to solution

How do you add an existing rdm to a VM using New-HardDisk?

Hi,

   I'm running powercli 5.0.1 build 581491 and i'm trying to write a powercli script to:

1) create an RDM

2) assign that RDM to VM1

3) assign the same RDM to VM2

I'm able to create the RDM and assign it to VM1, but I'm unable to use "New-HardDisk" to assign it to VM2.  Using the following command i'm able to create the RDM and assign it to VM1.

     New-HardDisk -VM VM1 -DiskType RawVirtual -DeviceName /vmfs/devices/disks/naa.60000970000192603729533030353531

While trying to assign it to VM2, i'm doing the following:

     New-HardDisk -VM VM2 -DiskPath "[san-datastore:1] vm1/vm1_1.vmdk"

And i get the following error:

     New-HardDisk : 6/6/2012 2:13:19 PM    New-HardDisk        Incompatible device backing specified for device '0'.

What i realized is that if i create a regular vmdk file, then use the New-HardDisk using the "-DiskPath" option, then the command works ok.  So my question is, what's the command to add an existing RDM to a VM?

Thanks in advance.

49 Replies
rheuvink
Contributor
Contributor
Jump to solution

We are currently adding multiple hosts with a new SCSI controller and multiple RDMs.  I used LucD base script in this topic for adding the RDMs.

The first part is working: adding scsi controller and map the first RDM to the hosts. The second part "loop to add multiple RDMs" is working for the first LUNID (L31) but not for the next IDs.

I receive error:

New-HardDisk   The operation for the entity "server1" failed with the following message: "Invalid configuration for device '0'."   

At line:109 char:8

+     $hd2= New-HardDisk -VM $vm1 -Controller $ctrl1 -Devicename $devicename2 -DiskTy ...

+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidDeviceSpec

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

Property 'Key' cannot be found on this object; make sure it exists and is settable.

At line:29 char:2

+     $spec.deviceChange[1].device.Key = -102

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyNotFound

Property 'ControllerKey' cannot be found on this object; make sure it exists and is settable.

At line:30 char:2

+     $spec.deviceChange[1].device.ControllerKey = -101

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It looks like the variables are kept locked so we created a function existingRDM and cleared the variables in the end but this did not help. Who can help me?

The script:

# set variables ##################
$VMHost = "host1"
$server1 = "server1"
$server2 = "server2"
$server3 = "server3"
$SCSIid1 = "L30"
$SCSIidAll = @("L31","L32","L33","L34")

###################################
function existingRDM($ctrl1){
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device += $ctrl1.ExtensionData
$spec.deviceChange[0].device.Key = -101
$spec.deviceChange[0].operation = "add"

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[1].device += $hd2.ExtensionData
$spec.deviceChange[1].device.Key = -102
$spec.deviceChange[1].device.ControllerKey = -101
$spec.deviceChange[1].operation = "add"

$vm2.ExtensionData.ReconfigVM($spec)
$vm3.ExtensionData.ReconfigVM($spec)
Clear-Variable -Name spec
}

$vm1 = Get-VM -Name $server1
$vm2 = Get-VM -Name $server2
$vm3 = Get-VM -Name $server3

# add first RDM + Controller
$diskinfo = Get-ScsiLun -VMHost $VMhost -LunType disk |
Select RuntimeName,ConsoleDeviceName,CapacityGB |
Sort-Object -Property {$_.RuntimeName.Split(‘:’)[0],
[int]($_.RuntimeName.Split(‘:’)[1].TrimStart(‘C’))},
{[int]($_.RuntimeName.Split(‘:’)[2].TrimStart(‘T’))},
{[int]($_.RuntimeName.Split(‘:’)[3].TrimStart(‘L’))}

foreach($disk in $diskinfo) {
 

# search lunID
  $lun_id = ($disk.RuntimeName.split(":"))[3]
if ($lun_id -match $SCSIid1){
$devicename = $disk.ConsoleDeviceName

# Map RDM to first |VM
$hd1= New-HardDisk -VM $vm1 -Devicename $devicename -DiskType RawPhysical
$ctrl1 = New-ScsiController -harddisk $hd1 -BusSharingMode Physical -Type VirtualLsiLogicSAS

# Map RDM to second and third VM
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device += $ctrl1.ExtensionData
$spec.deviceChange[0].device.Key = -101
$spec.deviceChange[0].operation = "add"

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[1].device += $hd1.ExtensionData
$spec.deviceChange[1].device.Key = -102
$spec.deviceChange[1].device.ControllerKey = -101
$spec.deviceChange[1].operation = "add"

$vm2.ExtensionData.ReconfigVM($spec)
$vm3.ExtensionData.ReconfigVM($spec)
}
}

#loop to add multiple RDMs

Foreach ($i in $SCSIidAll){

foreach($disk in $diskinfo) {

# search lunID

  $lun_id = ($disk.RuntimeName.split(":"))[3]

 

if ($lun_id -match $i){

$devicename2 = $disk.ConsoleDeviceName

# Map another RDM to first |VM

$hd2= New-HardDisk -VM $vm1 -Controller $ctrl1 -Devicename $devicename2 -DiskType RawPhysical

existingRDM $ctrl1

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In your function you seem to be adding a new controller for each RDM, is that the intention ?


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

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

No, that's not the intension.

We have iscsi controller 0 with OS disk on VMDK

We want to add iscsi controller 1 with physical bus sharing with multiple RDMs on scsi  1:0, 1:1, 1:2 enz.

The VMs are member of a SQL (MSCS)

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

I cannot simply add an extra RDM to the first VM with "New-HardDisk -VM $vm1 -Controller $ctrl1 -Devicename $devicename2 -DiskType RawPhysical"

When I query this to the second VM "New-HardDisk -VM $vm2 -Controller $ctrl1 -Devicename $devicename2 -DiskType RawPhysical" I get a new RDM, but not an existing RDM from VM1

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

@LucD could you help me out?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try like this ?

This works for me in vSphere 5.5 and with PowerCLI 5.8 R1

$esxName = 'MyEsx'

$vmNames = 'VM1','VM2','VM3'

$lunID = '30','31','32'

$lunTab = @{}

Get-VMHost -Name $esxName | Get-ScsiLun -LunType disk |

  where{$lunId -contains ($_.RuntimeName.Split(':')[3].TrimStart('L'))} | %{

  $lunTab.Add($_.RuntimeName.Split(':')[3].TrimStart('L'),$_.ConsoleDeviceName) 

}

foreach($vm in (Get-VM -Name $vmNames)){

  $hd = foreach($lun in $lunID){

    New-HardDisk -VM $vm -DiskType RawPhysical -DeviceName $lunTab[$lun]

  }

  $ctrl = New-ScsiController -HardDisk $hd -BusSharingMode Physical -Type VirtualLsiLogicSAS

}


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

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

Thanks LucD this works! I have one warning:

WARNING: Passing multiple values to the 'HardDisk' parameter of 'New-ScsiController' cmdlet is obsolete. The parameter will accept only a single object in future.

What I do not understand, normally you choose an Existing Disk for the next VMs (VM2, VM3 enz) and need to browse to the Datastore that the first VM is on and find the RDM VMDK file and select this.

Now all the VMs have own vmdks with the VM folder, and not the vmdk file from VM1? Is this correct?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The warning is just that, a warning. We can ignore it for now.

You observer correctly that each VM now has its own VMDK for the RDM.

But this VMDK is just a header file to the RDM.

I'm not 100% sure if this will be sufficient to set up a MSCS, but the quorum signature is on the RDM , not on the VMDK as I see it.

KB1037959 does not clarify sufficiently if the SCSI controller based sharing is sufficient imho.


The sharing was for a MSCS cluster, correct ?

Does the MSCS cluster install on this setup, is it functioning correctly ?


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

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

It is indeed for a MSCS cluster. The VMs power on succesfull but I can list the disk only on 1 server.

The other servers have an error in the File and Storage services (W2012) 'error occurred during storage enumeration'

Error occurred during enumeration of disks: Acces to a CIM resource was not available to the client

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you run the MSCS Cluster Validation report ?

Ok, so it looks we will have to fall back to the old method.

Let me prepare that script


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with this one.

$esxName = 'MyEsx'

$vmNames = 'VM1','VM2','VM3'

$lunID = '30','31','32'

$lunTab = @{}

Get-VMHost -Name $esxName | Get-ScsiLun -LunType disk |

  where{$lunId -contains ($_.RuntimeName.Split(':')[3].TrimStart('L'))} | %{

  $lunTab.Add($_.RuntimeName.Split(':')[3].TrimStart('L'),$_.ConsoleDeviceName) 

}

$hd = @()

$first = $true

foreach($vm in (Get-VM -Name $vmNames)){

  if($first){

    foreach($lun in $lunID){

      if($first){

        $hd += New-HardDisk -VM $vm -DiskType RawPhysical -DeviceName $lunTab[$lun]

      }

    }

    $ctrl = New-ScsiController -HardDisk $hd -BusSharingMode Physical -Type VirtualLsiLogicSAS

    $first = $false

  }

  else{

     $spec = New-Object VMware.Vim.VirtualMachineConfigSpec 

     $spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec  

     $spec.deviceChange[0].device += $ctrl.ExtensionData  

     $spec.deviceChange[0].device.Key = -101 

     $spec.deviceChange[0].operation = 'add'

     $i = -102

     $hd.GetEnumerator() | %{

       $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

       $dev.device += $_.ExtensionData 

       $dev.device.Key = $i 

       $dev.device.ControllerKey = -101 

       $dev.operation = "add"

       $spec.DeviceChange += $dev

       $i--

     }

     $vm.ExtensionData.ReconfigVM($spec)

  }

}


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

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution


Thanks for the support LucD!

The first server is created succesfull.

The seconds VM connects all the RDMs correct, but the SCSI ids (1:0, 1:1, enz) are messed up. for MSCS the scsi ids needs to be the same for all the RDMs.

The third VM is not connecting RDMs at all with error 'incompatible device backing for device '2'

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I forgot to create the SCSI controller on VM2, VM3...

That is probably the cause of the problem, let me see if I can fix that.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I checked that last script again in my lab, seems it seems to be working for me without an issue.

All three test VMs, got a new controller and all disks got the same SCSI Id on each VM.

All VMs (Windows 2008 R2 OS) had 1 controller with the System disk attached (0:0) before I ran the script.

After the script the RDMs got SCSI ids 1:1 and 1:2 ( I only had 2 LUNs free in my lab) on the new controller.

I'm not sure why 1:0 was not used, but I have seen this behavior before.

Could you perhaps give some more info on the config of the VMs before you run the script ?

And what actually was there after the script ?

Feel free to email me some screenshots via my blog


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

0 Kudos
rheuvink
Contributor
Contributor
Jump to solution

I've send you an e-mail, thanks!

0 Kudos
wamami
Contributor
Contributor
Jump to solution

Hi Luc,

How can I modify this script to use LUN naa. and not LunID?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change the construction of the $lunTab something like this

$lunNAA = 'naa.60050768011083af1800000000000071','naa.60050768011083af1800000000000072'

$lunTab = @{}

Get-VMHost -Name $esxName | Get-ScsiLun -LunType disk |

  where{$lunAAA -contains $_.CanonicalName}

  $lunTab.Add($_.CanonicalName,$_.ConsoleDeviceName)

}


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

0 Kudos
wamami
Contributor
Contributor
Jump to solution

Thanks

I am getting the errors below

Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: '1'  Key being added: '1'"

At C:\_LOCALdata\Powershell\AddExistingRdm.ps1:10 char:3

+  $lunTab.Add($_.RuntimeName.Split(':')[3].TrimStart('L'),$_.ConsoleDeviceName)

+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentException

New-HardDisk : 6/12/2015 3:27:20 PM    New-HardDisk        The operation for the entity "VNN3249" failed with the following message: "Failed to add disk scsi0:2.". Failed to lock the file.

snapshot disks it depends on. Failed to power on scsi0:2. Failed to add disk scsi0:2. this should be adding to a new scsi controller (1:0)

At C:\_LOCALdata\Powershell\AddExistingRdm.ps1:19 char:16

+        $hd += New-HardDisk -VM $vm -DiskType RawPhysical -DeviceName $lunTab[$l ...

+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], GenericVmConfigFault

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The message seems to indicate that the LUN is already used by another VM

Can you check.

My LUN report – datastore, RDM and node visibility script might help.


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

0 Kudos
wamami
Contributor
Contributor
Jump to solution

That's true, I am trying to add shared rdm for MS clustering on the second Node. I added all the rdm's  (20 rdms) on the first Node manually.

I am looking for a script that will add these rdms on the second, third and fourth Nodes, as we are migrating from one array to another.

0 Kudos