Split in an own function the code dong the mounting, the inspection of
the kernel files, and the downloading, including a per-OS function for
the actual kernel files. This gives few advantages:
- the download phease is not repeated for all the files to fetch
- it is easier to eventually support multi-root disk images
- it is possible to support OSes different than Linux; virt-get-kernel
now will just report the unsupported OS, instead of a generic
"no kernel found" message
This is mostly code refactoring, with (on Linux) no actual behaviour
change.
---
get-kernel/get_kernel.ml | 80 +++++++++++++++++++++++++++++-------------------
1 file changed, 49 insertions(+), 31 deletions(-)
diff --git a/get-kernel/get_kernel.ml b/get-kernel/get_kernel.ml
index 3b27740..233d459 100644
--- a/get-kernel/get_kernel.ml
+++ b/get-kernel/get_kernel.ml
@@ -120,24 +120,7 @@ read the man page virt-get-kernel(1).
add, output, unversioned, prefix
-(* Main program. *)
-let main () =
- let add, output, unversioned, prefix = parse_cmdline () in
-
- (* Connect to libguestfs. *)
- let g = new G.guestfs () in
- if trace () then g#set_trace true;
- if verbose () then g#set_verbose true;
- add g;
- g#launch ();
-
- let roots = g#inspect_os () in
- if Array.length roots = 0 then
- error (f_"no operating system found");
- if Array.length roots > 1 then
- error (f_"dual/multi-boot images are not supported by this tool");
- let root = roots.(0) in
-
+let rec do_fetch ~transform_fn ~outputdir g root =
(* Mount up the disks. *)
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in
@@ -148,6 +131,24 @@ let main () =
with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
) mps;
+ let files =
+ let typ = g#inspect_get_type root in
+ match typ with
+ | "linux" -> pick_kernel_files_linux g root
+ | typ ->
+ error (f_"operating system '%s' not supported") typ in
+
+ (* Download the files. *)
+ List.iter (
+ fun f ->
+ let dest = outputdir // transform_fn f in
+ printf "download: %s -> %s\n%!" f dest;
+ g#download f dest;
+ ) files;
+
+ g#umount_all ()
+
+and pick_kernel_files_linux g root =
(* Get all kernels and initramfses. *)
let glob w = Array.to_list (g#glob_expand w) in
let kernels = glob "/boot/vmlinuz-*" in
@@ -164,7 +165,34 @@ let main () =
let initrds = List.rev (List.sort compare_version initrds) in
if kernels = [] then
- error (f_"no kernel found");
+ error (f_"no kernel found in %s") root;
+
+ (* Pick the latest. *)
+ let kernel = [List.hd kernels] in
+ let initrd =
+ match initrds with
+ | [] -> []
+ | initrd :: _ -> [initrd] in
+
+ kernel @ initrd
+
+(* Main program. *)
+let main () =
+ let add, output, unversioned, prefix = parse_cmdline () in
+
+ (* Connect to libguestfs. *)
+ let g = new G.guestfs () in
+ if trace () then g#set_trace true;
+ if verbose () then g#set_verbose true;
+ add g;
+ g#launch ();
+
+ let roots = g#inspect_os () in
+ if Array.length roots = 0 then
+ error (f_"no operating system found");
+ if Array.length roots > 1 then
+ error (f_"dual/multi-boot images are not supported by this tool");
+ let root = roots.(0) in
let dest_filename fn =
let fn = Filename.basename fn in
@@ -175,22 +203,12 @@ let main () =
| None -> fn
| Some p -> p ^ "-" ^ fn in
- (* Download the latest. *)
let outputdir =
match output with
| None -> Filename.current_dir_name
| Some dir -> dir in
- let kernel_in = List.hd kernels in
- let kernel_out = outputdir // dest_filename kernel_in in
- printf "download: %s -> %s\n%!" kernel_in kernel_out;
- g#download kernel_in kernel_out;
-
- if initrds <> [] then (
- let initrd_in = List.hd initrds in
- let initrd_out = outputdir // dest_filename initrd_in in
- printf "download: %s -> %s\n%!" initrd_in initrd_out;
- g#download initrd_in initrd_out
- );
+
+ do_fetch ~transform_fn:dest_filename ~outputdir g root;
(* Shutdown. *)
g#shutdown ();
--
2.1.0