26ba25
From 68af7bd1c3085fb25f50a7ad8cada0c33dbba748 Mon Sep 17 00:00:00 2001
26ba25
From: Max Reitz <mreitz@redhat.com>
26ba25
Date: Mon, 18 Jun 2018 17:48:32 +0200
26ba25
Subject: [PATCH 071/268] qemu-img: Resolve relative backing paths in rebase
26ba25
26ba25
RH-Author: Max Reitz <mreitz@redhat.com>
26ba25
Message-id: <20180618174833.19439-2-mreitz@redhat.com>
26ba25
Patchwork-id: 80790
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 1/2] qemu-img: Resolve relative backing paths in rebase
26ba25
Bugzilla: 1569835
26ba25
RH-Acked-by: John Snow <jsnow@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
26ba25
Currently, rebase interprets a relative path for the new backing image
26ba25
as follows:
26ba25
(1) Open the new backing image with the given relative path (thus relative to
26ba25
    qemu-img's working directory).
26ba25
(2) Write it directly into the overlay's backing path field (thus
26ba25
    relative to the overlay).
26ba25
26ba25
If the overlay is not in qemu-img's working directory, both will be
26ba25
different interpretations, which may either lead to an error somewhere
26ba25
(either rebase fails because it cannot open the new backing image, or
26ba25
your overlay becomes unusable because its backing path does not point to
26ba25
a file), or, even worse, it may result in your rebase being performed
26ba25
for a different backing file than what your overlay will point to after
26ba25
the rebase.
26ba25
26ba25
Fix this by interpreting the target backing path as relative to the
26ba25
overlay, like qemu-img does everywhere else.
26ba25
26ba25
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1569835
26ba25
Cc: qemu-stable@nongnu.org
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Message-id: 20180509182002.8044-2-mreitz@redhat.com
26ba25
Reviewed-by: Eric Blake <eblake@redhat.com>
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit d16699b64671466b42079c45b89127aeea1ca565)
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 qemu-img.c | 23 ++++++++++++++++++++++-
26ba25
 1 file changed, 22 insertions(+), 1 deletion(-)
26ba25
26ba25
diff --git a/qemu-img.c b/qemu-img.c
26ba25
index 4bcf157..76d6e47 100644
26ba25
--- a/qemu-img.c
26ba25
+++ b/qemu-img.c
26ba25
@@ -3202,6 +3202,9 @@ static int img_rebase(int argc, char **argv)
26ba25
         }
26ba25
 
26ba25
         if (out_baseimg[0]) {
26ba25
+            const char *overlay_filename;
26ba25
+            char *out_real_path;
26ba25
+
26ba25
             options = qdict_new();
26ba25
             if (out_basefmt) {
26ba25
                 qdict_put_str(options, "driver", out_basefmt);
26ba25
@@ -3210,8 +3213,26 @@ static int img_rebase(int argc, char **argv)
26ba25
                 qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true);
26ba25
             }
26ba25
 
26ba25
-            blk_new_backing = blk_new_open(out_baseimg, NULL,
26ba25
+            overlay_filename = bs->exact_filename[0] ? bs->exact_filename
26ba25
+                                                     : bs->filename;
26ba25
+            out_real_path = g_malloc(PATH_MAX);
26ba25
+
26ba25
+            bdrv_get_full_backing_filename_from_filename(overlay_filename,
26ba25
+                                                         out_baseimg,
26ba25
+                                                         out_real_path,
26ba25
+                                                         PATH_MAX,
26ba25
+                                                         &local_err);
26ba25
+            if (local_err) {
26ba25
+                error_reportf_err(local_err,
26ba25
+                                  "Could not resolve backing filename: ");
26ba25
+                ret = -1;
26ba25
+                g_free(out_real_path);
26ba25
+                goto out;
26ba25
+            }
26ba25
+
26ba25
+            blk_new_backing = blk_new_open(out_real_path, NULL,
26ba25
                                            options, src_flags, &local_err);
26ba25
+            g_free(out_real_path);
26ba25
             if (!blk_new_backing) {
26ba25
                 error_reportf_err(local_err,
26ba25
                                   "Could not open new backing file '%s': ",
26ba25
-- 
26ba25
1.8.3.1
26ba25