Search code examples
cxen

c libxen api getting VDI size


I'm trying to get the size of Xen VM's disks My code is the following (error checking is omitted). I found no documentation about how to work with xen api, just a couple of examples. So I use only this http://fossies.org/dox/xen-4.2.1/xen__vdi_8h.html

xen_vm_get_by_uuid(XEN.session, &vm, uuid);
xen_vm_get_vbds(XEN.session,&vbd_set,vm);
for(int i=0; i<vbd_set->size; i++)
{
  xen_vbd_get_type(XEN.session,&vbd_type,vbd_set->contents[i]);
  //Just display VBD type, works well
  if(vbd_type==XEN_VBD_TYPE_CD) puts("CD");
  else if(vbd_type==XEN_VBD_TYPE_DISK ) puts("Disk");
  //Get VDI handle
  xen_vbd_get_vdi(XEN.session,&vdi,vbd_set->contents[i]);
  //The above returns 0, OK
  //Now I try to work with VDI
  if(!xen_vdi_get_type(XEN.session,&vdi_type,vdi))puts("fail");
  xen_vdi_free(vdi);

}
xen_vbd_set_free(vbd_set);
xen_vm_free(vm);

after xen_vdi_get_type sesion gets disconnected and thats all. I just can't understand what I'm dong wrong


Solution

  • Here is how to do it. Its quite simple....

    xen_vm vm;  
    xen_vbd_set *vbds;
    xen_vbd_record *vbdr;
    xen_vdi vdi;
    xen_vdi_record *vdir;
    enum xen_vbd_type vbd_type;
    xen_vm_get_by_uuid(XEN.session,&vm,vm_uuid);
    xen_vm_get_vbds(XEN.session, &vbds, vm);
    for(int i=0;i<vbds->size;i++)
    {       
        xen_vbd_get_record(XEN.session, &vbdr, vbds->contents[i]);
        xen_vbd_get_type(XEN.session, &vbd_type, vbds->contents[i]);
        if(vbd_type==XEN_VBD_TYPE_DISK)
        {
            xen_vbd_get_vdi(XEN.session, &vdi, vbds->contents[i]);              
            xen_vdi_get_record(XEN.session, &vdir, vdi);
            printf("%"PRId64"bytes\n",vdir->virtual_size);          
            xen_vdi_record_free(vdir);
            xen_vdi_free(vdi);
        }
        xen_vbd_record_free(vbdr);      
    }
    xen_vbd_set_free(vbds);
    xen_vm_free (vm);