Search in sources :

Example 46 with DatastoreMO

use of com.cloud.hypervisor.vmware.mo.DatastoreMO 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();
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 47 with DatastoreMO

use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.

the class VmwareStorageProcessor method getMatchingExistingDisk.

private VirtualMachineDiskInfo getMatchingExistingDisk(VmwareHypervisorHost hyperHost, VmwareContext context, VirtualMachineMO vmMo, DiskTO vol) throws Exception {
    VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
    if (diskInfoBuilder != null) {
        VolumeObjectTO volume = (VolumeObjectTO) vol.getData();
        ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, volume.getDataStore().getUuid());
        DatastoreMO dsMo = new DatastoreMO(context, morDs);
        String dsName = dsMo.getName();
        String diskBackingFileBaseName = volume.getPath();
        VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(diskBackingFileBaseName, dsName);
        if (diskInfo != null) {
            s_logger.info("Found existing disk info from volume path: " + volume.getPath());
            return diskInfo;
        } else {
            String chainInfo = volume.getChainInfo();
            if (chainInfo != null) {
                VirtualMachineDiskInfo infoInChain = _gson.fromJson(chainInfo, VirtualMachineDiskInfo.class);
                if (infoInChain != null) {
                    String[] disks = infoInChain.getDiskChain();
                    if (disks.length > 0) {
                        for (String diskPath : disks) {
                            DatastoreFile file = new DatastoreFile(diskPath);
                            diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(file.getFileBaseName(), dsName);
                            if (diskInfo != null) {
                                s_logger.info("Found existing disk from chain info: " + diskPath);
                                return diskInfo;
                            }
                        }
                    }
                    if (diskInfo == null) {
                        diskInfo = diskInfoBuilder.getDiskInfoByDeviceBusName(infoInChain.getDiskDeviceBusName());
                        if (diskInfo != null) {
                            s_logger.info("Found existing disk from from chain device bus information: " + infoInChain.getDiskDeviceBusName());
                            return diskInfo;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 48 with DatastoreMO

use of com.cloud.hypervisor.vmware.mo.DatastoreMO 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();
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 49 with DatastoreMO

use of com.cloud.hypervisor.vmware.mo.DatastoreMO in project cloudstack by apache.

the class VmwareStorageProcessor method renameDatastore.

private boolean renameDatastore(VmwareContext context, ManagedObjectReference morDs, String newName, List<Pair<ManagedObjectReference, String>> lstHosts) throws Exception {
    if (morDs != null) {
        DatastoreMO datastoreMO = new DatastoreMO(context, morDs);
        datastoreMO.renameDatastore(newName);
        waitForAllHostsToMountDatastore(lstHosts, datastoreMO);
        return true;
    }
    s_logger.debug("Unable to locate datastore to rename");
    return false;
}
Also used : DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO)

Example 50 with DatastoreMO

use of com.cloud.hypervisor.vmware.mo.DatastoreMO 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));
    }
}
Also used : VirtualStorageObjectManagerMO(com.cloud.hypervisor.vmware.mo.VirtualStorageObjectManagerMO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VStorageObject(com.vmware.vim25.VStorageObject)

Aggregations

DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)54 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)47 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)38 RemoteException (java.rmi.RemoteException)33 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)31 UnsupportedEncodingException (java.io.UnsupportedEncodingException)31 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)26 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)23 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)18 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)17 Pair (com.cloud.utils.Pair)16 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)15 CloudException (com.cloud.exception.CloudException)15 InternalErrorException (com.cloud.exception.InternalErrorException)15 IOException (java.io.IOException)15 ConnectException (java.net.ConnectException)15 ConfigurationException (javax.naming.ConfigurationException)15 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)15 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)12 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)9