From 6c544cb300752a9f799cda19e9c5b57ab890c8dd Mon Sep 17 00:00:00 2001 From: Marek Majkowski Date: Wed, 13 Nov 2013 05:20:26 -0800 Subject: [PATCH] Make it possible to share UTS namespace --- doc/lxc-start.sgml.in | 15 +++++++++++++++ src/lxc/conf.c | 8 +++++--- src/lxc/lxc_start.c | 4 ++++ src/lxc/start.c | 10 ++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/doc/lxc-start.sgml.in b/doc/lxc-start.sgml.in index 86024f66d..766a4ec2b 100644 --- a/doc/lxc-start.sgml.in +++ b/doc/lxc-start.sgml.in @@ -217,6 +217,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + + + + + + Inherit a UTS namespace from + a name container or + a pid. The starting LXC will + not set the hostname, but the container OS may do it + anyway. + + + + diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 3bebe46d1..dfc49e435 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3141,9 +3141,11 @@ int ttys_shift_ids(struct lxc_conf *c) int lxc_setup(const char *name, struct lxc_conf *lxc_conf, const char *lxcpath, struct cgroup_process_info *cgroup_info) { - if (setup_utsname(lxc_conf->utsname)) { - ERROR("failed to setup the utsname for '%s'", name); - return -1; + if (lxc_conf->inherit_ns_fd[LXC_NS_UTS] == -1) { + if (setup_utsname(lxc_conf->utsname)) { + ERROR("failed to setup the utsname for '%s'", name); + return -1; + } } if (setup_network(&lxc_conf->network)) { diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index a7976eafb..02867fb2e 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -53,6 +53,7 @@ #define OPT_SHARE_NET OPT_USAGE+1 #define OPT_SHARE_IPC OPT_USAGE+2 +#define OPT_SHARE_UTS OPT_USAGE+3 lxc_log_define(lxc_start_ui, lxc_start); @@ -153,6 +154,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) case 'p': args->pidfile = arg; break; case OPT_SHARE_NET: args->share_ns[LXC_NS_NET] = arg; break; case OPT_SHARE_IPC: args->share_ns[LXC_NS_IPC] = arg; break; + case OPT_SHARE_UTS: args->share_ns[LXC_NS_UTS] = arg; break; } return 0; } @@ -167,6 +169,7 @@ static const struct option my_longopts[] = { {"pidfile", required_argument, 0, 'p'}, {"share-net", required_argument, 0, OPT_SHARE_NET}, {"share-ipc", required_argument, 0, OPT_SHARE_IPC}, + {"share-uts", required_argument, 0, OPT_SHARE_UTS}, LXC_COMMON_OPTIONS }; @@ -190,6 +193,7 @@ Options :\n\ -s, --define KEY=VAL Assign VAL to configuration variable KEY\n\ --share-net=NAME Share a network namespace with another container or pid\n\ --share-ipc=NAME Share an IPC namespace with another container or pid\n\ + --share-uts=NAME Share a UTS namespace with another container or pid\n\ ", .options = my_longopts, .parser = my_parser, diff --git a/src/lxc/start.c b/src/lxc/start.c index f591ad7da..5e3ce7f0f 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -721,13 +721,13 @@ int lxc_spawn(struct lxc_handler *handler) int preserve_mask = 0, i; for (i = 0; i < LXC_NS_MAX; i++) - if (handler->conf->inherit_ns_fd[i] > -1) + if (handler->conf->inherit_ns_fd[i] != -1) preserve_mask |= ns_info[i].clone_flag; if (lxc_sync_init(handler)) return -1; - handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWNS; + handler->clone_flags = CLONE_NEWPID|CLONE_NEWNS; if (!lxc_list_empty(&handler->conf->id_map)) { INFO("Cloning a new user namespace"); handler->clone_flags |= CLONE_NEWUSER; @@ -772,6 +772,12 @@ int lxc_spawn(struct lxc_handler *handler) INFO("Inheriting an IPC namespace"); } + if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) { + handler->clone_flags |= CLONE_NEWUTS; + } else { + INFO("Inheriting a UTS namespace"); + } + cgroup_meta = lxc_cgroup_load_meta(); if (!cgroup_meta) {