Commit 7b35f3d6 by Serge Hallyn

rename physical nics at shutdown

When a physical nic is being set up, store its ifindex and original name in struct lxc_conf. At reboot, reset the original name. We can't just go over the original network list in lxc_conf at shutdown because that may be tweaked in the meantime through the C api. The saved_nics list is only setup during lxc_spawn(), and restored and freed after lxc_start. Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/1086244 Changelog: remove non-effect change in execute.c Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent 4245ba50
...@@ -1821,6 +1821,21 @@ static int setup_network(struct lxc_list *network) ...@@ -1821,6 +1821,21 @@ static int setup_network(struct lxc_list *network)
return 0; return 0;
} }
void lxc_rename_phys_nics_on_shutdown(struct lxc_conf *conf)
{
int i;
INFO("running to reset %d nic names", conf->num_savednics);
for (i=0; i<conf->num_savednics; i++) {
struct saved_nic *s = &conf->saved_nics[i];
INFO("resetting nic %d to %s\n", s->ifindex, s->orig_name);
lxc_netdev_rename_by_index(s->ifindex, s->orig_name);
free(s->orig_name);
}
conf->num_savednics = 0;
free(conf->saved_nics);
}
static int setup_private_host_hw_addr(char *veth1) static int setup_private_host_hw_addr(char *veth1)
{ {
struct ifreq ifr; struct ifreq ifr;
...@@ -2710,6 +2725,18 @@ int lxc_clear_hooks(struct lxc_conf *c, const char *key) ...@@ -2710,6 +2725,18 @@ int lxc_clear_hooks(struct lxc_conf *c, const char *key)
return 0; return 0;
} }
void lxc_clear_saved_nics(struct lxc_conf *conf)
{
int i;
if (!conf->num_savednics)
return;
for (i=0; i < conf->num_savednics; i++)
free(conf->saved_nics[i].orig_name);
conf->saved_nics = 0;
free(conf->saved_nics);
}
void lxc_conf_free(struct lxc_conf *conf) void lxc_conf_free(struct lxc_conf *conf)
{ {
if (!conf) if (!conf)
...@@ -2737,5 +2764,6 @@ void lxc_conf_free(struct lxc_conf *conf) ...@@ -2737,5 +2764,6 @@ void lxc_conf_free(struct lxc_conf *conf)
lxc_clear_cgroups(conf, "lxc.cgroup"); lxc_clear_cgroups(conf, "lxc.cgroup");
lxc_clear_hooks(conf, "lxc.hook"); lxc_clear_hooks(conf, "lxc.hook");
lxc_clear_mount_entries(conf); lxc_clear_mount_entries(conf);
lxc_clear_saved_nics(conf);
free(conf); free(conf);
} }
...@@ -211,6 +211,11 @@ enum lxchooks { ...@@ -211,6 +211,11 @@ enum lxchooks {
LXCHOOK_POSTSTOP, NUM_LXC_HOOKS}; LXCHOOK_POSTSTOP, NUM_LXC_HOOKS};
extern char *lxchook_names[NUM_LXC_HOOKS]; extern char *lxchook_names[NUM_LXC_HOOKS];
struct saved_nic {
int ifindex;
char *orig_name;
};
struct lxc_conf { struct lxc_conf {
char *fstab; char *fstab;
int tty; int tty;
...@@ -221,6 +226,8 @@ struct lxc_conf { ...@@ -221,6 +226,8 @@ struct lxc_conf {
struct utsname *utsname; struct utsname *utsname;
struct lxc_list cgroup; struct lxc_list cgroup;
struct lxc_list network; struct lxc_list network;
struct saved_nic *saved_nics;
int num_savednics;
struct lxc_list mount_list; struct lxc_list mount_list;
struct lxc_list caps; struct lxc_list caps;
struct lxc_tty_info tty_info; struct lxc_tty_info tty_info;
...@@ -273,4 +280,6 @@ extern int lxc_clear_hooks(struct lxc_conf *c, const char *key); ...@@ -273,4 +280,6 @@ extern int lxc_clear_hooks(struct lxc_conf *c, const char *key);
*/ */
extern int lxc_setup(const char *name, struct lxc_conf *lxc_conf); extern int lxc_setup(const char *name, struct lxc_conf *lxc_conf);
extern void lxc_rename_phys_nics_on_shutdown(struct lxc_conf *conf);
#endif #endif
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include "log.h" #include "log.h"
#include "start.h" #include "start.h"
......
...@@ -579,6 +579,37 @@ out_warn_father: ...@@ -579,6 +579,37 @@ out_warn_father:
return -1; return -1;
} }
int save_phys_nics(struct lxc_conf *conf)
{
struct lxc_list *iterator;
lxc_list_for_each(iterator, &conf->network) {
struct lxc_netdev *netdev = iterator->elem;
if (netdev->type != LXC_NET_PHYS)
continue;
conf->saved_nics = realloc(conf->saved_nics,
(conf->num_savednics+1)*sizeof(struct saved_nic));
if (!conf->saved_nics) {
SYSERROR("failed to allocate memory");
return -1;
}
conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
if (!conf->saved_nics[conf->num_savednics].orig_name) {
SYSERROR("failed to allocate memory");
return -1;
}
INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics,
conf->saved_nics[conf->num_savednics].ifindex,
conf->saved_nics[conf->num_savednics].orig_name);
conf->num_savednics++;
}
return 0;
}
int lxc_spawn(struct lxc_handler *handler) int lxc_spawn(struct lxc_handler *handler)
{ {
int failed_before_rename = 0; int failed_before_rename = 0;
...@@ -613,6 +644,11 @@ int lxc_spawn(struct lxc_handler *handler) ...@@ -613,6 +644,11 @@ int lxc_spawn(struct lxc_handler *handler)
} }
} }
if (save_phys_nics(handler->conf)) {
ERROR("failed to save physical nic info");
goto out_abort;
}
/* /*
* if the rootfs is not a blockdev, prevent the container from * if the rootfs is not a blockdev, prevent the container from
* marking it readonly. * marking it readonly.
...@@ -739,6 +775,8 @@ int __lxc_start(const char *name, struct lxc_conf *conf, ...@@ -739,6 +775,8 @@ int __lxc_start(const char *name, struct lxc_conf *conf,
} }
} }
lxc_rename_phys_nics_on_shutdown(handler->conf);
err = lxc_error_set_and_log(handler->pid, status); err = lxc_error_set_and_log(handler->pid, status);
out_fini: out_fini:
lxc_delete_network(handler); lxc_delete_network(handler);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment