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

Daniel P. Berrange berrange at redhat.com
Wed Jan 26 15:29:10 UTC 2011


On Wed, Jan 26, 2011 at 03:00:32PM +0000, Richard W.M. Jones wrote:
> 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.

For files 'st_blocks' always refers to number of 512 byte blocks,
regardless of whether the underlying FS/storage is actually using
512 byte blocks for storing the data. So hardcoding 512 is fine

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

No need for calling blockdev actually, the quick way to determine
the size of a block device is to just seek to the end of the device
and then query the file pointer position, eg

   open DEV, "/dev/sda";
   seek DEV, 0, SEEK_END;
   $size = tell DEV;
   close DEV;

Regards,
Daniel




More information about the Libguestfs mailing list