[libvirt-users] [API reference] Are there XML-handling API to retrieve info directly from xml configure file

Hi, everyone I'm writing a application using libvirt and need to query guest os's virtual network interface usage, like bytes read or written through a given interface. I know int virDomainInterfaceStats(virDomainPtr dom, const char * path, virDomainInterfaceStatsPtr stats, size_t size) is designed for my purpose but I 'm not sure how I determine the second parameter path. I'm now think of determining the virtual interface by parsing the domain's xml configure file. But I don't find that libvirt supply such functions, that means, I have to resort to other library lik libxml? thanks

On 04/06/2012 01:24 AM, Zhihua Che wrote:
Hi, everyone
I'm writing a application using libvirt and need to query guest os's virtual network interface usage, like bytes read or written through a given interface. I know int virDomainInterfaceStats(virDomainPtr dom, const char * path, virDomainInterfaceStatsPtr stats, size_t size) is designed for my purpose but I 'm not sure how I determine the second parameter path. I'm now think of determining the virtual interface by parsing the domain's xml configure file. But I don't find that libvirt supply such functions, that means, I have to resort to other library lik libxml?
You may be interested in the libvirt-glib project, which includes the libvirt-gconfig subpackage to provide an API on top of libvirt for more convenient access into domain XML: https://www.redhat.com/archives/libvirt-announce/2012-March/msg00004.html -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Fri, Apr 06, 2012 at 03:24:25PM +0800, Zhihua Che wrote:
Hi, everyone
I'm writing a application using libvirt and need to query guest os's virtual network interface usage, like bytes read or written through a given interface. I know int virDomainInterfaceStats(virDomainPtr dom, const char * path, virDomainInterfaceStatsPtr stats, size_t size) is designed for my purpose but I 'm not sure how I determine the second parameter path. I'm now think of determining the virtual interface by parsing the domain's xml configure file. But I don't find that libvirt supply such functions, that means, I have to resort to other library lik libxml?
I would also really like to see such an API/APIs in the basic libvirt API. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org

On Sat, Apr 07, 2012 at 10:36:04PM +0100, Richard W.M. Jones wrote:
On Fri, Apr 06, 2012 at 03:24:25PM +0800, Zhihua Che wrote:
Hi, everyone
I'm writing a application using libvirt and need to query guest os's virtual network interface usage, like bytes read or written through a given interface. I know int virDomainInterfaceStats(virDomainPtr dom, const char * path, virDomainInterfaceStatsPtr stats, size_t size) is designed for my purpose but I 'm not sure how I determine the second parameter path. I'm now think of determining the virtual interface by parsing the domain's xml configure file. But I don't find that libvirt supply such functions, that means, I have to resort to other library lik libxml?
I would also really like to see such an API/APIs in the basic libvirt API.
The libvirt-gconfig library provides this capability. NB, the libvirt-gconfig library is strictly independant of libvirt-gobject, since it is intended that you can use the libvirt-gconfig XML handling APIs, with the plain C libvirt APIs. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

在 2012年4月10日 下午4:33,Daniel P. Berrange <berrange@redhat.com> 写道:
On Sat, Apr 07, 2012 at 10:36:04PM +0100, Richard W.M. Jones wrote:
On Fri, Apr 06, 2012 at 03:24:25PM +0800, Zhihua Che wrote:
Hi, everyone
I'm writing a application using libvirt and need to query guest os's virtual network interface usage, like bytes read or written through a given interface. I know int virDomainInterfaceStats(virDomainPtr dom, const char * path, virDomainInterfaceStatsPtr stats, size_t size) is designed for my purpose but I 'm not sure how I determine the second parameter path. I'm now think of determining the virtual interface by parsing the domain's xml configure file. But I don't find that libvirt supply such functions, that means, I have to resort to other library lik libxml?
I would also really like to see such an API/APIs in the basic libvirt API.
The libvirt-gconfig library provides this capability. NB, the libvirt-gconfig library is strictly independant of libvirt-gobject, since it is intended that you can use the libvirt-gconfig XML handling APIs, with the plain C libvirt APIs.
Regards, Daniel
I compile and install the libvirt-glib and I find its doc a little lean. I mean it's really hard to get applicable info from it because it just lists a list of functions without introducing any concepts first. I find the doc in share/gtk-doc directory. Do I miss anything? Is there any sample code or introduction to its concepts? BTW, Does libvirt supply any function which can return the process identifier of the domain? Thanks

