[Libguestfs] [PATCH 0/3] Add support for guestfish -d UUID (etc)

Richard W.M. Jones rjones at redhat.com
Fri May 6 20:59:16 UTC 2011


This patch series lets you use UUIDs instead of names for any
of the virt tools that support the -d option.

It's a bit tedious to send email through my current internet
connection, so I'm attaching all 3 patches to this one email.

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/
-------------- next part --------------
>From a4c28b4ed1c5a102c8de2a7425568eb504d05c34 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 6 May 2011 12:32:59 -0400
Subject: [PATCH 1/3] add-domain: Suppress libvirt errors on stderr.

Install an error handler on the libvirt error connection so that
errors are not printed on stderr (instead they go up through the usual
libguestfs error mechanism).

Unfortunately this doesn't suppress initial connection error messages
to stderr.  I cannot see how to do this without affecting the global
libvirt error handler, which is not acceptable for a library to be
doing.
---
 src/virt.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/src/virt.c b/src/virt.c
index aa2c022..cd48888 100644
--- a/src/virt.c
+++ b/src/virt.c
@@ -53,6 +53,12 @@ init_libxml2 (void)
   LIBXML_TEST_VERSION;
 }
 
+static void
+ignore_errors (void *ignore, virErrorPtr ignore2)
+{
+  /* empty */
+}
+
 struct guestfs___add_libvirt_dom_argv {
   uint64_t bitmask;
 #define GUESTFS___ADD_LIBVIRT_DOM_READONLY_BITMASK (UINT64_C(1)<<0)
@@ -102,6 +108,12 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
     goto cleanup;
   }
 
