Search in sources :

Example 81 with Domain

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

the class LibvirtCreateVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final String vmSnapshotName = cmd.getTarget().getSnapshotName();
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to can not find vm: " + vmName);
        }
        final DomainState domainState = dm.getInfo().state;
        if (domainState != DomainState.VIR_DOMAIN_RUNNING) {
            return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to  vm is not running: " + vmName + " with domainState = " + domainState);
        }
        final String vmSnapshotXML = "<domainsnapshot><name>" + vmSnapshotName + "</name><memory snapshot='internal' /></domainsnapshot>";
        dm.snapshotCreateXML(vmSnapshotXML);
        return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
    } catch (final LibvirtException e) {
        final String msg = " Create VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new CreateVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) DomainState(org.libvirt.DomainInfo.DomainState) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain)

Example 82 with Domain

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

the class LibvirtRestoreVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final PowerState vmState = PowerState.PowerOn;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final 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);
        }
        final String xmlDesc = dm.getXMLDesc(0);
        final List<VMSnapshotTO> snapshots = cmd.getSnapshots();
        final Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
        for (final VMSnapshotTO snapshot : snapshots) {
            final VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
            final 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 (final 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 (final LibvirtException e) {
        final 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 (final 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(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 83 with Domain

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

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    PowerState vmState;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final 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);
        }
        final 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 = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (final LibvirtException e) {
        final 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(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 84 with Domain

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

the class LibvirtStartCommandWrapper method getFreeMemory.

private Long getFreeMemory(final Connect conn, final LibvirtComputingResource libvirtComputingResource) {
    try {
        long allocatedMem = 0;
        final int[] ids = conn.listDomains();
        for (final int id : ids) {
            final Domain dm = conn.domainLookupByID(id);
            allocatedMem += dm.getMaxMemory() * 1024L;
            s_logger.debug("vm: " + dm.getName() + " mem: " + dm.getMaxMemory() * 1024L);
        }
        final Long remainingMem = libvirtComputingResource.getTotalMemory() - allocatedMem;
        s_logger.debug("remaining mem" + remainingMem);
        return remainingMem;
    } catch (final Exception e) {
        s_logger.debug("failed to get free memory", e);
        return null;
    }
}
Also used : Domain(org.libvirt.Domain) URISyntaxException(java.net.URISyntaxException) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtException(org.libvirt.LibvirtException)

Example 85 with Domain

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

the class LibvirtUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final NicTO nic = command.getNic();
    final String vmName = command.getVmName();
    Domain vm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        vm = libvirtComputingResource.getDomain(conn, vmName);
        final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                vm.detachDevice(pluggedNic.toString());
                // each interface at this point, so inform all vif drivers
                for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
                    vifDriver.unplug(pluggedNic);
                }
                return new UnPlugNicAnswer(command, true, "success");
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final LibvirtException e) {
        final String msg = " Unplug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    } finally {
        if (vm != null) {
            try {
                vm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

Domain (org.libvirt.Domain)112 LibvirtException (org.libvirt.LibvirtException)92 Connect (org.libvirt.Connect)58 InternalErrorException (com.cloud.exception.InternalErrorException)35 Test (org.junit.Test)26 URISyntaxException (java.net.URISyntaxException)23 DomainState (org.libvirt.DomainInfo.DomainState)23 Answer (com.cloud.agent.api.Answer)22 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)22 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)22 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)22 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 IOException (java.io.IOException)21 DomainInfo (org.libvirt.DomainInfo)21 NicTO (com.cloud.agent.api.to.NicTO)20 ArrayList (java.util.ArrayList)19 ConfigurationException (javax.naming.ConfigurationException)19 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)16 FileNotFoundException (java.io.FileNotFoundException)16 ExecutionException (java.util.concurrent.ExecutionException)14