Search in sources :

Example 6 with StorageVol

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

the class LibvirtResizeVolumeCommandWrapper method execute.

@Override
public Answer execute(final ResizeVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String volid = command.getPath();
    final long newSize = command.getNewSize();
    final long currentSize = command.getCurrentSize();
    final String vmInstanceName = command.getInstanceName();
    final boolean shrinkOk = command.getShrinkOk();
    final StorageFilerTO spool = command.getPool();
    final String notifyOnlyType = "NOTIFYONLY";
    if (currentSize == newSize) {
        // nothing to do
        s_logger.info("No need to resize volume: current size " + currentSize + " is same as new size " + newSize);
        return new ResizeVolumeAnswer(command, true, "success", currentSize);
    }
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        KVMStoragePool pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
        final KVMPhysicalDisk vol = pool.getPhysicalDisk(volid);
        final String path = vol.getPath();
        String type = notifyOnlyType;
        if (pool.getType() != StoragePoolType.RBD) {
            type = libvirtComputingResource.getResizeScriptType(pool, vol);
            if (type.equals("QCOW2") && shrinkOk) {
                return new ResizeVolumeAnswer(command, false, "Unable to shrink volumes of type " + type);
            }
        } else {
            s_logger.debug("Volume " + path + " is on a RBD storage pool. No need to query for additional information.");
        }
        s_logger.debug("Resizing volume: " + path + "," + currentSize + "," + newSize + "," + type + "," + vmInstanceName + "," + shrinkOk);
        /* libvirt doesn't support resizing (C)LVM devices, and corrupts QCOW2 in some scenarios, so we have to do these via Bash script */
        if (pool.getType() != StoragePoolType.CLVM && vol.getFormat() != PhysicalDiskFormat.QCOW2) {
            s_logger.debug("Volume " + path + " can be resized by libvirt. Asking libvirt to resize the volume.");
            try {
                final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
                final Connect conn = libvirtUtilitiesHelper.getConnection();
                final StorageVol v = conn.storageVolLookupByPath(path);
                int flags = 0;
                if (conn.getLibVirVersion() > 1001000 && vol.getFormat() == PhysicalDiskFormat.RAW && pool.getType() != StoragePoolType.RBD) {
                    flags = 1;
                }
                if (shrinkOk) {
                    flags = 4;
                }
                v.resize(newSize, flags);
            } catch (final LibvirtException e) {
                return new ResizeVolumeAnswer(command, false, e.toString());
            }
        }
        s_logger.debug("Invoking resize script to handle type " + type);
        final Script resizecmd = new Script(libvirtComputingResource.getResizeVolumePath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
        resizecmd.add("-s", String.valueOf(newSize));
        resizecmd.add("-c", String.valueOf(currentSize));
        resizecmd.add("-p", path);
        resizecmd.add("-t", type);
        resizecmd.add("-r", String.valueOf(shrinkOk));
        resizecmd.add("-v", vmInstanceName);
        final String result = resizecmd.execute();
        if (result != null) {
            if (type.equals(notifyOnlyType)) {
                return new ResizeVolumeAnswer(command, true, "Resize succeeded, but need reboot to notify guest");
            } else {
                return new ResizeVolumeAnswer(command, false, result);
            }
        }
        /* fetch new size as seen from libvirt, don't want to assume anything */
        pool = storagePoolMgr.getStoragePool(spool.getType(), spool.getUuid());
        pool.refresh();
        final long finalSize = pool.getPhysicalDisk(volid).getVirtualSize();
        s_logger.debug("after resize, size reports as " + finalSize + ", requested " + newSize);
        return new ResizeVolumeAnswer(command, true, "success", finalSize);
    } catch (final CloudRuntimeException e) {
        final String error = "Failed to resize volume: " + e.getMessage();
        s_logger.debug(error);
        return new ResizeVolumeAnswer(command, false, error);
    }
}
Also used : Script(com.cloud.utils.script.Script) 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) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 7 with StorageVol

use of org.libvirt.StorageVol in project CloudStack-archive by CloudStack-extras.

the class LibvirtStorageAdaptor method createPhysicalDisk.

@Override
public KVMPhysicalDisk createPhysicalDisk(String name, KVMStoragePool pool, PhysicalDiskFormat format, long size) {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    StoragePool virtPool = libvirtPool.getPool();
    LibvirtStorageVolumeDef.volFormat libvirtformat = null;
    if (format == PhysicalDiskFormat.QCOW2) {
        libvirtformat = LibvirtStorageVolumeDef.volFormat.QCOW2;
    } else if (format == PhysicalDiskFormat.RAW) {
        libvirtformat = LibvirtStorageVolumeDef.volFormat.RAW;
    }
    LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null);
    s_logger.debug(volDef.toString());
    try {
        StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        KVMPhysicalDisk disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
        disk.setFormat(format);
        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) LibvirtStorageVolumeDef(com.cloud.agent.resource.computing.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) LibvirtStorageVolumeDef.volFormat(com.cloud.agent.resource.computing.LibvirtStorageVolumeDef.volFormat) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 8 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 9 with StorageVol

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

the class LibvirtStorageAdaptor method createPhysicalDiskByLibVirt.

private KVMPhysicalDisk createPhysicalDiskByLibVirt(String name, KVMStoragePool pool, PhysicalDiskFormat format, Storage.ProvisioningType provisioningType, long size) {
    LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    StoragePool virtPool = libvirtPool.getPool();
    LibvirtStorageVolumeDef.VolumeFormat libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.getFormat(format);
    String volPath = null;
    String volName = null;
    long volAllocation = 0;
    long volCapacity = 0;
    LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null);
    s_logger.debug(volDef.toString());
    try {
        StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        volPath = vol.getPath();
        volName = vol.getName();
        volAllocation = vol.getInfo().allocation;
        volCapacity = vol.getInfo().capacity;
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    KVMPhysicalDisk disk = new KVMPhysicalDisk(volPath, volName, pool);
    disk.setFormat(format);
    disk.setSize(volAllocation);
    disk.setVirtualSize(volCapacity);
    return disk;
}
Also used : VolumeFormat(com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef.VolumeFormat) StoragePool(org.libvirt.StoragePool) LibvirtStorageVolumeDef(com.cloud.hypervisor.kvm.resource.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 10 with StorageVol

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

the class LibvirtStorageAdaptor method getVolume.

public StorageVol getVolume(StoragePool pool, String volName) {
    StorageVol vol = null;
    try {
        vol = pool.storageVolLookupByName(volName);
    } catch (LibvirtException e) {
        s_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 {
            s_logger.debug("Refreshing storage pool " + pool.getName());
            refreshPool(pool);
        } catch (LibvirtException e) {
            s_logger.debug("Failed to refresh storage pool: " + e.getMessage());
        }
        try {
            vol = pool.storageVolLookupByName(volName);
            s_logger.debug("Found volume " + volName + " in storage pool " + pool.getName() + " after refreshing the pool");
        } catch (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)

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