On Thursday, 11 August 2016 00:35:55 CEST Tomáš Golembiovský wrote:
> On Debian family of OSes Grub2 tools are prefixed with 'grub-', not with
> 'grub2-'. We have to detect the correct name of the tool to use it.
> 
> Signed-off-by: Tomáš Golembiovský <[email protected]>
> ---
>  v2v/convert_linux.ml | 21 +++++++++++++++++++--
>  1 file changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/v2v/convert_linux.ml b/v2v/convert_linux.ml
> index 103728b..1f5f12c 100644
> --- a/v2v/convert_linux.ml
> +++ b/v2v/convert_linux.ml
> @@ -109,6 +109,23 @@ let rec convert ~keep_serial_console (g : G.guestfs) 
> inspect source rcaps =
>        Not_found ->
>          error (f_"no grub1/grub-legacy or grub2 configuration file was 
> found") in
>  
> +  let grub2_mkconfig_cmd =
> +    if grub = `Grub2 then
> +      try
> +        (* Red Hat and Suse families *)
> +        ignore (g#command [| "grub2-mkconfig"; "--version" |]);
> +        "grub2-mkconfig"
> +      with G.Error _ ->
> +        try
> +          (* Debian family *)
> +          ignore (g#command [| "grub-mkconfig"; "--version" |]);
> +          "grub-mkconfig"
> +        with G.Error _ ->
> +          error (f_"failed to find grub2-mkconfig binary (but Grub2 was 
> detected on guest)")
> +    else
> +      ""
> +  in

Maybe it would be worth to put the elements in a list, and iterate
using List.find:

  let elems = [ "grub2-mkconfig"; "grub-mkconfig" ] in
  let elem =
    try
      List.find (
        let e ->
          try ignore (g#command [| e; "--version" |]); true
          with G.Error _ -> false
      ) elems
    with Not_found ->
      error "not found" in

Also, what I've seen usually done is checking for the existance of the
executable, eg: if g#file_exists "/usr/sbin/grub2-mkconfig" then ...
this way, even if requiring to specify the exact locations of the tools,
would avoid ignoring a tool just because it cannot run for some reason,
while we need to fail because of that.

-- 
Pino Toscano

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Libguestfs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/libguestfs

Reply via email to