Search in sources :

Example 41 with DatastoreMO

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

the class VmwareStorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(CopyCommand cmd) {
    SnapshotObjectTO srcSnapshot = (SnapshotObjectTO) cmd.getSrcTO();
    DataStoreTO primaryStore = srcSnapshot.getDataStore();
    SnapshotObjectTO destSnapshot = (SnapshotObjectTO) cmd.getDestTO();
    DataStoreTO destStore = destSnapshot.getDataStore();
    if (!(destStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO destNfsStore = (NfsTO) destStore;
    String secondaryStorageUrl = destNfsStore.getUrl();
    String snapshotUuid = srcSnapshot.getPath();
    String prevSnapshotUuid = srcSnapshot.getParentSnapshotPath();
    String prevBackupUuid = destSnapshot.getParentSnapshotPath();
    VirtualMachineMO workerVm = null;
    String workerVMName = null;
    String volumePath = srcSnapshot.getVolume().getPath();
    ManagedObjectReference morDs;
    DatastoreMO dsMo;
    // By default assume failure
    String details;
    boolean success;
    String snapshotBackupUuid;
    boolean hasOwnerVm = false;
    Ternary<String, String, String[]> backupResult = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    VirtualMachineMO vmMo = null;
    String vmName = srcSnapshot.getVmName();
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
        CopyCmdAnswer answer = null;
        try {
            if (vmName != null) {
                vmMo = hyperHost.findVmOnHyperHost(vmName);
                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(vmName);
                }
            }
            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 = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumePath + ".vmdk");
                vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
            } else {
                s_logger.info("Using owner VM " + vmName + " for snapshot operation");
                hasOwnerVm = true;
            }
            s_logger.debug(String.format("Executing backup snapshot with UUID [%s] to secondary storage.", snapshotUuid));
            backupResult = backupSnapshotToSecondaryStorage(context, vmMo, hyperHost, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1, null), _nfsVersion);
            snapshotBackupUuid = backupResult.first();
            success = (snapshotBackupUuid != null);
            if (!success) {
                details = "Failed to backUp the snapshot with uuid: " + snapshotUuid + " to secondary storage.";
                answer = new CopyCmdAnswer(details);
            } else {
                details = "Successfully backedUp the snapshot with Uuid: " + snapshotUuid + " to secondary storage.";
                // Get snapshot physical size
                long physicalSize = 0;
                String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, _nfsVersion);
                String snapshotDir = destSnapshot.getPath() + "/" + snapshotBackupUuid;
                File[] files = new File(secondaryMountPoint + "/" + snapshotDir).listFiles();
                if (files != null) {
                    for (File file : files) {
                        String fileName = file.getName();
                        if (fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
                            physicalSize = new File(secondaryMountPoint + "/" + snapshotDir + "/" + fileName).length();
                            break;
                        }
                    }
                }
                SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
                newSnapshot.setPath(snapshotDir + "/" + snapshotBackupUuid);
                newSnapshot.setPhysicalSize(physicalSize);
                answer = new CopyCmdAnswer(newSnapshot);
            }
        } finally {
            if (vmMo != null) {
                ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
                if (snapshotMor != null) {
                    vmMo.removeSnapshot(snapshotUuid, false);
                    // in the middle
                    if (backupResult != null && hasOwnerVm) {
                        s_logger.info("Check if we have disk consolidation after snapshot operation");
                        boolean chainConsolidated = false;
                        for (String vmdkDsFilePath : backupResult.third()) {
                            s_logger.info("Validate disk chain file:" + vmdkDsFilePath);
                            if (vmMo.getDiskDevice(vmdkDsFilePath) == null) {
                                s_logger.info("" + vmdkDsFilePath + " no longer exists, consolidation detected");
                                chainConsolidated = true;
                                break;
                            } else {
                                s_logger.info("" + vmdkDsFilePath + " is found still in chain");
                            }
                        }
                        if (chainConsolidated) {
                            String topVmdkFilePath = null;
                            try {
                                topVmdkFilePath = vmMo.getDiskCurrentTopBackingFileInChain(backupResult.second());
                            } catch (Exception e) {
                                s_logger.error("Unexpected exception", e);
                            }
                            s_logger.info("Disk has been consolidated, top VMDK is now: " + topVmdkFilePath);
                            if (topVmdkFilePath != null) {
                                DatastoreFile file = new DatastoreFile(topVmdkFilePath);
                                SnapshotObjectTO snapshotInfo = (SnapshotObjectTO) answer.getNewData();
                                VolumeObjectTO vol = new VolumeObjectTO();
                                vol.setUuid(srcSnapshot.getVolume().getUuid());
                                vol.setPath(file.getFileBaseName());
                                snapshotInfo.setVolume(vol);
                            } else {
                                s_logger.error("Disk has been consolidated, but top VMDK is not found ?!");
                            }
                        }
                    }
                } else {
                    s_logger.info("No snapshots created to be deleted!");
                }
            }
            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);
            }
        }
        return answer;
    } catch (Throwable e) {
        return new CopyCmdAnswer(hostService.createLogMessageException(e, cmd));
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) 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) NfsTO(com.cloud.agent.api.to.NfsTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 42 with DatastoreMO

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

the class VmwareStorageProcessor method getDiskDatastoreMofromVM.

private DatastoreMO getDiskDatastoreMofromVM(VmwareHypervisorHost hyperHost, VmwareContext context, VirtualMachineMO vmMo, DiskTO disk, VirtualMachineDiskInfoBuilder diskInfoBuilder) throws Exception {
    assert (hyperHost != null) && (context != null);
    List<Pair<Integer, ManagedObjectReference>> diskDatastores = vmMo.getAllDiskDatastores();
    VolumeObjectTO volume = (VolumeObjectTO) disk.getData();
    String diskBackingFileBaseName = volume.getPath();
    for (Pair<Integer, ManagedObjectReference> diskDatastore : diskDatastores) {
        DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), diskDatastore.second());
        String dsName = dsMo.getName();
        VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(diskBackingFileBaseName, dsName);
        if (diskInfo != null) {
            s_logger.info("Found existing disk info from volume path: " + volume.getPath());
            return dsMo;
        } 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 dsMo;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) Pair(com.cloud.utils.Pair) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 43 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();
    String volumePath = volumeTO.getPath();
    String storagePolicyId = volumeTO.getvSphereStoragePolicyId();
    String vmdkPath = isManaged ? resource.getVmdkPath(volumePath) : 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 AttachCommand, vmName: " + vmName;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        vmName = vmMo.getName();
        ManagedObjectReference morDs;
        String diskUuid = volumeTO.getUuid().replace("-", "");
        if (isAttach && isManaged) {
            Map<String, String> details = disk.getDetails();
            morDs = prepareManagedStorage(context, hyperHost, diskUuid, iScsiName, storageHost, storagePort, vmdkPath, 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;
        boolean datastoreChangeObserved = false;
        boolean volumePathChangeObserved = false;
        String chainInfo = null;
        if (isAttach) {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath((vmdkPath != null ? vmdkPath : dsMo.getName()) + ".vmdk");
            } else {
                if (dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
                    datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumePath + ".vmdk");
                    if (!dsMo.fileExists(datastoreVolumePath)) {
                        datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumePath + ".vmdk");
                    }
                    if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmName) || !dsMo.fileExists(datastoreVolumePath)) {
                        datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, volumePath, volumePath + ".vmdk");
                    }
                    if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), volumePath) || !dsMo.fileExists(datastoreVolumePath)) {
                        datastoreVolumePath = dsMo.searchFileInSubFolders(volumePath + ".vmdk", true, null);
                    }
                } else {
                    datastoreVolumePath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dsMo.getOwnerDatacenter().first(), vmName, dsMo, volumePath, VmwareManager.s_vmwareSearchExcludeFolder.value());
                }
            }
        } else {
            if (isManaged) {
                datastoreVolumePath = dsMo.getDatastorePath((vmdkPath != null ? vmdkPath : dsMo.getName()) + ".vmdk");
            } else {
                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);
                            }
                        }
                    }
                }
                if (storagePort == DEFAULT_NFS_PORT) {
                    morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(diskUuid) : datastoreUUID);
                } else {
                    morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? VmwareResource.getDatastoreName(iScsiName) : datastoreUUID);
                }
                dsMo = new DatastoreMO(context, morDs);
                datastoreVolumePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, volumePath + ".vmdk");
                if (!dsMo.fileExists(datastoreVolumePath)) {
                    datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, vmName, volumePath + ".vmdk");
                }
                if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmName) || !dsMo.fileExists(datastoreVolumePath)) {
                    datastoreVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(dsMo, volumePath, volumePath + ".vmdk");
                }
                if (!dsMo.folderExists(String.format("[%s]", dsMo.getName()), volumePath) || !dsMo.fileExists(datastoreVolumePath)) {
                    datastoreVolumePath = dsMo.searchFileInSubFolders(volumePath + ".vmdk", true, null);
                }
            }
        }
        disk.setPath(datastoreVolumePath);
        AttachAnswer answer = new AttachAnswer(disk);
        if (isAttach) {
            String diskController = getLegacyVmDataDiskController();
            if (controllerInfo != null && StringUtils.isNotEmpty(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, storagePolicyId);
            VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
            VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, dsMo.getName());
            chainInfo = _gson.toJson(diskInfo);
            if (isManaged) {
                expandVirtualDisk(vmMo, datastoreVolumePath, volumeTO.getSize());
            }
        } else {
            vmMo.removeAllSnapshots();
            vmMo.detachDisk(datastoreVolumePath, false);
            if (isManaged) {
                handleDatastoreAndVmdkDetachManaged(cmd, diskUuid, iScsiName, storageHost, storagePort);
            } else {
                if (!dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
                    VmwareStorageLayoutHelper.syncVolumeToRootFolder(dsMo.getOwnerDatacenter().first(), dsMo, volumePath, vmName, VmwareManager.s_vmwareSearchExcludeFolder.value());
                }
            }
            if (datastoreChangeObserved) {
                answer.setContextParam("datastoreName", dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID));
            }
            if (volumePathChangeObserved) {
                answer.setContextParam("volumePath", volumePath);
            }
        }
        if (chainInfo != null && !chainInfo.isEmpty())
            answer.setContextParam("chainInfo", chainInfo);
        return answer;
    } catch (Throwable e) {
        String msg = String.format("Failed to %s volume!", isAttach ? "attach" : "detach");
        s_logger.error(msg, e);
        hostService.createLogMessageException(e, cmd);
        // Sending empty error message - too many duplicate errors in UI
        return new AttachAnswer("");
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) 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) VirtualMachineDiskInfo(org.apache.cloudstack.utils.volume.VirtualMachineDiskInfo) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 44 with DatastoreMO

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

