virt-ls: Small fix to documentation.
[libguestfs.git] / tools / virt-ls
1 #!/usr/bin/perl -w
2 # virt-ls
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 Locale::TextDomain 'libguestfs';
29 use File::Temp qw/tempdir/;
30
31 =encoding utf8
32
33 =head1 NAME
34
35 virt-ls - List files in a virtual machine
36
37 =head1 SYNOPSIS
38
39  virt-ls [--options] domname directory
40
41  virt-ls [--options] disk.img [disk.img ...] directory
42
43 =head1 DESCRIPTION
44
45 C<virt-ls> is a command line tool to list the names of files in a
46 directory inside a virtual machine or disk image.
47
48 C<virt-ls> is just a simple wrapper around L<libguestfs(3)>
49 functionality.  For more complex cases you should look at the
50 L<guestfish(1)> tool.
51
52 C<virt-ls> can be used in one of three modes: simple, long and
53 recursive.  A simple listing is like the ordinary L<ls(1)> command:
54
55  $ virt-ls myguest /
56  bin
57  boot
58  [etc.]
59
60 With the C<-l> (C<--long>) option, C<virt-ls> shows more detail:
61
62  $ virt-ls -l myguest /
63  total 204
64  dr-xr-xr-x.   2 root root   4096 2009-08-25 19:06 bin
65  dr-xr-xr-x.   5 root root   3072 2009-08-25 19:06 boot
66  [etc.]
67
68 With the C<-R> (C<--recursive>) option, C<virt-ls> lists the
69 names of files and directories recursively:
70
71  $ virt-ls -R myguest /tmp
72  foo
73  foo/bar
74  [etc.]
75
76 You I<cannot> combine these options.  To do more complicated things,
77 use L<guestfish(1)>.
78
79 =head1 OPTIONS
80
81 =over 4
82
83 =cut
84
85 my $help;
86
87 =item B<--help>
88
89 Display brief help.
90
91 =cut
92
93 my $version;
94
95 =item B<--version>
96
97 Display version number and exit.
98
99 =cut
100
101 my $uri;
102
103 =item B<--connect URI> | B<-c URI>
104
105 If using libvirt, connect to the given I<URI>.  If omitted, then we
106 connect to the default libvirt hypervisor.
107
108 If you specify guest block devices directly, then libvirt is not used
109 at all.
110
111 =cut
112
113 my $format;
114
115 =item B<--format> raw
116
117 Specify the format of disk images given on the command line.  If this
118 is omitted then the format is autodetected from the content of the
119 disk image.
120
121 If disk images are requested from libvirt, then this program asks
122 libvirt for this information.  In this case, the value of the format
123 parameter is ignored.
124
125 If working with untrusted raw-format guest disk images, you should
126 ensure the format is always specified.
127
128 =cut
129
130 my $mode;
131
132 =item B<-l> | B<--long>
133
134 =item B<-R> | B<--recursive>
135
136 Select the mode.  With neither of these options, C<virt-ls>
137 produces a simple, flat list of the files in the named directory.
138
139 C<virt-ls -l> produces a "long listing", which shows more detail (just
140 like the plain C<ls -l> command).
141
142 C<virt-ls -R> produces a recursive list of files starting at the named
143 directory.  See the documentation for L<guestfs(3)/guestfs_find>
144 for precise details.
145
146 You cannot combine these options.
147
148 =back
149
150 =cut
151
152 sub set_mode_l
153 {
154     die __"virt-ls: cannot combine -l and -R options\n" if $mode;
155     $mode = "l";
156 }
157
158 sub set_mode_R
159 {
160     die __"virt-ls: cannot combine -l and -R options\n" if $mode;
161     $mode = "R";
162 }
163
164 GetOptions ("help|?" => \$help,
165             "version" => \$version,
166             "connect|c=s" => \$uri,
167             "format=s" => \$format,
168             "long|l" => \&set_mode_l,
169             "recursive|R" => \&set_mode_R,
170     ) or pod2usage (2);
171 pod2usage (1) if $help;
172 if ($version) {
173     my $g = Sys::Guestfs->new ();
174     my %h = $g->version ();
175     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
176     exit
177 }
178
179 pod2usage (__"virt-ls: no image, VM names or directory to list given")
180     if @ARGV <= 1;
181
182 my $directory = pop @ARGV;
183
184 my $g;
185 if ($uri) {
186     $g = open_guest (\@ARGV, address => $uri, format => $format);
187 } else {
188     $g = open_guest (\@ARGV, format => $format);
189 }
190
191 $g->launch ();
192
193 # List of possible filesystems.
194 my @partitions = get_partitions ($g);
195
196 # Now query each one to build up a picture of what's in it.
197 my %fses =
198     inspect_all_partitions ($g, \@partitions,
199       use_windows_registry => 0);
200
201 my $oses = inspect_operating_systems ($g, \%fses);
202
203 my @roots = keys %$oses;
204 die __"multiboot operating systems are not supported by virt-ls\n" if @roots > 1;
205 my $root_dev = $roots[0];
206
207 my $os = $oses->{$root_dev};
208 mount_operating_system ($g, $os);
209
210 unless ($mode) {
211     my @r = $g->ls ($directory);
212     print "$_\n" foreach @r;
213 } elsif ($mode eq "l") {
214     print ($g->ll ($directory));
215 } else { # $mode eq "R"
216     my $dir = tempdir (CLEANUP => 1);
217     $g->find0 ($directory, "$dir/find0");
218     open F, "$dir/find0" or die "$dir/find0: $!\n";
219     my $r;
220     my $line;
221     while (($r = read (F, $line, 1024)) > 0) {
222         $line =~ tr{\0}{\n};
223         print $line;
224     }
225     close F;
226 }
227
228 =head1 SHELL QUOTING
229
230 Libvirt guest names can contain arbitrary characters, some of which
231 have meaning to the shell such as C<#> and space.  You may need to
232 quote or escape these characters on the command line.  See the shell
233 manual page L<sh(1)> for details.
234
235 =head1 SEE ALSO
236
237 L<guestfs(3)>,
238 L<guestfish(1)>,
239 L<virt-cat(1)>,
240 L<virt-tar(1)>,
241 L<Sys::Guestfs(3)>,
242 L<Sys::Guestfs::Lib(3)>,
243 L<Sys::Virt(3)>,
244 L<http://libguestfs.org/>.
245
246 =head1 AUTHOR
247
248 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
249
250 =head1 COPYRIGHT
251
252 Copyright (C) 2009 Red Hat Inc.
253
254 This program is free software; you can redistribute it and/or modify
255 it under the terms of the GNU General Public License as published by
256 the Free Software Foundation; either version 2 of the License, or
257 (at your option) any later version.
258
259 This program is distributed in the hope that it will be useful,
260 but WITHOUT ANY WARRANTY; without even the implied warranty of
261 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
262 GNU General Public License for more details.
263
264 You should have received a copy of the GNU General Public License
265 along with this program; if not, write to the Free Software
266 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.