[lxc-devel] [lxc/master] Rename physical devices earlier during shutdown [WIP

Blub on Github lxc-bot at linuxcontainers.org
Wed Aug 17 14:55:24 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1311 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160817/9709d078/attachment.bin>
-------------- next part --------------
From 61fc2e3c58763f2e8b8af20e4e354bd01956242b Mon Sep 17 00:00:00 2001
From: Wolfgang Bumiller <w.bumiller at proxmox.com>
Date: Wed, 17 Aug 2016 11:36:39 +0200
Subject: [PATCH 1/2] conf: merge network namespace move & rename on shutdown

On shutdown we move physical network interfaces back to the
host namespace and rename them afterwards as well as in the
later lxc_network_delete() step. However, if the device had
a name which already exists in the host namespace then the
moving fails and so do the subsequent rename attempts. When
the namespace ceases to exist the devices finally end up
in the host namespace named 'dev<ID>' by the kernel.

In order to avoid this, we do the moving and renaming in a
single step (lxc_netdev_move_by_*()'s move & rename happen
in a single netlink transaction).

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/lxc/conf.c  | 29 ++++++++---------------------
 src/lxc/conf.h  |  2 +-
 src/lxc/start.c |  2 +-
 3 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 4ece410..7ba693d 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -2397,15 +2397,17 @@ static int setup_network(struct lxc_list *network)
 }
 
 /* try to move physical nics to the init netns */
-void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
+void lxc_restore_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf)
 {
 	int i, ret, oldfd;
 	char path[MAXPATHLEN];
 	char ifname[IFNAMSIZ];
 
-	if (netnsfd < 0)
+	if (netnsfd < 0 || conf->num_savednics == 0)
 		return;
 
+	INFO("running to reset %d nic names", conf->num_savednics);
+
 	ret = snprintf(path, MAXPATHLEN, "/proc/self/ns/net");
 	if (ret < 0 || ret >= MAXPATHLEN) {
 		WARN("Failed to open monitor netns fd");
@@ -2427,32 +2429,17 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf)
 			WARN("no interface corresponding to index '%d'", s->ifindex);
 			continue;
 		}
-		if (lxc_netdev_move_by_name(ifname, 1, NULL))
+		if (lxc_netdev_move_by_name(ifname, 1, s->orig_name))
 			WARN("Error moving nic name:%s back to host netns", ifname);
+		free(s->orig_name);
 	}
+	conf->num_savednics = 0;
+
 	if (setns(oldfd, 0) != 0)
 		SYSERROR("Failed to re-enter monitor's netns");
 	close(oldfd);
 }
 
-void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf)
-{
-	int i;
-
-	if (conf->num_savednics == 0)
-		return;
-
-	INFO("running to reset %d nic names", conf->num_savednics);
-	restore_phys_nics_to_netns(netnsfd, conf);
-	for (i=0; i<conf->num_savednics; i++) {
-		struct saved_nic *s = &conf->saved_nics[i];
-		INFO("resetting nic %d to %s", s->ifindex, s->orig_name);
-		lxc_netdev_rename_by_index(s->ifindex, s->orig_name);
-		free(s->orig_name);
-	}
-	conf->num_savednics = 0;
-}
-
 static char *default_rootfs_mount = LXCROOTFSMOUNT;
 
 struct lxc_conf *lxc_conf_init(void)
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 2593ce5..7c5786a 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -433,7 +433,7 @@ extern int do_rootfs_setup(struct lxc_conf *conf, const char *name,
 struct cgroup_process_info;
 extern int lxc_setup(struct lxc_handler *handler);
 
-extern void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf);
+extern void lxc_restore_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf);
 
 extern int find_unmapped_nsuid(struct lxc_conf *conf, enum idtype idtype);
 extern int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype);
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 2411626..53cd2bd 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1402,7 +1402,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf,
 	}
 
 	DEBUG("Pushing physical nics back to host namespace");
-	lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf);
+	lxc_restore_phys_nics_on_shutdown(netnsfd, handler->conf);
 
 	DEBUG("Tearing down virtual network devices used by container");
 	lxc_delete_network(handler);

From 4804966c94da0ea37898566b27c4bf557c63f27a Mon Sep 17 00:00:00 2001
From: Wolfgang Bumiller <w.bumiller at proxmox.com>
Date: Wed, 17 Aug 2016 15:28:22 +0200
Subject: [PATCH 2/2] conf: remove netdev-rename from lxc_network_delete

lxc_network_delete() is called in the following cases:
  lxc_spawn's out_delete_net label:
    No renaming has happened yet, the interfaces may have
    already been assigned to the container's namespace,
    renaming will be either unnecessary or fail.
  __lx_start on a regular shutdown:
    -) Txc_restore_phys_nics_on_shutdown() is called before
       it, making the extra rename superfluous.
    -) The network device is still in the container's
       namespace causing the rename to fail.
  __lxc_start's out_fini label:
    -) The network device is still in the container's
       namespace causing the rename to fail.

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 src/lxc/conf.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 7ba693d..ab63d7c 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -2885,9 +2885,6 @@ void lxc_delete_network(struct lxc_handler *handler)
 		netdev = iterator->elem;
 
 		if (netdev->ifindex != 0 && netdev->type == LXC_NET_PHYS) {
-			if (lxc_netdev_rename_by_index(netdev->ifindex, netdev->link))
-				WARN("failed to rename to the initial name the " \
-				     "netdev '%s'", netdev->link);
 			continue;
 		}
 


More information about the lxc-devel mailing list