On Wed, Apr 11, 2012 at 06:41:34PM +0800, Zhihua Che wrote:
I compile and install the libvirt-glib and I find its doc a little lean. I mean it's really hard to get applicable info from it because it just lists a list of functions without introducing any concepts first.
I find the doc in share/gtk-doc directory. Do I miss anything? Is there any sample code or introduction to its concepts?
Yes, you are correct, the docs are not really there yet. The test cases actually provide pretty good examples of how to use the APis - take a look at libvirt-gconfig/tests/test-domain-create.c in particular. If you're interested in python, there is examples/config-demo.py
BTW, Does libvirt supply any function which can return the process identifier of the domain?
No, we don't consider the PID of the QEMU process to be part of the public API, since that is a hypervisor specific implementation detail. Why do you want to know that ? Maybe there is some API you can use to get do the same thing Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Wed, Apr 11, 2012 at 12:01:55PM +0100, Daniel P. Berrange wrote:
On Wed, Apr 11, 2012 at 06:41:34PM +0800, Zhihua Che wrote:
BTW, Does libvirt supply any function which can return the process identifier of the domain?
No, we don't consider the PID of the QEMU process to be part of the public API, since that is a hypervisor specific implementation detail. Why do you want to know that ? Maybe there is some API you can use to get do the same thing
I agree with Dan that you're probably doing it for the wrong reasons and shouldn't need to know the PID. Nevertheless, it's pretty simple to map a running domain name to a qemu PID, by parsing the '-name' parameter from the process list. eg the following works for simple names (no spaces, metachars etc): $ sudo virsh list --all Id Name State ---------------------------------- 2 builder-rhel6 running $ ps ax | grep '[-]name builder-rhel6' | awk '{print $1}' 2311 Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw

