Search in sources :

Example 6 with StoragePool

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

the class KVMHABase method getMountPoint.

protected String getMountPoint(NfsStoragePool storagePool) {
    StoragePool pool = null;
    String poolName = null;
    try {
        pool = LibvirtConnection.getConnection().storagePoolLookupByUUIDString(storagePool._poolUUID);
        if (pool != null) {
            StoragePoolInfo spi = pool.getInfo();
            if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
                pool.create(0);
            } else {
            /*
                     * Sometimes, the mount point is lost, even libvirt thinks
                     * the storage pool still running
                     */
            }
            poolName = pool.getName();
        }
    } catch (LibvirtException e) {
        s_logger.debug("Ignoring libvirt error.", e);
    } finally {
        try {
            if (pool != null) {
                pool.free();
            }
        } catch (LibvirtException e) {
            s_logger.debug("Ignoring libvirt error.", e);
        }
    }
    return checkingMountPoint(storagePool, poolName);
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) StoragePoolInfo(org.libvirt.StoragePoolInfo)

Example 7 with StoragePool

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

the class LibvirtStorageAdaptor method getStoragePool.

@Override
public KVMStoragePool getStoragePool(String uuid, boolean refreshInfo) {
    s_logger.info("Trying to fetch storage pool " + uuid + " from libvirt");
    StoragePool storage = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        storage = conn.storagePoolLookupByUUIDString(uuid);
        if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            s_logger.warn("Storage pool " + uuid + " is not in running state. Attempting to start it.");
            storage.create(0);
        }
        LibvirtStoragePoolDef spd = getStoragePoolDef(conn, storage);
        if (spd == null) {
            throw new CloudRuntimeException("Unable to parse the storage pool definition for storage pool " + uuid);
        }
        StoragePoolType type = null;
        if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.NETFS) {
            type = StoragePoolType.NetworkFilesystem;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.DIR) {
            type = StoragePoolType.Filesystem;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.RBD) {
            type = StoragePoolType.RBD;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.LOGICAL) {
            type = StoragePoolType.CLVM;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.GLUSTERFS) {
            type = StoragePoolType.Gluster;
        }
        LibvirtStoragePool pool = new LibvirtStoragePool(uuid, storage.getName(), type, this, storage);
        if (pool.getType() != StoragePoolType.RBD)
            pool.setLocalPath(spd.getTargetPath());
        else
            pool.setLocalPath("");
        if (pool.getType() == StoragePoolType.RBD || pool.getType() == StoragePoolType.Gluster) {
            pool.setSourceHost(spd.getSourceHost());
            pool.setSourcePort(spd.getSourcePort());
            pool.setSourceDir(spd.getSourceDir());
            String authUsername = spd.getAuthUserName();
            if (authUsername != null) {
                Secret secret = conn.secretLookupByUUIDString(spd.getSecretUUID());
                String secretValue = new String(Base64.encodeBase64(secret.getByteValue()), Charset.defaultCharset());
                pool.setAuthUsername(authUsername);
                pool.setAuthSecret(secretValue);
            }
        }
        /**
             * On large (RBD) storage pools it can take up to a couple of minutes
             * for libvirt to refresh the pool.
             *
             * Refreshing a storage pool means that libvirt will have to iterate the whole pool
             * and fetch information of each volume in there
             *
             * It is not always required to refresh a pool. So we can control if we want to or not
             *
             * By default only the getStorageStats call in the LibvirtComputingResource will ask to
             * refresh the pool
             */
        if (refreshInfo) {
            s_logger.info("Asking libvirt to refresh storage pool " + uuid);
            pool.refresh();
        }
        pool.setCapacity(storage.getInfo().capacity);
        pool.setUsed(storage.getInfo().allocation);
        pool.setAvailable(storage.getInfo().available);
        s_logger.debug("Succesfully refreshed pool " + uuid + " Capacity: " + storage.getInfo().capacity + " Used: " + storage.getInfo().allocation + " Available: " + storage.getInfo().available);
        return pool;
    } catch (LibvirtException e) {
        s_logger.debug("Could not find storage pool " + uuid + " in libvirt");
        throw new CloudRuntimeException(e.toString(), e);
    }
}
Also used : Secret(org.libvirt.Secret) StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)

