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<-l> | B<--long>
117 =item B<-R> | B<--recursive>
119 Select the mode. With neither of these options, C<virt-ls>
120 produces a simple, flat list of the files in the named directory.
122 C<virt-ls -l> produces a "long listing", which shows more detail (just
123 like the plain C<ls -l> command).
125 C<virt-ls -R> produces a recursive list of files starting at the named
126 directory. See the documentation for the C<guestfs_find> command
127 L<guestfs(3)> for precise details.
129 You cannot combine these options.
137 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
143 die __"virt-ls: cannot combine -l and -R options\n" if $mode;
147 GetOptions ("help|?" => \$help,
148 "version" => \$version,
149 "connect|c=s" => \$uri,
150 "long|l" => \&set_mode_l,
151 "recursive|R" => \&set_mode_R,
153 pod2usage (1) if $help;
155 my $g = Sys::Guestfs->new ();
156 my %h = $g->version ();
157 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
161 pod2usage (__"virt-ls: no image, VM names or directory to list given")
164 my $directory = pop @ARGV;
168 $g = open_guest (\@ARGV, address => $uri);
170 $g = open_guest (\@ARGV);
175 # List of possible filesystems.
176 my @partitions = get_partitions ($g);
178 # Now query each one to build up a picture of what's in it.
180 inspect_all_partitions ($g, \@partitions,
181 use_windows_registry => 0);
183 my $oses = inspect_operating_systems ($g, \%fses);
185 my @roots = keys %$oses;
186 die __"no root device found in this operating system image\n" if @roots == 0;
187 die __"multiboot operating systems are not supported by virt-ls\n" if @roots > 1;
188 my $root_dev = $roots[0];
190 my $os = $oses->{$root_dev};
191 mount_operating_system ($g, $os);
194 my @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 my $dir = tempdir (CLEANUP => 1);
200 $g->find0 ($directory, "$dir/find0");
201 open F, "$dir/find0" or die "$dir/find0: $!\n";
204 while (($r = read (F, $line, 1024)) > 0) {
218 L<Sys::Guestfs::Lib(3)>,
220 L<http://libguestfs.org/>.
224 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
228 Copyright (C) 2009 Red Hat Inc.
230 This program is free software; you can redistribute it and/or modify
231 it under the terms of the GNU General Public License as published by
232 the Free Software Foundation; either version 2 of the License, or
233 (at your option) any later version.
235 This program is distributed in the hope that it will be useful,
236 but WITHOUT ANY WARRANTY; without even the implied warranty of
237 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
238 GNU General Public License for more details.
240 You should have received a copy of the GNU General Public License
241 along with this program; if not, write to the Free Software
242 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.