Search in sources :

Example 26 with DatastoreMO

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

the class VmwareStorageManagerImpl method execute.

@Override
public Answer execute(VmwareHostService hostService, CreateVolumeFromSnapshotCommand cmd) {
    String primaryStorageNameLabel = cmd.getPrimaryStoragePoolNameLabel();
    Long accountId = cmd.getAccountId();
    Long volumeId = cmd.getVolumeId();
    String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
    String backedUpSnapshotUuid = cmd.getSnapshotUuid();
    String details = null;
    boolean success = false;
    String newVolumeName = UUID.randomUUID().toString().replaceAll("-", "");
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        ManagedObjectReference morPrimaryDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStorageNameLabel);
        if (morPrimaryDs == null) {
            String msg = "Unable to find datastore: " + primaryStorageNameLabel;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
        details = createVolumeFromSnapshot(hyperHost, primaryDsMo, newVolumeName, accountId, volumeId, secondaryStorageUrl, backedUpSnapshotUuid, cmd.getNfsVersion());
        if (details == null) {
            success = true;
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "CreateVolumeFromSnapshotCommand exception: " + StringUtils.getExceptionStackInfo(e);
    }
    return new CreateVolumeFromSnapshotAnswer(cmd, success, details, newVolumeName);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) CreateVolumeFromSnapshotAnswer(com.cloud.agent.api.CreateVolumeFromSnapshotAnswer)

Example 27 with DatastoreMO

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), 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("-", ""));
                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) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, e);
        return new CopyVolumeAnswer(cmd, false, "CopyVolumeCommand failed due to exception: " + StringUtils.getExceptionStackInfo(e), null, null);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 28 with DatastoreMO

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

the class VmwareStorageManagerImpl method copyVolumeToSecStorage.

private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyVolumeCommand cmd, String vmName, long volumeId, String poolId, String volumePath, String secStorageUrl, String workerVmName, Integer nfsVersion) throws Exception {
    String volumeFolder = String.valueOf(volumeId) + "/";
    VirtualMachineMO workerVm = null;
    VirtualMachineMO vmMo = null;
    String exportName = UUID.randomUUID().toString();
    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);
        }
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            // create a dummy worker vm for attaching the volume
            DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
            workerVm = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName);
            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");
            workerVm.attachDisk(new String[] { datastoreVolumePath }, morDs);
            vmMo = workerVm;
        }
        vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false);
        exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, "volumes/" + volumeFolder, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1), nfsVersion);
        return new Pair<String, String>(volumeFolder, exportName);
    } finally {
        vmMo.removeSnapshot(exportName, false);
        if (workerVm != null) {
            //detach volume and destroy worker vm
            workerVm.detachAllDisks();
            workerVm.destroy();
        }
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 29 with DatastoreMO

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

the class VmwareStorageProcessor method cloneVolumeFromBaseTemplate.

@Override
public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataTO destData = cmd.getDestTO();
    VolumeObjectTO volume = (VolumeObjectTO) destData;
    DataStoreTO primaryStore = volume.getDataStore();
    DataStoreTO srcStore = template.getDataStore();
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        VirtualMachineMO vmMo = null;
        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);
        String vmdkName = volume.getName();
        String vmdkFileBaseName = null;
        if (srcStore == null) {
            // create a root volume for blank VM (created from ISO)
            String dummyVmName = hostService.getWorkerName(context, cmd, 0);
            try {
                vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName);
                if (vmMo == null) {
                    throw new Exception("Unable to create a dummy VM for volume creation");
                }
                vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
                // we only use the first file in the pair, linked or not will not matter
                String[] vmdkFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, true);
                String volumeDatastorePath = vmdkFilePair[0];
                synchronized (this) {
                    s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
                    VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
                    vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, -1);
                    vmMo.detachDisk(volumeDatastorePath, false);
                }
            } finally {
                s_logger.info("Destroy dummy VM after volume creation");
                if (vmMo != null) {
                    s_logger.warn("Unable to destroy a null VM ManagedObjectReference");
                    vmMo.detachAllDisks();
                    vmMo.destroy();
                }
            }
        } else {
            String templatePath = template.getPath();
            VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templatePath), true);
            if (vmTemplate == null) {
                s_logger.warn("Template host in vSphere is not in connected state, request template reload");
                return new CopyCmdAnswer("Template host in vSphere is not in connected state, request template reload");
            }
            ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
            ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
            if (!_fullCloneFlag) {
                createVMLinkedClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool);
            } else {
                createVMFullClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool);
            }
            vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
            assert (vmMo != null);
            // TO-DO: Support for base template containing multiple disks
            vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
            s_logger.info("Move volume out of volume-wrapper VM ");
            String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.VMWARE, !_fullCloneFlag);
            String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, !_fullCloneFlag);
            dsMo.moveDatastoreFile(vmwareLayoutFilePair[0], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[0], dcMo.getMor(), true);
            dsMo.moveDatastoreFile(vmwareLayoutFilePair[1], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[1], dcMo.getMor(), true);
            s_logger.info("detach disks from volume-wrapper VM " + vmdkName);
            vmMo.detachAllDisks();
            s_logger.info("destroy volume-wrapper VM " + vmdkName);
            vmMo.destroy();
            String srcFile = dsMo.getDatastorePath(vmdkName, true);
            dsMo.deleteFile(srcFile, dcMo.getMor(), true);
        }
        // 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);
            }
        }
        VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(vmdkFileBaseName);
        newVol.setSize(volume.getSize());
        return new CopyCmdAnswer(newVol);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "clone volume from base image failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new CopyCmdAnswer(e.toString());
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 30 with DatastoreMO

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

