958e1b
From aa82f406b4d4ff21041ef39547d1b166e9c6ee38 Mon Sep 17 00:00:00 2001
958e1b
From: Kevin Wolf <kwolf@redhat.com>
958e1b
Date: Thu, 23 Oct 2014 10:10:10 +0200
958e1b
Subject: [PATCH 07/19] qemu-img: Avoid duplicate block device IDs
958e1b
958e1b
Message-id: <1414059011-15516-8-git-send-email-kwolf@redhat.com>
958e1b
Patchwork-id: 61842
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH v2 7/8] qemu-img: Avoid duplicate block device IDs
958e1b
Bugzilla: 1088176
958e1b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
958e1b
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
qemu-img used to use "image" as ID for all block devices. This means
958e1b
that e.g. img_convert() ended up with potentially multiple source images
958e1b
and one target image, all with the same ID. The next patch will catch
958e1b
this and fail to open the block device.
958e1b
958e1b
This patch makes sure that qemu-img uses meaningful unique IDs for the
958e1b
block devices it uses.
958e1b
958e1b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
958e1b
Reviewed-by: Eric Blake <eblake@redhat.com>
958e1b
(cherry picked from commit 9ffe333276de8ef463896303fb951f03fd4ffcb4)
958e1b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 qemu-img.c | 38 ++++++++++++++++++++++----------------
958e1b
 1 file changed, 22 insertions(+), 16 deletions(-)
958e1b
958e1b
diff --git a/qemu-img.c b/qemu-img.c
958e1b
index 5c2f36a..fe0ac65 100644
958e1b
--- a/qemu-img.c
958e1b
+++ b/qemu-img.c
958e1b
@@ -262,7 +262,8 @@ static int print_block_option_help(const char *filename, const char *fmt)
958e1b
     return 0;
958e1b
 }
958e1b
 
958e1b
-static BlockDriverState *bdrv_new_open(const char *filename,
958e1b
+static BlockDriverState *bdrv_new_open(const char *id,
958e1b
+                                       const char *filename,
958e1b
                                        const char *fmt,
958e1b
                                        int flags,
958e1b
                                        bool require_io,
958e1b
@@ -274,7 +275,7 @@ static BlockDriverState *bdrv_new_open(const char *filename,
958e1b
     Error *local_err = NULL;
958e1b
     int ret;
958e1b
 
958e1b
-    bs = bdrv_new("image", &error_abort);
958e1b
+    bs = bdrv_new(id, &error_abort);
958e1b
 
958e1b
     if (fmt) {
958e1b
         drv = bdrv_find_format(fmt);
958e1b
@@ -616,7 +617,7 @@ static int img_check(int argc, char **argv)
958e1b
         return 1;
958e1b
     }
958e1b
 
958e1b
-    bs = bdrv_new_open(filename, fmt, flags, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
958e1b
     if (!bs) {
958e1b
         return 1;
958e1b
     }
958e1b
@@ -725,7 +726,7 @@ static int img_commit(int argc, char **argv)
958e1b
         return -1;
958e1b
     }
958e1b
 
958e1b
-    bs = bdrv_new_open(filename, fmt, flags, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
958e1b
     if (!bs) {
958e1b
         return 1;
958e1b
     }
958e1b
@@ -968,14 +969,14 @@ static int img_compare(int argc, char **argv)
958e1b
     /* Initialize before goto out */
958e1b
     qemu_progress_init(progress, 2.0);
958e1b
 
958e1b
-    bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
958e1b
+    bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
958e1b
     if (!bs1) {
958e1b
         error_report("Can't open file %s", filename1);
958e1b
         ret = 2;
958e1b
         goto out3;
958e1b
     }
958e1b
 
958e1b
-    bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
958e1b
+    bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
958e1b
     if (!bs2) {
958e1b
         error_report("Can't open file %s", filename2);
958e1b
         ret = 2;
958e1b
@@ -1280,8 +1281,11 @@ static int img_convert(int argc, char **argv)
958e1b
 
958e1b
     total_sectors = 0;
958e1b
     for (bs_i = 0; bs_i < bs_n; bs_i++) {
958e1b
-        bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true,
958e1b
-                                 quiet);
958e1b
+        char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
958e1b
+                            : g_strdup("source");
958e1b
+        bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
958e1b
+                                 true, quiet);
958e1b
+        g_free(id);
958e1b
         if (!bs[bs_i]) {
958e1b
             error_report("Could not open '%s'", argv[optind + bs_i]);
958e1b
             ret = -1;
958e1b
@@ -1395,7 +1399,7 @@ static int img_convert(int argc, char **argv)
958e1b
         return -1;
958e1b
     }
958e1b
 
958e1b
-    out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
958e1b
+    out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
958e1b
     if (!out_bs) {
958e1b
         ret = -1;
958e1b
         goto out;
958e1b
@@ -1758,8 +1762,8 @@ static ImageInfoList *collect_image_info_list(const char *filename,
958e1b
         }
958e1b
         g_hash_table_insert(filenames, (gpointer)filename, NULL);
958e1b
 
958e1b
-        bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
958e1b
-                           false, false);
958e1b
+        bs = bdrv_new_open("image", filename, fmt,
958e1b
+                           BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
958e1b
         if (!bs) {
958e1b
             goto err;
958e1b
         }
958e1b
@@ -2018,7 +2022,7 @@ static int img_map(int argc, char **argv)
958e1b
         return 1;
958e1b
     }
958e1b
 
958e1b
-    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false);
958e1b
+    bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
958e1b
     if (!bs) {
958e1b
         return 1;
958e1b
     }
958e1b
@@ -2137,7 +2141,7 @@ static int img_snapshot(int argc, char **argv)
958e1b
     filename = argv[optind++];
958e1b
 
958e1b
     /* Open the image */
958e1b
-    bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
958e1b
     if (!bs) {
958e1b
         return 1;
958e1b
     }
958e1b
@@ -2264,7 +2268,7 @@ static int img_rebase(int argc, char **argv)
958e1b
      * Ignore the old backing file for unsafe rebase in case we want to correct
958e1b
      * the reference to a renamed or moved backing file.
958e1b
      */
958e1b
-    bs = bdrv_new_open(filename, fmt, flags, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
958e1b
     if (!bs) {
958e1b
         return 1;
958e1b
     }
958e1b
@@ -2561,7 +2565,8 @@ static int img_resize(int argc, char **argv)
958e1b
     n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
958e1b
     qemu_opts_del(param);
958e1b
 
958e1b
-    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
958e1b
+                       true, quiet);
958e1b
     if (!bs) {
958e1b
         ret = -1;
958e1b
         goto out;
958e1b
@@ -2662,7 +2667,8 @@ static int img_amend(int argc, char **argv)
958e1b
         help();
958e1b
     }
958e1b
 
958e1b
-    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
958e1b
+    bs = bdrv_new_open("image", filename, fmt,
958e1b
+                       BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
958e1b
     if (!bs) {
958e1b
         error_report("Could not open image '%s'", filename);
958e1b
         ret = -1;
958e1b
-- 
958e1b
1.8.3.1
958e1b