use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareStorageManagerImpl method execute.
@Override
public Answer execute(VmwareHostService hostService, CopyVolumeCommand cmd) {
Long volumeId = cmd.getVolumeId();
String volumePath = cmd.getVolumePath();
String secondaryStorageURL = cmd.getSecondaryStorageURL();
String vmName = cmd.getVmName();
VmwareContext context = hostService.getServiceContext(cmd);
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
Pair<String, String> result;
if (cmd.toSecondaryStorage()) {
result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, volumeId, cmd.getPool().getUuid(), volumePath, secondaryStorageURL, hostService.getWorkerName(context, cmd, 0, null), cmd.getNfsVersion());
} else {
StorageFilerTO poolTO = cmd.getPool();
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolTO.getUuid());
if (morDatastore == null) {
morDatastore = hyperHost.mountDatastore(false, poolTO.getHost(), 0, poolTO.getPath(), poolTO.getUuid().replace("-", ""), true);
if (morDatastore == null) {
throw new Exception("Unable to mount storage pool on host. storeUrl: " + poolTO.getHost() + ":/" + poolTO.getPath());
}
}
result = copyVolumeFromSecStorage(hyperHost, volumeId, new DatastoreMO(context, morDatastore), secondaryStorageURL, volumePath, cmd.getNfsVersion());
deleteVolumeDirOnSecondaryStorage(volumeId, secondaryStorageURL, cmd.getNfsVersion());
}
return new CopyVolumeAnswer(cmd, true, null, result.first(), result.second());
} catch (Throwable e) {
return new CopyVolumeAnswer(cmd, false, hostService.createLogMessageException(e, cmd), null, null);
}
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareStorageManagerImpl method execute.
@Override
@Deprecated
public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd) {
Long accountId = cmd.getAccountId();
Long volumeId = cmd.getVolumeId();
String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
// not null: Precondition.
String snapshotUuid = cmd.getSnapshotUuid();
String prevSnapshotUuid = cmd.getPrevSnapshotUuid();
String prevBackupUuid = cmd.getPrevBackupUuid();
String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
VirtualMachineMO workerVm = null;
String workerVMName = null;
String volumePath = cmd.getVolumePath();
ManagedObjectReference morDs = null;
DatastoreMO dsMo = null;
// By default assume failure
String details = null;
boolean success = false;
String snapshotBackupUuid = null;
VmwareContext context = hostService.getServiceContext(cmd);
VirtualMachineMO vmMo = null;
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, cmd.getPool().getUuid());
try {
vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
}
vmMo = hyperHost.findVmOnPeerHyperHost(cmd.getVmName());
if (vmMo == null) {
dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
workerVMName = hostService.getWorkerName(context, cmd, 0, dsMo);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null);
if (vmMo == null) {
throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
}
workerVm = vmMo;
// attach volume to worker VM
String datastoreVolumePath = getVolumePathInDatastore(dsMo, volumePath + ".vmdk", searchExcludedFolders);
vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
}
}
if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + cmd.getSnapshotName(), false, false)) {
throw new Exception("Failed to take snapshot " + cmd.getSnapshotName() + " on vm: " + cmd.getVmName());
}
snapshotBackupUuid = backupSnapshotToSecondaryStorage(vmMo, accountId, volumeId, cmd.getVolumePath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1, dsMo), cmd.getNfsVersion());
success = (snapshotBackupUuid != null);
if (success) {
details = "Successfully backedUp the snapshotUuid: " + snapshotUuid + " to secondary storage.";
}
} finally {
if (vmMo != null) {
ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
if (snapshotMor != null) {
vmMo.removeSnapshot(snapshotUuid, false);
}
}
try {
if (workerVm != null) {
workerVm.detachAllDisksAndDestroy();
}
} catch (Throwable e) {
s_logger.warn(String.format("Failed to destroy worker VM [%s] due to: [%s].", workerVMName, e.getMessage()), e);
}
}
} catch (Throwable e) {
return new BackupSnapshotAnswer(cmd, false, hostService.createLogMessageException(e, cmd), snapshotBackupUuid, true);
}
return new BackupSnapshotAnswer(cmd, success, details, snapshotBackupUuid, true);
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareStorageProcessor method deleteVolume.
@Override
public Answer deleteVolume(DeleteCommand cmd) {
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
DataStoreTO store = vol.getDataStore();
PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) store;
Map<String, String> details = primaryDataStoreTO.getDetails();
boolean isManaged = false;
String managedDatastoreName = null;
if (details != null) {
isManaged = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (isManaged) {
managedDatastoreName = getManagedDatastoreNameFromPath(vol.getPath());
}
}
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? managedDatastoreName : store.getUuid());
if (morDs == null) {
String msg = "Unable to find datastore based on volume mount point " + store.getUuid();
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(context, morDs);
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
DatacenterMO dcMo = new DatacenterMO(context, morDc);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO clusterMo = new ClusterMO(context, morCluster);
if (vol.getVolumeType() == Volume.Type.ROOT) {
String vmName = vol.getVmName();
if (vmName != null) {
VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
if (vmMo == null) {
// Volume might be on a zone-wide storage pool, look for VM in datacenter
vmMo = dcMo.findVm(vmName);
}
List<Map<String, String>> dynamicTargetsToRemove = null;
boolean deployAsIs = vol.isDeployAsIs();
if (vmMo != null) {
if (s_logger.isInfoEnabled()) {
if (deployAsIs) {
s_logger.info("Destroying root volume " + vol.getPath() + " of deploy-as-is VM " + vmName);
} else {
s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
}
}
VirtualMachineDiskInfo diskInfo = null;
if (vol.getChainInfo() != null)
diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
HostMO hostMo = vmMo.getRunningHost();
List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
// tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
if (VmwareResource.getVmState(vmMo) != PowerState.PowerOff) {
vmMo.safePowerOff(_shutdownWaitMs);
}
// call this before calling detachAllDisksExcept
// when expunging a VM, we need to see if any of its disks are serviced by managed storage
// if there is one or more disk serviced by managed storage, remove the iSCSI connection(s)
// don't remove the iSCSI connection(s) until the supported disk(s) is/are removed from the VM
// (removeManagedTargetsFromCluster should be called after detachAllDisksExcept and vm.destroy)
List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
List<String> managedDatastoreNames = getManagedDatastoreNamesFromVirtualDisks(virtualDisks);
// Preserve other disks of the VM
List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
// vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
if (isManaged) {
vmMo.unregisterVm();
} else {
vmMo.destroy();
}
// this.hostService.handleDatastoreAndVmdkDetach(iScsiName, storageHost, storagePort);
if (managedDatastoreNames != null && !managedDatastoreNames.isEmpty()) {
removeManagedTargetsFromCluster(managedDatastoreNames);
}
for (NetworkDetails netDetails : networks) {
if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) {
if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) {
resource.cleanupNetwork(dcMo, netDetails);
}
}
}
} else if (deployAsIs) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroying root volume " + vol.getPath() + " of already removed deploy-as-is VM " + vmName);
}
// The disks of the deploy-as-is VM have been detached from the VM and moved to root folder
String deployAsIsRootDiskPath = dsMo.searchFileInSubFolders(vol.getPath() + VmwareResource.VMDK_EXTENSION, true, null);
if (StringUtils.isNotBlank(deployAsIsRootDiskPath)) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Removing disk " + deployAsIsRootDiskPath);
}
dsMo.deleteFile(deployAsIsRootDiskPath, morDc, true);
String deltaFilePath = dsMo.searchFileInSubFolders(vol.getPath() + "-delta" + VmwareResource.VMDK_EXTENSION, true, null);
if (StringUtils.isNotBlank(deltaFilePath)) {
dsMo.deleteFile(deltaFilePath, morDc, true);
}
}
}
return new Answer(cmd, true, "");
}
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy root volume directly from datastore");
}
}
if (!isManaged) {
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc), VmwareManager.s_vmwareSearchExcludeFolder.value());
}
return new Answer(cmd, true, "Success");
} catch (Throwable e) {
return new Answer(cmd, false, hostService.createLogMessageException(e, cmd));
}
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareStorageProcessor method unmountVmfsDatastore2.
private void unmountVmfsDatastore2(VmwareContext context, VmwareHypervisorHost hyperHost, String datastoreName, List<HostMO> hosts) throws Exception {
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, datastoreName);
DatastoreMO dsMO = new DatastoreMO(context, morDs);
for (HostMO hostMO : hosts) {
unmountVmfsVolume(dsMO, hostMO);
}
}
use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.
the class VmwareStorageProcessor method attachIso.
private Answer attachIso(DiskTO disk, boolean isAttach, String vmName, boolean force) {
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + vmName;
s_logger.error(msg);
throw new Exception(msg);
}
TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
NfsTO nfsImageStore = (NfsTO) iso.getDataStore();
String storeUrl = null;
if (nfsImageStore != null) {
storeUrl = nfsImageStore.getUrl();
}
if (storeUrl == null) {
if (!iso.getName().equalsIgnoreCase(TemplateManager.VMWARE_TOOLS_ISO)) {
String msg = "ISO store root url is not found in AttachIsoCommand";
s_logger.error(msg);
throw new Exception(msg);
} else {
if (isAttach) {
vmMo.mountToolsInstaller();
} else {
try {
if (!vmMo.unmountToolsInstaller()) {
return new AttachAnswer("Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
}
} catch (Throwable e) {
vmMo.detachIso(null, force);
}
}
return new AttachAnswer(disk);
}
}
ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
String isoPath = nfsImageStore.getUrl() + File.separator + iso.getPath();
if (!isoPath.startsWith(storeUrl)) {
assert (false);
String msg = "ISO path does not start with the secondary storage root";
s_logger.error(msg);
throw new Exception(msg);
}
int isoNameStartPos = isoPath.lastIndexOf('/');
String isoFileName = isoPath.substring(isoNameStartPos + 1);
String isoStorePathFromRoot = isoPath.substring(storeUrl.length() + 1, isoNameStartPos);
// TODO, check if iso is already attached, or if there is a previous
// attachment
DatastoreMO secondaryDsMo = new DatastoreMO(context, morSecondaryDs);
String storeName = secondaryDsMo.getName();
String isoDatastorePath = String.format("[%s] %s/%s", storeName, isoStorePathFromRoot, isoFileName);
if (isAttach) {
vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false, force);
} else {
vmMo.detachIso(isoDatastorePath, force);
}
return new AttachAnswer(disk);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String message = String.format("AttachIsoCommand(%s) failed due to: [%s]. Also check if your guest os is a supported version", isAttach ? "attach" : "detach", VmwareHelper.getExceptionMessage(e));
s_logger.error(message, e);
return new AttachAnswer(message);
}
}
Aggregations