/** * Reconfigures VMware system disks and data disks. */ public void reconfigureDisks(VirtualMachineConfigSpec vmConfigSpec, ManagedObjectReference vmwInstance) throws Exception { logger.debug(""); long systemDiskMB = (long) paramHandler.getConfigDiskSpaceMB(); VirtualMachineConfigInfo configSpec = (VirtualMachineConfigInfo) vmw .getServiceUtil().getDynamicProperty(vmwInstance, "config"); List<VirtualDevice> devices = configSpec.getHardware().getDevice(); VirtualDisk vdSystemDisk = getVMSystemDisk(devices, configSpec.getName()); configureSystemDisk(vmConfigSpec, systemDiskMB, vdSystemDisk); configureDataDisks(vmConfigSpec, devices, vdSystemDisk); }
private int getDataDiskKey() throws Exception { List<VirtualDevice> devices = configSpec.getHardware().getDevice(); int countDisks = 0; int key = -1; for (VirtualDevice vdInfo : devices) { if (vdInfo instanceof VirtualDisk) { countDisks++; if (countDisks == 2) { key = ((VirtualDisk) vdInfo).getKey(); break; } } } return key; }
private String getDiskSizeInGB(int disk) throws Exception { String size = ""; List<VirtualDevice> devices = configSpec.getHardware().getDevice(); int countDisks = 0; for (VirtualDevice vdInfo : devices) { if (vdInfo instanceof VirtualDisk) { countDisks++; if (countDisks == disk) { long gigabyte = ((VirtualDisk) vdInfo).getCapacityInKB() / 1024 / 1024; size = Long.toString(gigabyte); break; } } } return size; }
/** * Get customization config spec for all the image disks if any */ private List<VirtualDeviceConfigSpec> getCustomizationConfigSpecs(ArrayOfVirtualDevice devices, List<DiskStateExpanded> diskStates) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { List<VirtualDeviceConfigSpec> specs = new ArrayList<>(); List<VirtualDisk> virtualDisks = devices.getVirtualDevice().stream() .filter(d -> d instanceof VirtualDisk) .map(d -> (VirtualDisk) d) .collect(Collectors.toList()); for (VirtualDisk vd : virtualDisks) { VirtualDeviceConfigSpec diskSpec = getBootDiskCustomizeConfigSpec( findMatchingImageDiskState(vd, diskStates), vd); if (diskSpec != null) { specs.add(diskSpec); } } return specs; }
public void detachDiskFromVM() throws Exception { ArrayOfVirtualDevice devices = this.get .entityProp(this.vm, VimPath.vm_config_hardware_device); VirtualDisk vd = (VirtualDisk) findMatchingVirtualDevice(getListOfVirtualDisk(devices)); if (vd == null) { throw new IllegalStateException( String.format( "Matching Virtual Disk is not for disk %s.", this.diskState.documentSelfLink)); } // Detach the disk from VM. VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE); deviceConfigSpec.setDevice(vd); VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec(); spec.getDeviceChange().add(deviceConfigSpec); ManagedObjectReference reconfigureTask = getVimPort().reconfigVMTask(this.vm, spec); TaskInfo info = VimUtils.waitTaskEnd(this.connection, reconfigureTask); if (info.getState() == TaskInfoState.ERROR) { VimUtils.rethrow(info.getError()); } }
/** * 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.getBacking() instanceof VirtualDeviceFileBackingInfo) { handleVirtualDiskUpdate(matchedDs, (VirtualDisk) disk, diskLinks, ctx); } else if (disk instanceof VirtualCdrom) { handleVirtualDeviceUpdate(matchedDs, DiskType.CDROM, disk, diskLinks, ctx); } else if (disk instanceof VirtualFloppy) { handleVirtualDeviceUpdate(matchedDs, DiskType.FLOPPY, disk, diskLinks, ctx); } else { continue; } } state.diskLinks = diskLinks; }
protected VirtualDisk getVirtualDisk(VirtualMachine machine, Integer scsiId) { // SCSIコントローラを取得 VirtualSCSIController scsiController = getSCSIController(machine); // SCSIコントローラとSCSI IDが一致するディスクを取得 VirtualDisk disk = null; for (VirtualDevice device : machine.getConfig().getHardware().getDevice()) { if (device instanceof VirtualDisk) { VirtualDisk tmpDisk = VirtualDisk.class.cast(device); if (tmpDisk.getControllerKey() != null && tmpDisk.getControllerKey().equals(scsiController.getKey())) { if (tmpDisk.getUnitNumber() != null && tmpDisk.getUnitNumber().equals(scsiId)) { disk = tmpDisk; break; } } } } if (disk == null) { // VirtualDiskが見つからない場合 throw new AutoException("EPROCESS-000518", scsiId); } return disk; }
public VirtualDisk findHardDisk(String diskName) { VirtualDevice[] devices = getAllVirtualDevices(); for(int i=0; i<devices.length; i++) { if(devices[i] instanceof VirtualDisk) { VirtualDisk vDisk = (VirtualDisk)devices[i]; if(diskName.equalsIgnoreCase(vDisk.getDeviceInfo().getLabel())) { return vDisk; } } } return null; }
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 VirtualDisk createVirtualDisk(String volumeName, int controllerKey, int unitNumber, int key, VmInputs vmInputs, String parameter) throws Exception { VirtualDiskFlatVer2BackingInfo diskFileBacking = new VirtualDiskFlatVer2BackingInfo(); diskFileBacking.setFileName(volumeName); if (Operation.CREATE.toString().equalsIgnoreCase(parameter)) { diskFileBacking.setDiskMode(DiskMode.PERSISTENT.toString()); } else { diskFileBacking.setDiskMode(DiskMode.getValue(vmInputs.getVmDiskMode())); } VirtualDisk disk = new VirtualDisk(); disk.setBacking(diskFileBacking); disk.setControllerKey(controllerKey); disk.setUnitNumber(unitNumber); disk.setKey(key); disk.setCapacityInKB(vmInputs.getLongVmDiskSize() * DISK_AMOUNT_MULTIPLIER); return disk; }
public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws Exception { List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber()); VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; String deviceNumbering = getDeviceBusName(devices, device); if (deviceNumbering.equals(deviceBusName)) return diskBackingInfo.getFileName(); } } } } return null; }
public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception { VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder(); List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; while (diskBackingInfo != null) { String deviceBusName = getDeviceBusName(devices, device); builder.addDisk(deviceBusName, diskBackingInfo.getFileName()); diskBackingInfo = diskBackingInfo.getParent(); } } } } } return builder; }
public List<Pair<Integer, ManagedObjectReference>> getAllDiskDatastores() throws Exception { List<Pair<Integer, ManagedObjectReference>> disks = new ArrayList<Pair<Integer, ManagedObjectReference>>(); List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; disks.add(new Pair<Integer, ManagedObjectReference>(new Integer(device.getKey()), diskBackingInfo.getDatastore())); } } } } return disks; }
public VirtualDisk[] getAllIndependentDiskDevice() throws Exception { List<VirtualDisk> independentDisks = new ArrayList<VirtualDisk>(); VirtualDisk[] allDisks = getAllDiskDevice(); if (allDisks.length > 0) { for (VirtualDisk disk : allDisks) { String diskMode = ""; if (disk.getBacking() instanceof VirtualDiskFlatVer1BackingInfo) { diskMode = ((VirtualDiskFlatVer1BackingInfo)disk.getBacking()).getDiskMode(); } else if (disk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) { diskMode = ((VirtualDiskFlatVer2BackingInfo)disk.getBacking()).getDiskMode(); } else if (disk.getBacking() instanceof VirtualDiskRawDiskMappingVer1BackingInfo) { diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo)disk.getBacking()).getDiskMode(); } else if (disk.getBacking() instanceof VirtualDiskSparseVer1BackingInfo) { diskMode = ((VirtualDiskSparseVer1BackingInfo)disk.getBacking()).getDiskMode(); } else if (disk.getBacking() instanceof VirtualDiskSparseVer2BackingInfo) { diskMode = ((VirtualDiskSparseVer2BackingInfo)disk.getBacking()).getDiskMode(); } if (diskMode.indexOf("independent") != -1) { independentDisks.add(disk); } } } return independentDisks.toArray(new VirtualDisk[0]); }
public int getFreeUnitNumberOnIDEController(int controllerKey) throws Exception { int freeUnitNumber = 0; List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient(). getDynamicProperty(_mor, "config.hardware.device"); int deviceCount = 0; int ideDeviceUnitNumber = -1; if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk && (controllerKey == device.getControllerKey())) { deviceCount++; ideDeviceUnitNumber = device.getUnitNumber(); } } } if (deviceCount == 1) { if (ideDeviceUnitNumber == 0) { freeUnitNumber = 1; } // else freeUnitNumber is already initialized to 0 } else if (deviceCount == 2) { throw new Exception("IDE controller with key [" + controllerKey + "] already has 2 device attached. Cannot attach more than the limit of 2."); } return freeUnitNumber; }
public static VirtualMachineDiskInfoBuilder getDiskInfoBuilder(List<VirtualDevice> devices) throws Exception { VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder(); if (devices != null) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDisk virtualDisk = (VirtualDisk)device; VirtualDeviceBackingInfo backingInfo = virtualDisk.getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; String deviceBusName = VMwareUtil.getDeviceBusName(devices, virtualDisk); while (diskBackingInfo != null) { builder.addDisk(deviceBusName, diskBackingInfo.getFileName()); diskBackingInfo = diskBackingInfo.getParent(); } } } } } return builder; }
private void updateDiskConfiguration(VirtualMachineConfigSpec vmConfigSpec, List<VirtualDevice> devices, int vdKey, long newDiskSpace) throws Exception { VirtualDisk vdDataDisk = findDataDisk(devices, vdKey); if (vdDataDisk != null && newDiskSpace > vdDataDisk.getCapacityInKB()) { logger.info("reconfigureDisks() extend data disk #" + vdKey + " space to " + newDiskSpace + " (" + vdDataDisk.getDeviceInfo().getLabel() + ")"); if (newDiskSpace < vdDataDisk.getCapacityInKB()) { logger.error("Cannot reduce size of data disk " + vdDataDisk.getDeviceInfo().getLabel()); logger.error("Current disk space: " + vdDataDisk.getCapacityInKB() + " new disk space: " + newDiskSpace); throw new Exception(Messages .getAll("error_invalid_diskspacereduction").get(0) .getText()); } else if (newDiskSpace > vdDataDisk.getCapacityInKB()) { vdDataDisk.setCapacityInKB(newDiskSpace); VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec(); vmDeviceSpec .setOperation(VirtualDeviceConfigSpecOperation.EDIT); vmDeviceSpec.setDevice(vdDataDisk); vmConfigSpec.getDeviceChange().add(vmDeviceSpec); } else { logger.debug("Data disk size has not been changed. " + newDiskSpace + " KB"); } } }
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; }
private void configureSystemDisk(VirtualMachineConfigSpec vmConfigSpec, long systemDiskMB, VirtualDisk vdSystemDisk) throws Exception { if (systemDiskMB > 0) { long newDiskSpace = systemDiskMB * 1024; if (newDiskSpace < vdSystemDisk.getCapacityInKB()) { logger.error("Cannot reduce size of system disk \"" + vdSystemDisk.getDeviceInfo().getLabel() + "\""); logger.error("Current disk size: " + vdSystemDisk.getCapacityInKB() + " new disk space: " + newDiskSpace); throw new Exception(Messages .getAll("error_invalid_diskspacereduction").get(0) .getText()); } else if (newDiskSpace > vdSystemDisk.getCapacityInKB()) { logger.info("reconfigureDisks() extend system disk space to " + newDiskSpace + " (" + vdSystemDisk.getDeviceInfo().getLabel() + ")"); vdSystemDisk.setCapacityInKB(newDiskSpace); VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec(); vmDeviceSpec .setOperation(VirtualDeviceConfigSpecOperation.EDIT); vmDeviceSpec.setDevice(vdSystemDisk); vmConfigSpec.getDeviceChange().add(vmDeviceSpec); } else { logger.debug("System disk size has not been changed. " + newDiskSpace + " KB"); } } else { logger.error("Reconfiguration of system disk not possible because system disk size is not defined."); } }
public String getTotalDiskSizeInMB() throws Exception { long megabyte = 0; List<VirtualDevice> devices = configSpec.getHardware().getDevice(); for (VirtualDevice vdInfo : devices) { if (vdInfo instanceof VirtualDisk) { megabyte = megabyte + (((VirtualDisk) vdInfo).getCapacityInKB() / 1024); } } return Long.toString(megabyte); }
public List<VirtualDevice> getDisks() { ArrayOfVirtualDevice dev = (ArrayOfVirtualDevice) getOrDefault( VimPath.vm_config_hardware_device, null); if (dev == null) { return Collections.emptyList(); } return dev.getVirtualDevice().stream() .filter(d -> d instanceof VirtualDisk || d instanceof VirtualCdrom || d instanceof VirtualFloppy) .collect(Collectors.toList()); }
/** * 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; }
/** * Capture virtual disk attributes in the disk state for reference. */ public static void updateDiskStateFromVirtualDisk(VirtualDisk vd, DiskService.DiskState disk) { disk.status = DiskService.DiskStatus.ATTACHED; disk.id = vd.getDiskObjectId(); CustomProperties.of(disk) .put(PROVIDER_DISK_UNIQUE_ID, vd.getDeviceInfo().getLabel()); }
public static DiskService.DiskStateExpanded findMatchingDiskState(VirtualDevice vd, List<DiskService.DiskStateExpanded> disks) { // Step 1: Match if there are matching bootOrder number with Unit number of disk // Step 2: If not, then find a custom property to match the bootOrder with the unit number // Step 3: If no match found then this is the new disk so return null if (disks == null || disks.isEmpty()) { return null; } return disks.stream() .filter(ds -> { if (vd instanceof VirtualDisk && ds.type == DiskService.DiskType.HDD) { return true; } else if (vd instanceof VirtualCdrom && ds.type == DiskService.DiskType.CDROM) { return true; } else if (vd instanceof VirtualFloppy && ds.type == DiskService.DiskType.FLOPPY) { return true; } return false; }) .filter(ds -> { boolean isFound = (ds.bootOrder != null && (ds.bootOrder - 1) == vd .getUnitNumber()); if (!isFound) { // Now check custom properties for controller unit number if (ds.customProperties != null && ds.customProperties.get (DISK_CONTROLLER_NUMBER) != null) { int unitNumber = Integer.parseInt(ds.customProperties.get (DISK_CONTROLLER_NUMBER)); isFound = unitNumber == vd.getUnitNumber(); } } return isFound; }).findFirst().orElse(null); }
/** * Find a matching DiskState object for the virtual disk */ private DiskStateExpanded findMatchingImageDiskState(VirtualDisk virtualDisk, List<DiskStateExpanded> diskStates) { if (diskStates == null || diskStates.isEmpty()) { return null; } // Filter the disk state that matches the virtual disk scsi number. If not then default // it to the properties of the boot disk return diskStates.stream().filter(ds -> ds.bootOrder != null && (ds.bootOrder - 1) == virtualDisk.getUnitNumber()).findFirst().orElse(this.bootDisk); }
/** * Construct VM config spec for boot disk size customization, if the user defined value is * greater then the existing size of the disk */ private VirtualDeviceConfigSpec getBootDiskCustomizeConfigSpec(DiskStateExpanded disk, VirtualDisk virtualDisk) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, FinderException { if (disk != null && virtualDisk != null) { // Resize happens if the new size is more than the existing disk size, other storage // related attributes will be applied the disk. VirtualDeviceConfigSpec hdd = resizeHdd(virtualDisk, disk); return hdd; } return null; }
private VirtualDeviceConfigSpec resizeHdd(VirtualDisk sysdisk, DiskStateExpanded ds) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualDiskFlatVer2BackingInfo oldbacking = (VirtualDiskFlatVer2BackingInfo) sysdisk .getBacking(); VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo(); backing.setDiskMode(getDiskMode(ds)); backing.setThinProvisioned(oldbacking.isThinProvisioned()); backing.setEagerlyScrub(oldbacking.isEagerlyScrub()); backing.setFileName(oldbacking.getFileName()); VirtualDisk disk = new VirtualDisk(); if (toKb(ds.capacityMBytes) > sysdisk.getCapacityInKB()) { disk.setCapacityInKB(toKb(ds.capacityMBytes)); } else { disk.setCapacityInKB(sysdisk.getCapacityInKB()); } disk.setBacking(backing); disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds)); disk.setControllerKey(sysdisk.getControllerKey()); disk.setUnitNumber(sysdisk.getUnitNumber()); fillInControllerUnitNumber(ds, sysdisk.getUnitNumber()); disk.setKey(sysdisk.getKey()); VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec(); change.setDevice(disk); change.setOperation(VirtualDeviceConfigSpecOperation.EDIT); return change; }
/** * Process VirtualDisk and update the details in the diskLinks of the provisioned compute */ private void handleVirtualDiskUpdate(DiskStateExpanded matchedDs, VirtualDisk disk, ArrayList<String> diskLinks, ProvisionContext ctx) { VirtualDeviceFileBackingInfo backing = (VirtualDeviceFileBackingInfo) disk.getBacking(); if (matchedDs == null) { // This is the new disk, hence add it to the list DiskState ds = new DiskState(); ds.documentSelfLink = UriUtils.buildUriPath( DiskService.FACTORY_LINK, getHost().nextUUID()); ds.name = disk.getDeviceInfo().getLabel(); ds.creationTimeMicros = Utils.getNowMicrosUtc(); ds.type = DiskType.HDD; ds.regionId = ctx.parent.description.regionId; ds.capacityMBytes = disk.getCapacityInKB() / 1024; ds.sourceImageReference = VimUtils.datastorePathToUri(backing.getFileName()); updateDiskStateFromVirtualDisk(disk, ds); if (disk.getStorageIOAllocation() != null) { StorageIOAllocationInfo storageInfo = disk.getStorageIOAllocation(); CustomProperties.of(ds) .put(SHARES, storageInfo.getShares().getShares()) .put(LIMIT_IOPS, storageInfo.getLimit()) .put(SHARES_LEVEL, storageInfo.getShares().getLevel().value()); } fillInControllerUnitNumber(ds, disk.getUnitNumber()); createDiskOnDemand(ds); diskLinks.add(ds.documentSelfLink); } else { // This is known disk, hence update with the provisioned attributes. matchedDs.sourceImageReference = VimUtils.datastorePathToUri(backing.getFileName()); updateDiskStateFromVirtualDisk(disk, matchedDs); createDiskPatch(matchedDs); } }
protected void verifyDiskProperties(ComputeService.ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualDisk vd = fetchVirtualDisk(vm, get); assertEquals(SharesLevel.HIGH.value(), vd.getStorageIOAllocation().getShares() .getLevel().value()); int shares = 2000; assertEquals(shares, vd.getStorageIOAllocation().getShares().getShares()); Long limitIops = 100L; assertEquals(limitIops, vd.getStorageIOAllocation().getLimit()); VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) vd.getBacking(); assertTrue(backing.isThinProvisioned()); }
/** * Verify that the boot disk is resized. */ protected void verifyDiskSize(ComputeState vm, GetMoRef get, long size) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualDisk vd = fetchVirtualDisk(vm, get); long diskSizeinMb = vd.getCapacityInBytes() / 1024 / 1024; assertEquals(size, diskSizeinMb); }
/** * Get the reference to Virtual Disk from VM. */ protected VirtualDisk fetchVirtualDisk(ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference vmMoRef = CustomProperties.of(vm).getMoRef(MOREF); ArrayOfVirtualDevice devices = get.entityProp(vmMoRef, VimPath.vm_config_hardware_device); VirtualDisk vd = devices.getVirtualDevice().stream() .filter(d -> d instanceof VirtualDisk) .map(d -> (VirtualDisk) d).findFirst().orElse(null); return vd; }
/** * Get the reference to Virtual Disk from VM. */ protected List<VirtualDisk> fetchAllVirtualDisks(ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference vmMoRef = CustomProperties.of(vm).getMoRef(MOREF); ArrayOfVirtualDevice devices = get.entityProp(vmMoRef, VimPath.vm_config_hardware_device); return devices.getVirtualDevice().stream() .filter(d -> d instanceof VirtualDisk) .map(d -> (VirtualDisk) d).collect(Collectors.toList()); }
@Test public void deployFromLibraryWithAdditionalDisks() throws Throwable { ComputeService.ComputeState vm = provisionVMAndGetState(true, true); try { if (vm == null) { return; } // Verify that the disk is resized BasicConnection connection = createConnection(); GetMoRef get = new GetMoRef(connection); List<VirtualDisk> virtualDisks = fetchAllVirtualDisks(vm, get); assertEquals(3, virtualDisks.size()); assertEquals(3, vm.diskLinks.size()); List<DeferredResult<DiskService.DiskState>> disks = vm.diskLinks.stream() .map(link -> { Operation getOp = Operation .createGet(this.host, link) .setReferer(this.host.getReferer()); return this.host.sendWithDeferredResult(getOp, DiskService.DiskState.class); }).collect(Collectors.toList()); DeferredResult.allOf(disks).thenAccept(diskStates -> diskStates.stream().forEach(ds -> { assertNotNull(ds.customProperties); assertNotNull(ds.sourceImageReference); assertNotNull(ds.customProperties.get(PROVIDER_DISK_UNIQUE_ID)); })); } finally { if (vm != null) { deleteVmAndWait(vm); } } }