Skip to contentSkip to conversation

qemu-img: Print error if check failed

PATCH Applied Max Reitz 3 participants 6 messages Oct 23, 2014

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qemu-img: Print error if check failed

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

---
 qemu-img.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
qemu-img.c +14 -7
index 09e7e72..731502c 100644
@@ -687,16 +687,23 @@ static int img_check(int argc, char **argv)
         check->corruptions_fixed    = corruptions_fixed;
     }
 
-    switch (output_format) {
-    case OFORMAT_HUMAN:
-        dump_human_image_check(check, quiet);
-        break;
-    case OFORMAT_JSON:
-        dump_json_image_check(check, quiet);
-        break;
+    if (!ret) {
+        switch (output_format) {
+        case OFORMAT_HUMAN:
+            dump_human_image_check(check, quiet);
+            break;
+        case OFORMAT_JSON:
+            dump_json_image_check(check, quiet);
+            break;
+        }
     }
 
     if (ret || check->check_errors) {
+        if (ret) {
+            error_report("Check failed: %s", strerror(-ret));
+        } else {
+            error_report("Check failed");
+        }
         ret = 1;
         goto fail;
     }
Signature · 1 line
1.9.3
#1 lore raw
Quoted 8 lines · Max Reitz

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

Is there a way to exercise this in the testsuite?

Quoted 22 lines
---
 qemu-img.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
qemu-img.c +2 -7
index 09e7e72..731502c 100644
@@ -687,16 +687,23 @@ static int img_check(int argc, char **argv)
         check->corruptions_fixed    = corruptions_fixed;
     }
 
-    switch (output_format) {
-    case OFORMAT_HUMAN:
-        dump_human_image_check(check, quiet);
-        break;
-    case OFORMAT_JSON:
-        dump_json_image_check(check, quiet);
-        break;
+    if (!ret) {
+        switch (output_format) {

qemu-img.c static int img_check(int argc, char **argv) · line 701

Quoted 10 lines
qemu-img.c +7 -0
+        case OFORMAT_HUMAN:
+            dump_human_image_check(check, quiet);
+            break;
+        case OFORMAT_JSON:
+            dump_json_image_check(check, quiet);
+            break;
+        }
     }
 
     if (ret || check->check_errors) {

Can we ever have ret == 0 (so we attempted dump_*_image_check) AND check->check_errors? Will that be confusing output, to have both a (probably incorrect) dump on stdout and an error message on stderr?

qemu-img.c static int img_check(int argc, char **argv) · line 709

Quoted 8 lines
qemu-img.c +5 -0
+        if (ret) {
+            error_report("Check failed: %s", strerror(-ret));
+        } else {
+            error_report("Check failed");
+        }
         ret = 1;
         goto fail;
     }

Or rephrasing the question, would it be better to hoist this chunk to occur before the switch (output_format)? And if so, then you don't need to reindent that code inside 'if (!ret)'.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 10 lines · Eric Blake
Quoted 8 lines · Max Reitz

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

Is there a way to exercise this in the testsuite?

It would involve some blkdebug things which try to break the qcow2 check function. I wouldn't rely on it, because this rather exercises the qcow2 check function than this patch.

Quoted 31 lines
Quoted 31 lines
---
  qemu-img.c | 21 ++++++++++++++-------
  1 file changed, 14 insertions(+), 7 deletions(-)
qemu-img.c +9 -7
index 09e7e72..731502c 100644
@@ -687,16 +687,23 @@ static int img_check(int argc, char **argv)
          check->corruptions_fixed    = corruptions_fixed;
      }
 