the class VmwareStorageProcessor method resignature.

@Override
public ResignatureAnswer resignature(ResignatureCommand cmd) {
    final Map<String, String> details = cmd.getDetails();
    String scsiNaaDeviceId = details.get(DiskTO.SCSI_NAA_DEVICE_ID);
    if (scsiNaaDeviceId == null || scsiNaaDeviceId.trim().length() == 0) {
        throw new CloudRuntimeException("The 'scsiNaaDeviceId' needs to be specified when resignaturing a VMware datastore.");
    }
    final String iScsiName = details.get(DiskTO.IQN);
    final String datastoreName = getMaximumDatastoreName(VmwareResource.getDatastoreName(iScsiName));
    String vmdk = null;
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
        ClusterMO clusterMO = new ClusterMO(context, morCluster);
        List<Pair<ManagedObjectReference, String>> lstHosts = clusterMO.getClusterHosts();
        // add iSCSI connection to host
        final String storageHost = details.get(DiskTO.STORAGE_HOST);
        final int storagePortNumber = Integer.parseInt(details.get(DiskTO.STORAGE_PORT));
        final String chapInitiatorUsername = details.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = details.get(DiskTO.CHAP_INITIATOR_SECRET);
        final String chapTargetUsername = details.get(DiskTO.CHAP_TARGET_USERNAME);
        final String chapTargetSecret = details.get(DiskTO.CHAP_TARGET_SECRET);
        HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageHost, lstHosts);
        List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
        if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
            List<HostInternetScsiHbaStaticTarget> lstTargets = getTargets(storageHost, storagePortNumber, trimIqn(iScsiName), chapInitiatorUsername, chapInitiatorSecret, chapTargetUsername, chapTargetSecret);
            addRemoveInternetScsiTargetsToAllHosts(true, lstTargets, hostsUsingStaticDiscovery);
        }
        rescanAllHosts(context, lstHosts, true, true);
        // perform resignature operation
        HostMO hostMO = new HostMO(context, lstHosts.get(0).first());
        HostDatastoreSystemMO hostDatastoreSystem = hostMO.getHostDatastoreSystemMO();
        List<HostUnresolvedVmfsVolume> hostUnresolvedVmfsVolumes = hostDatastoreSystem.queryUnresolvedVmfsVolumes();
        if (hostUnresolvedVmfsVolumes == null || hostUnresolvedVmfsVolumes.size() == 0) {
            throw new CloudRuntimeException("Unable to locate any snapshot datastores");
        }
        boolean foundExtent = false;
        for (HostUnresolvedVmfsVolume hostUnresolvedVmfsVolume : hostUnresolvedVmfsVolumes) {
            List<HostUnresolvedVmfsExtent> extents = hostUnresolvedVmfsVolume.getExtent();
            List<HostUnresolvedVmfsExtent> matchingExtents = getExtentsMatching(extents, scsiNaaDeviceId);
            if (matchingExtents.size() >= 1) {
                String extentDevicePath = matchingExtents.get(0).getDevicePath();
                HostResignatureRescanResult hostResignatureRescanResult = resignatureDatastore(hostDatastoreSystem, extentDevicePath);
                if (hostResignatureRescanResult == null) {
                    throw new CloudRuntimeException("'hostResignatureRescanResult' should not be 'null'.");
                }
                ManagedObjectReference morDs = hostResignatureRescanResult.getResult();
                if (morDs == null) {
                    throw new CloudRuntimeException("'morDs' should not be 'null'.");
                }
                DatastoreMO datastoreMO = new DatastoreMO(context, morDs);
                boolean isOnlyForTemplate = Boolean.parseBoolean(details.get(DiskTO.TEMPLATE_RESIGN));
                // then rename the datastore.
                if (isOnlyForTemplate) {
                    vmdk = details.get(DiskTO.VMDK);
                } else {
                    vmdk = cleanUpDatastore(cmd, hostDatastoreSystem, datastoreMO, details);
                }
                if (renameDatastore(context, morDs, datastoreName, lstHosts)) {
                    foundExtent = true;
                    break;
                }
            }
        }
        removeVmfsDatastore(cmd, hyperHost, datastoreName, storageHost, storagePortNumber, trimIqn(iScsiName), lstHosts);
        if (!foundExtent) {
            throw new CloudRuntimeException("Unable to locate the applicable extent");
        }
        final ResignatureAnswer answer = new ResignatureAnswer();
        final long volumeSize = Long.parseLong(details.get(DiskTO.VOLUME_SIZE));
        answer.setSize(volumeSize);
        answer.setPath("[" + datastoreName + "] " + vmdk);
        answer.setFormat(ImageFormat.OVA);
        return answer;
    } catch (Exception ex) {
        s_logger.error(String.format("Command %s failed due to: [%s].", cmd.getClass().getSimpleName(), ex.getMessage()), ex);
        throw new CloudRuntimeException(ex.getMessage());
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostUnresolvedVmfsVolume(com.vmware.vim25.HostUnresolvedVmfsVolume) HostDatastoreSystemMO(com.cloud.hypervisor.vmware.mo.HostDatastoreSystemMO) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Pair(com.cloud.utils.Pair) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) HostResignatureRescanResult(com.vmware.vim25.HostResignatureRescanResult) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HostUnresolvedVmfsExtent(com.vmware.vim25.HostUnresolvedVmfsExtent) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 45 with DatastoreMO

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

