mllib: factor out mounting of guest root

Introduce and use a new inspect_mount_root function to mount all the
mountpoints of a root in the guest, replacing the same code doing that
in different tools.

inspect_mount_root_ro is inspect_mount_root with readonly mount option.
This commit is contained in:
Pino Toscano
2016-03-03 14:20:46 +01:00
parent d53fd558ae
commit 82b0d25bb2
7 changed files with 34 additions and 44 deletions

View File

@@ -664,15 +664,7 @@ let main () =
let root =
match Array.to_list (g#inspect_os ()) with
| [root] ->
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) =
compare (String.length a) (String.length b) in
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
try g#mount dev mp
with G.Error msg -> warning (f_"%s (ignored)") msg
) mps;
inspect_mount_root g root;
root
| _ ->
error (f_"no guest operating systems or multiboot OS found in this disk image\nThis is a failure of the source repository. Use -v for more information.")

View File

@@ -197,14 +197,7 @@ read the man page virt-customize(1).
(* Mount up the disks, like guestfish -i.
* See [ocaml/examples/inspect_vm.ml].
*)
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
try g#mount dev mp;
with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
) mps;
inspect_mount_root g root;
(* Do the customization. *)
Customize_run.run g root ops;

View File

@@ -122,14 +122,7 @@ read the man page virt-get-kernel(1).
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
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
try g#mount_ro dev mp
with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
) mps;
inspect_mount_root_ro g root;
let files =
let typ = g#inspect_get_type root in

View File

@@ -833,3 +833,21 @@ let read_first_line_from_file filename =
let is_regular_file path = (* NB: follows symlinks. *)
try (Unix.stat path).Unix.st_kind = Unix.S_REG
with Unix.Unix_error _ -> false
let inspect_mount_root g ?mount_opts_fn root =
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) =
compare (String.length a) (String.length b) in
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
let mountfn =
match mount_opts_fn with
| Some fn -> g#mount_options (fn mp)
| None -> g#mount in
try mountfn dev mp
with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
) mps
let inspect_mount_root_ro =
inspect_mount_root ~mount_opts_fn:(fun _ -> "ro")

View File

@@ -273,3 +273,14 @@ val read_first_line_from_file : string -> string
val is_regular_file : string -> bool
(** Checks whether the file is a regular file. *)
val inspect_mount_root : Guestfs.guestfs -> ?mount_opts_fn:(string -> string) -> string -> unit
(** Mounts all the mount points of the specified root, just like
[guestfish -i] does.
[mount_opts_fn] represents a function providing the mount options
for each mount point. *)
val inspect_mount_root_ro : Guestfs.guestfs -> string -> unit
(** Like [inspect_mount_root], but mounting every mount point as
read-only. *)

View File

@@ -227,17 +227,7 @@ read the man page virt-sysprep(1).
(* Mount up the disks, like guestfish -i.
* See [ocaml/examples/inspect_vm.ml].
*)
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
(* Get mount options for this mountpoint. *)
let opts = mount_opts mp in
try g#mount_options opts dev mp;
with Guestfs.Error msg -> warning (f_"%s (ignored)") msg
) mps;
inspect_mount_root ~mount_opts_fn:mount_opts g root;
let side_effects = new Sysprep_operation.filesystem_side_effects in

View File

@@ -86,14 +86,7 @@ let run ~test ?input_disk ?input_xml ?(test_plan = default_plan) () =
| _ ->
failwithf "multiple roots found in disk image %s" filename in
let mps = g#inspect_get_mountpoints root in
let cmp (a,_) (b,_) = compare (String.length a) (String.length b) in
let mps = List.sort cmp mps in
List.iter (
fun (mp, dev) ->
try g#mount_ro dev mp
with G.Error msg -> eprintf "%s (ignored)\n" msg
) mps;
inspect_mount_root_ro g root;
g, root
in