Ondřej Svoboda has uploaded a new change for review.

Change subject: dhclient: consider NetworkManager lease files for DUID too
......................................................................

dhclient: consider NetworkManager lease files for DUID too

If a device to be replaced by our bridge is owned by NetworkManager,
its lease file lies in /var/lib/NetworkManager. In that case dhclient
would not reuse DUID and get a different lease.

A new function now looks in /var/lib/dhclient for initscripts-written
ifcfg file, and in the above directory, and returns the name of a lease
file with the most recent DUID for the given device.

Change-Id: I62a102a2964ed5e5864ff516c71752a23c4c55cf
Signed-off-by: Ondřej Svoboda <[email protected]>
---
M lib/vdsm/network/configurators/dhclient.py
M lib/vdsm/network/configurators/ifcfg.py
2 files changed, 35 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/51/57151/1

diff --git a/lib/vdsm/network/configurators/dhclient.py 
b/lib/vdsm/network/configurators/dhclient.py
index eb4c99b..b7921b7 100644
--- a/lib/vdsm/network/configurators/dhclient.py
+++ b/lib/vdsm/network/configurators/dhclient.py
@@ -20,6 +20,7 @@
 
 from __future__ import absolute_import
 import errno
+from glob import iglob
 import logging
 import os
 import subprocess
@@ -35,6 +36,8 @@
 DHCLIENT_CGROUP = 'vdsm-dhclient'
 LEASE_DIR = '/var/lib/dhclient'
 LEASE_FILE = os.path.join(LEASE_DIR, 'dhclient{0}--{1}.lease')
+NM_LEASE_FILE_GLOB = os.path.join('/var/lib/NetworkManager',
+                                  'dhclient{0}-*-{1}.lease')
 
 
 class DhcpClient(object):
@@ -46,7 +49,7 @@
         self.family = family
         self.default_route = default_route
         self.duid_source_file = None if duid_source is None else (
-            LEASE_FILE.format('' if family == 4 else '6', duid_source))
+            find_newest_lease_file(duid_source, family))
         self.pidFile = self.PID_FILE % (family, self.iface)
         if not os.path.exists(LEASE_DIR):
             os.mkdir(LEASE_DIR)
@@ -138,3 +141,32 @@
 
     _, err = probe.communicate()
     return '-df' in err
+
+
+def find_newest_lease_file(device_name, ip_family):
+    """
+    Return the path to the most recent lease file for a given device name and
+    IP family.
+
+    Lease files written both by initscripts and NetworkManager are considered.
+    """
+    dhclient_suffix = '' if ip_family == 4 else '6'
+    lease_file_names = [LEASE_FILE.format(dhclient_suffix, device_name)]
+    lease_file_names.extend(
+        iglob(NM_LEASE_FILE_GLOB.format(dhclient_suffix, device_name)))
+    dated_lease_file_names = []
+    for name in lease_file_names:
+        try:
+            modified_date = os.stat(name).st_mtime
+        except OSError as e:
+            if e.errno == errno.ENOENT:
+                continue
+            else:
+                raise
+        dated_lease_file_names.append((modified_date, name))
+
+    if dated_lease_file_names:
+        dated_lease_file_names.sort(reverse=True)
+        return dated_lease_file_names[0][1]
+    else:
+        return None
diff --git a/lib/vdsm/network/configurators/ifcfg.py 
b/lib/vdsm/network/configurators/ifcfg.py
index baf3d8d..265c64b 100644
--- a/lib/vdsm/network/configurators/ifcfg.py
+++ b/lib/vdsm/network/configurators/ifcfg.py
@@ -562,8 +562,8 @@
             conf += 'STP=%s\n' % ('on' if bridge.stp else 'off')
         conf += 'ONBOOT=yes\n'
         if bridge.duid_source and dhclient.supports_duid_file():
-            duid_source_file = dhclient.LEASE_FILE.format(
-                '', bridge.duid_source)
+            duid_source_file = dhclient.find_newest_lease_file(
+                bridge.duid_source, 4)
             conf += 'DHCLIENTARGS="-df %s"\n' % duid_source_file
 
         if 'custom' in opts and 'bridge_opts' in opts['custom']:


-- 
To view, visit https://gerrit.ovirt.org/57151
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I62a102a2964ed5e5864ff516c71752a23c4c55cf
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ondřej Svoboda <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to