static VirtualDeviceConfigSpec createAddCdConfigSpec(VirtualMachine vm, String dsName, String isoName) throws Exception { VirtualDeviceConfigSpec cdSpec = new VirtualDeviceConfigSpec(); cdSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualCdrom cdrom = new VirtualCdrom(); VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo(); DatastoreSummary ds = findDatastoreSummary(vm, dsName); cdDeviceBacking.setDatastore(ds.getDatastore()); cdDeviceBacking.setFileName("[" + dsName +"] "+ vm.getName() + "/" + isoName + ".iso"); VirtualDevice vd = getIDEController(vm); cdrom.setBacking(cdDeviceBacking); cdrom.setControllerKey(vd.getKey()); cdrom.setUnitNumber(vd.getUnitNumber()); cdrom.setKey(-1); cdSpec.setDevice(cdrom); return cdSpec; }
VirtualDeviceConfigSpec getPopulatedCDSpecs(String fileName, ManagedObjectReference dataStoreRef, List<VirtualDevice> virtualDevicesList, VirtualDeviceConfigSpecOperation operation, Integer controllerKey, Integer unitNumber, Integer key, String parameter, VmInputs vmInputs) { VirtualDeviceConfigSpec cdSpecs = new VirtualDeviceConfigSpec(); if (Operation.ADD.toString().equalsIgnoreCase(parameter)) { VirtualCdromIsoBackingInfo cdIsoBacking = new VirtualCdromIsoBackingInfo(); cdIsoBacking.setDatastore(dataStoreRef); cdIsoBacking.setFileName(fileName + TEST_CD_ISO); VirtualCdrom cdRom = new VirtualCdrom(); cdRom.setBacking(cdIsoBacking); cdRom.setControllerKey(controllerKey); cdRom.setUnitNumber(unitNumber); cdRom.setKey(key); return getCDSpec(cdSpecs, operation, cdRom); } else { VirtualCdrom cdRemove = findVirtualDevice(VirtualCdrom.class, virtualDevicesList, vmInputs); if (cdRemove != null) { return getCDSpec(cdSpecs, operation, cdRemove); } } throw new RuntimeException("No optical device named: [" + vmInputs.getUpdateValue() + "] can be found."); }
public VirtualDevice getIsoDevice(String filename) throws Exception { List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient(). getDynamicProperty(_mor, "config.hardware.device"); if(devices != null && devices.size() > 0) { for(VirtualDevice device : devices) { if(device instanceof VirtualCdrom && device.getBacking() instanceof VirtualCdromIsoBackingInfo && ((VirtualCdromIsoBackingInfo)device.getBacking()).getFileName().equals(filename)) { return device; } } } return null; }
/** * Changes to backing of the cdrom to an iso-backed one. * * @param cdrom * @param imagePath * path to iso on disk, sth. like "[datastore] /images/ubuntu-16.04-amd64.iso" */ public static void insertCdrom(VirtualCdrom cdrom, String imagePath) { VirtualCdromIsoBackingInfo backing = new VirtualCdromIsoBackingInfo(); backing.setFileName(imagePath); cdrom.setBacking(backing); }