Package org.libvirt

Examples of org.libvirt.Domain


    }

    public long getCPUTime() throws LibvirtException {
        long cpuTime = 0;
        for (int id : connection.listDomains()) {
            Domain domain = connection.domainLookupByID(id);
            if (domain != null) {
                cpuTime += domain.getInfo().cpuTime;
                domain.free();
            }
        }
        return cpuTime;
    }
View Full Code Here


            System.out.println(conn.connection.domainLookupByName(foo).getXMLDesc(0));
        }
    }
   
    private Domain getDomain(String domainName) throws LibvirtException, IllegalArgumentException {
        Domain ret = connection.domainLookupByName(domainName);
        if (ret == null) {
            throw new IllegalArgumentException("Could not find a domain called '" + domainName + "'.");
        }
       
        return ret;
View Full Code Here

                in libvirt which might haunt us.
            */

            // check for existing inactive vm definition and remove it
            // this can sometimes happen during crashes, etc
            Domain dm = null;
            try {
                dm = conn.domainLookupByName(vmName);
                if (dm != null && dm.isPersistent() == 1) {
                    // this is safe because it doesn't stop running VMs
                    dm.undefine();
                }
            } catch (LibvirtException e) {
                // this is what we want, no domain found
            } finally {
                if (dm != null) {
                    dm.free();
                }
            }

            conn.domainCreateXML(domainXML, 0);
        } catch (final LibvirtException e) {
View Full Code Here

      return new Answer(cmd, true, result);
    }

    private void vifHotUnPlug (Connect conn, String vmName, String macAddr) throws InternalErrorException, LibvirtException {

        Domain vm = null;
        vm = getDomain(conn, vmName);
        List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
        for (InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(macAddr)) {
                vm.detachDevice(pluggedNic.toString());
                // We don't know which "traffic type" is associated with
                // each interface at this point, so inform all vif drivers
                for (VifDriver vifDriver : getAllVifDrivers()) {
                    vifDriver.unplug(pluggedNic);
                }
View Full Code Here

            URI uri = BroadcastDomainType.fromString(broadcastUri);
            nicTO.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
            nicTO.setBroadcastUri(uri);
        }

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

            nic.setBroadcastUri(BroadcastDomainType.Vxlan.toUri(vnet));
            nic.setIsolationuri(BroadcastDomainType.Vxlan.toUri(vnet));
            s_logger.error("mismatch now resolved to " + nic);
        }
        String vmName = cmd.getVmName();
        Domain vm = null;
        try {
            Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
            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(getVifDriver(nic.getType()).plug(nic, "Other PV (32-bit)").toString());
            return new PlugNicAnswer(cmd, true, "success");
        } catch (LibvirtException e) {
            String msg = " Plug Nic failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new PlugNicAnswer(cmd, false, msg);
        } catch (InternalErrorException e) {
            String msg = " Plug Nic failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new PlugNicAnswer(cmd, false, msg);
        } finally {
            if (vm != null) {
                try {
                    vm.free();
                } catch (LibvirtException l) {
                    s_logger.trace("Ignoring libvirt error.", l);
                }
            }
        }
View Full Code Here

    private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
        Connect conn;
        NicTO nic = cmd.getNic();
        String vmName = cmd.getVmName();
        Domain vm = null;
        try {
            conn = LibvirtConnection.getConnectionByVmName(vmName);
            vm = getDomain(conn, vmName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                    vm.detachDevice(pluggedNic.toString());
                    // We don't know which "traffic type" is associated with
                    // each interface at this point, so inform all vif drivers
                    for(VifDriver vifDriver : getAllVifDrivers()){
                        vifDriver.unplug(pluggedNic);
                    }
                    return new UnPlugNicAnswer(cmd, true, "success");
                }
            }
            return new UnPlugNicAnswer(cmd, true, "success");
        } catch (LibvirtException e) {
            String msg = " Unplug Nic failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new UnPlugNicAnswer(cmd, false, msg);
        } finally {
            if (vm != null) {
                try {
                    vm.free();
                } catch (LibvirtException l) {
                    s_logger.trace("Ignoring libvirt error.", l);
                }
            }
        }
View Full Code Here

        String snapshotPath = cmd.getSnapshotPath();
        String vmName = cmd.getVmName();
        try {
            Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
            DomainInfo.DomainState state = null;
            Domain vm = null;
            if (vmName != null) {
                try {
                    vm = getDomain(conn, cmd.getVmName());
                    state = vm.getInfo().state;
                } catch (LibvirtException e) {
                    s_logger.trace("Ignoring libvirt error.", e);
                }
            }

            KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(
                    cmd.getPool().getType(),
                    cmd.getPool().getUuid());

            KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(cmd
                    .getVolumePath());
            if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING
                    && !primaryPool.isExternalSnapshot()) {
                String vmUuid = vm.getUUIDString();
                Object[] args = new Object[] { snapshotName, vmUuid };
                String snapshot = SnapshotXML.format(args);
                s_logger.debug(snapshot);
                if (cmd.getCommandSwitch().equalsIgnoreCase(
                        ManageSnapshotCommand.CREATE_SNAPSHOT)) {
                    vm.snapshotCreateXML(snapshot);
                } else {
                    DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                    snap.delete(0);
                }

                /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
                if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                    vm.resume();
                }
            } else {
                /**
                 * For RBD we can't use libvirt to do our snapshotting or any Bash scripts.
                 * libvirt also wants to store the memory contents of the Virtual Machine,
View Full Code Here

                }
            }
            /* Delete the snapshot on primary */

            DomainInfo.DomainState state = null;
            Domain vm = null;
            if (vmName != null) {
                try {
                    vm = getDomain(conn, cmd.getVmName());
                    state = vm.getInfo().state;
                } catch (LibvirtException e) {
                    s_logger.trace("Ignoring libvirt error.", e);
                }
            }

            KVMStoragePool primaryStorage = _storagePoolMgr.getStoragePool(
                    cmd.getPool().getType(),
                    cmd.getPool().getUuid());
            if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING
                    && !primaryStorage.isExternalSnapshot()) {
                String vmUuid = vm.getUUIDString();
                Object[] args = new Object[] { snapshotName, vmUuid };
                String snapshot = SnapshotXML.format(args);
                s_logger.debug(snapshot);
                DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
                snap.delete(0);

                /*
                 * libvirt on RHEL6 doesn't handle resume event emitted from
                 * qemu
                 */
                vm = getDomain(conn, cmd.getVmName());
                state = vm.getInfo().state;
                if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
                    vm.resume();
                }
            } else {
                Script command = new Script(_manageSnapshotPath, _cmdsTimeout,
                        s_logger);
                command.add("-d", snapshotDisk.getPath());
View Full Code Here

        return state == null ? PowerState.PowerUnknown : state;
    }

    protected State getVmState(Connect conn, final String vmName) {
        int retry = 3;
        Domain vms = null;
        while (retry-- > 0) {
            try {
                vms = conn.domainLookupByName(vmName);
                State s = convertToState(vms.getInfo().state);
                return s;
            } catch (final LibvirtException e) {
                s_logger.warn("Can't get vm state " + vmName + e.getMessage()
                        + "retry:" + retry);
            } finally {
                try {
                    if (vms != null) {
                        vms.free();
                    }
                } catch (final LibvirtException l) {
                    s_logger.trace("Ignoring libvirt error.", l);
                }
            }
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.