From 76920b1ab0b7204c2feaf7dc2fa04b6d72730b5a Mon Sep 17 00:00:00 2001
From: lixin <lixinb@uniontech.com>
Date: Wed, 6 Sep 2023 17:23:29 +0800
Subject: [PATCH] modify grub rules to match NIC name

---
 .../centos7/openeuler/centos72openeuler.py    | 41 ++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/ut-Migration-tools/ut-Migration-tools-0.1/centos7/openeuler/centos72openeuler.py b/ut-Migration-tools/ut-Migration-tools-0.1/centos7/openeuler/centos72openeuler.py
index f24d765..2be7b6b 100644
--- a/ut-Migration-tools/ut-Migration-tools-0.1/centos7/openeuler/centos72openeuler.py
+++ b/ut-Migration-tools/ut-Migration-tools-0.1/centos7/openeuler/centos72openeuler.py
@@ -83,6 +83,43 @@ def add_boot_option():
     except Exception as e:
         print(e)
 
+
+def set_grub_biosdev_rules():
+    """
+    Set grub bisodev rule which can be don`t modify network configuration names.
+    When the leapp has been made initrd, the function will set net.ifnames in /etc/default/grub.
+    Returns:
+    """
+    default_grub_path = "/etc/default/grub"
+    set_content = "net.ifnames=0  biosdevname=0"
+    if not os.path.exists(default_grub_path):
+        return
+    with open(default_grub_path, 'r') as gf:
+        gret = gf.readlines()
+        gf.close()
+    grub_content = ''
+    for i in range(len(gret)):
+        if "GRUB_CMDLINE_LINUX" in gret[i]:
+            cmdline_tmp = gret[i].split('"', -1)[1]
+            grub_content += 'GRUB_CMDLINE_LINUX="' + cmdline_tmp + ' ' + set_content + '"\n'
+            continue
+        grub_content += gret[i]
+    try:
+        if not os.path.exists(default_grub_path + '.disable'):
+            shutil.copyfile(default_grub_path, default_grub_path + '.disable')
+            os.remove(default_grub_path)
+        else:
+            print("grub file has been modified")
+            return
+    except Exception as e:
+        print(e)
+        return
+    with open(default_grub_path, 'w+') as wgf:
+        wgf.write(grub_content)
+        wgf.close()
+    return True
+
+
 def swap_release(release):
     tmp_dir = '/var/tmp'
     rpme_release = 'rpm -qf /etc/os-release | xargs -i rpm -e --nodeps {}'
@@ -214,8 +251,10 @@ def main():
     yum_conflict_dir = '/etc/yum/'
     if os.path.exists(yum_conflict_dir):
         shutil.rmtree(yum_conflict_dir)
-    print("Installing yum...")
+    print("Configuring grub...")
+    set_grub_biosdev_rules()
     conf_grub()
+    print("Installing yum...")
     run_subprocess('dnf install -y yum'.split())
     
     print("System migration completed, rebooting system")
-- 
2.41.0

