Search in sources :

Example 66 with VmwareHypervisorHost

use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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 67 with VmwareHypervisorHost

use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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 68 with VmwareHypervisorHost

use of com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost 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 69 with VmwareHypervisorHost

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

the class VmwareStorageProcessor method cleanUpDatastore.

/**
 * 1) Possibly expand the datastore.
 * 2) Possibly consolidate all relevant VMDK files into one VMDK file.
 * 3) Possibly move the VMDK file to the root folder (may already be there).
 * 4) If the VMDK file wasn't already in the root folder, then delete the folder the VMDK file was in.
 * 5) Possibly rename the VMDK file (this will lead to there being a delta file with the new name and the
 *    original file with the original name).
 *
 * Note: If the underlying VMDK file was for a root disk, the 'vmdk' parameter's value might look, for example,
 *  like "i-2-32-VM/ROOT-32.vmdk".
 *
 * Note: If the underlying VMDK file was for a data disk, the 'vmdk' parameter's value might look, for example,
 *  like "-iqn.2010-01.com.solidfire:4nhe.data-32.79-0.vmdk".
 *
 * Returns the (potentially new) name of the VMDK file.
 */
private String cleanUpDatastore(Command cmd, HostDatastoreSystemMO hostDatastoreSystem, DatastoreMO dsMo, Map<String, String> details) throws Exception {
    s_logger.debug(String.format("Executing clean up in DataStore: [%s].", dsMo.getName()));
    boolean expandDatastore = Boolean.parseBoolean(details.get(DiskTO.EXPAND_DATASTORE));
    // reserve value in use is set to the minimum for the cloned volume), but that's fine.
    if (expandDatastore) {
        expandDatastore(hostDatastoreSystem, dsMo);
    }
    String vmdk = details.get(DiskTO.VMDK);
    String fullVmdkPath = new DatastoreFile(dsMo.getName(), vmdk).getPath();
    VmwareContext context = hostService.getServiceContext(null);
    VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
    DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
    String vmName = getVmName(vmdk);
    // If vmName is not null, then move all VMDK files out of this folder to the root folder and then delete the folder named vmName.
    if (vmName != null) {
        String workerVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
        VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName, null);
        if (vmMo == null) {
            throw new Exception("Unable to create a worker VM for volume creation");
        }
        vmMo.attachDisk(new String[] { fullVmdkPath }, dsMo.getMor());
        List<String> backingFiles = new ArrayList<>(1);
        List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
        VirtualDisk virtualDisk = virtualDisks.get(0);
        VirtualDeviceBackingInfo virtualDeviceBackingInfo = virtualDisk.getBacking();
        while (virtualDeviceBackingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
            VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) virtualDeviceBackingInfo;
            backingFiles.add(backingInfo.getFileName());
            virtualDeviceBackingInfo = backingInfo.getParent();
        }
        vmMo.detachAllDisksAndDestroy();
        VmwareStorageLayoutHelper.moveVolumeToRootFolder(dcMo, backingFiles);
        vmdk = new DatastoreFile(vmdk).getFileName();
        // Delete the folder the VMDK file was in.
        DatastoreFile folderToDelete = new DatastoreFile(dsMo.getName(), vmName);
        dsMo.deleteFolder(folderToDelete.getPath(), dcMo.getMor());
    }
    return vmdk;
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ArrayList(java.util.ArrayList) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VirtualDisk(com.vmware.vim25.VirtualDisk) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 70 with VmwareHypervisorHost

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

the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
    VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    String vmName = srcVolume.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        DataStoreTO primaryStorage = srcVolume.getDataStore();
        NfsTO destStore = (NfsTO) destVolume.getDataStore();
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0, null));
        VolumeObjectTO newVolume = new VolumeObjectTO();
        newVolume.setPath(result.first() + File.separator + result.second());
        return new CopyCmdAnswer(newVolume);
    } catch (Throwable e) {
        return new CopyCmdAnswer(hostService.createLogMessageException(e, cmd));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Aggregations

VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)85 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)65 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)65 RemoteException (java.rmi.RemoteException)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)57 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)48 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)46 CloudException (com.cloud.exception.CloudException)40 InternalErrorException (com.cloud.exception.InternalErrorException)40 IOException (java.io.IOException)40 ConnectException (java.net.ConnectException)40 ConfigurationException (javax.naming.ConfigurationException)40 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)31 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)27 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)23 Pair (com.cloud.utils.Pair)15 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)12 Answer (com.cloud.agent.api.Answer)11 AttachIsoAnswer (com.cloud.agent.api.AttachIsoAnswer)10 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)10