Product SiteDocumentation Site

7.4. Managing interface configuration files

In libvirt, "defining" an interface means creating or changing the configuration, and "undefining" means deleting that configuration from the system. Newcomers may sometimes confuse these two operations with Create/Delete (which actually are used to activate and deactivate an existing interface - see section 7.5).

7.4.1. Defining an inteface configuration

The virInterfaceDefineXML function is used both for adding new interface configurations and modifying existing configurations. It either adds a new interface (with all information, including the interface name, given in the xml data) or modifies the configuration of an existing interface. The newly defined interface will be inactive until separate action is taken to make the new configuration take effect (for example, rebooting the host, or calling virInterfaceCreate, described in section 7.5)
If the interface is successfully added/modified in the host's configuration, virInterfaceDefineXML returns a virInterfacePtr, which can be used as a handle to perform further actions on the new interface, eg making it active with virInterfaceCreate.
When you are finished using the returned virInterfacePtr, you must free it with virInterfaceFree (this doesn't remove the interface itself, just the internal object used by libvirt).
/* xml is a char* containing the description, per section 7.2 */
virInterfacePtr iface;

iface = virInterfaceDefineXML(xml, 0);
if (!iface) {
   fprintf(stderr, "Failed to define interface.\n");
   /* other error handling */
   goto cleanup;
}
if (virInterfaceCreate(iface) != 0) {
   fprintf(stderr, "Failed to create (activate) interface\n");
   /* other error handling */
   goto cleanup;
}
virinterfaceFree(iface);

cleanup:
   /* ... */
Example 7.11. Defining a new interface