Search in sources :

Example 1 with Secret

use of org.libvirt.Secret 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;
        } else if (spd.getPoolType() == LibvirtStoragePoolDef.PoolType.POWERFLEX) {
            type = StoragePoolType.PowerFlex;
        }
        LibvirtStoragePool pool = new LibvirtStoragePool(uuid, storage.getName(), type, this, storage);
        if (pool.getType() != StoragePoolType.RBD && pool.getType() != StoragePoolType.PowerFlex)
            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: " + toHumanReadableSize(storage.getInfo().capacity) + " Used: " + toHumanReadableSize(storage.getInfo().allocation) + " Available: " + toHumanReadableSize(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 2 with Secret

use of org.libvirt.Secret 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 3 with Secret

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

the class LibvirtStorageAdaptor method createRbdStoragePool.

private StoragePool createRbdStoragePool(final Connect conn, final String uuid, final String host, final int port, final String userInfo, final String path) {
    final LibvirtStoragePoolDef storagePoolDefinition;
    StoragePool storagePool = null;
    Secret secretString = null;
    final String[] userInfoTemp = userInfo.split(":");
    if (userInfoTemp.length == 2) {
        final LibvirtSecretDef sd = new LibvirtSecretDef(Usage.CEPH, uuid);
        sd.setCephName(userInfoTemp[0] + "@" + host + ":" + port + "/" + path);
        try {
            logger.debug(sd.toString());
            secretString = conn.secretDefineXML(sd.toString());
            secretString.setValue(Base64.decodeBase64(userInfoTemp[1]));
        } catch (final LibvirtException e) {
            logger.error("Failed to define the libvirt secret: " + e.toString());
            if (secretString != null) {
                try {
                    secretString.undefine();
                    secretString.free();
                } catch (final LibvirtException l) {
                    logger.error("Failed to undefine the libvirt secret: " + l.toString());
                }
            }
            return null;
        }
        storagePoolDefinition = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, userInfoTemp[0], AuthenticationType.CEPH, uuid);
    } else {
        storagePoolDefinition = new LibvirtStoragePoolDef(PoolType.RBD, uuid, uuid, host, port, path, "");
    }
    try {
        logger.debug(storagePoolDefinition.toString());
        storagePool = conn.storagePoolCreateXML(storagePoolDefinition.toString(), 0);
        return storagePool;
    } catch (final LibvirtException e) {
        logger.error("Failed to create RBD storage pool: " + e.toString());
        if (storagePool != null) {
            try {
                if (storagePool.isPersistent() == 1) {
                    storagePool.destroy();
                    storagePool.undefine();
                } else {
                    storagePool.destroy();
                }
                storagePool.free();
            } catch (final LibvirtException l) {
                logger.error("Failed to undefine RBD storage pool: " + l.toString());
            }
        }
        if (secretString != null) {
            try {
                logger.error("Failed to create the RBD storage pool, cleaning up the libvirt secret");
                secretString.undefine();
                secretString.free();
            } catch (final LibvirtException se) {
                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 4 with Secret

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

the class LibvirtStorageAdaptor method getStoragePool.

@Override
public KvmStoragePool getStoragePool(final String uuid, final boolean refreshInfo) {
    logger.info("Trying to fetch storage pool " + uuid + " from libvirt");
    StoragePool storage = null;
    try {
        final Connect conn = LibvirtConnection.getConnection();
        storage = conn.storagePoolLookupByUUIDString(uuid);
        if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
            logger.warn("Storage pool " + uuid + " is not in running state. Attempting to start it.");
            storage.create(0);
        }
        final 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;
        }
        final 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());
            final String authUsername = spd.getAuthUserName();
            if (authUsername != null) {
                final Secret secret = conn.secretLookupByUUIDString(spd.getSecretUuid());
                final String secretValue = new String(Base64.encodeBase64(secret.getByteValue()), Charset.defaultCharset());
                pool.setAuthUsername(authUsername);
                pool.setAuthSecret(secretValue);
            }
        }
        if (refreshInfo) {
            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);
        logger.debug("Succesfully refreshed pool " + uuid + " Capacity: " + storage.getInfo().capacity + " Used: " + storage.getInfo().allocation + " Available: " + storage.getInfo().available);
        return pool;
    } catch (final LibvirtException e) {
        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 5 with Secret

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

the class LibvirtStorageAdaptor method deleteStoragePool.

@Override
public boolean deleteStoragePool(final String uuid) {
    logger.info("Attempting to remove storage pool " + uuid + " from libvirt");
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    StoragePool storagePool = null;
    Secret secretString = null;
    try {
        storagePool = conn.storagePoolLookupByUUIDString(uuid);
    } catch (final LibvirtException e) {
        logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed");
        return true;
    }
    /*
     * Some storage pools, like RBD also have 'secret' information stored in libvirt Destroy them if they exist
     */
    try {
        secretString = conn.secretLookupByUUIDString(uuid);
    } catch (final LibvirtException e) {
        logger.info("Storage pool " + uuid + " has no corresponding secret. Not removing any secret.");
    }
    try {
        if (storagePool.isPersistent() == 1) {
            storagePool.destroy();
            storagePool.undefine();
        } else {
            storagePool.destroy();
        }
        storagePool.free();
        if (secretString != null) {
            secretString.undefine();
            secretString.free();
        }
        logger.info("Storage pool " + uuid + " was succesfully removed from libvirt.");
        return true;
    } catch (final LibvirtException e) {
        // handle ebusy error when pool is quickly destroyed
        if (e.toString().contains("exit status 16")) {
            final String targetPath = mountPoint + File.separator + uuid;
            logger.error("deleteStoragePool removed pool from libvirt, but libvirt had trouble unmounting the pool. " + "Trying umount location " + targetPath + " again in a few seconds");
            final String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath);
            if (result == null) {
                logger.error("Succeeded in unmounting " + targetPath);
                return true;
            }
            logger.error("Failed to unmount " + targetPath);
        }
        throw new CloudRuntimeException(e.toString(), e);
    }
}
Also used : Secret(org.libvirt.Secret) LibvirtException(org.libvirt.LibvirtException) StoragePool(org.libvirt.StoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect)

Aggregations

LibvirtException (org.libvirt.LibvirtException)6 Secret (org.libvirt.Secret)6 StoragePool (org.libvirt.StoragePool)6 LibvirtStoragePoolDef (com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 Connect (org.libvirt.Connect)4 LibvirtSecretDef (com.cloud.hypervisor.kvm.resource.LibvirtSecretDef)2 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)2