add-domain: Add allowuuid flag to allow UUIDs to be used for names.
[libguestfs.git] / src / virt.c
index 4b66cc8..58cb999 100644 (file)
@@ -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)
@@ -76,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 };
 
@@ -87,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"));
@@ -102,7 +111,20 @@ guestfs__add_domain (guestfs_h *g, const char *domain_name,
     goto cleanup;
   }
 
-  dom = virDomainLookupByName (conn, domain_name);
+  /* 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);
+
+  /* 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"),
@@ -305,7 +327,7 @@ static int
 guestfs___add_libvirt_dom (guestfs_h *g, virDomainPtr dom,
                            const struct guestfs___add_libvirt_dom_argv *optargs)
 {
-  int cmdline_pos;
+  size_t cmdline_pos;
   int r;
   int readonly;
   const char *iface;
@@ -467,7 +489,8 @@ connect_live (guestfs_h *g, virDomainPtr dom)
   }
 
   if (path == NULL) {
-    error (g, _("this guest has no libvirt <channel> definition for guestfsd"));
+    error (g, _("this guest has no libvirt <channel> definition for guestfsd\n"
+                "See ATTACHING TO RUNNING DAEMONS in guestfs(3) for further information."));
     goto cleanup;
   }