static VirtualDeviceConfigSpec createRemoveDiskConfigSpec( VirtualMachine vm, String diskName) throws Exception { VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); VirtualDisk disk = (VirtualDisk) findVirtualDevice(vm.getConfig(), diskName); if(disk != null) { diskSpec.setOperation(VirtualDeviceConfigSpecOperation.remove); diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.destroy); diskSpec.setDevice(disk); return diskSpec; } else { System.out.println("No device found: " + diskName); return null; } }
static VirtualDeviceConfigSpec createRemoveDiskConfigSpec( VirtualMachineConfigInfo vmConfig, String diskName) throws Exception { VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); VirtualDisk disk = (VirtualDisk) findVirtualDevice( vmConfig, diskName); if(disk != null) { diskSpec.setOperation( VirtualDeviceConfigSpecOperation.remove); // remove the following line can keep the disk file diskSpec.setFileOperation( VirtualDeviceConfigSpecFileOperation.destroy); diskSpec.setDevice(disk); return diskSpec; } else { throw new Exception("No device found: " + diskName); } }
static VirtualDeviceConfigSpec createDiskSpec(String dsName, int cKey, long diskSizeKB, String diskMode) { VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); diskSpec.setFileOperation( VirtualDeviceConfigSpecFileOperation.create); VirtualDisk vd = new VirtualDisk(); vd.setCapacityInKB(diskSizeKB); diskSpec.setDevice(vd); vd.setKey(0); vd.setUnitNumber(0); vd.setControllerKey(cKey); VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); String fileName = "["+ dsName +"]"; diskfileBacking.setFileName(fileName); diskfileBacking.setDiskMode(diskMode); diskfileBacking.setThinProvisioned(true); vd.setBacking(diskfileBacking); return diskSpec; }
private VirtualDeviceConfigSpec createNewDataDisk(VirtualDisk vdSystemDisk, long newDiskSpace, int deviceKey, int unitNumber) throws Exception { logger.info("reconfigureDisks() create data disk space with " + newDiskSpace + " KB"); ManagedObjectReference vmDatastore = ((VirtualDeviceFileBackingInfo) vdSystemDisk .getBacking()).getDatastore(); String vmDatastoreName = (String) vmw.getServiceUtil() .getDynamicProperty(vmDatastore, "summary.name"); VirtualDisk vdDataDisk = new VirtualDisk(); VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); diskfileBacking.setFileName("[" + vmDatastoreName + "]"); diskfileBacking.setDiskMode("persistent"); vdDataDisk.setKey(deviceKey); vdDataDisk.setControllerKey(vdSystemDisk.getControllerKey()); vdDataDisk.setUnitNumber(new Integer(unitNumber)); vdDataDisk.setBacking(diskfileBacking); vdDataDisk.setCapacityInKB(newDiskSpace); VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec(); vmDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD); vmDeviceSpec .setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE); vmDeviceSpec.setDevice(vdDataDisk); return vmDeviceSpec; }
/** * Creates HDD virtual disk */ public static VirtualDeviceConfigSpec createHdd(Integer controllerKey, int unitNumber, DiskService.DiskStateExpanded ds, String diskName, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec, boolean isCreateFile) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo(); backing.setDiskMode(getDiskMode(ds)); VirtualDiskType provisionType = getDiskProvisioningType(ds); if (provisionType != null) { backing.setThinProvisioned(provisionType == VirtualDiskType.THIN); backing.setEagerlyScrub(provisionType == VirtualDiskType.EAGER_ZEROED_THICK); } backing.setFileName(diskName); backing.setDatastore(datastore); VirtualDisk disk = new VirtualDisk(); disk.setCapacityInKB(toKb(ds.capacityMBytes)); disk.setBacking(backing); disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds)); disk.setControllerKey(controllerKey); disk.setUnitNumber(unitNumber); fillInControllerUnitNumber(ds, unitNumber); disk.setKey(-1); VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec(); change.setDevice(disk); if (pbmSpec != null) { // Add storage policy spec pbmSpec.stream().forEach(sp -> { change.getProfile().add(sp); }); } change.setOperation(VirtualDeviceConfigSpecOperation.ADD); if (isCreateFile) { change.setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE); } return change; }
public void createHardDisk(int diskSizeMB, VirtualDiskType type, VirtualDiskMode mode) throws Exception { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); diskfileBacking.setFileName(""); diskfileBacking.setDiskMode(mode.toString()); diskfileBacking.setThinProvisioned(type==VirtualDiskType.thin); VirtualSCSIController scsiController = getFirstAvailableController(VirtualSCSIController.class); int unitNumber = getFirstFreeUnitNumberForController(scsiController); VirtualDisk disk = new VirtualDisk(); disk.setControllerKey(scsiController.key); disk.setUnitNumber(unitNumber); disk.setBacking(diskfileBacking); disk.setCapacityInKB(1024 * diskSizeMB); disk.setKey(-1); diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create); diskSpec.setDevice(disk); VirtualDeviceConfigSpec vdiskSpec = diskSpec; VirtualDeviceConfigSpec [] vdiskSpecArray = {vdiskSpec}; vmConfigSpec.setDeviceChange(vdiskSpecArray); Task task = vm.reconfigVM_Task(vmConfigSpec); task.waitForTask(200, 100); }
private VirtualDeviceConfigSpec getDiskSpecs(VirtualDeviceConfigSpec diskSpecs, VirtualDeviceConfigSpecOperation operation, VirtualDeviceConfigSpecFileOperation fileOperation, VirtualDisk disk) { diskSpecs.setOperation(operation); diskSpecs.setFileOperation(fileOperation); diskSpecs.setDevice(disk); return diskSpecs; }
public boolean configureVm(Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation>[] devices) throws Exception { assert (devices != null); VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length]; int i = 0; for (Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation> deviceTernary : devices) { VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); deviceConfigSpec.setDevice(deviceTernary.first()); deviceConfigSpec.setOperation(deviceTernary.second()); deviceConfigSpec.setFileOperation(deviceTernary.third()); deviceConfigSpecArray[i++] = deviceConfigSpec; } configSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray)); ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, configSpec); boolean result = _context.getVimClient().waitForTask(morTask); if (result) { _context.waitForTaskProgressDone(morTask); return true; } else { s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask)); } return false; }
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; }
static VirtualDeviceConfigSpec createAddDiskConfigSpec( VirtualMachine vm, int diskSize, String diskMode, String diskName) throws Exception { VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); VirtualMachineConfigInfo vmConfig = (VirtualMachineConfigInfo)vm.getConfig(); VirtualDevice[] vds = vmConfig.getHardware().getDevice(); VirtualDisk disk = new VirtualDisk(); VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); int key = 0; int unitNumber = 0; for(int k=0;k<vds.length;k++) { if(vds[k].getDeviceInfo().getLabel().equalsIgnoreCase("SCSI Controller 0")) { key = vds[k].getKey(); } } unitNumber = vds.length + 1; //***********************seems NOT correct!!! String dsName = getFreeDatastoreName(vm, diskSize); if(dsName==null) { return null; } String fileName = "["+ dsName +"] "+ vm.getName() + "/" + diskName + ".vmdk"; diskfileBacking.setFileName(fileName); diskfileBacking.setDiskMode(diskMode); disk.setControllerKey(key); disk.setUnitNumber(unitNumber); disk.setBacking(diskfileBacking); disk.setCapacityInKB(1024 * diskSize); disk.setKey(-1); diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create); diskSpec.setDevice(disk); return diskSpec; }