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]); }
private static void setParentBackingInfo(VirtualDiskFlatVer2BackingInfo backingInfo, ManagedObjectReference morDs, String[] parentDatastorePathList) { VirtualDiskFlatVer2BackingInfo parentBacking = new VirtualDiskFlatVer2BackingInfo(); parentBacking.setDatastore(morDs); parentBacking.setDiskMode(VirtualDiskMode.PERSISTENT.value()); if (parentDatastorePathList.length > 1) { String[] nextDatastorePathList = new String[parentDatastorePathList.length - 1]; for (int i = 0; i < parentDatastorePathList.length - 1; i++) nextDatastorePathList[i] = parentDatastorePathList[i + 1]; setParentBackingInfo(parentBacking, morDs, nextDatastorePathList); } parentBacking.setFileName(parentDatastorePathList[0]); backingInfo.setParent(parentBacking); }
@SuppressWarnings("unchecked") private static void setParentBackingInfo(VirtualDiskFlatVer2BackingInfo backingInfo, Pair<String, ManagedObjectReference>[] parentDatastorePathList) { VirtualDiskFlatVer2BackingInfo parentBacking = new VirtualDiskFlatVer2BackingInfo(); parentBacking.setDatastore(parentDatastorePathList[0].second()); parentBacking.setDiskMode(VirtualDiskMode.PERSISTENT.value()); if (parentDatastorePathList.length > 1) { Pair<String, ManagedObjectReference>[] nextDatastorePathList = new Pair[parentDatastorePathList.length - 1]; for (int i = 0; i < parentDatastorePathList.length - 1; i++) nextDatastorePathList[i] = parentDatastorePathList[i + 1]; setParentBackingInfo(parentBacking, nextDatastorePathList); } parentBacking.setFileName(parentDatastorePathList[0].first()); backingInfo.setParent(parentBacking); }
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 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; }
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; }
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()); }
protected VirtualDisk reuseDisk(VirtualMachine machine, Integer scsiId, String fileName) { // SCSIコントローラを取得 VirtualSCSIController scsiController = getSCSIController(machine); // 仮想マシン自体のディスクを取得 VirtualDisk machineDisk = getVirtualDisk(machine, 0); VirtualDiskFlatVer2BackingInfo machieBackingInfo = VirtualDiskFlatVer2BackingInfo.class.cast(machineDisk .getBacking()); // VirtualDisk VirtualDisk disk = new VirtualDisk(); disk.setUnitNumber(scsiId); disk.setControllerKey(scsiController.getKey()); // VirtualDiskFlatVer2BackingInfo VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo(); backingInfo.setFileName(fileName); backingInfo.setDiskMode("persistent"); backingInfo.setSplit(false); backingInfo.setEagerlyScrub(false); backingInfo.setThinProvisioned(machieBackingInfo.getThinProvisioned()); backingInfo.setWriteThrough(false); disk.setBacking(backingInfo); // VirtualDeviceConnectInfo VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo(); connectInfo.setAllowGuestControl(false); connectInfo.setStartConnected(true); connectInfo.setConnected(true); disk.setConnectable(connectInfo); return disk; }
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); }
public void addHardDisk(String diskFilePath, VirtualDiskMode diskMode) throws Exception { VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); VirtualDeviceConfigSpec[] vdiskSpecArray = {diskSpec}; vmConfigSpec.setDeviceChange(vdiskSpecArray); VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); diskfileBacking.setFileName(diskFilePath); diskfileBacking.setDiskMode(diskMode.toString()); VirtualSCSIController scsiController = getFirstAvailableController(VirtualSCSIController.class); int unitNumber = getFirstFreeUnitNumberForController(scsiController); VirtualDisk disk = new VirtualDisk(); disk.setControllerKey(scsiController.key); disk.setUnitNumber(unitNumber); disk.setBacking(diskfileBacking); //Unlike required by API ref, the capacityKB is optional. So skip setCapacityInKB() method. disk.setKey(-100); diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); diskSpec.setDevice(disk); Task task = vm.reconfigVM_Task(vmConfigSpec); task.waitForTask(200, 100); }
public String getVmdkFileBaseName(VirtualDisk disk) throws Exception { String vmdkFileBaseName = null; VirtualDeviceBackingInfo backingInfo = disk.getBacking(); if(backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName()); vmdkFileBaseName = dsBackingFile.getFileBaseName(); } return vmdkFileBaseName; }
protected void verifyDiskProperties(ComputeService.ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualDisk vd = fetchVirtualDisk(vm, get); assertEquals(SharesLevel.CUSTOM.value(), vd.getStorageIOAllocation().getShares().getLevel().value()); Long limitIops = 50L; assertEquals(limitIops, vd.getStorageIOAllocation().getLimit()); VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) vd.getBacking(); assertTrue(backing.isThinProvisioned()); }
private static ArrayList<Integer> getIndependentVirtualDiskKeys(VirtualMachine vm) throws Exception { ArrayList<Integer> diskKeys = Lists.newArrayList(); VirtualDevice[] devices = (VirtualDevice[]) vm.getPropertyByPath("config.hardware.device"); for (int i = 0; i < devices.length; i++) { if (devices[i] instanceof VirtualDisk) { VirtualDisk vDisk = (VirtualDisk) devices[i]; String diskMode = ""; VirtualDeviceBackingInfo vdbi = vDisk.getBacking(); if (vdbi instanceof VirtualDiskFlatVer1BackingInfo) { diskMode = ((VirtualDiskFlatVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskFlatVer2BackingInfo) { diskMode = ((VirtualDiskFlatVer2BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskRawDiskMappingVer1BackingInfo) { diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskSparseVer1BackingInfo) { diskMode = ((VirtualDiskSparseVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskSparseVer2BackingInfo) { diskMode = ((VirtualDiskSparseVer2BackingInfo) vdbi).getDiskMode(); } if (diskMode.indexOf("independent") != -1) { diskKeys.add(vDisk.getKey()); } } } return diskKeys; }
protected VirtualDisk newDisk(VirtualMachine machine, Integer scsiId, Integer size) { // SCSIコントローラを取得 VirtualSCSIController scsiController = getSCSIController(machine); // 仮想マシン自体のディスクを取得 VirtualDisk machineDisk = getVirtualDisk(machine, 0); VirtualDiskFlatVer2BackingInfo machieBackingInfo = VirtualDiskFlatVer2BackingInfo.class.cast(machineDisk .getBacking()); // VirtualDisk VirtualDisk disk = new VirtualDisk(); disk.setUnitNumber(scsiId); disk.setCapacityInKB(size * 1024L * 1024L); disk.setControllerKey(scsiController.getKey()); // VirtualDiskFlatVer2BackingInfo VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo(); backingInfo.setDatastore(null); backingInfo.setFileName(""); backingInfo.setDiskMode("persistent"); backingInfo.setSplit(false); backingInfo.setEagerlyScrub(false); backingInfo.setThinProvisioned(machieBackingInfo.getThinProvisioned()); backingInfo.setWriteThrough(false); disk.setBacking(backingInfo); // VirtualDeviceConnectInfo VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo(); connectInfo.setAllowGuestControl(false); connectInfo.setStartConnected(true); connectInfo.setConnected(true); disk.setConnectable(connectInfo); return disk; }
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception { VirtualDisk disk = new VirtualDisk(); VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo(); backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value()); backingInfo.setThinProvisioned(true); backingInfo.setEagerlyScrub(false); backingInfo.setDatastore(morDs); backingInfo.setFileName(vmdkDatastorePath); disk.setBacking(backingInfo); int ideControllerKey = vmMo.getIDEDeviceControllerKey(); if (controllerKey < 0) controllerKey = ideControllerKey; if (deviceNumber < 0) { deviceNumber = vmMo.getNextDeviceNumber(controllerKey); } disk.setControllerKey(controllerKey); disk.setKey(-contextNumber); disk.setUnitNumber(deviceNumber); disk.setCapacityInKB(sizeInMb * 1024); VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo(); connectInfo.setConnected(true); connectInfo.setStartConnected(true); disk.setConnectable(connectInfo); return disk; }
public static ManagedObjectReference getDiskDeviceDatastore(VirtualDisk diskDevice) throws Exception { VirtualDeviceBackingInfo backingInfo = diskDevice.getBacking(); assert (backingInfo instanceof VirtualDiskFlatVer2BackingInfo); return ((VirtualDiskFlatVer2BackingInfo)backingInfo).getDatastore(); }
public String getAbsoluteVmdkFile(VirtualDisk disk) { String vmdkAbsFile = null; VirtualDeviceBackingInfo backingInfo = disk.getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; vmdkAbsFile = diskBackingInfo.getFileName(); } return vmdkAbsFile; }
private List<String> getManagedDatastoreNamesFromVirtualDisks(List<VirtualDisk> virtualDisks) { List<String> managedDatastoreNames = new ArrayList<>(); if (virtualDisks != null) { for (VirtualDisk virtualDisk : virtualDisks) { if (virtualDisk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)virtualDisk.getBacking(); String path = backingInfo.getFileName(); String search = "[-"; int index = path.indexOf(search); if (index > -1) { path = path.substring(index + search.length()); String search2 = "-0]"; index = path.lastIndexOf(search2); if (index > -1) { path = path.substring(0, index); if (path.startsWith("iqn.")) { managedDatastoreNames.add("-" + path + "-0"); } } } } } } return managedDatastoreNames; }
private static ArrayList<Integer> getIndependentVirtualDiskKeys(VirtualMachine vm) throws Exception { ArrayList<Integer> _diskKeys = new ArrayList<Integer>(); VirtualDevice[] devices = (VirtualDevice[]) vm.getPropertyByPath("config.hardware.device"); for(int i=0; i<devices.length; i++) { if(devices[i] instanceof VirtualDisk) { VirtualDisk vDisk = (VirtualDisk) devices[i]; String diskMode = ""; VirtualDeviceBackingInfo vdbi = vDisk.getBacking(); if(vdbi instanceof VirtualDiskFlatVer1BackingInfo) { diskMode = ((VirtualDiskFlatVer1BackingInfo) vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskFlatVer2BackingInfo) { diskMode = ((VirtualDiskFlatVer2BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskRawDiskMappingVer1BackingInfo) { diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskSparseVer1BackingInfo) { diskMode = ((VirtualDiskSparseVer1BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskSparseVer2BackingInfo) { diskMode = ((VirtualDiskSparseVer2BackingInfo)vdbi).getDiskMode(); } if(diskMode.indexOf("independent") != -1) { _diskKeys.add(vDisk.getKey()); } } } return _diskKeys; }
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; }
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; }
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) throws Exception { if(s_logger.isTraceEnabled()) s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue()); int controllerKey = 0; int unitNumber = 0; if (DiskControllerType.getType(diskController) == DiskControllerType.ide) { // IDE virtual disk cannot be added if VM is running if (getPowerState() == VirtualMachinePowerState.POWERED_ON) { throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running."); } // Get next available unit number and controller key int ideDeviceCount = getNumberOfIDEDevices(); if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) { throw new Exception("Maximum limit of devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached."); } controllerKey = getIDEControllerKey(ideDeviceCount); unitNumber = getFreeUnitNumberOnIDEController(controllerKey); } else { controllerKey = getScsiDiskControllerKey(diskController); unitNumber = -1; } synchronized (_mor.getValue().intern()) { VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1); controllerKey = newDisk.getControllerKey(); unitNumber = newDisk.getUnitNumber(); VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo)newDisk.getBacking(); String vmdkFileName = backingInfo.getFileName(); DiskControllerType diskControllerType = DiskControllerType.getType(diskController); VmdkAdapterType vmdkAdapterType = VmdkAdapterType.getAdapterType(diskControllerType); if (vmdkAdapterType == VmdkAdapterType.none) { String message = "Failed to attach disk due to invalid vmdk adapter type for vmdk file [" + vmdkFileName + "] with controller : " + diskControllerType; s_logger.debug(message); throw new Exception(message); } updateVmdkAdapter(vmdkFileName, vmdkAdapterType.toString()); VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); deviceConfigSpec.setDevice(newDisk); deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD); 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 - attachDisk() done(failed)"); throw new Exception("Failed to attach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask)); } _context.waitForTaskProgressDone(morTask); } if(s_logger.isTraceEnabled()) s_logger.trace("vCenter API trace - attachDisk() done(successfully)"); }
public List<String> detachAllDisksExcept(String vmdkBaseName, String deviceBusName) throws Exception { List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec(); List<String> detachedDiskFiles = new ArrayList<String>(); for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)device.getBacking(); DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName()); String backingBaseName = dsBackingFile.getFileBaseName(); String deviceNumbering = getDeviceBusName(devices, device); if (backingBaseName.equalsIgnoreCase(vmdkBaseName) || (deviceBusName != null && deviceBusName.equals(deviceNumbering))) { continue; } else { s_logger.info("Detach " + diskBackingInfo.getFileName() + " from " + getName()); detachedDiskFiles.add(diskBackingInfo.getFileName()); deviceConfigSpec.setDevice(device); deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE); reConfigSpec.getDeviceChange().add(deviceConfigSpec); } } } if (detachedDiskFiles.size() > 0) { ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec); boolean result = _context.getVimClient().waitForTask(morTask); if (result) { _context.waitForTaskProgressDone(morTask); } else { s_logger.warn("Unable to reconfigure the VM to detach disks"); throw new Exception("Unable to reconfigure the VM to detach disks"); } } return detachedDiskFiles; }
@SuppressWarnings("unchecked") public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain, int deviceNumber, int contextNumber) throws Exception { assert (vmdkDatastorePathChain != null); assert (vmdkDatastorePathChain.length >= 1); VirtualDisk disk = new VirtualDisk(); VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo(); backingInfo.setDatastore(vmdkDatastorePathChain[0].second()); backingInfo.setFileName(vmdkDatastorePathChain[0].first()); backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value()); if (vmdkDatastorePathChain.length > 1) { Pair<String, ManagedObjectReference>[] parentDisks = new Pair[vmdkDatastorePathChain.length - 1]; for (int i = 0; i < vmdkDatastorePathChain.length - 1; i++) parentDisks[i] = vmdkDatastorePathChain[i + 1]; setParentBackingInfo(backingInfo, parentDisks); } disk.setBacking(backingInfo); int ideControllerKey = vmMo.getIDEDeviceControllerKey(); if (controllerKey < 0) controllerKey = ideControllerKey; if (deviceNumber < 0) { deviceNumber = vmMo.getNextDeviceNumber(controllerKey); } disk.setControllerKey(controllerKey); disk.setKey(-contextNumber); disk.setUnitNumber(deviceNumber); VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo(); connectInfo.setConnected(true); connectInfo.setStartConnected(true); disk.setConnectable(connectInfo); return disk; }