[Libguestfs] [PATCH 1/2] disk-create: Allow preallocation off/metadata/full.

Richard W.M. Jones rjones at redhat.com
Wed Aug 12 15:57:55 UTC 2015


For raw, this allows "off" as a synonym for "sparse" (to make it
consistent with qcow2).

For qcow2, this allows "sparse" as a synonym for "off".

It also adds qcow2 "full" preallocation, which is actually mapped to
the qemu option "falloc" (see arguments about this on the qemu-devel
mailing list, which we lost).

This also updates the test.
---
 generator/actions.ml             | 13 +++++++------
 src/create.c                     | 16 ++++++++++++----
 tests/create/test-disk-create.sh |  3 +++
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/generator/actions.ml b/generator/actions.ml
index d0d6a21..1c22f9f 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -3174,13 +3174,14 @@ The other optional parameters are:
 
 =item C<preallocation>
 
-If format is C<raw>, then this can be either C<sparse> or C<full>
-to create a sparse or fully allocated file respectively.  The default
-is C<sparse>.
+If format is C<raw>, then this can be either C<off> (or C<sparse>)
+or C<full> to create a sparse or fully allocated file respectively.
+The default is C<off>.
 
-If format is C<qcow2>, then this can be either C<off> or
-C<metadata>.  Preallocating metadata can be faster when doing lots
-of writes, but uses more space.  The default is C<off>.
+If format is C<qcow2>, then this can be C<off> (or C<sparse>),
+C<metadata> or C<full>.  Preallocating metadata can be faster
+when doing lots of writes, but uses more space.
+The default is C<off>.
 
 =item C<compat>
 
diff --git a/src/create.c b/src/create.c
index 46bee7c..e9bac95 100644
--- a/src/create.c
+++ b/src/create.c
@@ -1,5 +1,5 @@
 /* libguestfs
- * Copyright (C) 2012 Red Hat Inc.
+ * Copyright (C) 2012-2015 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -140,7 +140,8 @@ disk_create_raw (guestfs_h *g, const char *filename, int64_t size,
     return -1;
   }
   if (optargs->bitmask & GUESTFS_DISK_CREATE_PREALLOCATION_BITMASK) {
-    if (STREQ (optargs->preallocation, "sparse"))
+    if (STREQ (optargs->preallocation, "off") ||
+        STREQ (optargs->preallocation, "sparse"))
       allocated = 0;
     else if (STREQ (optargs->preallocation, "full"))
       allocated = 1;
@@ -273,8 +274,15 @@ disk_create_qcow2 (guestfs_h *g, const char *orig_filename, int64_t size,
     }
   }
   if (optargs->bitmask & GUESTFS_DISK_CREATE_PREALLOCATION_BITMASK) {
-    preallocation = optargs->preallocation;
-    if (STRNEQ (preallocation, "off") && STRNEQ (preallocation, "metadata")) {
+    if (STREQ (optargs->preallocation, "off") ||
+        STREQ (optargs->preallocation, "sparse"))
+      preallocation = "off";
+    else if (STREQ (optargs->preallocation, "metadata"))
+      preallocation = "metadata";
+    else if (STREQ (optargs->preallocation, "full"))
+      /* Ugh: https://lists.gnu.org/archive/html/qemu-devel/2014-08/msg03863.html */
+      preallocation = "falloc";
+    else {
       error (g, _("invalid value for preallocation parameter '%s'"),
              preallocation);
       return -1;
diff --git a/tests/create/test-disk-create.sh b/tests/create/test-disk-create.sh
index 93dc706..e18d6da 100755
--- a/tests/create/test-disk-create.sh
+++ b/tests/create/test-disk-create.sh
@@ -27,11 +27,14 @@ rm -f disk*.img file:*.img
 
 guestfish <<EOF
   disk-create disk1.img  raw   256K
+  disk-create disk2.img  raw   256K preallocation:off
   disk-create disk2.img  raw   256K preallocation:sparse
   disk-create disk3.img  raw   256K preallocation:full
   disk-create disk4.img  qcow2 256K
   disk-create disk5.img  qcow2 256K preallocation:off
+  disk-create disk5.img  qcow2 256K preallocation:sparse
   disk-create disk6.img  qcow2 256K preallocation:metadata
+  disk-create disk6.img  qcow2 256K preallocation:full
   disk-create disk7.img  qcow2 256K compat:1.1
   disk-create disk8.img  qcow2 256K clustersize:128K
   disk-create disk9.img  qcow2 -1   backingfile:disk1.img compat:1.1
-- 
2.5.0




More information about the Libguestfs mailing list