3 # Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
23 use Sys::Guestfs::Lib qw(open_guest);
26 use Locale::TextDomain 'libguestfs';
32 virt-list-partitions - List partitions in a virtual machine or disk image
36 virt-list-partitions [--options] domname
38 virt-list-partitions [--options] disk.img [disk.img ...]
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
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.
69 Display version number and exit.
75 =item B<--connect URI> | B<-c URI>
77 If using libvirt, connect to the given I<URI>. If omitted, then we
78 connect to the default libvirt hypervisor.
80 If you specify guest block devices directly, then libvirt is not used
87 =item B<-h> | B<--human-readable>
89 Show sizes in human-readable form (eg. "1G").
95 =item B<-l> | B<--long>
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.)
104 =item B<-t> | B<--total>
106 Display the total size of each block device (as a separate row or
113 # Configure bundling, otherwise '-lh' is unrecognized.
114 Getopt::Long::Configure ("bundling");
116 GetOptions ("help|?" => \$help,
117 "version" => \$version,
118 "connect|c=s" => \$uri,
119 "human-readable|h" => \$human,
121 "total|t" => \$total,
123 pod2usage (1) if $help;
125 my $g = Sys::Guestfs->new ();
126 my %h = $g->version ();
127 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
131 pod2usage (__"virt-list-partitions: no image or VM name given")
136 $g = open_guest (\@ARGV, address => $uri);
138 $g = open_guest (\@ARGV);
143 # List of partitions and sizes.
144 my @partitions = $g->list_partitions ();
145 foreach my $name (@partitions) {
146 $name = canonicalize ($name);
153 $type = $g->vfs_type ($name);
156 $type = "pv" if $type eq "LVM2_member";
161 $size = $g->blockdev_getsize64 ($name);
166 print (human_size($size));
175 # List of devices and sizes.
176 my @devices = $g->list_devices ();
177 foreach my $name (@devices) {
178 $name = canonicalize ($name);
187 $size = $g->blockdev_getsize64 ($name);
192 print (human_size($size));
201 # The reverse of device name translation, see
202 # BLOCK DEVICE NAMING in guestfs(3).
207 if (m{^/dev/[hv]d([a-z]+\d*)$}) {
213 # Convert a number of bytes to a human-readable number.
222 } elsif ($_ < 1024 * 1024) {
223 sprintf "%.1fM", ($_ / 1024);
225 sprintf "%.1fG", ($_ / 1024 / 1024);
233 L<virt-list-filesystems(1)>,
236 L<Sys::Guestfs::Lib(3)>,
238 L<http://libguestfs.org/>.
242 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
246 Copyright (C) 2009-2010 Red Hat Inc.
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.
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.
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.