the class VmwareStorageProcessor method attachVolume.

private Answer attachVolume(Command cmd, DiskTO disk, boolean isAttach, boolean isManaged, String vmName, String iScsiName, String storageHost, int storagePort, Map<String, String> controllerInfo) {
    VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
    DataStoreTO primaryStore = volumeTO.getDataStore();
    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 the VM to execute AttachCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        vmName = vmMo.getName();
        ManagedObjectReference morDs = null;
        String diskUuid = volumeTO.getUuid().replace("-", "");
        if (isAttach && isManaged) {
            Map<String, String> details = disk.getDetails();
            morDs = prepareManagedStorage(context, hyperHost, diskUuid, iScsiName, storageHost, storagePort, null, details.get(DiskTO.CHAP_INITIATOR_USERNAME), details.get(DiskTO.CHAP_INITIATOR_SECRET), details.get(DiskTO.CHAP_TARGET_USERNAME), details.get(DiskTO.CHAP_TARGET_SECRET), volumeTO.getSize(), cmd);
        } else {
            if (storagePort == DEFAULT_NFS_PORT) {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(diskUuid) : primaryStore.getUuid());
            } else {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(iScsiName) : primaryStore.getUuid());
            }
        }
        if (morDs == null) {
            String msg = "Unable to find the mounted datastore to execute AttachCommand, vmName: " + vmName;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDs);
        String datastoreVolumePath;
        if (isAttach) {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
            } else {
                datastoreVolumePath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dsMo.getOwnerDatacenter().first(), vmName, dsMo, volumeTO.getPath());
            }
        } else {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
            } else {
                datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumeTO.getPath() + ".vmdk");
                if (!dsMo.fileExists(datastoreVolumePath)) {
                    datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumeTO.getPath() + ".vmdk");
                }
            }
        }
        disk.setPath(datastoreVolumePath);
        AttachAnswer answer = new AttachAnswer(disk);
        if (isAttach) {
            String diskController = getLegacyVmDataDiskController();
            if (controllerInfo != null && !Strings.isNullOrEmpty(controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER))) {
                diskController = controllerInfo.get(VmDetailConstants.DATA_DISK_CONTROLLER);
            }
            if (DiskControllerType.getType(diskController) == DiskControllerType.osdefault) {
                diskController = vmMo.getRecommendedDiskController(null);
            }
            vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs, diskController);
        } else {
            vmMo.removeAllSnapshots();
            vmMo.detachDisk(datastoreVolumePath, false);
            if (isManaged) {
                handleDatastoreAndVmdkDetachManaged(diskUuid, iScsiName, storageHost, storagePort);
            } else {
                VmwareStorageLayoutHelper.syncVolumeToRootFolder(dsMo.getOwnerDatacenter().first(), dsMo, volumeTO.getPath(), vmName);
            }
        }
        return answer;
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            hostService.invalidateServiceContext(null);
        }
        String msg = "";
        if (isAttach)
            msg += "Failed to attach volume: " + e.getMessage();
        else
            msg += "Failed to detach volume: " + e.getMessage();
        s_logger.error(msg, e);
        return new AttachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Aggregations

DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)32 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)29 RemoteException (java.rmi.RemoteException)27 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)16 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)16 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)12 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)12 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 CloudException (com.cloud.exception.CloudException)11 InternalErrorException (com.cloud.exception.InternalErrorException)11 IOException (java.io.IOException)11 ConnectException (java.net.ConnectException)11 ConfigurationException (javax.naming.ConfigurationException)11 Pair (com.cloud.utils.Pair)8 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)7 NfsTO (com.cloud.agent.api.to.NfsTO)6 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)6