在 2012年4月11日 下午7:22,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 11, 2012 at 12:01:55PM +0100, Daniel P. Berrange wrote:
On Wed, Apr 11, 2012 at 06:41:34PM +0800, Zhihua Che wrote:
BTW, Does libvirt supply any function which can return the process identifier of the domain?
No, we don't consider the PID of the QEMU process to be part of the public API, since that is a hypervisor specific implementation detail. Why do you want to know that ? Maybe there is some API you can use to get do the same thing
I agree with Dan that you're probably doing it for the wrong reasons and shouldn't need to know the PID.
Yes, I know this is not reasonable, I just wanna try my luck.:( I just wonder if I could distinguish the VM process and non-VM process.
Nevertheless, it's pretty simple to map a running domain name to a qemu PID, by parsing the '-name' parameter from the process list. eg the following works for simple names (no spaces, metachars etc):
$ sudo virsh list --all Id Name State ---------------------------------- 2 builder-rhel6 running
$ ps ax | grep '[-]name builder-rhel6' | awk '{print $1}' 2311
Rich.
Yeah, I tried a similar way and just worried what if someone faked a VM process.

在 2012年4月11日 下午7:22,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 11, 2012 at 12:01:55PM +0100, Daniel P. Berrange wrote:
On Wed, Apr 11, 2012 at 06:41:34PM +0800, Zhihua Che wrote:
BTW, Does libvirt supply any function which can return the process identifier of the domain?
No, we don't consider the PID of the QEMU process to be part of the public API, since that is a hypervisor specific implementation detail. Why do you want to know that ? Maybe there is some API you can use to get do the same thing
I agree with Dan that you're probably doing it for the wrong reasons and shouldn't need to know the PID.
Nevertheless, it's pretty simple to map a running domain name to a qemu PID, by parsing the '-name' parameter from the process list. eg the following works for simple names (no spaces, metachars etc):
$ sudo virsh list --all Id Name State ---------------------------------- 2 builder-rhel6 running
$ ps ax | grep '[-]name builder-rhel6' | awk '{print $1}' 2311
Rich.
Hi, I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory. But I did a interesting experiment. I edited a xml config file ubuntu-1.xml in the above directory. After defining the domain, I removed the domain's xml config file and found that it could still be started by 'start ubuntu-1'. I guess the action 'define' would parse the original config file and store it in another 'safe' place. What's this place? I wish I could handle some emergence in case if I knew this place.

On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v

在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
I agree with you. But I wanna know where virsh stores all the domains' config files. Image that if I uninstalled virsh and reinstalled it, I wish I could manipulate all the previous domains.

在 2012年4月18日 下午5:47,Zhihua Che <zhihua.che@gmail.com> 写道:
在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
Correction I agree with you. But I wanna know where libvirt stores all the domains' config files. Image that if I uninstalled libvirt and reinstalled it, I wish I could manipulate all the previous domains.

On Wed, Apr 18, 2012 at 06:09:35PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午5:47,Zhihua Che <zhihua.che@gmail.com> 写道:
在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
Correction
I agree with you. But I wanna know where libvirt stores all the domains' config files. Image that if I uninstalled libvirt and reinstalled it, I wish I could manipulate all the previous domains.
All the persistent configs are under /etc/libvirt When a guest is running, there is a second config stored in /var/run/libvirt Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

在 2012年4月18日 下午6:13,Daniel P. Berrange <berrange@redhat.com> 写道:
On Wed, Apr 18, 2012 at 06:09:35PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午5:47,Zhihua Che <zhihua.che@gmail.com> 写道:
在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
Correction
I agree with you. But I wanna know where libvirt stores all the domains' config files. Image that if I uninstalled libvirt and reinstalled it, I wish I could manipulate all the previous domains.
All the persistent configs are under /etc/libvirt
When a guest is running, there is a second config stored in /var/run/libvirt
I thought so. But I removed config file uder working/etc/libvirt and It sill worked. By the way, I installed my libvirt under directory working. Of course I searched the whole system and found no other config files.

On Wed, Apr 18, 2012 at 06:48:56PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午6:13,Daniel P. Berrange <berrange@redhat.com> 写道:
On Wed, Apr 18, 2012 at 06:09:35PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午5:47,Zhihua Che <zhihua.che@gmail.com> 写道:
在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote:
I guess I miss another thing. I thought the domain xml config file was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
Correction
I agree with you. But I wanna know where libvirt stores all the domains' config files. Image that if I uninstalled libvirt and reinstalled it, I wish I could manipulate all the previous domains.
All the persistent configs are under /etc/libvirt
When a guest is running, there is a second config stored in /var/run/libvirt
I thought so. But I removed config file uder working/etc/libvirt and It sill worked.
Libvirtd loads all the configs under /etc/libvirt into memory, so if you delete them, libvirtd will still know about them until you restart it.
By the way, I installed my libvirt under directory working. Of course I searched the whole system and found no other config files.
If you are runing libvirtd as non-root then it uses $HOME/.libvirt If you built a custom libvirtd from source and run it as root, then it'll use $sysconfdir/libvirt (where $sysconfdir is the --sysconfdir value passed to the configure script) Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

在 2012年4月18日 下午6:52,Daniel P. Berrange <berrange@redhat.com> 写道:
On Wed, Apr 18, 2012 at 06:48:56PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午6:13,Daniel P. Berrange <berrange@redhat.com> 写道:
On Wed, Apr 18, 2012 at 06:09:35PM +0800, Zhihua Che wrote:
在 2012年4月18日 下午5:47,Zhihua Che <zhihua.che@gmail.com> 写道:
在 2012年4月18日 下午5:31,Richard W.M. Jones <rjones@redhat.com> 写道:
On Wed, Apr 18, 2012 at 10:24:24AM +0800, Zhihua Che wrote: > I guess I miss another thing. I thought the domain xml config file > was stored in etc/libvirt/qemu/ directory.
libvirt manages the config files. To edit them, DON'T edit them directly, use 'virsh edit'.
Rich.
Correction
I agree with you. But I wanna know where libvirt stores all the domains' config files. Image that if I uninstalled libvirt and reinstalled it, I wish I could manipulate all the previous domains.
All the persistent configs are under /etc/libvirt
When a guest is running, there is a second config stored in /var/run/libvirt
I thought so. But I removed config file uder working/etc/libvirt and It sill worked.
Libvirtd loads all the configs under /etc/libvirt into memory, so if you delete them, libvirtd will still know about them until you restart it.
By the way, I installed my libvirt under directory working. Of course I searched the whole system and found no other config files.
If you are runing libvirtd as non-root then it uses $HOME/.libvirt
If you built a custom libvirtd from source and run it as root, then it'll use $sysconfdir/libvirt (where $sysconfdir is the --sysconfdir value passed to the configure script)
Yeah, you're right. I found it in $HOME/.libvirt/qemu. I didn't search the hidden files. I made a mistake.:(
participants (4)
-
Daniel P. Berrange
-
Eric Blake
-
Richard W.M. Jones
-
Zhihua Che