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

# Example-11.py
#!/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="192.168.0.5" prefix="24"/>
    <route gateway="192.168.0.1"/>
  </protocol>
</interface>"""

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

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

print("The interface name is: "+iface.name())

iface.destroy()
iface.undefine()
conn.close()
exit(0)