Search in sources :

Example 56 with KvmPhysicalDisk

use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.

the class LibvirtComputingResource method templateToPrimaryDownload.

// this is much like PrimaryStorageDownloadCommand, but keeping it separate
public KVMPhysicalDisk templateToPrimaryDownload(final String templateUrl, final KVMStoragePool primaryPool, final String volUuid) {
    final int index = templateUrl.lastIndexOf("/");
    final String mountpoint = templateUrl.substring(0, index);
    String templateName = null;
    if (index < templateUrl.length() - 1) {
        templateName = templateUrl.substring(index + 1);
    }
    KVMPhysicalDisk templateVol = null;
    KVMStoragePool secondaryPool = null;
    try {
        secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
        /* Get template vol */
        if (templateName == null) {
            secondaryPool.refresh();
            final List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
            if (disks == null || disks.isEmpty()) {
                s_logger.error("Failed to get volumes from pool: " + secondaryPool.getUuid());
                return null;
            }
            for (final KVMPhysicalDisk disk : disks) {
                if (disk.getName().endsWith("qcow2")) {
                    templateVol = disk;
                    break;
                }
            }
            if (templateVol == null) {
                s_logger.error("Failed to get template from pool: " + secondaryPool.getUuid());
                return null;
            }
        } else {
            templateVol = secondaryPool.getPhysicalDisk(templateName);
        }
        /* Copy volume to primary storage */
        final KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(templateVol, volUuid, primaryPool, 0);
        return primaryVol;
    } catch (final CloudRuntimeException e) {
        s_logger.error("Failed to download template to primary storage", e);
        return null;
    } finally {
        if (secondaryPool != null) {
            _storagePoolMgr.deleteStoragePool(secondaryPool.getType(), secondaryPool.getUuid());
        }
    }
}
Also used : KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 57 with KvmPhysicalDisk

use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.

the class LibvirtComputingResource method getVolumePath.

public String getVolumePath(final Connect conn, final DiskTO volume, boolean diskOnHostCache) throws LibvirtException, URISyntaxException {
    final DataTO data = volume.getData();
    final DataStoreTO store = data.getDataStore();
    if (volume.getType() == Volume.Type.ISO && data.getPath() != null && (store instanceof NfsTO || store instanceof PrimaryDataStoreTO && data instanceof TemplateObjectTO && !((TemplateObjectTO) data).isDirectDownload())) {
        if (data.getPath().startsWith(ConfigDrive.CONFIGDRIVEDIR) && diskOnHostCache) {
            String configDrivePath = getConfigPath() + "/" + data.getPath();
            return configDrivePath;
        }
        final String isoPath = store.getUrl().split("\\?")[0] + File.separator + data.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String path = isoPath.substring(0, index);
        final String name = isoPath.substring(index + 1);
        final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        return isoVol.getPath();
    } else {
        return data.getPath();
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 58 with KvmPhysicalDisk

use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.

the class LibvirtComputingResource method attachOrDetachISO.

public synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach, final Integer diskSeq) throws LibvirtException, URISyntaxException, InternalErrorException {
    final DiskDef iso = new DiskDef();
    if (isoPath != null && isAttach) {
        final int index = isoPath.lastIndexOf("/");
        final String path = isoPath.substring(0, index);
        final String name = isoPath.substring(index + 1);
        final KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(path);
        final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
        isoPath = isoVol.getPath();
        iso.defISODisk(isoPath, diskSeq);
    } else {
        iso.defISODisk(null, diskSeq);
    }
    final String result = attachOrDetachDevice(conn, true, vmName, iso.toString());
    if (result == null && !isAttach) {
        final List<DiskDef> disks = getDisks(conn, vmName);
        for (final DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.CDROM && (diskSeq == null || disk.getDiskLabel() == iso.getDiskLabel())) {
                cleanupDisk(disk);
            }
        }
    }
    return result;
}
Also used : DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)

Example 59 with KvmPhysicalDisk

use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.

the class LibvirtCopyVolumeCommandWrapper method handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails.

