From 3d15f7e652340777514ff30c3cfc560a90b612ec Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 30 Jun 2009 11:16:22 +0100 Subject: [PATCH] Add 'set_memsize'/'get_memsize' calls. Allow the qemu memory size to be specified either by API calls or by setting the LIBGUESTFS_MEMSIZE environment variable. --- TODO | 5 ----- src/generator.ml | 29 +++++++++++++++++++++++++++++ src/guestfs.c | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/TODO b/TODO index 3938a98..bc00946 100644 --- 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). diff --git a/src/generator.ml b/src/generator.ml index da18ccf..13c1cfa 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -598,6 +598,35 @@ actions using the low-level API. For more information on states, see L."); + ("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. + +You can also change this by setting the environment +variable C before the handle is +created. + +For more information on the architecture of libguestfs, +see L."); + + ("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 was not called +on this handle, and if C was not set, +then this returns the compiled-in default value for memsize. + +For more information on the architecture of libguestfs, +see L."); + ] (* daemon_functions are any functions which cause some action diff --git a/src/guestfs.c b/src/guestfs.c index 016d803..5743a07 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -174,6 +174,8 @@ struct guestfs_h char *qemu; /* Qemu binary. */ char *append; /* Append to kernel command line. */ + int memsize; /* Size of RAM (megabytes). */ + char *last_error; /* Callbacks. */ @@ -246,6 +248,22 @@ guestfs_create (void) 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 @@ -597,6 +615,19 @@ guestfs_get_append (guestfs_h *g) 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) @@ -763,7 +794,7 @@ int 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; @@ -882,15 +913,6 @@ guestfs_launch (guestfs_h *g) 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; @@ -936,7 +958,7 @@ guestfs_launch (guestfs_h *g) 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); -- 1.8.3.1