[Libguestfs] [PATCH] Don't use libvirt for volume information when converting with libvirtxml

Richard W.M. Jones rjones at redhat.com
Wed Jan 26 15:00:32 UTC 2011


On Wed, Jan 26, 2011 at 02:34:49PM +0000, Matthew Booth wrote:
> Unless you're lucky, the target volumes won't be in a storage pool, so libvirt
> will return an error. For libvirtxml we know that the storage is local, so we
> can inspect it directly. We use qemu-img to determine format and file size.
[...]
> +    # Use the output of qemu-img to inspect the path
> +    open(my $qemuimg, '-|', 'env', 'LANG=C', 'qemu-img', 'info', $path)
> +        or die("Unable to execute qemu-img: $!");

You need to make sure qemu-img is an explicit Requires in the
spec file, ie:

  Requires: /usr/bin/qemu-img

and it should be mentioned in the README file too for other distros.
Also other distros name this differently, eg: 'kvm-qemu-img' and maybe
other variations.

> +    # For $usage, $is_sparse and $is_block, we need to know if it's a block
> +    # device
> +    # N.B. qemu-img's 'disk size' output isn't useful here
> +    my ($usage, $is_sparse, $is_block);
> +    if (-b $path) {
> +        $is_block = 1;
> +        $usage = $size;
> +        $is_sparse = 0;
> +    } else {
> +        $is_block = 0;
> +        my $st = stat($path);
> +        $usage = $st->blocks * 512;
> +        $is_sparse = $usage < $size ? 1 : 0;
> +    }
> +
> +    die("size ($size) < usage ($usage)") if $size < $usage;

Agreed that qemu-img gets it wrong:

# qemu-img info /dev/vg_pin/CentOS5x64 
image: /dev/vg_pin/CentOS5x64
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0

*but* I'm not sure your code is correct.  I don't know if $st->blocks
is always valid, and you can't assume 512-sized blocks.

I suggest calling 'blockdev --getsize64', with the caveats above about
dependencies and README file.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v




More information about the Libguestfs mailing list