private Answer handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails(CopyVolumeCommand command, LibvirtComputingResource libvirtComputingResource) {
    KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    PrimaryDataStoreTO srcPrimaryDataStore = null;
    KVMStoragePool secondaryStoragePool = null;
    Map<String, String> srcDetails = command.getSrcDetails();
    String srcPath = srcDetails.get(DiskTO.IQN);
    if (srcPath == null) {
        return new CopyVolumeAnswer(command, false, "No IQN was specified", null, null);
    }
    try {
        LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        String destVolumeName = libvirtUtilitiesHelper.generateUUIDName() + ".qcow2";
        String destVolumePath = command.getVolumePath() + File.separator;
        String secondaryStorageUrl = command.getSecondaryStorageURL();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
        secondaryStoragePool.createFolder(File.separator + destVolumePath);
        storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + File.separator + destVolumePath);
        VolumeObjectTO srcVolumeObjectTO = (VolumeObjectTO) command.getSrcData();
        srcPrimaryDataStore = (PrimaryDataStoreTO) srcVolumeObjectTO.getDataStore();
        storagePoolMgr.connectPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath, srcDetails);
        KVMPhysicalDisk srcPhysicalDisk = storagePoolMgr.getPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath);
        storagePoolMgr.copyPhysicalDisk(srcPhysicalDisk, destVolumeName, secondaryStoragePool, command.getWait() * 1000);
        return new CopyVolumeAnswer(command, true, null, null, destVolumePath + destVolumeName);
    } catch (final CloudRuntimeException e) {
        return new CopyVolumeAnswer(command, false, e.toString(), null, null);
    } finally {
        try {
            if (srcPrimaryDataStore != null) {
                storagePoolMgr.disconnectPhysicalDisk(srcPrimaryDataStore.getPoolType(), srcPrimaryDataStore.getUuid(), srcPath);
            }
        } catch (Exception e) {
            LOGGER.warn("Unable to disconnect from the source device.", e);
        }
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 60 with KvmPhysicalDisk

use of com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk in project cloudstack by apache.

the class LibvirtCopyVolumeCommandWrapper method execute.

@Override
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    /**
     * This method is only used for copying files from Primary Storage TO
     * Secondary Storage
     *
     * It COULD also do it the other way around, but the code in the
     * ManagementServerImpl shows that it always sets copyToSecondary to
     * true
     */
    Map<String, String> srcDetails = command.getSrcDetails();
    if (srcDetails != null) {
        return handleCopyDataFromVolumeToSecondaryStorageUsingSrcDetails(command, libvirtComputingResource);
    }
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final boolean copyToSecondary = command.toSecondaryStorage();
    final StorageFilerTO pool = command.getPool();
    final String secondaryStorageUrl = command.getSecondaryStorageURL();
    KVMStoragePool secondaryStoragePool = null;
    String volumePath;
    KVMStoragePool primaryPool;
    try {
        try {
            primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
        } catch (final CloudRuntimeException e) {
            if (e.getMessage().contains("not found")) {
                primaryPool = storagePoolMgr.createStoragePool(pool.getUuid(), pool.getHost(), pool.getPort(), pool.getPath(), pool.getUserInfo(), pool.getType());
            } else {
                return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
            }
        }
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final String volumeName = libvirtUtilitiesHelper.generateUUIDName();
        if (copyToSecondary) {
            final String destVolumeName = volumeName + ".qcow2";
            final KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(command.getVolumePath());
            final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
            secondaryStoragePool.createFolder(volumeDestPath);
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumeDestPath);
            storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        } else {
            volumePath = "/volumes/" + command.getVolumeId() + File.separator;
            secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumePath);
            final KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(command.getVolumePath() + ".qcow2");
            storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
            return new CopyVolumeAnswer(command, true, null, null, volumeName);
        }
    } catch (final CloudRuntimeException e) {
        return new CopyVolumeAnswer(command, false, e.toString(), null, null);
    } finally {
        if (secondaryStoragePool != null) {
            storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO)

Aggregations

Answer (com.cloud.agent.api.Answer)45 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)42 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)41 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)41 Test (org.junit.Test)41 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)40 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)35 KvmPhysicalDisk (com.cloud.hypervisor.kvm.storage.KvmPhysicalDisk)35 KvmStoragePool (com.cloud.hypervisor.kvm.storage.KvmStoragePool)34 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)31 KvmStoragePoolManager (com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager)31 StoragePool (com.cloud.storage.StoragePool)28 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)21 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)21 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 AttachAnswer (com.cloud.storage.command.AttachAnswer)20 LibvirtException (org.libvirt.LibvirtException)16 NfsStoragePool (com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool)14