Re: [Libguestfs] [PATCH v3 4/4] v2v: Add --print-estimate option to print copy size estimate.

Pino Toscano
Mon, 20 Aug 2018 14:15:52 -0000

On Friday, 17 August 2018 17:16:13 CEST Richard W.M. Jones
wrote:
> This option prints the estimated size of the data that will be copied
> from the source disk.
>
> Currently this overestimates by the size of the qcow2 header, but for
> real disk images that doesn't matter much.
>
> For example:
>
> $ virt-builder fedora-27
> $ virt-v2v -i disk fedora-27.img -o null --print-estimate
> [...]
> virt-v2v: This guest has virtio drivers installed.
> [  44.0] Mapping filesystem data to avoid copying unused and blank areas
> [  44.5] Closing the overlay
> 1047920640

This is not easy to parse, because this output contains every output,
from the info messages to this value.  IMHO a better idea is to make
use of the machine-parseable to print this in some better format
(JSON?), and possibly to a separate output than the normal stdout.

I had already an idea for this, i.e. improve the output of
--machine-readable.  Let me shape it in form of patches.

Regardless, a couple of notes follow.

> +  let fd = Unix.openfile json [O_WRONLY; O_CREAT; O_TRUNC] 0o600 in
> +  if run_command ~stdout_fd:fd !cmd <> 0 then
> +    error (f_"qemu-img measure failed, see earlier errors");
> +  (* Note that run_command closes fd. *)
> +
> +  let json = read_whole_file json in

I'd print 'json' here when debugging is enabled.

> +  (* We're expecting the tree to contain nodes:
> +   * object [| "required", number; "fully-allocated",
number |]
> +   * Of course the array could appear in any order.
> +   *)
> +  match tree with
> +  | JSON_parser_object fields ->
> +     let fields = Array.to_list fields in
> +     let rq > +       try List.assoc "required" fields
> +       with Not_found ->
> +            error_unexpected_output (s_"expecting
\"required\" field") json in
> +     (match rq with
> +      | JSON_parser_number i64 ->
> +         i64
> +      | _ ->
> +         error_unexpected_output (s_"expecting \"required\"
to be an integer")
> +                                 json
> +     )
> +  | _ ->
> +     error_unexpected_output (s_"expecting object at top level")
json

You can replace this whole using the helper object_get_number:

  let v = object_get_number "required" tree in

--
Pino Toscano