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);
28 use Locale::TextDomain 'libguestfs';
29 use File::Temp qw/tempdir/;
35 virt-ls - List files in a virtual machine
39 virt-ls [--options] domname directory
41 virt-ls [--options] disk.img [disk.img ...] directory
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.
48 C<virt-ls> is just a simple wrapper around L<libguestfs(3)>
49 functionality. For more complex cases you should look at the
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:
60 With the C<-l> (C<--long>) option, C<virt-ls> shows more detail:
62 $ virt-ls -l myguest /
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
68 With the C<-R> (C<--recursive>) option, C<virt-ls> lists the
69 names of files and directories recursively:
71 $ virt-ls -R myguest /tmp
76 You I<cannot> combine these options. To do more complicated things,
97 Display version number and exit.
103 =item B<--connect URI> | B<-c URI>
105 If using libvirt, connect to the given I<URI>. If omitted, then we
106 connect to the default libvirt hypervisor.
108 If you specify guest block devices directly, then libvirt is not used
115 =item B<--format> raw
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
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.
125 If working with untrusted raw-format guest disk images, you should
126 ensure the format is always specified.
132 =item B<-l> | B<--long>
134 =item B<-R> | B<--recursive>
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.
139 C<virt-ls -l> produces a "long listing", which shows more detail (just
140 like the plain C<ls -l> command).
142 C<virt-ls -R> produces a recursive list of files starting at the named
143 directory. See the documentation for the C<guestfs_find> command
144 L<guestfs(3)> for precise details.
146 You cannot combine these options.
154 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
160 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
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,
171 pod2usage (1) if $help;
173 my $g = Sys::Guestfs->new ();
174 my %h = $g->version ();
175 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
179 pod2usage (__"virt-ls: no image, VM names or directory to list given")
182 my $directory = pop @ARGV;
186 $g = open_guest (\@ARGV, address => $uri, format => $format);
188 $g = open_guest (\@ARGV, format => $format);
193 # List of possible filesystems.
194 my @partitions = get_partitions ($g);
196 # Now query each one to build up a picture of what's in it.
198 inspect_all_partitions ($g, \@partitions,
199 use_windows_registry => 0);
201 my $oses = inspect_operating_systems ($g, \%fses);
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];
207 my $os = $oses->{$root_dev};
208 mount_operating_system ($g, $os);
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";
221 while (($r = read (F, $line, 1024)) > 0) {
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.
242 L<Sys::Guestfs::Lib(3)>,
244 L<http://libguestfs.org/>.
248 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
252 Copyright (C) 2009 Red Hat Inc.
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.
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.
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.