Can anyone see why the following code gives me the error "Invalid configuration for device '1'"? I found sample JAVA code that did this almost exactly. The code I found had a "Copyright VMWare" in it. I have not successfully created a VM with disks in C# yet. Thanks!
- David Wright
--- <begin code snippet> --
int diskCtrlKey = 1;
int busNumber = 0;
// disk controller
VirtualDeviceConfigSpec vdcSpec = GetDiskControllerSpec(diskCtrlKey, busNumber);
// first (and only) disk
String storageName = (String)clientInfo.SvcUtil.GetDynamicProperty(dataStoreMOR, "info.name");
string backingFilePath = String.Format("[]/CDrive.vmdk", storageName, vmName); VirtualDeviceConfigSpec vdisk1Spec = GetDiskConfigSpec(diskCtrlKey, dataStoreMOR, backingFilePath, true); // add disk controller and disk configSpec.deviceChange = new VirtualDeviceConfigSpec[] { vdcSpec, vdisk1Spec }; ManagedObjectReference vmCreateTaskRef; try { vmCreateTaskRef = clientInfo.Connection.Service.CreateVM_Task(parentFolderMOR, configSpec, poolRef, null); log.LogLine(String.Format("Create VM Task = ''", vmCreateTaskRef.type, vmCreateTaskRef.Value)); } catch (Exception excep) { log.LogLine(String.Format("Exception creating VM: {0}: stack=", excep.Message, excep.StackTrace));
throw;
}
// wait for task to complete
String res = WaitForTask(vmCreateTaskRef);
if(String.Compare(res, "success", true) == 0)
log.LogLine("VM Created successfully");
else
log.LogLine(String.Format("VM creation failed: " + res));
...
/// <summary>
/// Get a SCSI controller spec to add
/// </summary>
/// <returns></returns>
private VirtualDeviceConfigSpec GetDiskControllerSpec(int key, int busNumber)
{
VirtualBusLogicController scsiCtrl = new VirtualBusLogicController();
scsiCtrl.busNumber = busNumber;
scsiCtrl.key = key;
scsiCtrl.sharedBus = VirtualSCSISharing.noSharing;
VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec();
scsiCtrlSpec.operation = VirtualDeviceConfigSpecOperation.add;
scsiCtrlSpec.operationSpecified = true;
scsiCtrlSpec.device = scsiCtrl;
return scsiCtrlSpec;
}
/// <summary>
/// Get a disk drive specification to add
/// </summary>
/// <param name="ctrlKey">Controller's key</param>
/// <param name="dataStoreMOR">Reference to the data store</param>
/// <param name="backingFilePath">Backing filename</param>
/// <param name="mustCreateBackingFile">true if the backing file must be created now</param>
/// <returns>The VirtualDeviceConfigSpec for the new disk</returns>
private VirtualDeviceConfigSpec GetDiskConfigSpec(int ctrlKey, ManagedObjectReference dataStoreMOR, string backingFilePath, bool mustCreateBackingFile)
{
VirtualDiskFlatVer2BackingInfo diskfileBacking1 = new VirtualDiskFlatVer2BackingInfo();
diskfileBacking1.fileName = backingFilePath;
diskfileBacking1.diskMode = "persistent";
diskfileBacking1.thinProvisioned = true;
diskfileBacking1.thinProvisionedSpecified = true;
// The following statement is not done by VMWare's JAVA example, so I guess it is not needed
// the backing filename is of form pathfile anyway
//
//diskfileBacking1.datastore = dataStoreMOR;
VirtualDisk disk1 = new VirtualDisk();
disk1.key = 0;
disk1.controllerKey = ctrlKey;
disk1.controllerKeySpecified = true;
disk1.unitNumber = 0; // next disk will be 1, etc.
disk1.unitNumberSpecified = true;
disk1.backing = diskfileBacking1;
VirtualDeviceConfigSpec diskSpec1 = new VirtualDeviceConfigSpec();
diskSpec1.operation = VirtualDeviceConfigSpecOperation.add;
diskSpec1.operationSpecified = true;
// If caller wants the backing file to be created by this operation, specify the fileOperation
if (mustCreateBackingFile)
{
diskSpec1.fileOperation = VirtualDeviceConfigSpecFileOperation.create;
diskSpec1.fileOperationSpecified = true;
}
diskSpec1.device = disk1;
return diskSpec1;
}
Hi David,
In order to get the above code snippet working, you need to make two corrections.
The first one is that you need to specify the "capacityInKB" property of the "VirtualDisk" data object as it is mandatory (Refer documentation for VirtualDisk ).
Secondly, you need assign a negative value for key (variable "diskCtrlKey" in the code snippet). Persistent device keys are always assigned and managed by the server, which guarantees that all devices will have non-negative key values. By assigning a negative value for key, the server assigns self-generated non-negative unique key for the device.
Hope the above information helps.
Regards,
Seemanki
That did the trick!
Thanks,Seemanki.