Search in sources :

Example 1 with DatastoreMO

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

the class VmwareResource method deleteUnregisteredVmFiles.

private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, DatacenterMO dcMo, boolean deleteDisks) throws Exception {
    s_logger.debug("Deleting files associated with an existing VM that was unregistered");
    DatastoreFile vmFolder = null;
    try {
        List<VirtualMachineFileLayoutExFileInfo> fileInfo = vmFileLayout.getFile();
        for (VirtualMachineFileLayoutExFileInfo file : fileInfo) {
            DatastoreFile fileInDatastore = new DatastoreFile(file.getName());
            // In case of linked clones, VM file layout includes the base disk so don't delete all disk files.
            if (file.getType().startsWith("disk") || file.getType().startsWith("digest"))
                continue;
            else if (file.getType().equals("config"))
                vmFolder = new DatastoreFile(fileInDatastore.getDatastoreName(), fileInDatastore.getDir());
            DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(fileInDatastore.getDatastoreName()));
            s_logger.debug("Deleting file: " + file.getName());
            dsMo.deleteFile(file.getName(), dcMo.getMor(), true);
        }
        // Delete files that are present in the VM folder - this will take care of the VM disks as well.
        DatastoreMO vmFolderDsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(vmFolder.getDatastoreName()));
        String[] files = vmFolderDsMo.listDirContent(vmFolder.getPath());
        if (deleteDisks) {
            for (String file : files) {
                String vmDiskFileFullPath = String.format("%s/%s", vmFolder.getPath(), file);
                s_logger.debug("Deleting file: " + vmDiskFileFullPath);
                vmFolderDsMo.deleteFile(vmDiskFileFullPath, dcMo.getMor(), true);
            }
        }
        // Delete VM folder
        if (deleteDisks || files.length == 0) {
            s_logger.debug("Deleting folder: " + vmFolder.getPath());
            vmFolderDsMo.deleteFolder(vmFolder.getPath(), dcMo.getMor());
        }
    } catch (Exception e) {
        String message = "Failed to delete files associated with an existing VM that was unregistered due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.warn(message, e);
    }
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineFileLayoutExFileInfo(com.vmware.vim25.VirtualMachineFileLayoutExFileInfo) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) 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)

Example 2 with DatastoreMO

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

the class VmwareResource method syncDiskChain.

// return the finalized disk chain for startup, from top to bottom
private String[] syncDiskChain(DatacenterMO dcMo, VirtualMachineMO vmMo, VirtualMachineTO vmSpec, DiskTO vol, VirtualMachineDiskInfo diskInfo, HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails) throws Exception {
    VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData();
    DataStoreTO primaryStore = volumeTO.getDataStore();
    Map<String, String> details = vol.getDetails();
    boolean isManaged = false;
    String iScsiName = null;
    if (details != null) {
        isManaged = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
        iScsiName = details.get(DiskTO.IQN);
    }
    // if the storage is managed, iScsiName should not be null
    String datastoreName = isManaged ? VmwareResource.getDatastoreName(iScsiName) : primaryStore.getUuid();
    Pair<ManagedObjectReference, DatastoreMO> volumeDsDetails = dataStoresDetails.get(datastoreName);
    if (volumeDsDetails == null) {
        throw new Exception("Primary datastore " + primaryStore.getUuid() + " is not mounted on host.");
    }
    DatastoreMO dsMo = volumeDsDetails.second();
    // we will honor vCenter's meta if it exists
    if (diskInfo != null) {
        // to deal with run-time upgrade to maintain the new datastore folder structure
        String[] disks = diskInfo.getDiskChain();
        for (int i = 0; i < disks.length; i++) {
            DatastoreFile file = new DatastoreFile(disks[i]);
            if (!isManaged && file.getDir() != null && file.getDir().isEmpty()) {
                s_logger.info("Perform run-time datastore folder upgrade. sync " + disks[i] + " to VM folder");
                disks[i] = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, file.getFileBaseName());
            }
        }
        return disks;
    }
    final String datastoreDiskPath;
    if (isManaged) {
        if (volumeTO.getVolumeType() == Volume.Type.ROOT) {
            datastoreDiskPath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, volumeTO.getName());
        } else {
            datastoreDiskPath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
        }
    } else {
        datastoreDiskPath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, volumeTO.getPath());
    }
    if (!dsMo.fileExists(datastoreDiskPath)) {
        s_logger.warn("Volume " + volumeTO.getId() + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath);
    }
    return new String[] { datastoreDiskPath };
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) 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) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 3 with DatastoreMO

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

the class VmwareResource method getDatastoreThatRootDiskIsOn.

private DatastoreMO getDatastoreThatRootDiskIsOn(HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails, DiskTO[] disks) {
    Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null;
    for (DiskTO vol : disks) {
        if (vol.getType() == Volume.Type.ROOT) {
            Map<String, String> details = vol.getDetails();
            boolean managed = false;
            if (details != null) {
                managed = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
            }
            if (managed) {
                String datastoreName = VmwareResource.getDatastoreName(details.get(DiskTO.IQN));
                rootDiskDataStoreDetails = dataStoresDetails.get(datastoreName);
                break;
            } else {
                DataStoreTO primaryStore = vol.getData().getDataStore();
                rootDiskDataStoreDetails = dataStoresDetails.get(primaryStore.getUuid());
                break;
            }
        }
    }
    if (rootDiskDataStoreDetails != null) {
        return rootDiskDataStoreDetails.second();
    }
    return null;
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 4 with DatastoreMO

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

the class VmwareResource method execute.

protected Answer execute(GetStoragePoolCapabilitiesCommand cmd) {
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        HostMO host = (HostMO) hyperHost;
        StorageFilerTO pool = cmd.getPool();
        ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid());
        if (morDatastore == null) {
            morDatastore = hyperHost.mountDatastore((pool.getType() == StoragePoolType.VMFS || pool.getType() == StoragePoolType.PreSetup || pool.getType() == StoragePoolType.DatastoreCluster), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUuid().replace("-", ""), true);
        }
        assert (morDatastore != null);
        DatastoreMO dsMo = new DatastoreMO(getServiceContext(), morDatastore);
        GetStoragePoolCapabilitiesAnswer answer = new GetStoragePoolCapabilitiesAnswer(cmd);
        boolean hardwareAccelerationSupportForDataStore = getHardwareAccelerationSupportForDataStore(host.getMor(), dsMo.getName());
        Map<String, String> poolDetails = answer.getPoolDetails();
        poolDetails.put(Storage.Capability.HARDWARE_ACCELERATION.toString(), String.valueOf(hardwareAccelerationSupportForDataStore));
        answer.setPoolDetails(poolDetails);
        answer.setResult(true);
        return answer;
    } catch (Throwable e) {
        GetStoragePoolCapabilitiesAnswer answer = new GetStoragePoolCapabilitiesAnswer(cmd);
        answer.setResult(false);
        answer.setDetails(createLogMessageException(e, cmd));
        return answer;
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) GetStoragePoolCapabilitiesAnswer(com.cloud.agent.api.GetStoragePoolCapabilitiesAnswer)

Example 5 with DatastoreMO

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

the class VmwareResource method getDataStoreWhereDiskExists.

private DatastoreMO getDataStoreWhereDiskExists(VmwareHypervisorHost hyperHost, VmwareContext context, VirtualMachineDiskInfoBuilder diskInfoBuilder, DiskTO disk, List<Pair<Integer, ManagedObjectReference>> diskDatastores) throws Exception {
    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) 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