Richard W.M. Jones
2015-Jun-18 09:21 UTC
[Libguestfs] [PATCH v2 0/3] daemon: parted: Always use -s option even with -m.
version 2: - Turn the "unrecognised disk label" error into errno == EINVAL - Fix virt-alignment-scan - Rework the fix for virt-v2v bug 1232192 (see description of patch 3/3)
Richard W.M. Jones
2015-Jun-18 09:21 UTC
[Libguestfs] [PATCH v2 1/3] daemon: parted: Always use -s option even with -m.
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
Richard W.M. Jones
2015-Jun-18 09:21 UTC
[Libguestfs] [PATCH v2 2/3] ocaml: Add handling for errno EINVAL.
--- generator/ocaml.ml | 3 +++ ocaml/guestfs-c.c | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/generator/ocaml.ml b/generator/ocaml.ml index d2a4690..05c7456 100644 --- a/generator/ocaml.ml +++ b/generator/ocaml.ml @@ -132,6 +132,7 @@ val last_errno : t -> int which you can use to test the return value of {!Guestfs.last_errno}. *) module Errno : sig + val errno_EINVAL : int val errno_ENOTSUP : int val errno_EPERM : int val errno_ESRCH : int @@ -286,6 +287,8 @@ external event_to_string : event list -> string external last_errno : t -> int = \"ocaml_guestfs_last_errno\" module Errno = struct + external einval : unit -> int = \"ocaml_guestfs_get_EINVAL\" \"noalloc\" + let errno_EINVAL = einval () external enotsup : unit -> int = \"ocaml_guestfs_get_ENOTSUP\" \"noalloc\" let errno_ENOTSUP = enotsup () external eperm : unit -> int = \"ocaml_guestfs_get_EPERM\" \"noalloc\" diff --git a/ocaml/guestfs-c.c b/ocaml/guestfs-c.c index f1a941c..9603f04 100644 --- a/ocaml/guestfs-c.c +++ b/ocaml/guestfs-c.c @@ -63,6 +63,7 @@ value ocaml_guestfs_set_event_callback (value gv, value closure, value events); value ocaml_guestfs_delete_event_callback (value gv, value eh); value ocaml_guestfs_event_to_string (value events); value ocaml_guestfs_last_errno (value gv); +value ocaml_guestfs_get_EINVAL (value unitv); value ocaml_guestfs_get_ENOTSUP (value unitv); value ocaml_guestfs_get_EPERM (value unitv); value ocaml_guestfs_get_ESRCH (value unitv); @@ -444,6 +445,13 @@ ocaml_guestfs_last_errno (value gv) /* NB: "noalloc" function. */ value +ocaml_guestfs_get_EINVAL (value unitv) +{ + return Val_int (EINVAL); +} + +/* NB: "noalloc" function. */ +value ocaml_guestfs_get_ENOTSUP (value unitv) { return Val_int (ENOTSUP); -- 2.3.1
Richard W.M. Jones
2015-Jun-18 09:21 UTC
[Libguestfs] [PATCH v2 3/3] v2v: Improve handling when one of the input disks is blank (RHBZ#1232192).
In commit 4c73d1d4f142c6f6211c963beea68773e11fd3ef, I changed the behaviour of virt-v2v so it ignores all errors from the g#part_get_parttype method. However that would ignore I/O errors and similar. Only ignore the "unrecognised disk label" error from parted, by testing if the return code is EINVAL. This fixes commit 4c73d1d4f142c6f6211c963beea68773e11fd3ef. --- v2v/v2v.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/v2v/v2v.ml b/v2v/v2v.ml index 3f49f68..418c836 100644 --- a/v2v/v2v.ml +++ b/v2v/v2v.ml @@ -555,7 +555,9 @@ and inspect_source g root_choice g#part_get_gpt_type dev (Int32.to_int partnum) = uefi_ESP_guid and parttype_is_gpt dev try g#part_get_parttype dev = "gpt" - with G.Error msg -> + with G.Error msg as exn -> + (* If it's _not_ "unrecognised disk label" then re-raise it. *) + if g#last_errno () <> G.Errno.errno_EINVAL then raise exn; if verbose () then printf "%s (ignored)\n" msg; false and is_uefi_bootable_device dev -- 2.3.1
Apparently Analagous Threads
- [PATCH 1/3] ocaml: dynamically generate the content of Guestfs.Errno
- [PATCH 1/4] ocaml: Add Guestfs.Errno submodule exposing useful raw errno numbers.
- Re: [PATCH 1/3] mllib: add checking for btrfs subvolume
- [PATCH 0/3] fix btrfs subvolume procession in tools
- [PATCH] OCaml tools: fix 3999 -> 3339 typo