Search in sources :

Example 46 with Connect

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

the class KVMStorageProcessor method attachVolume.

@Override
public Answer attachVolume(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
    final String vmName = cmd.getVmName();
    final String serial = resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
        final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), serial, vol.getBytesReadRate(), vol.getBytesWriteRate(), vol.getIopsReadRate(), vol.getIopsWriteRate());
        return new AttachAnswer(disk);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new AttachAnswer(e.toString());
    } catch (final InternalErrorException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) Connect(org.libvirt.Connect) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) InternalErrorException(com.cloud.exception.InternalErrorException) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 47 with Connect

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

the class KVMStorageProcessor method attachIso.

@Override
public Answer attachIso(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final TemplateObjectTO isoTO = (TemplateObjectTO) disk.getData();
    final DataStoreTO store = isoTO.getDataStore();
    if (!(store instanceof NfsTO)) {
        return new AttachAnswer("unsupported protocol");
    }
    final NfsTO nfsStore = (NfsTO) store;
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
        attachOrDetachISO(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTO.getPath(), true);
    } catch (final LibvirtException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final URISyntaxException e) {
        return new Answer(cmd, false, e.toString());
    } catch (final InternalErrorException e) {
        return new Answer(cmd, false, e.toString());
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 48 with Connect

use of org.libvirt.Connect 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 49 with Connect

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

the class LibvirtRestoreVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
        }
        String xmlDesc = dm.getXMLDesc(0);
        List<VMSnapshotTO> snapshots = cmd.getSnapshots();
        Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
        for (VMSnapshotTO snapshot : snapshots) {
            VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
            String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
            s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
            try {
                // VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
                int flags = 1;
                if (snapshot.getCurrent()) {
                    // VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
                    flags += 2;
                }
                dm.snapshotCreateXML(vmSnapshotXML, flags);
            } catch (LibvirtException e) {
                s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
                return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
            }
        }
        return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (LibvirtException e) {
        String msg = " Restore snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RestoreVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 50 with Connect

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

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    VirtualMachine.PowerState vmState = null;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        if (snapshot == null)
            return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
        dm.revertToSnapshot(snapshot);
        snapshot.free();
        if (!snapshotMemory) {
            dm.destroy();
            if (dm.isPersistent() == 1)
                dm.undefine();
            vmState = VirtualMachine.PowerState.PowerOff;
        } else {
            vmState = VirtualMachine.PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (LibvirtException e) {
        String msg = " Revert to VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11