HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/nspawn/nspawn.c
Git blob: 1c687382baea8193c023ec86840c44042195bef9
Lines 1–1000 of 6,878
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/loop.h>
#include <net/if.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/keyctl.h>
#include <sys/mount.h>
#include <sys/personality.h>
#include <sys/prctl.h> /* IWYU pragma: keep */
#include <sys/wait.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "sd-event.h"
#include "sd-id128.h"
#include "sd-json.h"
#include "sd-netlink.h"
#include "sd-path.h"
#include "sd-varlink.h"
#include "acl-util.h"
#include "alloc-util.h"
#include "barrier.h"
#include "base-filesystem.h"
#include "blkid-util.h"
#include "btrfs-util.h"
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "capability-list.h"
#include "capability-util.h"
#include "cgroup-setup.h"
#include "chase.h"
#include "common-signal.h"
#include "constants.h"
#include "copy.h"
#include "cpu-set-util.h"
#include "crypto-util.h"
#include "cryptsetup-util.h"
#include "daemon-util.h"
#include "dev-setup.h"
#include "devnum-util.h"
#include "discover-image.h"
#include "dissect-image.h"
#include "dlopen-note.h"
#include "env-util.h"
#include "escape.h"
#include "ether-addr-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fdset.h"
#include "fileio.h"
#include "fork-notify.h"
#include "format-util.h"
#include "fs-util.h"
#include "gpt.h"
#include "hexdecoct.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "image-policy.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "libmount-util.h"
#include "log.h"
#include "loop-util.h"
#include "loopback-setup.h"
#include "machine-bind-user.h"
#include "machine-credential.h"
#include "machine-register.h"
#include "main-func.h"
#include "mkdir.h"
#include "mount-util.h"
#include "mountpoint-util.h"
#include "mstack.h"
#include "namespace-util.h"
#include "netlink-internal.h"
#include "notify-recv.h"
#include "nspawn.h"
#include "nspawn-bind-user.h"
#include "nspawn-cgroup.h"
#include "nspawn-expose-ports.h"
#include "nspawn-mount.h"
#include "nspawn-network.h"
#include "nspawn-oci.h"
#include "nspawn-register.h"
#include "nspawn-seccomp.h"
#include "nspawn-settings.h"
#include "nspawn-setuid.h"
#include "nspawn-stub-pid1.h"
#include "nsresource.h"
#include "os-util.h"
#include "osc-context.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-helpers.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "pidfd-util.h"
#include "pidref.h"
#include "polkit-agent.h"
#include "pretty-print.h"
#include "process-util.h"
#include "ptyfwd.h"
#include "random-util.h"
#include "raw-clone.h"
#include "resolve-util.h"
#include "rlimit-util.h"
#include "rm-rf.h"
#include "runtime-scope.h"
#include "seccomp-util.h"
#include "selinux-util.h"
#include "set.h"
#include "shift-uid.h"
#include "signal-util.h"
#include "siphash24.h"
#include "snapshot-util.h"
#include "socket-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "sysctl-util.h"
#include "terminal-util.h"
#include "tmpfile-util.h"
#include "udev-util.h"
#include "uid-classification.h"
#include "umask-util.h"
#include "unit-name.h"
#include "user-record.h"
#include "user-util.h"
#include "verbs.h"
#include "vpick.h"
/* The notify socket inside the container it can use to talk to nspawn using the sd_notify(3) protocol */
#define NSPAWN_NOTIFY_SOCKET_PATH "/run/host/notify"
#define NSPAWN_MOUNT_TUNNEL "/run/host/incoming"
#define NSPAWN_JOURNAL_SOCKET_PATH "/run/host/journal/socket"
#define EXIT_FORCE_RESTART 133
typedef enum ContainerStatus {
CONTAINER_TERMINATED,
CONTAINER_REBOOTED,
} ContainerStatus;
static char *arg_directory = NULL;
static char *arg_template = NULL;
static char *arg_chdir = NULL;
static char *arg_pivot_root_new = NULL;
static char *arg_pivot_root_old = NULL;
static char *arg_user = NULL;
static uid_t arg_uid = UID_INVALID;
static gid_t arg_gid = GID_INVALID;
static gid_t* arg_supplementary_gids = NULL;
static size_t arg_n_supplementary_gids = 0;
static sd_id128_t arg_uuid = {};
static char *arg_machine = NULL; /* The name used by the host to refer to this */
static char *arg_hostname = NULL; /* The name the payload sees by default */
static const char *arg_selinux_context = NULL;
static const char *arg_selinux_apifs_context = NULL;
static char *arg_slice = NULL;
static bool arg_private_network = false;
static bool arg_read_only = false;
static StartMode arg_start_mode = START_PID1;
static bool arg_ephemeral = false;
static LinkJournal arg_link_journal = LINK_AUTO;
static bool arg_link_journal_try = false;
static uint64_t arg_caps_retain =
(1ULL << CAP_AUDIT_CONTROL) |
(1ULL << CAP_AUDIT_WRITE) |
(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_MKNOD) |
(1ULL << CAP_NET_BIND_SERVICE) |
(1ULL << CAP_NET_BROADCAST) |
(1ULL << CAP_NET_RAW) |
(1ULL << CAP_SETFCAP) |
(1ULL << CAP_SETGID) |
(1ULL << CAP_SETPCAP) |
(1ULL << CAP_SETUID) |
(1ULL << CAP_SYS_ADMIN) |
(1ULL << CAP_SYS_BOOT) |
(1ULL << CAP_SYS_CHROOT) |
(1ULL << CAP_SYS_NICE) |
(1ULL << CAP_SYS_PTRACE) |
(1ULL << CAP_SYS_RESOURCE) |
(1ULL << CAP_SYS_TTY_CONFIG);
static uint64_t arg_caps_ambient = 0;
static CapabilityQuintet arg_full_capabilities = CAPABILITY_QUINTET_NULL;
static CustomMount *arg_custom_mounts = NULL;
static size_t arg_n_custom_mounts = 0;
static char **arg_setenv = NULL;
static bool arg_quiet = false;
static int arg_register = -1;
static bool arg_keep_unit = false;
static char **arg_network_interfaces = NULL;
static char **arg_network_macvlan = NULL;
static char **arg_network_ipvlan = NULL;
static bool arg_network_veth = false;
static char **arg_network_veth_extra = NULL;
static char *arg_network_bridge = NULL;
static char *arg_network_zone = NULL;
static char *arg_network_namespace_path = NULL;
static struct ether_addr arg_network_provided_mac = {};
static PagerFlags arg_pager_flags = 0;
static unsigned long arg_personality = PERSONALITY_INVALID;
static char *arg_image = NULL;
static char *arg_mstack = NULL;
static char *arg_oci_bundle = NULL;
static VolatileMode arg_volatile_mode = VOLATILE_NO;
static ExposePort *arg_expose_ports = NULL;
static char **arg_property = NULL;
static sd_bus_message *arg_property_message = NULL;
static UserNamespaceMode arg_userns_mode = _USER_NAMESPACE_MODE_INVALID;
static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
static unsigned arg_delegate_container_ranges = 0;
static UserNamespaceOwnership arg_userns_ownership = _USER_NAMESPACE_OWNERSHIP_INVALID;
static int arg_kill_signal = 0;
static SettingsMask arg_settings_mask = 0;
static int arg_settings_trusted = -1;
static char **arg_parameters = NULL;
static const char *arg_container_service_name = "systemd-nspawn";
static bool arg_notify_ready = false;
static bool arg_use_cgns = true;
static unsigned long arg_clone_ns_flags = CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS;
static MountSettingsMask arg_mount_settings = MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_TMPFS_TMP;
static VeritySettings arg_verity_settings = VERITY_SETTINGS_DEFAULT;
static char **arg_syscall_allow_list = NULL;
static char **arg_syscall_deny_list = NULL;
#if HAVE_SECCOMP
static scmp_filter_ctx arg_seccomp = NULL;
#endif
static struct rlimit *arg_rlimit[_RLIMIT_MAX] = {};
static bool arg_no_new_privileges = false;
static int arg_oom_score_adjust = 0;
static bool arg_oom_score_adjust_set = false;
static CPUSet arg_cpu_set = {};
static ResolvConfMode arg_resolv_conf = RESOLV_CONF_AUTO;
static TimezoneMode arg_timezone = TIMEZONE_AUTO;
static unsigned arg_console_width = UINT_MAX, arg_console_height = UINT_MAX;
static DeviceNode* arg_extra_nodes = NULL;
static size_t arg_n_extra_nodes = 0;
static char **arg_sysctl = NULL;
static ConsoleMode arg_console_mode = _CONSOLE_MODE_INVALID;
static MachineCredentialContext arg_credentials = {};
static char **arg_bind_user = NULL;
static char *arg_bind_user_shell = NULL;
static bool arg_bind_user_shell_copy = false;
static char **arg_bind_user_groups = NULL;
static bool arg_suppress_sync = false;
static Set *arg_restrict_address_families = NULL;
static bool arg_restrict_address_families_is_allowlist = false;
static char *arg_settings_filename = NULL;
static Architecture arg_architecture = _ARCHITECTURE_INVALID;
static ImagePolicy *arg_image_policy = NULL;
static char *arg_background = NULL;
static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
static bool arg_cleanup = false;
static bool arg_ask_password = true;
static char *arg_forward_journal = NULL;
static uint64_t arg_forward_journal_max_use = UINT64_MAX;
static uint64_t arg_forward_journal_keep_free = UINT64_MAX;
static uint64_t arg_forward_journal_max_file_size = UINT64_MAX;
static uint64_t arg_forward_journal_max_files = UINT64_MAX;
STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
STATIC_DESTRUCTOR_REGISTER(arg_template, freep);
STATIC_DESTRUCTOR_REGISTER(arg_chdir, freep);
STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_new, freep);
STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_old, freep);
STATIC_DESTRUCTOR_REGISTER(arg_user, freep);
STATIC_DESTRUCTOR_REGISTER(arg_supplementary_gids, freep);
STATIC_DESTRUCTOR_REGISTER(arg_machine, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
STATIC_DESTRUCTOR_REGISTER(arg_slice, freep);
STATIC_DESTRUCTOR_REGISTER(arg_setenv, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_interfaces, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_macvlan, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_ipvlan, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_veth_extra, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_bridge, freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_zone, freep);
STATIC_DESTRUCTOR_REGISTER(arg_network_namespace_path, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
STATIC_DESTRUCTOR_REGISTER(arg_mstack, freep);
STATIC_DESTRUCTOR_REGISTER(arg_oci_bundle, freep);
STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_property_message, sd_bus_message_unrefp);
STATIC_DESTRUCTOR_REGISTER(arg_parameters, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_verity_settings, verity_settings_done);
STATIC_DESTRUCTOR_REGISTER(arg_syscall_allow_list, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_syscall_deny_list, strv_freep);
#if HAVE_SECCOMP
STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
#endif
STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_done);
STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user_shell, freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user_groups, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_restrict_address_families, set_freep);
STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
STATIC_DESTRUCTOR_REGISTER(arg_background, freep);
STATIC_DESTRUCTOR_REGISTER(arg_forward_journal, freep);
COMMAND(
"systemd-nspawn\0",
"Spawn a command or OS in a lightweight container.",
.argspec = "[PATH] [ARGUMENTS…]\0",
.man_pages = "systemd-nspawn(1)\0",
.pager_flags = &arg_pager_flags,
);
static int parse_private_users(
const char *s,
UserNamespaceMode *ret_userns_mode,
uid_t *ret_uid_shift,
uid_t *ret_uid_range) {
int boolean, r;
assert(ret_userns_mode);
assert(ret_uid_shift);
assert(ret_uid_range);
if (!s)
boolean = true;
else if (!in_charset(s, DIGITS))
/* do *not* parse numbers as booleans */
boolean = parse_boolean(s);
else
boolean = -1;
if (boolean == 0) {
/* no: User namespacing off */
*ret_userns_mode = USER_NAMESPACE_NO;
*ret_uid_shift = UID_INVALID;
*ret_uid_range = UINT32_C(0x10000);
} else if (boolean > 0) {
/* yes: User namespacing on, UID range is read from root dir */
*ret_userns_mode = USER_NAMESPACE_FIXED;
*ret_uid_shift = UID_INVALID;
*ret_uid_range = UINT32_C(0x10000);
} else if (streq(s, "pick")) {
/* pick: User namespacing on, UID range is picked randomly */
*ret_userns_mode = USER_NAMESPACE_PICK; /* Note that arg_userns_ownership is
* implied by USER_NAMESPACE_PICK
* further down. */
*ret_uid_shift = UID_INVALID;
*ret_uid_range = UINT32_C(0x10000);
} else if (streq(s, "identity")) {
/* identity: User namespaces on, UID range is map of the 0…0xFFFF range to
* itself, i.e. we don't actually map anything, but do take benefit of
* isolation of capability sets. */
*ret_userns_mode = USER_NAMESPACE_FIXED;
*ret_uid_shift = 0;
*ret_uid_range = UINT32_C(0x10000);
} else if (streq(s, "managed")) {
/* managed: User namespace on, and acquire it from systemd-nsresourced */
*ret_userns_mode = USER_NAMESPACE_MANAGED;
*ret_uid_shift = UID_INVALID;
*ret_uid_range = UINT32_C(0x10000);
} else {
/* anything else: User namespacing on, UID range is explicitly configured */
r = parse_userns_uid_range(s, ret_uid_shift, ret_uid_range);
if (r < 0)
return r;
*ret_userns_mode = USER_NAMESPACE_FIXED;
}
return 0;
}
static int custom_mount_check_all(void) {
FOREACH_ARRAY(m, arg_custom_mounts, arg_n_custom_mounts)
if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) {
if (arg_userns_ownership != USER_NAMESPACE_OWNERSHIP_OFF)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--private-users-ownership=%s may not be combined with custom root mounts.",
user_namespace_ownership_to_string(arg_userns_ownership));
if (arg_uid_shift == UID_INVALID)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--private-users with automatic UID shift may not be combined with custom root mounts.");
}
return 0;
}
static int parse_capability_spec(const char *spec, uint64_t *ret_mask) {
uint64_t mask = 0;
int r;
assert(ret_mask);
for (;;) {
_cleanup_free_ char *t = NULL;
r = extract_first_word(&spec, &t, ",", 0);
if (r < 0)
return log_error_errno(r, "Failed to parse capability %s.", t);
if (r == 0)
break;
if (streq(t, "help")) {
for (unsigned i = 0; i < capability_list_length(); i++) {
const char *name;
name = capability_to_name(i);
if (name)
puts(name);
}
return 0; /* quit */
}
if (streq(t, "all"))
mask = UINT64_MAX;
else {
r = capability_from_name(t);
if (r < 0)
return log_error_errno(r, "Failed to parse capability %s.", t);
mask |= 1ULL << r;
}
}
*ret_mask = mask;
return 1; /* continue */
}
static int parse_share_ns_env(const char *name, unsigned long ns_flag) {
int r;
r = getenv_bool(name);
if (r == -ENXIO)
return 0;
if (r < 0)
return log_error_errno(r, "Failed to parse $%s: %m", name);
arg_clone_ns_flags = (arg_clone_ns_flags & ~ns_flag) | (r > 0 ? 0 : ns_flag);
arg_settings_mask |= SETTING_CLONE_NS_FLAGS;
return 0;
}
static int parse_mount_settings_env(void) {
const char *e;
int r;
r = getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
if (r < 0 && r != -ENXIO)
return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP: %m");
if (r >= 0)
SET_FLAG(arg_mount_settings, MOUNT_APPLY_TMPFS_TMP, r > 0);
e = getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
if (streq_ptr(e, "network"))
arg_mount_settings |= MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS;
else if (e) {
r = parse_boolean(e);
if (r < 0)
return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_API_VFS_WRITABLE: %m");
SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_RO, r == 0);
SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, false);
}
return 0;
}
static int parse_environment(void) {
const char *e;
int r;
r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC);
if (r < 0)
return r;
r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID);
if (r < 0)
return r;
r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS);
if (r < 0)
return r;
r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
if (r < 0)
return r;
r = parse_mount_settings_env();
if (r < 0)
return r;
/* SYSTEMD_NSPAWN_USE_CGNS=0 can be used to disable CLONE_NEWCGROUP use,
* even if it is supported. If not supported, it has no effect. */
if (!namespace_type_supported(NAMESPACE_CGROUP))
arg_use_cgns = false;
else {
r = getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
if (r < 0) {
if (r != -ENXIO)
return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_USE_CGNS: %m");
arg_use_cgns = true;
} else {
arg_use_cgns = r > 0;
arg_settings_mask |= SETTING_USE_CGNS;
}
}
e = getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
if (e)
arg_container_service_name = e;
e = getenv("SYSTEMD_NSPAWN_NETWORK_MAC");
if (e) {
r = parse_ether_addr(e, &arg_network_provided_mac);
if (r < 0)
return log_error_errno(r, "Failed to parse provided MAC address via environment variable");
}
r = getenv_bool("SYSTEMD_SUPPRESS_SYNC");
if (r >= 0)
arg_suppress_sync = r;
else if (r != -ENXIO)
log_debug_errno(r, "Failed to parse $SYSTEMD_SUPPRESS_SYNC, ignoring: %m");
return 0;
}
static int parse_argv(int argc, char *argv[]) {
uint64_t plus = 0, minus = 0;
bool mask_all_settings = false, mask_no_settings = false;
int r;
assert(argc >= 0);
assert(argv);
OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION };
FOREACH_OPTION_OR_RETURN(c, &opts) {
switch (c) {
OPTION_COMMON_HELP:
return command_print_help();
OPTION_COMMON_VERSION:
return version();
OPTION('q', "quiet", NULL, "Do not show status information"):
arg_quiet = true;
break;
OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
OPTION_LONG("settings", "BOOLEAN", "Load additional settings from .nspawn file"):
/* no → do not read files
* yes → read files, do not override cmdline, trust only subset
* override → read files, override cmdline, trust only subset
* trusted → read files, do not override cmdline, trust all
*/
r = parse_boolean(opts.arg);
if (r < 0) {
if (streq(opts.arg, "trusted")) {
mask_all_settings = false;
mask_no_settings = false;
arg_settings_trusted = true;
} else if (streq(opts.arg, "override")) {
mask_all_settings = false;
mask_no_settings = true;
arg_settings_trusted = -1;
} else
return log_error_errno(r, "Failed to parse --settings= argument: %s", opts.arg);
} else if (r > 0) {
/* yes */
mask_all_settings = false;
mask_no_settings = false;
arg_settings_trusted = -1;
} else {
/* no */
mask_all_settings = true;
mask_no_settings = false;
arg_settings_trusted = false;
}
break;
OPTION_LONG("cleanup", NULL,
"Clean up left-over mounts and underlying mount points used by the container"):
arg_cleanup = true;
break;
OPTION_COMMON_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
OPTION_GROUP("Image"): {}
OPTION('D', "directory", "PATH", "Root directory for the container"):
r = parse_path_argument(opts.arg, false, &arg_directory);
if (r < 0)
return r;
arg_settings_mask |= SETTING_DIRECTORY;
break;
OPTION_LONG("template", "PATH",
"Initialize root directory from template directory, if missing"):
r = parse_path_argument(opts.arg, false, &arg_template);
if (r < 0)
return r;
arg_settings_mask |= SETTING_DIRECTORY;
break;
OPTION('x', "ephemeral", NULL,
"Run container with snapshot of root directory, and remove it after exit"):
arg_ephemeral = true;
arg_settings_mask |= SETTING_EPHEMERAL;
break;
OPTION('i', "image", "PATH",
"Root file system disk image (or device node) for the container"):
r = parse_path_argument(opts.arg, false, &arg_image);
if (r < 0)
return r;
arg_settings_mask |= SETTING_DIRECTORY;
break;
OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
r = parse_image_policy_argument(opts.arg, &arg_image_policy);
if (r < 0)
return r;
break;
OPTION_LONG("mstack", "PATH", /* help= */ NULL):
r = parse_path_argument(opts.arg, false, &arg_mstack);
if (r < 0)
return r;
arg_settings_mask |= SETTING_DIRECTORY;
break;
OPTION_LONG("oci-bundle", "PATH", "OCI bundle directory"):
r = parse_path_argument(opts.arg, false, &arg_oci_bundle);
if (r < 0)
return r;
break;
OPTION_LONG("read-only", NULL, "Mount the root directory read-only"):
arg_read_only = true;
arg_settings_mask |= SETTING_READ_ONLY;
break;
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "volatile", "MODE", "Run the system in volatile mode"):
if (!opts.arg)
arg_volatile_mode = VOLATILE_YES;
else if (streq(opts.arg, "help"))
return DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
else {
VolatileMode m;
m = volatile_mode_from_string(opts.arg);
if (m < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to parse --volatile= argument: %s", opts.arg);
else
arg_volatile_mode = m;
}
arg_settings_mask |= SETTING_VOLATILE_MODE;
break;
OPTION_LONG("root-hash", "HASH", "Specify verity root hash for root disk image"): {
_cleanup_(iovec_done) struct iovec k = {};
r = unhexmem(opts.arg, &k.iov_base, &k.iov_len);
if (r < 0)
return log_error_errno(r, "Failed to parse root hash: %s", opts.arg);
if (k.iov_len < sizeof(sd_id128_t))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root hash must be at least 128-bit long: %s", opts.arg);
iovec_done(&arg_verity_settings.root_hash);
arg_verity_settings.root_hash = TAKE_STRUCT(k);
break;
}
OPTION_LONG("root-hash-sig", "SIG",
"Specify pkcs7 signature of root hash for verity"): {
_cleanup_(iovec_done) struct iovec p = {};
const char *value;
if ((value = startswith(opts.arg, "base64:"))) {
r = unbase64mem(value, &p.iov_base, &p.iov_len);
if (r < 0)
return log_error_errno(r, "Failed to parse root hash signature '%s': %m", opts.arg);
} else {
r = read_full_file(opts.arg, (char**) &p.iov_base, &p.iov_len);
if (r < 0)
return log_error_errno(r, "Failed to parse root hash signature file '%s': %m", opts.arg);
}
iovec_done(&arg_verity_settings.root_hash_sig);
arg_verity_settings.root_hash_sig = TAKE_STRUCT(p);
break;
}
OPTION_LONG("verity-data", "PATH", "Specify hash device for verity"):
r = parse_path_argument(opts.arg, false, &arg_verity_settings.data_path);
if (r < 0)
return r;
break;
OPTION_LONG("pivot-root", "PATH[:PATH]",
"Pivot root to given directory in the container"):
r = pivot_root_parse(&arg_pivot_root_new, &arg_pivot_root_old, opts.arg);
if (r < 0)
return log_error_errno(r, "Failed to parse --pivot-root= argument %s: %m", opts.arg);
arg_settings_mask |= SETTING_PIVOT_ROOT;
break;
OPTION_GROUP("Execution"): {}
OPTION('a', "as-pid2", NULL, "Maintain a stub init as PID1, invoke binary as PID2"):
if (arg_start_mode == START_BOOT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--boot and --as-pid2 may not be combined.");
arg_start_mode = START_PID2;
arg_settings_mask |= SETTING_START_MODE;
break;
OPTION('b', "boot", NULL, "Boot up full system (i.e. invoke init)"):
if (arg_start_mode == START_PID2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--boot and --as-pid2 may not be combined.");
arg_start_mode = START_BOOT;
arg_settings_mask |= SETTING_START_MODE;
break;
OPTION_LONG("chdir", "PATH", "Set working directory in the container"): {
_cleanup_free_ char *wd = NULL;
if (!path_is_absolute(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Working directory %s is not an absolute path.", opts.arg);
r = path_simplify_alloc(opts.arg, &wd);
if (r < 0)
return log_error_errno(r, "Failed to simplify path %s: %m", opts.arg);
if (!path_is_normalized(wd))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Working directory path is not normalized: %s", wd);
if (path_below_api_vfs(wd))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Working directory is below API VFS, refusing: %s", wd);
free_and_replace(arg_chdir, wd);
arg_settings_mask |= SETTING_WORKING_DIRECTORY;
break;
}
OPTION('E', "setenv", "NAME[=VALUE]", "Pass an environment variable to PID 1"):
r = strv_env_replace_strdup_passthrough(&arg_setenv, opts.arg);
if (r < 0)
return log_error_errno(r, "Cannot assign environment variable %s: %m", opts.arg);
arg_settings_mask |= SETTING_ENVIRONMENT;
break;
OPTION('u', "uid", "USER", "Run the command under specified user or UID"):
r = free_and_strdup(&arg_user, opts.arg);
if (r < 0)
return log_oom();
arg_settings_mask |= SETTING_USER;
break;
OPTION_LONG("kill-signal", "SIGNAL", "Select signal to use for shutting down PID 1"):
if (streq(opts.arg, "help"))
return DUMP_STRING_TABLE(signal, int, _NSIG);
arg_kill_signal = signal_from_string(opts.arg);
if (arg_kill_signal < 0)
return log_error_errno(arg_kill_signal, "Cannot parse signal: %s", opts.arg);
arg_settings_mask |= SETTING_KILL_SIGNAL;
break;
OPTION_LONG("notify-ready", "BOOLEAN", "Receive notifications from the child init process"):
r = parse_boolean_argument("--notify-ready=", opts.arg, &arg_notify_ready);
if (r < 0)
return r;
arg_settings_mask |= SETTING_NOTIFY_READY;
break;
OPTION_LONG("suppress-sync", "BOOLEAN", "Suppress any form of disk data synchronization"):
r = parse_boolean_argument("--suppress-sync=", opts.arg, &arg_suppress_sync);
if (r < 0)
return r;
arg_settings_mask |= SETTING_SUPPRESS_SYNC;
break;
OPTION_GROUP("System Identity"): {}
OPTION('M', "machine", "NAME", "Set the machine name for the container"):
if (!isempty(opts.arg) && !hostname_is_valid(opts.arg, /* flags= */ 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid machine name: %s", opts.arg);
r = free_and_strdup_warn(&arg_machine, opts.arg);
if (r < 0)
return r;
break;
OPTION_LONG("hostname", "NAME", "Override the hostname for the container"):
if (!isempty(opts.arg) && !hostname_is_valid(opts.arg, /* flags= */ 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid hostname: %s", opts.arg);
r = free_and_strdup_warn(&arg_hostname, opts.arg);
if (r < 0)
return r;
arg_settings_mask |= SETTING_HOSTNAME;
break;
OPTION_LONG("uuid", "UUID", "Set a specific machine UUID for the container"):
r = id128_from_string_nonzero(opts.arg, &arg_uuid);
if (r == -ENXIO)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Machine UUID may not be all zeroes.");
if (r < 0)
return log_error_errno(r, "Invalid UUID: %s", opts.arg);
arg_settings_mask |= SETTING_MACHINE_ID;
break;
OPTION_GROUP("Properties"): {}
OPTION('S', "slice", "SLICE", "Place the container in the specified slice"): {
_cleanup_free_ char *mangled = NULL;
r = unit_name_mangle_with_suffix(opts.arg, NULL, UNIT_NAME_MANGLE_WARN, ".slice", &mangled);
if (r < 0)
return log_oom();
free_and_replace(arg_slice, mangled);
arg_settings_mask |= SETTING_SLICE;
break;
}
OPTION_LONG("property", "NAME=VALUE", "Set scope unit property"):
if (strv_extend(&arg_property, opts.arg) < 0)
return log_oom();
break;
OPTION_LONG("register", "BOOLEAN", "Register container as machine"):
r = parse_tristate_argument_with_auto("--register=", opts.arg, &arg_register);
if (r < 0)
return r;
break;
OPTION_LONG("keep-unit", NULL,
"Do not register a scope for the machine, reuse the service unit nspawn is running in"):
arg_keep_unit = true;
break;
OPTION_GROUP("User Namespacing"): {}
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "private-users", "MODE",
"Run within user namespace, configure UID/GID range"):
r = parse_private_users(opts.arg, &arg_userns_mode, &arg_uid_shift, &arg_uid_range);
if (r < 0)
return r;
arg_settings_mask |= SETTING_USERNS;
break;
OPTION_LONG("private-users-ownership", "MODE",
"Adjust ('chown') or map ('map') OS tree ownership to private UID/GID range"):
if (streq(opts.arg, "help"))
return DUMP_STRING_TABLE(user_namespace_ownership, UserNamespaceOwnership, _USER_NAMESPACE_OWNERSHIP_MAX);
arg_userns_ownership = user_namespace_ownership_from_string(opts.arg);
if (arg_userns_ownership < 0)
return log_error_errno(arg_userns_ownership, "Cannot parse --private-users-ownership= value: %s", opts.arg);
arg_settings_mask |= SETTING_USERNS;
break;
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "private-users-chown", "MODE", /* help= */ NULL): /* obsolete */
arg_userns_ownership = USER_NAMESPACE_OWNERSHIP_CHOWN;
arg_settings_mask |= SETTING_USERNS;
break;
OPTION_LONG("private-users-delegate", "N",
"Delegate N additional 64K UID/GID ranges for use by nested containers"):
r = safe_atou(opts.arg, &arg_delegate_container_ranges);
if (r < 0)
return log_error_errno(r, "Failed to parse --private-users-delegate= parameter: %s", opts.arg);
arg_settings_mask |= SETTING_USERNS;
break;
OPTION_SHORT('U', NULL,
"Equivalent to --private-users=pick and --private-users-ownership=auto"):
if (userns_supported()) {
arg_userns_mode = _USER_NAMESPACE_MODE_INVALID;
arg_uid_shift = UID_INVALID;
arg_uid_range = UINT32_C(0x10000);
arg_settings_mask |= SETTING_USERNS;
}
break;
OPTION_GROUP("Networking"): {}
OPTION_LONG("private-network", NULL, "Disable network in container"):
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-interface", "HOSTIF[:CONTAINERIF]",
"Assign an existing network interface to the container"):
r = interface_pair_parse(&arg_network_interfaces, opts.arg);
if (r < 0)
return r;
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-macvlan", "HOSTIF[:CONTAINERIF]",
"Create a macvlan network interface based on an existing network interface to the container"):
r = macvlan_pair_parse(&arg_network_macvlan, opts.arg);
if (r < 0)
return r;
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-ipvlan", "HOSTIF[:CONTAINERIF]",
"Create an ipvlan network interface based on an existing network interface to the container"):
r = ipvlan_pair_parse(&arg_network_ipvlan, opts.arg);
if (r < 0)
return r;
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION('n', "network-veth", NULL,
"Add a virtual Ethernet connection between host and container"):
arg_network_veth = true;
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-veth-extra", "HOSTIF[:CONTAINERIF]",
"Add an additional virtual Ethernet link between host and container"):
r = veth_extra_parse(&arg_network_veth_extra, opts.arg);
if (r < 0)
return log_error_errno(r, "Failed to parse --network-veth-extra= parameter: %s", opts.arg);
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-bridge", "INTERFACE",
"Add a virtual Ethernet connection to the container and attach it to an existing bridge on the host"):
if (!ifname_valid(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Bridge interface name not valid: %s", opts.arg);
r = free_and_strdup(&arg_network_bridge, opts.arg);
if (r < 0)
return log_oom();
arg_network_veth = true;
arg_private_network = true;
arg_settings_mask |= SETTING_NETWORK;
break;
OPTION_LONG("network-zone", "NAME",
"Similar, but attach the new interface to an automatically managed bridge interface"): {
_cleanup_free_ char *j = NULL;
j = strjoin("vz-", opts.arg);
if (!j)
return log_oom();
if (!ifname_valid(j))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Network zone name not valid: %s", j);
free_and_replace(arg_network_zone, j);
arg_network_veth = true;
arg_private_network = true;