This method will return a list with the MacVTap NICs (if any) of the running KVM instances. Currently, it is implemented for the KVM hypervisor only since MacVTap initial functionality targets the KVM hypervisor. For the rest hypervisors a HypervisorError exception will be raised if this method is called.
This is an essential method in order to be able to clean up stale MacVTap devices caused by a user instance shutdown, as stated by the next patch. Signed-off-by: Dimitris Bliablias <db...@skroutz.gr> --- lib/hypervisor/hv_base.py | 11 +++++++++++ lib/hypervisor/hv_kvm/__init__.py | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/hypervisor/hv_base.py b/lib/hypervisor/hv_base.py index a61fc78..4c20c6a 100644 --- a/lib/hypervisor/hv_base.py +++ b/lib/hypervisor/hv_base.py @@ -359,6 +359,17 @@ class BaseHypervisor(object): """Get the list of running instances.""" raise NotImplementedError + # pylint: disable=R0201,W0613 + def ListInstancesMacvtapNICs(self, instances=None): + """Get a list with macvtap NICs of the given instances. + + @type instances: list + @param instances: instances names to get their macvtap NICs (if any) + + """ + raise errors.HypervisorError("Macvtap functionality is not supported by" + " this hypervisor") + def GetInstanceInfo(self, instance_name, hvparams=None): """Get instance properties. diff --git a/lib/hypervisor/hv_kvm/__init__.py b/lib/hypervisor/hv_kvm/__init__.py index 0f32b18..f0f465e 100644 --- a/lib/hypervisor/hv_kvm/__init__.py +++ b/lib/hypervisor/hv_kvm/__init__.py @@ -1065,6 +1065,33 @@ class KVMHypervisor(hv_base.BaseHypervisor): result.append(name) return result + def ListInstancesMacvtapNICs(self, instances=None): + """Get a list with mactap NICs of the given instances. + + @type instances: list + @param instances: names of instances to get their macvtap NICs (if any) + + """ + if instances is None: + instances = self.ListInstances() + + result = [] + for inst_name in instances: + _, kvm_nics, _, _ = self._LoadKVMRuntime(inst_name) + + for nic_seq, nic in enumerate(kvm_nics): + if nic.nicparams[constants.NIC_MODE] != constants.NIC_MODE_MACVTAP: + continue + + try: + macvtap = utils.ReadFile(self._InstanceNICFile(inst_name, nic_seq)) + result.append(macvtap) + except EnvironmentError, err: + logging.warning("Failed to find host interface for %s NIC #%d: %s", + inst_name, nic_seq, str(err)) + + return result + @classmethod def _IsUserShutdown(cls, instance_name): return os.path.exists(cls._InstanceShutdownMonitor(instance_name)) -- 2.1.4