VMware {code} Community
jas123
Contributor
Contributor

SDK2.5, VS2008, C#: How to add an existing virtual disk to a VM?

Hi

I am using ReconfigVM_Task and the task completes successfully, but new disk is not added; not seen in VM config settings (nor in guest OS). The code segment I am using is attached below. I am able to do this manually in VI Client, so error must be in my program.

I could not find any C# sample code online.

Any help will be appreciated.

Thanks & Regards

Jas

var bckInfo = new VirtualDiskFlatVer2BackingInfo

{

diskMode = "persistent",

fileName = "[snap0]Win2k3_1/Win2k3_1.vmdk", //

split = false,

splitSpecified = true,

thinProvisioned = false,

thinProvisionedSpecified = true,

writeThrough = false,

writeThroughSpecified = true,

datastore = new ManagedObjectReference { Value = "datstore-80", type = "Datastore" }

};

var newDisk = new VirtualDisk

{

unitNumber = 1, //Hardcoded, but verified and indeed this number is 'free'

unitNumberSpecified = true,

controllerKey = ctrlKey, controllerKeySpecified = true,

key = 2001, //Verified and this key is unused

deviceInfo = new Description { label = "Hard Disk2 ", summary = "snapshot disk" },

backing = bckInfo,

};

var cfgSpec = new VirtualDeviceConfigSpec

{

operation = VirtualDeviceConfigSpecOperation.add,

fileOperationSpecified = false,

device = newDisk

};

var vmSpec = new VirtualMachineConfigSpec

{

deviceChange = new VirtualDeviceConfigSpec[] { cfgSpec },

};

var tsk = this.Service.ReconfigVM_Task(new ManagedObjectReference

{

type = "VirtualMachine",

Value = "vm-91"

}, vmSpec);

Reply
0 Kudos
5 Replies
admin
Immortal
Immortal

VirtualDeviceConfigSpecOperation must be specified and set to be "add", key must be specified as negative number and would be auto assigned to the disk. Please also refer KB article available at http://kb.vmware.com/kb/1003482 which might be useful for you.

Reply
0 Kudos
jas123
Contributor
Contributor

Hi

Thanks for the response. I set the key to -1 and it did not work. The sample you pointed to is for replacing an existing vmdk. I had also seen sample to create a new virtual disk. But so far not seen one to add an existing VMDK.

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

I know that the exceptions you get back are usually pretty vague (invalid config on dev x or some such), but you are not getting any exception so thats rather odd.

I know that for my code (which does work) for adding a new VirtualDisk (new file not existing vmdk so ymmv) has a couple differences which might help. Now I don't know how much is necessary, but all I know is it has worked for me.

For unitNumber I actually have a function that will give me the "next" in the array using a simple bubble sort.

For the key I multiply the unitNumber by -1 to get a unique number. I let VI give me the correct node.

Because this is an existing VMDK you may need to use some of the File methods to try and get the capacityKB as I am unsure if that matters (it shouldn't).

You also may want to turn on verbose logging at your Host or VC so you can eval what iscoming through.

Good luck

J

Reply
0 Kudos
jas123
Contributor
Contributor

Thanks.

I found out why it was failing: I forgot to add

OperationSpecified = true

in VirtualDeviceConfigSpec.

Thanks

jas

Reply
0 Kudos
jrackliffe
Hot Shot
Hot Shot

Ugghh... man I don't know how many times that had bitten me. Completely missed it even though I have seen that signature so many times. Forest for the trees I guess...

Well any ways glad you got your problem resolved and maybe both of us will recognize it next time and help the next poster.

J

Reply
0 Kudos