From: Robert Milasan <rmilasan@suse.com>
Cc: Werner Fink <werner@suse.de>
Subject: udev always rename network

Date: Thu, 28 Mar 2013 09:24:43 +0000
udev: ensure that the network interfaces are renamed even if they exist (bnc#809843).

Date: Tue,  4 Mar 2014 10:29:21 +0000
Port the patch of Robert to systemd v210 and test it out.

---
 udev-event.c |   42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)


--- systemd-210/src/udev/udev-event.c
+++ systemd-210/src/udev/udev-event.c	2014-03-27 13:19:06.727748307 +0000
@@ -750,8 +750,9 @@ static int rename_netif(struct udev_even
         struct udev_device *dev = event->dev;
         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
         char name[IFNAMSIZ];
+        char interim[IFNAMSIZ], *ptr = &interim[0];
         const char *oldname;
-        int r;
+        int r, loop;
 
         oldname = udev_device_get_sysname(dev);
 
@@ -765,12 +766,45 @@ static int rename_netif(struct udev_even
                 return r;
 
         r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), name);
-        if (r < 0)
+        if (r == 0) {
+                print_kmsg("renamed network interface %s to %s\n", oldname, name);
+                return r;
+        } else if (r != -EEXIST) {
                 log_error("error changing net interface name %s to %s: %s",
                           oldname, name, strerror(-r));
-        else
-                print_kmsg("renamed network interface %s to %s", oldname, name);
+                return r;
+        }
+
+        /* free our own name, another process may wait for us */
+        strpcpyf(&ptr, IFNAMSIZ, "rename%u", udev_device_get_ifindex(dev));
+
+        r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), interim);
+        if (r < 0) {
+                log_error("error changing net interface name %s to %s: %s",
+                          oldname, interim, strerror(-r));
+                return r;
+        }
 
+        /* log temporary name */
+        print_kmsg("renamed network interface %s to %s\n", oldname, interim);
+
+        loop = 90 * 20;
+        while (loop--) {
+                const struct timespec duration = { 0, 1000 * 1000 * 1000 / 20 };
+                nanosleep(&duration, NULL);
+
+                r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), name);
+                if (r == 0) {
+                        print_kmsg("renamed network interface %s to %s\n", interim, name);
+                        break;
+                }
+
+                if (r != -EEXIST) {
+                        log_error("error changing net interface name %s to %s: %s",
+                                  interim, name, strerror(-r));
+                        break;
+                }
+        }
         return r;
 }
 
