Search in sources :

Example 1 with StoragePool

use of org.libvirt.StoragePool in project CloudStack-archive by CloudStack-extras.

the class LibvirtStorageAdaptor method createFileBasedStoragePool.

public StoragePool createFileBasedStoragePool(Connect conn, String localStoragePath, String uuid) {
    if (!(_storageLayer.exists(localStoragePath) && _storageLayer.isDirectory(localStoragePath))) {
        return null;
    }
    File path = new File(localStoragePath);
    if (!(path.canWrite() && path.canRead() && path.canExecute())) {
        return null;
    }
    StoragePool pool = null;
    try {
        pool = conn.storagePoolLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
    }
    if (pool == null) {
        LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid, null, null, localStoragePath);
        try {
            pool = conn.storagePoolDefineXML(spd.toString(), 0);
            pool.create(0);
        } catch (LibvirtException e) {
            if (pool != null) {
                try {
                    pool.destroy();
                    pool.undefine();
                } catch (LibvirtException e1) {
                }
                pool = null;
            }
            throw new CloudRuntimeException(e.toString());
        }
    }
    try {
        StoragePoolInfo spi = pool.getInfo();
        if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            pool.create(0);
        }
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    return pool;
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolInfo(org.libvirt.StoragePoolInfo) LibvirtStoragePoolDef(com.cloud.agent.resource.computing.LibvirtStoragePoolDef) File(java.io.File)

Example 2 with StoragePool

use of org.libvirt.StoragePool in project CloudStack-archive by CloudStack-extras.

the class LibvirtStorageAdaptor method listPhysicalDisks.

@Override
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid, KVMStoragePool pool) {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    StoragePool virtPool = libvirtPool.getPool();
    List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
    try {
        String[] vols = virtPool.listVolumes();
        for (String volName : vols) {
            KVMPhysicalDisk disk = this.getPhysicalDisk(volName, pool);
            disks.add(disk);
        }
        return disks;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList)

Example 3 with StoragePool

use of org.libvirt.StoragePool in project CloudStack-archive by CloudStack-extras.

the class LibvirtStorageAdaptor method deleteStoragePool.

@Override
public boolean deleteStoragePool(KVMStoragePool pool) {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    StoragePool virtPool = libvirtPool.getPool();
    try {
        virtPool.destroy();
        virtPool.undefine();
        virtPool.free();
    } catch (LibvirtException e) {
        return false;
    }
    return true;
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException)

Example 4 with StoragePool

use of org.libvirt.StoragePool in project CloudStack-archive by CloudStack-extras.

the class LibvirtStorageAdaptor method getVolumeFromURI.

public StorageVol getVolumeFromURI(Connect conn, String volPath) throws LibvirtException, URISyntaxException {
    int index = volPath.lastIndexOf("/");
    URI volDir = null;
    StoragePool sp = null;
    StorageVol vol = null;
    try {
        volDir = new URI(volPath.substring(0, index));
        String volName = volPath.substring(index + 1);
        sp = getStoragePoolbyURI(conn, volDir);
        vol = sp.storageVolLookupByName(volName);
        return vol;
    } catch (LibvirtException e) {
        s_logger.debug("Faild to get vol path: " + e.toString());
        throw e;
    } finally {
        try {
            if (sp != null) {
                sp.free();
            }
        } catch (LibvirtException e) {
        }
    }
}
Also used : StoragePool(org.libvirt.StoragePool) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) URI(java.net.URI)

Example 5 with StoragePool

use of org.libvirt.StoragePool in project cloudstack by apache.

the class ManagedNfsStorageAdaptor method getPhysicalDisk.

/*
     * creates a disk based on the created nfs storage pool using libvirt
     */
@Override
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
    // now create the volume upon the given storage pool in kvm
    Connect conn;
    StoragePool virtPool = null;
    try {
        conn = LibvirtConnection.getConnection();
        virtPool = conn.storagePoolLookupByName("/" + volumeUuid);
    } catch (LibvirtException e1) {
        throw new CloudRuntimeException(e1.toString());
    }
    LibvirtStorageVolumeDef.VolumeFormat libvirtformat = null;
    long volCapacity = 0;
    // check whether the volume is present on the given pool
    StorageVol vol = getVolume(virtPool, volumeUuid);
    try {
        if (vol == null) {
            libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.QCOW2;
            StoragePoolInfo poolinfo = virtPool.getInfo();
            volCapacity = poolinfo.available;
            LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(volumeUuid, volCapacity, libvirtformat, null, null);
            s_logger.debug(volDef.toString());
            vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        }
        KVMPhysicalDisk disk = new KVMPhysicalDisk(vol.getPath(), volumeUuid, pool);
        disk.setFormat(PhysicalDiskFormat.QCOW2);
        disk.setSize(vol.getInfo().allocation);
        disk.setVirtualSize(vol.getInfo().capacity);
        return disk;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStorageVolumeDef(com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) StoragePoolInfo(org.libvirt.StoragePoolInfo)

Aggregations

StoragePool (org.libvirt.StoragePool)51 LibvirtException (org.libvirt.LibvirtException)42 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)20 Connect (org.libvirt.Connect)16 LibvirtStoragePoolDef (com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)14 StoragePoolInfo (org.libvirt.StoragePoolInfo)8 LibvirtStoragePoolDef (com.cloud.agent.resource.computing.LibvirtStoragePoolDef)7 StorageVol (org.libvirt.StorageVol)7 Secret (org.libvirt.Secret)6 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)5 LibvirtStorageVolumeDef (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef)4 ArrayList (java.util.ArrayList)4 LibvirtSecretDef (com.cloud.hypervisor.kvm.resource.LibvirtSecretDef)2 VolumeFormat (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef.VolumeFormat)2 Domain (org.libvirt.Domain)2 MigrateWithStorageAcrossClustersAnswer (com.cloud.agent.api.MigrateWithStorageAcrossClustersAnswer)1 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)1 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 VolumeTO (com.cloud.agent.api.to.VolumeTO)1