private ObjectNode makeSpec(ManagedObjectReference datastore, VirtualMachineDefinedProfileSpec pbmSpec, String vmName, Map<String, String> networkMappings, VirtualDiskType diskType) { ObjectNode res = newNode(); ObjectNode t = res.putObject(K_STRUCTURE) .putObject("com.vmware.vcenter.ovf.library_item.resource_pool_deployment_spec"); t.put("accept_all_EULA", true); // Only one of datastore / policy can be set. Not both. If datastore is present that will // be given preference if (datastore != null) { t.putObject("default_datastore_id").put(K_OPTIONAL, datastore.getValue()); } else if (pbmSpec != null) { t.putObject("storage_profile_id").put(K_OPTIONAL, pbmSpec.getProfileId()); } if (diskType != null) { t.putObject("storage_provisioning").put(K_OPTIONAL, diskType.value()); } t.putObject("name").put(K_OPTIONAL, vmName); return res; }
private DiskService.DiskState buildBootDiskWithCustomProperties(String cloudConfig, Map<String, String> customProperties) throws Throwable { HashMap<String, String> diskCustomProperties = new HashMap<>(); VirtualDiskType diskProvisionType = customProperties.get(PROVISION_TYPE) != null ? VirtualDiskType.fromValue(customProperties.get(PROVISION_TYPE)) : null; if (diskProvisionType == null) { diskProvisionType = VirtualDiskType.THIN; } diskCustomProperties.put(DISK_MODE_PERSISTENT, "true"); diskCustomProperties.put(PROVISION_TYPE, diskProvisionType.value()); diskCustomProperties.put(SHARES_LEVEL, SharesLevel.HIGH.value()); diskCustomProperties.put(LIMIT_IOPS, "100"); DiskService.DiskState res = buildBootDisk(cloudConfig); res.customProperties = diskCustomProperties; return res; }
/** * Get the provisioning type of the disk */ public static VirtualDiskType getDiskProvisioningType(DiskService.DiskStateExpanded diskState) throws IllegalArgumentException { try { // Return null as default so that what ever defaults picked by vc will be honored // instead of we setting to default THIN. return diskState.customProperties != null && diskState.customProperties.get(PROVISION_TYPE) != null ? VirtualDiskType.fromValue(diskState.customProperties.get(PROVISION_TYPE)) : null; } catch (Exception e) { return null; } }
/** * 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; }
/** * Create virtual disk spec for creating the virtual disk */ private FileBackedVirtualDiskSpec createVirtualDiskSpec(DiskStateExpanded diskStateExpanded, List<VirtualMachineDefinedProfileSpec> pbmSpec) { if (diskStateExpanded.capacityMBytes <= 0) { throw new IllegalArgumentException("Capacity of disk should be greater than 0"); } if (diskStateExpanded.name == null || diskStateExpanded.name.isEmpty()) { throw new IllegalArgumentException("Disk name should not be empty"); } FileBackedVirtualDiskSpec diskSpec = new FileBackedVirtualDiskSpec(); diskSpec.setCapacityKb(toKb(diskStateExpanded.capacityMBytes)); VirtualDiskType provisionType = getDiskProvisioningType(diskStateExpanded); if (provisionType != null) { diskSpec.setDiskType(provisionType.value()); } else { // Default it to THIN diskSpec.setDiskType(VirtualDiskType.THIN.value()); } diskSpec.setAdapterType(VirtualDiskAdapterType.LSI_LOGIC.value()); if (pbmSpec != null) { diskSpec.getProfile().addAll(pbmSpec); } return diskSpec; }
public ObjectNode deployOvfLibItem( String itemId, String vmName, ManagedObjectReference vmFolder, ManagedObjectReference datastore, VirtualMachineDefinedProfileSpec pbmSpec, ManagedObjectReference resourcePool, Map<String, String> networkMappings, VirtualDiskType diskType) throws IOException, RpcException { RpcRequest call = newCall("com.vmware.vcenter.ovf.library_item", "deploy"); bindToSession(call, this.sessionId); call.params.input = newNode(); ObjectNode opInput = call.params.input .putObject(K_STRUCTURE) .putObject(K_OPERATION_INPUT); ObjectNode target = makeTarget(vmFolder.getValue(), resourcePool.getValue()); ObjectNode spec = makeSpec(datastore, pbmSpec, vmName, networkMappings, diskType); opInput.put("deployment_spec", spec); opInput.put("ovf_library_item_id", itemId); opInput.put("target", target); RpcResponse resp = rpc(call); logger.info("Deploying library item {} : {}", itemId, resp.error != null ? resp.error : ""); throwIfError("Cannot deploy library item " + itemId, resp); ObjectNode result = resp.result; if (result == null) { return null; } return (ObjectNode) result .get(K_OUTPUT) .get(K_STRUCTURE) .get("com.vmware.vcenter.ovf.library_item.deployment_result"); }
private HashMap<String, String> buildDiskCustomProperties() { HashMap<String, String> customProperties = new HashMap<>(); customProperties.put(PROVISION_TYPE, VirtualDiskType.THIN.value()); customProperties.put(SHARES_LEVEL, SharesLevel.CUSTOM.value()); customProperties.put(SHARES, "3000"); customProperties.put(LIMIT_IOPS, "50"); customProperties.put(DISK_MODE_INDEPENDENT, "true"); return customProperties; }
private HashMap<String, String> buildCustomPropertiesForCloneBoot(boolean isPersistent) { HashMap<String, String> customProperties = new HashMap<>(); customProperties.put(DISK_MODE_INDEPENDENT, "true"); customProperties.put(DISK_MODE_PERSISTENT, String.valueOf(isPersistent)); customProperties.put(PROVISION_TYPE, VirtualDiskType.THICK.value()); customProperties.put(SHARES_LEVEL, SharesLevel.NORMAL.value()); customProperties.put(LIMIT_IOPS, "50"); return customProperties; }
protected HashMap<String, String> buildCustomProperties() { HashMap<String, String> customProperties = new HashMap<>(); customProperties.put(DISK_MODE_PERSISTENT, "true"); customProperties.put(PROVISION_TYPE, VirtualDiskType.THIN.value()); customProperties.put(SHARES_LEVEL, SharesLevel.HIGH.value()); customProperties.put(LIMIT_IOPS, "100"); return customProperties; }
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); }
@Test public void deployOvfWithThickProvision() throws Throwable { Map<String, String> customProperties = new HashMap<>(); customProperties.put(PROVISION_TYPE, VirtualDiskType.THICK.value()); deployOvf(false, false, customProperties); }
@Test public void deployOvfWithThickEagerZeroedProvision() throws Throwable { Map<String, String> customProperties = new HashMap<>(); customProperties.put(PROVISION_TYPE, VirtualDiskType.EAGER_ZEROED_THICK.value()); deployOvf(false, false, customProperties); }
public void createDisk(String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, int controllerKey) throws Exception { createDisk(vmdkDatastorePath, VirtualDiskType.THIN, VirtualDiskMode.PERSISTENT, null, sizeInMb, morDs, controllerKey); }
private VirtualDeviceConfigSpec createFullCloneAndAttach(String sourcePath, DiskStateExpanded ds, String dir, VirtualDevice scsiController, int unitNumber, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws Exception { ManagedObjectReference diskManager = this.connection.getServiceContent() .getVirtualDiskManager(); String dsDirForDisk = getDatastorePathForDisk(ds, dir); // put full clone in the vm folder String destName = makePathToVmdkFile(ds.name, dsDirForDisk); // all ops are within a datacenter ManagedObjectReference sourceDc = this.ctx.datacenterMoRef; ManagedObjectReference destDc = sourceDc; Boolean force = true; // spec is not supported, should use null for now VirtualDiskSpec spec = null; ManagedObjectReference task = getVimPort() .copyVirtualDiskTask(diskManager, sourcePath, sourceDc, destName, destDc, spec, force); // wait for the disk to be copied TaskInfo taskInfo = waitTaskEnd(task); if (taskInfo.getState() == TaskInfoState.ERROR) { return VimUtils.rethrow(taskInfo.getError()); } 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(destName); backing.setDatastore(getDataStoreForDisk(ds, pbmSpec)); VirtualDisk disk = new VirtualDisk(); disk.setBacking(backing); disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds)); disk.setControllerKey(scsiController.getKey()); disk.setUnitNumber(unitNumber); fillInControllerUnitNumber(ds, unitNumber); disk.setKey(-1); VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec(); change.setDevice(disk); // Add storage policy spec if (pbmSpec != null) { pbmSpec.stream().forEach(sp -> { change.getProfile().add(sp); }); } change.setOperation(VirtualDeviceConfigSpecOperation.ADD); return change; }
private VirtualMachineRelocateSpecDiskLocator setProvisioningType(VirtualDisk vDisk, ManagedObjectReference datastore, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws InvalidPropertyFaultMsg, FinderException, RuntimeFaultFaultMsg { if (vDisk == null) { return null; } // If datastore for disk is null, if storage policy is configured pick the compatible // datastore from that. if (datastore == null) { ManagedObjectReference dsFromSp = getDatastoreFromStoragePolicy(this.connection, pbmSpec); datastore = dsFromSp == null ? getDatastore() : dsFromSp; } VirtualDiskFlatVer2BackingInfo flatBacking = (VirtualDiskFlatVer2BackingInfo) vDisk.getBacking(); VirtualDiskType provisioningType = getDiskProvisioningType(this.bootDisk); boolean wasThinProvision = flatBacking.isThinProvisioned(); Boolean wasEagerScrubbed = flatBacking.isEagerlyScrub() != null ? flatBacking.isEagerlyScrub() : false; if (provisioningType != null) { flatBacking.setThinProvisioned(provisioningType == VirtualDiskType.THIN); flatBacking.setEagerlyScrub(provisioningType == VirtualDiskType.EAGER_ZEROED_THICK); } VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator(); diskLocator.setDiskId(vDisk.getKey()); diskLocator.setDiskBackingInfo(flatBacking); diskLocator.setDatastore(datastore); Boolean isEagerScrub = flatBacking.isEagerlyScrub() != null ? flatBacking.isEagerlyScrub() : false; //If there is a change from thin to thick or vice-versa then we need to change the DiskMoveType //to MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING if (wasThinProvision != flatBacking.isThinProvisioned() || !wasEagerScrubbed.equals(isEagerScrub)) { diskLocator.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions .MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value()); } return diskLocator; }