Search in sources :

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

Example 12 with StorageVol

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

the class LibvirtStorageAdaptor method copyVolume.

public StorageVol copyVolume(StoragePool destPool, LibvirtStorageVolumeDef destVol, StorageVol srcVol, int timeout) throws LibvirtException {
    StorageVol vol = destPool.storageVolCreateXML(destVol.toString(), 0);
    String srcPath = srcVol.getKey();
    String destPath = vol.getKey();
    Script.runSimpleBashScript("cp " + srcPath + " " + destPath, timeout);
    return vol;
}
Also used : StorageVol(org.libvirt.StorageVol)

Example 13 with StorageVol

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

the class LibvirtComputingResourceTest method testResizeVolumeCommand.

@Test
public void testResizeVolumeCommand() {
    final String path = "nfs:/127.0.0.1/storage/secondary";
    final StorageFilerTO pool = Mockito.mock(StorageFilerTO.class);
    final Long currentSize = 100l;
    final Long newSize = 200l;
    final boolean shrinkOk = true;
    final String vmInstance = "Test";
    final ResizeVolumeCommand command = new ResizeVolumeCommand(path, pool, currentSize, newSize, shrinkOk, vmInstance);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final KVMStoragePool storagePool = Mockito.mock(KVMStoragePool.class);
    final KVMPhysicalDisk vol = Mockito.mock(KVMPhysicalDisk.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final StorageVol v = Mockito.mock(StorageVol.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(storagePool);
    when(storagePool.getPhysicalDisk(path)).thenReturn(vol);
    when(vol.getPath()).thenReturn(path);
    when(libvirtComputingResource.getResizeScriptType(storagePool, vol)).thenReturn("FILE");
    when(storagePool.getType()).thenReturn(StoragePoolType.RBD);
    when(vol.getFormat()).thenReturn(PhysicalDiskFormat.FILE);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnection()).thenReturn(conn);
        when(conn.storageVolLookupByPath(path)).thenReturn(v);
        when(conn.getLibVirVersion()).thenReturn(10010l);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnection();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) Connect(org.libvirt.Connect) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) ResizeVolumeCommand(com.cloud.agent.api.storage.ResizeVolumeCommand) Test(org.junit.Test)

Aggregations

StorageVol (org.libvirt.StorageVol)13 LibvirtException (org.libvirt.LibvirtException)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 StoragePool (org.libvirt.StoragePool)4 LibvirtStorageVolumeDef (com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef)3 Connect (org.libvirt.Connect)3 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)2 LibvirtStorageVolumeDef (com.cloud.agent.resource.computing.LibvirtStorageVolumeDef)2 KVMPhysicalDisk (com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk)2 KVMStoragePool (com.cloud.hypervisor.kvm.storage.KVMStoragePool)2 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)2 IoCTX (com.ceph.rados.IoCTX)1 Rados (com.ceph.rados.Rados)1 RadosException (com.ceph.rados.exceptions.RadosException)1 Rbd (com.ceph.rbd.Rbd)1 RbdException (com.ceph.rbd.RbdException)1 RbdImage (com.ceph.rbd.RbdImage)1 RbdSnapInfo (com.ceph.rbd.jna.RbdSnapInfo)1 Answer (com.cloud.agent.api.Answer)1 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)1