fish: Don't fail if some mountpoints in /etc/fstab are bogus (RHBZ#668574).
[libguestfs.git] / ocaml / examples / inspect_vm.ml
1 (* Example showing how to inspect a virtual machine disk. *)
2
3 open Printf
4
5 let disk =
6   if Array.length Sys.argv = 2 then
7     Sys.argv.(1)
8   else
9     failwith "usage: inspect_vm disk.img"
10
11 let () =
12   let g = new Guestfs.guestfs () in
13
14   (* Attach the disk image read-only to libguestfs. *)
15   g#add_drive_opts (*~format:"raw"*) ~readonly:true disk;
16
17   (* Run the libguestfs back-end. *)
18   g#launch ();
19
20   (* Ask libguestfs to inspect for operating systems. *)
21   let roots = g#inspect_os () in
22   if Array.length roots = 0 then
23     failwith "inspect_vm: no operating systems found";
24
25   Array.iter (
26     fun root ->
27       printf "Root device: %s\n" root;
28
29       (* Print basic information about the operating system. *)
30       printf "  Product name: %s\n" (g#inspect_get_product_name root);
31       printf "  Version:      %d.%d\n"
32         (g#inspect_get_major_version root)
33         (g#inspect_get_minor_version root);
34       printf "  Type:         %s\n" (g#inspect_get_type root);
35       printf "  Distro:       %s\n" (g#inspect_get_distro root);
36
37       (* Mount up the disks, like guestfish -i.
38        *
39        * Sort keys by length, shortest first, so that we end up
40        * mounting the filesystems in the correct order.
41        *)
42       let mps = g#inspect_get_mountpoints root in
43       let cmp (a,_) (b,_) =
44         compare (String.length a) (String.length b) in
45       let mps = List.sort cmp mps in
46       List.iter (
47         fun (mp, dev) ->
48           try g#mount_ro dev mp
49           with Guestfs.Error msg -> eprintf "%s (ignored)\n" msg
50       ) mps;
51
52       (* If /etc/issue.net file exists, print up to 3 lines. *)
53       let filename = "/etc/issue.net" in
54       if g#is_file filename then (
55         printf "--- %s ---\n" filename;
56         let lines = g#head_n 3 filename in
57         Array.iter print_endline lines
58       );
59
60       (* Unmount everything. *)
61       g#umount_all ()
62   ) roots