3 # Example showing how to inspect a virtual machine disk.
9 die "usage: inspect_vm disk.img"
14 my $g = new Sys::Guestfs ();
16 # Attach the disk image read-only to libguestfs.
17 # You could also add an optional format => ... argument here. This is
18 # advisable since automatic format detection is insecure.
19 $g->add_drive_opts ($disk, readonly => 1);
21 # Run the libguestfs back-end.
24 # Ask libguestfs to inspect for operating systems.
25 my @roots = $g->inspect_os ();
27 die "inspect_vm: no operating systems found";
30 for my $root (@roots) {
31 printf "Root device: %s\n", $root;
33 # Print basic information about the operating system.
34 printf " Product name: %s\n", $g->inspect_get_product_name ($root);
35 printf " Version: %d.%d\n",
36 $g->inspect_get_major_version ($root),
37 $g->inspect_get_minor_version ($root);
38 printf " Type: %s\n", $g->inspect_get_type ($root);
39 printf " Distro: %s\n", $g->inspect_get_distro ($root);
41 # Mount up the disks, like guestfish -i.
43 # Sort keys by length, shortest first, so that we end up
44 # mounting the filesystems in the correct order.
45 my %mps = $g->inspect_get_mountpoints ($root);
46 my @mps = sort { length $a <=> length $b } (keys %mps);
48 eval { $g->mount_ro ($mps{$mp}, $mp) };
50 print "$@ (ignored)\n"
54 # If /etc/issue.net file exists, print up to 3 lines.
55 my $filename = "/etc/issue.net";
56 if ($g->is_file ($filename)) {
57 printf "--- %s ---\n", $filename;
58 my @lines = $g->head_n (3, $filename);
59 print "$_\n" foreach @lines;