public FakeVirtualMachine addVM(String name, boolean isRunning, VirtualMachineCloneSpec spec){ final FakeVirtualMachine vm = new FakeVirtualMachine(name, name.contains("template"), isRunning); putVM(name, vm); if (spec != null && spec.getLocation()!= null && VirtualMachineRelocateDiskMoveOptions.createNewChildDiskBacking.name().equals(spec.getLocation().getDiskMoveType())){ //((FakeVirtualMachine)vm).set } if (spec != null && spec.getConfig() != null) { final OptionValue[] extraConfig = spec.getConfig().getExtraConfig(); if (extraConfig != null) { for (OptionValue optionValue : extraConfig) { vm.addCustomParam(optionValue.getKey(), String.valueOf(optionValue.getValue())); } } } return vm; }
private VirtualMachineRelocateDiskMoveOptions computeDiskMoveType() { String strategy = CustomProperties.of(this.ctx.child).getString(CustomProperties.CLONE_STRATEGY); if (CLONE_STRATEGY_FULL.equals(strategy)) { return VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING; } else if (CLONE_STRATEGY_LINKED.equals(strategy)) { return VirtualMachineRelocateDiskMoveOptions.CREATE_NEW_CHILD_DISK_BACKING; } else { logger.warn("Unknown clone strategy {}, defaulting to MOVE_CHILD_MOST_DISK_BACKING", strategy); return VirtualMachineRelocateDiskMoveOptions.MOVE_CHILD_MOST_DISK_BACKING; } }
private VirtualMachineRelocateSpec configureRelocateSpec(ResourcePool resourcePool, Datastore datastore, VirtualMachine master) throws Exception, InvalidProperty, RuntimeFault, RemoteException { VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec(); if (cloningStrategy.equals("linked")) { ArrayList<Integer> diskKeys = getIndependentVirtualDiskKeys(master); if (diskKeys.size() > 0) { Datastore[] dss = master.getDatastores(); VirtualMachineRelocateSpecDiskLocator[] diskLocator = new VirtualMachineRelocateSpecDiskLocator[diskKeys.size()]; int count = 0; for (Integer key : diskKeys) { diskLocator[count] = new VirtualMachineRelocateSpecDiskLocator(); diskLocator[count].setDatastore(dss[0].getMOR()); diskLocator[count] .setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.moveAllDiskBackingsAndDisallowSharing .toString()); diskLocator[count].setDiskId(key); count = count + 1; } rSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.createNewChildDiskBacking.toString()); rSpec.setDisk(diskLocator); } else { rSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.createNewChildDiskBacking.toString()); } } else if (cloningStrategy.equals("full")) { rSpec.setDatastore(datastore.getMOR()); rSpec.setPool(resourcePool.getMOR()); //rSpec.setHost(); } else throw new Exception(String.format("Cloning strategy %s not supported", cloningStrategy)); return rSpec; }
public ComputeState createInstanceFromSnapshot() throws Exception { String message = ""; if (this.ctx.snapshotMoRef == null) { message = String.format("No MoRef found for the specified snapshot %s", this.ctx.child.documentSelfLink); logger.error(message); this.ctx.fail(new IllegalStateException(message)); } if (this.ctx.referenceComputeMoRef == null) { if (this.ctx.snapshotMoRef == null) { message = String.format("No MoRef found for the reference compute for linkedclone creation for %s.", this.ctx.child.documentSelfLink); logger.error(message); this.ctx.fail(new IllegalStateException(message)); } } VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec(); relocateSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions .CREATE_NEW_CHILD_DISK_BACKING.value()); VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setPowerOn(false); cloneSpec.setLocation(relocateSpec); cloneSpec.setSnapshot(this.ctx.snapshotMoRef); cloneSpec.setTemplate(false); ManagedObjectReference folder = getVmFolder(); String displayName = this.ctx.child.name; ManagedObjectReference linkedCloneTask = getVimPort() .cloneVMTask(this.ctx.referenceComputeMoRef, folder, displayName, cloneSpec); TaskInfo info = waitTaskEnd(linkedCloneTask); if (info.getState() == TaskInfoState.ERROR) { MethodFault fault = info.getError().getFault(); if (fault instanceof FileAlreadyExists) { // a .vmx file already exists, assume someone won the race to create the vm return null; } else { return VimUtils.rethrow(info.getError()); } } ManagedObjectReference clonedVM = (ManagedObjectReference) info.getResult(); if (clonedVM == null) { // vm was created by someone else return null; } // store reference to created vm for further processing this.vm = clonedVM; customizeAfterClone(); ComputeState state = new ComputeState(); state.resourcePoolLink = VimUtils .firstNonNull(this.ctx.child.resourcePoolLink, this.ctx.parent.resourcePoolLink); return state; }
public boolean createLinkedClone(String cloneName, ManagedObjectReference morBaseSnapshot, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs) throws Exception { assert (morBaseSnapshot != null); assert (morFolder != null); assert (morResourcePool != null); assert (morDs != null); VirtualDisk[] independentDisks = getAllIndependentDiskDevice(); VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec(); if (independentDisks.length > 0) { List<VirtualMachineRelocateSpecDiskLocator> diskLocator = new ArrayList<VirtualMachineRelocateSpecDiskLocator>(independentDisks.length); for (int i = 0; i < independentDisks.length; i++) { VirtualMachineRelocateSpecDiskLocator loc = new VirtualMachineRelocateSpecDiskLocator(); loc.setDatastore(morDs); loc.setDiskId(independentDisks[i].getKey()); loc.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value()); diskLocator.add(loc); } rSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.CREATE_NEW_CHILD_DISK_BACKING.value()); rSpec.getDisk().addAll(diskLocator); } else { rSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.CREATE_NEW_CHILD_DISK_BACKING.value()); } rSpec.setPool(morResourcePool); VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setPowerOn(false); cloneSpec.setTemplate(false); cloneSpec.setLocation(rSpec); cloneSpec.setSnapshot(morBaseSnapshot); ManagedObjectReference morTask = _context.getService().cloneVMTask(_mor, morFolder, cloneName, cloneSpec); boolean result = _context.getVimClient().waitForTask(morTask); if (result) { _context.waitForTaskProgressDone(morTask); return true; } else { s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask)); } return false; }
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; }