-    switch (output_format) {
-    case OFORMAT_HUMAN:
-        dump_human_image_check(check, quiet);
-        break;
-    case OFORMAT_JSON:
-        dump_json_image_check(check, quiet);
-        break;
+    if (!ret) {
+        switch (output_format) {
+        case OFORMAT_HUMAN:
+            dump_human_image_check(check, quiet);
+            break;
+        case OFORMAT_JSON:
+            dump_json_image_check(check, quiet);
+            break;
+        }
      }
 
Quoted 4 lines

qemu-img.c static int img_check(int argc, char **argv) · line 701

Quoted 1 line
qemu-img.c +0 -0
      if (ret || check->check_errors) {

Can we ever have ret == 0 (so we attempted dump_*_image_check) AND check->check_errors? Will that be confusing output, to have both a (probably incorrect) dump on stdout and an error message on stderr?

Yes, I think we can. I interpreted that as "Test completed, but there were errors". The dump should not be incorrect, because if it was, the check function should not have returned 0.

Therefore, I think we should dump the test result because by returning 0 the check function says it's valid. If there were check_errors, the dump function will show their number, too.

Max

Quoted 12 lines

qemu-img.c static int img_check(int argc, char **argv) · line 709

Quoted 8 lines
qemu-img.c +5 -0
+        if (ret) {
+            error_report("Check failed: %s", strerror(-ret));
+        } else {
+            error_report("Check failed");
+        }
          ret = 1;
          goto fail;
      }

Or rephrasing the question, would it be better to hoist this chunk to occur before the switch (output_format)? And if so, then you don't need to reindent that code inside 'if (!ret)'.

Reviewed-by
#3 lore raw
Quoted 10 lines · Max Reitz
Quoted 9 lines · Eric Blake
Quoted 8 lines · Max Reitz

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

Quoted 5 lines
Quoted 1 line

Is there a way to exercise this in the testsuite?

It would involve some blkdebug things which try to break the qcow2 check function. I wouldn't rely on it, because this rather exercises the qcow2 check function than this patch.

Fair enough.

Quoted 10 lines
Quoted 9 lines

qemu-img.c static int img_check(int argc, char **argv) · line 701

Quoted 6 lines
qemu-img.c +4 -0
+        case OFORMAT_JSON:
+            dump_json_image_check(check, quiet);
+            break;
+        }
      }
        if (ret || check->check_errors) {

Can we ever have ret == 0 (so we attempted dump_*_image_check) AND check->check_errors? Will that be confusing output, to have both a (probably incorrect) dump on stdout and an error message on stderr?

Quoted 7 lines

Yes, I think we can. I interpreted that as "Test completed, but there were errors". The dump should not be incorrect, because if it was, the check function should not have returned 0.

Therefore, I think we should dump the test result because by returning 0 the check function says it's valid. If there were check_errors, the dump function will show their number, too.

Furthermore, if 'quiet' is true, then the dump_* call probably output nothing, but the fact that we want to return non-zero to flag that check detected problems is worth being noisy about. If I understand correctly, 'quiet' is only about suppressing stdout, not stderr, and always being noisy on stderr for non-zero exit is a good idea.

Okay, I think your answers convinced me, and I'm comfortable enough with the patch as-is to give:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/1
#4 lore raw
In reply to Max Reitz
Quoted 38 lines · Max Reitz

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

---
 qemu-img.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
qemu-img.c +9 -7
index 09e7e72..731502c 100644
@@ -687,16 +687,23 @@ static int img_check(int argc, char **argv)
         check->corruptions_fixed    = corruptions_fixed;
     }
 
-    switch (output_format) {
-    case OFORMAT_HUMAN:
-        dump_human_image_check(check, quiet);
-        break;
-    case OFORMAT_JSON:
-        dump_json_image_check(check, quiet);
-        break;
+    if (!ret) {
+        switch (output_format) {
+        case OFORMAT_HUMAN:
+            dump_human_image_check(check, quiet);
+            break;
+        case OFORMAT_JSON:
+            dump_json_image_check(check, quiet);
+            break;
+        }
     }

qemu-img.c static int img_check(int argc, char **argv) · line 709

Quoted 10 lines
qemu-img.c +5 -0
 
     if (ret || check->check_errors) {
+        if (ret) {
+            error_report("Check failed: %s", strerror(-ret));
+        } else {
+            error_report("Check failed");
+        }
         ret = 1;
         goto fail;
     }

Non-zero ret can only come from bdrv_check() via collect_image_check(). bdrv_check() is specified to return zero or -errno. Okay.

Aside: ret is used for error codes, 0/-1 and for the return value 0/1/2/3/63. Unclean, but not this patch's fault.

author Applied
#5 lore raw
In reply to Max Reitz
Quoted 4 lines · Max Reitz

Currently, if bdrv_check() fails either by returning -errno or having check_errors set, qemu-img check just exits with 1 after having told the user that there were no errors on the image. This is bad.

Quoted 8 lines

Instead of printing the check result if there were internal errors which were so bad that bdrv_check() could not even complete with 0 as a return value, qemu-img check should inform the user about the error.

---
  qemu-img.c | 21 ++++++++++++++-------
  1 file changed, 14 insertions(+), 7 deletions(-)

This feels greedy, but Kevin encouraged me to on IRC, so:

Applied to my block tree:
https://github.com/XanClic/qemu/commits/block

Max