virt-make-fs: Use Sys::Guestfs::Lib::feature_available helper function.
[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 $long;
85
86 =item B<-l> | B<--long>
87
88 With this option, C<virt-list-filesystems> displays the type of
89 each filesystem too (where "type" means C<ext3>, C<xfs> etc.)
90
91 =cut
92
93 my $all;
94
95 =item B<-a> | B<--all>
96
97 Normally we only show mountable filesystems.  If this option
98 is given then swap devices are shown too.
99
100 =back
101
102 =cut
103
104 # Configure bundling, otherwise '-al' is treated as '--all'.
105 Getopt::Long::Configure ("bundling");
106
107 GetOptions ("help|?" => \$help,
108             "version" => \$version,
109             "connect|c=s" => \$uri,
110             "long|l" => \$long,
111             "all|a" => \$all,
112     ) or pod2usage (2);
113 pod2usage (1) if $help;
114 if ($version) {
115     my $g = Sys::Guestfs->new ();
116     my %h = $g->version ();
117     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
118     exit
119 }
120
121 pod2usage (__"virt-list-filesystems: no image or VM name given")
122     if @ARGV <= 0;
123
124 my $g;
125 if ($uri) {
126     $g = open_guest (\@ARGV, address => $uri);
127 } else {
128     $g = open_guest (\@ARGV);
129 }
130
131 $g->launch ();
132
133 # List of partitions, LVs etc.
134 my @partitions = get_partitions ($g);
135
136 my @fses;
137 my @not_mountable;
138 my $dev;
139
140 # Try and mount each one, to see what's mountable.
141 foreach $dev (@partitions) {
142     eval { $g->mount_ro ($dev, "/"); };
143     my $mountable = $@ ? 0 : 1;
144     $g->umount_all ();
145     if ($mountable) {
146         push @fses, $dev;
147     } else {
148         push @not_mountable, $dev;
149     }
150 }
151
152 foreach $dev (@fses) {
153     print canonicalize($dev);
154     if ($long) {
155         my $fstype;
156         eval { $fstype = $g->vfs_type ($dev); };
157         if ($fstype) {
158             print " $fstype";
159         } else {
160             print " unknown";
161         }
162     }
163     print "\n";
164 }
165
166 # If asked, look in the not_mountable list for potential swap devices.
167 if ($all) {
168     foreach $dev (@not_mountable) {
169         my $file;
170         eval { $file = $g->file ($dev); };
171         if ($file && $file =~ /\bswap\b/) {
172             print canonicalize($dev);
173             print " swap" if $long;
174             print "\n"
175         }
176     }
177 }
178
179 # The reverse of device name translation, see
180 # BLOCK DEVICE NAMING in guestfs(3).
181 sub canonicalize
182 {
183     local $_ = shift;
184
185     if (m{^/dev/[hv]d([a-z]\d)$}) {
186         return "/dev/sd$1";
187     }
188     $_;
189 }
190
191 =head1 SEE ALSO
192
193 L<guestfs(3)>,
194 L<guestfish(1)>,
195 L<virt-cat(1)>,
196 L<virt-tar(1)>,
197 L<virt-list-partitions(1)>,
198 L<Sys::Guestfs(3)>,
199 L<Sys::Guestfs::Lib(3)>,
200 L<Sys::Virt(3)>,
201 L<http://libguestfs.org/>.
202
203 =head1 AUTHOR
204
205 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
206
207 =head1 COPYRIGHT
208
209 Copyright (C) 2009 Red Hat Inc.
210
211 This program is free software; you can redistribute it and/or modify
212 it under the terms of the GNU General Public License as published by
213 the Free Software Foundation; either version 2 of the License, or
214 (at your option) any later version.
215
216 This program is distributed in the hope that it will be useful,
217 but WITHOUT ANY WARRANTY; without even the implied warranty of
218 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
219 GNU General Public License for more details.
220
221 You should have received a copy of the GNU General Public License
222 along with this program; if not, write to the Free Software
223 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.