use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class VirtualMachineMO method ensureBusLogicDeviceControllers.
public void ensureBusLogicDeviceControllers(int count, int availableBusNum) throws Exception {
int scsiControllerKey = getBusLogicDeviceControllerKeyNoException();
if (scsiControllerKey < 0) {
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
int busNum = availableBusNum;
while (busNum < count) {
VirtualBusLogicController scsiController = new VirtualBusLogicController();
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(busNum);
scsiController.setKey(busNum - VmwareHelper.MAX_SCSI_CONTROLLER_COUNT);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
busNum++;
}
if (configureVm(vmConfig)) {
throw new Exception("Unable to add Scsi BusLogic controllers to the VM " + getName());
} else {
s_logger.info("Successfully added " + count + " SCSI BusLogic controllers.");
}
}
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class VirtualMachineMO method attachDisk.
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue());
int controllerKey = 0;
int unitNumber = 0;
if (DiskControllerType.getType(diskController) == DiskControllerType.ide) {
// IDE virtual disk cannot be added if VM is running
if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running.");
}
// Get next available unit number and controller key
int ideDeviceCount = getNumberOfIDEDevices();
if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) {
throw new Exception("Maximum limit of devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached.");
}
controllerKey = getIDEControllerKey(ideDeviceCount);
unitNumber = getFreeUnitNumberOnIDEController(controllerKey);
} else {
controllerKey = getScsiDiskControllerKey(diskController);
unitNumber = -1;
}
synchronized (_mor.getValue().intern()) {
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1);
controllerKey = newDisk.getControllerKey();
unitNumber = newDisk.getUnitNumber();
VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) newDisk.getBacking();
String vmdkFileName = backingInfo.getFileName();
DiskControllerType diskControllerType = DiskControllerType.getType(diskController);
VmdkAdapterType vmdkAdapterType = VmdkAdapterType.getAdapterType(diskControllerType);
if (vmdkAdapterType == VmdkAdapterType.none) {
String message = "Failed to attach disk due to invalid vmdk adapter type for vmdk file [" + vmdkFileName + "] with controller : " + diskControllerType;
s_logger.debug(message);
throw new Exception(message);
}
updateVmdkAdapter(vmdkFileName, vmdkAdapterType.toString());
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(newDisk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
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.VirtualDeviceConfigSpec in project cloudstack by apache.
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.getValue() + ", 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);
if (deviceInfo == null) {
s_logger.warn("vCenter API trace - detachDisk() done (failed)");
throw new Exception("No such disk device: " + vmdkDatastorePath);
}
// IDE virtual disk cannot be detached if VM is running
if (deviceInfo.second() != null && deviceInfo.second().contains("ide")) {
if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
throw new Exception("Removing a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. " + "Please re-try when VM is not running.");
}
}
List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(deviceInfo.first());
if (deleteBackingFile) {
deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.DESTROY);
}
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
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 VirtualMachineMO method tearDownDevices.
public void tearDownDevices(Class<?>[] deviceClasses) throws Exception {
VirtualDevice[] devices = getMatchedDevices(deviceClasses);
if (devices.length > 0) {
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
for (int i = 0; i < devices.length; i++) {
deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
deviceConfigSpecArray[i].setDevice(devices[i]);
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
vmConfigSpec.getDeviceChange().add(deviceConfigSpecArray[i]);
}
if (!configureVm(vmConfigSpec)) {
throw new Exception("Failed to detach devices");
}
}
}
use of com.vmware.vim25.VirtualDeviceConfigSpec in project cloudstack by apache.
the class VirtualMachineMO method ensureLsiLogicSasDeviceControllers.
public void ensureLsiLogicSasDeviceControllers(int count, int availableBusNum) throws Exception {
int scsiControllerKey = getLsiLogicSasDeviceControllerKeyNoException();
if (scsiControllerKey < 0) {
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
int busNum = availableBusNum;
while (busNum < count) {
VirtualLsiLogicSASController scsiController = new VirtualLsiLogicSASController();
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(busNum);
scsiController.setKey(busNum - VmwareHelper.MAX_SCSI_CONTROLLER_COUNT);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
busNum++;
}
if (configureVm(vmConfig)) {
throw new Exception("Unable to add Scsi controller of type LsiLogic SAS.");
}
}
}
Aggregations