use of com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator in project photon-model by vmware.
the class InstanceClient method cloneVm.
private ManagedObjectReference cloneVm(ManagedObjectReference template) throws Exception {
ManagedObjectReference folder = getVmFolder();
List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk);
ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec);
ManagedObjectReference resourcePool = getResourcePool();
Map<String, Object> props = this.get.entityProps(template, VimPath.vm_config_hardware_device);
ArrayOfVirtualDevice devices = (ArrayOfVirtualDevice) props.get(VimPath.vm_config_hardware_device);
VirtualDisk vd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).findFirst().orElse(null);
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
relocSpec.setDatastore(datastore);
if (pbmSpec != null) {
pbmSpec.stream().forEach(spec -> {
relocSpec.getProfile().add(spec);
});
}
relocSpec.setFolder(folder);
relocSpec.setPool(resourcePool);
relocSpec.setDiskMoveType(computeDiskMoveType().value());
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setLocation(relocSpec);
// Set the provisioning type of the parent disk.
VirtualMachineRelocateSpecDiskLocator diskProvisionTypeLocator = setProvisioningType(vd, datastore, pbmSpec);
if (diskProvisionTypeLocator != null) {
cloneSpec.getLocation().getDisk().add(diskProvisionTypeLocator);
}
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
String displayName = this.ctx.child.name;
ManagedObjectReference cloneTask = getVimPort().cloneVMTask(template, folder, displayName, cloneSpec);
TaskInfo info = waitTaskEnd(cloneTask);
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());
}
}
return (ManagedObjectReference) info.getResult();
}
use of com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator in project cloudstack by apache.
the class VmwareResource method execute.
// OfflineVmwareMigration: refactor to be able to handle a detached volume
private Answer execute(MigrateVolumeCommand cmd) {
String volumePath = cmd.getVolumePath();
String chainInfo = cmd.getChainInfo();
StorageFilerTO poolTo = cmd.getPool();
VolumeObjectTO volumeObjectTO = (VolumeObjectTO) cmd.getSrcData();
String vmName = cmd.getAttachedVmName();
VirtualMachineMO vmMo = null;
VmwareHypervisorHost srcHyperHost = null;
// OfflineVmwareMigration: ifhost is null ???
if (StringUtils.isBlank(cmd.getAttachedVmName())) {
return migrateVolume(cmd);
}
ManagedObjectReference morDs = null;
ManagedObjectReference morDc = null;
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
VirtualMachineRelocateSpecDiskLocator diskLocator = null;
String tgtDsName = "";
try {
srcHyperHost = getHyperHost(getServiceContext());
morDc = srcHyperHost.getHyperHostDatacenter();
tgtDsName = poolTo.getUuid();
// find VM in this datacenter not just in this cluster.
DatacenterMO dcMo = new DatacenterMO(getServiceContext(), morDc);
vmMo = dcMo.findVm(vmName);
if (vmMo == null) {
String msg = "VM " + vmName + " does not exist in VMware datacenter " + morDc.getValue();
s_logger.error(msg);
throw new CloudRuntimeException(msg);
}
vmName = vmMo.getName();
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, tgtDsName);
if (morDs == null) {
String msg = "Unable to find the mounted datastore with name: " + tgtDsName + " on source host: " + srcHyperHost.getHyperHostName() + " to execute MigrateVolumeCommand";
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO targetDsMo = new DatastoreMO(srcHyperHost.getContext(), morDs);
if (cmd.getContextParam(DiskTO.PROTOCOL_TYPE) != null && cmd.getContextParam(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDiskWithVolumeDetails(diskInfoBuilder, volumePath, chainInfo, false, null, poolTo.getUuid(), hyperHost, context);
if (diskInfoBuilder != null && matchingExistingDisk != null) {
String[] diskChain = matchingExistingDisk.getDiskChain();
DatastoreFile file = new DatastoreFile(diskChain[0]);
if (!file.getFileBaseName().equalsIgnoreCase(volumePath)) {
if (s_logger.isInfoEnabled())
s_logger.info("Detected disk-chain top file change on volume: " + volumePath + " -> " + file.getFileBaseName());
volumePath = file.getFileBaseName();
}
}
}
String fullVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(targetDsMo, vmName, volumePath + VMDK_EXTENSION);
Pair<VirtualDisk, String> diskInfo = getVirtualDiskInfo(vmMo, appendFileType(volumePath, VMDK_EXTENSION));
String vmdkAbsFile = getAbsoluteVmdkFile(diskInfo.first());
if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
}
int diskId = diskInfo.first().getKey();
diskLocator = new VirtualMachineRelocateSpecDiskLocator();
diskLocator.setDatastore(morDs);
diskLocator.setDiskId(diskId);
diskLocators.add(diskLocator);
if (cmd.getVolumeType() == Volume.Type.ROOT) {
relocateSpec.setDatastore(morDs);
// If a target datastore is provided for the VM, then by default all volumes associated with the VM will be migrated to that target datastore.
// Hence set the existing datastore as target datastore for volumes that are not to be migrated.
List<Pair<Integer, ManagedObjectReference>> diskDatastores = vmMo.getAllDiskDatastores();
for (Pair<Integer, ManagedObjectReference> diskDatastore : diskDatastores) {
if (diskDatastore.first().intValue() != diskId) {
diskLocator = new VirtualMachineRelocateSpecDiskLocator();
diskLocator.setDiskId(diskDatastore.first().intValue());
diskLocator.setDatastore(diskDatastore.second());
diskLocators.add(diskLocator);
}
}
}
relocateSpec.getDisk().addAll(diskLocators);
// Change datastore
if (!vmMo.changeDatastore(relocateSpec)) {
throw new Exception("Change datastore operation failed during volume migration");
} else {
s_logger.debug("Successfully migrated volume " + volumePath + " to target datastore " + tgtDsName);
}
// further volume operations on the ROOT volume such as volume snapshot etc. will result in DB inconsistencies.
if (!vmMo.consolidateVmDisks()) {
s_logger.warn("VM disk consolidation failed after storage migration.");
} else {
s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
}
// Update and return volume path and chain info because that could have changed after migration
if (!targetDsMo.fileExists(fullVolumePath)) {
VirtualDisk[] disks = vmMo.getAllDiskDevice();
for (VirtualDisk disk : disks) if (disk.getKey() == diskId) {
volumePath = vmMo.getVmdkFileBaseName(disk);
}
}
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
chainInfo = _gson.toJson(diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, targetDsMo.getName()));
MigrateVolumeAnswer answer = new MigrateVolumeAnswer(cmd, true, null, volumePath);
answer.setVolumeChainInfo(chainInfo);
return answer;
} catch (Exception e) {
String msg = "Catch Exception " + e.getClass().getName() + " due to " + e.toString();
s_logger.error(msg, e);
return new MigrateVolumeAnswer(cmd, false, msg, null);
}
}
use of com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator in project cloudstack by apache.
the class VirtualMachineMO method setDiskProvisioningType.
private void setDiskProvisioningType(VirtualMachineRelocateSpec relocSpec, ManagedObjectReference morDs, Storage.ProvisioningType diskProvisioningType) throws Exception {
if (diskProvisioningType == null) {
return;
}
List<VirtualMachineRelocateSpecDiskLocator> relocateDisks = relocSpec.getDisk();
List<VirtualDisk> disks = this.getVirtualDisks();
for (VirtualDisk disk : disks) {
VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) disk.getBacking();
if (diskProvisioningType == Storage.ProvisioningType.FAT) {
backing.setThinProvisioned(false);
backing.setEagerlyScrub(true);
} else if (diskProvisioningType == Storage.ProvisioningType.THIN) {
backing.setThinProvisioned(true);
} else if (diskProvisioningType == Storage.ProvisioningType.SPARSE) {
backing.setThinProvisioned(false);
backing.setEagerlyScrub(false);
}
VirtualMachineRelocateSpecDiskLocator diskLocator = new VirtualMachineRelocateSpecDiskLocator();
diskLocator.setDiskId(disk.getKey());
diskLocator.setDiskBackingInfo(backing);
diskLocator.setDatastore(morDs);
relocateDisks.add(diskLocator);
}
}
Aggregations