[lxc-devel] [lxc/master] wishlist: prefix veth interface name with uid info

hn on Github lxc-bot at linuxcontainers.org
Tue Jan 8 20:43:04 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1278 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190108/7d0c061b/attachment.bin>
-------------- next part --------------
From 5e84c5cc393e02be43ce32ee0d72b3250d39a404 Mon Sep 17 00:00:00 2001
From: hn <hn at users.noreply.github.com>
Date: Tue, 8 Jan 2019 21:23:41 +0100
Subject: [PATCH] prefix veth interface name with uid info

Signed-off-by: Hajo Noerenberg <hajo-github at noerenberg.de>
---
 src/lxc/network.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/lxc/network.c b/src/lxc/network.c
index 499ddff6e..895ab59ae 100644
--- a/src/lxc/network.c
+++ b/src/lxc/network.c
@@ -1953,6 +1953,8 @@ char *lxc_mkifname(char *template)
 	char name[IFNAMSIZ];
 	bool exists = false;
 	size_t i = 0;
+	uid_t uid = getuid();
+	unsigned int uidpad;
 #ifdef HAVE_RAND_R
 	unsigned int seed;
 
@@ -1978,14 +1980,21 @@ char *lxc_mkifname(char *template)
 		(void)strlcpy(name, template, IFNAMSIZ);
 
 		exists = false;
+		uidpad = (unsigned int) uid;
 
 		for (i = 0; i < strlen(name); i++) {
 			if (name[i] == 'X') {
+				/* insert max 4 chars UID info, 1679615 = base36 'ZZZZ' */
+				if (uidpad > 0 && uidpad <= 1679615) {
+					name[i] = padchar[uidpad % strlen(padchar)];
+					uidpad /= strlen(padchar);
+				} else {
 #ifdef HAVE_RAND_R
-				name[i] = padchar[rand_r(&seed) % strlen(padchar)];
+					name[i] = padchar[rand_r(&seed) % strlen(padchar)];
 #else
-				name[i] = padchar[rand() % strlen(padchar)];
+					name[i] = padchar[rand() % strlen(padchar)];
 #endif
+				}
 			}
 		}
 


More information about the lxc-devel mailing list