nspawn.c revision eb91eb187b7491e05fb95215b77cb62061f41d08
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <signal.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/capability.h>
#include <getopt.h>
#include <termios.h>
#include <sys/signalfd.h>
#include <grp.h>
#ifdef HAVE_SELINUX
#endif
#include "sd-daemon.h"
#include "sd-bus.h"
#include "sd-id128.h"
#include "log.h"
#include "util.h"
#include "mkdir.h"
#include "macro.h"
#include "audit.h"
#include "missing.h"
#include "cgroup-util.h"
#include "strv.h"
#include "path-util.h"
#include "loopback-setup.h"
#include "dev-setup.h"
#include "fdset.h"
#include "build.h"
#include "fileio.h"
#include "bus-util.h"
#include "bus-error.h"
#include "ptyfwd.h"
#include "bus-kernel.h"
#include "env-util.h"
#include "def.h"
typedef enum LinkJournal {
} LinkJournal;
static char *arg_directory = NULL;
static sd_id128_t arg_uuid = {};
static char *arg_machine = NULL;
static char *arg_selinux_context = NULL;
static char *arg_selinux_apifs_context = NULL;
static bool arg_private_network = false;
static bool arg_read_only = false;
static bool arg_boot = false;
static uint64_t arg_retain =
(1ULL << CAP_CHOWN) |
(1ULL << CAP_DAC_OVERRIDE) |
(1ULL << CAP_DAC_READ_SEARCH) |
(1ULL << CAP_FOWNER) |
(1ULL << CAP_FSETID) |
(1ULL << CAP_IPC_OWNER) |
(1ULL << CAP_KILL) |
(1ULL << CAP_LEASE) |
(1ULL << CAP_LINUX_IMMUTABLE) |
(1ULL << CAP_NET_BIND_SERVICE) |
(1ULL << CAP_NET_BROADCAST) |
(1ULL << CAP_NET_RAW) |
(1ULL << CAP_SETGID) |
(1ULL << CAP_SETFCAP) |
(1ULL << CAP_SETPCAP) |
(1ULL << CAP_SETUID) |
(1ULL << CAP_SYS_ADMIN) |
(1ULL << CAP_SYS_CHROOT) |
(1ULL << CAP_SYS_NICE) |
(1ULL << CAP_SYS_PTRACE) |
(1ULL << CAP_SYS_TTY_CONFIG) |
(1ULL << CAP_SYS_RESOURCE) |
(1ULL << CAP_SYS_BOOT) |
(1ULL << CAP_AUDIT_WRITE) |
(1ULL << CAP_AUDIT_CONTROL) |
(1ULL << CAP_MKNOD);
static char **arg_bind_ro = NULL;
static char **arg_setenv = NULL;
static bool arg_quiet = false;
static bool arg_share_system = false;
static bool arg_register = true;
static int help(void) {
printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
"Spawn a minimal namespace container for debugging, testing and building.\n\n"
" -h --help Show this help\n"
" --version Print version string\n"
" -D --directory=NAME Root directory for the container\n"
" -b --boot Boot up full system (i.e. invoke init)\n"
" -u --user=USER Run the command under specified user or uid\n"
" --uuid=UUID Set a specific machine UUID for the container\n"
" -M --machine=NAME Set the machine name for the container\n"
" -S --slice=SLICE Place the container in the specified slice\n"
" -Z --selinux-context=SECLABEL\n"
" Set the SELinux security context to be used by\n"
" processes in the container\n"
" -L --selinux-apifs-context=SECLABEL\n"
" Set the SELinux security context to be used by\n"
" --private-network Disable network in container\n"
" --share-system Share system namespaces with host\n"
" --read-only Mount the root directory read-only\n"
" --capability=CAP In addition to the default, retain specified\n"
" capability\n"
" --drop-capability=CAP Drop the specified capability from the default set\n"
" --link-journal=MODE Link up guest journal, one of no, auto, guest, host\n"
" -j Equivalent to --link-journal=host\n"
" --bind=PATH[:PATH] Bind mount a file or directory from the host into\n"
" the container\n"
" --bind-ro=PATH[:PATH] Similar, but creates a read-only bind mount\n"
" --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
" --register=BOOLEAN Register container as machine\n"
" -q --quiet Do not show status information\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c, r;
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
case 'D':
if (!arg_directory) {
log_error("Invalid root directory: %m");
return -ENOMEM;
}
break;
case 'u':
if (!arg_user)
return log_oom();
break;
case ARG_PRIVATE_NETWORK:
arg_private_network = true;
break;
case 'b':
arg_boot = true;
break;
case ARG_UUID:
if (r < 0) {
return r;
}
break;
case 'S':
if (!arg_slice)
return log_oom();
break;
case 'M':
arg_machine = NULL;
} else {
if (!hostname_is_valid(optarg)) {
return -EINVAL;
}
if (!arg_machine)
return log_oom();
break;
}
case 'Z':
break;
case 'L':
break;
case ARG_READ_ONLY:
arg_read_only = true;
break;
case ARG_CAPABILITY:
case ARG_DROP_CAPABILITY: {
char *t;
if (!t)
return log_oom();
if (cap_from_name(t, &cap) < 0) {
log_error("Failed to parse capability %s.", t);
free(t);
return -EINVAL;
}
free(t);
if (c == ARG_CAPABILITY)
else
}
break;
}
case 'j':
break;
case ARG_LINK_JOURNAL:
else {
return -EINVAL;
}
break;
case ARG_BIND:
case ARG_BIND_RO: {
char *e;
char ***x;
if (e) {
b = strdup(e + 1);
} else {
}
if (!a || !b)
return log_oom();
if (!path_is_absolute(a) || !path_is_absolute(b)) {
return -EINVAL;
}
r = strv_extend(x, a);
if (r < 0)
return log_oom();
r = strv_extend(x, b);
if (r < 0)
return log_oom();
break;
}
case ARG_SETENV: {
char **n;
if (!env_assignment_is_valid(optarg)) {
return -EINVAL;
}
if (!n)
return log_oom();
arg_setenv = n;
break;
}
case 'q':
arg_quiet = true;
break;
case ARG_SHARE_SYSTEM:
arg_share_system = true;
break;
case ARG_REGISTER:
r = parse_boolean(optarg);
if (r < 0) {
return r;
}
arg_register = r;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
}
if (arg_share_system)
arg_register = false;
if (arg_boot && arg_share_system) {
log_error("--boot and --share-system may not be combined.");
return -EINVAL;
}
return 1;
}
typedef struct MountPoint {
const char *what;
const char *where;
const char *type;
const char *options;
unsigned long flags;
bool fatal;
} MountPoint;
static const MountPoint mount_table[] = {
{ "devpts", "/dev/pts", "devpts","newinstance,ptmxmode=0666,mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, true },
#ifdef HAVE_SELINUX
{ NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, false }, /* Then, make it r/o */
#endif
};
unsigned k;
int r = 0;
for (k = 0; k < ELEMENTSOF(mount_table); k++) {
#ifdef HAVE_SELINUX
#endif
const char *o;
int t;
if (!where)
return log_oom();
t = path_is_mount_point(where, true);
if (t < 0) {
if (r == 0)
r = t;
continue;
}
/* Skip this entry if it is not a remount. */
if (mount_table[k].what && t > 0)
continue;
#ifdef HAVE_SELINUX
if (arg_selinux_apifs_context &&
if (!options)
return log_oom();
o = options;
} else
#endif
o = mount_table[k].options;
mount_table[k].type,
mount_table[k].flags,
o) < 0 &&
mount_table[k].fatal) {
if (r == 0)
r = -errno;
}
}
return r;
}
char **x, **y;
STRV_FOREACH_PAIR(x, y, l) {
char *where;
int r;
log_error("failed to stat %s: %m", *x);
return -errno;
}
if (r == 0) {
log_error("The file types of %s and %s do not match. Refusing bind mount",
*x, where);
return -EINVAL;
}
if (r < 0) {
return r;
}
} else {
return -errno;
}
/* Create the mount point, but be conservative -- refuse to create block
* and char devices. */
else {
log_error("Refusing to create mountpoint for file: %s", *x);
return -ENOTSUP;
}
return -errno;
}
return -errno;
}
}
return 0;
}
static int setup_timezone(const char *dest) {
char *z, *y;
int r;
/* Fix the timezone, if possible */
r = readlink_malloc("/etc/localtime", &p);
if (r < 0) {
log_warning("/etc/localtime is not a symlink, not updating container timezone.");
return 0;
}
z = path_startswith(p, "../usr/share/zoneinfo/");
if (!z)
z = path_startswith(p, "/usr/share/zoneinfo/");
if (!z) {
log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
return 0;
}
if (!where)
return log_oom();
r = readlink_malloc(where, &q);
if (r >= 0) {
y = path_startswith(q, "../usr/share/zoneinfo/");
if (!y)
y = path_startswith(q, "/usr/share/zoneinfo/");
/* Already pointing to the right place? Then do nothing .. */
if (y && streq(y, z))
return 0;
}
if (!check)
return log_oom();
log_warning("Timezone %s does not exist in container, not updating container timezone.", z);
return 0;
}
if (!what)
return log_oom();
log_error("Failed to correct timezone of container: %m");
return 0;
}
return 0;
}
static int setup_resolv_conf(const char *dest) {
if (arg_private_network)
return 0;
/* Fix resolv.conf, if possible */
if (!where)
return log_oom();
/* We don't really care for the results of this really. If it
* fails, it fails, but meh... */
return 0;
}
static int setup_boot_id(const char *dest) {
char as_uuid[37];
int r;
if (arg_share_system)
return 0;
/* Generate a new randomized boot ID, so that each boot-up of
* the container gets a new one */
return log_oom();
r = sd_id128_randomize(&rnd);
if (r < 0) {
return r;
}
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
if (r < 0) {
return r;
}
log_error("Failed to bind mount boot id: %m");
r = -errno;
log_warning("Failed to make boot id read-only: %m");
return r;
}
static int copy_devnodes(const char *dest) {
static const char devnodes[] =
"null\0"
"zero\0"
"full\0"
"random\0"
"urandom\0"
"tty\0";
const char *d;
int r = 0;
u = umask(0000);
NULSTR_FOREACH(d, devnodes) {
return log_oom();
return -errno;
}
return -EIO;
return -errno;
}
}
return r;
}
static int setup_ptmx(const char *dest) {
_cleanup_free_ char *p = NULL;
if (!p)
return log_oom();
return -errno;
}
return 0;
}
int r;
u = umask(0000);
return -errno;
return -EIO;
}
if (r < 0) {
return r;
}
return log_oom();
* ptys can only exist on pts file systems. To have something
* to bind mount things on we create a device node first, that
* doesn't actually matter here, since we mount it over
* anyway). */
return -errno;
}
return -errno;
}
return 0;
}
int r, fd, k;
union {
} control = {};
.msg_control = &control,
.msg_controllen = sizeof(control),
};
assert(kmsg_socket >= 0);
u = umask(0000);
* that writing blocks when nothing is reading. In order to
* avoid any problems with containers deadlocking due to this
return log_oom();
return -errno;
}
if (r < 0) {
return r;
}
return -errno;
}
if (fd < 0) {
log_error("Failed to open fifo: %m");
return -errno;
}
/* Store away the fd in the socket, so that it stays open as
* long as we run the child */
if (k < 0) {
log_error("Failed to send FIFO fd: %m");
return -errno;
}
return 0;
}
static int setup_hostname(void) {
if (arg_share_system)
return 0;
return -errno;
return 0;
}
static int setup_journal(const char *directory) {
char *id;
int r;
if (!p)
return log_oom();
r = read_one_line_file(p, &b);
return 0;
else if (r < 0) {
return r;
}
return 0;
/* Verify validity */
if (r < 0) {
return r;
}
r = sd_id128_get_machine(&this_id);
if (r < 0) {
return r;
}
"Host and machine ids are equal (%s): refusing to link journals", id);
if (arg_link_journal == LINK_AUTO)
return 0;
return
-EEXIST;
}
if (arg_link_journal == LINK_NO)
return 0;
free(p);
if (!p || !q)
return log_oom();
if (path_is_mount_point(p, false) > 0) {
if (arg_link_journal != LINK_AUTO) {
log_error("%s: already a mount point, refusing to use for journal", p);
return -EEXIST;
}
return 0;
}
if (path_is_mount_point(q, false) > 0) {
if (arg_link_journal != LINK_AUTO) {
log_error("%s: already a mount point, refusing to use for journal", q);
return -EEXIST;
}
return 0;
}
r = readlink_and_make_absolute(p, &d);
if (r >= 0) {
if ((arg_link_journal == LINK_GUEST ||
arg_link_journal == LINK_AUTO) &&
path_equal(d, q)) {
r = mkdir_p(q, 0755);
if (r < 0)
log_warning("failed to create directory %s: %m", q);
return 0;
}
if (unlink(p) < 0) {
log_error("Failed to remove symlink %s: %m", p);
return -errno;
}
} else if (r == -EINVAL) {
if (arg_link_journal == LINK_GUEST &&
rmdir(p) < 0) {
log_error("%s already exists and is neither a symlink nor a directory", p);
return r;
} else {
log_error("Failed to remove %s: %m", p);
return -errno;
}
}
} else if (r != -ENOENT) {
log_error("readlink(%s) failed: %m", p);
return r;
}
if (arg_link_journal == LINK_GUEST) {
if (symlink(q, p) < 0) {
log_error("Failed to symlink %s to %s: %m", q, p);
return -errno;
}
r = mkdir_p(q, 0755);
if (r < 0)
log_warning("failed to create directory %s: %m", q);
return 0;
}
if (arg_link_journal == LINK_HOST) {
r = mkdir_p(p, 0755);
if (r < 0) {
log_error("Failed to create %s: %m", p);
return r;
}
return 0;
if (dir_is_empty(q) == 0) {
log_error("%s not empty.", q);
return -ENOTEMPTY;
}
r = mkdir_p(q, 0755);
if (r < 0) {
log_error("Failed to create %s: %m", q);
return r;
}
log_error("Failed to bind mount journal from host into guest: %m");
return -errno;
}
return 0;
}
const char *p;
if (!path)
return 0;
if (mkdir(p, 0755) < 0) {
log_error("Failed to create kdbus path: %m");
return -errno;
}
log_error("Failed to mount kdbus domain path: %m");
return -errno;
}
return 0;
}
static int drop_capabilities(void) {
return capability_bounding_set_drop(~arg_retain, false);
}
int r;
if (!arg_register)
return 0;
r = sd_bus_default_system(&bus);
if (r < 0) {
return r;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"CreateMachine",
&error,
NULL,
"sayssusa(sv)",
"nspawn",
"container",
if (r < 0) {
return r;
}
return 0;
}
const char *path;
int r;
if (!arg_register)
return 0;
r = sd_bus_default_system(&bus);
if (r < 0) {
return r;
}
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"GetMachineByPID",
&error,
&reply,
"u",
if (r < 0) {
/* Note that the machine might already have been
* cleaned up automatically, hence don't consider it a
* failure if we cannot get the machine object. */
return 0;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
path,
"org.freedesktop.machine1.Machine",
"Terminate",
&error,
NULL,
NULL);
if (r < 0) {
return 0;
}
return 0;
}
static bool audit_enabled(void) {
int fd;
if (fd >= 0) {
return true;
}
return false;
}
int r = EXIT_FAILURE, k;
int n_fd_passed;
log_open();
if (k < 0)
goto finish;
else if (k == 0) {
r = EXIT_SUCCESS;
goto finish;
}
if (arg_directory) {
char *p;
arg_directory = p;
} else
if (!arg_directory) {
log_error("Failed to determine path, please use -D.");
goto finish;
}
if (!arg_machine) {
if (!arg_machine) {
log_oom();
goto finish;
}
hostname_cleanup(arg_machine, false);
if (isempty(arg_machine)) {
log_error("Failed to determine machine name automatically, please use -M.");
goto finish;
}
}
if (geteuid() != 0) {
log_error("Need to be root.");
goto finish;
}
if (sd_booted() <= 0) {
log_error("Not running on a systemd system.");
goto finish;
}
if (arg_boot && audit_enabled()) {
log_warning("The kernel auditing subsystem is known to be incompatible with containers.\n"
"Please make sure to turn off auditing with 'audit=0' on the kernel command\n"
"line before using systemd-nspawn. Sleeping for 5s...\n");
sleep(5);
}
log_error("Spawning container on root directory not supported.");
goto finish;
}
log_error("Directory %s doesn't look like an OS root directory (/etc/os-release is missing). Refusing.", arg_directory);
goto finish;
}
log_close();
n_fd_passed = sd_listen_fds(false);
if (n_fd_passed > 0) {
k = fdset_new_listen_fds(&fds, false);
if (k < 0) {
goto finish;
}
}
log_open();
if (master < 0) {
log_error("Failed to acquire pseudo tty: %m");
goto finish;
}
if (!console) {
log_error("Failed to determine tty name: %m");
goto finish;
}
if (!arg_quiet)
log_info("Spawning container %s on %s. Press ^] three times within 1s to abort execution.", arg_machine, arg_directory);
log_error("Failed to unlock tty: %m");
goto finish;
}
if (arg_share_system) {
if (!kdbus_domain) {
log_oom();
goto finish;
}
} else {
const char *ns;
if (r < 0)
else
}
}
log_error("Failed to create kmsg socket pair: %m");
goto finish;
}
sd_notify(0, "READY=1");
for (;;) {
if (sync_fd < 0) {
log_error("Failed to create event fd: %m");
goto finish;
}
if (pid < 0) {
log_error("clone() failed, do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in): %m");
else
log_error("clone() failed: %m");
goto finish;
}
if (pid == 0) {
/* child */
unsigned n_env = 2;
const char *envp[] = {
"PATH=" DEFAULT_PATH_SPLIT_USR,
"container=systemd-nspawn", /* LXC sets container=lxc, so follow the scheme here */
NULL, /* TERM */
NULL, /* HOME */
NULL, /* USER */
NULL, /* LOGNAME */
NULL, /* container_uuid */
NULL, /* LISTEN_FDS */
NULL, /* LISTEN_PID */
};
char **env_use;
eventfd_t x;
n_env ++;
master = -1;
kmsg_socket_pair[0] = -1;
if (k != STDIN_FILENO) {
if (k >= 0) {
k = -EINVAL;
}
goto child_fail;
}
log_error("Failed to duplicate console: %m");
goto child_fail;
}
if (setsid() < 0) {
log_error("setsid() failed: %m");
goto child_fail;
}
log_error("PR_SET_PDEATHSIG failed: %m");
goto child_fail;
}
/* Mark everything as slave, so that we still
* receive mounts from the real root, but don't
* propagate mounts to the real root. */
log_error("MS_SLAVE|MS_REC failed: %m");
goto child_fail;
}
/* Turn directory into bind mount */
log_error("Failed to make bind mount.");
goto child_fail;
}
if (arg_read_only)
log_error("Failed to make read-only.");
goto child_fail;
}
if (mount_all(arg_directory) < 0)
goto child_fail;
if (copy_devnodes(arg_directory) < 0)
goto child_fail;
if (setup_ptmx(arg_directory) < 0)
goto child_fail;
goto child_fail;
goto child_fail;
if (setup_boot_id(arg_directory) < 0)
goto child_fail;
if (setup_timezone(arg_directory) < 0)
goto child_fail;
if (setup_resolv_conf(arg_directory) < 0)
goto child_fail;
if (setup_journal(arg_directory) < 0)
goto child_fail;
goto child_fail;
goto child_fail;
goto child_fail;
if (chdir(arg_directory) < 0) {
goto child_fail;
}
log_error("mount(MS_MOVE) failed: %m");
goto child_fail;
}
if (chroot(".") < 0) {
log_error("chroot() failed: %m");
goto child_fail;
}
if (chdir("/") < 0) {
log_error("chdir() failed: %m");
goto child_fail;
}
umask(0022);
if (arg_private_network)
if (drop_capabilities() < 0) {
log_error("drop_capabilities() failed: %m");
goto child_fail;
}
if (arg_user) {
/* Note that this resolves user names
* inside the container, and hence
* accesses the NSS modules from the
* container and not the host. This is
* a bit weird... */
log_error("get_user_creds() failed: %m");
goto child_fail;
}
log_error("mkdir_parents_label() failed: %m");
goto child_fail;
}
log_error("mkdir_safe_label() failed: %m");
goto child_fail;
}
log_error("initgroups() failed: %m");
goto child_fail;
}
log_error("setregid() failed: %m");
goto child_fail;
}
log_error("setreuid() failed: %m");
goto child_fail;
}
} else {
/* Reset everything fully to 0, just in case */
log_error("setgroups() failed: %m");
goto child_fail;
}
if (setresgid(0, 0, 0) < 0) {
log_error("setregid() failed: %m");
goto child_fail;
}
if (setresuid(0, 0, 0) < 0) {
log_error("setreuid() failed: %m");
goto child_fail;
}
}
log_oom();
goto child_fail;
}
if (asprintf((char**)(envp + n_env++), "container_uuid=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(arg_uuid)) < 0) {
log_oom();
goto child_fail;
}
}
if (fdset_size(fds) > 0) {
k = fdset_cloexec(fds, false);
if (k < 0) {
log_error("Failed to unset O_CLOEXEC for file descriptors.");
goto child_fail;
}
log_oom();
goto child_fail;
}
}
eventfd_read(sync_fd, &x);
sync_fd = -1;
if (!strv_isempty(arg_setenv)) {
char **n;
if (!n) {
log_oom();
goto child_fail;
}
env_use = n;
} else
#ifdef HAVE_SELINUX
if (arg_selinux_context)
if (setexeccon(arg_selinux_context) < 0)
#endif
if (arg_boot) {
char **a;
size_t l;
/* Automatically search for the init system */
a = newa(char*, l + 1);
else {
}
log_error("execv() failed: %m");
}
r = register_machine(pid);
if (r < 0)
goto finish;
sync_fd = -1;
if (k < 0) {
r = EXIT_FAILURE;
break;
}
if (!arg_quiet)
/* Kill if it is not dead yet anyway */
/* Redundant, but better safe than sorry */
pid = 0;
if (k < 0) {
r = EXIT_FAILURE;
break;
}
break;
}
if (!arg_quiet)
break;
if (!arg_quiet)
r = 0;
break;
if (!arg_quiet)
continue;
log_error("Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
r = EXIT_FAILURE;
break;
} else {
r = EXIT_FAILURE;
break;
}
}
if (pid > 0)
return r;
}