]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
unprivileged containers: use next available nic name if unspecified
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 21 Jul 2014 21:36:44 +0000 (16:36 -0500)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Wed, 23 Jul 2014 14:42:16 +0000 (09:42 -0500)
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.hallyn@ubuntu.com>
src/lxc/lxc_user_nic.c

index 1105b3d0eaf6771d6d182489e0b39aba307c15ba..5a1f6dbd999059a6e919fb4a2095ad6f0d2b7fce 100644 (file)
@@ -470,10 +470,13 @@ again:
        goto again;
 }
 
-static int rename_in_ns(int pid, char *oldname, char *newname)
+#define VETH_DEF_NAME "eth%d"
+
+static int rename_in_ns(int pid, char *oldname, char **newnamep)
 {
        char nspath[MAXPATHLEN];
-       int fd = -1, ofd = -1, ret;
+       int fd = -1, ofd = -1, ret, ifindex;
+       bool grab_newname = false;
 
        ret = snprintf(nspath, MAXPATHLEN, "/proc/%d/ns/net", getpid());
        if (ret < 0 || ret >= MAXPATHLEN)
@@ -495,10 +498,28 @@ 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) {
+               grab_newname = true;
+               *newnamep = VETH_DEF_NAME;
+               if (!(ifindex = if_nametoindex(oldname))) {
+                       fprintf(stderr, "failed to get netdev index\n");
+                       goto out_err;
+               }
+       }
+       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 (grab_newname) {
+               char ifname[IFNAMSIZ], *namep = ifname;
+               if (!if_indextoname(ifindex, namep)) {
+                       fprintf(stderr, "Failed to get new netdev name\n");
+                       goto out_err;
+               }
+               *newnamep = strdup(namep);
+               if (!*newnamep)
+                       goto out_err;
+       }
        if (setns(ofd, 0) < 0) {
                fprintf(stderr, "Error returning to original netns\n");
                close(ofd);
@@ -566,7 +587,7 @@ int main(int argc, char *argv[])
        char *me;
        char *nicname = alloca(40);
        char *cnic = NULL; // created nic name in container is returned here.
-       char *vethname;
+       char *vethname = NULL;
        int pid;
 
        if ((me = get_username()) == NULL) {
@@ -578,8 +599,6 @@ int main(int argc, char *argv[])
                usage(argv[0], true);
        if (argc >= 5)
                vethname = argv[4];
-       else
-               vethname = "eth0";
 
        errno = 0;
        pid = (int) strtol(argv[1], NULL, 10);
@@ -614,7 +633,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);
        }