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, “Interface lifecycle management”).

7.4.1. Defining an interface configuration

The interfaceDefineXML method is used for both 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 create, described in Section 7.5, “Interface lifecycle management”)
If the interface is successfully added/modified in the host's configuration, interfaceDefineXML returns a virInterface instance. This can be used as a handle to perform further actions on the new interface, for example making it active with create.
Currently the flags parameter should always be 0.

Example 7.12. Defining a new interface

#!/usr/bin/env python3
import sys
import libvirt

xml = """
<interface type='ethernet' name='eth0'>
  <start mode='onboot'/>
  <mac address='aa:bb:cc:dd:ee:ff'/>
  <protocol family='ipv4'>
    <ip address="" prefix="24"/>
    <route gateway=""/>

conn = None
    conn ='qemu:///system')
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)

# create/modify a network interface
iface = conn.interfaceDefineXML(xml, 0)
# activate the interface
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)

print("The interface name is: "