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';
34 virt-ls - List files in a virtual machine
38 virt-ls [--options] domname directory
40 virt-ls [--options] disk.img [disk.img ...] directory
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.
47 C<virt-ls> is just a simple wrapper around L<libguestfs(3)>
48 functionality. For more complex cases you should look at the
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:
59 With the C<-l> (C<--long>) option, C<virt-ls> shows more detail:
61 $ virt-ls -l myguest /
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
67 With the C<-R> (C<--recursive>) option, C<virt-ls> lists the
68 names of files and directories recursively:
70 $ virt-ls -R myguest /tmp
75 You I<cannot> combine these options. To do more complicated things,
96 Display version number and exit.
102 =item B<--connect URI> | B<-c URI>
104 If using libvirt, connect to the given I<URI>. If omitted, then we
105 connect to the default libvirt hypervisor.
107 If you specify guest block devices directly, then libvirt is not used
114 =item B<-l> | B<--long>
116 =item B<-R> | B<--recursive>
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.
121 C<virt-ls -l> produces a "long listing", which shows more detail (just
122 like the plain C<ls -l> command).
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.
128 You cannot combine these options.
136 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
142 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
146 GetOptions ("help|?" => \$help,
147 "version" => \$version,
148 "connect|c=s" => \$uri,
149 "long|l" => \&set_mode_l,
150 "recursive|R" => \&set_mode_R,
152 pod2usage (1) if $help;
154 my $g = Sys::Guestfs->new ();
155 my %h = $g->version ();
156 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
160 pod2usage (__"virt-ls: no image, VM names or directory to list given")
163 my $directory = pop @ARGV;
167 $g = open_guest (\@ARGV, address => $uri);
169 $g = open_guest (\@ARGV);
174 # List of possible filesystems.
175 my @partitions = get_partitions ($g);
177 # Now query each one to build up a picture of what's in it.
179 inspect_all_partitions ($g, \@partitions,
180 use_windows_registry => 0);
182 my $oses = inspect_operating_systems ($g, \%fses);
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];
189 my $os = $oses->{$root_dev};
190 mount_operating_system ($g, $os);
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;
210 L<Sys::Guestfs::Lib(3)>,
212 L<http://libguestfs.org/>.
216 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
220 Copyright (C) 2009 Red Hat Inc.
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.
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.
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.