nspawn.c revision 080e78329a742dd95bdee321fee81c305e073491
/*-*- 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 <getopt.h>
#include <termios.h>
#include <sys/signalfd.h>
#include <grp.h>
#include <sys/personality.h>
#ifdef HAVE_SELINUX
#endif
#ifdef HAVE_SECCOMP
#include <seccomp.h>
#endif
#ifdef HAVE_BLKID
#endif
#include "sd-daemon.h"
#include "sd-bus.h"
#include "sd-id128.h"
#include "sd-rtnl.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"
#include "rtnl-util.h"
#include "udev-util.h"
#include "blkid-util.h"
#include "gpt.h"
#include "siphash24.h"
#include "copy.h"
#include "base-filesystem.h"
#include "barrier.h"
#include "event-util.h"
#include "capability.h"
#include "cap-list.h"
#include "btrfs-util.h"
#include "machine-image.h"
#ifdef HAVE_SECCOMP
#include "seccomp-util.h"
#endif
typedef enum ContainerStatus {
typedef enum LinkJournal {
} LinkJournal;
typedef enum Volatile {
} Volatile;
static char *arg_directory = NULL;
static char *arg_template = NULL;
static sd_id128_t arg_uuid = {};
static char *arg_machine = NULL;
static const char *arg_selinux_context = NULL;
static const char *arg_selinux_apifs_context = NULL;
static bool arg_private_network = false;
static bool arg_read_only = false;
static bool arg_boot = false;
static bool arg_ephemeral = false;
static bool arg_link_journal_try = 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 bool arg_keep_unit = false;
static char **arg_network_interfaces = NULL;
static char **arg_network_macvlan = NULL;
static bool arg_network_veth = false;
static const char *arg_network_bridge = NULL;
static unsigned long arg_personality = 0xffffffffLU;
static void 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"
" -q --quiet Do not show status information\n"
" -D --directory=PATH Root directory for the container\n"
" --template=PATH Initialize root directory from template directory,\n"
" if missing\n"
" -x --ephemeral Run container with snapshot of root directory, and\n"
" remove it after exit\n"
" -i --image=PATH File system device or disk image 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"
" -M --machine=NAME Set the machine name for the container\n"
" --uuid=UUID Set a specific machine UUID for the container\n"
" -S --slice=SLICE Place the container in the specified slice\n"
" --private-network Disable network in container\n"
" --network-interface=INTERFACE\n"
" Assign an existing network interface to the\n"
" container\n"
" --network-macvlan=INTERFACE\n"
" Create a macvlan network interface based on an\n"
" existing network interface to the container\n"
" --network-veth Add a virtual ethernet connection between host\n"
" and container\n"
" --network-bridge=INTERFACE\n"
" Add a virtual ethernet connection between host\n"
" and container and add it to an existing bridge on\n"
" the host\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"
" --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"
" try-guest, try-host\n"
" -j Equivalent to --link-journal=try-guest\n"
" --read-only Mount the root directory read-only\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"
" --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
" --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
" --share-system Share system namespaces with host\n"
" --register=BOOLEAN Register container as machine\n"
" --keep-unit Do not register a scope for the machine, reuse\n"
" the service unit nspawn is running in\n"
" --volatile[=MODE] Run the system in volatile mode\n",
}
static int set_sanitized_path(char **b, const char *path) {
char *p;
assert(b);
p = canonicalize_file_name(path);
if (!p) {
return -errno;
p = path_make_absolute_cwd(path);
if (!p)
return -ENOMEM;
}
free(*b);
*b = path_kill_slashes(p);
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c, r;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case 'D':
if (r < 0)
return log_error_errno(r, "Invalid root directory: %m");
break;
case ARG_TEMPLATE:
if (r < 0)
return log_error_errno(r, "Invalid template directory: %m");
break;
case 'i':
if (r < 0)
return log_error_errno(r, "Invalid image path: %m");
break;
case 'x':
arg_ephemeral = true;
break;
case 'u':
if (!arg_user)
return log_oom();
break;
case ARG_NETWORK_BRIDGE:
/* fall through */
case ARG_NETWORK_VETH:
arg_network_veth = true;
arg_private_network = true;
break;
case ARG_NETWORK_INTERFACE:
return log_oom();
arg_private_network = true;
break;
case ARG_NETWORK_MACVLAN:
return log_oom();
/* fall through */
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':
break;
case 'M':
arg_machine = NULL;
} else {
if (!machine_name_is_valid(optarg)) {
return -EINVAL;
}
if (r < 0)
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: {
_cleanup_free_ char *t;
if (!t)
return log_oom();
if (streq(t, "all")) {
if (c == ARG_CAPABILITY)
else
} else {
int cap;
cap = capability_from_name(t);
if (cap < 0) {
log_error("Failed to parse capability %s.", t);
return -EINVAL;
}
if (c == ARG_CAPABILITY)
else
}
}
break;
}
case 'j':
arg_link_journal_try = true;
break;
case ARG_LINK_JOURNAL:
arg_link_journal_try = false;
arg_link_journal_try = false;
arg_link_journal_try = false;
arg_link_journal_try = false;
arg_link_journal_try = true;
arg_link_journal_try = true;
} 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_TMPFS: {
char *e;
if (e) {
b = strdup(e + 1);
} else {
b = strdup("mode=0755");
}
if (!a || !b)
return log_oom();
if (!path_is_absolute(a)) {
return -EINVAL;
}
if (r < 0)
return log_oom();
a = NULL;
if (r < 0)
return log_oom();
b = NULL;
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 ARG_KEEP_UNIT:
arg_keep_unit = true;
break;
case ARG_PERSONALITY:
if (arg_personality == 0xffffffffLU) {
return -EINVAL;
}
break;
case ARG_VOLATILE:
if (!optarg)
else {
r = parse_boolean(optarg);
if (r < 0) {
else {
return r;
}
} else
}
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;
}
log_error("--keep-unit may not be used when invoked from a user session.");
return -EINVAL;
}
if (arg_directory && arg_image) {
log_error("--directory= and --image= may not be combined.");
return -EINVAL;
}
if (arg_template && arg_image) {
log_error("--template= and --image= may not be combined.");
return -EINVAL;
}
log_error("--template= needs --directory= or --machine=.");
return -EINVAL;
}
if (arg_ephemeral && arg_template) {
log_error("--ephemeral and --template= may not be combined.");
return -EINVAL;
}
if (arg_ephemeral && arg_image) {
log_error("--ephemeral and --image= may not be combined.");
return -EINVAL;
}
log_error("--ephemeral and --link-journal= may not be combined.");
return -EINVAL;
}
log_error("Cannot combine --read-only with --volatile. Note that --volatile already implies a read-only base hierarchy.");
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;
if (t < 0) {
if (mount_table[k].fatal) {
if (r == 0)
r = t;
} else
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) {
if (mount_table[k].fatal) {
if (r == 0)
r = -errno;
} else
}
}
return r;
}
char **x, **y;
STRV_FOREACH_PAIR(x, y, l) {
int r;
if (!where)
return log_oom();
if (r == 0) {
return -EINVAL;
}
if (r < 0)
return log_error_errno(r, "Failed to bind mount %s: %m", *x);
} else {
return -errno;
}
/* Create the mount point, but be conservative -- refuse to create block
* and char devices. */
if (r < 0)
} else {
log_error("Refusing to create mountpoint for file: %s", *x);
return -ENOTSUP;
}
if (ro) {
r = bind_remount_recursive(where, true);
if (r < 0)
return log_error_errno(r, "Read-Only bind mount failed: %m");
}
}
return 0;
}
static int mount_cgroup_hierarchy(const char *dest, const char *controller, const char *hierarchy, bool read_only) {
char *to;
int r;
r = path_is_mount_point(to, false);
if (r < 0)
if (r > 0)
return 0;
if (mount("cgroup", to, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV|(read_only ? MS_RDONLY : 0), controller) < 0)
return 1;
}
static int mount_cgroup(const char *dest) {
int r;
if (!controllers)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to determine cgroup controllers: %m");
if (r < 0)
return log_error_errno(r, "Failed to determine our own cgroup path: %m");
if (mount("tmpfs", cgroup_root, "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, "mode=755") < 0)
for (;;) {
if (!controller)
break;
if (!origin)
return log_oom();
if (r == -EINVAL) {
/* Not a symbolic link, but directly a single cgroup hierarchy */
if (r < 0)
return r;
} else if (r < 0)
else {
if (!target)
return log_oom();
/* A symbolic link, a combination of controllers in one hierarchy */
if (!filename_is_valid(combined)) {
continue;
}
if (r < 0)
return r;
}
}
if (r < 0)
return r;
/* Make our own cgroup a (writable) bind mount */
/* And then remount the systemd cgroup root read-only */
if (mount(NULL, systemd_root, NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL) < 0)
if (mount(NULL, cgroup_root, NULL, MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755") < 0)
return 0;
}
static int mount_tmpfs(const char *dest) {
char **i, **o;
STRV_FOREACH_PAIR(i, o, arg_tmpfs) {
int r;
if (!where)
return log_oom();
if (r < 0 && r != -EEXIST)
}
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();
if (r < 0) {
return 0;
}
return 0;
}
return 0;
}
return 0;
}
static int setup_resolv_conf(const char *dest) {
int r;
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... */
if (r < 0) {
return 0;
}
if (r < 0) {
return 0;
}
return 0;
}
static int setup_volatile_state(const char *directory) {
const char *p;
int r;
if (arg_volatile != VOLATILE_STATE)
return 0;
/* --volatile=state means we simply overmount /var
with a tmpfs, and the rest read-only. */
r = bind_remount_recursive(directory, true);
if (r < 0)
r = mkdir(p, 0755);
return 0;
}
static int setup_volatile(const char *directory) {
bool tmpfs_mounted = false, bind_mounted = false;
char template[] = "/tmp/nspawn-volatile-XXXXXX";
const char *f, *t;
int r;
if (arg_volatile != VOLATILE_YES)
return 0;
/* --volatile=yes means we mount a tmpfs to the root dir, and
the original /usr to use inside it, and that read-only. */
r = -errno;
goto fail;
}
tmpfs_mounted = true;
r = mkdir(t, 0755);
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
bind_mounted = true;
r = bind_remount_recursive(t, true);
if (r < 0) {
log_error_errno(r, "Failed to remount %s read-only: %m", t);
goto fail;
}
r = -errno;
goto fail;
}
return 0;
fail:
if (bind_mounted)
umount(t);
if (tmpfs_mounted)
return r;
}
snprintf(s, 37,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
return s;
}
static int setup_boot_id(const char *dest) {
sd_id128_t rnd = {};
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 log_error_errno(r, "Failed to generate random boot id: %m");
if (r < 0)
return log_error_errno(r, "Failed to write boot id: %m");
r = -errno;
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 -EIO;
} else {
if (r < 0) {
return -r;
}
}
}
return r;
}
static int setup_ptmx(const char *dest) {
_cleanup_free_ char *p = NULL;
if (!p)
return log_oom();
return 0;
}
const char *to;
int r;
u = umask(0000);
if (r < 0)
return log_error_errno(r, "Failed to correct access mode for TTY: %m");
* ptys can only exist on pts file systems. To have something
* to bind mount things on we create a device node first, and
* allows us to create that freely, while we cannot create
* matter here, since we mount it over anyway). */
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();
if (r < 0)
return log_error_errno(r, "Failed to correct access mode for /dev/kmsg: %m");
if (fd < 0)
/* Store away the fd in the socket, so that it stays open as
* long as we run the child */
safe_close(fd);
if (k < 0)
return 0;
}
static int setup_hostname(void) {
if (arg_share_system)
return 0;
if (sethostname_idempotent(arg_machine) < 0)
return -errno;
return 0;
}
static int setup_journal(const char *directory) {
char *id;
int r;
/* Don't link journals in ephemeral mode */
if (arg_ephemeral)
return 0;
if (!p)
return log_oom();
r = read_one_line_file(p, &b);
return 0;
else if (r < 0)
return log_error_errno(r, "Failed to read machine ID from %s: %m", p);
return 0;
/* Verify validity */
if (r < 0)
return log_error_errno(r, "Failed to parse machine ID from %s: %m", p);
r = sd_id128_get_machine(&this_id);
if (r < 0)
return log_error_errno(r, "Failed to retrieve machine ID: %m");
"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)
return 0;
}
if (unlink(p) < 0)
} 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 {
return -errno;
}
}
} else if (r != -ENOENT) {
return r;
}
if (arg_link_journal == LINK_GUEST) {
if (symlink(q, p) < 0) {
if (arg_link_journal_try) {
return 0;
} else {
return -errno;
}
}
r = mkdir_p(q, 0755);
if (r < 0)
return 0;
}
if (arg_link_journal == LINK_HOST) {
/* don't create parents here -- if the host doesn't have
* permanent journal set up, don't force it here */
r = mkdir(p, 0755);
if (r < 0) {
if (arg_link_journal_try) {
return 0;
} else {
return r;
}
}
return 0;
if (dir_is_empty(q) == 0)
log_warning("%s is not empty, proceeding anyway.", q);
r = mkdir_p(q, 0755);
if (r < 0) {
return r;
}
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 log_error_errno(r, "Failed to open system bus: %m");
if (arg_keep_unit) {
r = sd_bus_call_method(
bus,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"RegisterMachineWithNetwork",
&error,
NULL,
"sayssusai",
"nspawn",
"container",
} else {
bus,
&m,
"org.freedesktop.machine1",
"/org/freedesktop/machine1",
"org.freedesktop.machine1.Manager",
"CreateMachineWithNetwork");
if (r < 0)
return log_error_errno(r, "Failed to create message: %m");
m,
"sayssusai",
"nspawn",
"container",
if (r < 0)
return log_error_errno(r, "Failed to append message arguments: %m");
if (r < 0)
return log_error_errno(r, "Failed to open container: %m");
if (r < 0)
return log_error_errno(r, "Failed to append slice: %m");
}
if (r < 0)
return log_error_errno(r, "Failed to add device policy: %m");
/* Allow the container to
* access and create the API
* device nodes, so that
* PrivateDevices= in the
* container can work
* fine */
/* Allow the container
* access to ptys. However,
* do not permit the
* container to ever create
* these device nodes. */
"char-pts", "rw");
if (r < 0)
return log_error_errno(r, "Failed to add device whitelist: %m");
r = sd_bus_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close container: %m");
}
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 log_error_errno(r, "Failed to open system bus: %m");
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 int reset_audit_loginuid(void) {
_cleanup_free_ char *p = NULL;
int r;
if (arg_share_system)
return 0;
r = read_one_line_file("/proc/self/loginuid", &p);
if (r == -ENOENT)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to read /proc/self/loginuid: %m");
/* Already reset? */
if (streq(p, "4294967295"))
return 0;
if (r < 0) {
log_error("Failed to reset audit login UID. This probably means that your kernel is too\n"
"old and you have audit enabled. Note that the auditing subsystem is known to\n"
"be incompatible with containers on old kernels. Please make sure to upgrade\n"
"your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
"using systemd-nspawn. Sleeping for 5s... (%s)\n", strerror(-r));
sleep(5);
}
return 0;
}
uint8_t *v, *i;
int r;
l = strlen(arg_machine);
sz = sizeof(sd_id128_t) + l;
if (idx > 0)
/* fetch some persistent data unique to the host */
r = sd_id128_get_machine((sd_id128_t*) v);
if (r < 0)
return r;
/* combine with some data unique (on this host) to this
* container instance */
if (idx > 0) {
}
/* Let's hash the host machine ID plus the container name. We
* use a fixed, but originally randomly created hash key here. */
/* see eth_random_addr in the kernel */
return 0;
}
int r, i;
if (!arg_private_network)
return 0;
if (!arg_network_veth)
return 0;
/* Use two different interface name prefixes depending whether
* we are in bridge mode or not. */
if (r < 0)
return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m");
if (r < 0)
return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m");
r = sd_rtnl_open(&rtnl, 0);
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate netlink message: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink interface name: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink MAC address: %m");
if (r < 0)
return log_error_errno(r, "Failed to open netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to open netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to open netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink interface name: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink MAC address: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink namespace field: %m");
r = sd_rtnl_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close netlink container: %m");
r = sd_rtnl_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close netlink container: %m");
r = sd_rtnl_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to add new veth interfaces: %m");
i = (int) if_nametoindex(iface_name);
if (i <= 0)
*ifi = i;
return 0;
}
int r, bridge;
if (!arg_private_network)
return 0;
if (!arg_network_veth)
return 0;
if (!arg_network_bridge)
return 0;
if (bridge <= 0)
r = sd_rtnl_open(&rtnl, 0);
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate netlink message: %m");
if (r < 0)
return log_error_errno(r, "Failed to set IFF_UP flag: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink interface name field: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink master field: %m");
if (r < 0)
return log_error_errno(r, "Failed to add veth interface to bridge: %m");
return 0;
}
int ifi;
if (ifi <= 0)
if (!d)
if (udev_device_get_is_initialized(d) <= 0) {
return -EBUSY;
}
return ifi;
}
char **i;
int r;
if (!arg_private_network)
return 0;
return 0;
r = sd_rtnl_open(&rtnl, 0);
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
if (!udev) {
log_error("Failed to connect to udev.");
return -ENOMEM;
}
int ifi;
if (ifi < 0)
return ifi;
if (r < 0)
return log_error_errno(r, "Failed to allocate netlink message: %m");
if (r < 0)
return log_error_errno(r, "Failed to append namespace PID to netlink message: %m");
if (r < 0)
return log_error_errno(r, "Failed to move interface %s to namespace: %m", *i);
}
return 0;
}
unsigned idx = 0;
char **i;
int r;
if (!arg_private_network)
return 0;
return 0;
r = sd_rtnl_open(&rtnl, 0);
if (r < 0)
return log_error_errno(r, "Failed to connect to netlink: %m");
if (!udev) {
log_error("Failed to connect to udev.");
return -ENOMEM;
}
_cleanup_free_ char *n = NULL;
struct ether_addr mac;
int ifi;
if (ifi < 0)
return ifi;
if (r < 0)
return log_error_errno(r, "Failed to create MACVLAN MAC address: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate netlink message: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink interface index: %m");
n = strappend("mv-", *i);
if (!n)
return log_oom();
r = sd_rtnl_message_append_string(m, IFLA_IFNAME, n);
if (r < 0)
return log_error_errno(r, "Failed to add netlink interface name: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink MAC address: %m");
if (r < 0)
return log_error_errno(r, "Failed to add netlink namespace field: %m");
if (r < 0)
return log_error_errno(r, "Failed to open netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to open netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to append macvlan mode: %m");
r = sd_rtnl_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close netlink container: %m");
r = sd_rtnl_message_close_container(m);
if (r < 0)
return log_error_errno(r, "Failed to close netlink container: %m");
if (r < 0)
return log_error_errno(r, "Failed to add new macvlan interfaces: %m");
}
return 0;
}
static int setup_seccomp(void) {
#ifdef HAVE_SECCOMP
static const int blacklist[] = {
};
unsigned i;
int r;
if (!seccomp)
return log_oom();
if (r < 0) {
log_error_errno(r, "Failed to add secondary archs to seccomp filter: %m");
goto finish;
}
for (i = 0; i < ELEMENTSOF(blacklist); i++) {
if (r == -EFAULT)
continue; /* unknown syscall */
if (r < 0) {
log_error_errno(r, "Failed to block syscall: %m");
goto finish;
}
}
/*
Audit is broken in containers, much of the userspace audit
hookup will fail if running inside a container. We don't
care and just turn off creation of audit sockets.
This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail
with EAFNOSUPPORT which audit userspace uses as indication
that audit is disabled in the kernel.
*/
r = seccomp_rule_add(
2,
if (r < 0) {
log_error_errno(r, "Failed to add audit seccomp rule: %m");
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to unset NO_NEW_PRIVS: %m");
goto finish;
}
r = seccomp_load(seccomp);
if (r < 0)
log_error_errno(r, "Failed to install seccomp audit filter: %m");
return r;
#else
return 0;
#endif
}
static int setup_propagate(const char *root) {
const char *p, *q;
(void) mkdir_p(p, 0600);
mkdir_parents(q, 0755);
mkdir_p(q, 0600);
return 0;
}
struct loop_info64 info = {
};
int r, nr;
if (fd < 0)
char *p;
if (!p)
return log_oom();
*device_path = p;
*loop_nr = -1;
r = fd;
fd = -1;
return r;
}
return -EINVAL;
}
if (control < 0)
if (nr < 0)
return log_oom();
if (loop < 0)
if (arg_read_only)
*device_path = loopdev;
r = loop;
loop = -1;
return r;
}
static int dissect_image(
int fd,
char **root_device, bool *root_device_rw,
char **home_device, bool *home_device_rw,
char **srv_device, bool *srv_device_rw,
bool *secondary) {
#ifdef HAVE_BLKID
#ifdef GPT_ROOT_NATIVE
int root_nr = -1;
#endif
#ifdef GPT_ROOT_SECONDARY
int secondary_root_nr = -1;
#endif
int r;
b = blkid_new_probe();
if (!b)
return log_oom();
errno = 0;
r = blkid_probe_set_device(b, fd, 0, 0);
if (r != 0) {
if (errno == 0)
return log_oom();
return -errno;
}
errno = 0;
r = blkid_do_safeprobe(b);
if (r == -2 || r == 1) {
log_error("Failed to identify any partition table on %s.\n"
"Note that the disk image needs to follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ to be supported by systemd-nspawn.", arg_image);
return -EINVAL;
} else if (r != 0) {
if (errno == 0)
return -errno;
}
log_error("Image %s does not carry a GUID Partition Table.\n"
"Note that the disk image needs to follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ to be supported by systemd-nspawn.", arg_image);
return -EINVAL;
}
errno = 0;
pl = blkid_probe_get_partitions(b);
if (!pl) {
if (errno == 0)
return log_oom();
return -errno;
}
if (!udev)
return log_oom();
if (!d)
return log_oom();
e = udev_enumerate_new(udev);
if (!e)
return log_oom();
r = udev_enumerate_add_match_parent(e, d);
if (r < 0)
return log_oom();
r = udev_enumerate_scan_devices(e);
if (r < 0)
_cleanup_udev_device_unref_ struct udev_device *q;
unsigned long long flags;
int nr;
errno = 0;
if (!q) {
if (!errno)
return -errno;
}
qn = udev_device_get_devnum(q);
continue;
continue;
node = udev_device_get_devnode(q);
if (!node)
continue;
if (!pp)
continue;
if (flags & GPT_FLAG_NO_AUTO)
continue;
if (nr < 0)
continue;
if (!stype)
continue;
continue;
continue;
if (!home)
return log_oom();
continue;
if (!srv)
return log_oom();
}
#ifdef GPT_ROOT_NATIVE
continue;
if (!root)
return log_oom();
}
#endif
#ifdef GPT_ROOT_SECONDARY
continue;
if (!secondary_root)
return log_oom();
}
#endif
}
if (!root && !secondary_root) {
log_error("Failed to identify root partition in disk image %s.\n"
"Note that the disk image needs to follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ to be supported by systemd-nspawn.", arg_image);
return -EINVAL;
}
if (root) {
*root_device = root;
*secondary = false;
} else if (secondary_root) {
*secondary = true;
}
if (home) {
*home_device = home;
}
if (srv) {
*srv_device = srv;
*srv_device_rw = srv_rw;
}
return 0;
#else
log_error("--image= is not supported, compiled without blkid support.");
return -ENOTSUP;
#endif
}
#ifdef HAVE_BLKID
const char *fstype, *p;
int r;
if (arg_read_only)
rw = false;
if (directory)
else
p = where;
errno = 0;
if (!b) {
if (errno == 0)
return log_oom();
return -errno;
}
errno = 0;
r = blkid_do_safeprobe(b);
if (r == -1 || r == 1) {
return -EINVAL;
} else if (r != 0) {
if (errno == 0)
return -errno;
}
errno = 0;
if (errno == 0)
return -errno;
}
log_error("nspawn currently does not support LUKS disk images.");
return -ENOTSUP;
}
return 0;
#else
log_error("--image= is not supported, compiled without blkid support.");
return -ENOTSUP;
#endif
}
static int mount_devices(
const char *where,
const char *root_device, bool root_device_rw,
const char *home_device, bool home_device_rw,
const char *srv_device, bool srv_device_rw) {
int r;
if (root_device) {
if (r < 0)
return log_error_errno(r, "Failed to mount root directory: %m");
}
if (home_device) {
if (r < 0)
return log_error_errno(r, "Failed to mount home directory: %m");
}
if (srv_device) {
if (r < 0)
return log_error_errno(r, "Failed to mount server data directory: %m");
}
return 0;
}
int r;
if (nr < 0)
return;
if (r < 0)
}
if (control < 0) {
return;
}
if (r < 0)
}
int pipe_fds[2];
if (pid < 0)
else if (pid == 0) {
int nullfd;
if (pipe_fds[0] > 2)
safe_close(pipe_fds[0]);
if (nullfd < 0)
if (nullfd > 2)
close_all_fds(NULL, 0);
}
return pipe_fds[0];
}
static int change_uid_gid(char **_home) {
unsigned n_uids = 0;
int r;
/* Reset everything fully to 0, just in case */
if (setresgid(0, 0, 0) < 0)
if (setresuid(0, 0, 0) < 0)
return 0;
}
/* First, get user credentials */
if (fd < 0)
return fd;
if (!f)
return log_oom();
fd = -1;
if (!ferror(f)) {
return -ESRCH;
}
return -errno;
}
if (!x) {
return -EIO;
}
if (!u) {
return -EIO;
}
u++;
g = strchr(u, ':');
if (!g) {
return -EIO;
}
*g = 0;
g++;
x = strchr(g, ':');
if (!x) {
return -EIO;
}
*x = 0;
if (!h) {
return -EIO;
}
h++;
x = strchr(h, ':');
if (!x) {
return -EIO;
}
*x = 0;
if (r < 0) {
log_error("Failed to parse UID of user.");
return -EIO;
}
if (r < 0) {
log_error("Failed to parse GID of user.");
return -EIO;
}
if (!home)
return log_oom();
/* Second, get group memberships */
if (fd < 0)
return fd;
fclose(f);
if (!f)
return log_oom();
fd = -1;
if (!ferror(f)) {
return -ESRCH;
}
return -errno;
}
/* Skip over the username and subsequent separator whitespace */
x = line;
x += strcspn(x, WHITESPACE);
x += strspn(x, WHITESPACE);
char c[l+1];
c[l] = 0;
return log_oom();
if (r < 0) {
log_error("Failed to parse group data from getent.");
return -EIO;
}
}
if (r < 0)
return log_error_errno(r, "Failed to make home root directory: %m");
if (r < 0 && r != -EEXIST)
return log_error_errno(r, "Failed to make home directory: %m");
if (_home) {
}
return 0;
}
/*
* Return values:
* < 0 : wait_for_terminate() failed to get the state of the
* container, the container was terminated by a signal, or
* failed for an unknown reason. No change is made to the
* container argument.
* > 0 : The program executed in the container terminated with an
* error. The exit code of the program executed in the
* container is returned. The container argument has been set
* to CONTAINER_TERMINATED.
* 0 : The container is being rebooted, has been shut down or exited
* successfully. The container argument has been set to either
* CONTAINER_TERMINATED or CONTAINER_REBOOTED.
*
* That is, success is indicated by a return value of zero, and an
* error is indicated by a non-zero value.
*/
int r;
if (r < 0)
return log_warning_errno(r, "Failed to wait for container: %m");
case CLD_EXITED:
} else
log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s failed with error code %i.", arg_machine, status.si_status);
case CLD_KILLED:
return 0;
return 0;
}
/* CLD_KILLED fallthrough */
case CLD_DUMPED:
log_error("Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
return -EIO;
default:
return -EIO;
}
return r;
}
static void nop_handler(int sig) {}
static int on_orderly_shutdown(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
if (pid > 0) {
log_info("Trying to halt container. Send SIGTERM again to trigger immediate termination.");
return 0;
}
}
return 0;
}
static int determine_names(void) {
int r;
if (!arg_image && !arg_directory) {
if (arg_machine) {
r = image_find(arg_machine, &i);
if (r < 0)
else if (r == 0) {
return -ENOENT;
}
else
if (r < 0)
return log_error_errno(r, "Invalid image directory: %m");
} else
if (!arg_directory && !arg_machine) {
log_error("Failed to determine path, please use -D or -i.");
return -EINVAL;
}
}
if (!arg_machine) {
else
if (!arg_machine)
return log_oom();
hostname_cleanup(arg_machine, false);
if (!machine_name_is_valid(arg_machine)) {
log_error("Failed to determine machine name automatically, please use -M.");
return -EINVAL;
}
if (arg_ephemeral) {
char *b;
/* Add a random suffix when this is an
* ephemeral machine, so that we can run many
* instances at once without manually having
* to specify -M each time. */
return log_oom();
arg_machine = b;
}
}
return 0;
}
_cleanup_free_ char *device_path = NULL, *root_device = NULL, *home_device = NULL, *srv_device = NULL, *console = NULL;
bool secondary = false, remove_subvol = false;
int ret = EXIT_SUCCESS;
log_open();
if (r <= 0)
goto finish;
r = determine_names();
if (r < 0)
goto finish;
if (geteuid() != 0) {
log_error("Need to be root.");
r = -EPERM;
goto finish;
}
if (sd_booted() <= 0) {
log_error("Not running on a systemd system.");
r = -EINVAL;
goto finish;
}
log_close();
n_fd_passed = sd_listen_fds(false);
if (n_fd_passed > 0) {
r = fdset_new_listen_fds(&fds, false);
if (r < 0) {
log_error_errno(r, "Failed to collect file descriptors: %m");
goto finish;
}
}
log_open();
if (arg_directory) {
log_error("Spawning container on root directory is not supported. Consider using --ephemeral.");
r = -EINVAL;
goto finish;
}
if (arg_template) {
if (r == -EEXIST) {
if (!arg_quiet)
log_info("Directory %s already exists, not populating from template %s.", arg_directory, arg_template);
} else if (r < 0) {
goto finish;
} else {
if (!arg_quiet)
}
} else if (arg_ephemeral) {
char *np;
/* If the specified path is a mount point we
* generate the new snapshot immediately
* inside it under a random name. However if
* the specified is not a mount point we
* create the new snapshot in the parent
* directory, just next to it. */
r = path_is_mount_point(arg_directory, false);
if (r < 0) {
goto finish;
}
if (r > 0)
else
if (r < 0) {
log_error_errno(r, "Failed to generate name for snapshot: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
arg_directory = np;
remove_subvol = true;
}
if (arg_boot) {
if (path_is_os_tree(arg_directory) <= 0) {
log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", arg_directory);
r = -EINVAL;
goto finish;
}
} else {
const char *p;
p = strappenda(arg_directory,
log_error("Directory %s lacks the binary to execute or doesn't look like a binary tree. Refusing.", arg_directory);
r = -EINVAL;
goto finish;
}
}
} else {
char template[] = "/tmp/nspawn-root-XXXXXX";
r = -errno;
goto finish;
}
if (!arg_directory) {
r = log_oom();
goto finish;
}
if (image_fd < 0) {
r = image_fd;
goto finish;
}
r = dissect_image(image_fd,
&secondary);
if (r < 0)
goto finish;
}
if (master < 0) {
goto finish;
}
if (r < 0) {
r = log_error_errno(r, "Failed to determine tty name: %m");
goto finish;
}
if (!arg_quiet)
log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
goto finish;
}
goto finish;
}
for (;;) {
.sa_flags = SA_NOCLDSTOP,
};
r = barrier_create(&barrier);
if (r < 0) {
log_error_errno(r, "Cannot initialize IPC barrier: %m");
goto finish;
}
/* Child can be killed before execv(), so handle SIGCHLD
* in order to interrupt parent's blocking calls and
* give it a chance to call wait() and terminate. */
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
if (pid < 0) {
r = log_error_errno(errno, "clone() failed, do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in): %m");
else
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;
n_env ++;
if (r != STDIN_FILENO) {
if (r >= 0) {
safe_close(r);
r = -EINVAL;
}
log_error_errno(r, "Failed to open console: %m");
}
}
if (setsid() < 0) {
}
if (reset_audit_loginuid() < 0)
}
/* Mark everything as slave, so that we still
* receive mounts from the real root, but don't
* propagate mounts to the real root. */
}
srv_device, srv_device_rw) < 0)
/* Turn directory into bind mount */
}
r = setup_volatile(arg_directory);
if (r < 0)
if (setup_volatile_state(arg_directory) < 0)
if (r < 0)
if (arg_read_only) {
r = bind_remount_recursive(arg_directory, true);
if (r < 0) {
log_error_errno(r, "Failed to make tree read-only: %m");
}
}
if (mount_all(arg_directory) < 0)
if (copy_devnodes(arg_directory) < 0)
if (setup_ptmx(arg_directory) < 0)
if (setup_propagate(arg_directory) < 0)
if (setup_seccomp() < 0)
/* Tell the parent that we are ready, and that
* it can cgroupify us to that we lack access
* to certain devices and resources. */
(void) barrier_place(&barrier);
if (setup_boot_id(arg_directory) < 0)
if (setup_timezone(arg_directory) < 0)
if (setup_resolv_conf(arg_directory) < 0)
if (setup_journal(arg_directory) < 0)
if (mount_tmpfs(arg_directory) < 0)
/* Wait until we are cgroup-ified, so that we
* can mount the right cgroup path writable */
(void) barrier_sync_next(&barrier);
if (mount_cgroup(arg_directory) < 0)
if (chdir(arg_directory) < 0) {
}
}
if (chroot(".") < 0) {
}
if (chdir("/") < 0) {
}
umask(0022);
if (arg_private_network)
if (drop_capabilities() < 0) {
}
r = change_uid_gid(&home);
if (r < 0)
log_oom();
}
char as_uuid[37];
if (asprintf((char**)(envp + n_env++), "container_uuid=%s", id128_format_as_uuid(arg_uuid, as_uuid)) < 0) {
log_oom();
}
}
if (fdset_size(fds) > 0) {
r = fdset_cloexec(fds, false);
if (r < 0) {
log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
}
log_oom();
}
}
if (arg_personality != 0xffffffffLU) {
if (personality(arg_personality) < 0) {
}
} else if (secondary) {
if (personality(PER_LINUX32) < 0) {
}
}
#ifdef HAVE_SELINUX
if (arg_selinux_context)
}
#endif
if (!strv_isempty(arg_setenv)) {
char **n;
if (!n) {
log_oom();
}
env_use = n;
} else
/* Wait until the parent is ready with the setup, too... */
if (!barrier_place_and_sync(&barrier))
if (arg_boot) {
char **a;
size_t l;
/* Automatically search for the init system */
a = newa(char*, l + 1);
else {
}
}
/* Wait for the most basic Child-setup to be done,
* before we add hardware to it, and place it in a
* cgroup. */
if (barrier_sync_next(&barrier)) {
char last_char = 0;
int ifi = 0;
r = move_network_interfaces(pid);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
r = setup_macvlan(pid);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
/* Block SIGCHLD here, before notifying child.
* process_pty() will handle it with the other signals. */
if (r < 0)
goto finish;
/* Reset signal to default */
if (r < 0)
goto finish;
/* Notify the child that the parent is ready with all
* its setup, and that the child can now hand over
* control to the code to run inside the container. */
(void) barrier_place(&barrier);
/* And wait that the child is completely ready now. */
(void) barrier_place_and_sync(&barrier);
sd_notify(false,
"READY=1\n"
"STATUS=Container running.");
r = sd_event_new(&event);
if (r < 0) {
log_error_errno(r, "Failed to get default event source: %m");
goto finish;
}
if (arg_boot) {
/* Try to kill the init system on SIGINT or SIGTERM */
} else {
/* Immediately exit */
}
/* simply exit on sigchld */
if (r < 0) {
log_error_errno(r, "Failed to create PTY forwarder: %m");
goto finish;
}
r = sd_event_loop(event);
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
/* Kill if it is not dead yet anyway */
}
/* Normally redundant, but better safe than sorry */
pid = 0;
if (r < 0)
/* We failed to wait for the container, or the
* container exited abnormally */
goto finish;
else if (r > 0 || container_status == CONTAINER_TERMINATED){
/* The container exited with a non-zero
* status, or with zero status and no reboot
* was requested. */
ret = r;
break;
}
/* CONTAINER_REBOOTED, loop again */
if (arg_keep_unit) {
/* Special handling if we are running as a
* service: instead of simply restarting the
* machine we want to restart the entire
* service, so let's inform systemd about this
* with the special exit code 133. The service
* file uses RestartForceExitStatus=133 so
* that this results in a full nspawn
* restart. This is necessary since we might
* have cgroup parameters set we want to have
* flushed out. */
ret = 133;
r = 0;
break;
}
}
sd_notify(false,
"STOPPING=1\n"
"STATUS=Terminating...");
if (pid > 0)
if (remove_subvol && arg_directory) {
int k;
if (k < 0)
}
if (arg_machine) {
const char *p;
(void) rm_rf(p, false, true, false);
}
return r < 0 ? EXIT_FAILURE : ret;
}