Search in sources :

Example 51 with DatastoreMO

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

the class VmwareStorageProcessor method syncVolumePath.

@Override
public Answer syncVolumePath(SyncVolumePathCommand cmd) {
    DiskTO disk = cmd.getDisk();
    VolumeObjectTO volumeTO = (VolumeObjectTO) disk.getData();
    DataStoreTO primaryStore = volumeTO.getDataStore();
    String volumePath = volumeTO.getPath();
    String vmName = volumeTO.getVmName();
    boolean datastoreChangeObserved = false;
    boolean volumePathChangeObserved = false;
    String chainInfo = 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 SyncVolumePathCommand, vmName: " + vmName;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        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);
                    }
                }
            }
        }
        SyncVolumePathAnswer answer = new SyncVolumePathAnswer(disk);
        if (datastoreChangeObserved) {
            answer.setContextParam("datastoreName", datastoreUUID);
        }
        if (volumePathChangeObserved) {
            answer.setContextParam("volumePath", volumePath);
        }
        if (chainInfo != null && !chainInfo.isEmpty()) {
            answer.setContextParam("chainInfo", chainInfo);
        }
        return answer;
    } catch (Throwable e) {
        return new SyncVolumePathAnswer(hostService.createLogMessageException(e, cmd));
    }
}
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) SyncVolumePathAnswer(org.apache.cloudstack.storage.command.SyncVolumePathAnswer) 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) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 52 with DatastoreMO

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

the class VmwareStorageProcessor method getVmfsDatastore.

