7e489a234f5ae888885c2f551eb2947697a1ead4
[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
30 =encoding utf8
31
32 =head1 NAME
33
34 virt-ls - List files in a virtual machine
35
36 =head1 SYNOPSIS
37
38  virt-ls [--options] domname directory
39
40  virt-ls [--options] disk.img [disk.img ...] directory
41
42 =head1 DESCRIPTION
43
44 C<virt-ls> is a command line tool to list the names of files in a
45 directory inside a virtual machine or disk image.
46
47 C<virt-ls> is just a simple wrapper around L<libguestfs(3)>
48 functionality.  For more complex cases you should look at the
49 L<guestfish(1)> tool.
50
51 C<virt-ls> can be used in one of three modes: simple, long and
52 recursive.  A simple listing is like the ordinary L<ls(1)> command:
53
54  $ virt-ls myguest /
55  bin
56  boot
57  [etc.]
58
59 With the C<-l> (C<--long>) option, C<virt-ls> shows more detail:
60
61  $ virt-ls -l myguest /
62  total 204
63  dr-xr-xr-x.   2 root root   4096 2009-08-25 19:06 bin
64  dr-xr-xr-x.   5 root root   3072 2009-08-25 19:06 boot
65  [etc.]
66
67 With the C<-R> (C<--recursive>) option, C<virt-ls> lists the
68 names of files and directories recursively:
69
70  $ virt-ls -R myguest /tmp
71  foo
72  foo/bar
73  [etc.]
74
75 You I<cannot> combine these options.  To do more complicated things,
76 use L<guestfish(1)>.
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 =cut
111
112 my $mode;
113
114 =item B<-l> | B<--long>
115
116 =item B<-R> | B<--recursive>
117
118 Select the mode.  With neither of these options, C<virt-ls>
119 produces a simple, flat list of the files in the named directory.
120
121 C<virt-ls -l> produces a "long listing", which shows more detail (just
122 like the plain C<ls -l> command).
123
124 C<virt-ls -R> produces a recursive list of files starting at the named
125 directory.  See the documentation for the C<guestfs_find> command
126 L<guestfs(3)> for precise details.
127
128 You cannot combine these options.
129
130 =back
131
132 =cut
133
134 sub set_mode_l
135 {
136     die __"virt-ls: cannot combine -l and -R options\n" if $mode;
137     $mode = "l";
138 }
139
140 sub set_mode_R
141 {
142     die __"virt-ls: cannot combine -l and -R options\n" if $mode;
143     $mode = "R";
144 }
145
146 GetOptions ("help|?" => \$help,
147             "version" => \$version,
148             "connect|c=s" => \$uri,
149             "long|l" => \&set_mode_l,
150             "recursive|R" => \&set_mode_R,
151     ) or pod2usage (2);
152 pod2usage (1) if $help;
153 if ($version) {
154     my $g = Sys::Guestfs->new ();
155     my %h = $g->version ();
156     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
157     exit
158 }
159
160 pod2usage (__"virt-ls: no image, VM names or directory to list given")
161     if @ARGV <= 1;
162
163 my $directory = pop @ARGV;
164
165 my $g;
166 if ($uri) {
167     $g = open_guest (\@ARGV, address => $uri);
168 } else {
169     $g = open_guest (\@ARGV);
170 }
171
172 $g->launch ();
173
174 # List of possible filesystems.
175 my @partitions = get_partitions ($g);
176
177 # Now query each one to build up a picture of what's in it.
178 my %fses =
179     inspect_all_partitions ($g, \@partitions,
180       use_windows_registry => 0);
181
182 my $oses = inspect_operating_systems ($g, \%fses);
183
184 my @roots = keys %$oses;
185 die __"no root device found in this operating system image\n" if @roots == 0;
186 die __"multiboot operating systems are not supported by virt-ls\n" if @roots > 1;
187 my $root_dev = $roots[0];
188
189 my $os = $oses->{$root_dev};
190 mount_operating_system ($g, $os);
191
192 my @r;
193 unless ($mode) {
194     @r = $g->ls ($directory);
195     print "$_\n" foreach @r;
196 } elsif ($mode eq "l") {
197     print ($g->ll ($directory));
198 } else { # $mode eq "R"
199     @r = $g->find ($directory);
200     print "$_\n" foreach @r;
201 }
202
203 =head1 SEE ALSO
204
205 L<guestfs(3)>,
206 L<guestfish(1)>,
207 L<virt-cat(1)>,
208 L<virt-tar(1)>,
209 L<Sys::Guestfs(3)>,
210 L<Sys::Guestfs::Lib(3)>,
211 L<Sys::Virt(3)>,
212 L<http://libguestfs.org/>.
213
214 =head1 AUTHOR
215
216 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
217
218 =head1 COPYRIGHT
219
220 Copyright (C) 2009 Red Hat Inc.
221
222 This program is free software; you can redistribute it and/or modify
223 it under the terms of the GNU General Public License as published by
224 the Free Software Foundation; either version 2 of the License, or
225 (at your option) any later version.
226
227 This program is distributed in the hope that it will be useful,
228 but WITHOUT ANY WARRANTY; without even the implied warranty of
229 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
230 GNU General Public License for more details.
231
232 You should have received a copy of the GNU General Public License
233 along with this program; if not, write to the Free Software
234 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.