1 (* Example showing how to inspect a virtual machine disk. *)
6 if Array.length Sys.argv = 2 then
9 failwith "usage: inspect_vm disk.img"
12 let g = new Guestfs.guestfs () in
14 (* Attach the disk image read-only to libguestfs. *)
15 g#add_drive_opts (*~format:"raw"*) ~readonly:true disk;
17 (* Run the libguestfs back-end. *)
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";
27 printf "Root device: %s\n" root;
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);
37 (* Mount up the disks, like guestfish -i.
39 * Sort keys by length, shortest first, so that we end up
40 * mounting the filesystems in the correct order.
42 let mps = g#inspect_get_mountpoints root in
44 compare (String.length a) (String.length b) in
45 let mps = List.sort cmp mps in
49 with Guestfs.Error msg -> eprintf "%s (ignored)\n" msg
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
60 (* Unmount everything. *)