summaryrefslogtreecommitdiffstats
path: root/inspector/virt-inspector
blob: 03b01b38ff5df54f39ced4c439a2e70ffba244e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/perl -w
# virt-inspector
# Copyright (C) 2010 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use warnings;
use strict;

use Sys::Guestfs;
use Sys::Guestfs::Lib qw(open_guest);
use Pod::Usage;
use Getopt::Long;
use File::Temp qw/tempfile/;
use File::Basename;
use XML::Writer;
use Locale::TextDomain 'libguestfs';

=encoding utf8

=head1 NAME

virt-inspector - Display operating system version and other information about a virtual machine

=head1 SYNOPSIS

 virt-inspector [--connect URI] domname

 virt-inspector guest.img [guest.img ...]

=head1 DESCRIPTION

B<virt-inspector> examines a virtual machine or disk image and tries
to determine the version of the operating system and other information
about the virtual machine.

Virt-inspector produces XML output for feeding into other programs.

In the normal usage, use C<virt-inspector domname> where C<domname> is
the libvirt domain (see: C<virsh list --all>).

You can also run virt-inspector directly on disk images from a single
virtual machine.  Use C<virt-inspector guest.img>.  In rare cases a
domain has several block devices, in which case you should list them
one after another, with the first corresponding to the guest's
C</dev/sda>, the second to the guest's C</dev/sdb> and so on.

Virt-inspector can only inspect and report upon I<one domain at a
time>.  To inspect several virtual machines, you have to run
virt-inspector several times (for example, from a shell script
for-loop).

Because virt-inspector needs direct access to guest images, it won't
normally work over remote libvirt connections.

=head1 OPTIONS

=over 4

=cut

my $help;

=item B<--help>

Display brief help.

=cut

my $version;

=item B<--version>

Display version number and exit.

=cut

my $uri;

=item B<--connect URI> | B<-c URI>

If using libvirt, connect to the given I<URI>.  If omitted,
then we connect to the default libvirt hypervisor.

Libvirt is only used if you specify a C<domname> on the
command line.  If you specify guest block devices directly,
then libvirt is not used at all.

=cut

my $format;

=item B<--format> raw

Specify the format of disk images given on the command line.  If this
is omitted then the format is autodetected from the content of the
disk image.

If disk images are requested from libvirt, then this program asks
libvirt for this information.  In this case, the value of the format
parameter is ignored.

If working with untrusted raw-format guest disk images, you should
ensure the format is always specified.

=back

=cut

GetOptions ("help|?" => \$help,
            "version" => \$version,
            "connect|c=s" => \$uri,
            "format=s" => \$format,
    ) or pod2usage (2);
pod2usage (1) if $help;
if ($version) {
    my $g = Sys::Guestfs->new ();
    my %h = $g->version ();
    print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
    exit
}
pod2usage (__"virt-inspector: no image or VM names given") if @ARGV == 0;

my @args = (\@ARGV);
push @args, address => $uri if defined $uri;
push @args, format => $format if defined $format;
my $g = open_guest (@args);

$g->launch ();

my @roots = $g->inspect_os ();
if (@roots == 0) {
    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",
            prog => basename ($0));
}

# Start the XML output.
my $xml = new XML::Writer (DATA_MODE => 1, DATA_INDENT => 2);

$xml->startTag ("operatingsystems");

my $root;
foreach $root (@roots) {
    my %fses = $g->inspect_get_mountpoints ($root);
    my @fses = sort { length $a <=> length $b } keys %fses;
    foreach (@fses) {
        $g->mount_ro ($fses{$_}, $_);
    }

    $xml->startTag ("operatingsystem");

    # Basic OS fields.
    $xml->dataElement (root => canonicalize ($root));

    my ($s, $distro, $major_version);
    $s = $g->inspect_get_type ($root);
    $xml->dataElement (name => $s) if $s ne "unknown";
    $s = $g->inspect_get_arch ($root);
    $xml->dataElement (arch => $s) if $s ne "unknown";
    $distro = $g->inspect_get_distro ($root);
    $xml->dataElement (distro => $distro) if $distro ne "unknown";
    $s = $g->inspect_get_product_name ($root);
    $xml->dataElement (product_name => $s) if $s ne "unknown";
    $major_version = $g->inspect_get_major_version ($root);
    $xml->dataElement (major_version => $major_version);
    $s = $g->inspect_get_minor_version ($root);
    $xml->dataElement (minor_version => $s);

    eval {
        $s = $g->inspect_get_windows_systemroot ($root);
        $xml->dataElement (windows_systemroot => $s);
    };

    # Mountpoints.
    output_mountpoints ($root, \@fses, \%fses);

    # Filesystems.
    output_filesystems ($root);

    # Package format / management and applications.
    output_applications ($root, $distro, $major_version);

    $xml->endTag("operatingsystem");

    $g->umount_all ();
}