private ManagedObjectReference getVmfsDatastore(VmwareContext context, VmwareHypervisorHost hyperHost, String datastoreName, String storageIpAddress, int storagePortNumber, String iqn, String chapName, String chapSecret, String mutualChapName, String mutualChapSecret) throws Exception {
    ManagedObjectReference morDs;
    ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
    ClusterMO cluster = new ClusterMO(context, morCluster);
    List<Pair<ManagedObjectReference, String>> lstHosts = cluster.getClusterHosts();
    Pair<ManagedObjectReference, String> firstHost = lstHosts.get(0);
    HostMO firstHostMO = new HostMO(context, firstHost.first());
    HostDatastoreSystemMO firstHostDatastoreSystemMO = firstHostMO.getHostDatastoreSystemMO();
    HostDiscoveryMethod hostDiscoveryMethod = getHostDiscoveryMethod(context, storageIpAddress, lstHosts);
    List<HostMO> hostsUsingStaticDiscovery = hostDiscoveryMethod.getHostsUsingStaticDiscovery();
    if (hostsUsingStaticDiscovery != null && hostsUsingStaticDiscovery.size() > 0) {
        List<HostInternetScsiHbaStaticTarget> lstTargets = getTargets(storageIpAddress, storagePortNumber, iqn, chapName, chapSecret, mutualChapName, mutualChapSecret);
        addRemoveInternetScsiTargetsToAllHosts(true, lstTargets, hostsUsingStaticDiscovery);
    }
    rescanAllHosts(context, lstHosts, true, false);
    HostStorageSystemMO firstHostStorageSystem = firstHostMO.getHostStorageSystemMO();
    List<HostScsiDisk> lstHostScsiDisks = firstHostDatastoreSystemMO.queryAvailableDisksForVmfs();
    HostScsiDisk hostScsiDisk = getHostScsiDisk(firstHostStorageSystem.getStorageDeviceInfo().getScsiTopology(), lstHostScsiDisks, iqn);
    if (hostScsiDisk == null) {
        rescanAllHosts(context, lstHosts, false, true);
        morDs = firstHostDatastoreSystemMO.findDatastoreByName(datastoreName);
        if (morDs != null) {
            waitForAllHostsToSeeDatastore(lstHosts, new DatastoreMO(context, morDs));
            mountVmfsDatastore(new DatastoreMO(context, morDs), lstHosts);
            expandDatastore(firstHostDatastoreSystemMO, new DatastoreMO(context, morDs));
            return morDs;
        }
        throw new Exception("A relevant SCSI disk could not be located to use to create a datastore.");
    }
    morDs = firstHostDatastoreSystemMO.createVmfsDatastore(datastoreName, hostScsiDisk);
    if (morDs != null) {
        waitForAllHostsToMountDatastore(lstHosts, new DatastoreMO(context, morDs));
        expandDatastore(firstHostDatastoreSystemMO, new DatastoreMO(context, morDs));
        return morDs;
    }
    throw new Exception("Unable to create a datastore");
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HostInternetScsiHbaStaticTarget(com.vmware.vim25.HostInternetScsiHbaStaticTarget) HostDatastoreSystemMO(com.cloud.hypervisor.vmware.mo.HostDatastoreSystemMO) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HostScsiDisk(com.vmware.vim25.HostScsiDisk) HostStorageSystemMO(com.cloud.hypervisor.vmware.mo.HostStorageSystemMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 53 with DatastoreMO

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

the class VmwareStorageProcessor method cloneVolumeFromBaseTemplate.

@Override
public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataTO destData = cmd.getDestTO();
    VolumeObjectTO volume = (VolumeObjectTO) destData;
    DataStoreTO primaryStore = volume.getDataStore();
    DataStoreTO srcStore = template.getDataStore();
    String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
    try {
        VmwareContext context = hostService.getServiceContext(null);
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        VirtualMachineMO vmMo = null;
        ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
        if (morDatastore == null) {
            throw new Exception("Unable to find datastore in vSphere");
        }
        DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
        String vmdkName = volume.getName();
        String vmName = volume.getVmName();
        String vmdkFileBaseName = null;
        if (template.isDeployAsIs() && volume.getVolumeType() == Volume.Type.ROOT) {
            VirtualMachineMO existingVm = dcMo.findVm(vmName);
            if (volume.getDeviceId().equals(0L)) {
                if (existingVm != null) {
                    s_logger.info(String.format("Found existing VM wth name [%s] before cloning from template, destroying it", vmName));
                    existingVm.detachAllDisksAndDestroy();
                }
                s_logger.info("ROOT Volume from deploy-as-is template, cloning template");
                cloneVMFromTemplate(hyperHost, template.getPath(), vmName, primaryStore.getUuid());
            } else {
                s_logger.info("ROOT Volume from deploy-as-is template, volume already created at this point");
            }
        } else {
            if (srcStore == null) {
                // create a root volume for blank VM (created from ISO)
                String dummyVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
                try {
                    vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null);
                    if (vmMo == null) {
                        throw new Exception("Unable to create a dummy VM for volume creation");
                    }
                    vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
                    // we only use the first file in the pair, linked or not will not matter
                    String[] vmdkFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, true);
                    String volumeDatastorePath = vmdkFilePair[0];
                    synchronized (this) {
                        s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
                        VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo, searchExcludedFolders);
                        vmMo.createDisk(volumeDatastorePath, (long) (volume.getSize() / (1024L * 1024L)), morDatastore, -1, null);
                        vmMo.detachDisk(volumeDatastorePath, false);
                    }
                } finally {
                    s_logger.info("Destroy dummy VM after volume creation");
                    if (vmMo != null) {
                        s_logger.warn("Unable to destroy a null VM ManagedObjectReference");
                        vmMo.detachAllDisksAndDestroy();
                    }
                }
            } else {
                String templatePath = template.getPath();
                VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templatePath), true);
                if (vmTemplate == null) {
                    s_logger.warn("Template host in vSphere is not in connected state, request template reload");
                    return new CopyCmdAnswer("Template host in vSphere is not in connected state, request template reload");
                }
                if (dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
                    vmdkFileBaseName = cloneVMforVvols(context, hyperHost, template, vmTemplate, volume, dcMo, dsMo);
                } else {
                    vmdkFileBaseName = createVMAndFolderWithVMName(context, hyperHost, template, vmTemplate, volume, dcMo, dsMo, searchExcludedFolders);
                }
            }
            // restoreVM - move the new ROOT disk into corresponding VM folder
            VirtualMachineMO restoreVmMo = dcMo.findVm(volume.getVmName());
            if (restoreVmMo != null) {
                if (!dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
                    // VM folder name in datastore will be VM's name in vCenter.
                    String vmNameInVcenter = restoreVmMo.getName();
                    if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmNameInVcenter)) {
                        VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmNameInVcenter, dsMo, vmdkFileBaseName, searchExcludedFolders);
                    }
                }
            }
        }
        VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setPath(vmdkFileBaseName);
        if (template.isDeployAsIs()) {
            newVol.setSize(volume.getSize());
        } else if (template.getSize() != null) {
            newVol.setSize(template.getSize());
        } else {
            newVol.setSize(volume.getSize());
        }
        return new CopyCmdAnswer(newVol);
    } catch (Throwable e) {
        return new CopyCmdAnswer(hostService.createLogMessageException(e, cmd));
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) 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) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 54 with DatastoreMO

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

