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

Re: Add Virtual Disk to VM using C#

$
0
0

I just fixed my own problem. My confusion was with the difference between the "Key" and "ControllerKey" properties. The "ControllerKey" in the DeviceBacking actually refers to the "Key" of the SCSI controller...huh.

 

Here's the working code for anyone that's interested:

 

 

 

//Add Disk to VM

 

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 through each device and get the controller key

 

foreach (VirtualDevice device in devices)

{

//Find our SCSI Controller

 

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

{

     controllerKey = (int)device.Key;

 

}

 

//Check for any existing disks

 

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

{

     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)

{

 

//Setup Disk BackingInfo

 

VirtualDiskFlatVer2BackingInfo backInfo = new VirtualDiskFlatVer2BackingInfo();

 

if (isThinProvisioned)

{

     backInfo.ThinProvisioned = true;

}

backInfo.Datastore = morDatastore;

backInfo.DiskMode = "persistent";

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

 

//Setup Virtual Disk

 

VirtualDisk disk = new VirtualDisk();

disk.Backing = backInfo;

disk.CapacityInKB = sizeKB;

disk.ControllerKey = controllerKey;

disk.Key = -1;

disk.UnitNumber = diskCount;

 

 

//Setup DeviceConfigSpec

 

VirtualDeviceConfigSpec devConfSpec = new VirtualDeviceConfigSpec();

devConfSpec.Device = disk;

devConfSpec.FileOperation =

VirtualDeviceConfigSpecFileOperation.create;

devConfSpec.Operation =

VirtualDeviceConfigSpecOperation.add;

 

//Put DeviceConfigSpec into an array

 

VirtualDeviceConfigSpec[] specs = {devConfSpec};

 

//Create VirtualMachine Config Spec

 

VirtualMachineConfigSpec vmConfSpec = new VirtualMachineConfigSpec();

vmConfSpec.DeviceChange = specs;

 

//Call ReconfigureVM

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>