bug_juice
Contributor
Contributor

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

Jump to solution

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.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

No, there seems to be indeed something wrong with the New-Harddisk cmdlet when adding shared RDMs.

I have the following, which assigns the shared RDM to both VMs.

It's more or less the same what you saw in Onyx but it adds the RDM shared to both VMs.

$vm1 = Get-VM -Name VM1 
$vm2 = Get-VM -Name VM2
$DeviceName
= "/vmfs/devices/disks/naa.600507680180701ed00000000000124"

$hd1 = New-HardDisk -VM $vm1 -DeviceName $DeviceName -DiskType RawVirtual
$ctrl1
= New-ScsiController -HardDisk $hd -BusSharingMode Virtual -Type VirtualLsiLogicSAS
$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)


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

View solution in original post

0 Kudos
49 Replies
LucD
Leadership
Leadership

I suspect you will have to configure the SCSI controller in sharing mode (physical/virtual)


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

0 Kudos
bug_juice
Contributor
Contributor

Thanks for the reply. On both VM1 and VM2, I already added some RDMs via the GUI and my "SCSI Controller 1" is setup for SCSI Bus Sharing 'Virtual'.  I also just tried:

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

But i still get the same error "Incompatible device backing specified for device '0'."

0 Kudos
LucD
Leadership
Leadership

Can you try adding the RDM with it's DeviceName.

See Adding a RAW disk to existing VM for an example.


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

0 Kudos
bug_juice
Contributor
Contributor

I did that on VM1 already, but I can't run that on VM2 from what I understand because it creates another VMDK file that's pointed back to the same RDM that's already been created by VM1.  While the command does work on VM2, if i attempt to power up VM2 after VM1, VM2 complains about the newly created VMDK file created in VM2's datastore folder being locked.

I'm simply trying to create a MSCS between VM1 and VM2 with one RDM lun.  And if i use the GUI, all i do is create and add the RDM to VM1 and then on VM2, I add an existing RDM and it all works fine (setting the RDM to a different controller, setting the SCSI bus sharing to virtual, and setting the Compatibility mode to virtual, etc).  There's one VMDK file that sits in the VM1 datastore folder and both VM1 and VM2 are pointed back to it and it works fine.  But this is only if i use the GUI to provision it.

From some examples I've seen on google, New-HardDisk -VM VM2 -DiskPath "[san-datastore:1] vm1/vm1_1.vmdk" should work, but from what i've tested so far, it won't work if the VMDK is pointed back to and RDM.  It only seems to work on regular vmdk files.

0 Kudos
bug_juice
Contributor
Contributor

I was able to add an existing RDM created by "New-HardDisk" for VM1 and then add it to VM2 using "project onyx".

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
# $spec.changeVersion = ""
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (2)
$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "add"
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.key = -100
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo
$spec.deviceChange[0].device.backing.fileName = "[san-datastore:1] vm1/vm1_1.vmdk"
$spec.deviceChange[0].device.backing.compatibilityMode = ""
$spec.deviceChange[0].device.backing.diskMode = "independent_persistent"
$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
$spec.deviceChange[0].device.connectable.startConnected = $true
$spec.deviceChange[0].device.connectable.allowGuestControl = $false
$spec.deviceChange[0].device.connectable.connected = $true
$spec.deviceChange[0].device.controllerKey = -101
$spec.deviceChange[0].device.unitNumber = 0
$spec.deviceChange[0].device.capacityInKB = 104857920
$spec.deviceChange[1] = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[1].operation = "add"
$spec.deviceChange[1].device = New-Object VMware.Vim.VirtualLsiLogicSASController
$spec.deviceChange[1].device.key = -101
$spec.deviceChange[1].device.controllerKey = 100
$spec.deviceChange[1].device.busNumber = 1
$spec.deviceChange[1].device.sharedBus = "virtualSharing"

$_this = Get-View -Id 'VirtualMachine-vm-443'
$_this.ReconfigVM_Task($spec)

So my question is, "Does the New-HardDisk cmdlet support the above code?"

0 Kudos
LucD
Leadership
Leadership

No, there seems to be indeed something wrong with the New-Harddisk cmdlet when adding shared RDMs.

I have the following, which assigns the shared RDM to both VMs.

It's more or less the same what you saw in Onyx but it adds the RDM shared to both VMs.

$vm1 = Get-VM -Name VM1 
$vm2 = Get-VM -Name VM2
$DeviceName
= "/vmfs/devices/disks/naa.600507680180701ed00000000000124"

$hd1 = New-HardDisk -VM $vm1 -DeviceName $DeviceName -DiskType RawVirtual
$ctrl1
= New-ScsiController -HardDisk $hd -BusSharingMode Virtual -Type VirtualLsiLogicSAS
$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)


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

