Re: API calls to get interfaces and block devices

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Apr 11, 2014 at 1:23 PM, Daniel P. Berrange <berrange@xxxxxxxxxx> wrote:
> On Fri, Apr 11, 2014 at 01:14:50PM +0200, Ruben Kerkhof wrote:
>> Hi all,
>>
>> I have a few python scripts which use the libvirt api to get interface and block device statistics.
>> What has been bugging me for a while now that is that there’s no high level api to get a list of all interfaces or block devices for a vm.
>> The list can be retrieved from the xml with a bit of Xpath magic, but this seems to me to break the nice abstraction layer libvirt provides.
>> Ideally, I don’t have to do anything with xml, and add dependencies on xml parsers to my code.
>>
>> I’ve seen examples of code doing this, for example the collectd libvirt plugin, but there must be many others.
>>
>> Can I kindly ask for such an API? Unfortunately I don’t have the skills to code this up myself.
>
> The libvirt-glib project provides a set of libraries including
> libvirt-gconfig which intend to fill the gap you describe. They
> are accessible from Python using GObject Introspection support.
> The goal of libvirt-gconfig is to remove the need for apps to
> know anything about XML at all.

So now instead of having to learn XML parsing I'll have to learn
GObject and glib?
And instead of a dependency on an xml parser I'll have to add one on glib.
Not exactly the easy solution I was hoping for ;-)

I still think that a method to get the interfaces for a vm is a gap in
the current api,
which is working fine for all my other needs. I can get the interfaces
for a node, so why not for a vm?

>
> eg,this demo program shows how to build a domain config using
> the APIs. For parsing you just pass it the XML document and
> then can use the various getters to access info
>
> #!/usr/bin/python
>
> from gi.repository import LibvirtGConfig;
>
> domain = LibvirtGConfig.Domain.new()
> domain.set_virt_type(LibvirtGConfig.DomainVirtType.KVM)
> domain.set_name("foo")
> domain.set_memory(1024*1024) # 1 GB
> domain.set_vcpus(2)
> domain.set_lifecycle(LibvirtGConfig.DomainLifecycleEvent.ON_POWEROFF,
>                      LibvirtGConfig.DomainLifecycleAction.DESTROY)
> domain.set_virt_type(LibvirtGConfig.DomainVirtType.KVM)
>
> clock = LibvirtGConfig.DomainClock.new()
> clock.set_offset(LibvirtGConfig.DomainClockOffset.UTC)
> domain.set_clock(clock)
>
> os = LibvirtGConfig.DomainOs.new()
> os.set_os_type(LibvirtGConfig.DomainOsType.HVM)
> os.set_arch("x86_64")
> devices = [ LibvirtGConfig.DomainOsBootDevice.CDROM,
>             LibvirtGConfig.DomainOsBootDevice.NETWORK ]
> os.set_boot_devices(devices)
> domain.set_os(os)
>
> disk = LibvirtGConfig.DomainDisk.new()
> disk.set_type(LibvirtGConfig.DomainDiskType.FILE)
> disk.set_guest_device_type(LibvirtGConfig.DomainDiskGuestDeviceType.DISK)
> disk.set_source("/tmp/foo/bar")
> disk.set_driver_name("qemu")
> disk.set_driver_format(LibvirtGConfig.DomainDiskFormat.QCOW2)
> disk.set_target_bus(LibvirtGConfig.DomainDiskBus.IDE)
> disk.set_target_dev("hda")
> domain.add_device(disk)
>
> interface = LibvirtGConfig.DomainInterfaceNetwork.new()
> interface.set_source("default")
> filterref = LibvirtGConfig.DomainInterfaceFilterref.new()
> filterref.set_name("clean-traffic")
> parameter = LibvirtGConfig.DomainInterfaceFilterrefParameter.new()
> parameter.set_name("IP")
> parameter.set_value("205.23.12.40")
> filterref.add_parameter(parameter)
> interface.set_filterref(filterref)
> domain.add_device(interface)
>
> interface = LibvirtGConfig.DomainInterfaceUser.new()
> interface.set_ifname("eth0")
> interface.set_link_state(LibvirtGConfig.DomainInterfaceLinkState.UP)
> interface.set_mac("00:11:22:33:44:55")
> interface.set_model("foo")
> domain.add_device(interface)
>
> input = LibvirtGConfig.DomainInput.new()
> input.set_device_type(LibvirtGConfig.DomainInputDeviceType.TABLET)
> input.set_bus(LibvirtGConfig.DomainInputBus.USB)
> domain.add_device(input)
>
> graphics = LibvirtGConfig.DomainGraphicsSpice.new()
> graphics.set_port(1234)
> domain.add_device(graphics)
>
> video = LibvirtGConfig.DomainVideo.new()
> video.set_model(LibvirtGConfig.DomainVideoModel.QXL)
> domain.add_device(video)
>
> console = LibvirtGConfig.DomainConsole.new()
> pty = LibvirtGConfig.DomainChardevSourcePty.new()
> console.set_source(pty)
> domain.add_device(console)
>
> print domain.to_xml()
>
>
> pool = LibvirtGConfig.StoragePool.new()
> pool.set_pool_type(LibvirtGConfig.StoragePoolType.DIR)
>
> pool_source = LibvirtGConfig.StoragePoolSource.new()
> pool_source.set_directory("/foo/bar")
> pool.set_source(pool_source)
>
> perms = LibvirtGConfig.StoragePermissions.new()
> perms.set_owner(1001)
> perms.set_group(1005)
> perms.set_mode(0744)
> perms.set_label("virt_image_t")
>
> pool_target = LibvirtGConfig.StoragePoolTarget.new()
> pool_target.set_path("/dev/disk/by-path")
> pool_target.set_permissions(perms)
> pool.set_target(pool_target)
>
> print pool.to_xml()
>
>
> vol = LibvirtGConfig.StorageVol.new()
> vol.set_name("my-vol")
> vol.set_capacity(0xdeadbeef)
>
> vol_target = LibvirtGConfig.StorageVolTarget.new()
> vol_target.set_format("qcow2")
> vol_target.set_permissions(perms)
> vol.set_target(vol_target)
>
> print vol.to_xml()

Thanks for the example, I'll dig into it.
I just read https://www.berrange.com/posts/2011/11/22/introducing-the-libvirt-glib-a-mapping-of-the-libvirt-api-and-xml-to-glibgobject/
Is there any documentation available for  libvirt-gobject online?

Unfortunately I'm still stuck on Scientific Linux 6.5
There are no packages for libvirt-glib in the Scientific Linux repo,
are they available for libvirt 0.10 on RHEL 6.5?

>
>
> Regards,
> Daniel
> --
> |: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
> |: http://libvirt.org              -o-             http://virt-manager.org :|
> |: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
> |: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

Kind regards,

Ruben

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]