Skip to contentSkip to conversation

swapping interface names (again)

DISCUSSION MS Colclough 5 participants 9 messages Jun 24, 2006 – Jun 26, 2006

lore
Share & tools
lore
#0 lore raw

Sorry to labour this point. I understand that swapping eth0 and eth1 (should that prove necessary because of the initial ordering) can be done by an application like ifrename, by making use of a temporary name and invoking dev_change_name() in the kernel. But does the recent answer:
On 2006-06-15 06:29, Kay Sievers wrote:
...

Quoted 3 lines

You probably try to rename the interface to an already existing name. If you want to swap names, you have to give one interface a temporary name first.

imply that the whole process can be done by some udev rules that run at boot time? If so, how please?

Personally, I am happier with meaningful names, which I know how to substitute using udev, but we have some users (and scripts!) who would be happier with the old names, nailed to particular interfaces.

Thanks,
Mark
Zenwalk udev warrior

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#1 lore raw
Quoted 2 lines · MS Colclough

imply that the whole process can be done by some udev rules that run at boot time? If so, how please?

Look at the Debian package for a simple script which generates rules on demand.

Signature · 11 lines
ciao,
Marco

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#2 lore raw
In reply to MS Colclough

I accidentally only sent my message to Marco. Here it is for the list also:

Quoting Marco d'Itri <[email protected]>:

Quoted 6 lines · MS Colclough
Quoted 3 lines

imply that the whole process can be done by some udev rules that run at boot time? If so, how please?

Look at the Debian package for a simple script which generates rules on demand.

It's of course easy to write a script that creates something like z99-network.rules. But what should it contain?

If I have eth0 and eth1, and want to swap their names, I first need to rename them to e.g. tempeth0 and tempeth1, and then to eth1 and eth0. But what kind of rule would recognise when all temporary names are created and apply the final ones?

udevmonitor doesn't show any event where the interface is renamed. The last line is "add@/class/net/eth0", even though that interface was renamed tempeth0 by a rule.

I tried to catch the renaming event with 'KERNEL="tempeth0", NAME="eth1"' and 'NAME="tempeth0", NAME="eth1"', but they didn't work. And even if they did, how could I be sure that the original eth1 wasn't still around messing things up? Both temp interface names would need to be set.

Any ideas?

Thanks,
Juuso

----------------------------------------------------------------
This mail sent through L-secure: http://www.l-secure.net/

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to MS Colclough
Quoted 11 lines · [email protected] wrote:

I accidentally only sent my message to Marco. Here it is for the list also:

Quoting Marco d'Itri <[email protected]>:

Quoted 6 lines · MS Colclough
Quoted 3 lines

imply that the whole process can be done by some udev rules that run at boot time? If so, how please?

Look at the Debian package for a simple script which generates rules on demand.

Quoted 7 lines

It's of course easy to write a script that creates something like z99-network.rules. But what should it contain?

If I have eth0 and eth1, and want to swap their names, I first need to rename them to e.g. tempeth0 and tempeth1, and then to eth1 and eth0. But what kind of rule would recognise when all temporary names are created and apply the final ones?

Don't look at the Debian package. It is very likely to contain this problem, because for my single-card setup, Debian udev 0.093-1 writes:

# This file was automatically generated by the /lib/udev/write_net_rules # program, probably run by the persistent-net-generator.rules rules file. #
# You can modify it, as long as you keep each rule on a single line.

# PCI device 10ec:8139 (8139too)
SUBSYSTEM="net", DRIVER="?*", SYSFS{address}="00:e0:4c:7c:4a:d3", NAME="eth0"

Of course this will break if I a new card in such a way (read: PCI slot) so that without this rule it becomes eth0. Then this rule will attempt to rename my old card to eth0, and boom... there is already eth0.

And I don't know any solution that allows eth%d as a final name.

Signature · 10 lines
Alexander E. Patrakov

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#4 lore raw
In reply to MS Colclough
Quoted 2 lines · Alexander E. Patrakov

Don't look at the Debian package. It is very likely to contain this problem, because for my single-card setup, Debian udev 0.093-1 writes:

It's not, because there is an Ubuntu-derived patch which prevents this. I have been shipping it since february with no issues, so maybe it's time to consider merging it.

Signature · 11 lines
ciao,
Marco

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#5 lore raw
In reply to MS Colclough

Quoting Marco d'Itri <[email protected]>:

Quoted 8 lines · Alexander E. Patrakov
Quoted 2 lines

Don't look at the Debian package. It is very likely to contain this

problem,

Quoted 1 line

because for my single-card setup, Debian udev 0.093-1 writes:

It's not, because there is an Ubuntu-derived patch which prevents this. I have been shipping it since february with no issues, so maybe it's time to consider merging it.

That would be useful. Does the patch really allow normal interface naming rules (as described in the Writing udev rules document) to bind to existing names without any extra steps needed, or have I not understood this correctly?

I'd be happy to try the patch, but I'm not C-literate and couldn't tell from the debian diff which parts of it are relevant for this feature. Is the patch available as separate file somewhere?

Juuso

----------------------------------------------------------------
This mail sent through L-secure: http://www.l-secure.net/

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#6 lore raw
In reply to MS Colclough
Quoted 3 lines · On Jun 25, [email protected] wrote:

That would be useful. Does the patch really allow normal interface naming rules (as described in the Writing udev rules document) to bind to existing names without any extra steps needed, or have I not understood this correctly?

Yes.

Quoted 3 lines

I'd be happy to try the patch, but I'm not C-literate and couldn't tell from the debian diff which parts of it are relevant for this feature. Is the patch available as separate file somewhere?

