[Libguestfs] [PATCH v2 1/3] daemon: parted: Always use -s option even with -m.

Richard W.M. Jones rjones at redhat.com
Thu Jun 18 09:21:48 UTC 2015


See: https://bugzilla.redhat.com/show_bug.cgi?id=1232241#c3

However adding the -s parameter changes the error code returned by
parted when it processes a blank disk (or other unrecognized partition
table).  Without -s it prints an error but returns a non-error exit
code (0).  With -s it prints an error and return an error exit code.

Because it's useful to be able to catch this case in user code, turn
"unrecognised disk label" into EINVAL.

Change virt-alignment-scan to catch this error and ignore it.
---
 align/scan.c    | 14 ++++++++++----
 daemon/parted.c | 14 ++++++++++----
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/align/scan.c b/align/scan.c
index 202b148..188450c 100644
--- a/align/scan.c
+++ b/align/scan.c
@@ -282,11 +282,17 @@ scan (guestfs_h *g, const char *prefix, FILE *fp)
 
   for (i = 0; devices[i] != NULL; ++i) {
     CLEANUP_FREE char *name = NULL;
+    CLEANUP_FREE_PARTITION_LIST struct guestfs_partition_list *parts = NULL;
 
-    CLEANUP_FREE_PARTITION_LIST struct guestfs_partition_list *parts =
-      guestfs_part_list (g, devices[i]);
-    if (parts == NULL)
-      return -1;
+    guestfs_push_error_handler (g, NULL, NULL);
+    parts = guestfs_part_list (g, devices[i]);
+    guestfs_pop_error_handler (g);
+    if (parts == NULL) {
+      if (guestfs_last_errno (g) == EINVAL) /* unrecognised disk label */
+        continue;
+      else
+        return -1;
+    }
 
     /* Canonicalize the name of the device for printing. */
     name = guestfs_canonical_device_name (g, devices[i]);
diff --git a/daemon/parted.c b/daemon/parted.c
index a36e4e7..b516067 100644
--- a/daemon/parted.c
+++ b/daemon/parted.c
@@ -356,7 +356,7 @@ print_partition_table (const char *device,
   int r;
 
   if (PARTED_OPT_HAS_M == parted_has_m_opt)
-    r = command (&out, &err, str_parted, "-m", "--", device,
+    r = command (&out, &err, str_parted, "-m", "-s", "--", device,
                  "unit", "b",
                  "print", NULL);
   else
@@ -364,9 +364,15 @@ print_partition_table (const char *device,
                  "unit", "b",
                  "print", NULL);
   if (r == -1) {
-    reply_with_error ("parted print: %s: %s", device,
-                      /* Hack for parted 1.x which sends errors to stdout. */
-                      *err ? err : out);
+    /* Hack for parted 1.x which sends errors to stdout. */
+    const char *msg = *err ? err : out;
+    int errcode = 0;
+
+    /* Translate "unrecognised disk label" into an errno code. */
+    if (msg && strstr (msg, "unrecognised disk label") != NULL)
+      errcode = EINVAL;
+
+    reply_with_error_errno (errcode, "parted print: %s: %s", device, msg);
     free (out);
     return NULL;
   }
-- 
2.3.1




More information about the Libguestfs mailing list