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