[libvirt-users] Finding out if Xen or KVM is used

Matthias Bolte matthias.bolte at googlemail.com
Sun Nov 6 14:02:13 UTC 2011


2011/11/5 PREETHI RAMESH <iampreethiramesh at gmail.com>:
> I've got 2 doubts:
> 1.I've been trying to write an application using Libvirt's Java API which
> should run on both Xen and KVM running machines and execute the Xen or KVM
> code based on the hypervisor used. I want the physical machine to know if
> it's running Xen or KVM and execute only that code.
>
> While using getURI(), I need to use it as
> conn.getURI()
> but then I can't use conn without declaring it previously as Connect
> conn=new Connect("URI");
>
> Is there any way to get the URI used and hence the hypervisor used?

The URI is the way you can tell libvirt how and to which hypervisor it
should connect.

libvirt also has some autodetection mechanism for local hypervisors.
Pass null as URI and libvirt will connect to the first available
locally installed hypervisor. This should give you the behavior you
want, because you can then use conn.getURI() to get the actual URI
used and detect the hypervisor from it.

> 2. This question concerns the usage of the listDomains(). When I run this
> piece of code in KVM, it works perfectly fine but throws an array out of
> bounds exception in Xen.
>
> Connect conn=null;
>              conn = new Connect("xen:///", true);
>              int[] id = conn.listDomains();
>              System.out.println("lD of VM " +id[0]);

Well, your code assumes that id.length is >= 1, but listDomains() can
return an empty array when there are no active domains known to
libvirt. So you need to check id.length before accessing any element
in the array.

-- 
Matthias Bolte
http://photron.blogspot.com




More information about the libvirt-users mailing list