zcat udev*.diff.gz | patch -p1
cd debian/patches/

Signature · 11 lines
ciao,
Marco

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#7 lore raw
In reply to MS Colclough

I also found the Ubuntu patch (70-ifrename.patch). Taking just the part that deals with the rename_net_if() function, I found it to be exactly what I want. A pair of rules like
... SYSFS{address}="00:16:17:5a:e5:7e", NAME="eth0"
... SYSFS{address}="00:40:95:46:f4:16", NAME="eth1"
enforce the necessary stability, swapping the kernel names on occasions when that proves necessary.

I would certainly like to see this merged with udev. It's certainly better than me telling users "you can have any names you like, except those you are used to".

Thanks, Mark

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#8 lore raw
In reply to MS Colclough
Quoted 8 lines · Marco d'Itri
Quoted 2 lines · Alexander E. Patrakov

Don't look at the Debian package. It is very likely to contain this problem, because for my single-card setup, Debian udev 0.093-1 writes:

It's not, because there is an Ubuntu-derived patch which prevents this. I have been shipping it since february with no issues, so maybe it's time to consider merging it.

That patch is normally sent out with my occasional "Ubuntu Collection" mails (one is due soon as we're getting back up to date with upstream post-release), here it is applied to 093.

Scott

Signature · 2 lines
Scott James Remnant
[email protected]
udev_device.c +96 -3
diff -ruNp udev-093~/udev_device.c udev-093/udev_device.c
@@ -30,6 +30,8 @@
 #include <sys/socket.h>
 #include <net/if.h>
 #include <linux/sockios.h>
+#include <errno.h>
+#include <ctype.h>
 
 #include "udev.h"
 #include "udev_rules.h"
@@ -102,10 +104,97 @@ static int rename_net_if(struct udevice 
 	strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
 
 	retval = ioctl(sk, SIOCSIFNAME, &ifr);
-	if (retval != 0)
-		err("error changing net interface name: %s", strerror(errno));
+	if (retval != 0) {
+		if (errno != EEXIST) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Destination interface already exits.
+		 * First rename our interface to something temporary in case
+		 * we're trying to swap with that one. */
+		strlcpy(ifr.ifr_newname, udev->dev->kernel_name, IFNAMSIZ);
+		strlcat(ifr.ifr_newname, "_ifrename", IFNAMSIZ);
+
+		retval = ioctl(sk, SIOCSIFNAME, &ifr);
+		if (retval != 0) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Now we loop until our target interface goes away. */
+		strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
+		strlcpy(ifr.ifr_newname, udev->name, IFNAMSIZ);
+		while ((retval = ioctl(sk, SIOCSIFNAME, &ifr)) != 0) {
+			if (errno != EEXIST) {
+				err("error changing net interface name: %s", strerror(errno));
+				break;
+			}
+
+			usleep(50000);
+		}
+	}
+
+error:
 	close(sk);
+	return retval;
+}
+
+static int rename_net_if_increment(struct udevice *udev)
+{
+	struct ifreq ifr;
+	char *ptr, *end;
+	int dev = 0, sk, retval;
+
+	info("changing net interface name from '%s' to first matching '%s'", udev->dev->kernel_name, udev->name);
+	if (udev->test_run)
+		return 0;
+
+
+	/* Split the name up */
+	ptr = udev->name;
+	while (*ptr && *ptr != '*')
+		ptr++;
+
+	end = ptr + 1;
+	if (isdigit(*end))
+		dev = atoi(end);
 
+	while (isdigit(*end))
+		end++;
+
+
+	sk = socket(PF_INET, SOCK_DGRAM, 0);
+	if (sk < 0) {
+		err("error opening socket: %s", strerror(errno));
+		return -1;
+	}
+
+	memset(&ifr, 0x00, sizeof(struct ifreq));
+	strlcpy(ifr.ifr_name, udev->dev->kernel_name, IFNAMSIZ);
+
+	do {
+		strncpy(ifr.ifr_newname, udev->name, ptr - udev->name);
+		sprintf(ifr.ifr_newname + (ptr - udev->name), "%d%s",
+			dev, end);
+
+		retval = ioctl(sk, SIOCSIFNAME, &ifr);
+		if (retval == 0) {
+			strlcpy(udev->name, ifr.ifr_newname, sizeof(udev->name));
+			break;
+		}
+
+		if (errno != EEXIST) {
+			err("error changing net interface name: %s", strerror(errno));
+			goto error;
+		}
+
+		/* Try the next one */
+		dev++;
+	} while (1);
+
+error:
+	close(sk);
 	return retval;
 }
 
@@ -159,7 +248,11 @@ int udev_device_event(struct udev_rules 
 		if (strcmp(udev->name, udev->dev->kernel_name) != 0) {
 			char *pos;
 
-			retval = rename_net_if(udev);
+			if (strchr(udev->name, '*')) {
+				retval = rename_net_if_increment(udev);
+			} else {
+				retval = rename_net_if(udev);
+			}
 			if (retval != 0)
 				goto exit;
 			info("renamed netif to '%s'", udev->name);
udev_utils_string.c +1 -1
diff -ruNp udev-093~/udev_utils_string.c udev-093/udev_utils_string.c
@@ -241,7 +241,7 @@ int replace_untrusted_chars(char *str)
 		if ((str[i] >= '0' && str[i] <= '9') ||
 		    (str[i] >= 'A' && str[i] <= 'Z') ||
 		    (str[i] >= 'a' && str[i] <= 'z') ||
-		    strchr("#$%+-./:=?@_,", str[i])) {
+		    strchr("#$%+-./:=?*@_,", str[i])) {
 			i++;
 			continue;
 		}

Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel