24501fb32647b5d17c6e68dccf2fa0eee0bc24cf
[libguestfs.git] / inspector / virt-inspector
1 #!/usr/bin/perl -w
2 # virt-inspector
3 # Copyright (C) 2010 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 use warnings;
20 use strict;
21
22 use Sys::Guestfs;
23 use Sys::Guestfs::Lib qw(open_guest);
24 use Pod::Usage;
25 use Getopt::Long;
26 use File::Temp qw/tempfile/;
27 use File::Basename;
28 use XML::Writer;
29 use Locale::TextDomain 'libguestfs';
30
31 =encoding utf8
32
33 =head1 NAME
34
35 virt-inspector - Display operating system version and other information about a virtual machine
36
37 =head1 SYNOPSIS
38
39  virt-inspector [--connect URI] domname
40
41  virt-inspector guest.img [guest.img ...]
42
43 =head1 DESCRIPTION
44
45 B<virt-inspector> examines a virtual machine or disk image and tries
46 to determine the version of the operating system and other information
47 about the virtual machine.
48
49 Virt-inspector produces XML output for feeding into other programs.
50
51 In the normal usage, use C<virt-inspector domname> where C<domname> is
52 the libvirt domain (see: C<virsh list --all>).
53
54 You can also run virt-inspector directly on disk images from a single
55 virtual machine.  Use C<virt-inspector guest.img>.  In rare cases a
56 domain has several block devices, in which case you should list them
57 one after another, with the first corresponding to the guest's
58 C</dev/sda>, the second to the guest's C</dev/sdb> and so on.
59
60 Virt-inspector can only inspect and report upon I<one domain at a
61 time>.  To inspect several virtual machines, you have to run
62 virt-inspector several times (for example, from a shell script
63 for-loop).
64
65 Because virt-inspector needs direct access to guest images, it won't
66 normally work over remote libvirt connections.
67
68 =head1 OPTIONS
69
70 =over 4
71
72 =cut
73
74 my $help;
75
76 =item B<--help>
77
78 Display brief help.
79
80 =cut
81
82 my $version;
83
84 =item B<--version>
85
86 Display version number and exit.
87
88 =cut
89
90 my $uri;
91
92 =item B<--connect URI> | B<-c URI>
93
94 If using libvirt, connect to the given I<URI>.  If omitted,
95 then we connect to the default libvirt hypervisor.
96
97 Libvirt is only used if you specify a C<domname> on the
98 command line.  If you specify guest block devices directly,
99 then libvirt is not used at all.
100
101 =cut
102
103 my $format;
104
105 =item B<--format> raw
106
107 Specify the format of disk images given on the command line.  If this
108 is omitted then the format is autodetected from the content of the
109 disk image.
110
111 If disk images are requested from libvirt, then this program asks
112 libvirt for this information.  In this case, the value of the format
113 parameter is ignored.
114
115 If working with untrusted raw-format guest disk images, you should
116 ensure the format is always specified.
117
118 =back
119
120 =cut
121
122 GetOptions ("help|?" => \$help,
123             "version" => \$version,
124             "connect|c=s" => \$uri,
125             "format=s" => \$format,
126     ) or pod2usage (2);
127 pod2usage (1) if $help;
128 if ($version) {
129     my $g = Sys::Guestfs->new ();
130     my %h = $g->version ();
131     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
132     exit
133 }
134 pod2usage (__"virt-inspector: no image or VM names given") if @ARGV == 0;
135
136 my @args = (\@ARGV);
137 push @args, address => $uri if defined $uri;
138 push @args, format => $format if defined $format;
139 my $g = open_guest (@args);
140
141 $g->launch ();
142
143 my @roots = $g->inspect_os ();
144 if (@roots == 0) {
145     die __x("{prog}: No operating system could be detected inside this disk image.\n\nThis may be because the file is not a disk image, or is not a virtual machine\nimage, or because the OS type is not understood by libguestfs.\n\nIf you feel this is an error, please file a bug report including as much\ninformation about the disk image as possible.\n",
146             prog => basename ($0));
147 }
148
149 # Start the XML output.
150 my $xml = new XML::Writer (DATA_MODE => 1, DATA_INDENT => 2);
151
152 $xml->startTag ("operatingsystems");
153
154 my $root;
155 foreach $root (@roots) {
156     my %fses = $g->inspect_get_mountpoints ($root);
157     my @fses = sort { length $a <=> length $b } keys %fses;
158     foreach (@fses) {
159         $g->mount_ro ($fses{$_}, $_);
160     }
161
162     $xml->startTag ("operatingsystem");
163
164     # Basic OS fields.
165     $xml->dataElement (root => $root);
166
167     my ($s, $distro, $major_version);
168     $s = $g->inspect_get_type ($root);
169     $xml->dataElement (name => $s) if $s ne "unknown";
170     $s = $g->inspect_get_arch ($root);
171     $xml->dataElement (arch => $s) if $s ne "unknown";
172     $distro = $g->inspect_get_distro ($root);
173     $xml->dataElement (distro => $distro) if $distro ne "unknown";
174     $s = $g->inspect_get_product_name ($root);
175     $xml->dataElement (product_name => $s) if $s ne "unknown";
176     $major_version = $g->inspect_get_major_version ($root);
177     $xml->dataElement (major_version => $major_version);
178     $s = $g->inspect_get_minor_version ($root);
179     $xml->dataElement (minor_version => $s);
180
181     eval {
182         $s = $g->inspect_get_windows_systemroot ($root);
183         $xml->dataElement (windows_systemroot => $s);
184     };
185
186     # Mountpoints.
187     output_mountpoints ($root, \@fses, \%fses);
188
189     # Filesystems.
190     output_filesystems ($root);
191
192     # Package format / management and applications.
193     output_applications ($root, $distro, $major_version);
194
195     $xml->endTag("operatingsystem");
196
197     $g->umount_all ();
198 }
199
200 # End the XML output.
201 $xml->endTag ("operatingsystems");
202 $xml->end ();
203
204 sub output_mountpoints
205 {
206     local $_;
207     my $root = shift;
208     my $fskeys = shift;
209     my $fshash = shift;
210
211     $xml->startTag ("mountpoints");
212     foreach (@$fskeys) {
213         $xml->dataElement ("mountpoint", $_, dev => $fshash->{$_});
214     }
215     $xml->endTag ("mountpoints");
216 }
217
218 sub output_filesystems
219 {
220     local $_;
221     my $root = shift;
222
223     $xml->startTag ("filesystems");
224
225     my @fses = $g->inspect_get_filesystems ($root);
226     foreach (@fses) {
227         $xml->startTag ("filesystem", dev => $_);
228
229         eval {
230             my $type = $g->vfs_type ($_);
231             $xml->dataElement (type => $type)
232                 if defined $type && $type ne "";
233         };
234
235         eval {
236             my $label = $g->vfs_label ($_);
237             $xml->dataElement (label => $label)
238                 if defined $label && $label ne "";
239         };
240
241         eval {
242             my $uuid = $g->vfs_uuid ($_);
243             $xml->dataElement (uuid => $uuid)
244                 if defined $uuid && $uuid ne "";
245         };
246
247         $xml->endTag ("filesystem");
248     }
249
250     $xml->endTag ("filesystems");
251 }
252
253 sub output_applications
254 {
255     local $_;
256     my $root = shift;
257     my $distro = shift;
258     my $major_version = shift;
259
260     # Based on the distro, take a guess at the package format
261     # and package management.
262     my ($package_format, $package_management);
263     if (defined $distro) {
264         if ($distro eq "debian") {
265             $package_format = "dpkg";
266             $package_management = "apt";
267         }
268         elsif ($distro eq "fedora") {
269             $package_format = "rpm";
270             $package_management = "yum";
271         }
272         elsif ($distro =~ /redhat/ || $distro =~ /rhel/) {
273             if ($major_version >= 5) {
274                 $package_format = "rpm";
275                 $package_management = "yum";
276             } else {
277                 $package_format = "rpm";
278                 $package_management = "up2date";
279             }
280         }
281         # else unknown.
282     }
283
284     $xml->dataElement (package_format => $package_format)
285         if defined $package_format;
286     $xml->dataElement (package_management => $package_management)
287         if defined $package_management;
288
289     # Do we know how to get a list of applications?
290     if (defined $package_format) {
291         if ($package_format eq "rpm") {
292             output_applications_rpm ($root);
293         }
294         # else no we don't.
295     }
296 }
297
298 sub output_applications_rpm
299 {
300     local $_;
301     my $root = shift;
302
303     # Previous virt-inspector ran the 'rpm' program from the guest.
304     # This is insecure, and unnecessary because we can get the same
305     # information directly from the RPM database.
306
307     my @applications;
308
309     eval {
310         my ($fh, $filename) = tempfile (UNLINK => 1);
311         my $fddev = "/dev/fd/" . fileno ($fh);
312         $g->download ("/var/lib/rpm/Name", $fddev);
313         close $fh or die "close: $!";
314
315         # Read the database with the Berkeley DB dump tool.
316         my $cmd = "db_dump -p '$filename'";
317         open PIPE, "$cmd |" or die "close: $!";
318         while (<PIPE>) {
319             chomp;
320             last if /^HEADER=END$/;
321         }
322         while (<PIPE>) {
323             chomp;
324             last if /^DATA=END$/;
325
326             # First character on each data line is a space.
327             if (length $_ > 0 && substr ($_, 0, 1) eq ' ') {
328                 $_ = substr ($_, 1);
329             }
330             # Name should never contain non-printable chars.
331             die "name contains non-printable chars" if /\\/;
332             push @applications, $_;
333
334             $_ = <PIPE>; # discard value
335         }
336         close PIPE or die "close: $!";
337     };
338     if (!$@ && @applications > 0) {
339         @applications = sort @applications;
340         $xml->startTag ("applications");
341         foreach (@applications) {
342             $xml->startTag ("application");
343             $xml->dataElement (name => $_);
344             $xml->endTag ("application");
345         }
346         $xml->endTag ("applications");
347     }
348 }
349
350 =head1 SHELL QUOTING
351
352 Libvirt guest names can contain arbitrary characters, some of which
353 have meaning to the shell such as C<#> and space.  You may need to
354 quote or escape these characters on the command line.  See the shell
355 manual page L<sh(1)> for details.
356
357 =head1 SEE ALSO
358
359 L<guestfs(3)>,
360 L<guestfish(1)>,
361 L<Sys::Guestfs(3)>,
362 L<Sys::Guestfs::Lib(3)>,
363 L<Sys::Virt(3)>,
364 L<http://libguestfs.org/>.
365
366 =head1 AUTHORS
367
368 =over 4
369
370 =item *
371
372 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
373
374 =item *
375
376 Matthew Booth L<mbooth@redhat.com>
377
378 =back
379
380 =head1 COPYRIGHT
381
382 Copyright (C) 2010 Red Hat Inc.
383
384 This program is free software; you can redistribute it and/or modify
385 it under the terms of the GNU General Public License as published by
386 the Free Software Foundation; either version 2 of the License, or
387 (at your option) any later version.
388
389 This program is distributed in the hope that it will be useful,
390 but WITHOUT ANY WARRANTY; without even the implied warranty of
391 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
392 GNU General Public License for more details.
393
394 You should have received a copy of the GNU General Public License
395 along with this program; if not, write to the Free Software
396 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.