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
89 Specify the format of disk images given on the command line. If this
90 is omitted then the format is autodetected from the content of the
93 If disk images are requested from libvirt, then this program asks
94 libvirt for this information. In this case, the value of the format
97 If working with untrusted raw-format guest disk images, you should
98 ensure the format is always specified.
104 =item B<-h> | B<--human-readable>
106 Show sizes in human-readable form (eg. "1G").
112 =item B<-l> | B<--long>
114 With this option, C<virt-list-partitions> displays the type
115 and size of each partition too (where "type" means C<ext3>, C<pv> etc.)
121 =item B<-t> | B<--total>
123 Display the total size of each block device (as a separate row or
130 # Configure bundling, otherwise '-lh' is unrecognized.
131 Getopt::Long::Configure ("bundling");
133 GetOptions ("help|?" => \$help,
134 "version" => \$version,
135 "connect|c=s" => \$uri,
136 "format=s" => \$format,
137 "human-readable|h" => \$human,
139 "total|t" => \$total,
141 pod2usage (1) if $help;
143 my $g = Sys::Guestfs->new ();
144 my %h = $g->version ();
145 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
149 pod2usage (__"virt-list-partitions: no image or VM name given")
154 $g = open_guest (\@ARGV, address => $uri, format => $format);
156 $g = open_guest (\@ARGV, format => $format);
161 # List of partitions and sizes.
162 my @partitions = $g->list_partitions ();
163 foreach my $name (@partitions) {
164 $name = canonicalize ($name);
171 $type = $g->vfs_type ($name);
174 $type = "pv" if $type eq "LVM2_member";
179 $size = $g->blockdev_getsize64 ($name);
184 print (human_size($size));
193 # List of devices and sizes.
194 my @devices = $g->list_devices ();
195 foreach my $name (@devices) {
196 $name = canonicalize ($name);
205 $size = $g->blockdev_getsize64 ($name);
210 print (human_size($size));
219 # The reverse of device name translation, see
220 # BLOCK DEVICE NAMING in guestfs(3).
225 if (m{^/dev/[hv]d([a-z]+\d*)$}) {
231 # Convert a number of bytes to a human-readable number.
240 } elsif ($_ < 1024 * 1024) {
241 sprintf "%.1fM", ($_ / 1024);
243 sprintf "%.1fG", ($_ / 1024 / 1024);
249 Libvirt guest names can contain arbitrary characters, some of which
250 have meaning to the shell such as C<#> and space. You may need to
251 quote or escape these characters on the command line. See the shell
252 manual page L<sh(1)> for details.
258 L<virt-list-filesystems(1)>,
261 L<Sys::Guestfs::Lib(3)>,
263 L<http://libguestfs.org/>.
267 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
271 Copyright (C) 2009-2010 Red Hat Inc.
273 This program is free software; you can redistribute it and/or modify
274 it under the terms of the GNU General Public License as published by
275 the Free Software Foundation; either version 2 of the License, or
276 (at your option) any later version.
278 This program is distributed in the hope that it will be useful,
279 but WITHOUT ANY WARRANTY; without even the implied warranty of
280 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
281 GNU General Public License for more details.
283 You should have received a copy of the GNU General Public License
284 along with this program; if not, write to the Free Software
285 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.