Search in sources :

Example 91 with Connect

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

the class LibvirtConnection method getConnection.

public static Connect getConnection(String hypervisorURI) throws LibvirtException {
    s_logger.debug("Looking for libvirtd connection at: " + hypervisorURI);
    Connect conn = s_connections.get(hypervisorURI);
    if (conn == null) {
        s_logger.info("No existing libvirtd connection found. Opening a new one");
        conn = new Connect(hypervisorURI, false);
        s_logger.debug("Successfully connected to libvirt at: " + hypervisorURI);
        s_connections.put(hypervisorURI, conn);
    } else {
        try {
            conn.getVersion();
        } catch (LibvirtException e) {
            s_logger.error("Connection with libvirtd is broken: " + e.getMessage());
            s_logger.debug("Opening a new libvirtd connection to: " + hypervisorURI);
            conn = new Connect(hypervisorURI, false);
            s_connections.put(hypervisorURI, conn);
        }
    }
    return conn;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 92 with Connect

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

the class LibvirtComputingResource method getHostVmStateReport.

private HashMap<String, HostVmStateReportEntry> getHostVmStateReport() {
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<String, HostVmStateReportEntry>();
    Connect conn = null;
    if (_hypervisorType == HypervisorType.LXC) {
        try {
            conn = LibvirtConnection.getConnectionByType(HypervisorType.LXC.toString());
            vmStates.putAll(getHostVmStateReport(conn));
            conn = LibvirtConnection.getConnectionByType(HypervisorType.KVM.toString());
            vmStates.putAll(getHostVmStateReport(conn));
        } catch (final LibvirtException e) {
            s_logger.debug("Failed to get connection: " + e.getMessage());
        }
    }
    if (_hypervisorType == HypervisorType.KVM) {
        try {
            conn = LibvirtConnection.getConnectionByType(HypervisorType.KVM.toString());
            vmStates.putAll(getHostVmStateReport(conn));
        } catch (final LibvirtException e) {
            s_logger.debug("Failed to get connection: " + e.getMessage());
        }
    }
    return vmStates;
}
Also used : LibvirtException(org.libvirt.LibvirtException) HostVmStateReportEntry(com.cloud.agent.api.HostVmStateReportEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect)

Example 93 with Connect

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

the class LibvirtComputingResource method getHostInfo.

protected List<Object> getHostInfo() {
    final ArrayList<Object> info = new ArrayList<Object>();
    long speed = 0;
    long cpus = 0;
    long ram = 0;
    int cpuSockets = 0;
    String cap = null;
    try {
        final Connect conn = LibvirtConnection.getConnection();
        final NodeInfo hosts = conn.nodeInfo();
        speed = getCpuSpeed(hosts);
        /*
            * Some CPUs report a single socket and multiple NUMA cells.
            * We need to multiply them to get the correct socket count.
            */
        cpuSockets = hosts.sockets;
        if (hosts.nodes > 0) {
            cpuSockets = hosts.sockets * hosts.nodes;
        }
        cpus = hosts.cpus;
        ram = hosts.memory * 1024L;
        final LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
        parser.parseCapabilitiesXML(conn.getCapabilities());
        final ArrayList<String> oss = parser.getGuestOsType();
        for (final String s : oss) {
            /*
                 * Even host supports guest os type more than hvm, we only
                 * report hvm to management server
                 */
            if (s.equalsIgnoreCase("hvm")) {
                cap = "hvm";
            }
        }
    } catch (final LibvirtException e) {
        s_logger.trace("Ignoring libvirt error.", e);
    }
    if (isSnapshotSupported()) {
        cap = cap + ",snapshot";
    }
    info.add((int) cpus);
    info.add(speed);
    // Report system's RAM as actual RAM minus host OS reserved RAM
    ram = ram - _dom0MinMem;
    info.add(ram);
    info.add(cap);
    info.add(_dom0MinMem);
    info.add(cpuSockets);
    s_logger.debug("cpus=" + cpus + ", speed=" + speed + ", ram=" + ram + ", _dom0MinMem=" + _dom0MinMem + ", cpu sockets=" + cpuSockets);
    return info;
}
Also used : LibvirtException(org.libvirt.LibvirtException) NodeInfo(org.libvirt.NodeInfo) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList)

Example 94 with Connect

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final SetSourceNatCommand cmd) {
    Connect conn;
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final IpAddressTO pubIP = cmd.getIpAddress();
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        Integer devNum = 0;
        final String pubVlan = pubIP.getBroadcastUri();
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            final String pluggedVlanBr = pluggedNic.getBrName();
            final String pluggedVlanId = getBroadcastUriFromBridge(pluggedVlanBr);
            if (pubVlan.equalsIgnoreCase(Vlan.UNTAGGED) && pluggedVlanBr.equalsIgnoreCase(_publicBridgeName)) {
                break;
            } else if (pluggedVlanBr.equalsIgnoreCase(_linkLocalBridgeName)) {
            /*skip over, no physical bridge device exists*/
            } else if (pluggedVlanId == null) {
                /*this should only be true in the case of link local bridge*/
                return new ExecutionResult(false, "unable to find the vlan id for bridge " + pluggedVlanBr + " when attempting to set up" + pubVlan + " on router " + routerName);
            } else if (pluggedVlanId.equals(pubVlan)) {
                break;
            }
            devNum++;
        }
        pubIP.setNicDevId(devNum);
        return new ExecutionResult(true, "success");
    } catch (final LibvirtException e) {
        final String msg = "Ip SNAT failure due to " + e.toString();
        s_logger.error(msg, e);
        return new ExecutionResult(false, msg);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult)

Example 95 with Connect

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

public ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    Connect conn;
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> nics = getInterfaces(conn, routerName);
        final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (final InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
                    broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
                } else {
                    final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
                    broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
                }
            }
            nicPos++;
        }
        final IpAddressTO[] ips = cmd.getIpAddresses();
        int nicNum = 0;
        for (final IpAddressTO ip : ips) {
            boolean newNic = false;
            if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
                broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
                newNic = true;
            }
            nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            ip.setNicDevId(nicNum);
            ip.setNewNic(newNic);
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final InternalErrorException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)

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