use of com.vmware.vim25.VirtualCdrom in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method attachIso.
// isoDatastorePath: [datastore name] isoFilePath
public void attachIso(String isoDatastorePath, ManagedObjectReference morDs, boolean connect, boolean connectAtBoot) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachIso(). target MOR: " + _mor.get_value() + ", isoDatastorePath: " + isoDatastorePath + ", datastore: " + morDs.get_value() + ", connect: " + connect + ", connectAtBoot: " + connectAtBoot);
assert (isoDatastorePath != null);
assert (morDs != null);
boolean newCdRom = false;
VirtualCdrom cdRom = (VirtualCdrom) getIsoDevice();
if (cdRom == null) {
newCdRom = true;
cdRom = new VirtualCdrom();
cdRom.setControllerKey(getIDEDeviceControllerKey());
int deviceNumber = getNextIDEDeviceNumber();
cdRom.setUnitNumber(deviceNumber);
cdRom.setKey(-deviceNumber);
}
VirtualDeviceConnectInfo cInfo = new VirtualDeviceConnectInfo();
cInfo.setConnected(connect);
cInfo.setStartConnected(connectAtBoot);
cdRom.setConnectable(cInfo);
VirtualCdromIsoBackingInfo backingInfo = new VirtualCdromIsoBackingInfo();
backingInfo.setFileName(isoDatastorePath);
backingInfo.setDatastore(morDs);
cdRom.setBacking(backingInfo);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdRom);
if (newCdRom) {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
} else {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
}
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 - detachIso() done(failed)");
throw new Exception("Failed to attach ISO due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachIso() done(successfully)");
}
use of com.vmware.vim25.VirtualCdrom in project photon-model by vmware.
the class ClientUtils method attachDiskToVM.
public static String attachDiskToVM(ArrayOfVirtualDevice devices, ManagedObjectReference vm, DiskService.DiskStateExpanded diskState, ManagedObjectReference diskDatastore, Connection connection, VimPortType vimPort) throws Exception {
String diskPath = VimUtils.uriToDatastorePath(diskState.sourceImageReference);
String diskFullPath = CustomProperties.of(diskState).getString(DISK_FULL_PATH, null);
Boolean insertCdRom = CustomProperties.of(diskState).getBoolean(INSERT_CDROM, false);
VirtualDeviceConfigSpec deviceConfigSpec = null;
if (diskState.type == DiskService.DiskType.HDD) {
VirtualSCSIController scsiController = getFirstScsiController(devices);
// Get available free unit numbers for the given scsi controller.
Integer[] scsiUnits = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(diskState);
deviceConfigSpec = createHdd(scsiController.getKey(), scsiUnits[0], diskState, diskFullPath, diskDatastore, pbmSpec, false);
} else if (diskState.type == DiskService.DiskType.CDROM) {
if (insertCdRom) {
if (diskPath == null) {
throw new IllegalStateException(String.format("Cannot insert empty iso file into CD-ROM"));
}
// Find first available CD ROM to insert the iso file
VirtualCdrom cdrom = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualCdrom).map(d -> (VirtualCdrom) d).findFirst().orElse(null);
if (cdrom == null) {
throw new IllegalStateException(String.format("Could not find Virtual CD ROM to insert %s.", diskPath));
}
insertCdrom(cdrom, diskPath);
deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdrom);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
} else {
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(ideUnit);
deviceConfigSpec = createCdrom(ideController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
// mount iso image
insertCdrom((VirtualCdrom) deviceConfigSpec.getDevice(), diskPath);
}
// Live add of cd-rom is not possible. Hence it needs to be powered off
// Power off is needed to ADD cd-rom
powerOffVm(connection, vimPort, vm);
}
} else if (diskState.type == DiskService.DiskType.FLOPPY) {
VirtualDevice sioController = getFirstSioController(devices);
int sioUnit = findFreeUnit(sioController, devices.getVirtualDevice());
int availableUnitNumber = nextUnitNumber(sioUnit);
deviceConfigSpec = createFloppy(sioController, availableUnitNumber);
fillInControllerUnitNumber(diskState, availableUnitNumber);
if (diskPath != null) {
insertFloppy((VirtualFloppy) deviceConfigSpec.getDevice(), diskPath);
}
// Power off is needed to ADD floppy
powerOffVm(connection, vimPort, vm);
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference reconfigureTask = vimPort.reconfigVMTask(vm, spec);
TaskInfo info = VimUtils.waitTaskEnd(connection, reconfigureTask);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
if (!insertCdRom && diskState.type != DiskService.DiskType.HDD) {
// This means it is CDROM or Floppy. Hence power on the VM as it is powered off to
// perform the operation
powerOnVM(connection, vimPort, vm);
}
return diskFullPath;
}
use of com.vmware.vim25.VirtualCdrom in project photon-model by vmware.
the class VSphereAdapterInstanceService method updateDiskLinksAfterProvisionSuccess.
/**
* Update the details of the disk into compute state after the provisioning is successful
*/
private void updateDiskLinksAfterProvisionSuccess(ComputeState state, List<VirtualDevice> disks, ProvisionContext ctx) {
ArrayList<String> diskLinks = new ArrayList<>(disks.size());
// Fill in the disk links from the input to the ComputeState, as it may contain non hdd
// disk as well. For ex, Floppy or CD-Rom
ctx.disks.stream().forEach(ds -> diskLinks.add(ds.documentSelfLink));
// Handle all the HDD disk
for (VirtualDevice disk : disks) {
DiskStateExpanded matchedDs = findMatchingDiskState(disk, ctx.disks);
if (disk instanceof VirtualDisk) {
handleVirtualDiskUpdate(ctx.child.endpointLink, matchedDs, (VirtualDisk) disk, diskLinks, ctx.parent.description.regionId, this, CustomProperties.of(state).getString(CustomProperties.MOREF), CustomProperties.of(state).getString(CustomProperties.DATACENTER_SELF_LINK), null, null).sendWith(this);
} else if (disk instanceof VirtualCdrom) {
handleVirtualDeviceUpdate(ctx.child.endpointLink, matchedDs, DiskType.CDROM, disk, diskLinks, ctx.parent.description.regionId, this, true, CustomProperties.of(state).getString(CustomProperties.DATACENTER_SELF_LINK)).sendWith(this);
} else if (disk instanceof VirtualFloppy) {
handleVirtualDeviceUpdate(ctx.child.endpointLink, matchedDs, DiskType.FLOPPY, disk, diskLinks, ctx.parent.description.regionId, this, true, CustomProperties.of(state).getString(CustomProperties.DATACENTER_SELF_LINK)).sendWith(this);
}
}
state.diskLinks = diskLinks;
}
use of com.vmware.vim25.VirtualCdrom in project photon-model by vmware.
the class InstanceClient method reconfigureCdrom.
private VirtualDeviceConfigSpec reconfigureCdrom(VirtualCdrom vcd) {
VirtualCdrom cdrom = new VirtualCdrom();
cdrom.setControllerKey(vcd.getControllerKey());
cdrom.setKey(vcd.getKey());
cdrom.setUnitNumber(vcd.getUnitNumber());
VirtualDeviceConnectInfo info = new VirtualDeviceConnectInfo();
info.setAllowGuestControl(true);
info.setConnected(true);
info.setStartConnected(true);
cdrom.setConnectable(info);
cdrom.setBacking(vcd.getBacking());
VirtualDeviceConfigSpec spec = new VirtualDeviceConfigSpec();
spec.setDevice(cdrom);
spec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
return spec;
}
use of com.vmware.vim25.VirtualCdrom in project cloudstack by apache.
the class VirtualMachineMO method attachIso.
// isoDatastorePath: [datastore name] isoFilePath
public void attachIso(String isoDatastorePath, ManagedObjectReference morDs, boolean connect, boolean connectAtBoot, Integer key, boolean force) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachIso(). target MOR: " + _mor.getValue() + ", isoDatastorePath: " + isoDatastorePath + ", datastore: " + morDs.getValue() + ", connect: " + connect + ", connectAtBoot: " + connectAtBoot);
assert (isoDatastorePath != null);
assert (morDs != null);
boolean newCdRom = false;
VirtualCdrom cdRom;
if (key == null) {
cdRom = (VirtualCdrom) getIsoDevice();
} else {
cdRom = (VirtualCdrom) getIsoDevice(key);
}
if (cdRom == null) {
newCdRom = true;
cdRom = new VirtualCdrom();
cdRom.setControllerKey(getIDEDeviceControllerKey());
int deviceNumber = getNextIDEDeviceNumber();
cdRom.setUnitNumber(deviceNumber);
cdRom.setKey(-deviceNumber);
}
VirtualDeviceConnectInfo cInfo = new VirtualDeviceConnectInfo();
cInfo.setConnected(connect);
cInfo.setStartConnected(connectAtBoot);
cdRom.setConnectable(cInfo);
VirtualCdromIsoBackingInfo backingInfo = new VirtualCdromIsoBackingInfo();
backingInfo.setFileName(isoDatastorePath);
backingInfo.setDatastore(morDs);
cdRom.setBacking(backingInfo);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
// VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdRom);
if (newCdRom) {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
} else {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
}
// deviceConfigSpecArray[0] = deviceConfigSpec;
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
final Boolean[] flags = { false };
final VirtualMachineMO vmMo = this;
Future<?> future = answerVmwareQuestion(flags, vmMo, force);
try {
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachIso() done(failed)");
throw new Exception("Failed to attach ISO due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachIso() done(successfully)");
} finally {
flags[0] = true;
future.cancel(true);
}
}
Aggregations