New tool: virt-list-filesystems
[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 use File::Temp qw/tempdir/;
28
29 =encoding utf8
30
31 =head1 NAME
32
33 virt-list-filesystems - List filesystems in a virtual machine or disk image
34
35 =head1 SYNOPSIS
36
37  virt-list-filesystems [--options] domname
38
39  virt-list-filesystems [--options] disk.img [disk.img ...]
40
41 =head1 DESCRIPTION
42
43 C<virt-list-filesystems> is a command line tool to list
44 the filesystems that are contained in a virtual machine or
45 disk image.
46
47 C<virt-list-filesystems> is just a simple wrapper around
48 L<libguestfs(3)> functionality.  For more complex cases you should
49 look at the L<guestfish(1)> tool.
50
51 =head1 OPTIONS
52
53 =over 4
54
55 =cut
56
57 my $help;
58
59 =item B<--help>
60
61 Display brief help.
62
63 =cut
64
65 my $version;
66
67 =item B<--version>
68
69 Display version number and exit.
70
71 =cut
72
73 my $uri;
74
75 =item B<--connect URI> | B<-c URI>
76
77 If using libvirt, connect to the given I<URI>.  If omitted, then we
78 connect to the default libvirt hypervisor.
79
80 If you specify guest block devices directly, then libvirt is not used
81 at all.
82
83 =cut
84
85 my $long;
86
87 =item B<-l> | B<--long>
88
89 With this option, C<virt-list-filesystems> displays the type of
90 each filesystem too (where "type" means C<ext3>, C<xfs> etc.)
91
92 =cut
93
94 my $all;
95
96 =item B<-a> | B<--all>
97
98 Normally we only show mountable filesystems.  If this option
99 is given then swap devices are shown too.
100
101 =back
102
103 =cut
104
105 # Configure bundling, otherwise '-al' is treated as '--all'.
106 Getopt::Long::Configure ("bundling");
107
108 GetOptions ("help|?" => \$help,
109             "version" => \$version,
110             "connect|c=s" => \$uri,
111             "long|l" => \$long,
112             "all|a" => \$all,
113     ) or pod2usage (2);
114 pod2usage (1) if $help;
115 if ($version) {
116     my $g = Sys::Guestfs->new ();
117     my %h = $g->version ();
118     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
119     exit
120 }
121
122 pod2usage (__"virt-list-filesystems: no image or VM name given")
123     if @ARGV <= 0;
124
125 my $g;
126 if ($uri) {
127     $g = open_guest (\@ARGV, address => $uri);
128 } else {
129     $g = open_guest (\@ARGV);
130 }
131
132 $g->launch ();
133
134 # List of partitions, LVs etc.
135 my @partitions = get_partitions ($g);
136
137 my @fses;
138 my @not_mountable;
139 my $dev;
140
141 # Try and mount each one, to see what's mountable.
142 foreach $dev (@partitions) {
143     eval { $g->mount_ro ($dev, "/"); };
144     my $mountable = $@ ? 0 : 1;
145     $g->umount_all ();
146     if ($mountable) {
147         push @fses, $dev;
148     } else {
149         push @not_mountable, $dev;
150     }
151 }
152
153 foreach $dev (@fses) {
154     print canonicalize($dev);
155     if ($long) {
156         my $fstype;
157         eval { $fstype = $g->vfs_type ($dev); };
158         if ($fstype) {
159             print " $fstype";
160         } else {
161             print " unknown";
162         }
163     }
164     print "\n";
165 }
166
167 # If asked, look in the not_mountable list for potential swap devices.
168 if ($all) {
169     foreach $dev (@not_mountable) {
170         my $file;
171         eval { $file = $g->file ($dev); };
172         if ($file && $file =~ /\bswap\b/) {
173             print canonicalize($dev);
174             print " swap" if $long;
175             print "\n"
176         }
177     }
178 }
179
180 # The reverse of device name translation, see
181 # BLOCK DEVICE NAMING in guestfs(3).
182 sub canonicalize
183 {
184     local $_ = shift;
185
186     if (m{^/dev/[hv]d([a-z]\d)$}) {
187         return "/dev/sd$1";
188     }
189     $_;
190 }
191
192 =head1 SEE ALSO
193
194 L<guestfs(3)>,
195 L<guestfish(1)>,
196 L<virt-cat(1)>,
197 L<virt-tar(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.