the class VmwareResource method execute.

protected AttachIsoAnswer execute(AttachIsoCommand cmd) {
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        if (vmMo == null) {
            String msg = "Unable to find VM in vSphere to execute AttachIsoCommand, vmName: " + cmd.getVmName();
            s_logger.error(msg);
            throw new Exception(msg);
        }
        String storeUrl = cmd.getStoreUrl();
        if (storeUrl == null) {
            if (!cmd.getIsoPath().equalsIgnoreCase(TemplateManager.VMWARE_TOOLS_ISO)) {
                String msg = "ISO store root url is not found in AttachIsoCommand";
                s_logger.error(msg);
                throw new Exception(msg);
            } else {
                if (cmd.isAttach()) {
                    vmMo.mountToolsInstaller();
                } else {
                    try {
                        if (!vmMo.unmountToolsInstaller()) {
                            return new AttachIsoAnswer(cmd, false, "Failed to unmount vmware-tools installer ISO as the corresponding CDROM device is locked by VM. Please unmount the CDROM device inside the VM and ret-try.");
                        }
                    } catch (Throwable e) {
                        vmMo.detachIso(null, cmd.isForce());
                    }
                }
                return new AttachIsoAnswer(cmd);
            }
        }
        ManagedObjectReference morSecondaryDs = prepareSecondaryDatastoreOnHost(storeUrl);
        String isoPath = cmd.getIsoPath();
        if (!isoPath.startsWith(storeUrl)) {
            assert (false);
            String msg = "ISO path does not start with the secondary storage root";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        int isoNameStartPos = isoPath.lastIndexOf('/');
        String isoFileName = isoPath.substring(isoNameStartPos + 1);
        String isoStorePathFromRoot = isoPath.substring(storeUrl.length() + 1, isoNameStartPos + 1);
        // TODO, check if iso is already attached, or if there is a previous
        // attachment
        DatastoreMO secondaryDsMo = new DatastoreMO(getServiceContext(), morSecondaryDs);
        String storeName = secondaryDsMo.getName();
        String isoDatastorePath = String.format("[%s] %s%s", storeName, isoStorePathFromRoot, isoFileName);
        if (cmd.isAttach()) {
            vmMo.attachIso(isoDatastorePath, morSecondaryDs, true, false, cmd.getDeviceKey(), cmd.isForce());
            return new AttachIsoAnswer(cmd);
        } else {
            int key = vmMo.detachIso(isoDatastorePath, cmd.isForce());
            return new AttachIsoAnswer(cmd, key);
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String message = String.format("AttachIsoCommand(%s) failed due to [%s].", cmd.isAttach() ? "attach" : "detach", VmwareHelper.getExceptionMessage(e));
        s_logger.error(message, e);
        return new AttachIsoAnswer(cmd, false, message);
    }
}
Also used : AttachIsoAnswer(com.cloud.agent.api.AttachIsoAnswer) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) 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