Skip to contentSkip to conversation

qemu-img: Allow source cache mode specification

PATCH Applied Max Reitz 3 participants 7 messages Jul 22, 2014 – Aug 22, 2014

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v2 cover qemu-img: Allow source cache mode specification

Currently, qemu-img does not allow setting the cache mode for source images. However, it reads images generally only once, therefore a full writeback cache unnecessarily clutters the host cache. In case the user finds this undesirable, there has to be a way of disabling that cache. This series first introduces such a way for check, compare, convert and rebase and then (while at it) adds a cache mode switch to amend (which was missing so far).

Peter already tried to add a BDRV_O_SEQUENTIAL mode to qemu-img convert. If we want to add such a mode (which in my opinion would be the logical consequence of this series), we may want to make it just another cache mode which can be selected by the user.

v2:
- Patch 1:
- Remove superfluous space in qemu-img-cmds.hx [Eric]
- Reorder all getopt() optstrings which are touched by this patch anyway according to the subsequent switch [Eric (implicitly)]
- Patch 2:
- Reorder img_amend()'s optstring [Eric]

Max Reitz (2):
qemu-img: Allow source cache mode specification
qemu-img: Allow cache mode specification for amend

 qemu-img-cmds.hx | 20 ++++++------
 qemu-img.c       | 97 ++++++++++++++++++++++++++++++++++++++++++++------------
 qemu-img.texi    | 16 +++++++---
 3 files changed, 98 insertions(+), 35 deletions(-)
Signature · 1 line
2.0.1
author
#3 lore raw

PATCH v2 2/2 qemu-img: Allow cache mode specification for amend

qemu-img amend may extensively modify the target image, depending on the options to be amended (e.g. conversion to qcow2 compat level 0.10 from 1.1 for an image with many unallocated zero clusters). Therefore it makes sense to allow the user to specify the cache mode to be used.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 19 +++++++++++++++----
 qemu-img.texi    |  2 +-
 3 files changed, 18 insertions(+), 7 deletions(-)
qemu-img-cmds.hx +2 -2
index 48df0b8..55aec6b 100644
@@ -70,8 +70,8 @@ STEXI
 ETEXI
 
 DEF("amend", img_amend,
-    "amend [-q] [-f fmt] -o options filename")
+    "amend [-q] [-f fmt] [-t cache] -o options filename")
 STEXI
-@item amend [-q] [-f @var{fmt}] -o @var{options} @var{filename}
+@item amend [-q] [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename}
 @end table
 ETEXI
qemu-img.c +15 -4
index 9f3909c..ef74ae4 100644
@@ -2738,12 +2738,14 @@ static int img_amend(int argc, char **argv)
     char *options = NULL;
     QemuOptsList *create_opts = NULL;
     QemuOpts *opts = NULL;
-    const char *fmt = NULL, *filename;
+    const char *fmt = NULL, *filename, *cache;
+    int flags;
     bool quiet = false;
     BlockDriverState *bs = NULL;
 
+    cache = BDRV_DEFAULT_CACHE;
     for (;;) {
-        c = getopt(argc, argv, "hqf:o:");
+        c = getopt(argc, argv, "ho:f:t:q");
         if (c == -1) {
             break;
         }
@@ -2770,6 +2772,9 @@ static int img_amend(int argc, char **argv)
             case 'f':
                 fmt = optarg;
                 break;
+            case 't':
+                cache = optarg;
+                break;
             case 'q':
                 quiet = true;
                 break;
@@ -2792,8 +2797,14 @@ static int img_amend(int argc, char **argv)
         error_exit("Expecting one image file name");
     }
 
