[libvirt] Using libvirt from PHP

Hi, I am developing wimple web application in PHP to monitor and control VMs using libvirt. I hope it could become simple library for doing basic things with VMs. And I would like to ask for your opinion about different possibilities of calling libvirt. Option 1) I am calling virsh via exec command now (one virsh for every libvirt command). This can be used for local libvirt but it is very inefficient. The overhead for remote libvirt would be unbearable. I could run one virsh per page and use it "interactively" so the overhead gets lower. Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like that so I do not know, whether it is reasonable or not. Option 3) I could also create some "wrapper daemon" in C or other language with libvirt binding. But I think that this is an ugly way :-) Option 4) I could "talk" directly to the libvirt socket. But I am not sure how the communication goes there and whether it is stable or changes with every version. And I consider the socket to be internal thing of libvirt... Is there any other option I missed? And which one would you suggest? Radek

Radek, Not to advertise blatantly, but how attached are you to PHP? I'm currently working on a Django (python) WebApp that (although its *very* pre-release) could probably be adapted to what you're doing. If you are interested in assisting in development you'd be welcome to, or if you just wanted to fork off the code into your own thing you could do that too. My system consists of a python daemon used for actual libvirt interaction and a separate django web interface that interacts with the daemon via AMF over https. If you want to stick with what you have now, you could try doing something similar with a python daemon (you really need something like that for the sake of efficiency anyway, unless you can come up with a better way of maintaining libvirt connections from a stateless webapp) that your PHP frontend connects to. Or you could just steal my daemon code, although at the moment all it really does is monitoring and (as of yesterday) live migrations. What I have so far: http://trac.osuosl.org/trac/virtadmin -- Russell Haering Systems Administrator OSU Open Source Lab Radek Hladik wrote:
Hi, I am developing wimple web application in PHP to monitor and control VMs using libvirt. I hope it could become simple library for doing basic things with VMs. And I would like to ask for your opinion about different possibilities of calling libvirt.
Option 1) I am calling virsh via exec command now (one virsh for every libvirt command). This can be used for local libvirt but it is very inefficient. The overhead for remote libvirt would be unbearable. I could run one virsh per page and use it "interactively" so the overhead gets lower. Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like that so I do not know, whether it is reasonable or not. Option 3) I could also create some "wrapper daemon" in C or other language with libvirt binding. But I think that this is an ugly way :-) Option 4) I could "talk" directly to the libvirt socket. But I am not sure how the communication goes there and whether it is stable or changes with every version. And I consider the socket to be internal thing of libvirt...
Is there any other option I missed? And which one would you suggest?
Radek
-- Libvir-list mailing list Libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

Hi Russel, I am stick with PHP. I need to use this also in our internal administration system which is completely in PHP. And I would like to provide the possibility for other PHP developers to do at least the basic things (if not complete language binding) in their PHP projects. And not to mention my Python skills are lower than PHP ones. The idea with "intermediate" daemon does not sound bad to me. However it would need to be in other language than PHP or it would only solve the efficiency point of view. And I do not want to introduce other language than C. I do not think that using one connection per complete web page is a major problem. Web application can be stateful using cookies, sessions, etc. For example for calculating CPU usage I store cputime values into a session every time page is loaded and I use the session stored value for percent calculation. I am dividing tasks into PHP objects so I have object responsible for communicating with libvirt and everything else should be independent. So I am thinking about creating objects for direct virsh calls, zend extension and maybe for your daemon. However it means a lot of work to do and my time is limited :-( Radek Russell Haering napsal(a):
Radek,
Not to advertise blatantly, but how attached are you to PHP? I'm currently working on a Django (python) WebApp that (although its *very* pre-release) could probably be adapted to what you're doing. If you are interested in assisting in development you'd be welcome to, or if you just wanted to fork off the code into your own thing you could do that too. My system consists of a python daemon used for actual libvirt interaction and a separate django web interface that interacts with the daemon via AMF over https.
If you want to stick with what you have now, you could try doing something similar with a python daemon (you really need something like that for the sake of efficiency anyway, unless you can come up with a better way of maintaining libvirt connections from a stateless webapp) that your PHP frontend connects to. Or you could just steal my daemon code, although at the moment all it really does is monitoring and (as of yesterday) live migrations.
What I have so far: http://trac.osuosl.org/trac/virtadmin
-- Russell Haering Systems Administrator OSU Open Source Lab
Radek Hladik wrote:
Hi, I am developing wimple web application in PHP to monitor and control VMs using libvirt. I hope it could become simple library for doing basic things with VMs. And I would like to ask for your opinion about different possibilities of calling libvirt.
Option 1) I am calling virsh via exec command now (one virsh for every libvirt command). This can be used for local libvirt but it is very inefficient. The overhead for remote libvirt would be unbearable. I could run one virsh per page and use it "interactively" so the overhead gets lower. Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like that so I do not know, whether it is reasonable or not. Option 3) I could also create some "wrapper daemon" in C or other language with libvirt binding. But I think that this is an ugly way :-) Option 4) I could "talk" directly to the libvirt socket. But I am not sure how the communication goes there and whether it is stable or changes with every version. And I consider the socket to be internal thing of libvirt...
Is there any other option I missed? And which one would you suggest?
Radek
-- Libvir-list mailing list Libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

