Rather than always using eth0.  Otherwise unpriv containers cannot have
multiple lxc.network.type = veth's without manually setting
lxc.network.name =.

Signed-off-by: Serge Hallyn <serge.hal...@ubuntu.com>
---
 src/lxc/lxc_user_nic.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/lxc/lxc_user_nic.c b/src/lxc/lxc_user_nic.c
index 1105b3d..75e4851 100644
--- a/src/lxc/lxc_user_nic.c
+++ b/src/lxc/lxc_user_nic.c
@@ -470,7 +470,7 @@ again:
        goto again;
 }
 
-static int rename_in_ns(int pid, char *oldname, char *newname)
+static int rename_in_ns(int pid, char *oldname, char **newnamep)
 {
        char nspath[MAXPATHLEN];
        int fd = -1, ofd = -1, ret;
@@ -495,8 +495,29 @@ static int rename_in_ns(int pid, char *oldname, char 
*newname)
                goto out_err;
        }
        close(fd); fd = -1;
-       if ((ret = lxc_netdev_rename_by_name(oldname, newname)) < 0) {
-               fprintf(stderr, "Error %d renaming netdev %s to %s in 
container\n", ret, oldname, newname);
+
+       if (!*newnamep) {
+               int i = 0;
+               char nicname[10];
+               while (i < 100) {
+                       ret = snprintf(nicname, 10, "eth%d", i);
+                       if (ret < 0 || ret >= 10)
+                               return -1;
+                       if (if_nametoindex(nicname) == 0)
+                               break;
+                       i++;
+               }
+               if (i == 100) {
+                       fprintf(stderr, "too many nics in container\n");
+                       return -1;
+               }
+               *newnamep = strdup(nicname);
+               if (!*newnamep)
+                       return -1;
+       }
+
+       if ((ret = lxc_netdev_rename_by_name(oldname, *newnamep)) < 0) {
+               fprintf(stderr, "Error %d renaming netdev %s to %s in 
container\n", ret, oldname, *newnamep);
                goto out_err;
        }
        if (setns(ofd, 0) < 0) {
@@ -579,7 +600,7 @@ int main(int argc, char *argv[])
        if (argc >= 5)
                vethname = argv[4];
        else
-               vethname = "eth0";
+               vethname = NULL;
 
        errno = 0;
        pid = (int) strtol(argv[1], NULL, 10);
@@ -614,7 +635,7 @@ int main(int argc, char *argv[])
        }
 
        // Now rename the link
-       if (rename_in_ns(pid, cnic, vethname) < 0) {
+       if (rename_in_ns(pid, cnic, &vethname) < 0) {
                fprintf(stderr, "Failed to rename the link\n");
                exit(1);
        }
-- 
1.9.1

_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to