-    bs = bdrv_new_open("image", filename, fmt,
-                       BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
+    flags = BDRV_O_FLAGS | BDRV_O_RDWR;
+    ret = bdrv_parse_cache_flags(cache, &flags);
+    if (ret < 0) {
+        error_report("Invalid cache option: %s", cache);
+        goto out;
+    }
+
+    bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
     if (!bs) {
         error_report("Could not open image '%s'", filename);
         ret = -1;
qemu-img.texi +1 -1
index 688b28d..cb68948 100644
@@ -397,7 +397,7 @@ After using this command to grow a disk image, you must use file system and
 partitioning tools inside the VM to actually begin using the new space on the
 device.
 
-@item amend [-f @var{fmt}] -o @var{options} @var{filename}
+@item amend [-f @var{fmt}] [-t @var{cache}] -o @var{options} @var{filename}
 
 Amends the image format specific @var{options} for the image file
 @var{filename}. Not all file formats support this operation.
Signature · 1 line
2.0.1
author
#1 lore raw

PATCH v2 1/2 qemu-img: Allow source cache mode specification

In reply to Max Reitz

Many qemu-img subcommands only read the source file(s) once. For these use cases, a full write-back cache is unnecessary and mainly clutters host cache memory. Though this is generally no concern as cache memory is freely available and can be scaled by the host OS, it may become a concern with thin provisioning.

For these cases, it makes sense to allow users to freely specify the source cache mode (e.g. use no cache at all).

This commit adds a new switch (-T) for the qemu-img subcommands check, compare, convert and rebase to specify the cache to be used for source images (the backing file in case of rebase).

---
 qemu-img-cmds.hx | 16 ++++++------
 qemu-img.c       | 78 ++++++++++++++++++++++++++++++++++++++++++++------------
 qemu-img.texi    | 14 +++++++---
 3 files changed, 80 insertions(+), 28 deletions(-)
qemu-img-cmds.hx +8 -8
index d029609..48df0b8 100644
@@ -10,9 +10,9 @@ STEXI
 ETEXI
 
 DEF("check", img_check,
-    "check [-q] [-f fmt] [--output=ofmt]  [-r [leaks | all]] filename")
+    "check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename")
 STEXI
-@item check [-q] [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] @var{filename}
+@item check [-q] [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] [-T @var{src_cache}] @var{filename}
 ETEXI
 
 DEF("create", img_create,
@@ -28,15 +28,15 @@ STEXI
 ETEXI
 
 DEF("compare", img_compare,
-    "compare [-f fmt] [-F fmt] [-p] [-q] [-s] filename1 filename2")
+    "compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2")
 STEXI
-@item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-q] [-s] @var{filename1} @var{filename2}
+@item compare [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-q] [-s] @var{filename1} @var{filename2}
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename")
+    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,
@@ -58,9 +58,9 @@ STEXI
 ETEXI
 
 DEF("rebase", img_rebase,
-    "rebase [-q] [-f fmt] [-t cache] [-p] [-u] -b backing_file [-F backing_fmt] filename")
+    "rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename")
 STEXI
-@item rebase [-q] [-f @var{fmt}] [-t @var{cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename}
+@item rebase [-q] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename}
 ETEXI
 
 DEF("resize", img_resize,
qemu-img.c +62 -16

227 lines not on this page · raw message

qemu-img.texi +10 -4
index 514be90..688b28d 100644
@@ -72,6 +72,9 @@ down to the nearest 512 bytes. You may use the common size suffixes like
 specifies the cache mode that should be used with the (destination) file. See
 the documentation of the emulator's @code{-drive cache=...} option for allowed
 values.
+@item -T @var{src_cache}
+in contrast specifies the cache mode that should be used with the source
+file(s).
 @end table
 
 Parameters to snapshot subcommand:
@@ -113,7 +116,7 @@ Skip the creation of the target volume
 Command description:
 
 @table @option
-@item check [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] @var{filename}
+@item check [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] [-T @var{src_cache}] @var{filename}
 
 Perform a consistency check on the disk image @var{filename}. The command can
 output in the format @var{ofmt} which is either @code{human} or @code{json}.
@@ -172,7 +175,7 @@ the backing file, the backing file will not be truncated.  If you want the
 backing file to match the size of the smaller snapshot, you can safely truncate
 it yourself once the commit operation successfully completes.
 
-@item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-s] [-q] @var{filename1} @var{filename2}
+@item compare [-f @var{fmt}] [-F @var{fmt}] [-T @var{src_cache}] [-p] [-s] [-q] @var{filename1} @var{filename2}
 
 Check if two images have the same content. You can compare images with
 different format or settings.
@@ -213,7 +216,7 @@ Error on reading data
 
 @end table
 
