[PATCH 0/7] Add libvirt domain to core API
This series of patches aim to make adding disks from libvirt domains easy through the core API. These two new APIs allow you to add the disks from a libvirt domain. The higher level add-domain API takes the name of the libvirt domain as a string and connects to libvirt itself. The lower level add-libvirt-dom API relies on the program to connect to libvirt and pass the virDomainPtr into the API call. int guestfs_add_domain (guestfs_h *g, const char *dom, ...); int guestfs_add_libvirt_dom (guestfs_h *g, virDomainPtr dom, ...); In guestfish you can use the 'domain' command to access the higher level API, eg:
<fs> domain Fedora14 libvirturi:qemu:///system 1
(The returned number is the number of disks that were added.) Since it would be impossible for the user to create a virDomainPtr inside guestfish, the lower-level API is not exposed. The 'guestfish -d' option is reimplemented to use this new API, simplifying guestfish (and guestmount) and meaning that these programs no longer directly depend on libvirt or libxml2. As well as that I wanted libvirt and libxml2 to be optional dependencies. Distro packagers shouldn't need them in order to compile libguestfs. So I took the opportunity to make those optional, and at the same time to make pcre, libmagic and hivex optional. Of course if you don't have those dependencies then certain core API features are disabled. 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
-- 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
On 11/10/2010 11:45 AM, Richard W.M. Jones wrote:
From a0b4caa0821b759de01361b7019c9c9c9607027d Mon Sep 17 00:00:00 2001 From: Richard Jones<rjones@redhat.com> Date: Tue, 9 Nov 2010 15:59:40 +0000 Subject: [PATCH 1/7] lib: Make pcre, libmagic and hivex libraries optional.
This change makes these libraries optional. If they are not available at compile time then certain core API features will be disabled (see below).
This also changes PCRE detection to use pkg-config instead of the ad hoc autoconf checks.
The large inspect.c file has been split out into separate function-specific files.
file-architecture: requires pcre& libmagic inspection: requires pcre& hivex --- README | 4 +- configure.ac | 45 ++-- po/POTFILES.in | 2 + src/Makefile.am | 6 +- src/filearch.c | 267 +++++++++++++++++++++ src/guestfs-internal.h | 16 ++ src/guestfs.c | 9 + src/inspect.c | 607 +++++++++++++++--------------------------------- src/listfs.c | 169 ++++++++++++++ 9 files changed, 674 insertions(+), 451 deletions(-) create mode 100644 src/filearch.c create mode 100644 src/listfs.c
What's the use case here? Embedded systems running libguestfs? Doesn't seem likely. If nobody is actually likely to build libguestfs this way, this only seems to complicate the build system further in ways which will be rarely tested. Splitting inspect in 2 isn't a bad idea, though. Matt -- Matthew Booth, RHCA, RHCSS Red Hat Engineering, Virtualisation Team GPG ID: D33C3490 GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490
On Thu, Nov 11, 2010 at 09:56:37AM +0000, Matthew Booth wrote:
On 11/10/2010 11:45 AM, Richard W.M. Jones wrote:
From a0b4caa0821b759de01361b7019c9c9c9607027d Mon Sep 17 00:00:00 2001 From: Richard Jones<rjones@redhat.com> Date: Tue, 9 Nov 2010 15:59:40 +0000 Subject: [PATCH 1/7] lib: Make pcre, libmagic and hivex libraries optional.
This change makes these libraries optional. If they are not available at compile time then certain core API features will be disabled (see below).
This also changes PCRE detection to use pkg-config instead of the ad hoc autoconf checks.
The large inspect.c file has been split out into separate function-specific files.
file-architecture: requires pcre& libmagic inspection: requires pcre& hivex --- README | 4 +- configure.ac | 45 ++-- po/POTFILES.in | 2 + src/Makefile.am | 6 +- src/filearch.c | 267 +++++++++++++++++++++ src/guestfs-internal.h | 16 ++ src/guestfs.c | 9 + src/inspect.c | 607 +++++++++++++++--------------------------------- src/listfs.c | 169 ++++++++++++++ 9 files changed, 674 insertions(+), 451 deletions(-) create mode 100644 src/filearch.c create mode 100644 src/listfs.c
What's the use case here? Embedded systems running libguestfs? Doesn't seem likely. If nobody is actually likely to build libguestfs this way, this only seems to complicate the build system further in ways which will be rarely tested.
Splitting inspect in 2 isn't a bad idea, though.
I think it's better to view this patch as a cleanup of the configure script. Making hivex optional I think is a good idea, and once you've done that, it's a cleanup to make the other 2 libraries (+ libvirt & libxml2 in later patch) use the same style of configure test. Having said that, I did test this with each library both installed and removed. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
-- 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
-- Richard Jones, Virtualization Group, Red Hat http://people.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
-- 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
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does #include <libvirt/libvirt.h> #include <guestfs.h> because they'll get a duplicated definition of virDomain from both headers. IMHO, this hack is a waste of time and it is better to just #include <libvirt/libvirt.h> from the header files instead. A build time dependancy on the libvirt client is really not a burden, when you consider that weight of the entire build chain you already have to pull in. Even with the build time dep, you can still have the dep be optional at runtime via your use of dlopen().
diff --git a/src/virt.c b/src/virt.c new file mode 100644 index 0000000..5816f61 --- /dev/null +++ b/src/virt.c @@ -0,0 +1,287 @@ +/* libguestfs + * Copyright (C) 2010 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#ifdef HAVE_LIBVIRT +#include <libvirt/libvirt.h> +#include <libvirt/virterror.h> +#endif + +#ifdef HAVE_LIBXML2 +#include <libxml/xpath.h> +#include <libxml/parser.h> +#include <libxml/tree.h> +#endif + +#include "guestfs.h" +#include "guestfs-internal.h" +#include "guestfs-internal-actions.h" +#include "guestfs_protocol.h" + +#if defined(HAVE_LIBVIRT) && defined(HAVE_LIBXML2) + +static void init_libxml2 (void) __attribute__((constructor)); + +static void +init_libxml2 (void) +{ + /* I am told that you don't really need to call virInitialize ... */ + + xmlInitParser (); + LIBXML_TEST_VERSION; +} + +int +guestfs__add_domain (guestfs_h *g, const char *domain_name, + const struct guestfs_add_domain_argv *optargs) +{ + virErrorPtr err; + virConnectPtr conn = NULL; + virDomainPtr dom = NULL; + int r = -1; + const char *libvirturi; + int readonly; + const char *iface; + struct guestfs_add_libvirt_dom_argv optargs2 = { .bitmask = 0 }; + + libvirturi = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK + ? optargs->libvirturi : NULL; + readonly = optargs->bitmask & GUESTFS_ADD_DOMAIN_READONLY_BITMASK + ? optargs->readonly : 0; + iface = optargs->bitmask & GUESTFS_ADD_DOMAIN_IFACE_BITMASK + ? optargs->iface : NULL; + + /* Connect to libvirt, find the domain. */ + conn = virConnectOpenReadOnly (libvirturi); + if (!conn) { + err = virGetLastError (); + error (g, _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); + goto cleanup; + } + + dom = virDomainLookupByName (conn, domain_name); + if (!dom) { + err = virConnGetLastError (conn); + error (g, _("no libvirt domain called '%s': %s"), + domain_name, err->message); + goto cleanup; + } + + if (readonly) { + optargs2.bitmask |= GUESTFS_ADD_LIBVIRT_DOM_READONLY_BITMASK; + optargs2.readonly = readonly; + } + if (iface) { + optargs2.bitmask |= GUESTFS_ADD_LIBVIRT_DOM_IFACE_BITMASK; + optargs2.iface = iface; + } + + r = guestfs__add_libvirt_dom (g, dom, &optargs2); + + cleanup: + if (dom) virDomainFree (dom); + if (conn) virConnectClose (conn); + + return r; +} + +int +guestfs__add_libvirt_dom (guestfs_h *g, virDomainPtr dom, + const struct guestfs_add_libvirt_dom_argv *optargs) +{ + int r = -1, nr_added = 0, i; + virErrorPtr err; + virConnectPtr conn = virDomainGetConnect (dom); + xmlDocPtr doc = NULL; + xmlXPathContextPtr xpathCtx = NULL; + xmlXPathObjectPtr xpathObj = NULL; + char *xml = NULL; + int readonly; + const char *iface; + int cmdline_pos; + + cmdline_pos = guestfs___checkpoint_cmdline (g); + + readonly = optargs->bitmask & GUESTFS_ADD_LIBVIRT_DOM_READONLY_BITMASK + ? optargs->readonly : 0; + iface = optargs->bitmask & GUESTFS_ADD_LIBVIRT_DOM_IFACE_BITMASK + ? optargs->iface : NULL; + + if (!readonly) { + virDomainInfo info; + if (virDomainGetInfo (dom, &info) == -1) { + err = virConnGetLastError (conn); + error (g, _("error getting domain info: %s"), err->message); + goto cleanup; + } + if (info.state != VIR_DOMAIN_SHUTOFF) { + error (g, _("error: domain is a live virtual machine.\nYou must use readonly access because write access to a running virtual machine\ncan cause disk corruption.")); + goto cleanup; + } + } + + /* Domain XML. */ + xml = virDomainGetXMLDesc (dom, 0); + + if (!xml) { + err = virConnGetLastError (conn); + error (g, _("error reading libvirt XML information: %s"), + err->message); + goto cleanup; + } + + /* Now the horrible task of parsing out the fields we need from the XML. + * http://www.xmlsoft.org/examples/xpath1.c + */ + doc = xmlParseMemory (xml, strlen (xml)); + if (doc == NULL) { + error (g, _("unable to parse XML information returned by libvirt")); + goto cleanup; + } + + xpathCtx = xmlXPathNewContext (doc); + if (xpathCtx == NULL) { + error (g, _("unable to create new XPath context")); + goto cleanup; + } + + /* This gives us a set of all the <disk> nodes. */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx); + if (xpathObj == NULL) { + error (g, _("unable to evaluate XPath expression")); + goto cleanup; + } + + xmlNodeSetPtr nodes = xpathObj->nodesetval; + for (i = 0; i < nodes->nodeNr; ++i) { + xmlXPathObjectPtr xpfilename; + xmlXPathObjectPtr xpformat; + + /* Change the context to the current <disk> node. + * DV advises to reset this before each search since older versions of + * libxml2 might overwrite it. + */ + xpathCtx->node = nodes->nodeTab[i]; + + /* Filename can be in <source dev=..> or <source file=..> attribute. */ + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + xpathCtx->node = nodes->nodeTab[i]; + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + continue; /* disk filename not found, skip this */ + } + } + + assert (xpfilename->nodesetval->nodeTab[0]); + assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE); + xmlAttrPtr attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0]; + char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1); + + /* Get the disk format (may not be set). */ + xpathCtx->node = nodes->nodeTab[i]; + xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx); + char *format = NULL; + if (xpformat != NULL && + xpformat->nodesetval && + xpformat->nodesetval->nodeNr > 0) { + assert (xpformat->nodesetval->nodeTab[0]); + assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE); + attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0]; + format = (char *) xmlNodeListGetString (doc, attr->children, 1); + } + + /* Add the disk, with optional format. */ + struct guestfs_add_drive_opts_argv optargs2 = { .bitmask = 0 }; + if (readonly) { + optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK; + optargs2.readonly = readonly; + } + if (format) { + optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK; + optargs2.format = format; + } + if (iface) { + optargs2.bitmask |= GUESTFS_ADD_DRIVE_OPTS_IFACE_BITMASK; + optargs2.iface = iface; + } + + int t = guestfs__add_drive_opts (g, filename, &optargs2); + + xmlFree (filename); + xmlFree (format); + xmlXPathFreeObject (xpfilename); + xmlXPathFreeObject (xpformat); + + if (t == -1) + goto cleanup; + + nr_added++; + } + + if (nr_added == 0) { + error (g, _("libvirt domain has no disks")); + goto cleanup; + } + + /* Successful. */ + r = nr_added; + + cleanup: + if (r == -1) guestfs___rollback_cmdline (g, cmdline_pos); + free (xml); + if (xpathObj) xmlXPathFreeObject (xpathObj); + if (xpathCtx) xmlXPathFreeContext (xpathCtx); + if (doc) xmlFreeDoc (doc); + + return r; +} + +#else /* no libvirt or libxml2 at compile time */ + +#define NOT_IMPL(r) \ + error (g, _("add-domain APIs not available since this version of libguestfs was compiled without libvirt or libxml2")); \ + return r + +int +guestfs__add_domain (guestfs_h *g, const char *dom, + const struct guestfs_add_domain_argv *optargs) +{ + NOT_IMPL(-1); +} + +int +guestfs__add_libvirt_dom (guestfs_h *g, virDomainPtr dom, + const struct guestfs_add_libvirt_dom_argv *optargs) +{ + NOT_IMPL(-1); +} + +#endif /* no libvirt or libxml2 at compile time */
Would it not be better to remove the public API symbol entirely if building without libvirt support. That way any application code which has a dependancy on a libvirt enabled libguestfs, will get a clear compile time error instead of appearing to build fine and then failing at some random point while running. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
On Wed, Nov 10, 2010 at 11:58:40AM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does
#include <libvirt/libvirt.h> #include <guestfs.h>
because they'll get a duplicated definition of virDomain from both headers.
I checked and it works fine both ways round. It would break if libvirt changed the definition, but you've boxed yourself into a corner there by documenting the current definition: http://libvirt.org/html/libvirt-libvirt.html
IMHO, this hack is a waste of time and it is better to just #include <libvirt/libvirt.h> from the header files instead.
The problem is libvirt would no longer be an optional dependency for programmers using libguestfs.
A build time dependancy on the libvirt client is really not a burden, when you consider that weight of the entire build chain you already have to pull in. Even with the build time dep, you can still have the dep be optional at runtime via your use of dlopen().
In some quarters libvirt is not welcome and libguestfs doesn't depend on libvirt being there, but will use it if available to provide enhanced features. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
On Wed, Nov 10, 2010 at 12:11:22PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 11:58:40AM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does
#include <libvirt/libvirt.h> #include <guestfs.h>
because they'll get a duplicated definition of virDomain from both headers.
I checked and it works fine both ways round. It would break if libvirt changed the definition, but you've boxed yourself into a corner there by documenting the current definition:
Surely the compiler should complain about duplicated typedefs, even if they are identical, because C doesn't allow duplicate typedefs within the same scope. http://david.tribble.com/text/cdiffs.htm#C99-typedefs eg $ cat st.c typedef struct foo foo; typedef struct foo foo; $ gcc -Wall -o st st.c st.c:3:20: error: redefinition of typedef ‘foo’ st.c:2:20: note: previous declaration of ‘foo’ was here
IMHO, this hack is a waste of time and it is better to just #include <libvirt/libvirt.h> from the header files instead.
The problem is libvirt would no longer be an optional dependency for programmers using libguestfs.
A build time dependancy on the libvirt client is really not a burden, when you consider that weight of the entire build chain you already have to pull in. Even with the build time dep, you can still have the dep be optional at runtime via your use of dlopen().
In some quarters libvirt is not welcome and libguestfs doesn't depend on libvirt being there, but will use it if available to provide enhanced features.
Then perhaps the integration glue should be a separate library, so libguestfs.so provides the core infrastructure, and then a libguest-libvirt.so provides higher level libvirt integration. This way people who don't want it can avoid any dep, but apps that do want libvirt can use the extra library and guarentee they'll have the functionality there, avoiding any nasty runtime surprises from libvirt being missing. Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
On Wed, Nov 10, 2010 at 12:26:03PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 12:11:22PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 11:58:40AM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does
#include <libvirt/libvirt.h> #include <guestfs.h>
because they'll get a duplicated definition of virDomain from both headers.
I checked and it works fine both ways round. It would break if libvirt changed the definition, but you've boxed yourself into a corner there by documenting the current definition:
Surely the compiler should complain about duplicated typedefs, even if they are identical, because C doesn't allow duplicate typedefs within the same scope.
http://david.tribble.com/text/cdiffs.htm#C99-typedefs
eg
$ cat st.c typedef struct foo foo; typedef struct foo foo; $ gcc -Wall -o st st.c st.c:3:20: error: redefinition of typedef ‘foo’ st.c:2:20: note: previous declaration of ‘foo’ was here
Apparently "scope" there means something a bit different. Try this program to see it definitely works (and also swap the order of the include and the typedef): ---------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <libvirt/libvirt.h> typedef struct _virDomain virDomain; typedef virDomain *virDomainPtr; int main () { printf ("hello\n"); exit (0); } ---------------------------------------------------------------------- gcc -Wall -o test test.c It doesn't need the #ifndef as suggested by that link.
Then perhaps the integration glue should be a separate library, so libguestfs.so provides the core infrastructure, and then a libguest-libvirt.so provides higher level libvirt integration. This way people who don't want it can avoid any dep, but apps that do want libvirt can use the extra library and guarentee they'll have the functionality there, avoiding any nasty runtime surprises from libvirt being missing.
For a single function using virDomainPtr, this is all quite a lot more complicated ... It should be possible to check availability through the guestfs_available API, although I didn't implement that yet (see XXX comments in the patches). 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
On Wed, Nov 10, 2010 at 12:47:55PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 12:26:03PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 12:11:22PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 11:58:40AM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does
#include <libvirt/libvirt.h> #include <guestfs.h>
because they'll get a duplicated definition of virDomain from both headers.
I checked and it works fine both ways round. It would break if libvirt changed the definition, but you've boxed yourself into a corner there by documenting the current definition:
Surely the compiler should complain about duplicated typedefs, even if they are identical, because C doesn't allow duplicate typedefs within the same scope.
http://david.tribble.com/text/cdiffs.htm#C99-typedefs
eg
$ cat st.c typedef struct foo foo; typedef struct foo foo; $ gcc -Wall -o st st.c st.c:3:20: error: redefinition of typedef ‘foo’ st.c:2:20: note: previous declaration of ‘foo’ was here
Apparently "scope" there means something a bit different. Try this program to see it definitely works (and also swap the order of the include and the typedef):
---------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h>
#include <libvirt/libvirt.h> typedef struct _virDomain virDomain; typedef virDomain *virDomainPtr;
int main () { printf ("hello\n"); exit (0); } ----------------------------------------------------------------------
gcc -Wall -o test test.c
It doesn't need the #ifndef as suggested by that link.
This rather surprises me. I wonder if this is just an accident of the current GCC warning checker impl, or actually the correct standards compliant behaviour. I've always been under the impression that all header files had to have all their typedefs/functions/etc protected by #ifndef FOO/#define FOO/...content.../#endif to prevent multiple declaration. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
On Wed, Nov 10, 2010 at 01:48:10PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 12:47:55PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 12:26:03PM +0000, Daniel P. Berrange wrote:
diff --git a/src/guestfs.h b/src/guestfs.h index 0f4f9fd..be7398a 100644 --- a/src/guestfs.h +++ b/src/guestfs.h @@ -79,6 +79,11 @@ extern void guestfs_set_private (guestfs_h *g, const char *key, void *data); extern void *guestfs_get_private (guestfs_h *g, const char *key);
/*--- Structures and actions ---*/ + +/* Hack to avoid needing to include <libvirt.h> */ +typedef struct _virDomain virDomain; +typedef virDomain *virDomainPtr; + #include <rpc/types.h> #include <rpc/xdr.h> #include <guestfs-structs.h>
Surely this will break any application whose code does
#include <libvirt/libvirt.h> #include <guestfs.h>
because they'll get a duplicated definition of virDomain from both headers.
I checked and it works fine both ways round. It would break if libvirt changed the definition, but you've boxed yourself into a corner there by documenting the current definition:
Surely the compiler should complain about duplicated typedefs, even if they are identical, because C doesn't allow duplicate typedefs within the same scope.
http://david.tribble.com/text/cdiffs.htm#C99-typedefs
eg
$ cat st.c typedef struct foo foo; typedef struct foo foo; $ gcc -Wall -o st st.c st.c:3:20: error: redefinition of typedef ‘foo’ st.c:2:20: note: previous declaration of ‘foo’ was here
Apparently "scope" there means something a bit different. Try this program to see it definitely works (and also swap the order of the include and the typedef):
---------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h>
#include <libvirt/libvirt.h> typedef struct _virDomain virDomain; typedef virDomain *virDomainPtr;
int main () { printf ("hello\n"); exit (0); } ----------------------------------------------------------------------
gcc -Wall -o test test.c
It doesn't need the #ifndef as suggested by that link.
This rather surprises me. I wonder if this is just an accident of the current GCC warning checker impl, or actually the correct standards compliant behaviour. I've always been under the impression that all header files had to have all their typedefs/functions/etc protected by #ifndef FOO/#define FOO/...content.../#endif to prevent multiple declaration.
Repeating from IRC for the benefit of archival... The reason why it appeared to work when using #include <libvirt/libvirt.h> is because libvirt.h was being resolved to /usr/include. This is one of GCC's nominated "system include directories". A huge set of compiler warnings are disabled for defintions from headers in a system include directory. So if both libguestfs & libvirt are installed in /usr or /usr/local, the warnings will be avoided. If they're installed anywhere else, then the duplicated typedefs will raise the compiler warnings. Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Yup, I'm glad we cleared that one up. It was confusing that gcc seemed to be ignoring all the C99 documentation ... There seem to be three possible ways forward: (1) Pass the libvirt XML. (2) Change virDomainPtr in the function prototype to 'void *'. (3) Change <libvirt.h> to defend the definition of virDomainPtr with a macro. Option (3) is a non-starter since we'd break with old versions of libvirt until everyone upgraded, but I think it's worth getting such a patch in there. Option (1) is a problem: until the lock manager API actually turns up in a released version of libvirt, this would mean we would have to turn off the check for the domain being shut down (or push it up to all users). (2) seems reasonable to me, at the loss of some type safety. I was wondering if we could do something like this: #ifndef __VIR_VIRLIB_H__ #define virDomainPtr void * #endif extern int guestfs_add_libvirt_dom (guestfs_h *g, virDomainPtr dom, ...); #ifndef __VIR_VIRLIB_H__ #undef virDomainPtr #endif which would at least be type-safe if users included <libvirt.h> before <guestfs.h>, and not be type-safe but still work if they are included the other way around. I can't believe we are the only people to have hit this problem ... Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
diff --git a/perl/typemap b/perl/typemap index d978e60..223aee1 100644 --- a/perl/typemap +++ b/perl/typemap @@ -3,6 +3,7 @@ char * T_PV const char * T_PV guestfs_h * O_OBJECT_guestfs_h int64_t T_IV +virDomainPtr O_OBJECT_domain
INPUT O_OBJECT_guestfs_h @@ -18,6 +19,16 @@ O_OBJECT_guestfs_h croak (\"${Package}::$func_name(): $var is not a blessed HV reference\"); }
+# This comes from the Sys::Virt bindings. +INPUT +O_OBJECT_domain + if (sv_isobject ($arg) && (SvTYPE (SvRV ($arg)) == SVt_PVMG)) + $var = ($type)SvIV ((SV*) SvRV ($arg)); + else { + warn(\"${Package}::$func_name() -- $var is not a blessed SV reference\"); + XSRETURN_UNDEF; + }
I haven't been considering this Sys::Virt type mapping to be part of the stable ABI/API, just an internal impl details. I'm wondering how other Perl XS modules allow extension, without exposing their internal typedef implementation detail.
diff --git a/src/virt.c b/src/virt.c new file mode 100644 index 0000000..5816f61 --- /dev/null +++ b/src/virt.c + +int +guestfs__add_domain (guestfs_h *g, const char *domain_name, + const struct guestfs_add_domain_argv *optargs) +{ + virErrorPtr err; + virConnectPtr conn = NULL; + virDomainPtr dom = NULL; + int r = -1; + const char *libvirturi; + int readonly; + const char *iface; + struct guestfs_add_libvirt_dom_argv optargs2 = { .bitmask = 0 }; + + libvirturi = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIBVIRTURI_BITMASK + ? optargs->libvirturi : NULL; + readonly = optargs->bitmask & GUESTFS_ADD_DOMAIN_READONLY_BITMASK + ? optargs->readonly : 0; + iface = optargs->bitmask & GUESTFS_ADD_DOMAIN_IFACE_BITMASK + ? optargs->iface : NULL; + + /* Connect to libvirt, find the domain. */ + conn = virConnectOpenReadOnly (libvirturi); + if (!conn) { + err = virGetLastError (); + error (g, _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); + goto cleanup; + } + + dom = virDomainLookupByName (conn, domain_name); + if (!dom) { + err = virConnGetLastError (conn);
NB, virConnGetLastError() is deprecated because it isn't threadsafe. Instead use virGetLastError() in all places.
+ error (g, _("no libvirt domain called '%s': %s"), + domain_name, err->message); + goto cleanup; + } + + if (readonly) { + optargs2.bitmask |= GUESTFS_ADD_LIBVIRT_DOM_READONLY_BITMASK; + optargs2.readonly = readonly; + } + if (iface) { + optargs2.bitmask |= GUESTFS_ADD_LIBVIRT_DOM_IFACE_BITMASK; + optargs2.iface = iface; + } + + r = guestfs__add_libvirt_dom (g, dom, &optargs2); + + cleanup: + if (dom) virDomainFree (dom); + if (conn) virConnectClose (conn); + + return r; +} + +int +guestfs__add_libvirt_dom (guestfs_h *g, virDomainPtr dom, + const struct guestfs_add_libvirt_dom_argv *optargs) +{ + int r = -1, nr_added = 0, i; + virErrorPtr err; + virConnectPtr conn = virDomainGetConnect (dom); + xmlDocPtr doc = NULL; + xmlXPathContextPtr xpathCtx = NULL; + xmlXPathObjectPtr xpathObj = NULL; + char *xml = NULL; + int readonly; + const char *iface; + int cmdline_pos; + + cmdline_pos = guestfs___checkpoint_cmdline (g); + + readonly = optargs->bitmask & GUESTFS_ADD_LIBVIRT_DOM_READONLY_BITMASK + ? optargs->readonly : 0; + iface = optargs->bitmask & GUESTFS_ADD_LIBVIRT_DOM_IFACE_BITMASK + ? optargs->iface : NULL; + + if (!readonly) { + virDomainInfo info; + if (virDomainGetInfo (dom, &info) == -1) { + err = virConnGetLastError (conn); + error (g, _("error getting domain info: %s"), err->message); + goto cleanup; + } + if (info.state != VIR_DOMAIN_SHUTOFF) { + error (g, _("error: domain is a live virtual machine.\nYou must use readonly access because write access to a running virtual machine\ncan cause disk corruption.")); + goto cleanup; + } + } + + /* Domain XML. */ + xml = virDomainGetXMLDesc (dom, 0); + + if (!xml) { + err = virConnGetLastError (conn); + error (g, _("error reading libvirt XML information: %s"), + err->message); + goto cleanup; + } + + /* Now the horrible task of parsing out the fields we need from the XML. + * http://www.xmlsoft.org/examples/xpath1.c + */ + doc = xmlParseMemory (xml, strlen (xml)); + if (doc == NULL) { + error (g, _("unable to parse XML information returned by libvirt")); + goto cleanup; + } + + xpathCtx = xmlXPathNewContext (doc); + if (xpathCtx == NULL) { + error (g, _("unable to create new XPath context")); + goto cleanup; + } + + /* This gives us a set of all the <disk> nodes. */ + xpathObj = xmlXPathEvalExpression (BAD_CAST "//devices/disk", xpathCtx); + if (xpathObj == NULL) { + error (g, _("unable to evaluate XPath expression")); + goto cleanup; + } + + xmlNodeSetPtr nodes = xpathObj->nodesetval; + for (i = 0; i < nodes->nodeNr; ++i) { + xmlXPathObjectPtr xpfilename; + xmlXPathObjectPtr xpformat; + + /* Change the context to the current <disk> node. + * DV advises to reset this before each search since older versions of + * libxml2 might overwrite it. + */ + xpathCtx->node = nodes->nodeTab[i]; + + /* Filename can be in <source dev=..> or <source file=..> attribute. */ + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + xpathCtx->node = nodes->nodeTab[i]; + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + continue; /* disk filename not found, skip this */ + } + }
Rather than checking for @dev, and then checking for @file, it is safer to fetch @type, and then if type=='file' use @file and if type=='block' use @dev. This protects against addition of future types, which may libguestfs may not be able to treat as simple paths on a local disk.
+ + assert (xpfilename->nodesetval->nodeTab[0]); + assert (xpfilename->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE); + xmlAttrPtr attr = (xmlAttrPtr) xpfilename->nodesetval->nodeTab[0]; + char *filename = (char *) xmlNodeListGetString (doc, attr->children, 1); + + /* Get the disk format (may not be set). */ + xpathCtx->node = nodes->nodeTab[i]; + xpformat = xmlXPathEvalExpression (BAD_CAST "./driver/@type", xpathCtx); + char *format = NULL; + if (xpformat != NULL && + xpformat->nodesetval && + xpformat->nodesetval->nodeNr > 0) { + assert (xpformat->nodesetval->nodeTab[0]); + assert (xpformat->nodesetval->nodeTab[0]->type == XML_ATTRIBUTE_NODE); + attr = (xmlAttrPtr) xpformat->nodesetval->nodeTab[0]; + format = (char *) xmlNodeListGetString (doc, attr->children, 1); + }
Regards, Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
On Wed, Nov 10, 2010 at 01:17:54PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
+# This comes from the Sys::Virt bindings. +INPUT +O_OBJECT_domain + if (sv_isobject ($arg) && (SvTYPE (SvRV ($arg)) == SVt_PVMG)) + $var = ($type)SvIV ((SV*) SvRV ($arg)); + else { + warn(\"${Package}::$func_name() -- $var is not a blessed SV reference\"); + XSRETURN_UNDEF; + }
I haven't been considering this Sys::Virt type mapping to be part of the stable ABI/API, just an internal impl details. I'm wondering how other Perl XS modules allow extension, without exposing their internal typedef implementation detail.
Yup, this is a general problem with providing integration for these pointers through any non-C language bindings. It could be alleviated by having Sys::Virt provide a little bit of C to perform the SV -> pointer conversion -- I think if it was in Sys/Virt/Virt.so then we would be able to link to it.
+ /* Connect to libvirt, find the domain. */ + conn = virConnectOpenReadOnly (libvirturi); + if (!conn) { + err = virGetLastError (); + error (g, _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); + goto cleanup; + } + + dom = virDomainLookupByName (conn, domain_name); + if (!dom) { + err = virConnGetLastError (conn);
NB, virConnGetLastError() is deprecated because it isn't threadsafe. Instead use virGetLastError() in all places.
Can I call this if I don't have a connection pointer? See first case above.
+ xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + xpathCtx->node = nodes->nodeTab[i]; + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + continue; /* disk filename not found, skip this */ + } + }
Rather than checking for @dev, and then checking for @file, it is safer to fetch @type, and then if type=='file' use @file and if type=='block' use @dev. This protects against addition of future types, which may libguestfs may not be able to treat as simple paths on a local disk.
OK I will change this. Was there a reason to split dev and file in the first place? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
On Wed, Nov 10, 2010 at 01:23:50PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 01:17:54PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
+# This comes from the Sys::Virt bindings. +INPUT +O_OBJECT_domain + if (sv_isobject ($arg) && (SvTYPE (SvRV ($arg)) == SVt_PVMG)) + $var = ($type)SvIV ((SV*) SvRV ($arg)); + else { + warn(\"${Package}::$func_name() -- $var is not a blessed SV reference\"); + XSRETURN_UNDEF; + }
I haven't been considering this Sys::Virt type mapping to be part of the stable ABI/API, just an internal impl details. I'm wondering how other Perl XS modules allow extension, without exposing their internal typedef implementation detail.
Yup, this is a general problem with providing integration for these pointers through any non-C language bindings. It could be alleviated by having Sys::Virt provide a little bit of C to perform the SV -> pointer conversion -- I think if it was in Sys/Virt/Virt.so then we would be able to link to it.
+ /* Connect to libvirt, find the domain. */ + conn = virConnectOpenReadOnly (libvirturi); + if (!conn) { + err = virGetLastError (); + error (g, _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); + goto cleanup; + } + + dom = virDomainLookupByName (conn, domain_name); + if (!dom) { + err = virConnGetLastError (conn);
NB, virConnGetLastError() is deprecated because it isn't threadsafe. Instead use virGetLastError() in all places.
Can I call this if I don't have a connection pointer? See first case above.
/** * virGetLastError: * * Provide a pointer to the last error caught at the library level * * The error object is kept in thread local storage, so separate * threads can safely access this concurrently. * */ /** * virConnGetLastError: * @conn: pointer to the hypervisor connection * * Provide a pointer to the last error caught on that connection * * This method is not protected against access from multiple * threads. In a multi-threaded application, always use the * global virGetLastError() API which is backed by thread * local storage. * * If the connection object was discovered to be invalid by * an API call, then the error will be reported against the * global error object. * * Since 0.6.0, all errors reported in the per-connection object * are also duplicated in the global error object. As such an * application can always use virGetLastError(). This method * remains for backwards compatability. */
+ xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@dev", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + xpathCtx->node = nodes->nodeTab[i]; + xpfilename = xmlXPathEvalExpression (BAD_CAST "./source/@file", xpathCtx); + if (xpfilename == NULL || + xpfilename->nodesetval == NULL || + xpfilename->nodesetval->nodeNr == 0) { + xmlXPathFreeObject (xpfilename); + continue; /* disk filename not found, skip this */ + } + }
Rather than checking for @dev, and then checking for @file, it is safer to fetch @type, and then if type=='file' use @file and if type=='block' use @dev. This protects against addition of future types, which may libguestfs may not be able to treat as simple paths on a local disk.
OK I will change this. Was there a reason to split dev and file in the first place?
I don't really know Regards. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
On Wed, Nov 10, 2010 at 01:34:29PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 01:23:50PM +0000, Richard W.M. Jones wrote:
On Wed, Nov 10, 2010 at 01:17:54PM +0000, Daniel P. Berrange wrote:
On Wed, Nov 10, 2010 at 11:46:31AM +0000, Richard W.M. Jones wrote:
+# This comes from the Sys::Virt bindings. +INPUT +O_OBJECT_domain + if (sv_isobject ($arg) && (SvTYPE (SvRV ($arg)) == SVt_PVMG)) + $var = ($type)SvIV ((SV*) SvRV ($arg)); + else { + warn(\"${Package}::$func_name() -- $var is not a blessed SV reference\"); + XSRETURN_UNDEF; + }
I haven't been considering this Sys::Virt type mapping to be part of the stable ABI/API, just an internal impl details. I'm wondering how other Perl XS modules allow extension, without exposing their internal typedef implementation detail.
Yup, this is a general problem with providing integration for these pointers through any non-C language bindings. It could be alleviated by having Sys::Virt provide a little bit of C to perform the SV -> pointer conversion -- I think if it was in Sys/Virt/Virt.so then we would be able to link to it.
+ /* Connect to libvirt, find the domain. */ + conn = virConnectOpenReadOnly (libvirturi); + if (!conn) { + err = virGetLastError (); + error (g, _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); + goto cleanup; + } + + dom = virDomainLookupByName (conn, domain_name); + if (!dom) { + err = virConnGetLastError (conn);
NB, virConnGetLastError() is deprecated because it isn't threadsafe. Instead use virGetLastError() in all places.
Can I call this if I don't have a connection pointer? See first case above. [...]
Right, I was reading your comment the wrong way round :-) Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.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
Updated with Dan's suggested code changes. This declares the function to use void* instead of virDomainPtr, bypassing the question of whether and what <libvirt.h> might define. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.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
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
-- 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
participants (3)
-
Daniel P. Berrange -
Matthew Booth -
Richard W.M. Jones