Search in sources :

Example 16 with StorageVol

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

the class LibvirtStorageAdaptor method getVolume.

public StorageVol getVolume(final StoragePool pool, final String volName) {
    StorageVol vol = null;
    try {
        vol = pool.storageVolLookupByName(volName);
    } catch (final LibvirtException e) {
        logger.debug("Could not find volume " + volName + ": " + e.getMessage());
    }
    /**
     * The volume was not found in the storage pool This can happen when a volume has just been created on a different
     * host and since then the libvirt storage pool has not been refreshed.
     */
    if (vol == null) {
        try {
            logger.debug("Refreshing storage pool " + pool.getName());
            refreshPool(pool);
        } catch (final LibvirtException e) {
            logger.debug("Failed to refresh storage pool: " + e.getMessage());
        }
        try {
            vol = pool.storageVolLookupByName(volName);
            logger.debug("Found volume " + volName + " in storage pool " + pool.getName() + " after refreshing the pool");
        } catch (final LibvirtException e) {
            throw new CloudRuntimeException("Could not find volume " + volName + ": " + e.getMessage());
        }
    }
    return vol;
}
Also used : StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 17 with StorageVol

use of org.libvirt.StorageVol 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)

Example 18 with StorageVol

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

the class LibvirtMigrateCommandWrapper method deleteLocalVolume.

/**
 * Deletes the local volume from the storage pool.
 */
protected void deleteLocalVolume(String localPath) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        StorageVol storageVolLookupByPath = conn.storageVolLookupByPath(localPath);
        storageVolLookupByPath.delete(0);
    } catch (LibvirtException e) {
        s_logger.error(String.format("Cannot delete local volume [%s] due to: %s", localPath, e));
    }
}
Also used : StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 19 with StorageVol

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

the class LibvirtStorageAdaptor method getPhysicalDisk.

@Override
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    try {
        StorageVol vol = getVolume(libvirtPool.getPool(), volumeUuid);
        KVMPhysicalDisk disk;
        LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool.getPool().getConnect(), vol);
        disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
        disk.setSize(vol.getInfo().allocation);
        disk.setVirtualSize(vol.getInfo().capacity);
        /**
         * libvirt returns format = 'unknow', so we have to force
         * the format to RAW for RBD storage volumes
         */
        if (pool.getType() == StoragePoolType.RBD) {
            disk.setFormat(PhysicalDiskFormat.RAW);
        } else if (voldef.getFormat() == null) {
            File diskDir = new File(disk.getPath());
            if (diskDir.exists() && diskDir.isDirectory()) {
                disk.setFormat(PhysicalDiskFormat.DIR);
            } else if (volumeUuid.endsWith("tar") || volumeUuid.endsWith(("TAR"))) {
                disk.setFormat(PhysicalDiskFormat.TAR);
            } else if (volumeUuid.endsWith("raw") || volumeUuid.endsWith(("RAW"))) {
                disk.setFormat(PhysicalDiskFormat.RAW);
            } else {
                disk.setFormat(pool.getDefaultFormat());
            }
        } else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.QCOW2) {
            disk.setFormat(PhysicalDiskFormat.QCOW2);
        } else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.RAW) {
            disk.setFormat(PhysicalDiskFormat.RAW);
        }
        return disk;
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get physical disk:", e);
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StorageVol(org.libvirt.StorageVol) LibvirtStorageVolumeDef(com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) QemuImgFile(org.apache.cloudstack.utils.qemu.QemuImgFile) File(java.io.File)

Example 20 with StorageVol

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

the class LibvirtStorageAdaptor method deletePhysicalDisk.

