Search in sources :

Example 26 with StoragePool

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

the class ManagedNfsStorageAdaptor method disconnectPhysicalDisk.

/*
     * disconnect the disk by destroying the sp pointer
     */
public boolean disconnectPhysicalDisk(KVMStoragePool pool, String mountpoint) throws LibvirtException {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    StoragePool sp = libvirtPool.getPool();
    // destroy the pool
    sp.destroy();
    return true;
}
Also used : StoragePool(org.libvirt.StoragePool)

Example 27 with StoragePool

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

the class ManagedNfsStorageAdaptor method connectPhysicalDisk.

/*
     * creates a nfs storage pool using libvirt
     */
@Override
public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<String, String> details) {
    StoragePool sp = null;
    Connect conn = null;
    String targetPath = null;
    LibvirtStoragePoolDef spd = null;
    try {
        conn = LibvirtConnection.getConnection();
        if (conn == null) {
            throw new CloudRuntimeException("Failed to create Libvrt Connection");
        }
        targetPath = "/mnt" + volumeUuid;
        spd = new LibvirtStoragePoolDef(PoolType.NETFS, volumeUuid, details.get(DiskTO.UUID), pool.getSourceHost(), details.get(DiskTO.MOUNT_POINT), targetPath);
        _storageLayer.mkdir(targetPath);
        s_logger.debug(spd.toString());
        sp = conn.storagePoolCreateXML(spd.toString(), 0);
        if (sp == null) {
            throw new CloudRuntimeException("Failed to create storage pool:" + volumeUuid);
        }
        try {
            if (sp.isActive() == 0) {
                // s_logger.debug("attempting to activate pool " + name);
                sp.create(0);
            }
            // now add the storage pool
            LibvirtStoragePool storagePool = (LibvirtStoragePool) getStoragePool(pool.getUuid());
            storagePool.setPool(sp);
            return true;
        } catch (LibvirtException e) {
            String error = e.toString();
            if (error.contains("Storage source conflict")) {
                throw new CloudRuntimeException("A pool matching this location already exists in libvirt, " + " but has a different UUID/Name. Cannot create new pool without first " + " removing it. Check for inactive pools via 'virsh pool-list --all'. " + error);
            } else {
                throw new CloudRuntimeException(error);
            }
        }
    } catch (LibvirtException e) {
        s_logger.error(e.toString());
        // if error is that pool is mounted, try to handle it
        if (e.toString().contains("already mounted")) {
            s_logger.error("Attempting to unmount old mount libvirt is unaware of at " + targetPath);
            String result = Script.runSimpleBashScript("umount -l " + targetPath);
            if (result == null) {
                s_logger.error("Succeeded in unmounting " + targetPath);
                try {
                    conn.storagePoolCreateXML(spd.toString(), 0);
                    s_logger.error("Succeeded in redefining storage");
                    return true;
                } catch (LibvirtException l) {
                    s_logger.error("Target was already mounted, unmounted it but failed to redefine storage:" + l);
                }
            } else {
                s_logger.error("Failed in unmounting and redefining storage");
            }
        } else {
            s_logger.error("Internal error occurred when attempting to mount:" + e.getMessage());
            // stacktrace for agent.log
            e.printStackTrace();
            throw new CloudRuntimeException(e.toString());
        }
        return false;
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) LibvirtStoragePoolDef(com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef)

Example 28 with StoragePool

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

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 = 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 29 with StoragePool

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

the class LibvirtStorageAdaptor method deleteStoragePool.

@Override
public boolean deleteStoragePool(String uuid) {
    s_logger.info("Attempting to remove storage pool " + uuid + " from libvirt");
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    StoragePool sp = null;
    Secret s = null;
    try {
        sp = conn.storagePoolLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
        s_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 {
        s = conn.secretLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
        s_logger.info("Storage pool " + uuid + " has no corresponding secret. Not removing any secret.");
    }
    try {
        if (sp.isPersistent() == 1) {
            sp.destroy();
            sp.undefine();
        } else {
            sp.destroy();
        }
        sp.free();
        if (s != null) {
            s.undefine();
            s.free();
        }
        s_logger.info("Storage pool " + uuid + " was succesfully removed from libvirt.");
        return true;
    } catch (LibvirtException e) {
        // handle ebusy error when pool is quickly destroyed
        if (e.toString().contains("exit status 16")) {
            String targetPath = _mountPoint + File.separator + uuid;
            s_logger.error("deleteStoragePool removed pool from libvirt, but libvirt had trouble unmounting the pool. Trying umount location " + targetPath + "again in a few seconds");
            String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath);
            if (result == null) {
                s_logger.error("Succeeded in unmounting " + targetPath);
                return true;
            }
            s_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)

Example 30 with StoragePool

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

the class LibvirtStoragePoolTest method testAttributes.

public void testAttributes() {
    String uuid = "4c4fb08b-373e-4f30-a120-3aa3a43f31da";
    String name = "myfirstpool";
    StoragePoolType type = StoragePoolType.NetworkFilesystem;
    StorageAdaptor adapter = Mockito.mock(LibvirtStorageAdaptor.class);
    StoragePool storage = Mockito.mock(StoragePool.class);
    LibvirtStoragePool pool = new LibvirtStoragePool(uuid, name, type, adapter, storage);
    assertEquals(pool.getCapacity(), 0);
    assertEquals(pool.getUsed(), 0);
    assertEquals(pool.getName(), name);
    assertEquals(pool.getUuid(), uuid);
    assertEquals(pool.getAvailable(), 0);
    assertEquals(pool.getStoragePoolType(), type);
    pool.setCapacity(2048);
    pool.setUsed(1024);
    pool.setAvailable(1023);
    assertEquals(pool.getCapacity(), 2048);
    assertEquals(pool.getUsed(), 1024);
    assertEquals(pool.getAvailable(), 1023);
}
Also used : StoragePool(org.libvirt.StoragePool) StoragePoolType(com.cloud.storage.Storage.StoragePoolType)

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