Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 293210

Add Virtual Disk to VM using C#

$
0
0

Hi All,

 

I'm using the following code to attempt to add a virtual hard disk to a VM using C#. I'm getting the error : Invalid Configuration for Device '0'. Any ideas why that's happening?

 

 

public void addVMDisk(VimClient client, string vmUUID, int sizeGB, bool thinProvisioned)

{

 

//Setup thin-provisioning by default

 

bool isThinProvisioned = thinProvisioned;

 

int controllerKey = 0;

 

int sizeKB = sizeGB * 1048576;

 

int diskCount = 0;

 

string datastoreName = string.Empty;

 

ManagedObjectReference morDatastore = null;

  

 

NameValueCollection vmFilter = new NameValueCollection();

vmFilter.Add(

"config.instanceUuid", vmUUID);

 

try

{

 

VirtualMachine vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, vmFilter, null);

 

//Get the controller key

 

//Get all of the devices...

 

VirtualDevice[] devices = vm.Config.Hardware.Device;

 

//Loop throught each device and get the controller key

 

foreach (VirtualDevice device in devices)

{

 

 

if (device.DeviceInfo.Label.Equals("SCSI controller 0"))

{

controllerKey = (

int)device.ControllerKey;

 

VirtualSCSIController test = new VirtualSCSIController();

}

 

if (device.DeviceInfo.Label.Contains("Hard disk"))

{

diskCount++;

 

Console.WriteLine("Disk Count = " + diskCount);

}

}

 

//Get the datastores..

 

ManagedObjectReference[] datastores = vm.Datastore;

 

if (datastores.Length >= 1)

{

morDatastore = datastores[0];

 

Datastore datastore = (Datastore)client.GetView(morDatastore, new string[] { "Name" });

datastoreName = datastore.Name;

}

 

if (controllerKey != 0 && morDatastore != null)

{

 

Console.WriteLine("Contoller Key: " + controllerKey);

 

//Setup Disk BackingInfo

 

VirtualDiskFlatVer2BackingInfo backInfo = new VirtualDiskFlatVer2BackingInfo();

 

if (isThinProvisioned)

{

backInfo.ThinProvisioned =

true;

}

backInfo.DiskMode =

"persistent";

backInfo.FileName =

"[" + datastoreName + "] " + vm.Name.ToUpper().Trim() + "/" + vm.Name.ToUpper() + "_" + diskCount + ".vmdk";

 

Console.WriteLine("File Name: " + "[" + datastoreName + "] " + vm.Name.ToUpper().Trim() + "/" + vm.Name.ToUpper() + "_" + diskCount + ".vmdk");

 

VirtualDisk disk = new VirtualDisk();

disk.Backing = backInfo;

disk.CapacityInKB = sizeKB;

disk.ControllerKey = controllerKey;

disk.Key = -1;

disk.UnitNumber = diskCount;

 

 

VirtualDeviceConfigSpec devConfSpec = new VirtualDeviceConfigSpec();

devConfSpec.Device = disk;

devConfSpec.FileOperation =

VirtualDeviceConfigSpecFileOperation.create;

devConfSpec.Operation =

VirtualDeviceConfigSpecOperation.add;

 

VirtualDeviceConfigSpec[] specs = {devConfSpec};

 

VirtualMachineConfigSpec vmConfSpec = new VirtualMachineConfigSpec();

vmConfSpec.DeviceChange = specs;

vm.ReconfigVM_Task(vmConfSpec);

}

}

 

catch (Exception ex)

{

 

Console.WriteLine("Error: Could not add disk! Detail: " + ex);

}

}


Viewing all articles
Browse latest Browse all 293210

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>