Search in sources :

Example 6 with StoragePoolInfo

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

the class LibvirtStorageAdaptor method createStoragePool.

@Override
public KVMStoragePool createStoragePool(String name, String host, String path, StoragePoolType type) {
    StoragePool sp = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    try {
        sp = conn.storagePoolLookupByUUIDString(name);
        if (sp.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            sp.undefine();
            sp = null;
        }
    } catch (LibvirtException e) {
    }
    if (sp == null) {
        if (type == StoragePoolType.NetworkFilesystem) {
            sp = createNfsStoragePool(conn, name, host, path);
        } else if (type == StoragePoolType.SharedMountPoint || type == StoragePoolType.Filesystem) {
            sp = CreateSharedStoragePool(conn, name, host, path);
        }
    }
    try {
        StoragePoolInfo spi = sp.getInfo();
        if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            sp.create(0);
        }
        LibvirtStoragePoolDef spd = getStoragePoolDef(conn, sp);
        LibvirtStoragePool pool = new LibvirtStoragePool(name, sp.getName(), type, this, sp);
        pool.setLocalPath(spd.getTargetPath());
        getStats(pool);
        return pool;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) StoragePoolInfo(org.libvirt.StoragePoolInfo) LibvirtStoragePoolDef(com.cloud.agent.resource.computing.LibvirtStoragePoolDef)

Example 7 with StoragePoolInfo

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

the class LibvirtStorageAdaptor method getStoragePoolbyURI.

public StoragePool getStoragePoolbyURI(Connect conn, URI uri) throws LibvirtException {
    String sourcePath;
    String uuid;
    String sourceHost = "";
    String protocal;
    if (uri.getScheme().equalsIgnoreCase("local")) {
        sourcePath = _mountPoint + File.separator + uri.toString().replace("local:///", "");
        sourcePath = sourcePath.replace("//", "/");
        uuid = UUID.nameUUIDFromBytes(new String(sourcePath).getBytes()).toString();
        protocal = "DIR";
    } else {
        sourcePath = uri.getPath();
        sourcePath = sourcePath.replace("//", "/");
        sourceHost = uri.getHost();
        uuid = UUID.nameUUIDFromBytes(new String(sourceHost + sourcePath).getBytes()).toString();
        protocal = "NFS";
    }
    String targetPath = _mountPoint + File.separator + uuid;
    StoragePool sp = null;
    try {
        sp = conn.storagePoolLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
    }
    if (sp == null) {
        try {
            LibvirtStoragePoolDef spd = null;
            if (protocal.equalsIgnoreCase("NFS")) {
                _storageLayer.mkdir(targetPath);
                spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid, sourceHost, sourcePath, targetPath);
                s_logger.debug(spd.toString());
            // addStoragePool(uuid);
            } else if (protocal.equalsIgnoreCase("DIR")) {
                _storageLayer.mkdir(targetPath);
                spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid, null, null, sourcePath);
            }
            synchronized (getStoragePool(uuid)) {
                sp = conn.storagePoolDefineXML(spd.toString(), 0);
                if (sp == null) {
                    s_logger.debug("Failed to define storage pool");
                    return null;
                }
                sp.create(0);
            }
            return sp;
        } catch (LibvirtException e) {
            try {
                if (sp != null) {
                    sp.undefine();
                    sp.free();
                }
            } catch (LibvirtException l) {
            }
            throw e;
        }
    } else {
        StoragePoolInfo spi = sp.getInfo();
        if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            sp.create(0);
        }
        return sp;
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) StoragePoolInfo(org.libvirt.StoragePoolInfo) LibvirtStoragePoolDef(com.cloud.agent.resource.computing.LibvirtStoragePoolDef)

Example 8 with StoragePoolInfo

use of org.libvirt.StoragePoolInfo in project cosmic by MissionCriticalCloud.

the class ManagedNfsStorageAdaptor method getPhysicalDisk.

/*
     * creates a disk based on the created nfs storage pool using libvirt
     */
@Override
public KvmPhysicalDisk getPhysicalDisk(final String volumeUuid, final KvmStoragePool pool) {
    // now create the volume upon the given storage pool in kvm
    final Connect conn;
    StoragePool virtPool = null;
    try {
        conn = LibvirtConnection.getConnection();
        virtPool = conn.storagePoolLookupByName("/" + volumeUuid);
    } catch (final 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;
            final StoragePoolInfo poolinfo = virtPool.getInfo();
            volCapacity = poolinfo.available;
            final LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(volumeUuid, volCapacity, libvirtformat, null, null);
            logger.debug(volDef.toString());
            vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        }
        final 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 (final 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

LibvirtException (org.libvirt.LibvirtException)8 StoragePool (org.libvirt.StoragePool)8 StoragePoolInfo (org.libvirt.StoragePoolInfo)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 LibvirtStoragePoolDef (com.cloud.agent.resource.computing.LibvirtStoragePoolDef)3 Connect (org.libvirt.Connect)3 LibvirtStorageVolumeDef (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef)2 StorageVol (org.libvirt.StorageVol)2 File (java.io.File)1