[Libguestfs] [supermin] Be smarter about finding suitable kernel images

Richard W.M. Jones rjones at redhat.com
Sun Apr 27 12:36:02 UTC 2014


On Sat, Apr 26, 2014 at 02:27:07PM +0200, Hilko Bengen wrote:
> ---
>  src/kernel.ml | 43 ++++++++++++++++++++++++++++---------------
>  1 file changed, 28 insertions(+), 15 deletions(-)
> 
> diff --git a/src/kernel.ml b/src/kernel.ml
> index ed5aea3..436b1b0 100644
> --- a/src/kernel.ml
> +++ b/src/kernel.ml
> @@ -23,6 +23,19 @@ open Utils
>  open Ext2fs
>  open Fnmatch
>  
> +let patt_of_cpu host_cpu =
> +  let models =
> +    match host_cpu with
> +    | "mips" | "mips64" -> [host_cpu; "*-malta"]
> +    | "ppc" | "powerpc" -> ["ppc"; "powerpc"]
> +    | "sparc" | "sparc64" -> ["sparc"; "sparc64"]
> +    | "amd64" | "x86_64" -> ["amd64"; "x86_64"]
> +    | _ when host_cpu.[0] = 'i' && host_cpu.[2] = '8' && host_cpu.[3] = '6' -> ["?86"]
> +    | _ when String.sub host_cpu 0 5 = "armv7" ->  ["armmp"]
> +    | _ -> [host_cpu]
> +  in
> +  List.map (fun model -> sprintf "vmlinu?-*-%s" model) models
> +
>  let rec build_kernel debug host_cpu dtb_wildcard copy_kernel kernel dtb =
>    (* Locate the kernel. *)
>    let kernel_name, kernel_version =
> @@ -59,9 +72,6 @@ and find_kernel debug host_cpu copy_kernel kernel =
>        let kernel_name = Filename.basename kernel_env in
>        kernel_env, kernel_name, kernel_version
>      with Not_found ->
> -      let is_x86 =
> -        String.length host_cpu = 4 &&
> -        host_cpu.[0] = 'i' && host_cpu.[2] = '8' && host_cpu.[3] = '6' in
>        let is_arm =
>          String.length host_cpu >= 3 &&
>          host_cpu.[0] = 'a' && host_cpu.[1] = 'r' && host_cpu.[2] = 'm' in
> @@ -70,18 +80,16 @@ and find_kernel debug host_cpu copy_kernel kernel =
>        let all_files = Array.to_list all_files in
>  
>        (* In original: ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen *)
> -      let patt =
> -        if is_x86 then "vmlinuz-*.i?86*"
> -        else "vmlinuz-*." ^ host_cpu ^ "*" in
> -      let files = kernel_filter patt is_arm all_files in
> +      let patterns = patt_of_cpu host_cpu in
> +      let files = kernel_filter patterns is_arm all_files in
>  
>        let files =
>          if files <> [] then files
>          else
>            (* In original: ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen *)
> -          kernel_filter "vmlinuz-*" is_arm all_files in
> +          kernel_filter ["vmlinu?-*"] is_arm all_files in
>  
> -      if files = [] then no_kernels ();
> +      if files = [] then no_kernels (host_cpu);

As a point mainly of style, you don't need the parentheses around
host_cpu here, nor in the function definition below.

>        let files = List.sort (fun a b -> compare_version b a) files in
>        let kernel_name = List.hd files in
> @@ -95,10 +103,13 @@ and find_kernel debug host_cpu copy_kernel kernel =
>    copy_or_symlink_file copy_kernel kernel_file kernel;
>    kernel_name, kernel_version
>  
> -and kernel_filter patt is_arm all_files =
> +and kernel_filter patterns is_arm all_files =
>    let files =
>      List.filter
> -      (fun filename -> fnmatch patt filename [FNM_NOESCAPE]) all_files in
> +      (fun filename ->
> +        List.exists
> +          (fun patt -> fnmatch patt filename [FNM_NOESCAPE]) patterns
> +      ) all_files in
>    let files =
>      List.filter (fun filename -> find filename "xen" = -1) files in
>    let files =
> @@ -110,15 +121,16 @@ and kernel_filter patt is_arm all_files =
>      ) in
>    List.filter (fun filename -> has_modpath filename) files
>  
> -and no_kernels () =
> +and no_kernels (host_cpu) =
>    eprintf "\
> -supermin: failed to find a suitable kernel.
> +supermin: failed to find a suitable kernel (host_cpu=%s).
>  
>  I looked for kernels in /boot and modules in /lib/modules.
>  
>  If this is a Xen guest, and you only have Xen domU kernels
>  installed, try installing a fullvirt kernel (only for
> -supermin use, you shouldn't boot the Xen guest with it).\n";
> +supermin use, you shouldn't boot the Xen guest with it).\n"
> +    host_cpu;
>    exit 1
>  
>  and find_dtb debug copy_kernel kernel_name wildcard dtb =
> @@ -200,7 +212,8 @@ and has_modpath kernel_name =
>    | Not_found -> false
>  
>  and get_kernel_version kernel_name =
> -  if string_prefix "vmlinuz-" kernel_name then (
> +  if (string_prefix "vmlinuz-" kernel_name) ||
> +    (string_prefix "vmlinux-" kernel_name) then (
>      let kv = String.sub kernel_name 8 (String.length kernel_name - 8) in
>      if modules_dep_exists kv then kv
>      else get_kernel_version_from_name kernel_name
> -- 

This looks good, ACK.

This patch might fix Roberto's bug too I think:

https://www.redhat.com/archives/libguestfs/2014-April/msg00051.html

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org




More information about the Libguestfs mailing list