3 # Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
23 use Sys::Guestfs::Lib qw(open_guest get_partitions resolve_windows_path
24 inspect_all_partitions inspect_partition
25 inspect_operating_systems mount_operating_system inspect_in_detail);
30 use Locale::TextDomain 'libguestfs';
33 eval "use YAML::Any;";
39 virt-inspector - Display OS version, kernel, drivers, mount points, applications, etc. in a virtual machine
43 virt-inspector [--connect URI] domname
45 virt-inspector guest.img [guest.img ...]
49 B<virt-inspector> examines a virtual machine and tries to determine
50 the version of the OS, the kernel version, what drivers are installed,
51 whether the virtual machine is fully virtualized (FV) or
52 para-virtualized (PV), what applications are installed and more.
54 Virt-inspector can produce output in several formats, including a
55 readable text report, and XML for feeding into other programs.
57 In the normal usage, use C<virt-inspector domname> where C<domname> is
58 the libvirt domain (see: C<virsh list --all>).
60 You can also run virt-inspector directly on disk images from a single
61 virtual machine. Use C<virt-inspector guest.img>. In rare cases a
62 domain has several block devices, in which case you should list them
63 one after another, with the first corresponding to the guest's
64 C</dev/sda>, the second to the guest's C</dev/sdb> and so on.
66 Virt-inspector can only inspect and report upon I<one domain at a
67 time>. To inspect several virtual machines, you have to run
68 virt-inspector several times (for example, from a shell script
71 Because virt-inspector needs direct access to guest images, it won't
72 normally work over remote libvirt connections.
92 Display version number and exit.
98 =item B<--connect URI> | B<-c URI>
100 If using libvirt, connect to the given I<URI>. If omitted,
101 then we connect to the default libvirt hypervisor.
103 Libvirt is only used if you specify a C<domname> on the
104 command line. If you specify guest block devices directly,
105 then libvirt is not used at all.
113 The following options select the output format. Use only one of them.
114 The default is a readable text report.
118 =item B<--text> (default)
124 Produce no output at all.
128 If you select I<--xml> then you get XML output which can be fed
133 If you select I<--yaml> then you get YAML output which can be fed
138 If you select I<--perl> then you get Perl structures output which
139 can be used directly in another Perl program.
145 If you select I<--fish> then we print a L<guestfish(1)> command
146 line which will automatically mount up the filesystems on the
147 correct mount points. Try this for example:
149 guestfish $(virt-inspector --fish guest.img)
151 I<--ro-fish> is the same, but the I<--ro> option is passed to
152 guestfish so that the filesystems are mounted read-only.
156 In "query mode" we answer common questions about the guest, such
157 as whether it is fullvirt or needs a Xen hypervisor to run.
159 See section I<QUERY MODE> below.
163 my $windows_registry;
165 =item B<--windows-registry>
167 If this item is passed, I<and> the guest is Windows, I<and> the
168 external program C<reged> is available (see SEE ALSO section), then we
169 attempt to parse the Windows registry. This allows much more
170 information to be gathered for Windows guests.
172 This is quite an expensive and slow operation, so we don't do it by
179 GetOptions ("help|?" => \$help,
180 "version" => \$version,
181 "connect|c=s" => \$uri,
182 "text" => sub { $output = "text" },
183 "none" => sub { $output = "none" },
184 "xml" => sub { $output = "xml" },
185 "yaml" => sub { $output = "yaml" },
186 "perl" => sub { $output = "perl" },
187 "fish" => sub { $output = "fish" },
188 "guestfish" => sub { $output = "fish" },
189 "ro-fish" => sub { $output = "ro-fish" },
190 "ro-guestfish" => sub { $output = "ro-fish" },
191 "query" => sub { $output = "query" },
192 "windows-registry" => \$windows_registry,
194 pod2usage (1) if $help;
196 my $g = Sys::Guestfs->new ();
197 my %h = $g->version ();
198 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
201 pod2usage (__"virt-inspector: no image or VM names given") if @ARGV == 0;
204 $rw = 1 if $output eq "fish";
209 ($g, $conn, $dom, @images) =
210 open_guest (\@ARGV, rw => $rw, address => $uri);
213 ($g, $conn, $dom, @images) =
214 open_guest (\@ARGV, rw => $rw);
223 Linux (distro + version)
227 +--- Filesystems ---------- Installed apps --- Kernel & drivers
228 ----------- -------------- ----------------
229 mount point => device List of apps Extra information
230 mount point => device and versions about kernel(s)
233 (plus lots of extra information
234 about each filesystem)
236 The output of virt-inspector is a complex two-level data structure.
238 At the top level is a list of the operating systems installed on the
239 guest. (For the vast majority of guests, only a single OS is
240 installed.) The data returned for the OS includes the name (Linux,
241 Windows), the distribution and version.
243 The diagram above shows what we return for each OS.
245 With the I<--xml> option the output is mapped into an XML document.
246 There is a RELAX-NG schema for this XML in the file
247 I<virt-inspector.rng> which normally ships with virt-inspector, or can
248 be found in the source.
250 With the I<--fish> or I<--ro-fish> option the mount points are mapped to
251 L<guestfish(1)> command line parameters, so that you can go in
252 afterwards and inspect the guest with everything mounted in the
253 right place. For example:
255 guestfish $(virt-inspector --ro-fish guest.img)
256 ==> guestfish --ro -a guest.img -m /dev/VG/LV:/ -m /dev/sda1:/boot
260 # List of possible filesystems.
261 my @partitions = get_partitions ($g);
263 # Now query each one to build up a picture of what's in it.
265 inspect_all_partitions ($g, \@partitions,
266 use_windows_registry => $windows_registry);
268 #print "fses -----------\n";
269 #print Dumper(\%fses);
271 my $oses = inspect_operating_systems ($g, \%fses);
273 #print "oses -----------\n";
274 #print Dumper($oses);
276 # Mount up the disks so we can check for applications
277 # and kernels. Skip this if the output is "*fish" because
278 # we don't need to know.
280 if ($output !~ /.*fish$/) {
282 foreach $root_dev (sort keys %$oses) {
283 my $os = $oses->{$root_dev};
284 mount_operating_system ($g, $os);
285 inspect_in_detail ($g, $os);
290 #----------------------------------------------------------------------
293 if ($output eq "fish" || $output eq "ro-fish") {
294 my @osdevs = keys %$oses;
295 # This only works if there is a single OS.
296 die __"--fish output is only possible with a single OS\n" if @osdevs != 1;
298 my $root_dev = $osdevs[0];
300 if ($output eq "ro-fish") {
304 print "-a $_ " foreach @images;
306 my $mounts = $oses->{$root_dev}->{mounts};
307 # Have to mount / first. Luckily '/' is early in the ASCII
308 # character set, so this should be OK.
309 foreach (sort keys %$mounts) {
310 print "-m $mounts->{$_}:$_ " if $_ ne "swap" && $_ ne "none";
316 elsif ($output eq "perl") {
317 print Dumper(%$oses);
321 elsif ($output eq "yaml") {
322 die __"virt-inspector: no YAML support\n"
323 unless exists $INC{"YAML/Any.pm"};
328 # Plain text output (the default).
329 elsif ($output eq "text") {
334 elsif ($output eq "xml") {
339 elsif ($output eq "query") {
345 output_text_os ($oses->{$_}) foreach sort keys %$oses;
352 print $os->{os}, " " if exists $os->{os};
353 print $os->{distro}, " " if exists $os->{distro};
354 print $os->{arch}, " " if exists $os->{arch};
355 print $os->{major_version} if exists $os->{major_version};
356 print ".", $os->{minor_version} if exists $os->{minor_version};
358 print "on ", $os->{root_device}, ":\n";
360 print __" Mountpoints:\n";
361 my $mounts = $os->{mounts};
362 foreach (sort keys %$mounts) {
363 printf " %-30s %s\n", $mounts->{$_}, $_
366 print __" Filesystems:\n";
367 my $filesystems = $os->{filesystems};
368 foreach (sort keys %$filesystems) {
370 print " label: $filesystems->{$_}{label}\n"
371 if exists $filesystems->{$_}{label};
372 print " UUID: $filesystems->{$_}{uuid}\n"
373 if exists $filesystems->{$_}{uuid};
374 print " type: $filesystems->{$_}{fstype}\n"
375 if exists $filesystems->{$_}{fstype};
376 print " content: $filesystems->{$_}{content}\n"
377 if exists $filesystems->{$_}{content};
380 if (exists $os->{modprobe_aliases}) {
381 my %aliases = %{$os->{modprobe_aliases}};
382 my @keys = sort keys %aliases;
384 print __" Modprobe aliases:\n";
386 printf " %-30s %s\n", $_, $aliases{$_}->{modulename}
391 if (exists $os->{initrd_modules}) {
392 my %modvers = %{$os->{initrd_modules}};
393 my @keys = sort keys %modvers;
395 print __" Initrd modules:\n";
397 my @modules = @{$modvers{$_}};
399 print " $_\n" foreach @modules;
404 print __" Applications:\n";
405 my @apps = @{$os->{apps}};
407 print " $_->{name} $_->{version}\n"
410 if ($os->{kernels}) {
411 print __" Kernels:\n";
412 my @kernels = @{$os->{kernels}};
414 print " $_->{version} ($_->{arch})\n";
415 my @modules = @{$_->{modules}};
422 if (exists $os->{root}->{registry}) {
423 print __" Windows Registry entries:\n";
424 # These are just lumps of text - dump them out.
425 foreach (@{$os->{root}->{registry}}) {
433 my $xml = new XML::Writer(DATA_MODE => 1, DATA_INDENT => 2);
435 $xml->startTag("operatingsystems");
436 output_xml_os ($oses->{$_}, $xml) foreach sort keys %$oses;
437 $xml->endTag("operatingsystems");
446 $xml->startTag("operatingsystem");
448 foreach ( [ "name" => "os" ],
449 [ "distro" => "distro" ],
450 [ "arch" => "arch" ],
451 [ "major_version" => "major_version" ],
452 [ "minor_version" => "minor_version" ],
453 [ "package_format" => "package_format" ],
454 [ "package_management" => "package_management" ],
455 [ "root" => "root_device" ] ) {
456 $xml->dataElement($_->[0], $os->{$_->[1]}) if exists $os->{$_->[1]};
459 $xml->startTag("mountpoints");
460 my $mounts = $os->{mounts};
461 foreach (sort keys %$mounts) {
462 $xml->dataElement("mountpoint", $_, "dev" => $mounts->{$_});
464 $xml->endTag("mountpoints");
466 $xml->startTag("filesystems");
467 my $filesystems = $os->{filesystems};
468 foreach (sort keys %$filesystems) {
469 $xml->startTag("filesystem", "dev" => $_);
471 foreach my $field ( [ "label" => "label" ],
472 [ "uuid" => "uuid" ],
473 [ "type" => "fstype" ],
474 [ "content" => "content" ],
475 [ "spec" => "spec" ] ) {
476 $xml->dataElement($field->[0], $filesystems->{$_}{$field->[1]})
477 if exists $filesystems->{$_}{$field->[1]};
480 $xml->endTag("filesystem");
482 $xml->endTag("filesystems");
484 if (exists $os->{modprobe_aliases}) {
485 my %aliases = %{$os->{modprobe_aliases}};
486 my @keys = sort keys %aliases;
488 $xml->startTag("modprobealiases");
490 $xml->startTag("alias", "device" => $_);
492 foreach my $field ( [ "modulename" => "modulename" ],
493 [ "augeas" => "augeas" ],
494 [ "file" => "file" ] ) {
495 $xml->dataElement($field->[0], $aliases{$_}->{$field->[1]});
498 $xml->endTag("alias");
500 $xml->endTag("modprobealiases");
504 if (exists $os->{initrd_modules}) {
505 my %modvers = %{$os->{initrd_modules}};
506 my @keys = sort keys %modvers;
508 $xml->startTag("initrds");
510 my @modules = @{$modvers{$_}};
511 $xml->startTag("initrd", "version" => $_);
512 $xml->dataElement("module", $_) foreach @modules;
513 $xml->endTag("initrd");
515 $xml->endTag("initrds");
519 $xml->startTag("applications");
520 my @apps = @{$os->{apps}};
522 $xml->startTag("application");
523 $xml->dataElement("name", $_->{name});
524 $xml->dataElement("epoch", $_->{epoch}) if defined $_->{epoch};
525 $xml->dataElement("version", $_->{version});
526 $xml->dataElement("release", $_->{release});
527 $xml->dataElement("arch", $_->{arch});
528 $xml->endTag("application");
530 $xml->endTag("applications");
532 if(defined($os->{boot}) && defined($os->{boot}->{configs})) {
533 my $default = $os->{boot}->{default};
534 my $configs = $os->{boot}->{configs};
536 $xml->startTag("boot");
537 for(my $i = 0; $i < scalar(@$configs); $i++) {
538 my $config = $configs->[$i];
541 push(@attrs, ("default" => 1)) if($default == $i);
542 $xml->startTag("config", @attrs);
543 $xml->dataElement("title", $config->{title});
544 $xml->dataElement("kernel", $config->{kernel}->{version})
545 if(defined($config->{kernel}));
546 $xml->dataElement("cmdline", $config->{cmdline})
547 if(defined($config->{cmdline}));
548 $xml->endTag("config");
550 $xml->endTag("boot");
553 if ($os->{kernels}) {
554 $xml->startTag("kernels");
555 my @kernels = @{$os->{kernels}};
557 $xml->startTag("kernel",
558 "version" => $_->{version},
559 "arch" => $_->{arch});
560 $xml->startTag("modules");
561 my @modules = @{$_->{modules}};
563 $xml->dataElement("module", $_);
565 $xml->endTag("modules");
566 $xml->dataElement("path", $_->{path}) if(defined($_->{path}));
567 $xml->dataElement("package", $_->{package}) if(defined($_->{package}));
568 $xml->endTag("kernel");
570 $xml->endTag("kernels");
573 if (exists $os->{root}->{registry}) {
574 $xml->startTag("windowsregistryentries");
575 # These are just lumps of text - dump them out.
576 foreach (@{$os->{root}->{registry}}) {
577 $xml->dataElement("windowsregistryentry", $_);
579 $xml->endTag("windowsregistryentries");
582 $xml->endTag("operatingsystem");
587 When you use C<virt-inspector --query>, the output is a series of
595 (each answer is usually C<yes> or C<no>, or the line is completely
596 missing if we could not determine the answer at all).
598 If the guest is multiboot, you can get apparently conflicting answers
599 (eg. C<windows=yes> and C<linux=yes>, or a guest which is both
600 fullvirt and has a Xen PV kernel). This is normal, and just means
601 that the guest can do both things, although it might require operator
602 intervention such as selecting a boot option when the guest is
605 This section describes the full range of answers possible.
613 output_query_windows ();
614 output_query_linux ();
615 output_query_rhel ();
616 output_query_fedora ();
617 output_query_debian ();
618 output_query_fullvirt ();
619 output_query_xen_domU_kernel ();
620 output_query_xen_pv_drivers ();
621 output_query_virtio_drivers ();
622 output_query_kernel_arch ();
623 output_query_userspace_arch ();
626 =item windows=(yes|no)
628 Answer C<yes> if Microsoft Windows is installed in the guest.
632 sub output_query_windows
635 foreach my $os (keys %$oses) {
636 $windows="yes" if $oses->{$os}->{os} eq "windows";
638 print "windows=$windows\n";
643 Answer C<yes> if a Linux kernel is installed in the guest.
647 sub output_query_linux
650 foreach my $os (keys %$oses) {
651 $linux="yes" if $oses->{$os}->{os} eq "linux";
653 print "linux=$linux\n";
658 Answer C<yes> if the guest contains Red Hat Enterprise Linux.
662 sub output_query_rhel
665 foreach my $os (keys %$oses) {
666 $rhel="yes" if ($oses->{$os}->{os} eq "linux" &&
667 $oses->{$os}->{distro} eq "rhel");
669 print "rhel=$rhel\n";
672 =item fedora=(yes|no)
674 Answer C<yes> if the guest contains the Fedora Linux distribution.
678 sub output_query_fedora
681 foreach my $os (keys %$oses) {
682 $fedora="yes" if $oses->{$os}->{os} eq "linux" && $oses->{$os}->{distro} eq "fedora";
684 print "fedora=$fedora\n";
687 =item debian=(yes|no)
689 Answer C<yes> if the guest contains the Debian Linux distribution.
693 sub output_query_debian
696 foreach my $os (keys %$oses) {
697 $debian="yes" if $oses->{$os}->{os} eq "linux" && $oses->{$os}->{distro} eq "debian";
699 print "debian=$debian\n";
702 =item fullvirt=(yes|no)
704 Answer C<yes> if there is at least one operating system kernel
705 installed in the guest which runs fully virtualized. Such a guest
706 would require a hypervisor which supports full system virtualization.
710 sub output_query_fullvirt
712 # The assumption is full-virt, unless all installed kernels
713 # are identified as paravirt.
714 # XXX Fails on Windows guests.
715 foreach my $os (keys %$oses) {
716 foreach my $kernel (@{$oses->{$os}->{kernels}}) {
717 my $is_pv = $kernel->{version} =~ m/xen/;
719 print "fullvirt=yes\n";
724 print "fullvirt=no\n";
727 =item xen_domU_kernel=(yes|no)
729 Answer C<yes> if there is at least one Linux kernel installed in
730 the guest which is compiled as a Xen DomU (a Xen paravirtualized
735 sub output_query_xen_domU_kernel
737 foreach my $os (keys %$oses) {
738 foreach my $kernel (@{$oses->{$os}->{kernels}}) {
739 my $is_xen = $kernel->{version} =~ m/xen/;
741 print "xen_domU_kernel=yes\n";
746 print "xen_domU_kernel=no\n";
749 =item xen_pv_drivers=(yes|no)
751 Answer C<yes> if the guest has Xen paravirtualized drivers installed
752 (usually the kernel itself will be fully virtualized, but the PV
753 drivers have been installed by the administrator for performance
758 sub output_query_xen_pv_drivers
760 foreach my $os (keys %$oses) {
761 foreach my $kernel (@{$oses->{$os}->{kernels}}) {
762 foreach my $module (@{$kernel->{modules}}) {
763 if ($module =~ m/xen-/) {
764 print "xen_pv_drivers=yes\n";
770 print "xen_pv_drivers=no\n";
773 =item virtio_drivers=(yes|no)
775 Answer C<yes> if the guest has virtio paravirtualized drivers
776 installed. Virtio drivers are commonly used to improve the
781 sub output_query_virtio_drivers
783 foreach my $os (keys %$oses) {
784 foreach my $kernel (@{$oses->{$os}->{kernels}}) {
785 foreach my $module (@{$kernel->{modules}}) {
786 if ($module =~ m/virtio_/) {
787 print "virtio_drivers=yes\n";
793 print "virtio_drivers=no\n";
796 =item userspace_arch=(x86_64|...)
798 Print the architecture of userspace.
800 NB. For multi-boot VMs this can print several lines.
804 sub output_query_userspace_arch
808 foreach my $os (keys %$oses) {
809 $arches{$oses->{$os}->{arch}} = 1 if exists $oses->{$os}->{arch};
812 foreach (sort keys %arches) {
813 print "userspace_arch=$_\n";
817 =item kernel_arch=(x86_64|...)
819 Print the architecture of the kernel.
821 NB. For multi-boot VMs this can print several lines.
825 sub output_query_kernel_arch
829 foreach my $os (keys %$oses) {
830 foreach my $kernel (@{$oses->{$os}->{kernels}}) {
831 $arches{$kernel->{arch}} = 1 if exists $kernel->{arch};
835 foreach (sort keys %arches) {
836 print "kernel_arch=$_\n";
847 L<Sys::Guestfs::Lib(3)>,
849 L<http://libguestfs.org/>.
851 For Windows registry parsing we require the C<reged> program
852 from L<http://home.eunet.no/~pnordahl/ntpasswd/>.
856 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
858 Matthew Booth L<mbooth@redhat.com>
862 Copyright (C) 2009 Red Hat Inc.
864 This program is free software; you can redistribute it and/or modify
865 it under the terms of the GNU General Public License as published by
866 the Free Software Foundation; either version 2 of the License, or
867 (at your option) any later version.
869 This program is distributed in the hope that it will be useful,
870 but WITHOUT ANY WARRANTY; without even the implied warranty of
871 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
872 GNU General Public License for more details.
874 You should have received a copy of the GNU General Public License
875 along with this program; if not, write to the Free Software
876 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.