# End the XML output.
$xml->endTag ("operatingsystems");
$xml->end ();

sub output_mountpoints
{
    local $_;
    my $root = shift;
    my $fskeys = shift;
    my $fshash = shift;

    $xml->startTag ("mountpoints");
    foreach (@$fskeys) {
        $xml->dataElement ("mountpoint", $_,
                           dev => canonicalize ($fshash->{$_}));
    }
    $xml->endTag ("mountpoints");
}

sub output_filesystems
{
    local $_;
    my $root = shift;

    $xml->startTag ("filesystems");

    my @fses = $g->inspect_get_filesystems ($root);
    foreach (@fses) {
        $xml->startTag ("filesystem",
                        dev => canonicalize ($_));

        eval {
            my $type = $g->vfs_type ($_);
            $xml->dataElement (type => $type)
                if defined $type && $type ne "";
        };

        eval {
            my $label = $g->vfs_label ($_);
            $xml->dataElement (label => $label)
                if defined $label && $label ne "";
        };

        eval {
            my $uuid = $g->vfs_uuid ($_);
            $xml->dataElement (uuid => $uuid)
                if defined $uuid && $uuid ne "";
        };

        $xml->endTag ("filesystem");
    }

    $xml->endTag ("filesystems");
}

sub output_applications
{
    local $_;
    my $root = shift;
    my $distro = shift;
    my $major_version = shift;

    # Based on the distro, take a guess at the package format
    # and package management.
    my ($package_format, $package_management);
    if (defined $distro) {
        if ($distro eq "debian") {
            $package_format = "deb";
            $package_management = "apt";
        }
        elsif ($distro eq "fedora") {
            $package_format = "rpm";
            $package_management = "yum";
        }
        elsif ($distro =~ /redhat/ || $distro =~ /rhel/) {
            if ($major_version >= 5) {
                $package_format = "rpm";
                $package_management = "yum";
            } else {
                $package_format = "rpm";
                $package_management = "up2date";
            }
        }
        # else unknown.
    }

    $xml->dataElement (package_format => $package_format)
        if defined $package_format;
    $xml->dataElement (package_management => $package_management)
        if defined $package_management;

    # Do we know how to get a list of applications?
    if (defined $package_format) {
        if ($package_format eq "rpm") {
            output_applications_rpm ($root);
        }
        # else no we don't.
    }
}

sub output_applications_rpm
{
    local $_;
    my $root = shift;

    # Previous virt-inspector ran the 'rpm' program from the guest.
    # This is insecure, and unnecessary because we can get the same
    # information directly from the RPM database.

    my @applications;

    eval {
        my ($fh, $filename) = tempfile (UNLINK => 1);
        my $fddev = "/dev/fd/" . fileno ($fh);
        $g->download ("/var/lib/rpm/Name", $fddev);
        close $fh or die "close: $!";

        # Read the database with the Berkeley DB dump tool.
        my $cmd = "db_dump -p '$filename'";
        open PIPE, "$cmd |" or die "close: $!";
        while (<PIPE>) {
            chomp;
            last if /^HEADER=END$/;
        }
        while (<PIPE>) {
            chomp;
            last if /^DATA=END$/;

            # First character on each data line is a space.
            if (length $_ > 0 && substr ($_, 0, 1) eq ' ') {
                $_ = substr ($_, 1);
            }
            # Name should never contain non-printable chars.
            die "name contains non-printable chars" if /\\/;
            push @applications, $_;

            $_ = <PIPE>; # discard value
        }
        close PIPE or die "close: $!";
    };
    if (!$@ && @applications > 0) {
        @applications = sort @applications;
        $xml->startTag ("applications");
        foreach (@applications) {
            $xml->startTag ("application");
            $xml->dataElement (name => $_);
            $xml->endTag ("application");
        }
        $xml->endTag ("applications");
    }
}

# The reverse of device name translation, see
# BLOCK DEVICE NAMING in guestfs(3).
sub canonicalize
{
    local $_ = shift;

    if (m{^/dev/[hv]d([a-z]\d)$}) {
        return "/dev/sd$1";
    }
    $_;
}

=head1 SHELL QUOTING

Libvirt guest names can contain arbitrary characters, some of which
have meaning to the shell such as C<#> and space.  You may need to
quote or escape these characters on the command line.  See the shell
manual page L<sh(1)> for details.

=head1 SEE ALSO

L<guestfs(3)>,
L<guestfish(1)>,
L<Sys::Guestfs(3)>,
L<Sys::Guestfs::Lib(3)>,
L<Sys::Virt(3)>,
L<http://libguestfs.org/>.

=head1 AUTHORS

=over 4

=item *

Richard W.M. Jones L<http://people.redhat.com/~rjones/>

=item *

Matthew Booth L<mbooth@redhat.com>

=back

=head1 COPYRIGHT

Copyright (C) 2010 Red Hat Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.