daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / perl / examples / inspect_vm.pl
1 #!/usr/bin/perl -w
2
3 # Example showing how to inspect a virtual machine disk.
4
5 use strict;
6 use Sys::Guestfs;
7
8 if (@ARGV < 1) {
9     die "usage: inspect_vm disk.img"
10 }
11
12 my $disk = $ARGV[0];
13
14 my $g = new Sys::Guestfs ();
15
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);
20
21 # Run the libguestfs back-end.
22 $g->launch ();
23
24 # Ask libguestfs to inspect for operating systems.
25 my @roots = $g->inspect_os ();
26 if (@roots == 0) {
27     die "inspect_vm: no operating systems found";
28 }
29
30 for my $root (@roots) {
31     printf "Root device: %s\n", $root;
32
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);
40
41     # Mount up the disks, like guestfish -i.
42     #
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);
47     for my $mp (@mps) {
48         eval { $g->mount_ro ($mps{$mp}, $mp) };
49         if ($@) {
50             print "$@ (ignored)\n"
51         }
52     }
53
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;
60     }
61
62     # Unmount everything.
63     $g->umount_all ()
64 }