From 389b9bf545a3b94fd0239080f57eb63fe6953f5f Mon Sep 17 00:00:00 2001
From: Akkela Saikrishna <Akkela.Saikrishna@ibm.com>
Date: Fri, 28 Jul 2023 19:13:26 +0530
Subject: [PATCH 2/3] commiting changes for cache fix and chdev correction

---
 cloud-init-22.1/cloudinit/distros/aix.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/cloud-init-22.1/cloudinit/distros/aix.py b/cloud-init-22.1/cloudinit/distros/aix.py
index e302946..e63765e 100644
--- a/cloud-init-22.1/cloudinit/distros/aix.py
+++ b/cloud-init-22.1/cloudinit/distros/aix.py
@@ -80,11 +80,21 @@ class Distro(distros.Distro):
         aix_util.disable_ndpd_host()
         aix_util.disable_autoconf6()
 
+        # Remove the chdev ipv6 entries present in the /etc/rc.tcpip from earlier runs.
+        # Read the content of the file and filter out lines containing both words
+        with open('/etc/rc.tcpip', "r") as infile:
+            lines = [line for line in infile if "chdev" not in line and "anetaddr6" not in line]
+
+        # Write the filtered lines back to the file
+        with open('/etc/rc.tcpip', "w") as outfile:
+            outfile.writelines(lines)
+
         for (dev, info) in list(entries.items()):
             print("dev %s info %s" % (dev, info))
 
         for (dev, info) in list(entries.items()):
             run_cmd = 0
+            ipv6_present = 0
             chdev_cmd = ['/usr/sbin/chdev']
 
             if dev not in 'lo':
@@ -100,6 +110,7 @@ class Distro(distros.Distro):
                     ipv6_info = info.get("ipv6")
                     if ipv6_info is not None and len(ipv6_info) > 0:
                         run_cmd = 1
+                        ipv6_present = 1
                         run_autoconf6 = True
                         ipv6_address = ipv6_info.get("address")
                         ipv6_netmask = ipv6_info.get("netmask")
@@ -116,6 +127,7 @@ class Distro(distros.Distro):
 
                     else:
                         run_cmd = 1
+                        ipv6_present = 0
                         ipv4_address = info.get("address")
                         ipv4_netmask = info.get("netmask")
                         if ipv4_address is not None:
@@ -128,7 +140,10 @@ class Distro(distros.Distro):
                             print("running ", chdev_cmd)
                             subp.subp(chdev_cmd, logstring=chdev_cmd)
                             time.sleep(2)
-                            util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(chdev_cmd))) 
+
+                            if ipv6_present:
+                                util.append_file("/etc/rc.tcpip", "%s\n" % (" ".join(chdev_cmd)))
+                            
                         except Exception as e:
                             raise e
 
-- 
2.38.3

