virt-ls: Small fix to documentation.
[libguestfs.git] / tools / virt-list-filesystems
1 #!/usr/bin/perl -w
2 # virt-list-filesystems
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);
24 use Pod::Usage;
25 use Getopt::Long;
26 use Locale::TextDomain 'libguestfs';
27
28 =encoding utf8
29
30 =head1 NAME
31
32 virt-list-filesystems - List filesystems in a virtual machine or disk image
33
34 =head1 SYNOPSIS
35
36  virt-list-filesystems [--options] domname
37
38  virt-list-filesystems [--options] disk.img [disk.img ...]
39
40 =head1 DESCRIPTION
41
42 C<virt-list-filesystems> is a command line tool to list
43 the filesystems that are contained in a virtual machine or
44 disk image.
45
46 C<virt-list-filesystems> is just a simple wrapper around
47 L<libguestfs(3)> functionality.  For more complex cases you should
48 look at the L<guestfish(1)> tool.
49
50 =head1 OPTIONS
51
52 =over 4
53
54 =cut
55
56 my $help;
57
58 =item B<--help>
59
60 Display brief help.
61
62 =cut
63
64 my $version;
65
66 =item B<--version>
67
68 Display version number and exit.
69
70 =cut
71
72 my $uri;
73
74 =item B<--connect URI> | B<-c URI>
75
76 If using libvirt, connect to the given I<URI>.  If omitted, then we
77 connect to the default libvirt hypervisor.
78
79 If you specify guest block devices directly, then libvirt is not used
80 at all.
81
82 =cut
83
84 my $format;
85
86 =item B<--format> raw
87
88 Specify the format of disk images given on the command line.  If this
89 is omitted then the format is autodetected from the content of the
90 disk image.
91
92 If disk images are requested from libvirt, then this program asks
93 libvirt for this information.  In this case, the value of the format
94 parameter is ignored.
95
96 If working with untrusted raw-format guest disk images, you should
97 ensure the format is always specified.
98
99 =cut
100
101 my $long;
102
103 =item B<-l> | B<--long>
104
105 With this option, C<virt-list-filesystems> displays the type of
106 each filesystem too (where "type" means C<ext3>, C<xfs> etc.)
107
108 =cut
109
110 my $all;
111
112 =item B<-a> | B<--all>
113
114 Normally we only show mountable filesystems.  If this option
115 is given then swap devices are shown too.
116
117 =back
118
119 =cut
120
121 # Configure bundling, otherwise '-al' is treated as '--all'.
122 Getopt::Long::Configure ("bundling");
123
124 GetOptions ("help|?" => \$help,
125             "version" => \$version,
126             "connect|c=s" => \$uri,
127             "format=s" => \$format,
128             "long|l" => \$long,
129             "all|a" => \$all,
130     ) or pod2usage (2);
131 pod2usage (1) if $help;
132 if ($version) {
133     my $g = Sys::Guestfs->new ();
134     my %h = $g->version ();
135     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
136     exit
137 }
138
139 pod2usage (__"virt-list-filesystems: no image or VM name given")
140     if @ARGV <= 0;
141
142 my $g;
143 if ($uri) {
144     $g = open_guest (\@ARGV, address => $uri, format => $format);
145 } else {
146     $g = open_guest (\@ARGV, format => $format);
147 }
148
149 $g->launch ();
150
151 # List of partitions, LVs etc.
152 my @partitions = get_partitions ($g);
153
154 my @fses;
155 my @not_mountable;
156 my $dev;
157
158 # Try and mount each one, to see what's mountable.
159 foreach $dev (@partitions) {
160     eval { $g->mount_ro ($dev, "/"); };
161     my $mountable = $@ ? 0 : 1;
162     $g->umount_all ();
163     if ($mountable) {
164         push @fses, $dev;
165     } else {
166         push @not_mountable, $dev;
167     }
168 }
169
170 foreach $dev (@fses) {
171     print canonicalize($dev);
172     if ($long) {
173         my $fstype;
174         eval { $fstype = $g->vfs_type ($dev); };
175         if ($fstype) {
176             print " $fstype";
177         } else {
178             print " unknown";
179         }
180     }
181     print "\n";
182 }
183
184 # If asked, look in the not_mountable list for potential swap devices.
185 if ($all) {
186     foreach $dev (@not_mountable) {
187         my $file;
188         eval { $file = $g->file ($dev); };
189         if ($file && $file =~ /\bswap\b/) {
190             print canonicalize($dev);
191             print " swap" if $long;
192             print "\n"
193         }
194     }
195 }
196
197 # The reverse of device name translation, see
198 # BLOCK DEVICE NAMING in guestfs(3).
199 sub canonicalize
200 {
201     local $_ = shift;
202
203     if (m{^/dev/[hv]d([a-z]\d)$}) {
204         return "/dev/sd$1";
205     }
206     $_;
207 }
208
209 =head1 SHELL QUOTING
210
211 Libvirt guest names can contain arbitrary characters, some of which
212 have meaning to the shell such as C<#> and space.  You may need to
213 quote or escape these characters on the command line.  See the shell
214 manual page L<sh(1)> for details.
215
216 =head1 SEE ALSO
217
218 L<guestfs(3)>,
219 L<guestfish(1)>,
220 L<virt-cat(1)>,
221 L<virt-tar(1)>,
222 L<virt-list-partitions(1)>,
223 L<Sys::Guestfs(3)>,
224 L<Sys::Guestfs::Lib(3)>,
225 L<Sys::Virt(3)>,
226 L<http://libguestfs.org/>.
227
228 =head1 AUTHOR
229
230 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
231
232 =head1 COPYRIGHT
233
234 Copyright (C) 2009 Red Hat Inc.
235
236 This program is free software; you can redistribute it and/or modify
237 it under the terms of the GNU General Public License as published by
238 the Free Software Foundation; either version 2 of the License, or
239 (at your option) any later version.
240
241 This program is distributed in the hope that it will be useful,
242 but WITHOUT ANY WARRANTY; without even the implied warranty of
243 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
244 GNU General Public License for more details.
245
246 You should have received a copy of the GNU General Public License
247 along with this program; if not, write to the Free Software
248 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.