Add 'set_memsize'/'get_memsize' calls.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 30 Jun 2009 10:16:22 +0000 (11:16 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Tue, 30 Jun 2009 12:10:44 +0000 (13:10 +0100)
Allow the qemu memory size to be specified either by API
calls or by setting the LIBGUESTFS_MEMSIZE environment
variable.

TODO
src/generator.ml
src/guestfs.c

diff --git a/TODO b/TODO
index 3938a98..bc00946 100644 (file)
--- a/TODO
+++ b/TODO
@@ -160,11 +160,6 @@ Allow swap space from the guest to be used.  Is it a good idea?
 
 ----------------------------------------------------------------------
 
 
 ----------------------------------------------------------------------
 
-Allow memsize to be configured (ie. guestfs_set_memsize etc)
-Also have an environment variable (LIBGUESTFS_MEMSIZE).
-
-----------------------------------------------------------------------
-
 Need a way to query a binary or library file for its architecture.
 Using objdump or readelf?
 What about non-ELF files (eg. Windows, BSD).
 Need a way to query a binary or library file for its architecture.
 Using objdump or readelf?
 What about non-ELF files (eg. Windows, BSD).
index da18ccf..13c1cfa 100755 (executable)
@@ -598,6 +598,35 @@ actions using the low-level API.
 
 For more information on states, see L<guestfs(3)>.");
 
 
 For more information on states, see L<guestfs(3)>.");
 
+  ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
+   [],
+   "set memory allocated to the qemu subprocess",
+   "\
+This sets the memory size in megabytes allocated to the
+qemu subprocess.  This only has any effect if called before
+C<guestfs_launch>.
+
+You can also change this by setting the environment
+variable C<LIBGUESTFS_MEMSIZE> before the handle is
+created.
+
+For more information on the architecture of libguestfs,
+see L<guestfs(3)>.");
+
+  ("get_memsize", (RInt "memsize", []), -1, [],
+   [],
+   "get memory allocated to the qemu subprocess",
+   "\
+This gets the memory size in megabytes allocated to the
+qemu subprocess.
+
+If C<guestfs_set_memsize> was not called
+on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
+then this returns the compiled-in default value for memsize.
+
+For more information on the architecture of libguestfs,
+see L<guestfs(3)>.");
+
 ]
 
 (* daemon_functions are any functions which cause some action
 ]
 
 (* daemon_functions are any functions which cause some action
index 016d803..5743a07 100644 (file)
@@ -174,6 +174,8 @@ struct guestfs_h
   char *qemu;                  /* Qemu binary. */
   char *append;                        /* Append to kernel command line. */
 
   char *qemu;                  /* Qemu binary. */
   char *append;                        /* Append to kernel command line. */
 
+  int memsize;                 /* Size of RAM (megabytes). */
+
   char *last_error;
 
   /* Callbacks. */
   char *last_error;
 
   /* Callbacks. */
@@ -246,6 +248,22 @@ guestfs_create (void)
     if (!g->append) goto error;
   }
 
     if (!g->append) goto error;
   }
 
+  /* Choose a suitable memory size.  Previously we tried to choose
+   * a minimal memory size, but this isn't really necessary since
+   * recent QEMU and KVM don't do anything nasty like locking
+   * memory into core any more.  Thus we can safely choose a
+   * large, generous amount of memory, and it'll just get swapped
+   * on smaller systems.
+   */
+  str = getenv ("LIBGUESTFS_MEMSIZE");
+  if (str) {
+    if (sscanf (str, "%d", &g->memsize) != 1 || g->memsize <= 256) {
+      fprintf (stderr, "libguestfs: non-numeric or too small value for LIBGUESTFS_MEMSIZE\n");
+      goto error;
+    }
+  } else
+    g->memsize = 500;
+
   g->main_loop = guestfs_get_default_main_loop ();
 
   /* Start with large serial numbers so they are easy to spot
   g->main_loop = guestfs_get_default_main_loop ();
 
   /* Start with large serial numbers so they are easy to spot
@@ -597,6 +615,19 @@ guestfs_get_append (guestfs_h *g)
   return g->append;
 }
 
   return g->append;
 }
 
+int
+guestfs_set_memsize (guestfs_h *g, int memsize)
+{
+  g->memsize = memsize;
+  return 0;
+}
+
+int
+guestfs_get_memsize (guestfs_h *g)
+{
+  return g->memsize;
+}
+
 /* Add a string to the current command line. */
 static void
 incr_cmdline_size (guestfs_h *g)
 /* Add a string to the current command line. */
 static void
 incr_cmdline_size (guestfs_h *g)
@@ -763,7 +794,7 @@ int
 guestfs_launch (guestfs_h *g)
 {
   static const char *dir_template = "/tmp/libguestfsXXXXXX";
 guestfs_launch (guestfs_h *g)
 {
   static const char *dir_template = "/tmp/libguestfsXXXXXX";
-  int r, i, pmore, memsize;
+  int r, i, pmore;
   size_t len;
   int wfd[2], rfd[2];
   int tries;
   size_t len;
   int wfd[2], rfd[2];
   int tries;
@@ -882,15 +913,6 @@ guestfs_launch (guestfs_h *g)
     goto cleanup0;
   }
 
     goto cleanup0;
   }
 
-  /* Choose a suitable memory size.  Previously we tried to choose
-   * a minimal memory size, but this isn't really necessary since
-   * recent QEMU and KVM don't do anything nasty like locking
-   * memory into core any more.  Thus we can safely choose a
-   * large, generous amount of memory, and it'll just get swapped
-   * on smaller systems.
-   */
-  memsize = 500;
-
   /* Get qemu help text and version. */
   if (test_qemu (g) == -1)
     goto cleanup0;
   /* Get qemu help text and version. */
   if (test_qemu (g) == -1)
     goto cleanup0;
@@ -936,7 +958,7 @@ guestfs_launch (guestfs_h *g)
              g->verbose ? " guestfs_verbose=1" : "",
              g->append ? " " : "", g->append ? g->append : "");
 
              g->verbose ? " guestfs_verbose=1" : "",
              g->append ? " " : "", g->append ? g->append : "");
 
-    snprintf (memsize_str, sizeof memsize_str, "%d", memsize);
+    snprintf (memsize_str, sizeof memsize_str, "%d", g->memsize);
 
     add_cmdline (g, "-m");
     add_cmdline (g, memsize_str);
 
     add_cmdline (g, "-m");
     add_cmdline (g, memsize_str);