3 # Copyright (C) 2010 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);
26 use File::Temp qw/tempfile/;
29 use Locale::TextDomain 'libguestfs';
35 virt-inspector - Display operating system version and other information about a virtual machine
39 virt-inspector [--connect URI] domname
41 virt-inspector guest.img [guest.img ...]
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.
49 Virt-inspector produces XML output for feeding into other programs.
51 In the normal usage, use C<virt-inspector domname> where C<domname> is
52 the libvirt domain (see: C<virsh list --all>).
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.
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
65 Because virt-inspector needs direct access to guest images, it won't
66 normally work over remote libvirt connections.
86 Display version number and exit.
92 =item B<--connect URI> | B<-c URI>
94 If using libvirt, connect to the given I<URI>. If omitted,
95 then we connect to the default libvirt hypervisor.
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.
105 =item B<--format> raw
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
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.
115 If working with untrusted raw-format guest disk images, you should
116 ensure the format is always specified.
122 GetOptions ("help|?" => \$help,
123 "version" => \$version,
124 "connect|c=s" => \$uri,
125 "format=s" => \$format,
127 pod2usage (1) if $help;
129 my $g = Sys::Guestfs->new ();
130 my %h = $g->version ();
131 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
134 pod2usage (__"virt-inspector: no image or VM names given") if @ARGV == 0;
137 push @args, address => $uri if defined $uri;
138 push @args, format => $format if defined $format;
139 my $g = open_guest (@args);
143 my @roots = $g->inspect_os ();
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));
151 The virt-inspector XML is described precisely in a RELAX NG schema
152 which is supplied with libguestfs. This section is just an overview.
154 The top-level element is E<lt>operatingsystemsE<gt>, and it contains
155 one or more E<lt>operatingsystemE<gt> elements. You would only see
156 more than one E<lt>operatingsystemE<gt> element if the virtual machine
157 is multi-boot, which is vanishingly rare in real world VMs.
159 =head2 E<lt>operatingsystemE<gt>
161 In the E<lt>operatingsystemE<gt> tag are various optional fields that
162 describe the operating system, its architecture, the descriptive
163 "product name" string, the type of OS and so on, as in this example:
167 <root>/dev/sda2</root>
170 <distro>windows</distro>
171 <product_name>Windows 7 Enterprise</product_name>
172 <major_version>6</major_version>
173 <minor_version>1</minor_version>
174 <windows_systemroot>/Windows</windows_systemroot>
176 These fields are derived from the libguestfs inspection API, and
177 you can find more details in L<guestfs(3)/INSPECTION>.
179 The E<lt>rootE<gt> element is the root filesystem device, but from the
180 point of view of libguestfs (block devices may have completely
181 different names inside the VM itself).
185 # Start the XML output.
186 my $xml = new XML::Writer (DATA_MODE => 1, DATA_INDENT => 2);
188 $xml->startTag ("operatingsystems");
191 foreach $root (@roots) {
192 # Note that output_applications requires the filesystems
194 my %fses = $g->inspect_get_mountpoints ($root);
195 my @fses = sort { length $a <=> length $b } keys %fses;
197 $g->mount_ro ($fses{$_}, $_);
200 $xml->startTag ("operatingsystem");
203 $xml->dataElement (root => canonicalize ($root));
206 $s = $g->inspect_get_type ($root);
207 $xml->dataElement (name => $s) if $s ne "unknown";
208 $s = $g->inspect_get_arch ($root);
209 $xml->dataElement (arch => $s) if $s ne "unknown";
210 $s = $g->inspect_get_distro ($root);
211 $xml->dataElement (distro => $s) if $s ne "unknown";
212 $s = $g->inspect_get_product_name ($root);
213 $xml->dataElement (product_name => $s) if $s ne "unknown";
214 $s = $g->inspect_get_major_version ($root);
215 $xml->dataElement (major_version => $s);
216 $s = $g->inspect_get_minor_version ($root);
217 $xml->dataElement (minor_version => $s);
218 $s = $g->inspect_get_package_format ($root);
219 $xml->dataElement (package_format => $s) if $s ne "unknown";
220 $s = $g->inspect_get_package_management ($root);
221 $xml->dataElement (package_management => $s) if $s ne "unknown";
224 $s = $g->inspect_get_windows_systemroot ($root);
225 $xml->dataElement (windows_systemroot => $s);
229 output_mountpoints ($root, \@fses, \%fses);
232 output_filesystems ($root);
234 # Package format / management and applications.
235 output_applications ($root);
237 $xml->endTag("operatingsystem");
242 # End the XML output.
243 $xml->endTag ("operatingsystems");
246 =head2 E<lt>mountpointsE<gt>
248 Un*x-like guests typically have multiple filesystems which are mounted
249 at various mountpoints, and these are described in the
250 E<lt>mountpointsE<gt> element which looks like this:
256 <mountpoint dev="/dev/vg_f13x64/lv_root">/</mountpoint>
257 <mountpoint dev="/dev/sda1">/boot</mountpoint>
260 As with E<lt>rootE<gt>, devices are from the point of view of
261 libguestfs, and may have completely different names inside the guest.
262 Only mountable filesystems appear in this list, not things like swap
267 sub output_mountpoints
274 $xml->startTag ("mountpoints");
276 $xml->dataElement ("mountpoint", $_,
277 dev => canonicalize ($fshash->{$_}));
279 $xml->endTag ("mountpoints");
282 =head2 E<lt>filesystemsE<gt>
284 E<lt>filesystemsE<gt> is like E<lt>mountpointsE<gt> but covers I<all>
285 filesystems belonging to the guest, including swap and empty
286 partitions. (In the rare case of a multi-boot guest, it covers
287 filesystems belonging to this OS or shared by this OS and other OSes).
289 You might see something like this:
295 <filesystem dev="/dev/vg_f13x64/lv_root">
297 <label>Fedora-13-x86_64</label>
298 <uuid>e6a4db1e-15c2-477b-ac2a-699181c396aa</uuid>
301 The optional elements within E<lt>filesystemE<gt> are the filesystem
302 type, the label, and the UUID.
306 sub output_filesystems
311 $xml->startTag ("filesystems");
313 my @fses = $g->inspect_get_filesystems ($root);
316 $xml->startTag ("filesystem",
317 dev => canonicalize ($_));
320 my $type = $g->vfs_type ($_);
321 $xml->dataElement (type => $type)
322 if defined $type && $type ne "";
326 my $label = $g->vfs_label ($_);
327 $xml->dataElement (label => $label)
328 if defined $label && $label ne "";
332 my $uuid = $g->vfs_uuid ($_);
333 $xml->dataElement (uuid => $uuid)
334 if defined $uuid && $uuid ne "";
337 $xml->endTag ("filesystem");
340 $xml->endTag ("filesystems");
343 =head2 E<lt>applicationsE<gt>
345 The related elements E<lt>package_formatE<gt>,
346 E<lt>package_managementE<gt> and E<lt>applicationsE<gt> describe
347 applications installed in the virtual machine. At the moment we are
348 only able to list RPMs and Debian packages installed, but in future we
349 will support other Linux distros and Windows.
351 E<lt>package_formatE<gt>, if present, describes the packaging
352 system used. Typical values would be C<rpm> and C<deb>.
354 E<lt>package_managementE<gt>, if present, describes the package
355 manager. Typical values include C<yum>, C<up2date> and C<apt>
357 E<lt>applicationsE<gt> lists the packages or applications
365 <name>coreutils</name>
366 <version>8.5</version>
370 The version and release fields may not be available for some types
371 guests. Other fields are possible, see
372 L<guestfs(3)/guestfs_inspect_list_applications>.
376 sub output_applications
381 my @apps = $g->inspect_list_applications ($root);
384 $xml->startTag ("applications");
386 $xml->startTag ("application");
387 $xml->dataElement (name => $_->{app_name});
388 $xml->dataElement (display_name => $_->{app_display_name})
389 if $_->{app_display_name} ne "";
390 $xml->dataElement (epoch => $_->{app_epoch})
391 if $_->{app_epoch} != 0;
392 $xml->dataElement (version => $_->{app_version})
393 if $_->{app_version} ne "";
394 $xml->dataElement (release => $_->{app_release})
395 if $_->{app_release} ne "";
396 $xml->dataElement (install_path => $_->{app_install_path})
397 if $_->{app_install_path} ne "";
398 $xml->dataElement (publisher => $_->{app_publisher})
399 if $_->{app_publisher} ne "";
400 $xml->dataElement (url => $_->{app_url})
401 if $_->{app_url} ne "";
402 $xml->dataElement (source_package => $_->{app_source_package})
403 if $_->{app_source_package} ne "";
404 $xml->dataElement (summary => $_->{app_summary})
405 if $_->{app_summary} ne "";
406 $xml->dataElement (description => $_->{app_description})
407 if $_->{app_description} ne "";
408 $xml->endTag ("application");
410 $xml->endTag ("applications");
414 # The reverse of device name translation, see
415 # BLOCK DEVICE NAMING in guestfs(3).
420 if (m{^/dev/[hv]d([a-z]\d)$}) {
428 You can use the XPath query language, and/or the xpath tool, in order
429 to select parts of the XML.
433 $ virt-inspector Guest | xpath //filesystems
437 <filesystem dev="/dev/vg_f13x64/lv_root">
441 $ virt-inspector Guest | \
442 xpath "string(//filesystem[@dev='/dev/sda1']/type)"
443 Query didn't return a nodeset. Value: ext4
447 Libvirt guest names can contain arbitrary characters, some of which
448 have meaning to the shell such as C<#> and space. You may need to
449 quote or escape these characters on the command line. See the shell
450 manual page L<sh(1)> for details.
457 L<Sys::Guestfs::Lib(3)>,
459 L<http://www.w3.org/TR/xpath/>,
460 L<http://libguestfs.org/>.
468 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
472 Matthew Booth L<mbooth@redhat.com>
478 Copyright (C) 2010 Red Hat Inc.
480 This program is free software; you can redistribute it and/or modify
481 it under the terms of the GNU General Public License as published by
482 the Free Software Foundation; either version 2 of the License, or
483 (at your option) any later version.
485 This program is distributed in the hope that it will be useful,
486 but WITHOUT ANY WARRANTY; without even the implied warranty of
487 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
488 GNU General Public License for more details.
490 You should have received a copy of the GNU General Public License
491 along with this program; if not, write to the Free Software
492 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.