From e21318d22359c7160ea7c7f4a610b28a30d48c84 Mon Sep 17 00:00:00 2001
From: systemd team <systemd-maint@redhat.com>
Date: Tue, 7 Mar 2017 08:20:10 +0000
Subject: [PATCH] udev-add-actions-while-rename-netif-failed
---
 src/udev/udev-event.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index e1c2baf..b64d279 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -820,6 +820,7 @@ static int rename_netif(UdevEvent *event) {
         sd_device *dev = event->dev;
         const char *oldname;
         int ifindex, r;
+        char name[IFNAMSIZ];
 
         if (!event->name)
                 return 0; /* No new name is requested. */
@@ -860,12 +861,45 @@ static int rename_netif(UdevEvent *event) {
         if (r < 0)
                 return log_device_debug_errno(event->dev_db_clone, r, "Failed to update database under /run/udev/data/: %m");
 
-        r = rtnl_set_link_name(&event->rtnl, ifindex, event->name);
+        strscpy(name, IFNAMSIZ, event->name);
+
+        r = rtnl_set_link_name(&event->rtnl, ifindex, name);
         if (r < 0)
-                return log_device_error_errno(dev, r, "Failed to rename network interface %i from '%s' to '%s': %m",
-                                              ifindex, oldname, event->name);
+        {
+                int loop;
+                if (r != -EEXIST) {
+                        return log_error_errno(r, "error changing net interface name '%s' to '%s': %m", oldname, name);
+                }
+
+                snprintf(name, IFNAMSIZ, "rename%u", ifindex);
+                r = rtnl_set_link_name(&event->rtnl, ifindex, name);
+                if (r < 0) {
+                        return log_error_errno(r, "error changing net interface name '%s' to '%s': %m", oldname, name);
+                }
+
+                log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, name);
+
+                /* wait 90 seconds for our target to become available */
+                loop = 90 * 20;
+                while (loop--) {
+                        const struct timespec duration = { 0, 1000 * 1000 * 1000 / 20 };
+
+                        r = rtnl_set_link_name(&event->rtnl, ifindex, event->name);
+                        if (r == 0) {
+                                log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, name, event->name);
+                                return 0;
+                        }
+
+                        if (r != -EEXIST) {
+                                return log_error_errno(r, "error changing net interface name '%s' to '%s': %m", name, event->name);
+                        }
+                        log_debug( "wait for netif '%s' to become free, loop=%i\n",
+                                event->name, (90 * 20) - loop);
+                        nanosleep(&duration, NULL);
+                }
+        }
 
-        log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, event->name);
+        log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, event->name);
 
         return 1;
 }
-- 
2.23.0

