Fix non-srcdir builds: further fixes to OCaml build rules.
[libguestfs.git] / cat / virt-cat.pl
1 #!/usr/bin/perl -w
2 # virt-cat
3 # Copyright (C) 2009 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 get_partitions resolve_windows_path
24   inspect_all_partitions inspect_partition
25   inspect_operating_systems mount_operating_system);
26 use Pod::Usage;
27 use Getopt::Long;
28 use Data::Dumper;
29 use File::Temp qw/tempdir/;
30 use XML::Writer;
31 use Locale::TextDomain 'libguestfs';
32
33 =encoding utf8
34
35 =head1 NAME
36
37 virt-cat - Display a file in a virtual machine
38
39 =head1 SYNOPSIS
40
41  virt-cat [--options] domname file
42
43  virt-cat [--options] disk.img [disk.img ...] file
44
45 =head1 DESCRIPTION
46
47 C<virt-cat> is a command line tool to display the contents of C<file>
48 where C<file> exists in the named virtual machine (or disk image).
49
50 C<virt-cat> can be used to quickly view a single file.  For more
51 complex cases you should look at the L<guestfish(1)> tool.
52
53 =head1 EXAMPLES
54
55 Display C</etc/fstab> file from inside the libvirt VM called
56 C<mydomain>:
57
58  virt-cat mydomain /etc/fstab
59
60 List syslog messages from a VM:
61
62  virt-cat mydomain /var/log/messages | tail
63
64 Find out what packages were recently installed:
65
66  virt-cat mydomain /var/log/yum.log | tail
67
68 Find out who is logged on inside a virtual machine:
69
70  virt-cat mydomain /var/run/utmp > /tmp/utmp
71  who /tmp/utmp
72
73 or who was logged on:
74
75  virt-cat mydomain /var/log/wtmp > /tmp/wtmp
76  last -f /tmp/wtmp
77
78 =head1 OPTIONS
79
80 =over 4
81
82 =cut
83
84 my $help;
85
86 =item B<--help>
87
88 Display brief help.
89
90 =cut
91
92 my $version;
93
94 =item B<--version>
95
96 Display version number and exit.
97
98 =cut
99
100 my $uri;
101
102 =item B<--connect URI> | B<-c URI>
103
104 If using libvirt, connect to the given I<URI>.  If omitted, then we
105 connect to the default libvirt hypervisor.
106
107 If you specify guest block devices directly, then libvirt is not used
108 at all.
109
110 =back
111
112 =cut
113
114 GetOptions ("help|?" => \$help,
115             "version" => \$version,
116             "connect|c=s" => \$uri,
117     ) or pod2usage (2);
118 pod2usage (1) if $help;
119 if ($version) {
120     my $g = Sys::Guestfs->new ();
121     my %h = $g->version ();
122     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
123     exit
124 }
125
126 pod2usage (__"virt-cat: no image, VM names or filenames to cat given")
127     if @ARGV <= 1;
128
129 my $filename = pop @ARGV;
130
131 my $g;
132 if ($uri) {
133     $g = open_guest (\@ARGV, address => $uri);
134 } else {
135     $g = open_guest (\@ARGV);
136 }
137
138 $g->launch ();
139 $g->wait_ready ();
140
141 # List of possible filesystems.
142 my @partitions = get_partitions ($g);
143
144 # Now query each one to build up a picture of what's in it.
145 my %fses =
146     inspect_all_partitions ($g, \@partitions,
147       use_windows_registry => 0);
148
149 my $oses = inspect_operating_systems ($g, \%fses);
150
151 my @roots = keys %$oses;
152 die __"no root device found in this operating system image" if @roots == 0;
153 die __"multiboot operating systems are not supported by virt-cat" if @roots > 1;
154 my $root_dev = $roots[0];
155
156 my $os = $oses->{$root_dev};
157 mount_operating_system ($g, $os);
158
159 # Allow this to fail in case eg. the file does not exist.
160 # NB: https://bugzilla.redhat.com/show_bug.cgi?id=501888
161 print $g->download($filename, "/dev/stdout");
162
163 =head1 SEE ALSO
164
165 L<guestfs(3)>,
166 L<guestfish(1)>,
167 L<Sys::Guestfs(3)>,
168 L<Sys::Guestfs::Lib(3)>,
169 L<Sys::Virt(3)>,
170 L<http://libguestfs.org/>.
171
172 =head1 AUTHOR
173
174 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
175
176 =head1 COPYRIGHT
177
178 Copyright (C) 2009 Red Hat Inc.
179
180 This program is free software; you can redistribute it and/or modify
181 it under the terms of the GNU General Public License as published by
182 the Free Software Foundation; either version 2 of the License, or
183 (at your option) any later version.
184
185 This program is distributed in the hope that it will be useful,
186 but WITHOUT ANY WARRANTY; without even the implied warranty of
187 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
188 GNU General Public License for more details.
189
190 You should have received a copy of the GNU General Public License
191 along with this program; if not, write to the Free Software
192 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.