Search in sources :

Example 81 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext 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 82 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method removeManagedTargetsFromCluster.

private void removeManagedTargetsFromCluster(List<String> managedDatastoreNames) throws Exception {
    List<HostInternetScsiHbaStaticTarget> lstManagedTargets = new ArrayList<>();
    VmwareContext context = hostService.getServiceContext(null);
    VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
    ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
    ClusterMO cluster = new ClusterMO(context, morCluster);
    List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
    HostMO hostMO = new HostMO(context, lstHosts.get(0).first());
    HostStorageSystemMO hostStorageSystem = hostMO.getHostStorageSystemMO();
    for (String managedDatastoreName : managedDatastoreNames) {
        unmountVmfsDatastore(context, hyperHost, managedDatastoreName, lstHosts);
    }
    for (HostHostBusAdapter hba : hostStorageSystem.getStorageDeviceInfo().getHostBusAdapter()) {
        if (hba instanceof HostInternetScsiHba && ((HostInternetScsiHba) hba).isIsSoftwareBased()) {
            List<HostInternetScsiHbaStaticTarget> lstTargets = ((HostInternetScsiHba) hba).getConfiguredStaticTarget();
            if (lstTargets != null) {
                for (HostInternetScsiHbaStaticTarget target : lstTargets) {
                    if (managedDatastoreNames.contains(VmwareResource.createDatastoreNameFromIqn(target.getIScsiName()))) {
                        lstManagedTargets.add(target);
                    }
                }
            }
        }
    }
    ExecutorService executorService = Executors.newFixedThreadPool(lstHosts.size());
    for (Pair<ManagedObjectReference, String> host : lstHosts) {
        List<Pair<ManagedObjectReference, String>> hosts = new ArrayList<>();
        hosts.add(host);
        List<HostInternetScsiHbaStaticTarget> staticTargetsForHost = new ArrayList<>();
        for (HostInternetScsiHbaStaticTarget iScsiManagedTarget : lstManagedTargets) {
            String storageAddress = iScsiManagedTarget.getAddress();
            HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageAddress, hosts);
            List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
            if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
                staticTargetsForHost.add(iScsiManagedTarget);
            }
        }
        if (staticTargetsForHost.size() > 0) {
            executorService.submit(new Thread(() -> {
                try {
                    addRemoveInternetScsiTargetsToAllHosts(context, false, staticTargetsForHost, hosts);
                    rescanAllHosts(context, hosts, true, false);
                } catch (Exception ex) {
                    s_logger.warn(ex.getMessage());
                }
            }));
        }
    }
    executorService.shutdown();
    if (!executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES)) {
        throw new Exception("The system timed out before completing the task 'removeManagedTargetsFromCluster'.");
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) ArrayList(java.util.ArrayList) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HostInternetScsiHba(com.vmware.vim25.HostInternetScsiHba) ExecutorService(java.util.concurrent.ExecutorService) HostStorageSystemMO(com.cloud.hypervisor.vmware.mo.HostStorageSystemMO) HostHostBusAdapter(com.vmware.vim25.HostHostBusAdapter) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 83 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext 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 84 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext in project cloudstack by apache.

the class VmwareStorageProcessor method removeVmfsDatastore.

private void removeVmfsDatastore(Command cmd, VmwareHypervisorHost hyperHost, String datastoreName, String storageIpAddress, int storagePortNumber, String iqn, List<Pair<ManagedObjectReference, String>> lstHosts) throws Exception {
    VmwareContext context = hostService.getServiceContext(cmd);
    unmountVmfsDatastore(context, hyperHost, datastoreName, lstHosts);
    HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageIpAddress, lstHosts);
    List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
    if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
        HostInternetScsiHbaStaticTarget target = new HostInternetScsiHbaStaticTarget();
        target.setAddress(storageIpAddress);
        target.setPort(storagePortNumber);
        target.setIScsiName(iqn);
        final List<HostInternetScsiHbaStaticTarget> lstTargets = new ArrayList<>();
        lstTargets.add(target);
        addRemoveInternetScsiTargetsToAllHosts(false, lstTargets, hostsUsingStaticDiscovery);
        rescanAllHosts(hostsUsingStaticDiscovery, true, false);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) ArrayList(java.util.ArrayList)

Example 85 with VmwareContext

use of com.cloud.hypervisor.vmware.util.VmwareContext 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)

Aggregations

VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)106 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)66 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)65 RemoteException (java.rmi.RemoteException)53 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)49 UnsupportedEncodingException (java.io.UnsupportedEncodingException)48 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)39 IOException (java.io.IOException)38 CloudException (com.cloud.exception.CloudException)36 ConfigurationException (javax.naming.ConfigurationException)34 InternalErrorException (com.cloud.exception.InternalErrorException)31 ConnectException (java.net.ConnectException)31 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)26 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)23 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)21 Pair (com.cloud.utils.Pair)20 ClusterMO (com.cloud.hypervisor.vmware.mo.ClusterMO)15 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)14 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)13 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)13