use of com.vmware.vim25.VirtualMachineConfigSpec in project CloudStack-archive by CloudStack-extras.
the class HypervisorHostHelper method createBlankVm.
public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent) throws Exception {
if (s_logger.isInfoEnabled())
s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
// VM config basics
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
// Scsi controller
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
scsiController.setSharedBus(VirtualSCSISharing.noSharing);
scsiController.setBusNumber(0);
scsiController.setKey(1);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
DatastoreMO dsMo = new DatastoreMO(host.getContext(), morDs);
fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
vmConfig.setFiles(fileInfo);
VirtualMachineVideoCard videoCard = new VirtualMachineVideoCard();
videoCard.setControllerKey(100);
videoCard.setUseAutoDetect(true);
VirtualDeviceConfigSpec videoDeviceSpec = new VirtualDeviceConfigSpec();
videoDeviceSpec.setDevice(videoCard);
videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec, videoDeviceSpec });
if (host.createVm(vmConfig)) {
VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
assert (vmMo != null);
int ideControllerKey = -1;
while (ideControllerKey < 0) {
ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
if (ideControllerKey >= 0)
break;
s_logger.info("Waiting for IDE controller be ready in VM: " + vmName);
Thread.sleep(1000);
}
if (snapshotDirToParent) {
String snapshotDir = String.format("/vmfs/volumes/%s/", dsMo.getName());
s_logger.info("Switch snapshot working directory to " + snapshotDir + " for " + vmName);
vmMo.setSnapshotDirectory(snapshotDir);
// Don't have a good way to test if the VM is really ready for use through normal API after configuration file manipulation,
// delay 3 seconds
Thread.sleep(3000);
}
s_logger.info("Blank VM: " + vmName + " is ready for use");
return true;
}
return false;
}
use of com.vmware.vim25.VirtualMachineConfigSpec in project CloudStack-archive by CloudStack-extras.
the class ClusterMO method createVm.
@Override
public boolean createVm(VirtualMachineConfigSpec vmSpec) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - createVM_Task(). target MOR: " + _mor.get_value() + ", VirtualMachineConfigSpec: " + new Gson().toJson(vmSpec));
assert (vmSpec != null);
DatacenterMO dcMo = new DatacenterMO(_context, getHyperHostDatacenter());
ManagedObjectReference morPool = getHyperHostOwnerResourcePool();
ManagedObjectReference morTask = _context.getService().createVM_Task(dcMo.getVmFolder(), vmSpec, morPool, null);
String result = _context.getServiceUtil().waitForTask(morTask);
if (result.equals("sucess")) {
_context.waitForTaskProgressDone(morTask);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - createVM_Task() done(successfully)");
return true;
} else {
s_logger.error("VMware createVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - createVM_Task() done(failed)");
return false;
}
use of com.vmware.vim25.VirtualMachineConfigSpec in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method attachDisk.
public void attachDisk(Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain, int controllerKey) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.get_value() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain));
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, controllerKey, vmdkDatastorePathChain, -1, 1);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(newDisk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
deviceConfigSpecArray[0] = deviceConfigSpec;
reConfigSpec.setDeviceChange(deviceConfigSpecArray);
ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, reConfigSpec);
String result = _context.getServiceUtil().waitForTask(morTask);
if (!result.equals("sucess")) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk() done(failed)");
throw new Exception("Failed to attach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk() done(successfully)");
}
use of com.vmware.vim25.VirtualMachineConfigSpec in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method cloneFromDiskChain.
public void cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryMb, String[] disks, ManagedObjectReference morDs) throws Exception {
assert (disks != null);
assert (disks.length >= 1);
HostMO hostMo = getRunningHost();
VirtualMachineConfigInfo vmConfigInfo = getConfigInfo();
if (!hostMo.createBlankVm(clonedVmName, 1, cpuSpeedMHz, 0, false, memoryMb, 0, vmConfigInfo.getGuestId(), morDs, false))
throw new Exception("Unable to create a blank VM");
VirtualMachineMO clonedVmMo = hostMo.findVmOnHyperHost(clonedVmName);
if (clonedVmMo == null)
throw new Exception("Unable to find just-created blank VM");
boolean bSuccess = false;
try {
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, -1, disks, morDs, -1, 1);
deviceConfigSpecArray[0].setDevice(device);
deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
clonedVmMo.configureVm(vmConfigSpec);
bSuccess = true;
} finally {
if (!bSuccess) {
clonedVmMo.detachAllDisks();
clonedVmMo.destroy();
}
}
}
use of com.vmware.vim25.VirtualMachineConfigSpec in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method plugDevice.
public void plugDevice(VirtualDevice device) throws Exception {
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
deviceConfigSpecArray[0].setDevice(device);
deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
if (!configureVm(vmConfigSpec)) {
throw new Exception("Failed to add devices");
}
}
Aggregations