VMware {code} Community
ISYS2
Enthusiast
Enthusiast
Jump to solution

Add disks to VM using .net

Apologies if this has been dealt with before but I couldn't find it anywhere. I am having trouble understanding how you define and add disks to a VM using the CreateVM_Task.

I have read the Reference Guide and think I need to create a New Virtual Disk and add it using the extraConfig option of the CreateVM_Task.

Any help or examples (C# or VB.NET) would be greatly appreciated and earn the sender a place on my new Xmas card list. Smiley Happy

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
tos2k
Expert
Expert
Jump to solution

Hi!

This is what I do:

> VirtualDeviceConfigSpec[] deviceSpecAdd = new VirtualDeviceConfigSpec[counter];

> foreach (Disk dk in newDisk)

> {

> foreach (String s in dk.DiskFiles)

> {

> VirtualDisk diskRef = new VirtualDisk();

> diskRef.key = dk.Key;

> diskRef.controllerKey = dk.Controller.Key;

> diskRef.controllerKeySpecified = true;

> diskRef.unitNumber = dk.UnitNumber;

> diskRef.unitNumberSpecified = true;

> Description devInfo = new Description();

> devInfo.label = dk.Label;

> devInfo.summary = dk.Summary;

> diskRef.deviceInfo = devInfo;

>

> VirtualDeviceFileBackingInfo backing = (VirtualDeviceFileBackingInfo)dk.Backing;

> backing.datastore = datastoreRef;

> backing.fileName = s;

> diskRef.backing = backing;

>

> deviceSpecAdd[counter] = new VirtualDeviceConfigSpec();

> deviceSpecAdd[counter].device = diskRef;

> deviceSpecAdd[counter].operation = VirtualDeviceConfigSpecOperation.add;

> deviceSpecAdd[counter].operationSpecified = true;

> logInfo += s + " " + dk.Controller.Key + " " + dk.UnitNumber + " " + dk.Label + " " + dk.Summary + "\r\n";

> counter++;

> }

> }

> VirtualMachineConfigSpec newSpec2 = new VirtualMachineConfigSpec();

> newSpec2.deviceChange = deviceSpecAdd;

>

> ManagedObjectReference reconfRefAddDisks = null;

> try

> {

> reconfRefAddDisks = service.ReconfigVMTask(vmRef, newSpec2);

> }

>...

HTH, Tos2k

View solution in original post

Reply
0 Kudos
5 Replies
tos2k
Expert
Expert
Jump to solution

Hi!

This is what I do:

> VirtualDeviceConfigSpec[] deviceSpecAdd = new VirtualDeviceConfigSpec[counter];

> foreach (Disk dk in newDisk)

> {

> foreach (String s in dk.DiskFiles)

> {

> VirtualDisk diskRef = new VirtualDisk();

> diskRef.key = dk.Key;

> diskRef.controllerKey = dk.Controller.Key;

> diskRef.controllerKeySpecified = true;

> diskRef.unitNumber = dk.UnitNumber;

> diskRef.unitNumberSpecified = true;

> Description devInfo = new Description();

> devInfo.label = dk.Label;

> devInfo.summary = dk.Summary;

> diskRef.deviceInfo = devInfo;

>

> VirtualDeviceFileBackingInfo backing = (VirtualDeviceFileBackingInfo)dk.Backing;

> backing.datastore = datastoreRef;

> backing.fileName = s;

> diskRef.backing = backing;

>

> deviceSpecAdd[counter] = new VirtualDeviceConfigSpec();

> deviceSpecAdd[counter].device = diskRef;

> deviceSpecAdd[counter].operation = VirtualDeviceConfigSpecOperation.add;

> deviceSpecAdd[counter].operationSpecified = true;

> logInfo += s + " " + dk.Controller.Key + " " + dk.UnitNumber + " " + dk.Label + " " + dk.Summary + "\r\n";

> counter++;

> }

> }

> VirtualMachineConfigSpec newSpec2 = new VirtualMachineConfigSpec();

> newSpec2.deviceChange = deviceSpecAdd;

>

> ManagedObjectReference reconfRefAddDisks = null;

> try

> {

> reconfRefAddDisks = service.ReconfigVMTask(vmRef, newSpec2);

> }

>...

HTH, Tos2k

Reply
0 Kudos
ISYS2
Enthusiast
Enthusiast
Jump to solution

Brilliant thanks.

I actually managed to work most of this out by reading throught the vmcreate.pl in the SDK this morning but your solution looks more logical.

Regards

ISYS

ps I will send the Xmas card in December Smiley Happy

Reply
0 Kudos
MessageOne
Contributor
Contributor
Jump to solution

Can anyone provide me an executable or a complete code in .NET or an API in Perl for adding an existing vmdk file to a VM? Thanks.

Reply
0 Kudos
tl3462
Contributor
Contributor
Jump to solution

I'm trying to add an existing vmdk to a virtual machine. Most relevant posts on this forum seem to point here but I'm still stumped.

I am fairly new to the VI SDK and am having trouble finding sufficient documentation to help me do this, maybe I'm just not looking in the right places.

The code snippet posted here by tos2k seems to refer to an old version of the library, can anyone help me out with something more up to date?

Please can someone explain to me how I get a VirtualDeviceConfigSpec from my existing host or vm too (if that's what I need to do)?

In essence what I'm trying to do is:

- connect to vm/host (which?)

- add vmdk to vm (programmatically if possible, otherwise just edit the vm's vmx file and refresh inventory?)

Thanks,

Tom

Reply
0 Kudos
Dreamer732
Contributor
Contributor
Jump to solution

Hi,

What should be the key value to set?

Or - will key be automatically set while configuring the vm?

Regards,

Dreamer

Reply
0 Kudos