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