Hi, I am developing wimple web application in PHP to monitor and control VMs using libvirt. I hope it could become simple library for doing basic things with VMs. And I would like to ask for your opinion about different possibilities of calling libvirt.
Option 1) I am calling virsh via exec command now (one virsh for every libvirt command). This can be used for local libvirt but it is very inefficient. The overhead for remote libvirt would be unbearable. I could run one virsh per page and use it "interactively" so the overhead gets lower. Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like
so I do not know, whether it is reasonable or not. Option 3) I could also create some "wrapper daemon" in C or other language with libvirt binding. But I think that this is an ugly way :-) Option 4) I could "talk" directly to the libvirt socket. But I am not sure how the communication goes there and whether it is stable or changes with every version. And I consider the socket to be internal thing of
* Sorry, I fail email 101, third attempt to make this go to the list * Radek, Not to advertise blatantly, but how attached are you to PHP? I'm currently working on a Django (python) WebApp that (although its *very* pre-release) could probably be adapted to what you're doing. If you are interested in assisting in development you'd be welcome to, or if you just wanted to fork off the code into your own thing you could do that too. My system consists of a python daemon used for actual libvirt interaction and a separate django web interface that interacts with the daemon via AMF over https. If you want to stick with what you have now, you could try doing something similar with a python daemon (you really need something like that for the sake of efficiency anyway, unless you can come up with a better way of maintaining libvirt connections from a stateless webapp) that your PHP frontend connects to. Or you could just steal my daemon code, although at the moment all it really does is monitoring and (as of yesterday) live migrations. What I have so far: http://trac.osuosl.org/trac/virtadmin On Wed, Mar 25, 2009 at 2:01 PM, Radek Hladik <radek@eadresa.cz> wrote: that libvirt...
Is there any other option I missed? And which one would you suggest?
Radek
-- Libvir-list mailing list Libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On Wed, Mar 25, 2009 at 10:01:22PM +0100, Radek Hladik wrote:
Hi, I am developing wimple web application in PHP to monitor and control VMs using libvirt. I hope it could become simple library for doing basic things with VMs. And I would like to ask for your opinion about different possibilities of calling libvirt.
Option 1) I am calling virsh via exec command now (one virsh for every libvirt command). This can be used for local libvirt but it is very inefficient. The overhead for remote libvirt would be unbearable. I could run one virsh per page and use it "interactively" so the overhead gets lower.
Particularly if you used the encryption & authentication for remote connections, the overhead of running new virsh commands for every web page would be prohibitive for this to work.
Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like that so I do not know, whether it is reasonable or not.
This is the option we'd recommend. A few people have expressed interest in this idea in the past, but unforatuntely I'm not aware of anyone having written any code for this yet. We'd very much like to see a PHP binding for libvirt developed & happy to give advice/support to anyone attempting this. It is definitely doable, but I don't know how much of a hard learning curve PHP C extensions have... The libvirt API is quite large, but of course you would not need to suport every single API in PHP. You could build it up incrementally focusing on the APIs that you actually need in your applicaiton.
Option 3) I could also create some "wrapper daemon" in C or other language with libvirt binding. But I think that this is an ugly way :-)
Yeah, if you're going to write C, then you're better off doing a real PHP binding to libvirt C API as per Option 2.
Option 4) I could "talk" directly to the libvirt socket. But I am not sure how the communication goes there and whether it is stable or changes with every version. And I consider the socket to be internal thing of libvirt...
Recommend against this option. We consider the wire RPC protocol to be a private implementation detail. The C library is the only place where we provide a guarentee of long term compatability. Or one of the language bindings ontop of the C library (Perl, Python, OCaml, Ruby, Java, etc) Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On Thu, Mar 26, 2009 at 01:10:12PM +0000, Daniel P. Berrange wrote:
On Wed, Mar 25, 2009 at 10:01:22PM +0100, Radek Hladik wrote:
Option 2) The other option is to create Zend extension which should be able to call libvirt directly from C. However I've never done anything like that so I do not know, whether it is reasonable or not.
This is the option we'd recommend. A few people have expressed interest in this idea in the past, but unforatuntely I'm not aware of anyone having written any code for this yet. We'd very much like to see a PHP binding for libvirt developed & happy to give advice/support to anyone attempting this. It is definitely doable, but I don't know how much of a hard learning curve PHP C extensions have...
The libvirt API is quite large, but of course you would not need to suport every single API in PHP. You could build it up incrementally focusing on the APIs that you actually need in your applicaiton.
Agreed, option 2) sounds the most sane to me. There is a XML description of the API as libvirt-api.xml under docs either in the tarball or in the developper package (at least RPM one) this may (or not) be helpful in autogenerating some of the binding code. E.g: <function name='virDomainCreateXML' file='libvirt' module='libvirt'> <info>Launch a new guest domain, based on an XML description similar to the one returned by virDomainGetXMLDesc() This function may requires privileged access to the hypervisor. The domain is not persistent, so its definition will disappear when it is destroyed, or if the host is restarted (see virDomainDefineXML() to define persistent domains).</info> <return type='virDomainPtr' info='a new domain object or NULL in case of failure'/> <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/> <arg name='xmlDesc' type='const char *' info='string containing an XML description of the domain'/> <arg name='flags' type='unsigned int' info='callers should always pass 0'/> </function> you have automatic description of the parameters, names and types as well as the return value. At least of the simple bindings it may help. Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/
participants (5)
-
Daniel P. Berrange
-
Daniel Veillard
-
Radek Hladik
-
Russell
-
Russell Haering