[Libguestfs] [PATCH 2/8 v2 DISCUSSION ONLY] New APIs: set-attach-method, get-attach-method.

Richard W.M. Jones rjones at redhat.com
Fri Jan 28 19:37:24 UTC 2011


-- 
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
-------------- next part --------------
>From d5411585bf02cb84136a523bb9a06db013fc3e24 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Thu, 27 Jan 2011 11:20:43 +0000
Subject: [PATCH 2/8] New APIs: set-attach-method, get-attach-method.

These allow you to get and set the attach method.  The format
is one of:

* appliance
* unix:<path>

It's stored broken out into an enum and a string in the handle.
---
 generator/generator_actions.ml |   30 ++++++++++++++++++++++++++
 src/guestfs-internal.h         |    6 +++++
 src/guestfs.c                  |   45 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index 0ae8790..a2e7467 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -1368,6 +1368,36 @@ part of a set.
 
 Please read L<guestfs(3)/INSPECTION> for more details.");
 
+  ("set_attach_method", (RErr, [String "attachmethod"], []), -1, [FishAlias "attach-method"],
+   [],
+   "set the attach method",
+   "\
+Set the method that libguestfs uses to connect to the back end
+guestfsd daemon.  Possible methods are:
+
+=over 4
+
+=item C<appliance>
+
+Launch an appliance and connect to it.  This is the ordinary method
+and the default.
+
+=item C<unix:I<path>>
+
+Connect to the Unix domain socket I<path>.
+
+This method lets you connect to an existing daemon or (using
+virtio-serial) to a live guest.
+
+=back");
+
+  ("get_attach_method", (RString "attachmethod", [], []), -1, [],
+   [InitNone, Always, TestOutput (
+      [["get_attach_method"]], "appliance")],
+   "get the attach method",
+   "\
+Return the current attach method.  See C<guestfs_set_attach_method>.");
+
 ]
 
 (* daemon_functions are any functions which cause some action
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 194c892..0eb395b 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -84,6 +84,9 @@
 /* GuestFS handle and connection. */
 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
 
+/* Attach method. */
+enum attach_method { ATTACH_METHOD_APPLIANCE = 0, ATTACH_METHOD_UNIX };
+
 struct guestfs_h
 {
   struct guestfs_h *next;	/* Linked list of open handles. */
@@ -116,6 +119,9 @@ struct guestfs_h
   char *qemu;			/* Qemu binary. */
   char *append;			/* Append to kernel command line. */
 
+  enum attach_method attach_method;
+  char *attach_method_arg;
+
   int memsize;			/* Size of RAM (megabytes). */
 
   int selinux;                  /* selinux enabled? */
diff --git a/src/guestfs.c b/src/guestfs.c
index 79fc5bf..8b7ab4d 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -645,6 +645,51 @@ guestfs__get_network (guestfs_h *g)
   return g->enable_network;
 }
 
+int
+guestfs__set_attach_method (guestfs_h *g, const char *method)
+{
+  if (STREQ (method, "appliance")) {
+    g->attach_method = ATTACH_METHOD_APPLIANCE;
+    free (g->attach_method_arg);
+    g->attach_method_arg = NULL;
+  }
+  else if (STRPREFIX (method, "unix:") && strlen (method) > 5) {
+    g->attach_method = ATTACH_METHOD_UNIX;
+    free (g->attach_method_arg);
+    g->attach_method_arg = safe_strdup (g, method + 5);
+    /* Note that we don't check the path exists until launch is called. */
+  }
+  else {
+    error (g, "invalid attach method: %s", method);
+    return -1;
+  }
+
+  return 0;
+}
+
+char *
+guestfs__get_attach_method (guestfs_h *g)
+{
+  char *ret;
+
+  switch (g->attach_method) {
+  case ATTACH_METHOD_APPLIANCE:
+    ret = safe_strdup (g, "appliance");
+    break;
+
+  case ATTACH_METHOD_UNIX:
+    ret = safe_malloc (g, strlen (g->attach_method_arg) + 5 + 1);
+    strcpy (ret, "unix:");
+    strcat (ret, g->attach_method_arg);
+    break;
+
+  default: /* keep GCC happy - this is not reached */
+    abort ();
+  }
+
+  return ret;
+}
+
 void
 guestfs_set_log_message_callback (guestfs_h *g,
                                   guestfs_log_message_cb cb, void *opaque)
-- 
1.7.3.5



More information about the Libguestfs mailing list