+  /* Suppress default behaviour of printing errors to stderr.  Note
+   * you can't set this to NULL to ignore errors; setting it to NULL
+   * restores the default error handler ...
+   */
+  virConnSetErrorFunc (conn, NULL, ignore_errors);
+
   dom = virDomainLookupByName (conn, domain_name);
   if (!dom) {
     err = virGetLastError ();
-- 
1.7.4.4

-------------- next part --------------
>From be3b028d7f2fadd8d2107a419393511e4510d0a4 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 6 May 2011 12:21:01 -0400
Subject: [PATCH 2/3] add-domain: Add allowuuid flag to allow UUIDs to be used
 for names.

This makes a backwards-compatible change to the add-domain API.  If
the optional allowuuid flag is true then UUIDs can be used instead of
names in the domain name parameter.
---
 generator/generator_actions.ml |    7 ++++++-
 src/virt.c                     |   12 +++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index abf3475..5db24af 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -1096,7 +1096,7 @@ Please read L<guestfs(3)/INSPECTION> for more details.");
 This returns the internal QEMU command line.  'debug' commands are
 not part of the formal API and can be removed or changed at any time.");
 
-  ("add_domain", (RInt "nrdisks", [String "dom"], [String "libvirturi"; Bool "readonly"; String "iface"; Bool "live"]), -1, [FishAlias "domain"],
+  ("add_domain", (RInt "nrdisks", [String "dom"], [String "libvirturi"; Bool "readonly"; String "iface"; Bool "live"; Bool "allowuuid"]), -1, [FishAlias "domain"],
    [],
    "add the disk(s) from a named libvirt domain",
    "\
@@ -1130,6 +1130,11 @@ XML definition.  The default (if the flag is omitted) is never
 to try.  See L<guestfs(3)/ATTACHING TO RUNNING DAEMONS> for more
 information.
 
+If the C<allowuuid> flag is true (default is false) then a UUID
+I<may> be passed instead of the domain name.  The C<dom> string is
+treated as a UUID first and looked up, and if that lookup fails
+then we treat C<dom> as a name as usual.
+
 The other optional parameters are passed directly through to
 C<guestfs_add_drive_opts>.");
 
diff --git a/src/virt.c b/src/virt.c
index cd48888..58cb999 100644
--- a/src/virt.c
+++ b/src/virt.c
@@ -82,6 +82,7 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
   const char *libvirturi;
   int readonly;
   int live;
+  int allowuuid;
   const char *iface;
   struct guestfs___add_libvirt_dom_argv optargs2 = { .bitmask = 0 };
 
@@ -93,6 +94,8 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
           ? optargs->iface : NULL;
   live = optargs->bitmask & GUESTFS_ADD_DOMAIN_LIVE_BITMASK
          ? optargs->live : 0;
+  allowuuid = optargs->bitmask & GUESTFS_ADD_DOMAIN_ALLOWUUID_BITMASK
+            ? optargs->allowuuid : 0;
 
   if (live && readonly) {
     error (g, _("you cannot set both live and readonly flags"));
@@ -114,7 +117,14 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
    */
   virConnSetErrorFunc (conn, NULL, ignore_errors);
 
-  dom = virDomainLookupByName (conn, domain_name);
+  /* Try UUID first. */
+  if (allowuuid)
+    dom = virDomainLookupByUUIDString (conn, domain_name);
+
+  /* Try ordinary domain name. */
+  if (!dom)
+    dom = virDomainLookupByName (conn, domain_name);
+
   if (!dom) {
     err = virGetLastError ();
     error (g, _("no libvirt domain called '%s': %s"),
-- 
1.7.4.4

-------------- next part --------------
>From 87fb6d852db04d0d707e6587d4579d1cf2bd05ef Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 6 May 2011 12:23:00 -0400
Subject: [PATCH 3/3] fish: Allow -d UUID (specify libvirt domains by UUID).

This applies in all the commands which use the common C option parsing
code, ie:

* guestfish
* guestmount
* virt-cat
* virt-df
* virt-filesystems
* virt-inspector
* virt-ls
* virt-rescue
---
 TODO                         |    5 -----
 cat/virt-cat.pod             |    3 ++-
 cat/virt-filesystems.pod     |    3 ++-
 cat/virt-ls.pod              |    3 ++-
 df/virt-df.pod               |    3 ++-
 fish/guestfish.pod           |    2 ++
 fish/virt.c                  |    3 +++
 fuse/guestmount.pod          |    2 ++
 inspector/virt-inspector.pod |    3 ++-
 rescue/virt-rescue.pod       |    3 ++-
 10 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/TODO b/TODO
index 9364e12..6579629 100644
--- a/TODO
+++ b/TODO
@@ -432,11 +432,6 @@ guestfish drive letters
 There should be an option to mount all Windows drives as separate
 paths, like C: => /c/, D: => /d/ etc.
 
-Select machines by UUID
------------------------
-
-guestfish -u UUID <or> guestfish -d UUID
-
 More inspection features
 ------------------------
 
diff --git a/cat/virt-cat.pod b/cat/virt-cat.pod
index 4ec9a0b..af8b8f4 100755
--- a/cat/virt-cat.pod
+++ b/cat/virt-cat.pod
@@ -92,7 +92,8 @@ not used at all.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--echo-keys>
 
diff --git a/cat/virt-filesystems.pod b/cat/virt-filesystems.pod
index fac139b..3ba925f 100755
--- a/cat/virt-filesystems.pod
+++ b/cat/virt-filesystems.pod
@@ -142,7 +142,8 @@ read L</NOTE ABOUT CSV FORMAT> below.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--echo-keys>
 
diff --git a/cat/virt-ls.pod b/cat/virt-ls.pod
index 1ba7417..dc3093a 100755
--- a/cat/virt-ls.pod
+++ b/cat/virt-ls.pod
@@ -92,7 +92,8 @@ not used at all.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--echo-keys>
 
diff --git a/df/virt-df.pod b/df/virt-df.pod
index d05596a..e9f416c 100755
--- a/df/virt-df.pod
+++ b/df/virt-df.pod
@@ -89,7 +89,8 @@ not used at all.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--format=raw|qcow2|..>
 
diff --git a/fish/guestfish.pod b/fish/guestfish.pod
index 94deb9c..77bf0ca 100644
--- a/fish/guestfish.pod
+++ b/fish/guestfish.pod
@@ -199,6 +199,8 @@ Add disks from the named libvirt domain.  If the I<--ro> option is
 also used, then any libvirt domain can be used.  However in write
 mode, only libvirt domains which are shut down can be named here.
 
+Domain UUIDs can be used instead of names.
+
 Using this flag is mostly equivalent to using the C<add-domain> command,
 with C<readonly:true> if the I<--ro> flag was given, and
 with C<format:...> if the I<--format:...> flag was given.
diff --git a/fish/virt.c b/fish/virt.c
index b14cee2..486f098 100644
--- a/fish/virt.c
+++ b/fish/virt.c
@@ -48,5 +48,8 @@ add_libvirt_drives (const char *guest)
     optargs.live = 1;
   }
 
+  optargs.bitmask |= GUESTFS_ADD_DOMAIN_ALLOWUUID_BITMASK;
+  optargs.allowuuid = 1;
+
   return guestfs_add_domain_argv (g, guest, &optargs);
 }
diff --git a/fuse/guestmount.pod b/fuse/guestmount.pod
index 9fbefcf..6a2f39b 100644
--- a/fuse/guestmount.pod
+++ b/fuse/guestmount.pod
@@ -93,6 +93,8 @@ Add disks from the named libvirt domain.  If the I<--ro> option is
 also used, then any libvirt domain can be used.  However in write
 mode, only libvirt domains which are shut down can be named here.
 
+Domain UUIDs can be used instead of names.
+
 =item B<--dir-cache-timeout N>
 
 Set the readdir cache timeout to I<N> seconds, the default being 60
diff --git a/inspector/virt-inspector.pod b/inspector/virt-inspector.pod
index 225e3f6..2192b39 100755
--- a/inspector/virt-inspector.pod
+++ b/inspector/virt-inspector.pod
@@ -86,7 +86,8 @@ then libvirt is not used at all.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--echo-keys>
 
diff --git a/rescue/virt-rescue.pod b/rescue/virt-rescue.pod
index f56acd0..81a24cf 100755
--- a/rescue/virt-rescue.pod
+++ b/rescue/virt-rescue.pod
@@ -118,7 +118,8 @@ not used at all.
 
 =item B<--domain> guest
 
-Add all the disks from the named libvirt guest.
+Add all the disks from the named libvirt guest.  Domain UUIDs can be
+used instead of names.
 
 =item B<--format=raw|qcow2|..>
 
-- 
1.7.4.4



More information about the Libguestfs mailing list