[PATCH] qemu-img: Add nocache command line option

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



qemu-img currently writes images using writeback and filling up
the cache buffers which are then flushed by the kernel preventing
other processes from accessing the storage.
This is particularly bad in cluster environments where time-based
algorithms might be in place and accessing the storage within
certain timeouts is critical.
This patch adds the option to use nocache when other solutions
(eg: cgroups) are not available.

Signed-off-by: Federico Simoncelli <fsimonce@xxxxxxxxxx>
---
 qemu-img.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 4f162d1..c10e4a6 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -78,6 +78,7 @@ static void help(void)
            "       rebasing in this case (useful for renaming the backing file)\n"
            "  '-h' with or without a command shows this help and lists the supported formats\n"
            "  '-p' show progress of command (only certain commands)\n"
+           "  '-t' use write-through (no cache), valid with: convert, commit and rebase\n"
            "\n"
            "Parameters to snapshot subcommand:\n"
            "  'snapshot' is the name of the snapshot to create, apply or delete\n"
@@ -441,13 +442,14 @@ static int img_check(int argc, char **argv)
 
 static int img_commit(int argc, char **argv)
 {
-    int c, ret;
+    int c, ret, flags;
     const char *filename, *fmt;
     BlockDriverState *bs;
 
     fmt = NULL;
+    flags = BDRV_O_FLAGS | BDRV_O_RDWR;
     for(;;) {
-        c = getopt(argc, argv, "f:h");
+        c = getopt(argc, argv, "f:ht");
         if (c == -1) {
             break;
         }
@@ -459,6 +461,10 @@ static int img_commit(int argc, char **argv)
         case 'f':
             fmt = optarg;
             break;
+        case 't':
+            flags &= ~BDRV_O_CACHE_WB;
+            flags |= BDRV_O_NOCACHE;
+            break;
         }
     }
     if (optind >= argc) {
@@ -466,7 +472,7 @@ static int img_commit(int argc, char **argv)
     }
     filename = argv[optind++];
 
-    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
+    bs = bdrv_new_open(filename, fmt, flags);
     if (!bs) {
         return 1;
     }
@@ -591,7 +597,7 @@ static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
 static int img_convert(int argc, char **argv)
 {
     int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
-    int progress = 0;
+    int progress = 0, flags;
     const char *fmt, *out_fmt, *out_baseimg, *out_filename;
     BlockDriver *drv, *proto_drv;
     BlockDriverState **bs = NULL, *out_bs = NULL;
@@ -610,8 +616,9 @@ static int img_convert(int argc, char **argv)
     out_fmt = "raw";
     out_baseimg = NULL;
     compress = 0;
+    flags = BDRV_O_FLAGS | BDRV_O_RDWR | BDRV_O_NO_FLUSH;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:s:hce6o:p");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:pt");
         if (c == -1) {
             break;
         }
@@ -649,6 +656,10 @@ static int img_convert(int argc, char **argv)
         case 'p':
             progress = 1;
             break;
+        case 't':
+            flags &= ~BDRV_O_CACHE_WB;
+            flags |= BDRV_O_NOCACHE;
+            break;
         }
     }
 
@@ -779,8 +790,7 @@ static int img_convert(int argc, char **argv)
         goto out;
     }
 
-    out_bs = bdrv_new_open(out_filename, out_fmt,
-        BDRV_O_FLAGS | BDRV_O_RDWR | BDRV_O_NO_FLUSH);
+    out_bs = bdrv_new_open(out_filename, out_fmt, flags);
     if (!out_bs) {
         ret = -1;
         goto out;
@@ -1234,9 +1244,10 @@ static int img_rebase(int argc, char **argv)
     fmt = NULL;
     out_baseimg = NULL;
     out_basefmt = NULL;
+    flags = BDRV_O_FLAGS | BDRV_O_RDWR;
 
     for(;;) {
-        c = getopt(argc, argv, "uhf:F:b:p");
+        c = getopt(argc, argv, "uhf:F:b:pt");
         if (c == -1) {
             break;
         }
@@ -1256,10 +1267,15 @@ static int img_rebase(int argc, char **argv)
             break;
         case 'u':
             unsafe = 1;
+            flags |= BDRV_O_NO_BACKING;
             break;
         case 'p':
             progress = 1;
             break;
+        case 't':
+            flags &= ~BDRV_O_CACHE_WB;
+            flags |= BDRV_O_NOCACHE;
+            break;
         }
     }
 
@@ -1277,7 +1293,6 @@ static int img_rebase(int argc, char **argv)
      * Ignore the old backing file for unsafe rebase in case we want to correct
      * the reference to a renamed or moved backing file.
      */
-    flags = BDRV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
     bs = bdrv_new_open(filename, fmt, flags);
     if (!bs) {
         return 1;
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux