use of com.vmware.vim25.VirtualDeviceConfigSpec in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method detachAllDisks.
public void detachAllDisks() throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachAllDisk(). target MOR: " + _mor.get_value());
VirtualDisk[] disks = getAllDiskDevice();
if (disks.length > 0) {
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[disks.length];
for (int i = 0; i < disks.length; i++) {
deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
deviceConfigSpecArray[i].setDevice(disks[i]);
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.remove);
}
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 - detachAllDisk() done(failed)");
throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachAllDisk() done(successfully)");
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method detachDisk.
// vmdkDatastorePath: [datastore name] vmdkFilePath
public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastorePath, boolean deleteBackingFile) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk(). target MOR: " + _mor.get_value() + ", vmdkDatastorePath: " + vmdkDatastorePath + ", deleteBacking: " + deleteBackingFile);
// Note: if VM has been taken snapshot, original backing file will be renamed, therefore, when we try to find the matching
// VirtualDisk, we only perform prefix matching
Pair<VirtualDisk, String> deviceInfo = getDiskDevice(vmdkDatastorePath, false);
if (deviceInfo == null) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk() done (failed)");
throw new Exception("No such disk device: " + vmdkDatastorePath);
}
List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(deviceInfo.first());
if (deleteBackingFile) {
deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.destroy);
}
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.remove);
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 - detachDisk() done (failed)");
throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
// VMware does not update snapshot references to the detached disk, we have to work around it
SnapshotDescriptor snapshotDescriptor = null;
try {
snapshotDescriptor = getSnapshotDescriptor();
} catch (Exception e) {
s_logger.info("Unable to retrieve snapshot descriptor, will skip updating snapshot reference");
}
if (snapshotDescriptor != null) {
for (Pair<String, ManagedObjectReference> pair : chain) {
DatastoreFile dsFile = new DatastoreFile(pair.first());
snapshotDescriptor.removeDiskReferenceFromSnapshot(dsFile.getFileName());
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
String dsPath = getSnapshotDescriptorDatastorePath();
assert (dsPath != null);
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), dsPath);
getContext().uploadResourceContent(url, snapshotDescriptor.getVmsdContent());
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk() done (successfully)");
return chain;
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class HypervisorHostHelper method getControllerSpec.
private static VirtualDeviceConfigSpec getControllerSpec(String diskController, int busNum) {
VirtualDeviceConfigSpec controllerSpec = new VirtualDeviceConfigSpec();
VirtualController controller = null;
if (diskController.equalsIgnoreCase(DiskControllerType.ide.toString())) {
controller = new VirtualIDEController();
} else if (DiskControllerType.pvscsi == DiskControllerType.getType(diskController)) {
controller = new ParaVirtualSCSIController();
} else if (DiskControllerType.lsisas1068 == DiskControllerType.getType(diskController)) {
controller = new VirtualLsiLogicSASController();
} else if (DiskControllerType.buslogic == DiskControllerType.getType(diskController)) {
controller = new VirtualBusLogicController();
} else if (DiskControllerType.lsilogic == DiskControllerType.getType(diskController)) {
controller = new VirtualLsiLogicController();
}
if (!diskController.equalsIgnoreCase(DiskControllerType.ide.toString())) {
((VirtualSCSIController) controller).setSharedBus(VirtualSCSISharing.NO_SHARING);
}
controller.setBusNumber(busNum);
controller.setKey(busNum - VmwareHelper.MAX_SCSI_CONTROLLER_COUNT);
controllerSpec.setDevice(controller);
controllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
return controllerSpec;
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class HypervisorHostHelper method createBlankVm.
public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, String vmInternalCSName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent, Pair<String, String> controllerInfo, Boolean systemVm) throws Exception {
if (s_logger.isInfoEnabled())
s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
VirtualDeviceConfigSpec controllerSpec = null;
// VM config basics
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
if (vmInternalCSName == null)
vmInternalCSName = vmName;
VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
String recommendedController = host.getRecommendedDiskController(guestOsIdentifier);
String newRootDiskController = controllerInfo.first();
String newDataDiskController = controllerInfo.second();
if (DiskControllerType.getType(controllerInfo.first()) == DiskControllerType.osdefault) {
newRootDiskController = recommendedController;
}
if (DiskControllerType.getType(controllerInfo.second()) == DiskControllerType.osdefault) {
newDataDiskController = recommendedController;
}
Pair<String, String> updatedControllerInfo = new Pair<String, String>(newRootDiskController, newDataDiskController);
String scsiDiskController = HypervisorHostHelper.getScsiController(updatedControllerInfo, recommendedController);
// If there is requirement for a SCSI controller, ensure to create those.
if (scsiDiskController != null) {
int busNum = 0;
int maxControllerCount = VmwareHelper.MAX_SCSI_CONTROLLER_COUNT;
if (systemVm) {
maxControllerCount = 1;
}
while (busNum < maxControllerCount) {
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec = getControllerSpec(DiskControllerType.getType(scsiDiskController).toString(), busNum);
vmConfig.getDeviceChange().add(scsiControllerSpec);
busNum++;
}
}
if (guestOsIdentifier.startsWith("darwin")) {
//Mac OS
s_logger.debug("Add USB Controller device for blank Mac OS VM " + vmName);
//For Mac OS X systems, the EHCI+UHCI controller is enabled by default and is required for USB mouse and keyboard access.
VirtualDevice usbControllerDevice = VmwareHelper.prepareUSBControllerDevice();
VirtualDeviceConfigSpec usbControllerSpec = new VirtualDeviceConfigSpec();
usbControllerSpec.setDevice(usbControllerDevice);
usbControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(usbControllerSpec);
}
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.getDeviceChange().add(videoDeviceSpec);
if (host.createVm(vmConfig)) {
// Here, when attempting to find the VM, we need to use the name
// with which we created it. This is the only such place where
// we need to do this. At all other places, we always use the
// VM's internal cloudstack generated name. Here, we cannot use
// the internal name because we can set the internal name into the
// VM's custom field CLOUD_VM_INTERNAL_NAME only after we create
// the VM.
VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
assert (vmMo != null);
vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME, vmInternalCSName);
int ideControllerKey = -1;
while (ideControllerKey < 0) {
ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
if (ideControllerKey >= 0)
break;
s_logger.info("Waiting for IDE controller be ready in VM: " + vmInternalCSName);
Thread.sleep(1000);
}
return true;
}
return false;
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class HypervisorHostHelper method createWorkerVM.
public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception {
// Allow worker VM to float within cluster so that we will have better chance to
// create it successfully
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (morCluster != null)
hyperHost = new ClusterMO(hyperHost.getContext(), morCluster);
VirtualMachineMO workingVM = null;
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
vmConfig.setMemoryMB((long) 4);
vmConfig.setNumCPUs(1);
vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
fileInfo.setVmPathName(dsMo.getDatastoreRootPath());
vmConfig.setFiles(fileInfo);
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(0);
scsiController.setKey(1);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
if (hyperHost.createVm(vmConfig)) {
// Ugly work-around, it takes time for newly created VM to appear
for (int i = 0; i < 10 && workingVM == null; i++) {
workingVM = hyperHost.findVmOnHyperHost(vmName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
s_logger.debug("[ignored] interupted while waiting to config vm.");
}
}
}
if (workingVM != null) {
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER, "true");
String workerTag = String.format("%d-%s", System.currentTimeMillis(), hyperHost.getContext().getStockObject("noderuninfo"));
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER_TAG, workerTag);
}
return workingVM;
}
Aggregations