virt-rescue: Refresh documentation.
[libguestfs.git] / tools / virt-list-partitions
1 #!/usr/bin/perl -w
2 # virt-list-partitions
3 # Copyright (C) 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-partitions - List partitions in a virtual machine or disk image
33
34 =head1 SYNOPSIS
35
36  virt-list-partitions [--options] domname
37
38  virt-list-partitions [--options] disk.img [disk.img ...]
39
40 =head1 DESCRIPTION
41
42 C<virt-list-partitions> is a command line tool to list
43 the partitions that are contained in a virtual machine or
44 disk image.  It is mainly useful as a first step to using
45 L<virt-resize(1)>.
46
47 C<virt-list-partitions> 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 $human;
86
87 =item B<-h> | B<--human-readable>
88
89 Show sizes in human-readable form (eg. "1G").
90
91 =cut
92
93 my $long;
94
95 =item B<-l> | B<--long>
96
97 With this option, C<virt-list-partitions> displays the type
98 and size of each partition too (where "type" means C<ext3>, C<pv> etc.)
99
100 =cut
101
102 my $total;
103
104 =item B<-t> | B<--total>
105
106 Display the total size of each block device (as a separate row or
107 rows).
108
109 =back
110
111 =cut
112
113 # Configure bundling, otherwise '-lh' is unrecognized.
114 Getopt::Long::Configure ("bundling");
115
116 GetOptions ("help|?" => \$help,
117             "version" => \$version,
118             "connect|c=s" => \$uri,
119             "human-readable|h" => \$human,
120             "long|l" => \$long,
121             "total|t" => \$total,
122     ) or pod2usage (2);
123 pod2usage (1) if $help;
124 if ($version) {
125     my $g = Sys::Guestfs->new ();
126     my %h = $g->version ();
127     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
128     exit
129 }
130
131 pod2usage (__"virt-list-partitions: no image or VM name given")
132     if @ARGV <= 0;
133
134 my $g;
135 if ($uri) {
136     $g = open_guest (\@ARGV, address => $uri);
137 } else {
138     $g = open_guest (\@ARGV);
139 }
140
141 $g->launch ();
142
143 # List of partitions and sizes.
144 my @partitions = $g->list_partitions ();
145 foreach my $name (@partitions) {
146     $name = canonicalize ($name);
147
148     print $name;
149
150     if ($long) {
151         my $type;
152         eval {
153             $type = $g->vfs_type ($name);
154         };
155         $type ||= "unknown";
156         $type = "pv" if $type eq "LVM2_member";
157         print " $type ";
158
159         my $size;
160         eval {
161             $size = $g->blockdev_getsize64 ($name);
162         };
163         $size ||= "unknown";
164
165         if ($human) {
166             print (human_size($size));
167         } else {
168             print $size;
169         }
170     }
171     print "\n";
172 }
173
174 if ($total) {
175     # List of devices and sizes.
176     my @devices = $g->list_devices ();
177     foreach my $name (@devices) {
178         $name = canonicalize ($name);
179
180         print $name;
181
182         if ($long) {
183             print " device ";
184
185             my $size;
186             eval {
187                 $size = $g->blockdev_getsize64 ($name);
188             };
189             $size ||= "unknown";
190
191             if ($human) {
192                 print (human_size($size));
193             } else {
194                 print $size;
195             }
196         }
197         print "\n";
198     }
199 }
200
201 # The reverse of device name translation, see
202 # BLOCK DEVICE NAMING in guestfs(3).
203 sub canonicalize
204 {
205     local $_ = shift;
206
207     if (m{^/dev/[hv]d([a-z]+\d*)$}) {
208         return "/dev/sd$1";
209     }
210     $_;
211 }
212
213 # Convert a number of bytes to a human-readable number.
214 sub human_size
215 {
216     local $_ = shift;
217
218     $_ /= 1024;                 # blocks
219
220     if ($_ < 1024) {
221         sprintf "%dK", $_;
222     } elsif ($_ < 1024 * 1024) {
223         sprintf "%.1fM", ($_ / 1024);
224     } else {
225         sprintf "%.1fG", ($_ / 1024 / 1024);
226     }
227 }
228
229 =head1 SEE ALSO
230
231 L<guestfs(3)>,
232 L<guestfish(1)>,
233 L<virt-list-filesystems(1)>,
234 L<virt-resize(1)>,
235 L<Sys::Guestfs(3)>,
236 L<Sys::Guestfs::Lib(3)>,
237 L<Sys::Virt(3)>,
238 L<http://libguestfs.org/>.
239
240 =head1 AUTHOR
241
242 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
243
244 =head1 COPYRIGHT
245
246 Copyright (C) 2009-2010 Red Hat Inc.
247
248 This program is free software; you can redistribute it and/or modify
249 it under the terms of the GNU General Public License as published by
250 the Free Software Foundation; either version 2 of the License, or
251 (at your option) any later version.
252
253 This program is distributed in the hope that it will be useful,
254 but WITHOUT ANY WARRANTY; without even the implied warranty of
255 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
256 GNU General Public License for more details.
257
258 You should have received a copy of the GNU General Public License
259 along with this program; if not, write to the Free Software
260 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.