the class VmwareStorageProcessor method handleTargets.

public void handleTargets(boolean add, ModifyTargetsCommand.TargetTypeToRemove targetTypeToRemove, boolean isRemoveAsync, List<Map<String, String>> targets, List<HostMO> lstHosts) throws Exception {
    ExecutorService executorService = Executors.newFixedThreadPool(lstHosts.size());
    for (HostMO host : lstHosts) {
        List<HostMO> hosts = new ArrayList<>();
        hosts.add(host);
        List<Map<String, String>> dynamicTargetsForHost = new ArrayList<>();
        List<Map<String, String>> staticTargetsForHost = new ArrayList<>();
        for (Map<String, String> target : targets) {
            String storageAddress = target.get(ModifyTargetsCommand.STORAGE_HOST);
            HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(storageAddress, hosts);
            List<HostMO> hostsUsingDynamicDiscovery = hostDiscoveryMethod.getHostsUsingDynamicDiscovery();
            if (hostsUsingDynamicDiscovery != null && hostsUsingDynamicDiscovery.size() > 0) {
                dynamicTargetsForHost.add(target);
            } else {
                staticTargetsForHost.add(target);
            }
        }
        if (add) {
            executorService.submit(new Thread(() -> {
                try {
                    boolean rescan = false;
                    if (staticTargetsForHost.size() > 0) {
                        addRemoveInternetScsiTargetsToAllHosts(true, getTargets(staticTargetsForHost), hosts);
                        rescan = true;
                    }
                    if (dynamicTargetsForHost.size() > 0) {
                        rescan = true;
                    }
                    if (rescan) {
                        rescanAllHosts(hosts, true, false);
                        List<HostInternetScsiHbaStaticTarget> targetsToAdd = new ArrayList<>();
                        targetsToAdd.addAll(getTargets(staticTargetsForHost));
                        targetsToAdd.addAll(getTargets(dynamicTargetsForHost));
                        for (HostInternetScsiHbaStaticTarget targetToAdd : targetsToAdd) {
                            HostDatastoreSystemMO hostDatastoreSystemMO = host.getHostDatastoreSystemMO();
                            String datastoreName = waitForDatastoreName(hostDatastoreSystemMO, targetToAdd.getIScsiName());
                            ManagedObjectReference morDs = hostDatastoreSystemMO.findDatastoreByName(datastoreName);
                            DatastoreMO datastoreMO = new DatastoreMO(host.getContext(), morDs);
                            mountVmfsDatastore2(datastoreMO, hosts);
                        }
                    }
                } catch (Exception ex) {
                    s_logger.warn(ex.getMessage());
                }
            }));
        } else {
            List<HostInternetScsiHbaStaticTarget> targetsToRemove = new ArrayList<>();
            if (staticTargetsForHost.size() > 0 && (ModifyTargetsCommand.TargetTypeToRemove.STATIC.equals(targetTypeToRemove) || ModifyTargetsCommand.TargetTypeToRemove.BOTH.equals(targetTypeToRemove))) {
                targetsToRemove.addAll(getTargets(staticTargetsForHost));
            }
            if (dynamicTargetsForHost.size() > 0 && (ModifyTargetsCommand.TargetTypeToRemove.DYNAMIC.equals(targetTypeToRemove) || ModifyTargetsCommand.TargetTypeToRemove.BOTH.equals(targetTypeToRemove))) {
                targetsToRemove.addAll(getTargets(dynamicTargetsForHost));
            }
            if (targetsToRemove.size() > 0) {
                if (isRemoveAsync) {
                    new Thread(() -> handleRemove(targetsToRemove, host, hosts)).start();
                } else {
                    executorService.submit(new Thread(() -> handleRemove(targetsToRemove, host, hosts)));
                }
            }
        }
    }
    executorService.shutdown();
    if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES)) {
        throw new Exception("The system timed out before completing the task 'handleTargets'.");
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) ArrayList(java.util.ArrayList) HostDatastoreSystemMO(com.cloud.hypervisor.vmware.mo.HostDatastoreSystemMO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

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