Search in sources :

Example 6 with Domain

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

the class LibvirtComputingResource method getDisks.

protected List<DiskDef> getDisks(Connect conn, String vmName) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getDisks();
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<DiskDef>();
    } catch (Exception e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<DiskDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 7 with Domain

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

the class LibvirtComputingResource method stopVM.

protected String stopVM(Connect conn, String vmName, boolean force) {
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        if (force) {
            if (dm.getInfo().state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
                dm.destroy();
            }
        } else {
            if (dm.getInfo().state == DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
                return null;
            }
            dm.shutdown();
            int retry = _stopTimeout / 2000;
            /* Wait for the domain gets into shutoff state */
            while ((dm.getInfo().state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) && (retry >= 0)) {
                Thread.sleep(2000);
                retry--;
            }
            if (retry < 0) {
                s_logger.warn("Timed out waiting for domain " + vmName + " to shutdown gracefully");
                return Script.ERR_TIMEOUT;
            }
        }
    } catch (LibvirtException e) {
        s_logger.debug("Failed to stop VM :" + vmName + " :", e);
        return e.getMessage();
    } catch (InterruptedException ie) {
        s_logger.debug("Interrupted sleep");
        return ie.getMessage();
    } catch (Exception e) {
        s_logger.debug("Failed to stop VM :" + vmName + " :", e);
        return e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 8 with Domain

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

the class LibvirtComputingResource method attachOrDetachDisk.

protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName, KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, InternalErrorException {
    List<DiskDef> disks = null;
    Domain dm = null;
    DiskDef diskdef = null;
    try {
        if (!attach) {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
            LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
            String xml = dm.getXMLDesc(0);
            parser.parseDomainXML(xml);
            disks = parser.getDisks();
            for (DiskDef disk : disks) {
                String file = disk.getDiskPath();
                if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
                    diskdef = disk;
                    break;
                }
            }
            if (diskdef == null) {
                throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
            }
        } else {
            diskdef = new DiskDef();
            if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
            } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO);
            }
        }
        String xml = diskdef.toString();
        return attachOrDetachDevice(conn, attach, vmName, xml);
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain)

Example 9 with Domain

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

the class CloudZonesComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        try {
            Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        } catch (LibvirtException e) {
            state = State.Stopped;
            return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
        }
        String macAddress = null;
        if (vmName.startsWith("i-")) {
            List<InterfaceDef> nics = getInterfaces(conn, vmName);
            if (!nics.isEmpty()) {
                macAddress = nics.get(0).getMacAddress();
            }
        }
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        try {
            cleanupVnet(conn, cmd.getVnet());
            _dhcpSnooper.cleanup(macAddress, vmName);
            _vmDataServer.handleVmStopped(cmd.getVmName());
        } catch (Exception e) {
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException)

Example 10 with Domain

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

the class LibvirtCreateVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final CreateVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    String vmName = cmd.getVmName();
    String vmSnapshotName = cmd.getTarget().getSnapshotName();
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        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);
        }
        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);
        }
        String vmSnapshotXML = "<domainsnapshot>" + "  <name>" + vmSnapshotName + "</name>" + "  <memory snapshot='internal' />" + "</domainsnapshot>";
        dm.snapshotCreateXML(vmSnapshotXML);
        return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
    } catch (LibvirtException e) {
        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 (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)

Aggregations

Domain (org.libvirt.Domain)67 LibvirtException (org.libvirt.LibvirtException)54 Connect (org.libvirt.Connect)30 InternalErrorException (com.cloud.exception.InternalErrorException)24 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 URISyntaxException (java.net.URISyntaxException)17 IOException (java.io.IOException)16 ConfigurationException (javax.naming.ConfigurationException)16 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)15 FileNotFoundException (java.io.FileNotFoundException)14 Test (org.junit.Test)14 DomainInfo (org.libvirt.DomainInfo)14 ExecutionException (java.util.concurrent.ExecutionException)13 Answer (com.cloud.agent.api.Answer)11 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)11 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)11 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)11 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)11 NicTO (com.cloud.agent.api.to.NicTO)10 ArrayList (java.util.ArrayList)10