Libvirt-gconfig is part of libvirt-glib which wraps libvirt to provide a high-level object-oriented API better suited for glib-based applications via three libraries:
- GLib main loop integration & misc helper APIs
- GObjects for manipulating libvirt XML documents
- GObjects for mangling libvirt object
It's distributed under LGPLv2+. The latest official releases can be found at ftp://libvirt.org/libvirt/glib
Glossary of terms
Libvirt-gconfig is designed to be perfectly usable even without whole GLib. Hence, you can create an application using libvirt-gconfig without need to run GMainLoop.
With libvirt-gconfig you can create any type of object known to bare libvirt:
domain, storage pool, storage volume, etc. Each object has its own handle
assigned with special type:
GVirConfigDomain for domain,
GVirConfigNetwork for network,
for storage volume and so on. Each of these types are derived from GVirConfigObject.
We will meet this fact later, though.
Each object must be created firstly in order to work with it. This can be done in two ways:
- creating simple empty object:
GVirConfigDomain *domain = gvir_config_domain_new();
- creating object from XML:
GError *error = NULL;
char *xml = get_xml();
GVirConfigDomain *domain = gvir_config_domain_new_from_xml(xml, &error);
Then we can use set/get method over each object attribute we want:
What each method does can be derived simple from its name. The complete list
of these methods can be seen in libvirt-gconfig GTK documentation. To generate
this documentation pass
--enable-gtk-doc to configure script.
Some parts of object configuration can be, however, again objects. For
os element in domain XML is represented as
GVirConfigDomainOs, domain device is represented by
GVirConfigDomainDevice and so on. However, when creating an
interface it is a type of
GVirConfigDomainInterface. So how to
convert it to
GVirConfigDomainDevice? There are macros for that:
GVirConfigDomainInterface interface = GVIR_CONFIG_DOMAIN_INTERFACE(gvir_config_domain_interface_network_new());
Once we have objects created and all attributes set, nothing hold us from generating a XML for the object:
xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(domain));
It's worth noticing that it's caller responsibility to free returned string as soon as it's no longer needed.