@Override
public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool, Storage.ImageFormat format) {
    s_logger.info("Attempting to remove volume " + uuid + " from pool " + pool.getUuid());
    /**
     * RBD volume can have snapshots and while they exist libvirt
     * can't remove the RBD volume
     *
     * We have to remove those snapshots first
     */
    if (pool.getType() == StoragePoolType.RBD) {
        try {
            s_logger.info("Unprotecting and Removing RBD snapshots of image " + pool.getSourceDir() + "/" + uuid + " prior to removing the image");
            Rados r = new Rados(pool.getAuthUserName());
            r.confSet("mon_host", pool.getSourceHost() + ":" + pool.getSourcePort());
            r.confSet("key", pool.getAuthSecret());
            r.confSet("client_mount_timeout", "30");
            r.connect();
            s_logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
            IoCTX io = r.ioCtxCreate(pool.getSourceDir());
            Rbd rbd = new Rbd(io);
            RbdImage image = rbd.open(uuid);
            s_logger.debug("Fetching list of snapshots of RBD image " + pool.getSourceDir() + "/" + uuid);
            List<RbdSnapInfo> snaps = image.snapList();
            try {
                for (RbdSnapInfo snap : snaps) {
                    if (image.snapIsProtected(snap.name)) {
                        s_logger.debug("Unprotecting snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name);
                        image.snapUnprotect(snap.name);
                    } else {
                        s_logger.debug("Snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name + " is not protected.");
                    }
                    s_logger.debug("Removing snapshot " + pool.getSourceDir() + "/" + uuid + "@" + snap.name);
                    image.snapRemove(snap.name);
                }
                s_logger.info("Succesfully unprotected and removed any remaining snapshots (" + snaps.size() + ") of " + pool.getSourceDir() + "/" + uuid + " Continuing to remove the RBD image");
            } catch (RbdException e) {
                s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
                throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
            } finally {
                s_logger.debug("Closing image and destroying context");
                rbd.close(image);
                r.ioCtxDestroy(io);
            }
        } catch (RadosException e) {
            s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
            throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
        } catch (RbdException e) {
            s_logger.error("Failed to remove snapshot with exception: " + e.toString() + ", RBD error: " + ErrorCode.getErrorMessage(e.getReturnValue()));
            throw new CloudRuntimeException(e.toString() + " - " + ErrorCode.getErrorMessage(e.getReturnValue()));
        }
    }
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    try {
        StorageVol vol = getVolume(libvirtPool.getPool(), uuid);
        s_logger.debug("Instructing libvirt to remove volume " + uuid + " from pool " + pool.getUuid());
        if (Storage.ImageFormat.DIR.equals(format)) {
            deleteDirVol(libvirtPool, vol);
        } else {
            deleteVol(libvirtPool, vol);
        }
        vol.free();
        return true;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : RbdSnapInfo(com.ceph.rbd.jna.RbdSnapInfo) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) Rbd(com.ceph.rbd.Rbd) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Rados(com.ceph.rados.Rados) RbdImage(com.ceph.rbd.RbdImage) IoCTX(com.ceph.rados.IoCTX) RadosException(com.ceph.rados.exceptions.RadosException) RbdException(com.ceph.rbd.RbdException)

Aggregations

StorageVol (org.libvirt.StorageVol)24 LibvirtException (org.libvirt.LibvirtException)20 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 Connect (org.libvirt.Connect)9 StoragePool (org.libvirt.StoragePool)7 LibvirtStorageVolumeDef (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef)6 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)4 Test (org.junit.Test)3 IoCTX (com.ceph.rados.IoCTX)2 Rados (com.ceph.rados.Rados)2 RadosException (com.ceph.rados.exceptions.RadosException)2 Rbd (com.ceph.rbd.Rbd)2 RbdException (com.ceph.rbd.RbdException)2 RbdImage (com.ceph.rbd.RbdImage)2 RbdSnapInfo (com.ceph.rbd.jna.RbdSnapInfo)2 Answer (com.cloud.agent.api.Answer)2 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)2 ResizeVolumeAnswer (com.cloud.agent.api.storage.ResizeVolumeAnswer)2 ResizeVolumeCommand (com.cloud.agent.api.storage.ResizeVolumeCommand)2 LibvirtStorageVolumeDef (com.cloud.agent.resource.computing.LibvirtStorageVolumeDef)2