As an experiment I managed to call libvirt from C# (using Mono, but
similar code should work for .Net). This email should give interested
people enough information to produce a proper set of bindings for
libvirt.
----------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
[StructLayout (LayoutKind.Sequential)]
class NodeInfo {
[MarshalAs (UnmanagedType.ByValTStr, SizeConst=32)]
public string model;
public ulong memory;
public uint cpus;
public uint mhz;
public uint nodes;
public uint sockets;
public uint cores;
public uint threads;
}
class Connection {
IntPtr _handle;
[DllImport ("libvirt.so")]
static extern IntPtr virConnectOpen ([In] string name);
public Connection (string name) {
_handle = virConnectOpen (name);
}
[DllImport ("libvirt.so")]
static extern IntPtr virConnectClose (IntPtr h);
~Connection () {
virConnectClose (_handle);
}
[DllImport ("libvirt.so")]
static extern int virNodeGetInfo (IntPtr h, [Out] NodeInfo info);
public NodeInfo nodeGetInfo () {
NodeInfo info = new NodeInfo ();
if (virNodeGetInfo (_handle, info) == -1) {
// throw new LibvirtException ("virNodeGetInfo");
}
return info;
}
}
class Test
{
public static void Main ()
{
Connection conn = new Connection ("test:///default");
NodeInfo info = conn.nodeGetInfo ();
Console.WriteLine ("Results of virNodeGetInfo:");
Console.WriteLine ("model = {0}", info.model);
Console.WriteLine ("memory = {0}", info.memory);
Console.WriteLine ("cpus = {0}", info.cpus);
Console.WriteLine ("mhz = {0}", info.mhz);
Console.WriteLine ("nodes = {0}", info.nodes);
Console.WriteLine ("sockets = {0}", info.sockets);
Console.WriteLine ("cores = {0}", info.cores);
Console.WriteLine ("threads = {0}", info.threads);
}
}
----------------------------------------------------------------------
Compile and run this code with:
$ gmcs libvirt-test.c#
$ LIBVIRT_DEBUG=1 mono ./libvirt-test.exe
----------------------------------------------------------------------
DEBUG: libvirt.c: virInitialize (register drivers)
DEBUG: libvirt.c: virConnectOpen (name=test:///default)
DEBUG: libvirt.c: do_open (name "test:///default" to URI components:
scheme test
opaque (null)
authority (null)
server (null)
user (null)
port 0
path /default
)
DEBUG: libvirt.c: do_open (trying driver 0 (Test) ...)
DEBUG: libvirt.c: do_open (driver 0 Test returned SUCCESS)
DEBUG: libvirt.c: do_open (network driver 0 Test returned SUCCESS)
DEBUG: libvirt.c: do_open (storage driver 0 Test returned SUCCESS)
DEBUG: libvirt.c: virNodeGetInfo (conn=0xa8d560, info=0x7fff9530c070)
Results of virNodeGetInfo:
model = i686
memory = 3145728
cpus = 16
mhz = 1400
nodes = 2
sockets = 2
cores = 2
threads = 2
DEBUG: libvirt.c: virConnectClose (conn=0xa8d560)
DEBUG: hash.c: virUnrefConnect (unref connection 0xa8d560 test:///default 1)
DEBUG: hash.c: virReleaseConnect (release connection 0xa8d560 test:///default)
----------------------------------------------------------------------
Rich.
--
Richard Jones, Emerging Technologies, Red Hat
http://et.redhat.com/~rjones
virt-top is 'top' for virtual machines. Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://et.redhat.com/~rjones/virt-top