use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageProcessor method copyVolumeToSecStorage.
private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyCommand cmd, String vmName, String poolId, String volumePath, String destVolumePath, String secStorageUrl, String workerVmName) throws Exception {
VirtualMachineMO workerVm = null;
VirtualMachineMO vmMo = null;
String exportName = UUID.randomUUID().toString().replace("-", "");
String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
try {
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
if (morDs == null) {
String msg = "Unable to find volumes's storage pool for copy volume operation";
s_logger.error(msg);
throw new Exception(msg);
}
boolean clonedWorkerVMNeeded = true;
vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null || VmwareResource.getVmState(vmMo) == PowerState.PowerOff) {
// create a dummy worker vm for attaching the volume
DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
workerVm = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName, null);
if (workerVm == null) {
String msg = "Unable to create worker VM to execute CopyVolumeCommand";
s_logger.error(msg);
throw new Exception(msg);
}
// attach volume to worker VM
String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk", searchExcludedFolders);
workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs);
vmMo = workerVm;
clonedWorkerVMNeeded = false;
}
exportVolumeToSecondaryStorage(hyperHost.getContext(), vmMo, hyperHost, volumePath, secStorageUrl, destVolumePath, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1, null), _nfsVersion, clonedWorkerVMNeeded);
return new Pair<>(destVolumePath, exportName);
} finally {
if (vmMo != null && vmMo.getSnapshotMor(exportName) != null) {
vmMo.removeSnapshot(exportName, false);
}
if (workerVm != null) {
workerVm.detachAllDisksAndDestroy();
}
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageProcessor method createVMAndFolderWithVMName.
private String createVMAndFolderWithVMName(VmwareContext context, VmwareHypervisorHost hyperHost, TemplateObjectTO template, VirtualMachineMO vmTemplate, VolumeObjectTO volume, DatacenterMO dcMo, DatastoreMO dsMo, String searchExcludedFolders) throws Exception {
String vmdkName = volume.getName();
try {
ManagedObjectReference morDatastore = dsMo.getMor();
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (template.getSize() != null) {
_fullCloneFlag = volume.getSize() > template.getSize() ? true : _fullCloneFlag;
}
if (!_fullCloneFlag) {
if (_diskProvisioningStrictness && volume.getProvisioningType() != ProvisioningType.THIN) {
throw new CloudRuntimeException("Unable to create linked clones with strict disk provisioning enabled");
}
createVMLinkedClone(vmTemplate, dcMo, vmdkName, morDatastore, morPool);
} else {
createVMFullClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool, volume.getProvisioningType());
}
VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
assert (vmMo != null);
String vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
s_logger.info("Move volume out of volume-wrapper VM " + vmdkFileBaseName);
String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.VMWARE, !_fullCloneFlag);
String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, !_fullCloneFlag);
for (int i = 0; i < vmwareLayoutFilePair.length; i++) {
dsMo.moveDatastoreFile(vmwareLayoutFilePair[i], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[i], dcMo.getMor(), true);
}
s_logger.info("detach disks from volume-wrapper VM and destroy" + vmdkName);
vmMo.detachAllDisksAndDestroy();
String srcFile = dsMo.getDatastorePath(vmdkName, true);
dsMo.deleteFile(srcFile, dcMo.getMor(), true, searchExcludedFolders);
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmdkName)) {
dsMo.deleteFolder(srcFile, dcMo.getMor());
}
// restoreVM - move the new ROOT disk into corresponding VM folder
VirtualMachineMO restoreVmMo = dcMo.findVm(volume.getVmName());
if (restoreVmMo != null) {
// VM folder name in datastore will be VM's name in vCenter.
String vmNameInVcenter = restoreVmMo.getName();
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmNameInVcenter)) {
VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmNameInVcenter, dsMo, vmdkFileBaseName, searchExcludedFolders);
}
}
return vmdkFileBaseName;
} finally {
// check if volume wrapper VM is cleaned, if not cleanup
VirtualMachineMO vmdknamedVM = dcMo.findVm(vmdkName);
if (vmdknamedVM != null) {
vmdknamedVM.destroy();
}
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageProcessor method exportManagedStorageSnapshotToTemplate.
private void exportManagedStorageSnapshotToTemplate(CopyCommand cmd, String installFullPath, String snapshotPath, String exportName) throws Exception {
DatastoreFile dsFile = new DatastoreFile(snapshotPath);
VmwareContext context = hostService.getServiceContext(cmd);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
ManagedObjectReference dsMor = hyperHost.findDatastoreByName(dsFile.getDatastoreName());
DatastoreMO dsMo = new DatastoreMO(context, dsMor);
String workerVMName = hostService.getWorkerName(context, cmd, 0, dsMo);
VirtualMachineMO workerVM = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null);
if (workerVM == null) {
throw new CloudRuntimeException("Failed to find the newly created worker VM: " + workerVMName);
}
workerVM.attachDisk(new String[] { snapshotPath }, dsMor);
workerVM.exportVm(installFullPath, exportName, false, false);
workerVM.detachAllDisksAndDestroy();
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageProcessor method createVolume.
@Override
public Answer createVolume(CreateObjectCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
DataStoreTO primaryStore = volume.getDataStore();
String vSphereStoragePolicyId = volume.getvSphereStoragePolicyId();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
if (morDatastore == null) {
throw new Exception("Unable to find datastore in vSphere");
}
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
// create data volume
VirtualMachineMO vmMo = null;
String volumeUuid = UUID.randomUUID().toString().replace("-", "");
String volumeDatastorePath = VmwareStorageLayoutHelper.getDatastorePathBaseFolderFromVmdkFileName(dsMo, volumeUuid + ".vmdk");
VolumeObjectTO newVol = new VolumeObjectTO();
try {
VirtualStorageObjectManagerMO vStorageObjectManagerMO = new VirtualStorageObjectManagerMO(context);
VStorageObject virtualDisk = vStorageObjectManagerMO.createDisk(morDatastore, volume.getProvisioningType(), volume.getSize(), volumeDatastorePath, volumeUuid);
DatastoreFile file = new DatastoreFile(((BaseConfigInfoDiskFileBackingInfo) virtualDisk.getConfig().getBacking()).getFilePath());
newVol.setPath(file.getFileBaseName());
newVol.setSize(volume.getSize());
} catch (Exception e) {
s_logger.debug("Create disk using vStorageObject manager failed due to exception " + e.getMessage() + ", retying using worker VM");
String dummyVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
try {
s_logger.info("Create worker VM " + dummyVmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
synchronized (this) {
try {
vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey(), vSphereStoragePolicyId);
vmMo.detachDisk(volumeDatastorePath, false);
} catch (Exception e1) {
s_logger.error("Deleting file " + volumeDatastorePath + " due to error: " + e1.getMessage());
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid, dcMo, VmwareManager.s_vmwareSearchExcludeFolder.value());
throw new CloudRuntimeException("Unable to create volume due to: " + e1.getMessage());
}
}
newVol = new VolumeObjectTO();
newVol.setPath(volumeUuid);
newVol.setSize(volume.getSize());
return new CreateObjectAnswer(newVol);
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
vmMo.detachAllDisksAndDestroy();
}
}
}
return new CreateObjectAnswer(newVol);
} catch (Throwable e) {
return new CreateObjectAnswer(hostService.createLogMessageException(e, cmd));
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageProcessor method syncVolumePath.
@Override
public Answer syncVolumePath(SyncVolumePathCommand cmd) {
DiskTO disk = cmd.getDisk();
VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
DataStoreTO primaryStore = volumeTO.getDataStore();
String volumePath = volumeTO.getPath();
String vmName = volumeTO.getVmName();
boolean datastoreChangeObserved = false;
boolean volumePathChangeObserved = false;
String chainInfo = null;
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
String msg = "Unable to find the VM to execute SyncVolumePathCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
}
String datastoreUUID = primaryStore.getUuid();
if (disk.getDetails().get(DiskTO.PROTOCOL_TYPE) != null && disk.getDetails().get(DiskTO.PROTOCOL_TYPE).equalsIgnoreCase("DatastoreCluster")) {
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(hyperHost, context, vmMo, disk);
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
if (diskInfoBuilder != null && matchingExistingDisk != null) {
String[] diskChain = matchingExistingDisk.getDiskChain();
assert (diskChain.length > 0);
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: " + volumeTO.getId() + " " + volumePath + " -> " + file.getFileBaseName());
volumePathChangeObserved = true;
volumePath = file.getFileBaseName();
volumeTO.setPath(volumePath);
chainInfo = _gson.toJson(matchingExistingDisk);
}
DatastoreMO diskDatastoreMofromVM = getDiskDatastoreMofromVM(hyperHost, context, vmMo, disk, diskInfoBuilder);
if (diskDatastoreMofromVM != null) {
String actualPoolUuid = diskDatastoreMofromVM.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID);
if (!actualPoolUuid.equalsIgnoreCase(primaryStore.getUuid())) {
s_logger.warn(String.format("Volume %s found to be in a different storage pool %s", volumePath, actualPoolUuid));
datastoreChangeObserved = true;
datastoreUUID = actualPoolUuid;
chainInfo = _gson.toJson(matchingExistingDisk);
}
}
}
}
SyncVolumePathAnswer answer = new SyncVolumePathAnswer(disk);
if (datastoreChangeObserved) {
answer.setContextParam("datastoreName", datastoreUUID);
}
if (volumePathChangeObserved) {
answer.setContextParam("volumePath", volumePath);
}
if (chainInfo != null && !chainInfo.isEmpty()) {
answer.setContextParam("chainInfo", chainInfo);
}
return answer;
} catch (Throwable e) {
return new SyncVolumePathAnswer(hostService.createLogMessageException(e, cmd));
}
}
Aggregations