[libvirt-users] Libvirt API for getting disk capacity from VM XML

Martin Kletzander mkletzan at redhat.com
Fri Jun 14 14:37:15 UTC 2019

On Thu, Jun 13, 2019 at 03:17:16PM +0530, Varsha Verma wrote:
>Hello everyone,
>I am doing an outreachy internship at Openstack Ironic. In the sushy-tools
>project, we are using libvirt VMs to simulate bare metal machines for
>testing purposes.
>In the XML description of a domain, there are a bunch of disk elements
>giving information about the various storage devices attached to the
>domain. Is there some way to get the size/capacity of those disks using the
>libvirt API?

Yes, there is, virStorageVolGetXMLDesc:


Look for <capacity/>

The tricky part might be to get the storage volume object.  I'm not sure how
sushy-tools are using libvirt and what the deployment looks like, so here are
few tips and feel free to ask for more info.

If it is a path on the local filesystem, you might just use
`virStorageVolLookupByKey` and be done with it:


If it is not, you might need to look it up based on the pool it is in.

If you have the pool already, then it is a matter of `virStorageVolLookupBy*`
for example.  If you don't have that you might look for the pool with
`virStoragePoolLookupBy*`, for example `virStoragePoolLookupByName`:


And one more general libvirt advice (although maybe not really a good one,
definitely not a silver bullet):

You can always have a look at how `virsh` does things.  It might be a bit
confusing at times, but if you have a command name (in our case we'd use the
`vol-dumpxml` command), you can CamelCase it ("VolDumpXML"), prepend "cmd" (as
in "command") and the resulting name (`cmdVolDumpXML`) is the name of the
function that virsh runs for that command.  You can see it in


The main part of how it looks up a volume is `virshCommandOptVol` which is just
a syntax sugar for `virshCommandOptVolBy` in the same file:


Which is trying to do the lookup by all various possible things.  Same thing can
be found for `pool-*` commands.

HTH, have a nice day,

>*Varsha Verma*
>*Fourth Year Undergraduate*
>*Department of Electrical Engineering*
>*IIT-BHU, Varanasi*

>libvirt-users mailing list
>libvirt-users at redhat.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvirt-users/attachments/20190614/b9e33c16/attachment.sig>

More information about the libvirt-users mailing list