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;
}
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;
}
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;
}
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);
}
}
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());
}
}
Aggregations