]>
Commit | Line | Data |
---|---|---|
cd70d421 WB |
1 | From 5610055a113f46a017bad687c40e7570da2a7035 Mon Sep 17 00:00:00 2001 |
2 | From: Wolfgang Bumiller <w.bumiller@proxmox.com> | |
3 | Date: Wed, 17 Aug 2016 11:36:39 +0200 | |
4 | Subject: [PATCH] conf: merge network namespace move & rename on shutdown | |
5 | ||
6 | On shutdown we move physical network interfaces back to the | |
7 | host namespace and rename them afterwards as well as in the | |
8 | later lxc_network_delete() step. However, if the device had | |
9 | a name which already exists in the host namespace then the | |
10 | moving fails and so do the subsequent rename attempts. When | |
11 | the namespace ceases to exist the devices finally end up | |
12 | in the host namespace named 'dev<ID>' by the kernel. | |
13 | ||
14 | In order to avoid this, we do the moving and renaming in a | |
15 | single step (lxc_netdev_move_by_*()'s move & rename happen | |
16 | in a single netlink transaction). | |
17 | ||
18 | Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com> | |
19 | --- | |
20 | src/lxc/conf.c | 29 ++++++++--------------------- | |
21 | src/lxc/conf.h | 2 +- | |
22 | src/lxc/start.c | 2 +- | |
23 | 3 files changed, 10 insertions(+), 23 deletions(-) | |
24 | ||
25 | diff --git a/src/lxc/conf.c b/src/lxc/conf.c | |
26 | index f688f94..b2e0fd9 100644 | |
27 | --- a/src/lxc/conf.c | |
28 | +++ b/src/lxc/conf.c | |
29 | @@ -2397,15 +2397,17 @@ static int setup_network(struct lxc_list *network) | |
30 | } | |
31 | ||
32 | /* try to move physical nics to the init netns */ | |
33 | -void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf) | |
34 | +void lxc_restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf) | |
35 | { | |
36 | int i, ret, oldfd; | |
37 | char path[MAXPATHLEN]; | |
38 | char ifname[IFNAMSIZ]; | |
39 | ||
40 | - if (netnsfd < 0) | |
41 | + if (netnsfd < 0 || conf->num_savednics == 0) | |
42 | return; | |
43 | ||
44 | + INFO("running to reset %d nic names", conf->num_savednics); | |
45 | + | |
46 | ret = snprintf(path, MAXPATHLEN, "/proc/self/ns/net"); | |
47 | if (ret < 0 || ret >= MAXPATHLEN) { | |
48 | WARN("Failed to open monitor netns fd"); | |
49 | @@ -2427,32 +2429,17 @@ void restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf) | |
50 | WARN("no interface corresponding to index '%d'", s->ifindex); | |
51 | continue; | |
52 | } | |
53 | - if (lxc_netdev_move_by_name(ifname, 1, NULL)) | |
54 | + if (lxc_netdev_move_by_name(ifname, 1, s->orig_name)) | |
55 | WARN("Error moving nic name:%s back to host netns", ifname); | |
56 | + free(s->orig_name); | |
57 | } | |
58 | + conf->num_savednics = 0; | |
59 | + | |
60 | if (setns(oldfd, 0) != 0) | |
61 | SYSERROR("Failed to re-enter monitor's netns"); | |
62 | close(oldfd); | |
63 | } | |
64 | ||
65 | -void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf) | |
66 | -{ | |
67 | - int i; | |
68 | - | |
69 | - if (conf->num_savednics == 0) | |
70 | - return; | |
71 | - | |
72 | - INFO("running to reset %d nic names", conf->num_savednics); | |
73 | - restore_phys_nics_to_netns(netnsfd, conf); | |
74 | - for (i=0; i<conf->num_savednics; i++) { | |
75 | - struct saved_nic *s = &conf->saved_nics[i]; | |
76 | - INFO("resetting nic %d to %s", s->ifindex, s->orig_name); | |
77 | - lxc_netdev_rename_by_index(s->ifindex, s->orig_name); | |
78 | - free(s->orig_name); | |
79 | - } | |
80 | - conf->num_savednics = 0; | |
81 | -} | |
82 | - | |
83 | static char *default_rootfs_mount = LXCROOTFSMOUNT; | |
84 | ||
85 | struct lxc_conf *lxc_conf_init(void) | |
86 | diff --git a/src/lxc/conf.h b/src/lxc/conf.h | |
87 | index e484667..842e4dc 100644 | |
88 | --- a/src/lxc/conf.h | |
89 | +++ b/src/lxc/conf.h | |
90 | @@ -440,7 +440,7 @@ extern int do_rootfs_setup(struct lxc_conf *conf, const char *name, | |
91 | struct cgroup_process_info; | |
92 | extern int lxc_setup(struct lxc_handler *handler); | |
93 | ||
94 | -extern void lxc_rename_phys_nics_on_shutdown(int netnsfd, struct lxc_conf *conf); | |
95 | +extern void lxc_restore_phys_nics_to_netns(int netnsfd, struct lxc_conf *conf); | |
96 | ||
97 | extern int find_unmapped_nsuid(struct lxc_conf *conf, enum idtype idtype); | |
98 | extern int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype); | |
99 | diff --git a/src/lxc/start.c b/src/lxc/start.c | |
100 | index ecc7b08..0da7f9f 100644 | |
101 | --- a/src/lxc/start.c | |
102 | +++ b/src/lxc/start.c | |
103 | @@ -1420,7 +1420,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf, | |
104 | } | |
105 | ||
106 | DEBUG("Pushing physical nics back to host namespace"); | |
107 | - lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf); | |
108 | + lxc_restore_phys_nics_to_netns(netnsfd, handler->conf); | |
109 | ||
110 | DEBUG("Tearing down virtual network devices used by container"); | |
111 | lxc_delete_network(handler); | |
112 | -- | |
113 | 2.1.4 | |
114 |