Skip to content

Commit d0f9d8c

Browse files
committed
CP-54444: In upgrade convert interface-rename data for networkd
The interface-rename functionality will disappear after the upgrade. The networkd will replace the interface-rename to maintain the order of the host network devices without renaming them. The order generated and maintained by interface-rename before upgrade should be passed to the networkd during upgrade so that the networkd can still keep the order after upgrade. This commit is to transform the data during upgrade. Signed-off-by: Ming Lu <[email protected]>
1 parent 8b41fbf commit d0f9d8c

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

netutil.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,13 @@ def disable_ipv6_module(root):
371371
dv6fd.write("alias net-pf-10 off\n")
372372
dv6fd.close()
373373

374+
375+
from xcp.net.ifrename.dynamic import DynamicRules
376+
377+
def net_devs_of_last_boot(file_path):
378+
dynamic_rules = DynamicRules(file_path)
379+
if not dynamic_rules.load_and_parse():
380+
LOG.warning(f"Failed to parse the interface-rename dynamic rules.")
381+
return []
382+
else:
383+
return [(d.tname, d.mac.as_string(sep=":")) for d in dynamic_rules.lastboot]

upgrade.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,47 @@ def restore_file(src_base, f, d=None, attr=None):
174174
else:
175175
logger.log("WARNING: /%s did not exist in the backup image." % f)
176176

177+
def convert_interface_rename_data(src, dest):
178+
"""
179+
Convert data of interface-rename to the data consumed by xcp-networkd.
180+
src - the mount point of backup partition which contains the interface-rename data.
181+
dest - the mount point of the root partition of installation.
182+
"""
183+
interface_rename_dir = os.path.join(
184+
src,
185+
'etc/sysconfig/network-scripts/interface-rename-data/'
186+
)
187+
src_file_path = os.path.join(interface_rename_dir, 'dynamic-rules.json')
188+
dest_file_path = os.path.join(
189+
dest,
190+
constants.FIRSTBOOT_DATA_DIR,
191+
'initial_network_device_rules.conf'
192+
)
193+
net_devs = []
194+
if os.path.exists(src_file_path):
195+
net_devs = netutil.net_devs_of_last_boot(src_file_path)
196+
else:
197+
logger.log(f"The interface-rename data file {src_file_path} does not exist.")
198+
199+
if not net_devs:
200+
return
201+
202+
# The sorting result of interface-rename is to rename the interfaces to eth<N>
203+
pattern = re.compile(r'^eth(\d+)$')
204+
try:
205+
with open(dest_file_path, mode='w', encoding='utf-8') as f:
206+
for (name, mac) in net_devs:
207+
match = pattern.search(name)
208+
if match:
209+
position = match.group(1)
210+
line = f'{position}:mac="{mac}"\n'
211+
logger.log(f"Converted interface-rename data: {line}")
212+
f.write(line)
213+
else:
214+
logger.error(f'Network device name {name} is not like ethN.')
215+
except Exception as e:
216+
logger.error(f"Failed to convert interface-rename data: {e}")
217+
177218
backup_volume = partitionDevice(target_disk, backup_partnum)
178219
tds = util.TempMount(backup_volume, 'upgrade-src-', options=['ro'])
179220
try:
@@ -200,6 +241,9 @@ def restore_file(src_base, f, d=None, attr=None):
200241
restore_file(tds.mount_point, fn, attr=f.get('attr'))
201242
else:
202243
restore_file(tds.mount_point, f['dir'])
244+
245+
convert_interface_rename_data(tds.mount_point, mounts['root'])
246+
203247
finally:
204248
tds.unmount()
205249

@@ -441,10 +485,6 @@ def buildRestoreList(self, src_base):
441485

442486
restore_list.append('etc/sysconfig/mkinitrd.latches')
443487

444-
# EA-1069: Udev network device naming
445-
restore_list += [{'dir': 'etc/sysconfig/network-scripts/interface-rename-data'}]
446-
restore_list += [{'dir': 'etc/sysconfig/network-scripts/interface-rename-data/.from_install'}]
447-
448488
# CA-67890: preserve root's ssh state
449489
restore_list += [{'dir': 'root/.ssh'}]
450490

0 commit comments

Comments
 (0)