0 Kudos
bug_juice
Contributor
Contributor

Thanks for confirming and thanks for the snipit of code.

0 Kudos
OsburnM
Hot Shot
Hot Shot
$vm1 = Get-VM -Name VM1 
$vm2 = Get-VM -Name VM2
$DeviceName
= "/vmfs/devices/disks/naa.600507680180701ed00000000000124"

$hd1 = New-HardDisk -VM $vm1 -DeviceName $DeviceName -DiskType RawVirtual
$ctrl1
= New-ScsiController -HardDisk $hd -BusSharingMode Virtual -Type VirtualLsiLogicSAS
$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)

Okay so I think PowerCLI may have met its match.

We are putting in a new Exchange infrastructure and our SAN guys are going to be giving us a ton of LUN IDs for specific VMs to get added as RDMs (they're doing some crazy flashcopy stuff in the backend).

What I'd like is to feed a csv into powercli that'll contain a line for each VM name its given LUN ID.

I'd like the script to add the RDM as physical compat mode, BUT I want it on a (as of yet) non-existant SCSI Controller 1.  (So I want all the RDMs on 1:x)

Here's what I've got to set it up-- This all works but I can't figure out how to get "SCSI controller 1" added first.

CSV Headers -
VM,LUNID


SCRIPT -


ForEach ($Server in Import-csv $RDMList) {

$VM = (Get-VM $Server.VM)
$VMHost = (Get-VMHost $VM.Host)
$DeviceName = ($VMHost | Get-ScsiLun | Where {$_.CanonicalName -match "naa.$Server.LUNID"}).ConsoleDeviceName

#It's this part of adding SCSI controller 1 that's getting me*

$Disk = New-HardDisk -VM $VM -DiskType RawPhysical -DeviceName $DeviceName -Controller "SCSI controller 1"

}

0 Kudos
LucD
Leadership
Leadership

Have a look at the New-ScsiController cmdlet, more specifically Example 1.


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

0 Kudos
OsburnM
Hot Shot
Hot Shot

Yeah, I tried that my idea was to query the VM for existings of "SCSI controller 1" and if it doesn't exist, do something like:

$TmpVMDisk = Get-VM NODCAPTSV1018 | New-HardDisk -CapacityKB 1024 | New-ScsiController -BusSharingMode NoSharing -Type VirtualLsiLogicSAS

And then simply remove the VMDK after the fact and proceed with the script; however, the above results in the script failing because it insist the VM be powered off.

0 Kudos
LucD
Leadership
Leadership

Is that also the case when you use the Controller parameter on the New-Harddisk cmdlet ?


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

0 Kudos
OsburnM
Hot Shot
Hot Shot

Negative.  When I specify -Controller with the new-harddisk as-is, it accepts it as long as the controller already exists.  if it doesn't exist it errors saying the controller doesn't exist.

0 Kudos
LucD
Leadership
Leadership

That should work when you use the object returned by the New-ScsiController cmdlet as a value for the Controller parameter.


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

0 Kudos
OsburnM
Hot Shot
Hot Shot

Not sure I follow?

0 Kudos
LucD
Leadership
Leadership

There are 2 ways of doing it;

1)

Create a new harddisk with $hd = New-Harddisk

Then do New-ScsiController -Harddisk $hd

2)

Create a new SCSI controller with $ctrl = New-ScsiController

And the do New-Harddisk -Controller $ctrl

Needless to say I only showed the required parameters, you'll probably have to add additional parameters.


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

0 Kudos
OsburnM
Hot Shot
Hot Shot

Hmmm.. Every option still results in error with "The VM must be in the following state: PoweredOff."

BTW, I'm running PowerCLI 5.0.1, on a v8 VM, from a 5.0.1 hosts with up-to-date tools.

0 Kudos
LucD
Leadership
Leadership

I think it is a limitation of the virtual hardware.

Apparently, in combination with the guest OS, it doesn't seem to support hot-adding a controller.

Btw does it work from the vSphere client ?

If yes, it could be that the checking in the cmdlet is too restrictive.


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

0 Kudos
OsburnM
Hot Shot
Hot Shot

Yeah, I probably should have mentioned that to begin with.

Using the VC Client, I can directly add a new RDM, specify physical mode, specify controller 1:0, all at once, and it will add scsi controller, then add the rdm to 1:0, and do it all at once with the VM powered on.

0 Kudos
LucD
Leadership
Leadership

Then it will work with the ReconfigVM_Task as well.

Have a look at my older script in Re: Adding VMDK's to VM's in new vSphere PowerCLI


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

0 Kudos