Backing file loop should be checked before qemu-img create command
execution. If loop is found, qemu-img create should be stopped and
an error printed.

Signed-off-by: Xu Wang <gesa...@linux.vnet.ibm.com>
---
 block.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/block.c b/block.c
index 7016ce8..b8cea1c 100644
--- a/block.c
+++ b/block.c
@@ -4633,14 +4633,6 @@ void bdrv_img_create(const char *filename, const char 
*fmt,
     }
 
     backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
-    if (backing_file && backing_file->value.s) {
-        if (!strcmp(filename, backing_file->value.s)) {
-            error_setg(errp, "Error: Trying to create an image with the "
-                             "same filename as the backing file");
-            goto out;
-        }
-    }
-
     backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
     if (backing_fmt && backing_fmt->value.s) {
         backing_drv = bdrv_find_format(backing_fmt->value.s);
@@ -4651,6 +4643,16 @@ void bdrv_img_create(const char *filename, const char 
*fmt,
         }
     }
 
+    if (backing_file && backing_file->value.s) {
+        if (!bdrv_backing_chain_okay(backing_file->value.s,
+                                     backing_drv, filename,
+                                     &local_err)) {
+            error_setg(errp, "Failed to create image: %s",
+                       error_get_pretty(local_err));
+            goto out;
+        }
+    }
+
     // The size for the image must always be specified, with one exception:
     // If we are using a backing file, we can obtain the size from there
     size = get_option_parameter(param, BLOCK_OPT_SIZE);
-- 
1.8.1.4


Reply via email to