From: Ondrej Lichtner olichtne@redhat.com
Moving the VirtNetCtl class to the lnst.Devices package since it is only used by the VirtualDevice class.
Signed-off-by: Ondrej Lichtner olichtne@redhat.com --- lnst/Devices/VirtNetCtl.py | 85 +++++++++++++++++++++++++++++++++++++++++++ lnst/Devices/VirtualDevice.py | 2 +- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 lnst/Devices/VirtNetCtl.py
diff --git a/lnst/Devices/VirtNetCtl.py b/lnst/Devices/VirtNetCtl.py new file mode 100644 index 0000000..2795baf --- /dev/null +++ b/lnst/Devices/VirtNetCtl.py @@ -0,0 +1,85 @@ +""" +Defines the VirtNetCtl class used for dynamically adding and removing network +interface cards to libvirt guests. + +Copyright 2017 Red Hat, Inc. +Licensed under the GNU General Public License, version 2 as +published by the Free Software Foundation; see COPYING for details. +""" + +__author__ = """ +olichtne@redhat.com (Ondrej Lichtner) +""" + +import logging +import libvirt +from libvirt import libvirtError +from lnst.Common.LnstError import LnstError + +#this is a global object because opening the connection to libvirt in every +#object instance that uses it sometimes fails - the libvirt server probably +#can't handle that many connections at a time +_libvirt_conn = None + +def init_libvirt_con(): + global _libvirt_conn + if _libvirt_conn is None: + _libvirt_conn = libvirt.open(None) + +class VirtNetCtlError(LnstError): + pass + +class VirtNetCtl(object): + _network_template = """ + <network ipv6='yes'> + <name>{0}</name> + <bridge name='virbr_{0}' stp='off' delay='0' /> + <domain name='{0}'/> + </network> + """ + + def __init__(self, name=None): + init_libvirt_con() + + if not name: + name = self._generate_name() + self._name = name + + def _generate_name(self): + devs = _libvirt_conn.listNetworks() + + index = 0 + while True: + name = "lnst_net%d" % index + index += 1 + if name not in devs: + return name + + def get_name(self): + return self._name + + def init(self): + try: + network_xml = self._network_template.format(self._name) + _libvirt_conn.networkCreateXML(network_xml) + logging.debug("libvirt network '%s' created" % self._name) + return True + except libvirtError as e: + raise VirtNetCtlError(str(e)) + + def cleanup(self): + try: + network = _libvirt_conn.networkLookupByName(self._name) + network.destroy() + logging.debug("libvirt network '%s' destroyed" % self._name) + return True + except libvirtError as e: + raise VirtNetCtlError(str(e)) + + @classmethod + def network_exist(cls, net_name): + try: + _libvirt_conn.networkLookupByName(net_name) + return True + except: + return False diff --git a/lnst/Devices/VirtualDevice.py b/lnst/Devices/VirtualDevice.py index 49fe088..5c41a12 100644 --- a/lnst/Devices/VirtualDevice.py +++ b/lnst/Devices/VirtualDevice.py @@ -19,7 +19,7 @@ from lnst.Devices.RemoteDevice import RemoteDevice
# conditional support for libvirt if check_process_running("libvirtd"): - from lnst.Controller.VirtUtils import VirtNetCtl + from lnst.Devices.VirtNetCtl import VirtNetCtl
class VirtualDevice(RemoteDevice): """Remote eth device created on the controller through libvirt