[lxc-devel] [PATCH 1/1] lxc-user-nic: compute first available nic name in container
Stéphane Graber
stgraber at ubuntu.com
Mon Jul 21 19:34:14 UTC 2014
On Mon, Jul 21, 2014 at 07:27:02PM +0000, Serge Hallyn wrote:
> 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 at 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;
> + }
As I said on IRC, are you sure we can't somehow get rid of all of that
code and just somehow pass 'eth%d' to the kernel through netlink which
will then make it use the next available ethX device (we already use
that trick for privileged networking).
> +
> + 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 at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel
--
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140721/c8c5df64/attachment-0001.sig>
More information about the lxc-devel
mailing list