2d666a03c3fe0816db46de9928e203d3f22ddc72
[libguestfs.git] / df / virt-df.pl
1 #!/usr/bin/perl -w
2 # virt-df
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 resolve_windows_path
24   inspect_all_partitions inspect_partition
25   inspect_operating_systems mount_operating_system inspect_in_detail);
26 use Pod::Usage;
27 use Getopt::Long;
28 use Data::Dumper;
29 use File::Temp qw/tempdir/;
30 use XML::Writer;
31
32 =encoding utf8
33
34 =head1 NAME
35
36 virt-df - Display free space on virtual filesystems
37
38 =head1 SYNOPSIS
39
40  virt-df [--options]
41
42  virt-df [--options] domname
43
44  virt-df [--options] disk.img [disk.img ...]
45
46 =head1 DESCRIPTION
47
48 C<virt-df> is a command line tool to display free space on virtual
49 machine filesystems.  Unlike other tools, it doesn't just display the
50 amount of space allocated to a virtual machine, but can look inside
51 the virtual machine to see how much space is really being used.
52
53 It is like the L<df(1)> command, but for virtual machines, except that
54 it also works for Windows virtual machines.
55
56 If used without any arguments, C<virt-df> checks with libvirt to get a
57 list of all active and inactive guests, and performs a C<df>-type
58 operation on each one in turn, printing out the results.
59
60 If used with any argument(s), C<virt-df> performs a C<df>-type
61 operation on either the single named libvirt domain, or on the disk
62 image(s) listed on the command line (which must all belong to a single
63 VM).  In this mode (with arguments), C<virt-df> will I<only work for a
64 single guest>.  If you want to run on multiple guests, then you have
65 to invoke C<virt-df> multiple times.
66
67 Use the C<--csv> option to get a format which can be easily parsed by
68 other programs.  Other options are mostly similar to standard C<df>
69 options.  See below for the complete list.
70
71 =head1 OPTIONS
72
73 =over 4
74
75 =cut
76
77 my $help;
78
79 =item B<--help>
80
81 Display brief help.
82
83 =cut
84
85 my $uri;
86
87 =item B<--connect URI> | B<-c URI>
88
89 If using libvirt, connect to the given I<URI>.  If omitted, then we
90 connect to the default libvirt hypervisor.
91
92 If you specify guest block devices directly, then libvirt is not used
93 at all.
94
95 =cut
96
97 my $csv;
98
99 =item B<--csv>
100
101 Write out the results in CSV format (comma-separated values).
102 This format can be imported easily into databases and spreadsheets.
103
104 =cut
105
106 my $human;
107
108 =item B<--human-readable> | B<-h>
109
110 Print sizes in human-readable format.
111
112 =cut
113
114 my $inodes;
115
116 =item B<--inodes> | B<-i>
117
118 Print inodes instead of blocks.
119
120 =back
121
122 =cut
123
124 GetOptions ("help|?" => \$help,
125             "connect|c=s" => \$uri,
126             "csv" => \$csv,
127             "human-readable|human|h" => \$human,
128             "inodes|i" => \$inodes,
129     ) or pod2usage (2);
130 pod2usage (1) if $help;
131
132 # Open the guest handle.
133
134 if (@ARGV == 0) {
135     my $conn;
136
137     if ($uri) {
138         $conn = Sys::Virt->new (readonly => 1, address => $uri);
139     } else {
140         $conn = Sys::Virt->new (readonly => 1);
141     }
142
143     my @doms = $conn->list_defined_domains ();
144     push @doms, $conn->list_domains ();
145
146     my @domnames = map { $_->get_name () } @doms;
147
148     if (@domnames) {
149         print_title ();
150         foreach (@domnames) {
151             do_df ($_);
152         }
153     }
154 } else {
155     print_title ();
156     do_df (@ARGV);
157 }
158
159 sub do_df
160 {
161     my $g;
162
163     if ($uri) {
164         $g = open_guest (\@_, address => $uri);
165     } else {
166         $g = open_guest (\@_);
167     }
168
169     $g->launch ();
170     $g->wait_ready ();
171
172     my @partitions = get_partitions ($g);
173
174     # Think of a printable name for this domain.  Just choose the
175     # first parameter passed to this function, which will work for
176     # most cases (it'll either be the domain name or the first disk
177     # image name).
178     my $domname = $_[0];
179
180     # Mount each partition in turn, and if mountable, do a statvfs on it.
181     foreach my $partition (@partitions) {
182         my %stat;
183         eval {
184             $g->mount_ro ($partition, "/");
185             %stat = $g->statvfs ("/");
186         };
187         if (!$@) {
188             print_stat ($domname, $partition, \%stat);
189         }
190         $g->umount_all ();
191     }
192 }
193
194 sub print_stat
195 {
196     my $domname = shift;
197     my $partition = shift;
198     my $stat = shift;
199
200     my @cols = ($domname, $partition);
201
202     if (!$inodes) {
203         my $bsize = $stat->{bsize};     # block size
204         my $blocks = $stat->{blocks};   # total number of blocks
205         my $bfree = $stat->{bfree};     # blocks free (total)
206         my $bavail = $stat->{bavail};   # blocks free (for non-root users)
207
208         my $factor = $bsize / 1024;
209
210         push @cols, $blocks*$factor;    # total 1K blocks
211         push @cols, ($blocks-$bfree)*$factor; # total 1K blocks used
212         push @cols, $bavail*$factor;    # total 1K blocks available
213
214         # XXX %used column comes out different from the native 'df'
215         # program.  Need to check how 'df' calculates this.
216         push @cols, 100.0 - 100.0 * $bavail / $blocks;
217
218         if ($human) {
219             $cols[2] = human_size ($cols[2]);
220             $cols[3] = human_size ($cols[3]);
221             $cols[4] = human_size ($cols[4]);
222         }
223     } else {
224         my $files = $stat->{files};     # total number of inodes
225         my $ffree = $stat->{ffree};     # inodes free (total)
226         my $favail = $stat->{favail};   # inodes free (for non-root users)
227
228         push @cols, $files;
229         push @cols, $files-$ffree;
230         push @cols, $ffree;
231
232         # XXX %used column comes out different from the native 'df'
233         # program.  Need to check how 'df' calculates this.
234         push @cols, 100.0 - 100.0 * $favail / $files;
235     }
236
237     print_cols (@cols);
238 }
239
240 sub print_title
241 {
242     my @cols = ("Virtual Machine", "Filesystem");
243     if (!$inodes) {
244         if (!$human) {
245             push @cols, "1K-blocks";
246         } else {
247             push @cols, "Size";
248         }
249         push @cols, "Used";
250         push @cols, "Available";
251         push @cols, "Use%";
252     } else {
253         push @cols, "Inodes";
254         push @cols, "IUsed";
255         push @cols, "IFree";
256         push @cols, "IUse%";
257     }
258
259     if (!$csv) {
260         # ignore $cols[0] in this mode
261         printf "%-36s%10s %10s %10s %5s\n",
262           $cols[1], $cols[2], $cols[3], $cols[4], $cols[5];
263     } else {
264         print (join (",", @cols), "\n");
265     }
266 }
267
268 sub print_cols
269 {
270     if (!$csv) {
271         my $label = sprintf "%s:%s", $_[0], $_[1];
272
273         printf ("%-36s", $label);
274         print "\n"," "x32 if length ($label) > 36;
275
276         my $percent = sprintf "%3.1f%%", $_[5];
277         printf ("%10s %10s %10s %5s\n", $_[2], $_[3], $_[4], $percent);
278     } else {
279         printf ("\"%s\",\"%s\",%d,%d,%d,%.1f%%\n", @_);
280     }
281 }
282
283 # Convert a number of 1K blocks to a human-readable number.
284 sub human_size
285 {
286     local $_ = shift;
287
288     if ($_ < 1024) {
289         sprintf "%dK", $_;
290     } elsif ($_ < 1024 * 1024) {
291         sprintf "%.1fM", ($_ / 1024);
292     } else {
293         sprintf "%.1fG", ($_ / 1024 / 1024);
294     }
295 }
296
297 =head1 SEE ALSO
298
299 L<guestfs(3)>,
300 L<guestfish(1)>,
301 L<Sys::Guestfs(3)>,
302 L<Sys::Guestfs::Lib(3)>,
303 L<Sys::Virt(3)>,
304 L<http://libguestfs.org/>.
305
306 =head1 AUTHOR
307
308 Richard W.M. Jones L<http://et.redhat.com/~rjones/>
309
310 =head1 COPYRIGHT
311
312 Copyright (C) 2009 Red Hat Inc.
313
314 This program is free software; you can redistribute it and/or modify
315 it under the terms of the GNU General Public License as published by
316 the Free Software Foundation; either version 2 of the License, or
317 (at your option) any later version.
318
319 This program is distributed in the hope that it will be useful,
320 but WITHOUT ANY WARRANTY; without even the implied warranty of
321 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
322 GNU General Public License for more details.
323
324 You should have received a copy of the GNU General Public License
325 along with this program; if not, write to the Free Software
326 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.