Example 8 with StoragePool

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

the class LibvirtStorageAdaptor method createRBDStoragePool.

private StoragePool createRBDStoragePool(Connect conn, String uuid, String host, int port, String userInfo, String path) {
    LibvirtStoragePoolDef spd;
    StoragePool sp = null;
    Secret s = null;
    String[] userInfoTemp = userInfo.split(":");
    if (userInfoTemp.length == 2) {
        LibvirtSecretDef sd = new LibvirtSecretDef(Usage.CEPH, uuid);
        sd.setCephName(userInfoTemp[0] + "@" + host + ":" + port + "/" + path);
        try {
            s_logger.debug(sd.toString());
            s = conn.secretDefineXML(sd.toString());
            s.setValue(Base64.decodeBase64(userInfoTemp[1]));
        } catch (LibvirtException e) {
            s_logger.error("Failed to define the libvirt secret: " + e.toString());
            if (s != null) {
                try {
                    s.undefine();
                    s.free();
                } catch (LibvirtException l) {
                    s_logger.error("Failed to undefine the libvirt secret: " + l.toString());
                }
            }
            return null;
        }
        spd = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, userInfoTemp[0], AuthenticationType.CEPH, uuid);
    } else {
        spd = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, "");
    }
    try {
        s_logger.debug(spd.toString());
        sp = conn.storagePoolCreateXML(spd.toString(), 0);
        return sp;
    } catch (LibvirtException e) {
        s_logger.error("Failed to create RBD storage pool: " + e.toString());
        if (sp != null) {
            try {
                if (sp.isPersistent() == 1) {
                    sp.destroy();
                    sp.undefine();
                } else {
                    sp.destroy();
                }
                sp.free();
            } catch (LibvirtException l) {
                s_logger.error("Failed to undefine RBD storage pool: " + l.toString());
            }
        }
        if (s != null) {
            try {
                s_logger.error("Failed to create the RBD storage pool, cleaning up the libvirt secret");
                s.undefine();
                s.free();
            } catch (LibvirtException se) {
                s_logger.error("Failed to remove the libvirt secret: " + se.toString());
            }
        }
        return null;
    }
}
Also used : Secret(org.libvirt.Secret) StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStoragePoolDef(com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef) LibvirtSecretDef(com.cloud.hypervisor.kvm.resource.LibvirtSecretDef)

Example 9 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)

Example 10 with StoragePool

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

the class LibvirtStorageAdaptor method refresh.

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

Aggregations

StoragePool (org.libvirt.StoragePool)31 LibvirtException (org.libvirt.LibvirtException)27 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 Connect (org.libvirt.Connect)8 LibvirtStoragePoolDef (com.cloud.agent.resource.computing.LibvirtStoragePoolDef)7 LibvirtStoragePoolDef (com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)7 StoragePoolInfo (org.libvirt.StoragePoolInfo)6 StorageVol (org.libvirt.StorageVol)4 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)3 Secret (org.libvirt.Secret)3 LibvirtStorageVolumeDef (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef)2 ArrayList (java.util.ArrayList)2 LibvirtStorageVolumeDef (com.cloud.agent.resource.computing.LibvirtStorageVolumeDef)1 LibvirtStorageVolumeDef.volFormat (com.cloud.agent.resource.computing.LibvirtStorageVolumeDef.volFormat)1 LibvirtSecretDef (com.cloud.hypervisor.kvm.resource.LibvirtSecretDef)1 VolumeFormat (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef.VolumeFormat)1 File (java.io.File)1 URI (java.net.URI)1