-@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 
 Convert the disk image @var{filename} or a snapshot @var{snapshot_param}(@var{snapshot_id_or_name} is deprecated)
 to disk image @var{output_filename} using format @var{output_fmt}. It can be optionally compressed (@code{-c}
@@ -325,7 +328,7 @@ source code.
 
 List, apply, create or delete snapshots in image @var{filename}.
 
-@item rebase [-f @var{fmt}] [-t @var{cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename}
+@item rebase [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-p] [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename}
 
 Changes the backing file of an image. Only the formats @code{qcow2} and
 @code{qed} support changing the backing file.
@@ -336,6 +339,9 @@ The backing file is changed to @var{backing_file} and (if the image format of
 string), then the image is rebased onto no backing file (i.e. it will exist
 independently of any backing file).
 
+@var{cache} specifies the cache mode to be used for @var{filename}, whereas
+@var{src_cache} specifies the cache mode for reading the new backing file.
+
 There are two different modes in which @code{rebase} can operate:
 @table @option
 @item Safe mode
Signature · 1 line
2.0.1
Reviewed-by on v2 1/2
#2 lore raw
Quoted 13 lines · Max Reitz

Many qemu-img subcommands only read the source file(s) once. For these use cases, a full write-back cache is unnecessary and mainly clutters host cache memory. Though this is generally no concern as cache memory is freely available and can be scaled by the host OS, it may become a concern with thin provisioning.

For these cases, it makes sense to allow users to freely specify the source cache mode (e.g. use no cache at all).

This commit adds a new switch (-T) for the qemu-img subcommands check, compare, convert and rebase to specify the cache to be used for source images (the backing file in case of rebase).

Quoted 6 lines
---
 qemu-img-cmds.hx | 16 ++++++------
 qemu-img.c       | 78 ++++++++++++++++++++++++++++++++++++++++++++------------
 qemu-img.texi    | 14 +++++++---
 3 files changed, 80 insertions(+), 28 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on the v2 series
#4 lore raw
In reply to Max Reitz

Am 22.07.2014 um 22:58 hat Max Reitz geschrieben:

Quoted 8 lines

Currently, qemu-img does not allow setting the cache mode for source images. However, it reads images generally only once, therefore a full writeback cache unnecessarily clutters the host cache. In case the user finds this undesirable, there has to be a way of disabling that cache. This series first introduces such a way for check, compare, convert and rebase and then (while at it) adds a cache mode switch to amend (which was missing so far).

Quoted 4 lines

Peter already tried to add a BDRV_O_SEQUENTIAL mode to qemu-img convert. If we want to add such a mode (which in my opinion would be the logical consequence of this series), we may want to make it just another cache mode which can be selected by the user.

I would prefer if we used -t in all places where only one image is used, so that I don't have to remember that supposedly check -r has only an input image, whereas amend has only an output image. But that's probably a matter of taste...

If you don't want to change it, the implementation looks okay:

author
#5 lore raw
Quoted 13 lines · Kevin Wolf

Am 22.07.2014 um 22:58 hat Max Reitz geschrieben:

Quoted 8 lines

Currently, qemu-img does not allow setting the cache mode for source images. However, it reads images generally only once, therefore a full writeback cache unnecessarily clutters the host cache. In case the user finds this undesirable, there has to be a way of disabling that cache. This series first introduces such a way for check, compare, convert and rebase and then (while at it) adds a cache mode switch to amend (which was missing so far).

Quoted 4 lines

Peter already tried to add a BDRV_O_SEQUENTIAL mode to qemu-img convert. If we want to add such a mode (which in my opinion would be the logical consequence of this series), we may want to make it just another cache mode which can be selected by the user.

Quoted 8 lines

I would prefer if we used -t in all places where only one image is used, so that I don't have to remember that supposedly check -r has only an input image, whereas amend has only an output image. But that's probably a matter of taste...

If you don't want to change it, the implementation looks okay:

In order to get this series in as fast as possible, I won't change it in this series anymore.

Maybe we can add support for "-t" and "-T" being usable interchangably for such single-image commands in the future, but for now I hope the error messages will suffice.

Max

Applied
#6 lore raw
In reply to Max Reitz

Am 22.07.2014 um 22:58 hat Max Reitz geschrieben:

Quoted 13 lines

Currently, qemu-img does not allow setting the cache mode for source images. However, it reads images generally only once, therefore a full writeback cache unnecessarily clutters the host cache. In case the user finds this undesirable, there has to be a way of disabling that cache. This series first introduces such a way for check, compare, convert and rebase and then (while at it) adds a cache mode switch to amend (which was missing so far).

Peter already tried to add a BDRV_O_SEQUENTIAL mode to qemu-img convert. If we want to add such a mode (which in my opinion would be the logical consequence of this series), we may want to make it just another cache mode which can be selected by the user.

Quoted 7 lines

v2:
- Patch 1:
- Remove superfluous space in qemu-img-cmds.hx [Eric]
- Reorder all getopt() optstrings which are touched by this patch anyway according to the subsequent switch [Eric (implicitly)]
- Patch 2:
- Reorder img_amend()'s optstring [Eric]

Thanks, applied all to the block branch.

Kevin