Package org.libvirt

Examples of org.libvirt.Domain


    }

    protected Integer getVncPort(Connect conn, String vmName)
            throws LibvirtException {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
                    .getBytes()));
            String xmlDesc = dm.getXMLDesc(0);
            parser.parseDomainXML(xmlDesc);
            return parser.getVncPort();
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException l) {

            }
        }
View Full Code Here


        return parser.getEmulator();
    }

    private String getGuestType(Connect conn, String vmName) {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
                    .getBytes()));
            String xmlDesc = dm.getXMLDesc(0);
            parser.parseDomainXML(xmlDesc);
            return parser.getDescription();
        } catch (LibvirtException e) {
            return null;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException l) {

            }
        }
View Full Code Here

                .domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
    }

    protected List<InterfaceDef> getInterfaces(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.getInterfaces();

        } catch (LibvirtException e) {
            s_logger.debug("Failed to get dom xml: " + e.toString());
            return new ArrayList<InterfaceDef>();
        } catch (Exception e) {
            s_logger.debug("Failed to get dom xml: " + e.toString());
            return new ArrayList<InterfaceDef>();
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException e) {

            }
        }
View Full Code Here

        }
    }

    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) {

            }
        }
View Full Code Here

        Calendar _timestamp;
    }

    private VmStatsEntry getVmStat(Connect conn, String vmName)
            throws LibvirtException {
        Domain dm = null;
        try {
            dm = getDomain(conn, vmName);
            DomainInfo info = dm.getInfo();

            VmStatsEntry stats = new VmStatsEntry();
            stats.setNumCPUs(info.nrVirtCpu);
            stats.setEntityType("vm");

            /* get cpu utilization */
            vmStats oldStats = null;

            Calendar now = Calendar.getInstance();

            oldStats = _vmStats.get(vmName);

            long elapsedTime = 0;
            if (oldStats != null) {
                elapsedTime = now.getTimeInMillis()
                        - oldStats._timestamp.getTimeInMillis();
                double utilization = (info.cpuTime - oldStats._usedTime)
                        / ((double) elapsedTime * 1000000);

                NodeInfo node = conn.nodeInfo();
                utilization = utilization / node.cpus;
                if(utilization > 0){
                  stats.setCPUUtilization(utilization * 100);
                }
            }

            /* get network stats */

            List<InterfaceDef> vifs = getInterfaces(conn, vmName);
            long rx = 0;
            long tx = 0;
            for (InterfaceDef vif : vifs) {
                DomainInterfaceStats ifStats = dm.interfaceStats(vif
                        .getDevName());
                rx += ifStats.rx_bytes;
                tx += ifStats.tx_bytes;
            }

            if (oldStats != null) {
                long deltarx = rx - oldStats._rx;
                if (deltarx > 0)
                    stats.setNetworkReadKBs(deltarx / 1000);
                long deltatx = tx - oldStats._tx;
                if (deltatx > 0)
                    stats.setNetworkWriteKBs(deltatx / 1000);
            }

            vmStats newStat = new vmStats();
            newStat._usedTime = info.cpuTime;
            newStat._rx = rx;
            newStat._tx = tx;
            newStat._timestamp = now;
            _vmStats.put(vmName, newStat);
            return stats;
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
    }
View Full Code Here

    protected String startDomain(Connect conn, String vmName, String domainXML)
            throws LibvirtException, InternalErrorException {
        /* No duplicated vm, we will success, or failed */
        boolean failed = false;
        Domain dm = null;
        try {
            dm = conn.domainDefineXML(domainXML);
        } catch (final LibvirtException e) {
            /* Duplicated defined vm */
            s_logger.warn("Failed to define domain " + vmName + ": "
                    + e.getMessage());
            failed = true;
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {

            }
        }

        /* If failed, undefine the vm */
        Domain dmOld = null;
        Domain dmNew = null;
        try {
            if (failed) {
                dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
                        .getBytes()));
                dmOld.undefine();
                dmNew = conn.domainDefineXML(domainXML);
            }
        } catch (final LibvirtException e) {
            s_logger.warn("Failed to define domain (second time) " + vmName
                    + ": " + e.getMessage());
            throw e;
        } catch (Exception e) {
            s_logger.warn("Failed to define domain (second time) " + vmName
                    + ": " + e.getMessage());
            throw new InternalErrorException(e.toString());
        } finally {
            try {
                if (dmOld != null) {
                    dmOld.free();
                }
                if (dmNew != null) {
                    dmNew.free();
                }
            } catch (final LibvirtException e) {

            }
        }
View Full Code Here

        } else {
            nicTO.setBroadcastType(BroadcastDomainType.Vlan);
            nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
        }

        Domain vm = getDomain(conn, vmName);
        vm.attachDevice(_vifDriver.plug(nicTO, "Other PV (32-bit)").toString());
    }
View Full Code Here

        Connect conn;
        NicTO nic = cmd.getNic();
        String vmName = cmd.getVmName();
        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, vmName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
            Integer nicnum = 0;
            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                    s_logger.debug("found existing nic for mac "+ pluggedNic.getMacAddress() + " at index "+nicnum);
                    return new PlugNicAnswer(cmd, true, "success");
                }
                nicnum++;
            }
            vm.attachDevice(_vifDriver.plug(nic, "Other PV (32-bit)").toString());
            return new PlugNicAnswer(cmd, true, "success");
        } catch (Exception e) {
            String msg = " Plug Nic failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new PlugNicAnswer(cmd, false, msg);
View Full Code Here

        Connect conn;
        NicTO nic = cmd.getNic();
        String vmName = cmd.getInstanceName();
        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, vmName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                    vm.detachDevice(pluggedNic.toString());
                    return new UnPlugNicAnswer(cmd, true, "success");
                }
            }
            return new UnPlugNicAnswer(cmd, true, "success");
        } catch (Exception e) {
View Full Code Here

            }
        }

        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, routerName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
            InterfaceDef routerNic = null;

            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
View Full Code Here

TOP

Related Classes of org.libvirt.Domain

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.