]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn.c
tree-wide: make sure our control buffers are properly aligned
[thirdparty/systemd.git] / src / nspawn / nspawn.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
88213476 2
349cc4a5 3#if HAVE_BLKID
8fe0087e 4#endif
88213476 5#include <errno.h>
88213476 6#include <getopt.h>
503f480f 7#include <linux/fs.h>
1b9e5b12 8#include <linux/loop.h>
349cc4a5 9#if HAVE_SELINUX
8fe0087e 10#include <selinux/selinux.h>
1b9e5b12 11#endif
8fe0087e 12#include <stdlib.h>
8fe0087e 13#include <sys/file.h>
8fe0087e
LP
14#include <sys/personality.h>
15#include <sys/prctl.h>
16#include <sys/types.h>
6916b164 17#include <sys/wait.h>
8fe0087e 18#include <unistd.h>
1b9e5b12 19
b053cd5f 20#include "sd-bus.h"
1f0cd86b 21#include "sd-daemon.h"
1f0cd86b 22#include "sd-id128.h"
8fe0087e 23
b5efdb8a 24#include "alloc-util.h"
8fe0087e
LP
25#include "barrier.h"
26#include "base-filesystem.h"
27#include "blkid-util.h"
28#include "btrfs-util.h"
b8ea7a6e 29#include "bus-error.h"
b053cd5f 30#include "bus-util.h"
8fe0087e 31#include "cap-list.h"
430f0182 32#include "capability-util.h"
04d391da 33#include "cgroup-util.h"
8fe0087e 34#include "copy.h"
d107bb7d 35#include "cpu-set-util.h"
4fc9982c 36#include "dev-setup.h"
2d845785 37#include "dissect-image.h"
8fe0087e 38#include "env-util.h"
3ffd4af2 39#include "fd-util.h"
842f3b0f 40#include "fdset.h"
a5c32cff 41#include "fileio.h"
f97b34a6 42#include "format-util.h"
f4f15635 43#include "fs-util.h"
1b9e5b12 44#include "gpt.h"
4623e8e6 45#include "hexdecoct.h"
8fe0087e 46#include "hostname-util.h"
910fd145 47#include "id128-util.h"
8fe0087e 48#include "log.h"
2d845785 49#include "loop-util.h"
8fe0087e 50#include "loopback-setup.h"
1b9cebf6 51#include "machine-image.h"
8fe0087e 52#include "macro.h"
44dbef90 53#include "main-func.h"
f5947a5e 54#include "missing_sched.h"
8fe0087e 55#include "mkdir.h"
4349cd7c 56#include "mount-util.h"
049af8ad 57#include "mountpoint-util.h"
0cb8e3d1 58#include "namespace-util.h"
8fe0087e 59#include "netlink-util.h"
07630cea 60#include "nspawn-cgroup.h"
3603efde 61#include "nspawn-def.h"
07630cea
LP
62#include "nspawn-expose-ports.h"
63#include "nspawn-mount.h"
64#include "nspawn-network.h"
de40a303 65#include "nspawn-oci.h"
7336138e 66#include "nspawn-patch-uid.h"
07630cea 67#include "nspawn-register.h"
910fd145 68#include "nspawn-seccomp.h"
07630cea
LP
69#include "nspawn-settings.h"
70#include "nspawn-setuid.h"
7732f92b 71#include "nspawn-stub-pid1.h"
d8b4d14d 72#include "nulstr-util.h"
d58ad743 73#include "os-util.h"
50ebcf6c 74#include "pager.h"
6bedfcbb 75#include "parse-util.h"
8fe0087e 76#include "path-util.h"
294bf0c3 77#include "pretty-print.h"
0b452006 78#include "process-util.h"
8fe0087e
LP
79#include "ptyfwd.h"
80#include "random-util.h"
8869a0b4 81#include "raw-clone.h"
86775e35 82#include "resolve-util.h"
bf428efb 83#include "rlimit-util.h"
8fe0087e 84#include "rm-rf.h"
de40a303
LP
85#if HAVE_SECCOMP
86#include "seccomp-util.h"
87#endif
68b02049 88#include "selinux-util.h"
8fe0087e 89#include "signal-util.h"
2583fbea 90#include "socket-util.h"
8fcde012 91#include "stat-util.h"
15a5e950 92#include "stdio-util.h"
5c828e66 93#include "string-table.h"
07630cea 94#include "string-util.h"
8fe0087e 95#include "strv.h"
de40a303 96#include "sysctl-util.h"
8fe0087e 97#include "terminal-util.h"
e4de7287 98#include "tmpfile-util.h"
affb60b1 99#include "umask-util.h"
43c3fb46 100#include "unit-name.h"
b1d4f8e1 101#include "user-util.h"
8fe0087e 102#include "util.h"
e9642be2 103
9c1e04d0
AP
104/* nspawn is listening on the socket at the path in the constant nspawn_notify_socket_path
105 * nspawn_notify_socket_path is relative to the container
106 * the init process in the container pid can send messages to nspawn following the sd_notify(3) protocol */
107#define NSPAWN_NOTIFY_SOCKET_PATH "/run/systemd/nspawn/notify"
0e7ac751 108
2a49b612
ZJS
109#define EXIT_FORCE_RESTART 133
110
113cea80
DH
111typedef enum ContainerStatus {
112 CONTAINER_TERMINATED,
6145bb4f 113 CONTAINER_REBOOTED,
113cea80
DH
114} ContainerStatus;
115
88213476 116static char *arg_directory = NULL;
ec16945e 117static char *arg_template = NULL;
5f932eb9 118static char *arg_chdir = NULL;
b53ede69
PW
119static char *arg_pivot_root_new = NULL;
120static char *arg_pivot_root_old = NULL;
687d0825 121static char *arg_user = NULL;
de40a303
LP
122static uid_t arg_uid = UID_INVALID;
123static gid_t arg_gid = GID_INVALID;
124static gid_t* arg_supplementary_gids = NULL;
125static size_t arg_n_supplementary_gids = 0;
9444b1f2 126static sd_id128_t arg_uuid = {};
3a9530e5
LP
127static char *arg_machine = NULL; /* The name used by the host to refer to this */
128static char *arg_hostname = NULL; /* The name the payload sees by default */
c74e630d
LP
129static const char *arg_selinux_context = NULL;
130static const char *arg_selinux_apifs_context = NULL;
de40a303 131static char *arg_slice = NULL;
ff01d048 132static bool arg_private_network = false;
bc2f673e 133static bool arg_read_only = false;
7732f92b 134static StartMode arg_start_mode = START_PID1;
ec16945e 135static bool arg_ephemeral = false;
57fb9fb5 136static LinkJournal arg_link_journal = LINK_AUTO;
574edc90 137static bool arg_link_journal_try = false;
520e0d54 138static uint64_t arg_caps_retain =
50b52222
LP
139 (1ULL << CAP_AUDIT_CONTROL) |
140 (1ULL << CAP_AUDIT_WRITE) |
5076f0cc
LP
141 (1ULL << CAP_CHOWN) |
142 (1ULL << CAP_DAC_OVERRIDE) |
143 (1ULL << CAP_DAC_READ_SEARCH) |
144 (1ULL << CAP_FOWNER) |
145 (1ULL << CAP_FSETID) |
146 (1ULL << CAP_IPC_OWNER) |
147 (1ULL << CAP_KILL) |
148 (1ULL << CAP_LEASE) |
149 (1ULL << CAP_LINUX_IMMUTABLE) |
50b52222 150 (1ULL << CAP_MKNOD) |
5076f0cc
LP
151 (1ULL << CAP_NET_BIND_SERVICE) |
152 (1ULL << CAP_NET_BROADCAST) |
153 (1ULL << CAP_NET_RAW) |
5076f0cc 154 (1ULL << CAP_SETFCAP) |
50b52222 155 (1ULL << CAP_SETGID) |
5076f0cc
LP
156 (1ULL << CAP_SETPCAP) |
157 (1ULL << CAP_SETUID) |
158 (1ULL << CAP_SYS_ADMIN) |
50b52222 159 (1ULL << CAP_SYS_BOOT) |
5076f0cc
LP
160 (1ULL << CAP_SYS_CHROOT) |
161 (1ULL << CAP_SYS_NICE) |
162 (1ULL << CAP_SYS_PTRACE) |
d87be9b0 163 (1ULL << CAP_SYS_RESOURCE) |
50b52222 164 (1ULL << CAP_SYS_TTY_CONFIG);
de40a303 165static CapabilityQuintet arg_full_capabilities = CAPABILITY_QUINTET_NULL;
5a8af538 166static CustomMount *arg_custom_mounts = NULL;
88614c8a 167static size_t arg_n_custom_mounts = 0;
f4889f65 168static char **arg_setenv = NULL;
284c0b91 169static bool arg_quiet = false;
eb91eb18 170static bool arg_register = true;
89f7c846 171static bool arg_keep_unit = false;
aa28aefe 172static char **arg_network_interfaces = NULL;
c74e630d 173static char **arg_network_macvlan = NULL;
4bbfe7ad 174static char **arg_network_ipvlan = NULL;
69c79d3c 175static bool arg_network_veth = false;
f6d6bad1 176static char **arg_network_veth_extra = NULL;
f757855e 177static char *arg_network_bridge = NULL;
22b28dfd 178static char *arg_network_zone = NULL;
d7bea6b6 179static char *arg_network_namespace_path = NULL;
bb068de0 180static PagerFlags arg_pager_flags = 0;
050f7277 181static unsigned long arg_personality = PERSONALITY_INVALID;
ec16945e 182static char *arg_image = NULL;
de40a303 183static char *arg_oci_bundle = NULL;
f757855e 184static VolatileMode arg_volatile_mode = VOLATILE_NO;
6d0b55c2 185static ExposePort *arg_expose_ports = NULL;
f36933fe 186static char **arg_property = NULL;
de40a303 187static sd_bus_message *arg_property_message = NULL;
0de7acce 188static UserNamespaceMode arg_userns_mode = USER_NAMESPACE_NO;
6dac160c 189static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
0de7acce 190static bool arg_userns_chown = false;
c6c8f6e2 191static int arg_kill_signal = 0;
5da38d07 192static CGroupUnified arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_UNKNOWN;
f757855e
LP
193static SettingsMask arg_settings_mask = 0;
194static int arg_settings_trusted = -1;
195static char **arg_parameters = NULL;
6aadfa4c 196static const char *arg_container_service_name = "systemd-nspawn";
9c1e04d0 197static bool arg_notify_ready = false;
5a8ff0e6 198static bool arg_use_cgns = true;
0c582db0 199static unsigned long arg_clone_ns_flags = CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS;
1099ceeb 200static MountSettingsMask arg_mount_settings = MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_TMPFS_TMP;
4623e8e6
LP
201static void *arg_root_hash = NULL;
202static size_t arg_root_hash_size = 0;
960e4569
LP
203static char **arg_syscall_whitelist = NULL;
204static char **arg_syscall_blacklist = NULL;
de40a303
LP
205#if HAVE_SECCOMP
206static scmp_filter_ctx arg_seccomp = NULL;
207#endif
bf428efb 208static struct rlimit *arg_rlimit[_RLIMIT_MAX] = {};
66edd963 209static bool arg_no_new_privileges = false;
81f345df
LP
210static int arg_oom_score_adjust = 0;
211static bool arg_oom_score_adjust_set = false;
0985c7c4 212static CPUSet arg_cpu_set = {};
09d423e9 213static ResolvConfMode arg_resolv_conf = RESOLV_CONF_AUTO;
1688841f 214static TimezoneMode arg_timezone = TIMEZONE_AUTO;
de40a303
LP
215static unsigned arg_console_width = (unsigned) -1, arg_console_height = (unsigned) -1;
216static DeviceNode* arg_extra_nodes = NULL;
217static size_t arg_n_extra_nodes = 0;
218static char **arg_sysctl = NULL;
219static ConsoleMode arg_console_mode = _CONSOLE_MODE_INVALID;
88213476 220
6145bb4f
LP
221STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
222STATIC_DESTRUCTOR_REGISTER(arg_template, freep);
223STATIC_DESTRUCTOR_REGISTER(arg_chdir, freep);
224STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_new, freep);
225STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_old, freep);
226STATIC_DESTRUCTOR_REGISTER(arg_user, freep);
227STATIC_DESTRUCTOR_REGISTER(arg_supplementary_gids, freep);
228STATIC_DESTRUCTOR_REGISTER(arg_machine, freep);
229STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
230STATIC_DESTRUCTOR_REGISTER(arg_slice, freep);
231STATIC_DESTRUCTOR_REGISTER(arg_setenv, strv_freep);
232STATIC_DESTRUCTOR_REGISTER(arg_network_interfaces, strv_freep);
233STATIC_DESTRUCTOR_REGISTER(arg_network_macvlan, strv_freep);
234STATIC_DESTRUCTOR_REGISTER(arg_network_ipvlan, strv_freep);
235STATIC_DESTRUCTOR_REGISTER(arg_network_veth_extra, strv_freep);
236STATIC_DESTRUCTOR_REGISTER(arg_network_bridge, freep);
237STATIC_DESTRUCTOR_REGISTER(arg_network_zone, freep);
238STATIC_DESTRUCTOR_REGISTER(arg_network_namespace_path, freep);
239STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
240STATIC_DESTRUCTOR_REGISTER(arg_oci_bundle, freep);
241STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
242STATIC_DESTRUCTOR_REGISTER(arg_property_message, sd_bus_message_unrefp);
243STATIC_DESTRUCTOR_REGISTER(arg_parameters, strv_freep);
244STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
245STATIC_DESTRUCTOR_REGISTER(arg_syscall_whitelist, strv_freep);
246STATIC_DESTRUCTOR_REGISTER(arg_syscall_blacklist, strv_freep);
247#if HAVE_SECCOMP
248STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
249#endif
0985c7c4 250STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_reset);
6145bb4f
LP
251STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
252
dce66ffe
ZJS
253static int handle_arg_console(const char *arg) {
254 if (streq(arg, "help")) {
255 puts("interactive\n"
256 "read-only\n"
257 "passive\n"
258 "pipe");
259 return 0;
260 }
261
262 if (streq(arg, "interactive"))
263 arg_console_mode = CONSOLE_INTERACTIVE;
264 else if (streq(arg, "read-only"))
265 arg_console_mode = CONSOLE_READ_ONLY;
266 else if (streq(arg, "passive"))
267 arg_console_mode = CONSOLE_PASSIVE;
268 else if (streq(arg, "pipe"))
269 arg_console_mode = CONSOLE_PIPE;
270 else
271 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown console mode: %s", optarg);
272
273 arg_settings_mask |= SETTING_CONSOLE_MODE;
274 return 1;
275}
276
37ec0fdd
LP
277static int help(void) {
278 _cleanup_free_ char *link = NULL;
279 int r;
280
bb068de0 281 (void) pager_open(arg_pager_flags);
50ebcf6c 282
37ec0fdd
LP
283 r = terminal_urlify_man("systemd-nspawn", "1", &link);
284 if (r < 0)
285 return log_oom();
286
25148653 287 printf("%1$s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
37a92352 288 "%5$sSpawn a command or OS in a light-weight container.%6$s\n\n"
a8828ed9
DW
289 " -h --help Show this help\n"
290 " --version Print version string\n"
69c79d3c 291 " -q --quiet Do not show status information\n"
bb068de0 292 " --no-pager Do not pipe output into a pager\n"
25148653
LP
293 " --settings=BOOLEAN Load additional settings from .nspawn file\n\n"
294 "%3$sImage:%4$s\n"
1b9e5b12 295 " -D --directory=PATH Root directory for the container\n"
ec16945e
LP
296 " --template=PATH Initialize root directory from template directory,\n"
297 " if missing\n"
298 " -x --ephemeral Run container with snapshot of root directory, and\n"
299 " remove it after exit\n"
25e68fd3
LP
300 " -i --image=PATH Root file system disk image (or device node) for\n"
301 " the container\n"
de40a303 302 " --oci-bundle=PATH OCI bundle directory\n"
25148653
LP
303 " --read-only Mount the root directory read-only\n"
304 " --volatile[=MODE] Run the system in volatile mode\n"
25e68fd3 305 " --root-hash=HASH Specify verity root hash for root disk image\n"
25148653
LP
306 " --pivot-root=PATH[:PATH]\n"
307 " Pivot root to given directory in the container\n\n"
308 "%3$sExecution:%4$s\n"
7732f92b 309 " -a --as-pid2 Maintain a stub init as PID1, invoke binary as PID2\n"
a8828ed9 310 " -b --boot Boot up full system (i.e. invoke init)\n"
5f932eb9 311 " --chdir=PATH Set working directory in the container\n"
25148653
LP
312 " -E --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
313 " -u --user=USER Run the command under specified user or UID\n"
314 " --kill-signal=SIGNAL Select signal to use for shutting down PID 1\n"
315 " --notify-ready=BOOLEAN Receive notifications from the child init process\n\n"
316 "%3$sSystem Identity:%4$s\n"
a8828ed9 317 " -M --machine=NAME Set the machine name for the container\n"
3a9530e5 318 " --hostname=NAME Override the hostname for the container\n"
25148653
LP
319 " --uuid=UUID Set a specific machine UUID for the container\n\n"
320 "%3$sProperties:%4$s\n"
a8828ed9 321 " -S --slice=SLICE Place the container in the specified slice\n"
f36933fe 322 " --property=NAME=VALUE Set scope unit property\n"
25148653
LP
323 " --register=BOOLEAN Register container as machine\n"
324 " --keep-unit Do not register a scope for the machine, reuse\n"
325 " the service unit nspawn is running in\n\n"
326 "%3$sUser Namespacing:%4$s\n"
90b4a64d 327 " -U --private-users=pick Run within user namespace, autoselect UID/GID range\n"
03cfe0d5 328 " --private-users[=UIDBASE[:NUIDS]]\n"
90b4a64d 329 " Similar, but with user configured UID/GID range\n"
25148653
LP
330 " --private-users-chown Adjust OS tree ownership to private UID/GID range\n\n"
331 "%3$sNetworking:%4$s\n"
69c79d3c
LP
332 " --private-network Disable network in container\n"
333 " --network-interface=INTERFACE\n"
334 " Assign an existing network interface to the\n"
335 " container\n"
c74e630d
LP
336 " --network-macvlan=INTERFACE\n"
337 " Create a macvlan network interface based on an\n"
338 " existing network interface to the container\n"
4bbfe7ad
TG
339 " --network-ipvlan=INTERFACE\n"
340 " Create a ipvlan network interface based on an\n"
341 " existing network interface to the container\n"
a8eaaee7 342 " -n --network-veth Add a virtual Ethernet connection between host\n"
69c79d3c 343 " and container\n"
f6d6bad1
LP
344 " --network-veth-extra=HOSTIF[:CONTAINERIF]\n"
345 " Add an additional virtual Ethernet link between\n"
346 " host and container\n"
ab046dde 347 " --network-bridge=INTERFACE\n"
90b4a64d
ZJS
348 " Add a virtual Ethernet connection to the container\n"
349 " and attach it to an existing bridge on the host\n"
350 " --network-zone=NAME Similar, but attach the new interface to an\n"
351 " an automatically managed bridge interface\n"
d7bea6b6
DP
352 " --network-namespace-path=PATH\n"
353 " Set network namespace to the one represented by\n"
354 " the specified kernel namespace file node\n"
6d0b55c2 355 " -p --port=[PROTOCOL:]HOSTPORT[:CONTAINERPORT]\n"
25148653
LP
356 " Expose a container IP port on the host\n\n"
357 "%3$sSecurity:%4$s\n"
a8828ed9
DW
358 " --capability=CAP In addition to the default, retain specified\n"
359 " capability\n"
360 " --drop-capability=CAP Drop the specified capability from the default set\n"
f4e803c8 361 " --no-new-privileges Set PR_SET_NO_NEW_PRIVS flag for container payload\n"
960e4569
LP
362 " --system-call-filter=LIST|~LIST\n"
363 " Permit/prohibit specific system calls\n"
25148653
LP
364 " -Z --selinux-context=SECLABEL\n"
365 " Set the SELinux security context to be used by\n"
366 " processes in the container\n"
367 " -L --selinux-apifs-context=SECLABEL\n"
368 " Set the SELinux security context to be used by\n"
369 " API/tmpfs file systems in the container\n\n"
370 "%3$sResources:%4$s\n"
bf428efb 371 " --rlimit=NAME=LIMIT Set a resource limit for the payload\n"
81f345df
LP
372 " --oom-score-adjust=VALUE\n"
373 " Adjust the OOM score value for the payload\n"
f4e803c8
LP
374 " --cpu-affinity=CPUS Adjust the CPU affinity of the container\n"
375 " --personality=ARCH Pick personality for this container\n\n"
25148653 376 "%3$sIntegration:%4$s\n"
09d423e9 377 " --resolv-conf=MODE Select mode of /etc/resolv.conf initialization\n"
1688841f 378 " --timezone=MODE Select mode of /etc/localtime initialization\n"
25148653
LP
379 " --link-journal=MODE Link up guest journal, one of no, auto, guest, \n"
380 " host, try-guest, try-host\n"
381 " -j Equivalent to --link-journal=try-guest\n\n"
382 "%3$sMounts:%4$s\n"
5e5bfa6e
EY
383 " --bind=PATH[:PATH[:OPTIONS]]\n"
384 " Bind mount a file or directory from the host into\n"
a8828ed9 385 " the container\n"
5e5bfa6e
EY
386 " --bind-ro=PATH[:PATH[:OPTIONS]\n"
387 " Similar, but creates a read-only bind mount\n"
de40a303
LP
388 " --inaccessible=PATH Over-mount file node with inaccessible node to mask\n"
389 " it\n"
06c17c39 390 " --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
5a8af538
LP
391 " --overlay=PATH[:PATH...]:PATH\n"
392 " Create an overlay mount from the host to \n"
393 " the container\n"
394 " --overlay-ro=PATH[:PATH...]:PATH\n"
25148653
LP
395 " Similar, but creates a read-only overlay mount\n\n"
396 "%3$sInput/Output:%4$s\n"
de40a303
LP
397 " --console=MODE Select how stdin/stdout/stderr and /dev/console are\n"
398 " set up for the container.\n"
399 " -P --pipe Equivalent to --console=pipe\n"
25148653 400 "\nSee the %2$s for details.\n"
37ec0fdd
LP
401 , program_invocation_short_name
402 , link
37a92352
LP
403 , ansi_underline(), ansi_normal()
404 , ansi_highlight(), ansi_normal()
405 );
37ec0fdd
LP
406
407 return 0;
88213476
LP
408}
409
86c0dd4a 410static int custom_mount_check_all(void) {
88614c8a 411 size_t i;
5a8af538 412
5a8af538
LP
413 for (i = 0; i < arg_n_custom_mounts; i++) {
414 CustomMount *m = &arg_custom_mounts[i];
415
0de7acce 416 if (path_equal(m->destination, "/") && arg_userns_mode != USER_NAMESPACE_NO) {
baaa35ad
ZJS
417 if (arg_userns_chown)
418 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
419 "--private-users-chown may not be combined with custom root mounts.");
420 else if (arg_uid_shift == UID_INVALID)
421 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
422 "--private-users with automatic UID shift may not be combined with custom root mounts.");
825d5287 423 }
5a8af538
LP
424 }
425
426 return 0;
427}
428
8199d554 429static int detect_unified_cgroup_hierarchy_from_environment(void) {
c78c095b 430 const char *e, *var = "SYSTEMD_NSPAWN_UNIFIED_HIERARCHY";
415fc41c 431 int r;
5da38d07 432
efdb0237 433 /* Allow the user to control whether the unified hierarchy is used */
c78c095b
ZJS
434
435 e = getenv(var);
436 if (!e) {
d5fc5b2f 437 /* $UNIFIED_CGROUP_HIERARCHY has been renamed to $SYSTEMD_NSPAWN_UNIFIED_HIERARCHY. */
c78c095b
ZJS
438 var = "UNIFIED_CGROUP_HIERARCHY";
439 e = getenv(var);
c78c095b
ZJS
440 }
441
442 if (!isempty(e)) {
efdb0237
LP
443 r = parse_boolean(e);
444 if (r < 0)
c78c095b 445 return log_error_errno(r, "Failed to parse $%s: %m", var);
5da38d07
TH
446 if (r > 0)
447 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
448 else
449 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
efdb0237
LP
450 }
451
8199d554
LP
452 return 0;
453}
454
455static int detect_unified_cgroup_hierarchy_from_image(const char *directory) {
456 int r;
457
75b0d8b8
ZJS
458 /* Let's inherit the mode to use from the host system, but let's take into consideration what systemd
459 * in the image actually supports. */
b4cccbc1
LP
460 r = cg_all_unified();
461 if (r < 0)
462 return log_error_errno(r, "Failed to determine whether we are in all unified mode.");
463 if (r > 0) {
a8725a06
ZJS
464 /* Unified cgroup hierarchy support was added in 230. Unfortunately the detection
465 * routine only detects 231, so we'll have a false negative here for 230. */
466 r = systemd_installation_has_version(directory, 230);
467 if (r < 0)
468 return log_error_errno(r, "Failed to determine systemd version in container: %m");
469 if (r > 0)
470 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
471 else
472 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
c22800e4 473 } else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) {
2977724b
TH
474 /* Mixed cgroup hierarchy support was added in 233 */
475 r = systemd_installation_has_version(directory, 233);
0fd9563f
ZJS
476 if (r < 0)
477 return log_error_errno(r, "Failed to determine systemd version in container: %m");
478 if (r > 0)
479 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
480 else
481 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
482 } else
5da38d07 483 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
efdb0237 484
8199d554
LP
485 log_debug("Using %s hierarchy for container.",
486 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_NONE ? "legacy" :
487 arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_SYSTEMD ? "hybrid" : "unified");
488
efdb0237
LP
489 return 0;
490}
491
8a99bd0c
ZJS
492static int parse_capability_spec(const char *spec, uint64_t *ret_mask) {
493 uint64_t mask = 0;
494 int r;
495
496 for (;;) {
497 _cleanup_free_ char *t = NULL;
498
499 r = extract_first_word(&spec, &t, ",", 0);
500 if (r < 0)
501 return log_error_errno(r, "Failed to parse capability %s.", t);
502 if (r == 0)
503 break;
504
505 if (streq(t, "help")) {
506 for (int i = 0; i < capability_list_length(); i++) {
507 const char *name;
508
509 name = capability_to_name(i);
510 if (name)
511 puts(name);
512 }
513
514 return 0; /* quit */
515 }
516
517 if (streq(t, "all"))
518 mask = (uint64_t) -1;
519 else {
520 r = capability_from_name(t);
521 if (r < 0)
522 return log_error_errno(r, "Failed to parse capability %s.", t);
523
524 mask |= 1ULL << r;
525 }
526 }
527
528 *ret_mask = mask;
529 return 1; /* continue */
530}
531
49048684 532static int parse_share_ns_env(const char *name, unsigned long ns_flag) {
0c582db0
LB
533 int r;
534
535 r = getenv_bool(name);
536 if (r == -ENXIO)
49048684 537 return 0;
0c582db0 538 if (r < 0)
49048684 539 return log_error_errno(r, "Failed to parse $%s: %m", name);
de40a303 540
0c582db0 541 arg_clone_ns_flags = (arg_clone_ns_flags & ~ns_flag) | (r > 0 ? 0 : ns_flag);
de40a303 542 arg_settings_mask |= SETTING_CLONE_NS_FLAGS;
49048684 543 return 0;
0c582db0
LB
544}
545
49048684 546static int parse_mount_settings_env(void) {
4f086aab 547 const char *e;
1099ceeb
LP
548 int r;
549
550 r = getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
49048684
ZJS
551 if (r < 0 && r != -ENXIO)
552 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP: %m");
1099ceeb
LP
553 if (r >= 0)
554 SET_FLAG(arg_mount_settings, MOUNT_APPLY_TMPFS_TMP, r > 0);
4f086aab
SU
555
556 e = getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
49048684 557 if (streq_ptr(e, "network"))
4f086aab 558 arg_mount_settings |= MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS;
4f086aab 559
49048684
ZJS
560 else if (e) {
561 r = parse_boolean(e);
562 if (r < 0)
563 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_API_VFS_WRITABLE: %m");
564
565 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_RO, r == 0);
566 SET_FLAG(arg_mount_settings, MOUNT_APPLY_APIVFS_NETNS, false);
ab8ee0f2 567 }
4f086aab 568
49048684 569 return 0;
4f086aab
SU
570}
571
49048684 572static int parse_environment(void) {
d5455d2f
LP
573 const char *e;
574 int r;
575
49048684
ZJS
576 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC);
577 if (r < 0)
578 return r;
579 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID);
580 if (r < 0)
581 return r;
582 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS);
583 if (r < 0)
584 return r;
585 r = parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
586 if (r < 0)
587 return r;
d5455d2f 588
49048684
ZJS
589 r = parse_mount_settings_env();
590 if (r < 0)
591 return r;
d5455d2f 592
489fae52
ZJS
593 /* SYSTEMD_NSPAWN_USE_CGNS=0 can be used to disable CLONE_NEWCGROUP use,
594 * even if it is supported. If not supported, it has no effect. */
de40a303 595 if (!cg_ns_supported())
489fae52 596 arg_use_cgns = false;
de40a303
LP
597 else {
598 r = getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
599 if (r < 0) {
600 if (r != -ENXIO)
49048684 601 return log_error_errno(r, "Failed to parse $SYSTEMD_NSPAWN_USE_CGNS: %m");
de40a303
LP
602
603 arg_use_cgns = true;
604 } else {
605 arg_use_cgns = r > 0;
606 arg_settings_mask |= SETTING_USE_CGNS;
607 }
608 }
d5455d2f
LP
609
610 e = getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
611 if (e)
612 arg_container_service_name = e;
613
49048684 614 return detect_unified_cgroup_hierarchy_from_environment();
d5455d2f
LP
615}
616
88213476 617static int parse_argv(int argc, char *argv[]) {
a41fe3a2 618 enum {
acbeb427
ZJS
619 ARG_VERSION = 0x100,
620 ARG_PRIVATE_NETWORK,
bc2f673e 621 ARG_UUID,
5076f0cc 622 ARG_READ_ONLY,
57fb9fb5 623 ARG_CAPABILITY,
420c7379 624 ARG_DROP_CAPABILITY,
17fe0523
LP
625 ARG_LINK_JOURNAL,
626 ARG_BIND,
f4889f65 627 ARG_BIND_RO,
06c17c39 628 ARG_TMPFS,
5a8af538
LP
629 ARG_OVERLAY,
630 ARG_OVERLAY_RO,
de40a303 631 ARG_INACCESSIBLE,
eb91eb18 632 ARG_SHARE_SYSTEM,
89f7c846 633 ARG_REGISTER,
aa28aefe 634 ARG_KEEP_UNIT,
69c79d3c 635 ARG_NETWORK_INTERFACE,
c74e630d 636 ARG_NETWORK_MACVLAN,
4bbfe7ad 637 ARG_NETWORK_IPVLAN,
ab046dde 638 ARG_NETWORK_BRIDGE,
22b28dfd 639 ARG_NETWORK_ZONE,
f6d6bad1 640 ARG_NETWORK_VETH_EXTRA,
d7bea6b6 641 ARG_NETWORK_NAMESPACE_PATH,
6afc95b7 642 ARG_PERSONALITY,
4d9f07b4 643 ARG_VOLATILE,
ec16945e 644 ARG_TEMPLATE,
f36933fe 645 ARG_PROPERTY,
6dac160c 646 ARG_PRIVATE_USERS,
c6c8f6e2 647 ARG_KILL_SIGNAL,
f757855e 648 ARG_SETTINGS,
5f932eb9 649 ARG_CHDIR,
b53ede69 650 ARG_PIVOT_ROOT,
7336138e 651 ARG_PRIVATE_USERS_CHOWN,
9c1e04d0 652 ARG_NOTIFY_READY,
4623e8e6 653 ARG_ROOT_HASH,
960e4569 654 ARG_SYSTEM_CALL_FILTER,
bf428efb 655 ARG_RLIMIT,
3a9530e5 656 ARG_HOSTNAME,
66edd963 657 ARG_NO_NEW_PRIVILEGES,
81f345df 658 ARG_OOM_SCORE_ADJUST,
d107bb7d 659 ARG_CPU_AFFINITY,
09d423e9 660 ARG_RESOLV_CONF,
1688841f 661 ARG_TIMEZONE,
de40a303
LP
662 ARG_CONSOLE,
663 ARG_PIPE,
664 ARG_OCI_BUNDLE,
bb068de0 665 ARG_NO_PAGER,
a41fe3a2
LP
666 };
667
88213476 668 static const struct option options[] = {
d7bea6b6
DP
669 { "help", no_argument, NULL, 'h' },
670 { "version", no_argument, NULL, ARG_VERSION },
671 { "directory", required_argument, NULL, 'D' },
672 { "template", required_argument, NULL, ARG_TEMPLATE },
673 { "ephemeral", no_argument, NULL, 'x' },
674 { "user", required_argument, NULL, 'u' },
675 { "private-network", no_argument, NULL, ARG_PRIVATE_NETWORK },
676 { "as-pid2", no_argument, NULL, 'a' },
677 { "boot", no_argument, NULL, 'b' },
678 { "uuid", required_argument, NULL, ARG_UUID },
679 { "read-only", no_argument, NULL, ARG_READ_ONLY },
680 { "capability", required_argument, NULL, ARG_CAPABILITY },
681 { "drop-capability", required_argument, NULL, ARG_DROP_CAPABILITY },
66edd963 682 { "no-new-privileges", required_argument, NULL, ARG_NO_NEW_PRIVILEGES },
d7bea6b6
DP
683 { "link-journal", required_argument, NULL, ARG_LINK_JOURNAL },
684 { "bind", required_argument, NULL, ARG_BIND },
685 { "bind-ro", required_argument, NULL, ARG_BIND_RO },
686 { "tmpfs", required_argument, NULL, ARG_TMPFS },
687 { "overlay", required_argument, NULL, ARG_OVERLAY },
688 { "overlay-ro", required_argument, NULL, ARG_OVERLAY_RO },
de40a303 689 { "inaccessible", required_argument, NULL, ARG_INACCESSIBLE },
d7bea6b6 690 { "machine", required_argument, NULL, 'M' },
3a9530e5 691 { "hostname", required_argument, NULL, ARG_HOSTNAME },
d7bea6b6
DP
692 { "slice", required_argument, NULL, 'S' },
693 { "setenv", required_argument, NULL, 'E' },
694 { "selinux-context", required_argument, NULL, 'Z' },
695 { "selinux-apifs-context", required_argument, NULL, 'L' },
696 { "quiet", no_argument, NULL, 'q' },
697 { "share-system", no_argument, NULL, ARG_SHARE_SYSTEM }, /* not documented */
698 { "register", required_argument, NULL, ARG_REGISTER },
699 { "keep-unit", no_argument, NULL, ARG_KEEP_UNIT },
700 { "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE },
701 { "network-macvlan", required_argument, NULL, ARG_NETWORK_MACVLAN },
702 { "network-ipvlan", required_argument, NULL, ARG_NETWORK_IPVLAN },
703 { "network-veth", no_argument, NULL, 'n' },
704 { "network-veth-extra", required_argument, NULL, ARG_NETWORK_VETH_EXTRA },
705 { "network-bridge", required_argument, NULL, ARG_NETWORK_BRIDGE },
706 { "network-zone", required_argument, NULL, ARG_NETWORK_ZONE },
707 { "network-namespace-path", required_argument, NULL, ARG_NETWORK_NAMESPACE_PATH },
708 { "personality", required_argument, NULL, ARG_PERSONALITY },
709 { "image", required_argument, NULL, 'i' },
710 { "volatile", optional_argument, NULL, ARG_VOLATILE },
711 { "port", required_argument, NULL, 'p' },
712 { "property", required_argument, NULL, ARG_PROPERTY },
713 { "private-users", optional_argument, NULL, ARG_PRIVATE_USERS },
714 { "private-users-chown", optional_argument, NULL, ARG_PRIVATE_USERS_CHOWN },
715 { "kill-signal", required_argument, NULL, ARG_KILL_SIGNAL },
716 { "settings", required_argument, NULL, ARG_SETTINGS },
717 { "chdir", required_argument, NULL, ARG_CHDIR },
718 { "pivot-root", required_argument, NULL, ARG_PIVOT_ROOT },
719 { "notify-ready", required_argument, NULL, ARG_NOTIFY_READY },
720 { "root-hash", required_argument, NULL, ARG_ROOT_HASH },
721 { "system-call-filter", required_argument, NULL, ARG_SYSTEM_CALL_FILTER },
bf428efb 722 { "rlimit", required_argument, NULL, ARG_RLIMIT },
81f345df 723 { "oom-score-adjust", required_argument, NULL, ARG_OOM_SCORE_ADJUST },
d107bb7d 724 { "cpu-affinity", required_argument, NULL, ARG_CPU_AFFINITY },
09d423e9 725 { "resolv-conf", required_argument, NULL, ARG_RESOLV_CONF },
1688841f 726 { "timezone", required_argument, NULL, ARG_TIMEZONE },
de40a303
LP
727 { "console", required_argument, NULL, ARG_CONSOLE },
728 { "pipe", no_argument, NULL, ARG_PIPE },
729 { "oci-bundle", required_argument, NULL, ARG_OCI_BUNDLE },
bb068de0 730 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
eb9da376 731 {}
88213476
LP
732 };
733
9444b1f2 734 int c, r;
a42c8b54 735 uint64_t plus = 0, minus = 0;
f757855e 736 bool mask_all_settings = false, mask_no_settings = false;
88213476
LP
737
738 assert(argc >= 0);
739 assert(argv);
740
de40a303 741 while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:P", options, NULL)) >= 0)
88213476
LP
742 switch (c) {
743
744 case 'h':
37ec0fdd 745 return help();
88213476 746
acbeb427 747 case ARG_VERSION:
3f6fd1ba 748 return version();
acbeb427 749
88213476 750 case 'D':
0f03c2a4 751 r = parse_path_argument_and_warn(optarg, false, &arg_directory);
ec16945e 752 if (r < 0)
0f03c2a4 753 return r;
de40a303
LP
754
755 arg_settings_mask |= SETTING_DIRECTORY;
ec16945e
LP
756 break;
757
758 case ARG_TEMPLATE:
0f03c2a4 759 r = parse_path_argument_and_warn(optarg, false, &arg_template);
ec16945e 760 if (r < 0)
0f03c2a4 761 return r;
de40a303
LP
762
763 arg_settings_mask |= SETTING_DIRECTORY;
88213476
LP
764 break;
765
1b9e5b12 766 case 'i':
0f03c2a4 767 r = parse_path_argument_and_warn(optarg, false, &arg_image);
ec16945e 768 if (r < 0)
0f03c2a4 769 return r;
de40a303
LP
770
771 arg_settings_mask |= SETTING_DIRECTORY;
772 break;
773
774 case ARG_OCI_BUNDLE:
775 r = parse_path_argument_and_warn(optarg, false, &arg_oci_bundle);
776 if (r < 0)
777 return r;
778
ec16945e
LP
779 break;
780
781 case 'x':
782 arg_ephemeral = true;
a2f577fc 783 arg_settings_mask |= SETTING_EPHEMERAL;
1b9e5b12
LP
784 break;
785
687d0825 786 case 'u':
2fc09a9c
DM
787 r = free_and_strdup(&arg_user, optarg);
788 if (r < 0)
7027ff61 789 return log_oom();
687d0825 790
f757855e 791 arg_settings_mask |= SETTING_USER;
687d0825
MV
792 break;
793
22b28dfd
LP
794 case ARG_NETWORK_ZONE: {
795 char *j;
796
b910cc72 797 j = strjoin("vz-", optarg);
22b28dfd
LP
798 if (!j)
799 return log_oom();
800
801 if (!ifname_valid(j)) {
802 log_error("Network zone name not valid: %s", j);
803 free(j);
804 return -EINVAL;
805 }
806
df1fac6d 807 free_and_replace(arg_network_zone, j);
22b28dfd
LP
808
809 arg_network_veth = true;
810 arg_private_network = true;
811 arg_settings_mask |= SETTING_NETWORK;
812 break;
813 }
814
ab046dde 815 case ARG_NETWORK_BRIDGE:
ef76dff2 816
baaa35ad
ZJS
817 if (!ifname_valid(optarg))
818 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
819 "Bridge interface name not valid: %s", optarg);
ef76dff2 820
f757855e
LP
821 r = free_and_strdup(&arg_network_bridge, optarg);
822 if (r < 0)
823 return log_oom();
ab046dde 824
4831981d 825 _fallthrough_;
0dfaa006 826 case 'n':
69c79d3c
LP
827 arg_network_veth = true;
828 arg_private_network = true;
f757855e 829 arg_settings_mask |= SETTING_NETWORK;
69c79d3c
LP
830 break;
831
f6d6bad1
LP
832 case ARG_NETWORK_VETH_EXTRA:
833 r = veth_extra_parse(&arg_network_veth_extra, optarg);
834 if (r < 0)
835 return log_error_errno(r, "Failed to parse --network-veth-extra= parameter: %s", optarg);
836
837 arg_private_network = true;
838 arg_settings_mask |= SETTING_NETWORK;
839 break;
840
aa28aefe 841 case ARG_NETWORK_INTERFACE:
baaa35ad
ZJS
842 if (!ifname_valid(optarg))
843 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
844 "Network interface name not valid: %s", optarg);
ef76dff2 845
b390f178
DDM
846 r = test_network_interface_initialized(optarg);
847 if (r < 0)
848 return r;
849
c74e630d
LP
850 if (strv_extend(&arg_network_interfaces, optarg) < 0)
851 return log_oom();
852
853 arg_private_network = true;
f757855e 854 arg_settings_mask |= SETTING_NETWORK;
c74e630d
LP
855 break;
856
857 case ARG_NETWORK_MACVLAN:
ef76dff2 858
baaa35ad
ZJS
859 if (!ifname_valid(optarg))
860 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
861 "MACVLAN network interface name not valid: %s", optarg);
ef76dff2 862
b390f178
DDM
863 r = test_network_interface_initialized(optarg);
864 if (r < 0)
865 return r;
866
c74e630d 867 if (strv_extend(&arg_network_macvlan, optarg) < 0)
aa28aefe
LP
868 return log_oom();
869
4bbfe7ad 870 arg_private_network = true;
f757855e 871 arg_settings_mask |= SETTING_NETWORK;
4bbfe7ad
TG
872 break;
873
874 case ARG_NETWORK_IPVLAN:
ef76dff2 875
baaa35ad
ZJS
876 if (!ifname_valid(optarg))
877 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
878 "IPVLAN network interface name not valid: %s", optarg);
ef76dff2 879
b390f178
DDM
880 r = test_network_interface_initialized(optarg);
881 if (r < 0)
882 return r;
883
4bbfe7ad
TG
884 if (strv_extend(&arg_network_ipvlan, optarg) < 0)
885 return log_oom();
886
4831981d 887 _fallthrough_;
ff01d048
LP
888 case ARG_PRIVATE_NETWORK:
889 arg_private_network = true;
f757855e 890 arg_settings_mask |= SETTING_NETWORK;
a41fe3a2
LP
891 break;
892
d7bea6b6
DP
893 case ARG_NETWORK_NAMESPACE_PATH:
894 r = parse_path_argument_and_warn(optarg, false, &arg_network_namespace_path);
895 if (r < 0)
896 return r;
897
de40a303 898 arg_settings_mask |= SETTING_NETWORK;
d7bea6b6
DP
899 break;
900
0f0dbc46 901 case 'b':
baaa35ad
ZJS
902 if (arg_start_mode == START_PID2)
903 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
904 "--boot and --as-pid2 may not be combined.");
7732f92b
LP
905
906 arg_start_mode = START_BOOT;
907 arg_settings_mask |= SETTING_START_MODE;
908 break;
909
910 case 'a':
baaa35ad
ZJS
911 if (arg_start_mode == START_BOOT)
912 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
913 "--boot and --as-pid2 may not be combined.");
7732f92b
LP
914
915 arg_start_mode = START_PID2;
916 arg_settings_mask |= SETTING_START_MODE;
0f0dbc46
LP
917 break;
918
144f0fc0 919 case ARG_UUID:
9444b1f2 920 r = sd_id128_from_string(optarg, &arg_uuid);
317feb4d
LP
921 if (r < 0)
922 return log_error_errno(r, "Invalid UUID: %s", optarg);
923
baaa35ad
ZJS
924 if (sd_id128_is_null(arg_uuid))
925 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
926 "Machine UUID may not be all zeroes.");
f757855e
LP
927
928 arg_settings_mask |= SETTING_MACHINE_ID;
9444b1f2 929 break;
aa96c6cb 930
43c3fb46
LP
931 case 'S': {
932 _cleanup_free_ char *mangled = NULL;
933
934 r = unit_name_mangle_with_suffix(optarg, NULL, UNIT_NAME_MANGLE_WARN, ".slice", &mangled);
de40a303
LP
935 if (r < 0)
936 return log_oom();
937
43c3fb46 938 free_and_replace(arg_slice, mangled);
de40a303 939 arg_settings_mask |= SETTING_SLICE;
144f0fc0 940 break;
43c3fb46 941 }
144f0fc0 942
7027ff61 943 case 'M':
c1521918 944 if (isempty(optarg))
97b11eed 945 arg_machine = mfree(arg_machine);
c1521918 946 else {
baaa35ad
ZJS
947 if (!machine_name_is_valid(optarg))
948 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
949 "Invalid machine name: %s", optarg);
7027ff61 950
0c3c4284
LP
951 r = free_and_strdup(&arg_machine, optarg);
952 if (r < 0)
eb91eb18 953 return log_oom();
eb91eb18 954 }
9ce6d1b3 955 break;
7027ff61 956
3a9530e5
LP
957 case ARG_HOSTNAME:
958 if (isempty(optarg))
959 arg_hostname = mfree(arg_hostname);
960 else {
baaa35ad
ZJS
961 if (!hostname_is_valid(optarg, false))
962 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
963 "Invalid hostname: %s", optarg);
3a9530e5
LP
964
965 r = free_and_strdup(&arg_hostname, optarg);
966 if (r < 0)
967 return log_oom();
968 }
969
970 arg_settings_mask |= SETTING_HOSTNAME;
971 break;
972
82adf6af
LP
973 case 'Z':
974 arg_selinux_context = optarg;
a8828ed9
DW
975 break;
976
82adf6af
LP
977 case 'L':
978 arg_selinux_apifs_context = optarg;
a8828ed9
DW
979 break;
980
bc2f673e
LP
981 case ARG_READ_ONLY:
982 arg_read_only = true;
f757855e 983 arg_settings_mask |= SETTING_READ_ONLY;
bc2f673e
LP
984 break;
985
420c7379
LP
986 case ARG_CAPABILITY:
987 case ARG_DROP_CAPABILITY: {
8a99bd0c
ZJS
988 uint64_t m;
989 r = parse_capability_spec(optarg, &m);
990 if (r <= 0)
991 return r;
5076f0cc 992
8a99bd0c
ZJS
993 if (c == ARG_CAPABILITY)
994 plus |= m;
995 else
996 minus |= m;
f757855e 997 arg_settings_mask |= SETTING_CAPABILITY;
5076f0cc
LP
998 break;
999 }
66edd963
LP
1000 case ARG_NO_NEW_PRIVILEGES:
1001 r = parse_boolean(optarg);
1002 if (r < 0)
1003 return log_error_errno(r, "Failed to parse --no-new-privileges= argument: %s", optarg);
1004
1005 arg_no_new_privileges = r;
1006 arg_settings_mask |= SETTING_NO_NEW_PRIVILEGES;
1007 break;
1008
57fb9fb5
LP
1009 case 'j':
1010 arg_link_journal = LINK_GUEST;
574edc90 1011 arg_link_journal_try = true;
4e1d6aa9 1012 arg_settings_mask |= SETTING_LINK_JOURNAL;
57fb9fb5
LP
1013 break;
1014
1015 case ARG_LINK_JOURNAL:
4e1d6aa9 1016 r = parse_link_journal(optarg, &arg_link_journal, &arg_link_journal_try);
c6147113
LP
1017 if (r < 0)
1018 return log_error_errno(r, "Failed to parse link journal mode %s", optarg);
57fb9fb5 1019
4e1d6aa9 1020 arg_settings_mask |= SETTING_LINK_JOURNAL;
57fb9fb5
LP
1021 break;
1022
17fe0523 1023 case ARG_BIND:
f757855e
LP
1024 case ARG_BIND_RO:
1025 r = bind_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_BIND_RO);
1026 if (r < 0)
1027 return log_error_errno(r, "Failed to parse --bind(-ro)= argument %s: %m", optarg);
17fe0523 1028
f757855e 1029 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
17fe0523 1030 break;
06c17c39 1031
f757855e
LP
1032 case ARG_TMPFS:
1033 r = tmpfs_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg);
1034 if (r < 0)
1035 return log_error_errno(r, "Failed to parse --tmpfs= argument %s: %m", optarg);
5a8af538 1036
f757855e 1037 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
5a8af538 1038 break;
5a8af538
LP
1039
1040 case ARG_OVERLAY:
ad85779a
LP
1041 case ARG_OVERLAY_RO:
1042 r = overlay_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg, c == ARG_OVERLAY_RO);
1043 if (r == -EADDRNOTAVAIL)
1044 return log_error_errno(r, "--overlay(-ro)= needs at least two colon-separated directories specified.");
1045 if (r < 0)
1046 return log_error_errno(r, "Failed to parse --overlay(-ro)= argument %s: %m", optarg);
06c17c39 1047
f757855e 1048 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
06c17c39 1049 break;
06c17c39 1050
de40a303
LP
1051 case ARG_INACCESSIBLE:
1052 r = inaccessible_mount_parse(&arg_custom_mounts, &arg_n_custom_mounts, optarg);
1053 if (r < 0)
1054 return log_error_errno(r, "Failed to parse --inaccessible= argument %s: %m", optarg);
1055
1056 arg_settings_mask |= SETTING_CUSTOM_MOUNTS;
1057 break;
1058
a5f1cb3b 1059 case 'E': {
f4889f65
LP
1060 char **n;
1061
baaa35ad
ZJS
1062 if (!env_assignment_is_valid(optarg))
1063 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1064 "Environment variable assignment '%s' is not valid.", optarg);
f4889f65
LP
1065
1066 n = strv_env_set(arg_setenv, optarg);
1067 if (!n)
1068 return log_oom();
1069
130d3d22 1070 strv_free_and_replace(arg_setenv, n);
f757855e 1071 arg_settings_mask |= SETTING_ENVIRONMENT;
f4889f65
LP
1072 break;
1073 }
1074
284c0b91
LP
1075 case 'q':
1076 arg_quiet = true;
1077 break;
1078
8a96d94e 1079 case ARG_SHARE_SYSTEM:
a6b5216c 1080 /* We don't officially support this anymore, except for compat reasons. People should use the
0c582db0 1081 * $SYSTEMD_NSPAWN_SHARE_* environment variables instead. */
63d1c29f 1082 log_warning("Please do not use --share-system anymore, use $SYSTEMD_NSPAWN_SHARE_* instead.");
0c582db0 1083 arg_clone_ns_flags = 0;
8a96d94e
LP
1084 break;
1085
eb91eb18
LP
1086 case ARG_REGISTER:
1087 r = parse_boolean(optarg);
1088 if (r < 0) {
1089 log_error("Failed to parse --register= argument: %s", optarg);
1090 return r;
1091 }
1092
1093 arg_register = r;
1094 break;
1095
89f7c846
LP
1096 case ARG_KEEP_UNIT:
1097 arg_keep_unit = true;
1098 break;
1099
6afc95b7
LP
1100 case ARG_PERSONALITY:
1101
ac45f971 1102 arg_personality = personality_from_string(optarg);
baaa35ad
ZJS
1103 if (arg_personality == PERSONALITY_INVALID)
1104 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1105 "Unknown or unsupported personality '%s'.", optarg);
6afc95b7 1106
f757855e 1107 arg_settings_mask |= SETTING_PERSONALITY;
6afc95b7
LP
1108 break;
1109
4d9f07b4
LP
1110 case ARG_VOLATILE:
1111
1112 if (!optarg)
f757855e 1113 arg_volatile_mode = VOLATILE_YES;
5c828e66
LP
1114 else if (streq(optarg, "help")) {
1115 DUMP_STRING_TABLE(volatile_mode, VolatileMode, _VOLATILE_MODE_MAX);
1116 return 0;
1117 } else {
f757855e 1118 VolatileMode m;
4d9f07b4 1119
f757855e 1120 m = volatile_mode_from_string(optarg);
baaa35ad
ZJS
1121 if (m < 0)
1122 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1123 "Failed to parse --volatile= argument: %s", optarg);
1124 else
f757855e 1125 arg_volatile_mode = m;
6d0b55c2
LP
1126 }
1127
f757855e
LP
1128 arg_settings_mask |= SETTING_VOLATILE_MODE;
1129 break;
6d0b55c2 1130
f757855e
LP
1131 case 'p':
1132 r = expose_port_parse(&arg_expose_ports, optarg);
1133 if (r == -EEXIST)
1134 return log_error_errno(r, "Duplicate port specification: %s", optarg);
1135 if (r < 0)
1136 return log_error_errno(r, "Failed to parse host port %s: %m", optarg);
6d0b55c2 1137
f757855e 1138 arg_settings_mask |= SETTING_EXPOSE_PORTS;
6d0b55c2 1139 break;
6d0b55c2 1140
f36933fe
LP
1141 case ARG_PROPERTY:
1142 if (strv_extend(&arg_property, optarg) < 0)
1143 return log_oom();
1144
1145 break;
1146
ae209204
ZJS
1147 case ARG_PRIVATE_USERS: {
1148 int boolean = -1;
0de7acce 1149
ae209204
ZJS
1150 if (!optarg)
1151 boolean = true;
1152 else if (!in_charset(optarg, DIGITS))
1153 /* do *not* parse numbers as booleans */
1154 boolean = parse_boolean(optarg);
1155
1156 if (boolean == false) {
0de7acce
LP
1157 /* no: User namespacing off */
1158 arg_userns_mode = USER_NAMESPACE_NO;
1159 arg_uid_shift = UID_INVALID;
1160 arg_uid_range = UINT32_C(0x10000);
ae209204 1161 } else if (boolean == true) {
0de7acce
LP
1162 /* yes: User namespacing on, UID range is read from root dir */
1163 arg_userns_mode = USER_NAMESPACE_FIXED;
1164 arg_uid_shift = UID_INVALID;
1165 arg_uid_range = UINT32_C(0x10000);
1166 } else if (streq(optarg, "pick")) {
1167 /* pick: User namespacing on, UID range is picked randomly */
1168 arg_userns_mode = USER_NAMESPACE_PICK;
1169 arg_uid_shift = UID_INVALID;
1170 arg_uid_range = UINT32_C(0x10000);
1171 } else {
6c2058b3 1172 _cleanup_free_ char *buffer = NULL;
6dac160c
LP
1173 const char *range, *shift;
1174
0de7acce
LP
1175 /* anything else: User namespacing on, UID range is explicitly configured */
1176
6dac160c
LP
1177 range = strchr(optarg, ':');
1178 if (range) {
6c2058b3
ZJS
1179 buffer = strndup(optarg, range - optarg);
1180 if (!buffer)
1181 return log_oom();
1182 shift = buffer;
6dac160c
LP
1183
1184 range++;
bfd292ec
ZJS
1185 r = safe_atou32(range, &arg_uid_range);
1186 if (r < 0)
be715731 1187 return log_error_errno(r, "Failed to parse UID range \"%s\": %m", range);
6dac160c
LP
1188 } else
1189 shift = optarg;
1190
be715731
ZJS
1191 r = parse_uid(shift, &arg_uid_shift);
1192 if (r < 0)
1193 return log_error_errno(r, "Failed to parse UID \"%s\": %m", optarg);
0de7acce
LP
1194
1195 arg_userns_mode = USER_NAMESPACE_FIXED;
6dac160c
LP
1196 }
1197
baaa35ad
ZJS
1198 if (arg_uid_range <= 0)
1199 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1200 "UID range cannot be 0.");
be715731 1201
0de7acce 1202 arg_settings_mask |= SETTING_USERNS;
6dac160c 1203 break;
ae209204 1204 }
6dac160c 1205
0de7acce 1206 case 'U':
ccabee0d
LP
1207 if (userns_supported()) {
1208 arg_userns_mode = USER_NAMESPACE_PICK;
1209 arg_uid_shift = UID_INVALID;
1210 arg_uid_range = UINT32_C(0x10000);
1211
1212 arg_settings_mask |= SETTING_USERNS;
6dac160c
LP
1213 }
1214
7336138e
LP
1215 break;
1216
0de7acce 1217 case ARG_PRIVATE_USERS_CHOWN:
19aac838 1218 arg_userns_chown = true;
0de7acce
LP
1219
1220 arg_settings_mask |= SETTING_USERNS;
6dac160c
LP
1221 break;
1222
c6c8f6e2 1223 case ARG_KILL_SIGNAL:
5c828e66
LP
1224 if (streq(optarg, "help")) {
1225 DUMP_STRING_TABLE(signal, int, _NSIG);
1226 return 0;
1227 }
1228
29a3db75 1229 arg_kill_signal = signal_from_string(optarg);
baaa35ad
ZJS
1230 if (arg_kill_signal < 0)
1231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1232 "Cannot parse signal: %s", optarg);
c6c8f6e2 1233
f757855e
LP
1234 arg_settings_mask |= SETTING_KILL_SIGNAL;
1235 break;
1236
1237 case ARG_SETTINGS:
1238
1239 /* no → do not read files
1240 * yes → read files, do not override cmdline, trust only subset
1241 * override → read files, override cmdline, trust only subset
1242 * trusted → read files, do not override cmdline, trust all
1243 */
1244
1245 r = parse_boolean(optarg);
1246 if (r < 0) {
1247 if (streq(optarg, "trusted")) {
1248 mask_all_settings = false;
1249 mask_no_settings = false;
1250 arg_settings_trusted = true;
1251
1252 } else if (streq(optarg, "override")) {
1253 mask_all_settings = false;
1254 mask_no_settings = true;
1255 arg_settings_trusted = -1;
1256 } else
1257 return log_error_errno(r, "Failed to parse --settings= argument: %s", optarg);
1258 } else if (r > 0) {
1259 /* yes */
1260 mask_all_settings = false;
1261 mask_no_settings = false;
1262 arg_settings_trusted = -1;
1263 } else {
1264 /* no */
1265 mask_all_settings = true;
1266 mask_no_settings = false;
1267 arg_settings_trusted = false;
1268 }
1269
c6c8f6e2
LP
1270 break;
1271
5f932eb9 1272 case ARG_CHDIR:
baaa35ad
ZJS
1273 if (!path_is_absolute(optarg))
1274 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1275 "Working directory %s is not an absolute path.", optarg);
5f932eb9
LP
1276
1277 r = free_and_strdup(&arg_chdir, optarg);
1278 if (r < 0)
1279 return log_oom();
1280
1281 arg_settings_mask |= SETTING_WORKING_DIRECTORY;
1282 break;
1283
b53ede69
PW
1284 case ARG_PIVOT_ROOT:
1285 r = pivot_root_parse(&arg_pivot_root_new, &arg_pivot_root_old, optarg);
1286 if (r < 0)
1287 return log_error_errno(r, "Failed to parse --pivot-root= argument %s: %m", optarg);
1288
1289 arg_settings_mask |= SETTING_PIVOT_ROOT;
1290 break;
1291
9c1e04d0
AP
1292 case ARG_NOTIFY_READY:
1293 r = parse_boolean(optarg);
baaa35ad
ZJS
1294 if (r < 0)
1295 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1296 "%s is not a valid notify mode. Valid modes are: yes, no, and ready.", optarg);
9c1e04d0
AP
1297 arg_notify_ready = r;
1298 arg_settings_mask |= SETTING_NOTIFY_READY;
1299 break;
1300
4623e8e6
LP
1301 case ARG_ROOT_HASH: {
1302 void *k;
1303 size_t l;
1304
1305 r = unhexmem(optarg, strlen(optarg), &k, &l);
1306 if (r < 0)
1307 return log_error_errno(r, "Failed to parse root hash: %s", optarg);
1308 if (l < sizeof(sd_id128_t)) {
4623e8e6 1309 free(k);
c6147113 1310 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root hash must be at least 128bit long: %s", optarg);
4623e8e6
LP
1311 }
1312
1313 free(arg_root_hash);
1314 arg_root_hash = k;
1315 arg_root_hash_size = l;
1316 break;
1317 }
1318
960e4569
LP
1319 case ARG_SYSTEM_CALL_FILTER: {
1320 bool negative;
1321 const char *items;
1322
1323 negative = optarg[0] == '~';
1324 items = negative ? optarg + 1 : optarg;
1325
1326 for (;;) {
1327 _cleanup_free_ char *word = NULL;
1328
1329 r = extract_first_word(&items, &word, NULL, 0);
1330 if (r == 0)
1331 break;
1332 if (r == -ENOMEM)
1333 return log_oom();
1334 if (r < 0)
1335 return log_error_errno(r, "Failed to parse system call filter: %m");
1336
1337 if (negative)
1338 r = strv_extend(&arg_syscall_blacklist, word);
1339 else
1340 r = strv_extend(&arg_syscall_whitelist, word);
1341 if (r < 0)
1342 return log_oom();
1343 }
1344
1345 arg_settings_mask |= SETTING_SYSCALL_FILTER;
1346 break;
1347 }
1348
bf428efb
LP
1349 case ARG_RLIMIT: {
1350 const char *eq;
622ecfa8 1351 _cleanup_free_ char *name = NULL;
bf428efb
LP
1352 int rl;
1353
5c828e66
LP
1354 if (streq(optarg, "help")) {
1355 DUMP_STRING_TABLE(rlimit, int, _RLIMIT_MAX);
1356 return 0;
1357 }
1358
bf428efb 1359 eq = strchr(optarg, '=');
baaa35ad
ZJS
1360 if (!eq)
1361 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1362 "--rlimit= expects an '=' assignment.");
bf428efb
LP
1363
1364 name = strndup(optarg, eq - optarg);
1365 if (!name)
1366 return log_oom();
1367
1368 rl = rlimit_from_string_harder(name);
baaa35ad
ZJS
1369 if (rl < 0)
1370 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1371 "Unknown resource limit: %s", name);
bf428efb
LP
1372
1373 if (!arg_rlimit[rl]) {
1374 arg_rlimit[rl] = new0(struct rlimit, 1);
1375 if (!arg_rlimit[rl])
1376 return log_oom();
1377 }
1378
1379 r = rlimit_parse(rl, eq + 1, arg_rlimit[rl]);
1380 if (r < 0)
1381 return log_error_errno(r, "Failed to parse resource limit: %s", eq + 1);
1382
1383 arg_settings_mask |= SETTING_RLIMIT_FIRST << rl;
1384 break;
1385 }
1386
81f345df
LP
1387 case ARG_OOM_SCORE_ADJUST:
1388 r = parse_oom_score_adjust(optarg, &arg_oom_score_adjust);
1389 if (r < 0)
1390 return log_error_errno(r, "Failed to parse --oom-score-adjust= parameter: %s", optarg);
1391
1392 arg_oom_score_adjust_set = true;
1393 arg_settings_mask |= SETTING_OOM_SCORE_ADJUST;
1394 break;
1395
d107bb7d 1396 case ARG_CPU_AFFINITY: {
0985c7c4 1397 CPUSet cpuset;
d107bb7d
LP
1398
1399 r = parse_cpu_set(optarg, &cpuset);
1400 if (r < 0)
0985c7c4 1401 return log_error_errno(r, "Failed to parse CPU affinity mask %s: %m", optarg);
d107bb7d 1402
0985c7c4
ZJS
1403 cpu_set_reset(&arg_cpu_set);
1404 arg_cpu_set = cpuset;
d107bb7d
LP
1405 arg_settings_mask |= SETTING_CPU_AFFINITY;
1406 break;
1407 }
1408
09d423e9
LP
1409 case ARG_RESOLV_CONF:
1410 if (streq(optarg, "help")) {
1411 DUMP_STRING_TABLE(resolv_conf_mode, ResolvConfMode, _RESOLV_CONF_MODE_MAX);
1412 return 0;
1413 }
1414
1415 arg_resolv_conf = resolv_conf_mode_from_string(optarg);
baaa35ad
ZJS
1416 if (arg_resolv_conf < 0)
1417 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1418 "Failed to parse /etc/resolv.conf mode: %s", optarg);
09d423e9
LP
1419
1420 arg_settings_mask |= SETTING_RESOLV_CONF;
1421 break;
1422
1688841f
LP
1423 case ARG_TIMEZONE:
1424 if (streq(optarg, "help")) {
1425 DUMP_STRING_TABLE(timezone_mode, TimezoneMode, _TIMEZONE_MODE_MAX);
1426 return 0;
1427 }
1428
1429 arg_timezone = timezone_mode_from_string(optarg);
baaa35ad
ZJS
1430 if (arg_timezone < 0)
1431 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1432 "Failed to parse /etc/localtime mode: %s", optarg);
1688841f
LP
1433
1434 arg_settings_mask |= SETTING_TIMEZONE;
1435 break;
1436
de40a303 1437 case ARG_CONSOLE:
dce66ffe
ZJS
1438 r = handle_arg_console(optarg);
1439 if (r <= 0)
1440 return r;
de40a303
LP
1441 break;
1442
1443 case 'P':
1444 case ARG_PIPE:
dce66ffe
ZJS
1445 r = handle_arg_console("pipe");
1446 if (r <= 0)
1447 return r;
de40a303
LP
1448 break;
1449
bb068de0
ZJS
1450 case ARG_NO_PAGER:
1451 arg_pager_flags |= PAGER_DISABLE;
1452 break;
1453
88213476
LP
1454 case '?':
1455 return -EINVAL;
1456
1457 default:
eb9da376 1458 assert_not_reached("Unhandled option");
88213476 1459 }
88213476 1460
60f1ec13
LP
1461 if (argc > optind) {
1462 strv_free(arg_parameters);
1463 arg_parameters = strv_copy(argv + optind);
1464 if (!arg_parameters)
1465 return log_oom();
d7bea6b6 1466
60f1ec13
LP
1467 arg_settings_mask |= SETTING_START_MODE;
1468 }
1469
1470 if (arg_ephemeral && arg_template && !arg_directory)
1471 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1472 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1473 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1474 * --directory=". */
1475 arg_directory = TAKE_PTR(arg_template);
1476
bd4b15f2 1477 arg_caps_retain = (arg_caps_retain | plus | (arg_private_network ? UINT64_C(1) << CAP_NET_ADMIN : 0)) & ~minus;
60f1ec13 1478
de40a303 1479 /* Make sure to parse environment before we reset the settings mask below */
49048684
ZJS
1480 r = parse_environment();
1481 if (r < 0)
1482 return r;
de40a303 1483
60f1ec13
LP
1484 /* Load all settings from .nspawn files */
1485 if (mask_no_settings)
1486 arg_settings_mask = 0;
1487
1488 /* Don't load any settings from .nspawn files */
1489 if (mask_all_settings)
1490 arg_settings_mask = _SETTINGS_MASK_ALL;
1491
1492 return 1;
1493}
1494
1495static int verify_arguments(void) {
1496 int r;
a6b5216c 1497
75b0d8b8
ZJS
1498 if (arg_start_mode == START_PID2 && arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
1499 /* If we are running the stub init in the container, we don't need to look at what the init
1500 * in the container supports, because we are not using it. Let's immediately pick the right
1501 * setting based on the host system configuration.
1502 *
1503 * We only do this, if the user didn't use an environment variable to override the detection.
1504 */
1505
1506 r = cg_all_unified();
1507 if (r < 0)
1508 return log_error_errno(r, "Failed to determine whether we are in all unified mode.");
1509 if (r > 0)
1510 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_ALL;
1511 else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0)
1512 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_SYSTEMD;
1513 else
1514 arg_unified_cgroup_hierarchy = CGROUP_UNIFIED_NONE;
1515 }
1516
4f086aab
SU
1517 if (arg_userns_mode != USER_NAMESPACE_NO)
1518 arg_mount_settings |= MOUNT_USE_USERNS;
1519
1520 if (arg_private_network)
1521 arg_mount_settings |= MOUNT_APPLY_APIVFS_NETNS;
1522
48a8d337
LB
1523 if (!(arg_clone_ns_flags & CLONE_NEWPID) ||
1524 !(arg_clone_ns_flags & CLONE_NEWUTS)) {
eb91eb18 1525 arg_register = false;
baaa35ad 1526 if (arg_start_mode != START_PID1)
60f1ec13 1527 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--boot cannot be used without namespacing.");
0c582db0 1528 }
eb91eb18 1529
0de7acce 1530 if (arg_userns_mode == USER_NAMESPACE_PICK)
0e7ac751
LP
1531 arg_userns_chown = true;
1532
60f1ec13
LP
1533 if (arg_start_mode == START_BOOT && arg_kill_signal <= 0)
1534 arg_kill_signal = SIGRTMIN+3;
1535
e5a4bb0d
LP
1536 if (arg_volatile_mode != VOLATILE_NO) /* Make sure all file systems contained in the image are mounted read-only if we are in volatile mode */
1537 arg_read_only = true;
1538
2436ea76
DDM
1539 if (has_custom_root_mount(arg_custom_mounts, arg_n_custom_mounts))
1540 arg_read_only = true;
1541
baaa35ad 1542 if (arg_keep_unit && arg_register && cg_pid_get_owner_uid(0, NULL) >= 0)
8d9c2bca
AJ
1543 /* Save the user from accidentally registering either user-$SESSION.scope or user@.service.
1544 * The latter is not technically a user session, but we don't need to labour the point. */
60f1ec13 1545 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--keep-unit --register=yes may not be used when invoked from a user session.");
89f7c846 1546
baaa35ad 1547 if (arg_directory && arg_image)
60f1ec13 1548 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--directory= and --image= may not be combined.");
1b9e5b12 1549
baaa35ad 1550 if (arg_template && arg_image)
60f1ec13 1551 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= and --image= may not be combined.");
8cd328d8 1552
baaa35ad 1553 if (arg_template && !(arg_directory || arg_machine))
60f1ec13 1554 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--template= needs --directory= or --machine=.");
ec16945e 1555
baaa35ad 1556 if (arg_ephemeral && arg_template)
60f1ec13 1557 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --template= may not be combined.");
ec16945e 1558
baaa35ad 1559 if (arg_ephemeral && !IN_SET(arg_link_journal, LINK_NO, LINK_AUTO))
60f1ec13 1560 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ephemeral and --link-journal= may not be combined.");
df9a75e4 1561
baaa35ad 1562 if (arg_userns_mode != USER_NAMESPACE_NO && !userns_supported())
60f1ec13 1563 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--private-users= is not supported, kernel compiled without user namespace support.");
7336138e 1564
baaa35ad 1565 if (arg_userns_chown && arg_read_only)
de40a303
LP
1566 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1567 "--read-only and --private-users-chown may not be combined.");
f757855e 1568
e5a4bb0d
LP
1569 /* We don't support --private-users-chown together with any of the volatile modes since we couldn't
1570 * change the read-only part of the tree (i.e. /usr) anyway, or because it would trigger a massive
5238e957 1571 * copy-up (in case of overlay) making the entire exercise pointless. */
e5a4bb0d
LP
1572 if (arg_userns_chown && arg_volatile_mode != VOLATILE_NO)
1573 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--volatile= and --private-users-chown may not be combined.");
1574
679ecd36
SZ
1575 /* If --network-namespace-path is given with any other network-related option (except --private-network),
1576 * we need to error out, to avoid conflicts between different network options. */
60f1ec13
LP
1577 if (arg_network_namespace_path &&
1578 (arg_network_interfaces || arg_network_macvlan ||
1579 arg_network_ipvlan || arg_network_veth_extra ||
1580 arg_network_bridge || arg_network_zone ||
679ecd36 1581 arg_network_veth))
de40a303 1582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--network-namespace-path= cannot be combined with other network options.");
86c0dd4a 1583
60f1ec13 1584 if (arg_network_bridge && arg_network_zone)
de40a303
LP
1585 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1586 "--network-bridge= and --network-zone= may not be combined.");
f757855e 1587
baaa35ad 1588 if (arg_userns_mode != USER_NAMESPACE_NO && (arg_mount_settings & MOUNT_APPLY_APIVFS_NETNS) && !arg_private_network)
60f1ec13 1589 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
4f086aab 1590
baaa35ad 1591 if (arg_userns_mode != USER_NAMESPACE_NO && !(arg_mount_settings & MOUNT_APPLY_APIVFS_RO))
60f1ec13 1592 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot combine --private-users with read-write mounts.");
f757855e 1593
baaa35ad 1594 if (arg_expose_ports && !arg_private_network)
60f1ec13 1595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --port= without private networking.");
6d0b55c2 1596
349cc4a5 1597#if ! HAVE_LIBIPTC
baaa35ad 1598 if (arg_expose_ports)
60f1ec13 1599 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--port= is not supported, compiled without libiptc support.");
1c1ea217
EV
1600#endif
1601
60f1ec13
LP
1602 r = custom_mount_check_all();
1603 if (r < 0)
1604 return r;
c6c8f6e2 1605
f757855e 1606 return 0;
88213476
LP
1607}
1608
03cfe0d5
LP
1609static int userns_lchown(const char *p, uid_t uid, gid_t gid) {
1610 assert(p);
1611
0de7acce 1612 if (arg_userns_mode == USER_NAMESPACE_NO)
03cfe0d5
LP
1613 return 0;
1614
1615 if (uid == UID_INVALID && gid == GID_INVALID)
1616 return 0;
1617
1618 if (uid != UID_INVALID) {
1619 uid += arg_uid_shift;
1620
1621 if (uid < arg_uid_shift || uid >= arg_uid_shift + arg_uid_range)
1622 return -EOVERFLOW;
1623 }
1624
1625 if (gid != GID_INVALID) {
1626 gid += (gid_t) arg_uid_shift;
1627
1628 if (gid < (gid_t) arg_uid_shift || gid >= (gid_t) (arg_uid_shift + arg_uid_range))
1629 return -EOVERFLOW;
1630 }
1631
1632 if (lchown(p, uid, gid) < 0)
1633 return -errno;
b12afc8c
LP
1634
1635 return 0;
1636}
1637
03cfe0d5
LP
1638static int userns_mkdir(const char *root, const char *path, mode_t mode, uid_t uid, gid_t gid) {
1639 const char *q;
dae8b82e 1640 int r;
03cfe0d5
LP
1641
1642 q = prefix_roota(root, path);
dae8b82e
ZJS
1643 r = mkdir_errno_wrapper(q, mode);
1644 if (r == -EEXIST)
1645 return 0;
1646 if (r < 0)
1647 return r;
03cfe0d5
LP
1648
1649 return userns_lchown(q, uid, gid);
1650}
1651
1688841f 1652static const char *timezone_from_path(const char *path) {
da9fc98d
LP
1653 return PATH_STARTSWITH_SET(
1654 path,
1655 "../usr/share/zoneinfo/",
1656 "/usr/share/zoneinfo/");
1688841f
LP
1657}
1658
83205269
LP
1659static bool etc_writable(void) {
1660 return !arg_read_only || IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY);
1661}
1662
e58a1277 1663static int setup_timezone(const char *dest) {
1688841f
LP
1664 _cleanup_free_ char *p = NULL, *etc = NULL;
1665 const char *where, *check;
1666 TimezoneMode m;
d4036145 1667 int r;
f8440af5 1668
e58a1277
LP
1669 assert(dest);
1670
1688841f 1671 if (IN_SET(arg_timezone, TIMEZONE_AUTO, TIMEZONE_SYMLINK)) {
1688841f
LP
1672 r = readlink_malloc("/etc/localtime", &p);
1673 if (r == -ENOENT && arg_timezone == TIMEZONE_AUTO)
83205269 1674 m = etc_writable() ? TIMEZONE_DELETE : TIMEZONE_OFF;
1688841f 1675 else if (r == -EINVAL && arg_timezone == TIMEZONE_AUTO) /* regular file? */
83205269 1676 m = etc_writable() ? TIMEZONE_COPY : TIMEZONE_BIND;
1688841f
LP
1677 else if (r < 0) {
1678 log_warning_errno(r, "Failed to read host's /etc/localtime symlink, not updating container timezone: %m");
1679 /* To handle warning, delete /etc/localtime and replace it with a symbolic link to a time zone data
1680 * file.
1681 *
1682 * Example:
1683 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1684 */
1685 return 0;
1686 } else if (arg_timezone == TIMEZONE_AUTO)
83205269 1687 m = etc_writable() ? TIMEZONE_SYMLINK : TIMEZONE_BIND;
1688841f
LP
1688 else
1689 m = arg_timezone;
1690 } else
1691 m = arg_timezone;
1692
1693 if (m == TIMEZONE_OFF)
1694 return 0;
1695
a5648b80 1696 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc, NULL);
d4036145 1697 if (r < 0) {
1688841f 1698 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
d4036145
LP
1699 return 0;
1700 }
1701
1688841f
LP
1702 where = strjoina(etc, "/localtime");
1703
1704 switch (m) {
1705
1706 case TIMEZONE_DELETE:
1707 if (unlink(where) < 0)
1708 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1709
d4036145 1710 return 0;
d4036145 1711
1688841f
LP
1712 case TIMEZONE_SYMLINK: {
1713 _cleanup_free_ char *q = NULL;
1714 const char *z, *what;
4d1c38b8 1715
1688841f
LP
1716 z = timezone_from_path(p);
1717 if (!z) {
1718 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
d4036145 1719 return 0;
1688841f 1720 }
d4036145 1721
1688841f
LP
1722 r = readlink_malloc(where, &q);
1723 if (r >= 0 && streq_ptr(timezone_from_path(q), z))
1724 return 0; /* Already pointing to the right place? Then do nothing .. */
1725
1726 check = strjoina(dest, "/usr/share/zoneinfo/", z);
a5648b80 1727 r = chase_symlinks(check, dest, 0, NULL, NULL);
1688841f
LP
1728 if (r < 0)
1729 log_debug_errno(r, "Timezone %s does not exist (or is not accessible) in container, not creating symlink: %m", z);
1730 else {
1731 if (unlink(where) < 0 && errno != ENOENT) {
1732 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING, /* Don't complain on read-only images */
1733 errno, "Failed to remove existing timezone info %s in container, ignoring: %m", where);
1734 return 0;
1735 }
1736
1737 what = strjoina("../usr/share/zoneinfo/", z);
1738 if (symlink(what, where) < 0) {
1739 log_full_errno(IN_SET(errno, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING,
1740 errno, "Failed to correct timezone of container, ignoring: %m");
1741 return 0;
1742 }
1743
1744 break;
1745 }
1746
1747 _fallthrough_;
d4036145 1748 }
68fb0892 1749
1688841f
LP
1750 case TIMEZONE_BIND: {
1751 _cleanup_free_ char *resolved = NULL;
1752 int found;
1753
a5648b80 1754 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved, NULL);
1688841f
LP
1755 if (found < 0) {
1756 log_warning_errno(found, "Failed to resolve /etc/localtime path in container, ignoring: %m");
1757 return 0;
1758 }
1759
1760 if (found == 0) /* missing? */
1761 (void) touch(resolved);
1762
1763 r = mount_verbose(LOG_WARNING, "/etc/localtime", resolved, NULL, MS_BIND, NULL);
1764 if (r >= 0)
1765 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
1766
1767 _fallthrough_;
79d80fc1 1768 }
4d9f07b4 1769
1688841f
LP
1770 case TIMEZONE_COPY:
1771 /* If mounting failed, try to copy */
8a016c74 1772 r = copy_file_atomic("/etc/localtime", where, 0644, 0, 0, COPY_REFLINK|COPY_REPLACE);
1688841f
LP
1773 if (r < 0) {
1774 log_full_errno(IN_SET(r, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
1775 "Failed to copy /etc/localtime to %s, ignoring: %m", where);
1776 return 0;
1777 }
1778
1779 break;
1780
1781 default:
1782 assert_not_reached("unexpected mode");
d4036145 1783 }
e58a1277 1784
1688841f 1785 /* Fix permissions of the symlink or file copy we just created */
03cfe0d5
LP
1786 r = userns_lchown(where, 0, 0);
1787 if (r < 0)
1688841f 1788 log_warning_errno(r, "Failed to chown /etc/localtime, ignoring: %m");
03cfe0d5 1789
e58a1277 1790 return 0;
88213476
LP
1791}
1792
09d423e9
LP
1793static int have_resolv_conf(const char *path) {
1794 assert(path);
1795
1796 if (access(path, F_OK) < 0) {
1797 if (errno == ENOENT)
1798 return 0;
1799
1800 return log_debug_errno(errno, "Failed to determine whether '%s' is available: %m", path);
1801 }
1802
1803 return 1;
1804}
1805
7357272e 1806static int resolved_listening(void) {
b8ea7a6e 1807 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b053cd5f 1808 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
7357272e 1809 _cleanup_free_ char *dns_stub_listener_mode = NULL;
b053cd5f
LP
1810 int r;
1811
7357272e 1812 /* Check if resolved is listening */
b053cd5f
LP
1813
1814 r = sd_bus_open_system(&bus);
1815 if (r < 0)
b8ea7a6e 1816 return log_debug_errno(r, "Failed to open system bus: %m");
b053cd5f 1817
7357272e 1818 r = bus_name_has_owner(bus, "org.freedesktop.resolve1", NULL);
b8ea7a6e
LP
1819 if (r < 0)
1820 return log_debug_errno(r, "Failed to check whether the 'org.freedesktop.resolve1' bus name is taken: %m");
1821 if (r == 0)
1822 return 0;
7357272e
DM
1823
1824 r = sd_bus_get_property_string(bus,
1825 "org.freedesktop.resolve1",
1826 "/org/freedesktop/resolve1",
1827 "org.freedesktop.resolve1.Manager",
1828 "DNSStubListener",
b8ea7a6e 1829 &error,
7357272e
DM
1830 &dns_stub_listener_mode);
1831 if (r < 0)
b8ea7a6e 1832 return log_debug_errno(r, "Failed to query DNSStubListener property: %s", bus_error_message(&error, r));
7357272e
DM
1833
1834 return STR_IN_SET(dns_stub_listener_mode, "udp", "yes");
b053cd5f
LP
1835}
1836
2547bb41 1837static int setup_resolv_conf(const char *dest) {
09d423e9
LP
1838 _cleanup_free_ char *etc = NULL;
1839 const char *where, *what;
1840 ResolvConfMode m;
1841 int r;
2547bb41
LP
1842
1843 assert(dest);
1844
09d423e9
LP
1845 if (arg_resolv_conf == RESOLV_CONF_AUTO) {
1846 if (arg_private_network)
1847 m = RESOLV_CONF_OFF;
86775e35
LP
1848 else if (have_resolv_conf(PRIVATE_STUB_RESOLV_CONF) > 0 && resolved_listening() > 0)
1849 m = etc_writable() ? RESOLV_CONF_COPY_STUB : RESOLV_CONF_BIND_STUB;
09d423e9 1850 else if (have_resolv_conf("/etc/resolv.conf") > 0)
83205269 1851 m = etc_writable() ? RESOLV_CONF_COPY_HOST : RESOLV_CONF_BIND_HOST;
09d423e9 1852 else
83205269 1853 m = etc_writable() ? RESOLV_CONF_DELETE : RESOLV_CONF_OFF;
86775e35 1854
09d423e9
LP
1855 } else
1856 m = arg_resolv_conf;
1857
1858 if (m == RESOLV_CONF_OFF)
2547bb41
LP
1859 return 0;
1860
a5648b80 1861 r = chase_symlinks("/etc", dest, CHASE_PREFIX_ROOT, &etc, NULL);
87447ae4
LP
1862 if (r < 0) {
1863 log_warning_errno(r, "Failed to resolve /etc path in container, ignoring: %m");
1864 return 0;
1865 }
1866
1867 where = strjoina(etc, "/resolv.conf");
09d423e9
LP
1868
1869 if (m == RESOLV_CONF_DELETE) {
1870 if (unlink(where) < 0)
1871 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, "Failed to remove '%s', ignoring: %m", where);
1872
87447ae4
LP
1873 return 0;
1874 }
79d80fc1 1875
86775e35
LP
1876 if (IN_SET(m, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_REPLACE_STATIC, RESOLV_CONF_COPY_STATIC))
1877 what = PRIVATE_STATIC_RESOLV_CONF;
1878 else if (IN_SET(m, RESOLV_CONF_BIND_UPLINK, RESOLV_CONF_REPLACE_UPLINK, RESOLV_CONF_COPY_UPLINK))
1879 what = PRIVATE_UPLINK_RESOLV_CONF;
1880 else if (IN_SET(m, RESOLV_CONF_BIND_STUB, RESOLV_CONF_REPLACE_STUB, RESOLV_CONF_COPY_STUB))
1881 what = PRIVATE_STUB_RESOLV_CONF;
09d423e9
LP
1882 else
1883 what = "/etc/resolv.conf";
87447ae4 1884
86775e35 1885 if (IN_SET(m, RESOLV_CONF_BIND_HOST, RESOLV_CONF_BIND_STATIC, RESOLV_CONF_BIND_UPLINK, RESOLV_CONF_BIND_STUB)) {
09d423e9
LP
1886 _cleanup_free_ char *resolved = NULL;
1887 int found;
1888
a5648b80 1889 found = chase_symlinks(where, dest, CHASE_NONEXISTENT, &resolved, NULL);
09d423e9
LP
1890 if (found < 0) {
1891 log_warning_errno(found, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
1892 return 0;
1893 }
3539724c 1894
87447ae4
LP
1895 if (found == 0) /* missing? */
1896 (void) touch(resolved);
5367354d 1897
09d423e9 1898 r = mount_verbose(LOG_WARNING, what, resolved, NULL, MS_BIND, NULL);
60e76d48 1899 if (r >= 0)
87447ae4 1900 return mount_verbose(LOG_ERR, NULL, resolved, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NODEV, NULL);
86775e35
LP
1901
1902 /* If that didn't work, let's copy the file */
3539724c
LP
1903 }
1904
86775e35
LP
1905 if (IN_SET(m, RESOLV_CONF_REPLACE_HOST, RESOLV_CONF_REPLACE_STATIC, RESOLV_CONF_REPLACE_UPLINK, RESOLV_CONF_REPLACE_STUB))
1906 r = copy_file_atomic(what, where, 0644, 0, 0, COPY_REFLINK|COPY_REPLACE);
1907 else
1908 r = copy_file(what, where, O_TRUNC|O_NOFOLLOW, 0644, 0, 0, COPY_REFLINK);
79d80fc1 1909 if (r < 0) {
3539724c
LP
1910 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
1911 * resolved or something similar runs inside and the symlink points there.
68a313c5 1912 *
3539724c 1913 * If the disk image is read-only, there's also no point in complaining.
68a313c5 1914 */
86775e35
LP
1915 log_full_errno(!IN_SET(RESOLV_CONF_COPY_HOST, RESOLV_CONF_COPY_STATIC, RESOLV_CONF_COPY_UPLINK, RESOLV_CONF_COPY_STUB) &&
1916 IN_SET(r, -ELOOP, -EROFS, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r,
3539724c 1917 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where);
79d80fc1
TG
1918 return 0;
1919 }
2547bb41 1920
03cfe0d5
LP
1921 r = userns_lchown(where, 0, 0);
1922 if (r < 0)
3539724c 1923 log_warning_errno(r, "Failed to chown /etc/resolv.conf, ignoring: %m");
03cfe0d5 1924
2547bb41
LP
1925 return 0;
1926}
1927
1e4f1671 1928static int setup_boot_id(void) {
cdde6ba6
LP
1929 _cleanup_(unlink_and_freep) char *from = NULL;
1930 _cleanup_free_ char *path = NULL;
3bbaff3e 1931 sd_id128_t rnd = SD_ID128_NULL;
cdde6ba6 1932 const char *to;
04bc4a3f
LP
1933 int r;
1934
1eacc470 1935 /* Generate a new randomized boot ID, so that each boot-up of the container gets a new one */
04bc4a3f 1936
1eacc470 1937 r = tempfn_random_child("/run", "proc-sys-kernel-random-boot-id", &path);
cdde6ba6
LP
1938 if (r < 0)
1939 return log_error_errno(r, "Failed to generate random boot ID path: %m");
04bc4a3f
LP
1940
1941 r = sd_id128_randomize(&rnd);
f647962d
MS
1942 if (r < 0)
1943 return log_error_errno(r, "Failed to generate random boot id: %m");
04bc4a3f 1944
cdde6ba6 1945 r = id128_write(path, ID128_UUID, rnd, false);
f647962d
MS
1946 if (r < 0)
1947 return log_error_errno(r, "Failed to write boot id: %m");
04bc4a3f 1948
cdde6ba6
LP
1949 from = TAKE_PTR(path);
1950 to = "/proc/sys/kernel/random/boot_id";
1951
60e76d48 1952 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
cdde6ba6
LP
1953 if (r < 0)
1954 return r;
04bc4a3f 1955
cdde6ba6 1956 return mount_verbose(LOG_ERR, NULL, to, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
04bc4a3f
LP
1957}
1958
e58a1277 1959static int copy_devnodes(const char *dest) {
88213476
LP
1960 static const char devnodes[] =
1961 "null\0"
1962 "zero\0"
1963 "full\0"
1964 "random\0"
1965 "urandom\0"
85614d66
TG
1966 "tty\0"
1967 "net/tun\0";
88213476 1968
de40a303 1969 _cleanup_umask_ mode_t u;
88213476 1970 const char *d;
e58a1277 1971 int r = 0;
a258bf26
LP
1972
1973 assert(dest);
124640f1
LP
1974
1975 u = umask(0000);
88213476 1976
03cfe0d5
LP
1977 /* Create /dev/net, so that we can create /dev/net/tun in it */
1978 if (userns_mkdir(dest, "/dev/net", 0755, 0, 0) < 0)
1979 return log_error_errno(r, "Failed to create /dev/net directory: %m");
1980
88213476 1981 NULSTR_FOREACH(d, devnodes) {
7fd1b19b 1982 _cleanup_free_ char *from = NULL, *to = NULL;
7f112f50 1983 struct stat st;
88213476 1984
c6134d3e 1985 from = path_join("/dev/", d);
8967f291
LP
1986 if (!from)
1987 return log_oom();
1988
c6134d3e 1989 to = path_join(dest, from);
8967f291
LP
1990 if (!to)
1991 return log_oom();
88213476
LP
1992
1993 if (stat(from, &st) < 0) {
1994
4a62c710
MS
1995 if (errno != ENOENT)
1996 return log_error_errno(errno, "Failed to stat %s: %m", from);
88213476 1997
baaa35ad
ZJS
1998 } else if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
1999 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2000 "%s is not a char or block device, cannot copy.", from);
2001 else {
8dfce114
LP
2002 _cleanup_free_ char *sl = NULL, *prefixed = NULL, *dn = NULL, *t = NULL;
2003
81f5049b 2004 if (mknod(to, st.st_mode, st.st_rdev) < 0) {
8dbf71ec 2005 /* Explicitly warn the user when /dev is already populated. */
41eb4362 2006 if (errno == EEXIST)
8dbf71ec 2007 log_notice("%s/dev is pre-mounted and pre-populated. If a pre-mounted /dev is provided it needs to be an unpopulated file system.", dest);
81f5049b
AC
2008 if (errno != EPERM)
2009 return log_error_errno(errno, "mknod(%s) failed: %m", to);
2010
8dfce114 2011 /* Some systems abusively restrict mknod but allow bind mounts. */
81f5049b
AC
2012 r = touch(to);
2013 if (r < 0)
2014 return log_error_errno(r, "touch (%s) failed: %m", to);
60e76d48
ZJS
2015 r = mount_verbose(LOG_DEBUG, from, to, NULL, MS_BIND, NULL);
2016 if (r < 0)
2017 return log_error_errno(r, "Both mknod and bind mount (%s) failed: %m", to);
81f5049b 2018 }
6278cf60 2019
03cfe0d5
LP
2020 r = userns_lchown(to, 0, 0);
2021 if (r < 0)
2022 return log_error_errno(r, "chown() of device node %s failed: %m", to);
8dfce114 2023
657ee2d8 2024 dn = path_join("/dev", S_ISCHR(st.st_mode) ? "char" : "block");
8dfce114
LP
2025 if (!dn)
2026 return log_oom();
2027
2028 r = userns_mkdir(dest, dn, 0755, 0, 0);
2029 if (r < 0)
2030 return log_error_errno(r, "Failed to create '%s': %m", dn);
2031
2032 if (asprintf(&sl, "%s/%u:%u", dn, major(st.st_rdev), minor(st.st_rdev)) < 0)
2033 return log_oom();
2034
c6134d3e 2035 prefixed = path_join(dest, sl);
8dfce114
LP
2036 if (!prefixed)
2037 return log_oom();
2038
2d9b74ba 2039 t = path_join("..", d);
8dfce114
LP
2040 if (!t)
2041 return log_oom();
2042
2043 if (symlink(t, prefixed) < 0)
2044 log_debug_errno(errno, "Failed to symlink '%s' to '%s': %m", t, prefixed);
88213476 2045 }
88213476
LP
2046 }
2047
e58a1277
LP
2048 return r;
2049}
88213476 2050
de40a303
LP
2051static int make_extra_nodes(const char *dest) {
2052 _cleanup_umask_ mode_t u;
2053 size_t i;
2054 int r;
2055
2056 u = umask(0000);
2057
2058 for (i = 0; i < arg_n_extra_nodes; i++) {
2059 _cleanup_free_ char *path = NULL;
2060 DeviceNode *n = arg_extra_nodes + i;
2061
c6134d3e 2062 path = path_join(dest, n->path);
de40a303
LP
2063 if (!path)
2064 return log_oom();
2065
2066 if (mknod(path, n->mode, S_ISCHR(n->mode) || S_ISBLK(n->mode) ? makedev(n->major, n->minor) : 0) < 0)
2067 return log_error_errno(errno, "Failed to create device node '%s': %m", path);
2068
2069 r = chmod_and_chown(path, n->mode, n->uid, n->gid);
2070 if (r < 0)
2071 return log_error_errno(r, "Failed to adjust device node ownership of '%s': %m", path);
2072 }
2073
2074 return 0;
2075}
2076
03cfe0d5
LP
2077static int setup_pts(const char *dest) {
2078 _cleanup_free_ char *options = NULL;
2079 const char *p;
709f6e46 2080 int r;
03cfe0d5 2081
349cc4a5 2082#if HAVE_SELINUX
03cfe0d5
LP
2083 if (arg_selinux_apifs_context)
2084 (void) asprintf(&options,
3dce8915 2085 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT ",context=\"%s\"",
03cfe0d5
LP
2086 arg_uid_shift + TTY_GID,
2087 arg_selinux_apifs_context);
2088 else
2089#endif
2090 (void) asprintf(&options,
3dce8915 2091 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT,
03cfe0d5 2092 arg_uid_shift + TTY_GID);
f2d88580 2093
03cfe0d5 2094 if (!options)
f2d88580
LP
2095 return log_oom();
2096
03cfe0d5 2097 /* Mount /dev/pts itself */
cc9fce65 2098 p = prefix_roota(dest, "/dev/pts");
dae8b82e
ZJS
2099 r = mkdir_errno_wrapper(p, 0755);
2100 if (r < 0)
2101 return log_error_errno(r, "Failed to create /dev/pts: %m");
2102
60e76d48
ZJS
2103 r = mount_verbose(LOG_ERR, "devpts", p, "devpts", MS_NOSUID|MS_NOEXEC, options);
2104 if (r < 0)
2105 return r;
709f6e46
MS
2106 r = userns_lchown(p, 0, 0);
2107 if (r < 0)
2108 return log_error_errno(r, "Failed to chown /dev/pts: %m");
03cfe0d5
LP
2109
2110 /* Create /dev/ptmx symlink */
2111 p = prefix_roota(dest, "/dev/ptmx");
4a62c710
MS
2112 if (symlink("pts/ptmx", p) < 0)
2113 return log_error_errno(errno, "Failed to create /dev/ptmx symlink: %m");
709f6e46
MS
2114 r = userns_lchown(p, 0, 0);
2115 if (r < 0)
2116 return log_error_errno(r, "Failed to chown /dev/ptmx: %m");
f2d88580 2117
03cfe0d5
LP
2118 /* And fix /dev/pts/ptmx ownership */
2119 p = prefix_roota(dest, "/dev/pts/ptmx");
709f6e46
MS
2120 r = userns_lchown(p, 0, 0);
2121 if (r < 0)
2122 return log_error_errno(r, "Failed to chown /dev/pts/ptmx: %m");
6278cf60 2123
f2d88580
LP
2124 return 0;
2125}
2126
3acc84eb
FB
2127static int setup_stdio_as_dev_console(void) {
2128 int terminal;
e58a1277 2129 int r;
e58a1277 2130
3acc84eb
FB
2131 terminal = open_terminal("/dev/console", O_RDWR);
2132 if (terminal < 0)
2133 return log_error_errno(terminal, "Failed to open console: %m");
e58a1277 2134
3acc84eb
FB
2135 /* Make sure we can continue logging to the original stderr, even if
2136 * stderr points elsewhere now */
2137 r = log_dup_console();
2138 if (r < 0)
2139 return log_error_errno(r, "Failed to duplicate stderr: %m");
de40a303 2140
3acc84eb
FB
2141 /* invalidates 'terminal' on success and failure */
2142 r = rearrange_stdio(terminal, terminal, terminal);
f647962d 2143 if (r < 0)
3acc84eb
FB
2144 return log_error_errno(r, "Failed to move console to stdin/stdout/stderr: %m");
2145
2146 return 0;
2147}
88213476 2148
3acc84eb
FB
2149static int setup_dev_console(const char *console) {
2150 _cleanup_free_ char *p = NULL;
2151 int r;
a258bf26 2152
3acc84eb
FB
2153 /* Create /dev/console symlink */
2154 r = path_make_relative("/dev", console, &p);
81f5049b 2155 if (r < 0)
3acc84eb
FB
2156 return log_error_errno(r, "Failed to create relative path: %m");
2157
2158 if (symlink(p, "/dev/console") < 0)
2159 return log_error_errno(errno, "Failed to create /dev/console symlink: %m");
a258bf26 2160
3acc84eb 2161 return 0;
e58a1277
LP
2162}
2163
8e5430c4
LP
2164static int setup_keyring(void) {
2165 key_serial_t keyring;
2166
2167 /* Allocate a new session keyring for the container. This makes sure the keyring of the session systemd-nspawn
2168 * was invoked from doesn't leak into the container. Note that by default we block keyctl() and request_key()
2169 * anyway via seccomp so doing this operation isn't strictly necessary, but in case people explicitly whitelist
2170 * these system calls let's make sure we don't leak anything into the container. */
2171
2172 keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
2173 if (keyring == -1) {
2174 if (errno == ENOSYS)
2175 log_debug_errno(errno, "Kernel keyring not supported, ignoring.");
2176 else if (IN_SET(errno, EACCES, EPERM))
2177 log_debug_errno(errno, "Kernel keyring access prohibited, ignoring.");
2178 else
2179 return log_error_errno(errno, "Setting up kernel keyring failed: %m");
2180 }
2181
2182 return 0;
2183}
2184
1e4f1671 2185static int setup_kmsg(int kmsg_socket) {
9ec5a93c
LP
2186 _cleanup_(unlink_and_freep) char *from = NULL;
2187 _cleanup_free_ char *fifo = NULL;
2188 _cleanup_close_ int fd = -1;
7fd1b19b 2189 _cleanup_umask_ mode_t u;
9ec5a93c 2190 int r;
e58a1277 2191
e58a1277 2192 assert(kmsg_socket >= 0);
a258bf26 2193
e58a1277 2194 u = umask(0000);
a258bf26 2195
1eacc470 2196 /* We create the kmsg FIFO as as temporary file in /run, but immediately delete it after bind mounting it to
9ec5a93c
LP
2197 * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
2198 * differently from /dev/kmsg in that writing blocks when nothing is reading. In order to avoid any problems
2199 * with containers deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
2200
1eacc470 2201 r = tempfn_random_child("/run", "proc-kmsg", &fifo);
9ec5a93c
LP
2202 if (r < 0)
2203 return log_error_errno(r, "Failed to generate kmsg path: %m");
e58a1277 2204
9ec5a93c 2205 if (mkfifo(fifo, 0600) < 0)
03cfe0d5 2206 return log_error_errno(errno, "mkfifo() for /run/kmsg failed: %m");
9ec5a93c
LP
2207
2208 from = TAKE_PTR(fifo);
9ec5a93c 2209
1eacc470 2210 r = mount_verbose(LOG_ERR, from, "/proc/kmsg", NULL, MS_BIND, NULL);
60e76d48
ZJS
2211 if (r < 0)
2212 return r;
e58a1277 2213
669fc4e5 2214 fd = open(from, O_RDWR|O_NONBLOCK|O_CLOEXEC);
4a62c710
MS
2215 if (fd < 0)
2216 return log_error_errno(errno, "Failed to open fifo: %m");
e58a1277 2217
9ec5a93c 2218 /* Store away the fd in the socket, so that it stays open as long as we run the child */
3ee897d6 2219 r = send_one_fd(kmsg_socket, fd, 0);
d9603714
DH
2220 if (r < 0)
2221 return log_error_errno(r, "Failed to send FIFO fd: %m");
a258bf26 2222
25ea79fe 2223 return 0;
88213476
LP
2224}
2225
1c4baffc 2226static int on_address_change(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
6d0b55c2
LP
2227 union in_addr_union *exposed = userdata;
2228
2229 assert(rtnl);
2230 assert(m);
2231 assert(exposed);
2232
7a8f6325 2233 expose_port_execute(rtnl, arg_expose_ports, exposed);
6d0b55c2
LP
2234 return 0;
2235}
2236
3a74cea5 2237static int setup_hostname(void) {
c818eef1 2238 int r;
3a74cea5 2239
0c582db0 2240 if ((arg_clone_ns_flags & CLONE_NEWUTS) == 0)
eb91eb18
LP
2241 return 0;
2242
c818eef1
LP
2243 r = sethostname_idempotent(arg_hostname ?: arg_machine);
2244 if (r < 0)
2245 return log_error_errno(r, "Failed to set hostname: %m");
3a74cea5 2246
7027ff61 2247 return 0;
3a74cea5
LP
2248}
2249
57fb9fb5 2250static int setup_journal(const char *directory) {
0f5e1382 2251 _cleanup_free_ char *d = NULL;
5905d7cf 2252 char id[SD_ID128_STRING_MAX];
b2238e38
LP
2253 const char *dirname, *p, *q;
2254 sd_id128_t this_id;
8054d749 2255 bool try;
57fb9fb5
LP
2256 int r;
2257
df9a75e4
LP
2258 /* Don't link journals in ephemeral mode */
2259 if (arg_ephemeral)
2260 return 0;
2261
8054d749
LP
2262 if (arg_link_journal == LINK_NO)
2263 return 0;
2264
2265 try = arg_link_journal_try || arg_link_journal == LINK_AUTO;
2266
4d680aee 2267 r = sd_id128_get_machine(&this_id);
f647962d
MS
2268 if (r < 0)
2269 return log_error_errno(r, "Failed to retrieve machine ID: %m");
4d680aee 2270
e01ff70a 2271 if (sd_id128_equal(arg_uuid, this_id)) {
8054d749 2272 log_full(try ? LOG_WARNING : LOG_ERR,
e192a281 2273 "Host and machine ids are equal (%s): refusing to link journals", sd_id128_to_string(arg_uuid, id));
8054d749 2274 if (try)
4d680aee 2275 return 0;
df9a75e4 2276 return -EEXIST;
4d680aee
ZJS
2277 }
2278
369ca6da
ZJS
2279 FOREACH_STRING(dirname, "/var", "/var/log", "/var/log/journal") {
2280 r = userns_mkdir(directory, dirname, 0755, 0, 0);
2281 if (r < 0) {
2282 bool ignore = r == -EROFS && try;
2283 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
2284 "Failed to create %s%s: %m", dirname, ignore ? ", ignoring" : "");
2285 return ignore ? 0 : r;
2286 }
2287 }
03cfe0d5 2288
e01ff70a
MS
2289 (void) sd_id128_to_string(arg_uuid, id);
2290
03cfe0d5
LP
2291 p = strjoina("/var/log/journal/", id);
2292 q = prefix_roota(directory, p);
27407a01 2293
e1873695 2294 if (path_is_mount_point(p, NULL, 0) > 0) {
8054d749
LP
2295 if (try)
2296 return 0;
27407a01 2297
baaa35ad
ZJS
2298 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2299 "%s: already a mount point, refusing to use for journal", p);
57fb9fb5
LP
2300 }
2301
e1873695 2302 if (path_is_mount_point(q, NULL, 0) > 0) {
8054d749
LP
2303 if (try)
2304 return 0;
57fb9fb5 2305
baaa35ad
ZJS
2306 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
2307 "%s: already a mount point, refusing to use for journal", q);
57fb9fb5
LP
2308 }
2309
2310 r = readlink_and_make_absolute(p, &d);
2311 if (r >= 0) {
3742095b 2312 if (IN_SET(arg_link_journal, LINK_GUEST, LINK_AUTO) &&
57fb9fb5
LP
2313 path_equal(d, q)) {
2314
03cfe0d5 2315 r = userns_mkdir(directory, p, 0755, 0, 0);
27407a01 2316 if (r < 0)
709f6e46 2317 log_warning_errno(r, "Failed to create directory %s: %m", q);
27407a01 2318 return 0;
57fb9fb5
LP
2319 }
2320
4a62c710
MS
2321 if (unlink(p) < 0)
2322 return log_error_errno(errno, "Failed to remove symlink %s: %m", p);
57fb9fb5
LP
2323 } else if (r == -EINVAL) {
2324
2325 if (arg_link_journal == LINK_GUEST &&
2326 rmdir(p) < 0) {
2327
27407a01
ZJS
2328 if (errno == ENOTDIR) {
2329 log_error("%s already exists and is neither a symlink nor a directory", p);
2330 return r;
4314d33f
MS
2331 } else
2332 return log_error_errno(errno, "Failed to remove %s: %m", p);
57fb9fb5 2333 }
4314d33f
MS
2334 } else if (r != -ENOENT)
2335 return log_error_errno(r, "readlink(%s) failed: %m", p);
57fb9fb5
LP
2336
2337 if (arg_link_journal == LINK_GUEST) {
2338
2339 if (symlink(q, p) < 0) {
8054d749 2340 if (try) {
56f64d95 2341 log_debug_errno(errno, "Failed to symlink %s to %s, skipping journal setup: %m", q, p);
574edc90 2342 return 0;
4314d33f
MS
2343 } else
2344 return log_error_errno(errno, "Failed to symlink %s to %s: %m", q, p);
57fb9fb5
LP
2345 }
2346
03cfe0d5 2347 r = userns_mkdir(directory, p, 0755, 0, 0);
27407a01 2348 if (r < 0)
709f6e46 2349 log_warning_errno(r, "Failed to create directory %s: %m", q);
27407a01 2350 return 0;
57fb9fb5
LP
2351 }
2352
2353 if (arg_link_journal == LINK_HOST) {
ccddd104 2354 /* don't create parents here — if the host doesn't have
574edc90 2355 * permanent journal set up, don't force it here */
ba8e6c4d 2356
dae8b82e
ZJS
2357 r = mkdir_errno_wrapper(p, 0755);
2358 if (r < 0 && r != -EEXIST) {
8054d749 2359 if (try) {
dae8b82e 2360 log_debug_errno(r, "Failed to create %s, skipping journal setup: %m", p);
574edc90 2361 return 0;
4314d33f 2362 } else
dae8b82e 2363 return log_error_errno(r, "Failed to create %s: %m", p);
57fb9fb5
LP
2364 }
2365
27407a01
ZJS
2366 } else if (access(p, F_OK) < 0)
2367 return 0;
57fb9fb5 2368
cdb2b9d0
LP
2369 if (dir_is_empty(q) == 0)
2370 log_warning("%s is not empty, proceeding anyway.", q);
2371
03cfe0d5 2372 r = userns_mkdir(directory, p, 0755, 0, 0);
709f6e46
MS
2373 if (r < 0)
2374 return log_error_errno(r, "Failed to create %s: %m", q);
57fb9fb5 2375
60e76d48
ZJS
2376 r = mount_verbose(LOG_DEBUG, p, q, NULL, MS_BIND, NULL);
2377 if (r < 0)
4a62c710 2378 return log_error_errno(errno, "Failed to bind mount journal from host into guest: %m");
57fb9fb5 2379
27407a01 2380 return 0;
57fb9fb5
LP
2381}
2382
de40a303
LP
2383static int drop_capabilities(uid_t uid) {
2384 CapabilityQuintet q;
2385
2386 /* Let's initialize all five capability sets to something valid. If the quintet was configured via
2387 * OCI use that, but fill in missing bits. If it wasn't then derive the quintet in full from
2388 * arg_caps_retain. */
2389
2390 if (capability_quintet_is_set(&arg_full_capabilities)) {
2391 q = arg_full_capabilities;
2392
2393 if (q.bounding == (uint64_t) -1)
2394 q.bounding = uid == 0 ? arg_caps_retain : 0;
2395
2396 if (q.effective == (uint64_t) -1)
2397 q.effective = uid == 0 ? q.bounding : 0;
2398
2399 if (q.inheritable == (uint64_t) -1)
2400 q.inheritable = uid == 0 ? q.bounding : 0;
2401
2402 if (q.permitted == (uint64_t) -1)
2403 q.permitted = uid == 0 ? q.bounding : 0;
2404
2405 if (q.ambient == (uint64_t) -1 && ambient_capabilities_supported())
2406 q.ambient = 0;
f66ad460
AZ
2407
2408 if (capability_quintet_mangle(&q))
2409 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Cannot set capabilities that are not in the current bounding set.");
2410
2411 } else {
de40a303
LP
2412 q = (CapabilityQuintet) {
2413 .bounding = arg_caps_retain,
2414 .effective = uid == 0 ? arg_caps_retain : 0,
2415 .inheritable = uid == 0 ? arg_caps_retain : 0,
2416 .permitted = uid == 0 ? arg_caps_retain : 0,
2417 .ambient = ambient_capabilities_supported() ? 0 : (uint64_t) -1,
2418 };
2419
f66ad460
AZ
2420 /* If we're not using OCI, proceed with mangled capabilities (so we don't error out)
2421 * in order to maintain the same behavior as systemd < 242. */
2422 if (capability_quintet_mangle(&q))
0ccdaa79
JT
2423 log_full(arg_quiet ? LOG_DEBUG : LOG_WARNING,
2424 "Some capabilities will not be set because they are not in the current bounding set.");
f66ad460
AZ
2425
2426 }
2427
de40a303 2428 return capability_quintet_enforce(&q);
88213476
LP
2429}
2430
db999e0f
LP
2431static int reset_audit_loginuid(void) {
2432 _cleanup_free_ char *p = NULL;
2433 int r;
2434
0c582db0 2435 if ((arg_clone_ns_flags & CLONE_NEWPID) == 0)
db999e0f
LP
2436 return 0;
2437
2438 r = read_one_line_file("/proc/self/loginuid", &p);
13e8ceb8 2439 if (r == -ENOENT)
db999e0f 2440 return 0;
f647962d
MS
2441 if (r < 0)
2442 return log_error_errno(r, "Failed to read /proc/self/loginuid: %m");
db999e0f
LP
2443
2444 /* Already reset? */
2445 if (streq(p, "4294967295"))
2446 return 0;
2447
57512c89 2448 r = write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_DISABLE_BUFFER);
db999e0f 2449 if (r < 0) {
10a87006
LP
2450 log_error_errno(r,
2451 "Failed to reset audit login UID. This probably means that your kernel is too\n"
2452 "old and you have audit enabled. Note that the auditing subsystem is known to\n"
2453 "be incompatible with containers on old kernels. Please make sure to upgrade\n"
2454 "your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
2455 "using systemd-nspawn. Sleeping for 5s... (%m)");
77b6e194 2456
db999e0f 2457 sleep(5);
77b6e194 2458 }
db999e0f
LP
2459
2460 return 0;
77b6e194
LP
2461}
2462
785890ac
LP
2463static int setup_propagate(const char *root) {
2464 const char *p, *q;
709f6e46 2465 int r;
785890ac
LP
2466
2467 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2468 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
63c372cb 2469 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
785890ac
LP
2470 (void) mkdir_p(p, 0600);
2471
709f6e46
MS
2472 r = userns_mkdir(root, "/run/systemd", 0755, 0, 0);
2473 if (r < 0)
2474 return log_error_errno(r, "Failed to create /run/systemd: %m");
03cfe0d5 2475
709f6e46
MS
2476 r = userns_mkdir(root, "/run/systemd/nspawn", 0755, 0, 0);
2477 if (r < 0)
2478 return log_error_errno(r, "Failed to create /run/systemd/nspawn: %m");
03cfe0d5 2479
709f6e46
MS
2480 r = userns_mkdir(root, "/run/systemd/nspawn/incoming", 0600, 0, 0);
2481 if (r < 0)
2482 return log_error_errno(r, "Failed to create /run/systemd/nspawn/incoming: %m");
785890ac 2483
03cfe0d5 2484 q = prefix_roota(root, "/run/systemd/nspawn/incoming");
60e76d48
ZJS
2485 r = mount_verbose(LOG_ERR, p, q, NULL, MS_BIND, NULL);
2486 if (r < 0)
2487 return r;
785890ac 2488
60e76d48
ZJS
2489 r = mount_verbose(LOG_ERR, NULL, q, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
2490 if (r < 0)
2491 return r;
785890ac 2492
19caffac
AC
2493 /* machined will MS_MOVE into that directory, and that's only
2494 * supported for non-shared mounts. */
60e76d48 2495 return mount_verbose(LOG_ERR, NULL, q, NULL, MS_SLAVE, NULL);
785890ac
LP
2496}
2497
317feb4d 2498static int setup_machine_id(const char *directory) {
691675ba
LP
2499 const char *etc_machine_id;
2500 sd_id128_t id;
3bbaff3e 2501 int r;
e01ff70a 2502
317feb4d
LP
2503 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2504 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2505 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2506 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2507 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2508 * container behaves nicely). */
2509
e01ff70a
MS
2510 etc_machine_id = prefix_roota(directory, "/etc/machine-id");
2511
691675ba 2512 r = id128_read(etc_machine_id, ID128_PLAIN, &id);
317feb4d
LP
2513 if (r < 0) {
2514 if (!IN_SET(r, -ENOENT, -ENOMEDIUM)) /* If the file is missing or empty, we don't mind */
2515 return log_error_errno(r, "Failed to read machine ID from container image: %m");
691675ba 2516
317feb4d
LP
2517 if (sd_id128_is_null(arg_uuid)) {
2518 r = sd_id128_randomize(&arg_uuid);
2519 if (r < 0)
2520 return log_error_errno(r, "Failed to acquire randomized machine UUID: %m");
2521 }
2522 } else {
baaa35ad
ZJS
2523 if (sd_id128_is_null(id))
2524 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2525 "Machine ID in container image is zero, refusing.");
e01ff70a 2526
317feb4d
LP
2527 arg_uuid = id;
2528 }
691675ba 2529
e01ff70a
MS
2530 return 0;
2531}
2532
7336138e
LP
2533static int recursive_chown(const char *directory, uid_t shift, uid_t range) {
2534 int r;
2535
2536 assert(directory);
2537
0de7acce 2538 if (arg_userns_mode == USER_NAMESPACE_NO || !arg_userns_chown)
7336138e
LP
2539 return 0;
2540
2541 r = path_patch_uid(directory, arg_uid_shift, arg_uid_range);
2542 if (r == -EOPNOTSUPP)
2543 return log_error_errno(r, "Automatic UID/GID adjusting is only supported for UID/GID ranges starting at multiples of 2^16 with a range of 2^16.");
2544 if (r == -EBADE)
2545 return log_error_errno(r, "Upper 16 bits of root directory UID and GID do not match.");
2546 if (r < 0)
2547 return log_error_errno(r, "Failed to adjust UID/GID shift of OS tree: %m");
2548 if (r == 0)
2549 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2550 else
2551 log_debug("Patched directory tree to match UID/GID range.");
2552
2553 return r;
2554}
2555
113cea80 2556/*
6d416b9c
LS
2557 * Return values:
2558 * < 0 : wait_for_terminate() failed to get the state of the
2559 * container, the container was terminated by a signal, or
2560 * failed for an unknown reason. No change is made to the
2561 * container argument.
2562 * > 0 : The program executed in the container terminated with an
2563 * error. The exit code of the program executed in the
919699ec
LP
2564 * container is returned. The container argument has been set
2565 * to CONTAINER_TERMINATED.
6d416b9c
LS
2566 * 0 : The container is being rebooted, has been shut down or exited
2567 * successfully. The container argument has been set to either
2568 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
113cea80 2569 *
6d416b9c
LS
2570 * That is, success is indicated by a return value of zero, and an
2571 * error is indicated by a non-zero value.
113cea80
DH
2572 */
2573static int wait_for_container(pid_t pid, ContainerStatus *container) {
113cea80 2574 siginfo_t status;
919699ec 2575 int r;
113cea80
DH
2576
2577 r = wait_for_terminate(pid, &status);
f647962d
MS
2578 if (r < 0)
2579 return log_warning_errno(r, "Failed to wait for container: %m");
113cea80
DH
2580
2581 switch (status.si_code) {
fddbb89c 2582
113cea80 2583 case CLD_EXITED:
b5a2179b 2584 if (status.si_status == 0)
919699ec 2585 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s exited successfully.", arg_machine);
b5a2179b 2586 else
919699ec 2587 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s failed with error code %i.", arg_machine, status.si_status);
fddbb89c 2588
919699ec
LP
2589 *container = CONTAINER_TERMINATED;
2590 return status.si_status;
113cea80
DH
2591
2592 case CLD_KILLED:
2593 if (status.si_status == SIGINT) {
919699ec 2594 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s has been shut down.", arg_machine);
113cea80 2595 *container = CONTAINER_TERMINATED;
919699ec
LP
2596 return 0;
2597
113cea80 2598 } else if (status.si_status == SIGHUP) {
919699ec 2599 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Container %s is being rebooted.", arg_machine);
113cea80 2600 *container = CONTAINER_REBOOTED;
919699ec 2601 return 0;
113cea80 2602 }
919699ec 2603
4831981d 2604 _fallthrough_;
113cea80 2605 case CLD_DUMPED:
baaa35ad
ZJS
2606 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2607 "Container %s terminated by signal %s.", arg_machine, signal_to_string(status.si_status));
113cea80
DH
2608
2609 default:
baaa35ad
ZJS
2610 return log_error_errno(SYNTHETIC_ERRNO(EIO),
2611 "Container %s failed due to unknown reason.", arg_machine);
113cea80 2612 }
113cea80
DH
2613}
2614
023fb90b
LP
2615static int on_orderly_shutdown(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
2616 pid_t pid;
2617
4a0b58c4 2618 pid = PTR_TO_PID(userdata);
023fb90b 2619 if (pid > 0) {
c6c8f6e2 2620 if (kill(pid, arg_kill_signal) >= 0) {
023fb90b
LP
2621 log_info("Trying to halt container. Send SIGTERM again to trigger immediate termination.");
2622 sd_event_source_set_userdata(s, NULL);
2623 return 0;
2624 }
2625 }
2626
2627 sd_event_exit(sd_event_source_get_event(s), 0);
2628 return 0;
2629}
2630
6916b164 2631static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *ssi, void *userdata) {
abdb9b08
LP
2632 pid_t pid;
2633
2634 assert(s);
2635 assert(ssi);
2636
2637 pid = PTR_TO_PID(userdata);
2638
6916b164
AU
2639 for (;;) {
2640 siginfo_t si = {};
abdb9b08 2641
6916b164
AU
2642 if (waitid(P_ALL, 0, &si, WNOHANG|WNOWAIT|WEXITED) < 0)
2643 return log_error_errno(errno, "Failed to waitid(): %m");
2644 if (si.si_pid == 0) /* No pending children. */
2645 break;
abdb9b08 2646 if (si.si_pid == pid) {
6916b164
AU
2647 /* The main process we care for has exited. Return from
2648 * signal handler but leave the zombie. */
2649 sd_event_exit(sd_event_source_get_event(s), 0);
2650 break;
2651 }
abdb9b08 2652
6916b164
AU
2653 /* Reap all other children. */
2654 (void) waitid(P_PID, si.si_pid, &si, WNOHANG|WEXITED);
2655 }
2656
2657 return 0;
2658}
2659
abdb9b08
LP
2660static int on_request_stop(sd_bus_message *m, void *userdata, sd_bus_error *error) {
2661 pid_t pid;
2662
2663 assert(m);
2664
2665 pid = PTR_TO_PID(userdata);
2666
2667 if (arg_kill_signal > 0) {
2668 log_info("Container termination requested. Attempting to halt container.");
2669 (void) kill(pid, arg_kill_signal);
2670 } else {
2671 log_info("Container termination requested. Exiting.");
2672 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0);
2673 }
2674
2675 return 0;
2676}
2677
ec16945e 2678static int determine_names(void) {
1b9cebf6 2679 int r;
ec16945e 2680
c1521918
LP
2681 if (arg_template && !arg_directory && arg_machine) {
2682
2683 /* If --template= was specified then we should not
2684 * search for a machine, but instead create a new one
2685 * in /var/lib/machine. */
2686
657ee2d8 2687 arg_directory = path_join("/var/lib/machines", arg_machine);
c1521918
LP
2688 if (!arg_directory)
2689 return log_oom();
2690 }
2691
ec16945e 2692 if (!arg_image && !arg_directory) {
1b9cebf6
LP
2693 if (arg_machine) {
2694 _cleanup_(image_unrefp) Image *i = NULL;
2695
5ef46e5f 2696 r = image_find(IMAGE_MACHINE, arg_machine, &i);
3a6ce860
LP
2697 if (r == -ENOENT)
2698 return log_error_errno(r, "No image for machine '%s'.", arg_machine);
1b9cebf6
LP
2699 if (r < 0)
2700 return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
1b9cebf6 2701
eb38edce 2702 if (IN_SET(i->type, IMAGE_RAW, IMAGE_BLOCK))
0f03c2a4 2703 r = free_and_strdup(&arg_image, i->path);
1b9cebf6 2704 else
0f03c2a4 2705 r = free_and_strdup(&arg_directory, i->path);
1b9cebf6 2706 if (r < 0)
0f3be6ca 2707 return log_oom();
1b9cebf6 2708
aee327b8
LP
2709 if (!arg_ephemeral)
2710 arg_read_only = arg_read_only || i->read_only;
d7249575
LP
2711 } else {
2712 r = safe_getcwd(&arg_directory);
2713 if (r < 0)
2714 return log_error_errno(r, "Failed to determine current directory: %m");
2715 }
ec16945e 2716
c6147113
LP
2717 if (!arg_directory && !arg_image)
2718 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine path, please use -D or -i.");
ec16945e
LP
2719 }
2720
2721 if (!arg_machine) {
b9ba4dab
LP
2722 if (arg_directory && path_equal(arg_directory, "/"))
2723 arg_machine = gethostname_malloc();
4827ab48
LP
2724 else {
2725 if (arg_image) {
2726 char *e;
2727
2728 arg_machine = strdup(basename(arg_image));
2729
2730 /* Truncate suffix if there is one */
2731 e = endswith(arg_machine, ".raw");
2732 if (e)
2733 *e = 0;
2734 } else
2735 arg_machine = strdup(basename(arg_directory));
2736 }
ec16945e
LP
2737 if (!arg_machine)
2738 return log_oom();
2739
ae691c1d 2740 hostname_cleanup(arg_machine);
c6147113
LP
2741 if (!machine_name_is_valid(arg_machine))
2742 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
b9ba4dab
LP
2743
2744 if (arg_ephemeral) {
2745 char *b;
2746
2747 /* Add a random suffix when this is an
2748 * ephemeral machine, so that we can run many
2749 * instances at once without manually having
2750 * to specify -M each time. */
2751
2752 if (asprintf(&b, "%s-%016" PRIx64, arg_machine, random_u64()) < 0)
2753 return log_oom();
2754
2755 free(arg_machine);
2756 arg_machine = b;
2757 }
ec16945e
LP
2758 }
2759
2760 return 0;
2761}
2762
8d4aa2bb 2763static int chase_symlinks_and_update(char **p, unsigned flags) {
3f342ec4
LP
2764 char *chased;
2765 int r;
2766
2767 assert(p);
2768
2769 if (!*p)
2770 return 0;
2771
a5648b80 2772 r = chase_symlinks(*p, NULL, flags, &chased, NULL);
3f342ec4
LP
2773 if (r < 0)
2774 return log_error_errno(r, "Failed to resolve path %s: %m", *p);
2775
a5648b80 2776 return free_and_replace(*p, chased);
3f342ec4
LP
2777}
2778
03cfe0d5 2779static int determine_uid_shift(const char *directory) {
6dac160c
LP
2780 int r;
2781
0de7acce 2782 if (arg_userns_mode == USER_NAMESPACE_NO) {
03cfe0d5 2783 arg_uid_shift = 0;
6dac160c 2784 return 0;
03cfe0d5 2785 }
6dac160c
LP
2786
2787 if (arg_uid_shift == UID_INVALID) {
2788 struct stat st;
2789
03cfe0d5 2790 r = stat(directory, &st);
6dac160c 2791 if (r < 0)
03cfe0d5 2792 return log_error_errno(errno, "Failed to determine UID base of %s: %m", directory);
6dac160c
LP
2793
2794 arg_uid_shift = st.st_uid & UINT32_C(0xffff0000);
2795
baaa35ad
ZJS
2796 if (arg_uid_shift != (st.st_gid & UINT32_C(0xffff0000)))
2797 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2798 "UID and GID base of %s don't match.", directory);
6dac160c
LP
2799
2800 arg_uid_range = UINT32_C(0x10000);
2801 }
2802
baaa35ad
ZJS
2803 if (arg_uid_shift > (uid_t) -1 - arg_uid_range)
2804 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2805 "UID base too high for UID range.");
6dac160c 2806
6dac160c
LP
2807 return 0;
2808}
2809
de40a303
LP
2810static unsigned long effective_clone_ns_flags(void) {
2811 unsigned long flags = arg_clone_ns_flags;
2812
2813 if (arg_private_network)
2814 flags |= CLONE_NEWNET;
2815 if (arg_use_cgns)
2816 flags |= CLONE_NEWCGROUP;
2817 if (arg_userns_mode != USER_NAMESPACE_NO)
2818 flags |= CLONE_NEWUSER;
2819
2820 return flags;
2821}
2822
2823static int patch_sysctl(void) {
2824
2825 /* This table is inspired by runc's sysctl() function */
2826 static const struct {
2827 const char *key;
2828 bool prefix;
2829 unsigned long clone_flags;
2830 } safe_sysctl[] = {
2831 { "kernel.hostname", false, CLONE_NEWUTS },
2832 { "kernel.domainname", false, CLONE_NEWUTS },
2833 { "kernel.msgmax", false, CLONE_NEWIPC },
2834 { "kernel.msgmnb", false, CLONE_NEWIPC },
2835 { "kernel.msgmni", false, CLONE_NEWIPC },
2836 { "kernel.sem", false, CLONE_NEWIPC },
2837 { "kernel.shmall", false, CLONE_NEWIPC },
2838 { "kernel.shmmax", false, CLONE_NEWIPC },
2839 { "kernel.shmmni", false, CLONE_NEWIPC },
2840 { "fs.mqueue.", true, CLONE_NEWIPC },
2841 { "net.", true, CLONE_NEWNET },
2842 };
2843
2844 unsigned long flags;
2845 char **k, **v;
2846 int r;
2847
2848 flags = effective_clone_ns_flags();
2849
2850 STRV_FOREACH_PAIR(k, v, arg_sysctl) {
2851 bool good = false;
2852 size_t i;
2853
2854 for (i = 0; i < ELEMENTSOF(safe_sysctl); i++) {
2855
2856 if (!FLAGS_SET(flags, safe_sysctl[i].clone_flags))
2857 continue;
2858
2859 if (safe_sysctl[i].prefix)
2860 good = startswith(*k, safe_sysctl[i].key);
2861 else
2862 good = streq(*k, safe_sysctl[i].key);
2863
2864 if (good)
2865 break;
2866 }
2867
c6147113
LP
2868 if (!good)
2869 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k);
de40a303
LP
2870
2871 r = sysctl_write(*k, *v);
2872 if (r < 0)
2873 return log_error_errno(r, "Failed to write sysctl '%s': %m", *k);
2874 }
2875
2876 return 0;
2877}
2878
03cfe0d5
LP
2879static int inner_child(
2880 Barrier *barrier,
2881 const char *directory,
2882 bool secondary,
2883 int kmsg_socket,
2884 int rtnl_socket,
3acc84eb 2885 int master_pty_socket,
f757855e 2886 FDSet *fds) {
69c79d3c 2887
03cfe0d5 2888 _cleanup_free_ char *home = NULL;
b5ea030d 2889 char as_uuid[ID128_UUID_STRING_MAX];
88614c8a 2890 size_t n_env = 1;
03cfe0d5 2891 const char *envp[] = {
0c300adf 2892 "PATH=" DEFAULT_PATH_COMPAT,
6aadfa4c 2893 NULL, /* container */
03cfe0d5
LP
2894 NULL, /* TERM */
2895 NULL, /* HOME */
2896 NULL, /* USER */
2897 NULL, /* LOGNAME */
2898 NULL, /* container_uuid */
2899 NULL, /* LISTEN_FDS */
2900 NULL, /* LISTEN_PID */
9c1e04d0 2901 NULL, /* NOTIFY_SOCKET */
03cfe0d5
LP
2902 NULL
2903 };
1a68e1e5 2904 const char *exec_target;
2371271c 2905 _cleanup_strv_free_ char **env_use = NULL;
de40a303 2906 int r, which_failed;
88213476 2907
b37469d7
LP
2908 /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
2909 * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
2910 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
2911 * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
2912 * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
2913 * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
2914 * namespace.
2915 *
2916 * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
2917 * unshare(). See below. */
2918
03cfe0d5
LP
2919 assert(barrier);
2920 assert(directory);
2921 assert(kmsg_socket >= 0);
88213476 2922
de40a303
LP
2923 log_debug("Inner child is initializing.");
2924
0de7acce 2925 if (arg_userns_mode != USER_NAMESPACE_NO) {
03cfe0d5
LP
2926 /* Tell the parent, that it now can write the UID map. */
2927 (void) barrier_place(barrier); /* #1 */
7027ff61 2928
03cfe0d5 2929 /* Wait until the parent wrote the UID map */
baaa35ad
ZJS
2930 if (!barrier_place_and_sync(barrier)) /* #2 */
2931 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
2932 "Parent died too early");
88213476
LP
2933 }
2934
6d66bd3b
EV
2935 r = reset_uid_gid();
2936 if (r < 0)
2937 return log_error_errno(r, "Couldn't become new root: %m");
2938
0de7acce 2939 r = mount_all(NULL,
4f086aab 2940 arg_mount_settings | MOUNT_IN_USERNS,
0de7acce 2941 arg_uid_shift,
0de7acce 2942 arg_selinux_apifs_context);
03cfe0d5
LP
2943 if (r < 0)
2944 return r;
2945
04413780
ZJS
2946 if (!arg_network_namespace_path && arg_private_network) {
2947 r = unshare(CLONE_NEWNET);
2948 if (r < 0)
2949 return log_error_errno(errno, "Failed to unshare network namespace: %m");
75116558
PS
2950
2951 /* Tell the parent that it can setup network interfaces. */
2952 (void) barrier_place(barrier); /* #3 */
04413780
ZJS
2953 }
2954
4f086aab 2955 r = mount_sysfs(NULL, arg_mount_settings);
d8fc6a00
LP
2956 if (r < 0)
2957 return r;
2958
03cfe0d5
LP
2959 /* Wait until we are cgroup-ified, so that we
2960 * can mount the right cgroup path writable */
baaa35ad
ZJS
2961 if (!barrier_place_and_sync(barrier)) /* #4 */
2962 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
2963 "Parent died too early");
88213476 2964
489fae52 2965 if (arg_use_cgns) {
0996ef00
CB
2966 r = unshare(CLONE_NEWCGROUP);
2967 if (r < 0)
04413780 2968 return log_error_errno(errno, "Failed to unshare cgroup namespace: %m");
0996ef00
CB
2969 r = mount_cgroups(
2970 "",
2971 arg_unified_cgroup_hierarchy,
2972 arg_userns_mode != USER_NAMESPACE_NO,
2973 arg_uid_shift,
2974 arg_uid_range,
5a8ff0e6 2975 arg_selinux_apifs_context,
ada54120 2976 true);
1433e0f2 2977 } else
0996ef00 2978 r = mount_systemd_cgroup_writable("", arg_unified_cgroup_hierarchy);
1433e0f2
LP
2979 if (r < 0)
2980 return r;
ec16945e 2981
1e4f1671 2982 r = setup_boot_id();
03cfe0d5
LP
2983 if (r < 0)
2984 return r;
ec16945e 2985
1e4f1671 2986 r = setup_kmsg(kmsg_socket);
03cfe0d5
LP
2987 if (r < 0)
2988 return r;
2989 kmsg_socket = safe_close(kmsg_socket);
ec16945e 2990
de40a303
LP
2991 r = mount_custom(
2992 "/",
2993 arg_custom_mounts,
2994 arg_n_custom_mounts,
de40a303
LP
2995 0,
2996 arg_selinux_apifs_context,
5f0a6347 2997 MOUNT_NON_ROOT_ONLY | MOUNT_IN_USERNS);
de40a303
LP
2998 if (r < 0)
2999 return r;
3000
03cfe0d5
LP
3001 if (setsid() < 0)
3002 return log_error_errno(errno, "setsid() failed: %m");
3003
3004 if (arg_private_network)
df883de9 3005 (void) loopback_setup();
03cfe0d5 3006
7a8f6325
LP
3007 if (arg_expose_ports) {
3008 r = expose_port_send_rtnl(rtnl_socket);
3009 if (r < 0)
3010 return r;
3011 rtnl_socket = safe_close(rtnl_socket);
3012 }
03cfe0d5 3013
3acc84eb 3014 if (arg_console_mode != CONSOLE_PIPE) {
cd132992 3015 _cleanup_close_ int master = -1;
3acc84eb
FB
3016 _cleanup_free_ char *console = NULL;
3017
3018 /* Allocate a pty and make it available as /dev/console. */
dc98caea 3019 master = openpt_allocate(O_RDWR|O_NONBLOCK, &console);
3acc84eb 3020 if (master < 0)
dc98caea 3021 return log_error_errno(master, "Failed to allocate a pty: %m");
3acc84eb
FB
3022
3023 r = setup_dev_console(console);
3024 if (r < 0)
105a1a36 3025 return log_error_errno(r, "Failed to set up /dev/console: %m");
3acc84eb
FB
3026
3027 r = send_one_fd(master_pty_socket, master, 0);
3028 if (r < 0)
3029 return log_error_errno(r, "Failed to send master fd: %m");
3030 master_pty_socket = safe_close(master_pty_socket);
3031
3032 r = setup_stdio_as_dev_console();
3033 if (r < 0)
3034 return r;
3035 }
3036
de40a303
LP
3037 r = patch_sysctl();
3038 if (r < 0)
3039 return r;
3040
81f345df
LP
3041 if (arg_oom_score_adjust_set) {
3042 r = set_oom_score_adjust(arg_oom_score_adjust);
3043 if (r < 0)
3044 return log_error_errno(r, "Failed to adjust OOM score: %m");
3045 }
3046
0985c7c4
ZJS
3047 if (arg_cpu_set.set)
3048 if (sched_setaffinity(0, arg_cpu_set.allocated, arg_cpu_set.set) < 0)
d107bb7d
LP
3049 return log_error_errno(errno, "Failed to set CPU affinity: %m");
3050
c818eef1 3051 (void) setup_hostname();
03cfe0d5 3052
050f7277 3053 if (arg_personality != PERSONALITY_INVALID) {
21022b9d
LP
3054 r = safe_personality(arg_personality);
3055 if (r < 0)
3056 return log_error_errno(r, "personality() failed: %m");
03cfe0d5 3057 } else if (secondary) {
21022b9d
LP
3058 r = safe_personality(PER_LINUX32);
3059 if (r < 0)
3060 return log_error_errno(r, "personality() failed: %m");
03cfe0d5
LP
3061 }
3062
de40a303
LP
3063 r = setrlimit_closest_all((const struct rlimit *const*) arg_rlimit, &which_failed);
3064 if (r < 0)
3065 return log_error_errno(r, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
3066
3067#if HAVE_SECCOMP
3068 if (arg_seccomp) {
3069
3070 if (is_seccomp_available()) {
3071
3072 r = seccomp_load(arg_seccomp);
7bc5e0b1 3073 if (ERRNO_IS_SECCOMP_FATAL(r))
de40a303
LP
3074 return log_error_errno(r, "Failed to install seccomp filter: %m");
3075 if (r < 0)
3076 log_debug_errno(r, "Failed to install seccomp filter: %m");
3077 }
3078 } else
3079#endif
3080 {
3081 r = setup_seccomp(arg_caps_retain, arg_syscall_whitelist, arg_syscall_blacklist);
3082 if (r < 0)
3083 return r;
3084 }
3085
349cc4a5 3086#if HAVE_SELINUX
03cfe0d5 3087 if (arg_selinux_context)
2ed96880 3088 if (setexeccon(arg_selinux_context) < 0)
03cfe0d5
LP
3089 return log_error_errno(errno, "setexeccon(\"%s\") failed: %m", arg_selinux_context);
3090#endif
3091
de40a303
LP
3092 /* Make sure we keep the caps across the uid/gid dropping, so that we can retain some selected caps
3093 * if we need to later on. */
3094 if (prctl(PR_SET_KEEPCAPS, 1) < 0)
3095 return log_error_errno(errno, "Failed to set PR_SET_KEEPCAPS: %m");
3096
3097 if (uid_is_valid(arg_uid) || gid_is_valid(arg_gid))
3098 r = change_uid_gid_raw(arg_uid, arg_gid, arg_supplementary_gids, arg_n_supplementary_gids);
3099 else
3100 r = change_uid_gid(arg_user, &home);
03cfe0d5
LP
3101 if (r < 0)
3102 return r;
3103
de40a303
LP
3104 r = drop_capabilities(getuid());
3105 if (r < 0)
3106 return log_error_errno(r, "Dropping capabilities failed: %m");
3107
66edd963
LP
3108 if (arg_no_new_privileges)
3109 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
3110 return log_error_errno(errno, "Failed to disable new privileges: %m");
3111
6aadfa4c
ILG
3112 /* LXC sets container=lxc, so follow the scheme here */
3113 envp[n_env++] = strjoina("container=", arg_container_service_name);
3114
03cfe0d5
LP
3115 envp[n_env] = strv_find_prefix(environ, "TERM=");
3116 if (envp[n_env])
313cefa1 3117 n_env++;
03cfe0d5 3118
de40a303
LP
3119 if (home || !uid_is_valid(arg_uid) || arg_uid == 0)
3120 if (asprintf((char**)(envp + n_env++), "HOME=%s", home ?: "/root") < 0)
3121 return log_oom();
3122
3123 if (arg_user || !uid_is_valid(arg_uid) || arg_uid == 0)
3124 if (asprintf((char**)(envp + n_env++), "USER=%s", arg_user ?: "root") < 0 ||
3125 asprintf((char**)(envp + n_env++), "LOGNAME=%s", arg_user ? arg_user : "root") < 0)
3126 return log_oom();
03cfe0d5 3127
3bbaff3e 3128 assert(!sd_id128_is_null(arg_uuid));
03cfe0d5 3129
691675ba 3130 if (asprintf((char**)(envp + n_env++), "container_uuid=%s", id128_to_uuid_string(arg_uuid, as_uuid)) < 0)
e01ff70a 3131 return log_oom();
03cfe0d5
LP
3132
3133 if (fdset_size(fds) > 0) {
3134 r = fdset_cloexec(fds, false);
3135 if (r < 0)
3136 return log_error_errno(r, "Failed to unset O_CLOEXEC for file descriptors.");
3137
3138 if ((asprintf((char **)(envp + n_env++), "LISTEN_FDS=%u", fdset_size(fds)) < 0) ||
3139 (asprintf((char **)(envp + n_env++), "LISTEN_PID=1") < 0))
3140 return log_oom();
3141 }
9c1e04d0
AP
3142 if (asprintf((char **)(envp + n_env++), "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH) < 0)
3143 return log_oom();
03cfe0d5 3144
2371271c
TG
3145 env_use = strv_env_merge(2, envp, arg_setenv);
3146 if (!env_use)
3147 return log_oom();
03cfe0d5
LP
3148
3149 /* Let the parent know that we are ready and
3150 * wait until the parent is ready with the
3151 * setup, too... */
baaa35ad
ZJS
3152 if (!barrier_place_and_sync(barrier)) /* #5 */
3153 return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
3154 "Parent died too early");
03cfe0d5 3155
5f932eb9
LP
3156 if (arg_chdir)
3157 if (chdir(arg_chdir) < 0)
3158 return log_error_errno(errno, "Failed to change to specified working directory %s: %m", arg_chdir);
3159
7732f92b 3160 if (arg_start_mode == START_PID2) {
75bf701f 3161 r = stub_pid1(arg_uuid);
7732f92b
LP
3162 if (r < 0)
3163 return r;
3164 }
3165
de40a303
LP
3166 log_debug("Inner child completed, invoking payload.");
3167
8ca082b4
LP
3168 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
3169 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
3170 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
03cfe0d5 3171 log_close();
8ca082b4
LP
3172 log_set_open_when_needed(true);
3173
03cfe0d5
LP
3174 (void) fdset_close_others(fds);
3175
7732f92b 3176 if (arg_start_mode == START_BOOT) {
03cfe0d5
LP
3177 char **a;
3178 size_t m;
3179
3180 /* Automatically search for the init system */
3181
75f32f04
ZJS
3182 m = strv_length(arg_parameters);
3183 a = newa(char*, m + 2);
3184 memcpy_safe(a + 1, arg_parameters, m * sizeof(char*));
3185 a[1 + m] = NULL;
03cfe0d5 3186
ced58da7 3187 a[0] = (char*) "/usr/lib/systemd/systemd";
03cfe0d5
LP
3188 execve(a[0], a, env_use);
3189
ced58da7 3190 a[0] = (char*) "/lib/systemd/systemd";
03cfe0d5
LP
3191 execve(a[0], a, env_use);
3192
ced58da7 3193 a[0] = (char*) "/sbin/init";
03cfe0d5 3194 execve(a[0], a, env_use);
ced58da7
LP
3195
3196 exec_target = "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
1a68e1e5 3197 } else if (!strv_isempty(arg_parameters)) {
b6b180b7
LP
3198 const char *dollar_path;
3199
1a68e1e5 3200 exec_target = arg_parameters[0];
b6b180b7
LP
3201
3202 /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
3203 * binary. */
3204 dollar_path = strv_env_get(env_use, "PATH");
3205 if (dollar_path) {
3206 if (putenv((char*) dollar_path) != 0)
3207 return log_error_errno(errno, "Failed to update $PATH: %m");
3208 }
3209
f757855e 3210 execvpe(arg_parameters[0], arg_parameters, env_use);
1a68e1e5 3211 } else {
5f932eb9 3212 if (!arg_chdir)
d929b0f9
ZJS
3213 /* If we cannot change the directory, we'll end up in /, that is expected. */
3214 (void) chdir(home ?: "/root");
5f932eb9 3215
03cfe0d5
LP
3216 execle("/bin/bash", "-bash", NULL, env_use);
3217 execle("/bin/sh", "-sh", NULL, env_use);
ced58da7
LP
3218
3219 exec_target = "/bin/bash, /bin/sh";
03cfe0d5
LP
3220 }
3221
8ca082b4 3222 return log_error_errno(errno, "execv(%s) failed: %m", exec_target);
03cfe0d5
LP
3223}
3224
9c1e04d0 3225static int setup_sd_notify_child(void) {
271f518f 3226 _cleanup_close_ int fd = -1;
9c1e04d0 3227 union sockaddr_union sa = {
44ed5214
LP
3228 .un.sun_family = AF_UNIX,
3229 .un.sun_path = NSPAWN_NOTIFY_SOCKET_PATH,
9c1e04d0
AP
3230 };
3231 int r;
3232
3233 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
3234 if (fd < 0)
3235 return log_error_errno(errno, "Failed to allocate notification socket: %m");
3236
3237 (void) mkdir_parents(NSPAWN_NOTIFY_SOCKET_PATH, 0755);
fbda85b0 3238 (void) sockaddr_un_unlink(&sa.un);
9c1e04d0 3239
9c1e04d0 3240 r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
271f518f 3241 if (r < 0)
44ed5214 3242 return log_error_errno(errno, "bind(" NSPAWN_NOTIFY_SOCKET_PATH ") failed: %m");
9c1e04d0 3243
adc7d9f0 3244 r = userns_lchown(NSPAWN_NOTIFY_SOCKET_PATH, 0, 0);
271f518f 3245 if (r < 0)
adc7d9f0 3246 return log_error_errno(r, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH ": %m");
adc7d9f0 3247
2ff48e98 3248 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
271f518f 3249 if (r < 0)
2ff48e98 3250 return log_error_errno(r, "SO_PASSCRED failed: %m");
9c1e04d0 3251
271f518f 3252 return TAKE_FD(fd);
9c1e04d0
AP
3253}
3254
03cfe0d5
LP
3255static int outer_child(
3256 Barrier *barrier,
3257 const char *directory,
2d845785 3258 DissectedImage *dissected_image,
03cfe0d5
LP
3259 bool secondary,
3260 int pid_socket,
e01ff70a 3261 int uuid_socket,
9c1e04d0 3262 int notify_socket,
03cfe0d5
LP
3263 int kmsg_socket,
3264 int rtnl_socket,
825d5287 3265 int uid_shift_socket,
3acc84eb 3266 int master_pty_socket,
8199d554 3267 int unified_cgroup_hierarchy_socket,
d7bea6b6
DP
3268 FDSet *fds,
3269 int netns_fd) {
03cfe0d5 3270
bf428efb 3271 _cleanup_close_ int fd = -1;
e5f10caf 3272 const char *p;
03cfe0d5
LP
3273 pid_t pid;
3274 ssize_t l;
de40a303 3275 int r;
03cfe0d5 3276
b37469d7
LP
3277 /* This is the "outer" child process, i.e the one forked off by the container manager itself. It already has
3278 * its own CLONE_NEWNS namespace (which was created by the clone()). It still lives in the host's CLONE_NEWPID,
3279 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER and CLONE_NEWNET namespaces. After it completed a number of
3280 * initializations a second child (the "inner" one) is forked off it, and it exits. */
3281
03cfe0d5
LP
3282 assert(barrier);
3283 assert(directory);
03cfe0d5 3284 assert(pid_socket >= 0);
e01ff70a 3285 assert(uuid_socket >= 0);
9c1e04d0 3286 assert(notify_socket >= 0);
3acc84eb 3287 assert(master_pty_socket >= 0);
03cfe0d5
LP
3288 assert(kmsg_socket >= 0);
3289
de40a303
LP
3290 log_debug("Outer child is initializing.");
3291
03cfe0d5
LP
3292 if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
3293 return log_error_errno(errno, "PR_SET_PDEATHSIG failed: %m");
3294
03cfe0d5
LP
3295 r = reset_audit_loginuid();
3296 if (r < 0)
3297 return r;
3298
3299 /* Mark everything as slave, so that we still
3300 * receive mounts from the real root, but don't
3301 * propagate mounts to the real root. */
60e76d48
ZJS
3302 r = mount_verbose(LOG_ERR, NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
3303 if (r < 0)
3304 return r;
03cfe0d5 3305
2d845785 3306 if (dissected_image) {
2d3a5a73
LP
3307 /* If we are operating on a disk image, then mount its root directory now, but leave out the rest. We
3308 * can read the UID shift from it if we need to. Further down we'll mount the rest, but then with the
3309 * uid shift known. That way we can mount VFAT file systems shifted to the right place right away. This
3310 * makes sure ESP partitions and userns are compatible. */
3311
3312 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
03bcb6d4 3313 DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|
4fcb96ce 3314 (arg_read_only ? DISSECT_IMAGE_READ_ONLY : DISSECT_IMAGE_FSCK)|
03bcb6d4 3315 (arg_start_mode == START_BOOT ? DISSECT_IMAGE_VALIDATE_OS : 0));
4fcb96ce
LP
3316 if (r == -EUCLEAN)
3317 return log_error_errno(r, "File system check for image failed: %m");
2d845785 3318 if (r < 0)
4fcb96ce 3319 return log_error_errno(r, "Failed to mount image root file system: %m");
2d845785 3320 }
03cfe0d5 3321
391567f4
LP
3322 r = determine_uid_shift(directory);
3323 if (r < 0)
3324 return r;
3325
0de7acce 3326 if (arg_userns_mode != USER_NAMESPACE_NO) {
0e7ac751 3327 /* Let the parent know which UID shift we read from the image */
825d5287
RM
3328 l = send(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), MSG_NOSIGNAL);
3329 if (l < 0)
3330 return log_error_errno(errno, "Failed to send UID shift: %m");
baaa35ad
ZJS
3331 if (l != sizeof(arg_uid_shift))
3332 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3333 "Short write while sending UID shift.");
0e7ac751 3334
0de7acce 3335 if (arg_userns_mode == USER_NAMESPACE_PICK) {
0e7ac751
LP
3336 /* When we are supposed to pick the UID shift, the parent will check now whether the UID shift
3337 * we just read from the image is available. If yes, it will send the UID shift back to us, if
3338 * not it will pick a different one, and send it back to us. */
3339
3340 l = recv(uid_shift_socket, &arg_uid_shift, sizeof(arg_uid_shift), 0);
3341 if (l < 0)
3342 return log_error_errno(errno, "Failed to recv UID shift: %m");
baaa35ad
ZJS
3343 if (l != sizeof(arg_uid_shift))
3344 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3345 "Short read while receiving UID shift.");
0e7ac751
LP
3346 }
3347
ff6c6cc1
LP
3348 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
3349 "Selected user namespace base " UID_FMT " and range " UID_FMT ".", arg_uid_shift, arg_uid_range);
825d5287
RM
3350 }
3351
6f83d3d1
LP
3352 if (path_equal(directory, "/")) {
3353 /* If the directory we shall boot is the host, let's operate on a bind mount at a different
3354 * place, so that we can make changes to its mount structure (for example, to implement
3355 * --volatile=) without this interfering with our ability to access files such as
3356 * /etc/localtime to copy into the container. Note that we use a fixed place for this
3357 * (instead of a temporary directory, since we are living in our own mount namspace here
3358 * already, and thus don't need to be afraid of colliding with anyone else's mounts).*/
3359 (void) mkdir_p("/run/systemd/nspawn-root", 0755);
3360
3361 r = mount_verbose(LOG_ERR, "/", "/run/systemd/nspawn-root", NULL, MS_BIND|MS_REC, NULL);
3362 if (r < 0)
3363 return r;
3364
3365 directory = "/run/systemd/nspawn-root";
e50cd82f 3366 }
7d0ecdd6
LP
3367
3368 r = setup_pivot_root(
3369 directory,
3370 arg_pivot_root_new,
3371 arg_pivot_root_old);
3372 if (r < 0)
3373 return r;
3374
3375 r = setup_volatile_mode(
3376 directory,
3377 arg_volatile_mode,
7d0ecdd6 3378 arg_uid_shift,
8f1ed04a 3379 arg_selinux_apifs_context);
7d0ecdd6
LP
3380 if (r < 0)
3381 return r;
3382
5f0a6347
DDM
3383 r = mount_custom(
3384 directory,
3385 arg_custom_mounts,
3386 arg_n_custom_mounts,
5f0a6347 3387 arg_uid_shift,
5f0a6347
DDM
3388 arg_selinux_apifs_context,
3389 MOUNT_ROOT_ONLY);
3390 if (r < 0)
3391 return r;
3392
5530dc87
DDM
3393 /* Make sure we always have a mount that we can move to root later on. */
3394 if (!path_is_mount_point(directory, NULL, 0)) {
3395 r = mount_verbose(LOG_ERR, directory, directory, NULL, MS_BIND|MS_REC, NULL);
3396 if (r < 0)
3397 return r;
3398 }
3399
2d3a5a73
LP
3400 if (dissected_image) {
3401 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
3402 r = dissected_image_mount(dissected_image, directory, arg_uid_shift,
4fcb96ce
LP
3403 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY|DISSECT_IMAGE_DISCARD_ON_LOOP|(arg_read_only ? DISSECT_IMAGE_READ_ONLY : DISSECT_IMAGE_FSCK));
3404 if (r == -EUCLEAN)
3405 return log_error_errno(r, "File system check for image failed: %m");
2d3a5a73 3406 if (r < 0)
4fcb96ce 3407 return log_error_errno(r, "Failed to mount image file system: %m");
2d3a5a73
LP
3408 }
3409
8199d554
LP
3410 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
3411 /* OK, we don't know yet which cgroup mode to use yet. Let's figure it out, and tell the parent. */
3412
3413 r = detect_unified_cgroup_hierarchy_from_image(directory);
3414 if (r < 0)
3415 return r;
3416
3417 l = send(unified_cgroup_hierarchy_socket, &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), MSG_NOSIGNAL);
3418 if (l < 0)
3419 return log_error_errno(errno, "Failed to send cgroup mode: %m");
baaa35ad
ZJS
3420 if (l != sizeof(arg_unified_cgroup_hierarchy))
3421 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3422 "Short write while sending cgroup mode.");
8199d554
LP
3423
3424 unified_cgroup_hierarchy_socket = safe_close(unified_cgroup_hierarchy_socket);
3425 }
3426
4ad14eff
LP
3427 /* Mark everything as shared so our mounts get propagated down. This is
3428 * required to make new bind mounts available in systemd services
5238e957 3429 * inside the container that create a new mount namespace.
4ad14eff
LP
3430 * See https://github.com/systemd/systemd/issues/3860
3431 * Further submounts (such as /dev) done after this will inherit the
5f0a6347
DDM
3432 * shared propagation mode.
3433 *
3434 * IMPORTANT: Do not overmount the root directory anymore from now on to
3435 * enable moving the root directory mount to root later on.
3436 * https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
3437 */
4ad14eff
LP
3438 r = mount_verbose(LOG_ERR, NULL, directory, NULL, MS_SHARED|MS_REC, NULL);
3439 if (r < 0)
3440 return r;
3441
3442 r = recursive_chown(directory, arg_uid_shift, arg_uid_range);
3443 if (r < 0)
3444 return r;
3445
03cfe0d5
LP
3446 r = base_filesystem_create(directory, arg_uid_shift, (gid_t) arg_uid_shift);
3447 if (r < 0)
3448 return r;
3449
bbd407ea
DDM
3450 if (arg_read_only && arg_volatile_mode == VOLATILE_NO &&
3451 !has_custom_root_mount(arg_custom_mounts, arg_n_custom_mounts)) {
64e82c19 3452 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
03cfe0d5
LP
3453 if (r < 0)
3454 return log_error_errno(r, "Failed to make tree read-only: %m");
3455 }
3456
0de7acce 3457 r = mount_all(directory,
4f086aab 3458 arg_mount_settings,
0de7acce 3459 arg_uid_shift,
0de7acce 3460 arg_selinux_apifs_context);
03cfe0d5
LP
3461 if (r < 0)
3462 return r;
3463
07fa00f9
LP
3464 r = copy_devnodes(directory);
3465 if (r < 0)
03cfe0d5
LP
3466 return r;
3467
de40a303
LP
3468 r = make_extra_nodes(directory);
3469 if (r < 0)
3470 return r;
3471
3472 (void) dev_setup(directory, arg_uid_shift, arg_uid_shift);
e5f10caf
AZ
3473
3474 p = prefix_roota(directory, "/run/systemd");
3475 (void) make_inaccessible_nodes(p, arg_uid_shift, arg_uid_shift);
03cfe0d5 3476
07fa00f9
LP
3477 r = setup_pts(directory);
3478 if (r < 0)
03cfe0d5
LP
3479 return r;
3480
3481 r = setup_propagate(directory);
3482 if (r < 0)
3483 return r;
3484
8e5430c4
LP
3485 r = setup_keyring();
3486 if (r < 0)
3487 return r;
3488
5c4deb9a
MJ
3489 r = mount_custom(
3490 directory,
3491 arg_custom_mounts,
3492 arg_n_custom_mounts,
3493 arg_uid_shift,
3494 arg_selinux_apifs_context,
3495 MOUNT_NON_ROOT_ONLY);
3496 if (r < 0)
3497 return r;
3498
03cfe0d5
LP
3499 r = setup_timezone(directory);
3500 if (r < 0)
3501 return r;
3502
3503 r = setup_resolv_conf(directory);
3504 if (r < 0)
3505 return r;
3506
e01ff70a
MS
3507 r = setup_machine_id(directory);
3508 if (r < 0)
3509 return r;
3510
03cfe0d5
LP
3511 r = setup_journal(directory);
3512 if (r < 0)
3513 return r;
3514
489fae52 3515 if (!arg_use_cgns) {
0996ef00
CB
3516 r = mount_cgroups(
3517 directory,
3518 arg_unified_cgroup_hierarchy,
3519 arg_userns_mode != USER_NAMESPACE_NO,
3520 arg_uid_shift,
3521 arg_uid_range,
5a8ff0e6 3522 arg_selinux_apifs_context,
ada54120 3523 false);
0996ef00
CB
3524 if (r < 0)
3525 return r;
3526 }
03cfe0d5
LP
3527
3528 r = mount_move_root(directory);
3529 if (r < 0)
3530 return log_error_errno(r, "Failed to move root directory: %m");
3531
9c1e04d0
AP
3532 fd = setup_sd_notify_child();
3533 if (fd < 0)
3534 return fd;
3535
03cfe0d5 3536 pid = raw_clone(SIGCHLD|CLONE_NEWNS|
0c582db0 3537 arg_clone_ns_flags |
8869a0b4 3538 (arg_userns_mode != USER_NAMESPACE_NO ? CLONE_NEWUSER : 0));
03cfe0d5
LP
3539 if (pid < 0)
3540 return log_error_errno(errno, "Failed to fork inner child: %m");
03cfe0d5
LP
3541 if (pid == 0) {
3542 pid_socket = safe_close(pid_socket);
e01ff70a 3543 uuid_socket = safe_close(uuid_socket);
9c1e04d0 3544 notify_socket = safe_close(notify_socket);
825d5287 3545 uid_shift_socket = safe_close(uid_shift_socket);
03cfe0d5
LP
3546
3547 /* The inner child has all namespaces that are
3548 * requested, so that we all are owned by the user if
3549 * user namespaces are turned on. */
3550
d7bea6b6
DP
3551 if (arg_network_namespace_path) {
3552 r = namespace_enter(-1, -1, netns_fd, -1, -1);
3553 if (r < 0)
e2d39e54 3554 return log_error_errno(r, "Failed to join network namespace: %m");
d7bea6b6
DP
3555 }
3556
3acc84eb 3557 r = inner_child(barrier, directory, secondary, kmsg_socket, rtnl_socket, master_pty_socket, fds);
03cfe0d5
LP
3558 if (r < 0)
3559 _exit(EXIT_FAILURE);
3560
3561 _exit(EXIT_SUCCESS);
3562 }
3563
3564 l = send(pid_socket, &pid, sizeof(pid), MSG_NOSIGNAL);
3565 if (l < 0)
3566 return log_error_errno(errno, "Failed to send PID: %m");
baaa35ad
ZJS
3567 if (l != sizeof(pid))
3568 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3569 "Short write while sending PID.");
03cfe0d5 3570
e01ff70a
MS
3571 l = send(uuid_socket, &arg_uuid, sizeof(arg_uuid), MSG_NOSIGNAL);
3572 if (l < 0)
3573 return log_error_errno(errno, "Failed to send machine ID: %m");
baaa35ad
ZJS
3574 if (l != sizeof(arg_uuid))
3575 return log_error_errno(SYNTHETIC_ERRNO(EIO),
3576 "Short write while sending machine ID.");
e01ff70a 3577
9c1e04d0
AP
3578 l = send_one_fd(notify_socket, fd, 0);
3579 if (l < 0)
ba72801d 3580 return log_error_errno(l, "Failed to send notify fd: %m");
9c1e04d0 3581
03cfe0d5 3582 pid_socket = safe_close(pid_socket);
e01ff70a 3583 uuid_socket = safe_close(uuid_socket);
9c1e04d0 3584 notify_socket = safe_close(notify_socket);
3acc84eb 3585 master_pty_socket = safe_close(master_pty_socket);
327e26d6
KN
3586 kmsg_socket = safe_close(kmsg_socket);
3587 rtnl_socket = safe_close(rtnl_socket);
d7bea6b6 3588 netns_fd = safe_close(netns_fd);
03cfe0d5
LP
3589
3590 return 0;
3591}
3592
0e7ac751 3593static int uid_shift_pick(uid_t *shift, LockFile *ret_lock_file) {
d381c8a6 3594 bool tried_hashed = false;
0e7ac751
LP
3595 unsigned n_tries = 100;
3596 uid_t candidate;
3597 int r;
3598
3599 assert(shift);
3600 assert(ret_lock_file);
0de7acce 3601 assert(arg_userns_mode == USER_NAMESPACE_PICK);
0e7ac751
LP
3602 assert(arg_uid_range == 0x10000U);
3603
3604 candidate = *shift;
3605
3606 (void) mkdir("/run/systemd/nspawn-uid", 0755);
3607
3608 for (;;) {
fbd0b64f 3609 char lock_path[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t) + 1];
8e766630 3610 _cleanup_(release_lock_file) LockFile lf = LOCK_FILE_INIT;
0e7ac751
LP
3611
3612 if (--n_tries <= 0)
3613 return -EBUSY;
3614
87d5e4f2 3615 if (candidate < CONTAINER_UID_BASE_MIN || candidate > CONTAINER_UID_BASE_MAX)
0e7ac751
LP
3616 goto next;
3617 if ((candidate & UINT32_C(0xFFFF)) != 0)
3618 goto next;
3619
3620 xsprintf(lock_path, "/run/systemd/nspawn-uid/" UID_FMT, candidate);
3621 r = make_lock_file(lock_path, LOCK_EX|LOCK_NB, &lf);
3622 if (r == -EBUSY) /* Range already taken by another nspawn instance */
3623 goto next;
3624 if (r < 0)
3625 return r;
3626
3627 /* Make some superficial checks whether the range is currently known in the user database */
3628 if (getpwuid(candidate))
3629 goto next;
3630 if (getpwuid(candidate + UINT32_C(0xFFFE)))
3631 goto next;
3632 if (getgrgid(candidate))
3633 goto next;
3634 if (getgrgid(candidate + UINT32_C(0xFFFE)))
3635 goto next;
3636
3637 *ret_lock_file = lf;
3638 lf = (struct LockFile) LOCK_FILE_INIT;
3639 *shift = candidate;
3640 return 0;
3641
3642 next:
d381c8a6
LP
3643 if (arg_machine && !tried_hashed) {
3644 /* Try to hash the base from the container name */
3645
3646 static const uint8_t hash_key[] = {
3647 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
3648 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
3649 };
3650
3651 candidate = (uid_t) siphash24(arg_machine, strlen(arg_machine), hash_key);
3652
3653 tried_hashed = true;
3654 } else
3655 random_bytes(&candidate, sizeof(candidate));
3656
87d5e4f2 3657 candidate = (candidate % (CONTAINER_UID_BASE_MAX - CONTAINER_UID_BASE_MIN)) + CONTAINER_UID_BASE_MIN;
0e7ac751
LP
3658 candidate &= (uid_t) UINT32_C(0xFFFF0000);
3659 }
3660}
3661
03cfe0d5 3662static int setup_uid_map(pid_t pid) {
fbd0b64f 3663 char uid_map[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t) + 1], line[DECIMAL_STR_MAX(uid_t)*3+3+1];
03cfe0d5
LP
3664 int r;
3665
3666 assert(pid > 1);
3667
3668 xsprintf(uid_map, "/proc/" PID_FMT "/uid_map", pid);
3669 xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, arg_uid_shift, arg_uid_range);
57512c89 3670 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
03cfe0d5
LP
3671 if (r < 0)
3672 return log_error_errno(r, "Failed to write UID map: %m");
3673
3674 /* We always assign the same UID and GID ranges */
3675 xsprintf(uid_map, "/proc/" PID_FMT "/gid_map", pid);
57512c89 3676 r = write_string_file(uid_map, line, WRITE_STRING_FILE_DISABLE_BUFFER);
03cfe0d5
LP
3677 if (r < 0)
3678 return log_error_errno(r, "Failed to write GID map: %m");
3679
3680 return 0;
3681}
3682
9c1e04d0 3683static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
9c1e04d0
AP
3684 char buf[NOTIFY_BUFFER_MAX+1];
3685 char *p = NULL;
3686 struct iovec iovec = {
3687 .iov_base = buf,
3688 .iov_len = sizeof(buf)-1,
3689 };
fb29cdbe
LP
3690 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) +
3691 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)) control;
9c1e04d0
AP
3692 struct msghdr msghdr = {
3693 .msg_iov = &iovec,
3694 .msg_iovlen = 1,
3695 .msg_control = &control,
3696 .msg_controllen = sizeof(control),
3697 };
371d72e0 3698 struct ucred *ucred;
9c1e04d0
AP
3699 ssize_t n;
3700 pid_t inner_child_pid;
3701 _cleanup_strv_free_ char **tags = NULL;
3702
3703 assert(userdata);
3704
3705 inner_child_pid = PTR_TO_PID(userdata);
3706
3707 if (revents != EPOLLIN) {
3708 log_warning("Got unexpected poll event for notify fd.");
3709 return 0;
3710 }
3711
3691bcf3
LP
3712 n = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
3713 if (IN_SET(n, -EAGAIN, -EINTR))
3714 return 0;
3715 if (n < 0)
3716 return log_warning_errno(n, "Couldn't read notification socket: %m");
9c1e04d0 3717
9c1e04d0
AP
3718 cmsg_close_all(&msghdr);
3719
371d72e0 3720 ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred);
9c1e04d0 3721 if (!ucred || ucred->pid != inner_child_pid) {
8cb57430 3722 log_debug("Received notify message without valid credentials. Ignoring.");
9c1e04d0
AP
3723 return 0;
3724 }
3725
3726 if ((size_t) n >= sizeof(buf)) {
3727 log_warning("Received notify message exceeded maximum size. Ignoring.");
3728 return 0;
3729 }
3730
3731 buf[n] = 0;
3732 tags = strv_split(buf, "\n\r");
3733 if (!tags)
3734 return log_oom();
3735
3736 if (strv_find(tags, "READY=1"))
04f590a4 3737 (void) sd_notifyf(false, "READY=1\n");
9c1e04d0
AP
3738
3739 p = strv_find_startswith(tags, "STATUS=");
3740 if (p)
04f590a4 3741 (void) sd_notifyf(false, "STATUS=Container running: %s", p);
9c1e04d0
AP
3742
3743 return 0;
3744}
3745
5773024d 3746static int setup_sd_notify_parent(sd_event *event, int fd, pid_t *inner_child_pid, sd_event_source **notify_event_source) {
9c1e04d0 3747 int r;
9c1e04d0 3748
5773024d 3749 r = sd_event_add_io(event, notify_event_source, fd, EPOLLIN, nspawn_dispatch_notify_fd, inner_child_pid);
9c1e04d0
AP
3750 if (r < 0)
3751 return log_error_errno(r, "Failed to allocate notify event source: %m");
3752
5773024d 3753 (void) sd_event_source_set_description(*notify_event_source, "nspawn-notify");
9c1e04d0
AP
3754
3755 return 0;
3756}
3757
5d961407
LP
3758static int merge_settings(Settings *settings, const char *path) {
3759 int rl;
f757855e 3760
5d961407
LP
3761 assert(settings);
3762 assert(path);
f757855e 3763
5d961407
LP
3764 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
3765 * that this steals the fields of the Settings* structure, and hence modifies it. */
f757855e 3766
7732f92b
LP
3767 if ((arg_settings_mask & SETTING_START_MODE) == 0 &&
3768 settings->start_mode >= 0) {
3769 arg_start_mode = settings->start_mode;
130d3d22 3770 strv_free_and_replace(arg_parameters, settings->parameters);
f757855e
LP
3771 }
3772
a2f577fc
JL
3773 if ((arg_settings_mask & SETTING_EPHEMERAL) == 0)
3774 arg_ephemeral = settings->ephemeral;
3775
de40a303
LP
3776 if ((arg_settings_mask & SETTING_DIRECTORY) == 0 &&
3777 settings->root) {
3778
3779 if (!arg_settings_trusted)
3780 log_warning("Ignoring root directory setting, file %s is not trusted.", path);
3781 else
3782 free_and_replace(arg_directory, settings->root);
3783 }
3784
b53ede69
PW
3785 if ((arg_settings_mask & SETTING_PIVOT_ROOT) == 0 &&
3786 settings->pivot_root_new) {
3787 free_and_replace(arg_pivot_root_new, settings->pivot_root_new);
3788 free_and_replace(arg_pivot_root_old, settings->pivot_root_old);
3789 }
3790
5f932eb9 3791 if ((arg_settings_mask & SETTING_WORKING_DIRECTORY) == 0 &&
1cc6c93a
YW
3792 settings->working_directory)
3793 free_and_replace(arg_chdir, settings->working_directory);
5f932eb9 3794
f757855e 3795 if ((arg_settings_mask & SETTING_ENVIRONMENT) == 0 &&
130d3d22
YW
3796 settings->environment)
3797 strv_free_and_replace(arg_setenv, settings->environment);
f757855e 3798
de40a303
LP
3799 if ((arg_settings_mask & SETTING_USER) == 0) {
3800
3801 if (settings->user)
3802 free_and_replace(arg_user, settings->user);
3803
3804 if (uid_is_valid(settings->uid))
3805 arg_uid = settings->uid;
3806 if (gid_is_valid(settings->gid))
3807 arg_gid = settings->gid;
3808 if (settings->n_supplementary_gids > 0) {
3809 free_and_replace(arg_supplementary_gids, settings->supplementary_gids);
3810 arg_n_supplementary_gids = settings->n_supplementary_gids;
3811 }
3812 }
f757855e
LP
3813
3814 if ((arg_settings_mask & SETTING_CAPABILITY) == 0) {
a3fc6b55 3815 uint64_t plus, minus;
7be830c6 3816 uint64_t network_minus = 0;
f757855e 3817
de40a303
LP
3818 /* Note that we copy both the simple plus/minus caps here, and the full quintet from the
3819 * Settings structure */
3820
0e265674 3821 plus = settings->capability;
a3fc6b55
LP
3822 minus = settings->drop_capability;
3823
3824 if ((arg_settings_mask & SETTING_NETWORK) == 0) {
3825 if (settings_private_network(settings))
3826 plus |= UINT64_C(1) << CAP_NET_ADMIN;
3827 else
7be830c6 3828 network_minus |= UINT64_C(1) << CAP_NET_ADMIN;
a3fc6b55 3829 }
0e265674
LP
3830
3831 if (!arg_settings_trusted && plus != 0) {
3832 if (settings->capability != 0)
5d961407 3833 log_warning("Ignoring Capability= setting, file %s is not trusted.", path);
7be830c6
TH
3834 } else {
3835 arg_caps_retain &= ~network_minus;
520e0d54 3836 arg_caps_retain |= plus;
7be830c6 3837 }
f757855e 3838
a3fc6b55 3839 arg_caps_retain &= ~minus;
de40a303
LP
3840
3841 /* Copy the full capabilities over too */
3842 if (capability_quintet_is_set(&settings->full_capabilities)) {
3843 if (!arg_settings_trusted)
5238e957 3844 log_warning("Ignoring capability settings, file %s is not trusted.", path);
de40a303
LP
3845 else
3846 arg_full_capabilities = settings->full_capabilities;
3847 }
f757855e
LP
3848 }
3849
3850 if ((arg_settings_mask & SETTING_KILL_SIGNAL) == 0 &&
3851 settings->kill_signal > 0)
3852 arg_kill_signal = settings->kill_signal;
3853
3854 if ((arg_settings_mask & SETTING_PERSONALITY) == 0 &&
3855 settings->personality != PERSONALITY_INVALID)
3856 arg_personality = settings->personality;
3857
3858 if ((arg_settings_mask & SETTING_MACHINE_ID) == 0 &&
3859 !sd_id128_is_null(settings->machine_id)) {
3860
3861 if (!arg_settings_trusted)
5d961407 3862 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path);
f757855e
LP
3863 else
3864 arg_uuid = settings->machine_id;
3865 }
3866
3867 if ((arg_settings_mask & SETTING_READ_ONLY) == 0 &&
3868 settings->read_only >= 0)
3869 arg_read_only = settings->read_only;
3870
3871 if ((arg_settings_mask & SETTING_VOLATILE_MODE) == 0 &&
3872 settings->volatile_mode != _VOLATILE_MODE_INVALID)
3873 arg_volatile_mode = settings->volatile_mode;
3874
3875 if ((arg_settings_mask & SETTING_CUSTOM_MOUNTS) == 0 &&
3876 settings->n_custom_mounts > 0) {
3877
3878 if (!arg_settings_trusted)
5d961407 3879 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path);
f757855e
LP
3880 else {
3881 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
1cc6c93a 3882 arg_custom_mounts = TAKE_PTR(settings->custom_mounts);
f757855e 3883 arg_n_custom_mounts = settings->n_custom_mounts;
f757855e
LP
3884 settings->n_custom_mounts = 0;
3885 }
3886 }
3887
3888 if ((arg_settings_mask & SETTING_NETWORK) == 0 &&
3889 (settings->private_network >= 0 ||
3890 settings->network_veth >= 0 ||
3891 settings->network_bridge ||
22b28dfd 3892 settings->network_zone ||
f757855e
LP
3893 settings->network_interfaces ||
3894 settings->network_macvlan ||
f6d6bad1 3895 settings->network_ipvlan ||
de40a303
LP
3896 settings->network_veth_extra ||
3897 settings->network_namespace_path)) {
f757855e
LP
3898
3899 if (!arg_settings_trusted)
5d961407 3900 log_warning("Ignoring network settings, file %s is not trusted.", path);
f757855e 3901 else {
f6d6bad1 3902 arg_network_veth = settings_network_veth(settings);
0e265674
LP
3903 arg_private_network = settings_private_network(settings);
3904
130d3d22
YW
3905 strv_free_and_replace(arg_network_interfaces, settings->network_interfaces);
3906 strv_free_and_replace(arg_network_macvlan, settings->network_macvlan);
3907 strv_free_and_replace(arg_network_ipvlan, settings->network_ipvlan);
3908 strv_free_and_replace(arg_network_veth_extra, settings->network_veth_extra);
f6d6bad1 3909
1cc6c93a
YW
3910 free_and_replace(arg_network_bridge, settings->network_bridge);
3911 free_and_replace(arg_network_zone, settings->network_zone);
de40a303
LP
3912
3913 free_and_replace(arg_network_namespace_path, settings->network_namespace_path);
f757855e
LP
3914 }
3915 }
3916
3917 if ((arg_settings_mask & SETTING_EXPOSE_PORTS) == 0 &&
3918 settings->expose_ports) {
3919
3920 if (!arg_settings_trusted)
5d961407 3921 log_warning("Ignoring Port= setting, file %s is not trusted.", path);
f757855e
LP
3922 else {
3923 expose_port_free_all(arg_expose_ports);
1cc6c93a 3924 arg_expose_ports = TAKE_PTR(settings->expose_ports);
f757855e
LP
3925 }
3926 }
3927
0de7acce
LP
3928 if ((arg_settings_mask & SETTING_USERNS) == 0 &&
3929 settings->userns_mode != _USER_NAMESPACE_MODE_INVALID) {
3930
3931 if (!arg_settings_trusted)
5d961407 3932 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path);
0de7acce
LP
3933 else {
3934 arg_userns_mode = settings->userns_mode;
3935 arg_uid_shift = settings->uid_shift;
3936 arg_uid_range = settings->uid_range;
3937 arg_userns_chown = settings->userns_chown;
3938 }
3939 }
3940
9c1e04d0
AP
3941 if ((arg_settings_mask & SETTING_NOTIFY_READY) == 0)
3942 arg_notify_ready = settings->notify_ready;
3943
960e4569
LP
3944 if ((arg_settings_mask & SETTING_SYSCALL_FILTER) == 0) {
3945
de40a303 3946 if (!arg_settings_trusted && !strv_isempty(settings->syscall_whitelist))
5d961407 3947 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path);
960e4569 3948 else {
130d3d22
YW
3949 strv_free_and_replace(arg_syscall_whitelist, settings->syscall_whitelist);
3950 strv_free_and_replace(arg_syscall_blacklist, settings->syscall_blacklist);
960e4569 3951 }
de40a303
LP
3952
3953#if HAVE_SECCOMP
3954 if (!arg_settings_trusted && settings->seccomp)
3955 log_warning("Ignoring SECCOMP filter, file %s is not trusted.", path);
3956 else {
3957 seccomp_release(arg_seccomp);
3958 arg_seccomp = TAKE_PTR(settings->seccomp);
3959 }
3960#endif
960e4569
LP
3961 }
3962
bf428efb
LP
3963 for (rl = 0; rl < _RLIMIT_MAX; rl ++) {
3964 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)))
3965 continue;
3966
3967 if (!settings->rlimit[rl])
3968 continue;
3969
3970 if (!arg_settings_trusted) {
5d961407 3971 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl), path);
bf428efb
LP
3972 continue;
3973 }
3974
3975 free_and_replace(arg_rlimit[rl], settings->rlimit[rl]);
3976 }
3977
3a9530e5
LP
3978 if ((arg_settings_mask & SETTING_HOSTNAME) == 0 &&
3979 settings->hostname)
3980 free_and_replace(arg_hostname, settings->hostname);
3981
66edd963
LP
3982 if ((arg_settings_mask & SETTING_NO_NEW_PRIVILEGES) == 0 &&
3983 settings->no_new_privileges >= 0)
3984 arg_no_new_privileges = settings->no_new_privileges;
3985
81f345df
LP
3986 if ((arg_settings_mask & SETTING_OOM_SCORE_ADJUST) == 0 &&
3987 settings->oom_score_adjust_set) {
3988
3989 if (!arg_settings_trusted)
5d961407 3990 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path);
81f345df
LP
3991 else {
3992 arg_oom_score_adjust = settings->oom_score_adjust;
3993 arg_oom_score_adjust_set = true;
3994 }
3995 }
3996
d107bb7d 3997 if ((arg_settings_mask & SETTING_CPU_AFFINITY) == 0 &&
0985c7c4 3998 settings->cpu_set.set) {
d107bb7d
LP
3999
4000 if (!arg_settings_trusted)
5d961407 4001 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
d107bb7d 4002 else {
0985c7c4
ZJS
4003 cpu_set_reset(&arg_cpu_set);
4004 arg_cpu_set = settings->cpu_set;
4005 settings->cpu_set = (CPUSet) {};
d107bb7d
LP
4006 }
4007 }
4008
09d423e9
LP
4009 if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
4010 settings->resolv_conf != _RESOLV_CONF_MODE_INVALID)
4011 arg_resolv_conf = settings->resolv_conf;
4012
4e1d6aa9
LP
4013 if ((arg_settings_mask & SETTING_LINK_JOURNAL) == 0 &&
4014 settings->link_journal != _LINK_JOURNAL_INVALID) {
4015
4016 if (!arg_settings_trusted)
4017 log_warning("Ignoring journal link setting, file '%s' is not trusted.", path);
4018 else {
4019 arg_link_journal = settings->link_journal;
4020 arg_link_journal_try = settings->link_journal_try;
4021 }
4022 }
4023
1688841f
LP
4024 if ((arg_settings_mask & SETTING_TIMEZONE) == 0 &&
4025 settings->timezone != _TIMEZONE_MODE_INVALID)
4026 arg_timezone = settings->timezone;
4027
de40a303
LP
4028 if ((arg_settings_mask & SETTING_SLICE) == 0 &&
4029 settings->slice) {
4030
4031 if (!arg_settings_trusted)
4032 log_warning("Ignoring slice setting, file '%s' is not trusted.", path);
4033 else
4034 free_and_replace(arg_slice, settings->slice);
4035 }
4036
4037 if ((arg_settings_mask & SETTING_USE_CGNS) == 0 &&
4038 settings->use_cgns >= 0) {
4039
4040 if (!arg_settings_trusted)
4041 log_warning("Ignoring cgroup namespace setting, file '%s' is not trusted.", path);
4042 else
4043 arg_use_cgns = settings->use_cgns;
4044 }
4045
4046 if ((arg_settings_mask & SETTING_CLONE_NS_FLAGS) == 0 &&
4047 settings->clone_ns_flags != (unsigned long) -1) {
4048
4049 if (!arg_settings_trusted)
4050 log_warning("Ignoring namespace setting, file '%s' is not trusted.", path);
4051 else
4052 arg_clone_ns_flags = settings->clone_ns_flags;
4053 }
4054
4055 if ((arg_settings_mask & SETTING_CONSOLE_MODE) == 0 &&
4056 settings->console_mode >= 0) {
4057
4058 if (!arg_settings_trusted)
4059 log_warning("Ignoring console mode setting, file '%s' is not trusted.", path);
4060 else
4061 arg_console_mode = settings->console_mode;
4062 }
4063
4064 /* The following properties can only be set through the OCI settings logic, not from the command line, hence we
4065 * don't consult arg_settings_mask for them. */
4066
4067 sd_bus_message_unref(arg_property_message);
4068 arg_property_message = TAKE_PTR(settings->properties);
4069
4070 arg_console_width = settings->console_width;
4071 arg_console_height = settings->console_height;
4072
b2645747 4073 device_node_array_free(arg_extra_nodes, arg_n_extra_nodes);
de40a303
LP
4074 arg_extra_nodes = TAKE_PTR(settings->extra_nodes);
4075 arg_n_extra_nodes = settings->n_extra_nodes;
4076
f757855e
LP
4077 return 0;
4078}
4079
5d961407
LP
4080static int load_settings(void) {
4081 _cleanup_(settings_freep) Settings *settings = NULL;
4082 _cleanup_fclose_ FILE *f = NULL;
4083 _cleanup_free_ char *p = NULL;
4084 const char *fn, *i;
4085 int r;
4086
de40a303
LP
4087 if (arg_oci_bundle)
4088 return 0;
4089
5d961407
LP
4090 /* If all settings are masked, there's no point in looking for
4091 * the settings file */
4092 if ((arg_settings_mask & _SETTINGS_MASK_ALL) == _SETTINGS_MASK_ALL)
4093 return 0;
4094
4095 fn = strjoina(arg_machine, ".nspawn");
4096
4097 /* We first look in the admin's directories in /etc and /run */
4098 FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
4099 _cleanup_free_ char *j = NULL;
4100
657ee2d8 4101 j = path_join(i, fn);
5d961407
LP
4102 if (!j)
4103 return log_oom();
4104
4105 f = fopen(j, "re");
4106 if (f) {
4107 p = TAKE_PTR(j);
4108
4109 /* By default, we trust configuration from /etc and /run */
4110 if (arg_settings_trusted < 0)
4111 arg_settings_trusted = true;
4112
4113 break;
4114 }
4115
4116 if (errno != ENOENT)
4117 return log_error_errno(errno, "Failed to open %s: %m", j);
4118 }
4119
4120 if (!f) {
4121 /* After that, let's look for a file next to the
4122 * actual image we shall boot. */
4123
4124 if (arg_image) {
4125 p = file_in_same_dir(arg_image, fn);
4126 if (!p)
4127 return log_oom();
cd6e3914 4128 } else if (arg_directory && !path_equal(arg_directory, "/")) {
5d961407
LP
4129 p = file_in_same_dir(arg_directory, fn);
4130 if (!p)
4131 return log_oom();
4132 }
4133
4134 if (p) {
4135 f = fopen(p, "re");
4136 if (!f && errno != ENOENT)
4137 return log_error_errno(errno, "Failed to open %s: %m", p);
4138
4139 /* By default, we do not trust configuration from /var/lib/machines */
4140 if (arg_settings_trusted < 0)
4141 arg_settings_trusted = false;
4142 }
4143 }
4144
4145 if (!f)
4146 return 0;
4147
4148 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted));
4149
4150 r = settings_load(f, p, &settings);
4151 if (r < 0)
4152 return r;
4153
4154 return merge_settings(settings, p);
4155}
4156
de40a303
LP
4157static int load_oci_bundle(void) {
4158 _cleanup_(settings_freep) Settings *settings = NULL;
4159 int r;
4160
4161 if (!arg_oci_bundle)
4162 return 0;
4163
4164 /* By default let's trust OCI bundles */
4165 if (arg_settings_trusted < 0)
4166 arg_settings_trusted = true;
4167
4168 r = oci_load(NULL, arg_oci_bundle, &settings);
4169 if (r < 0)
4170 return r;
4171
4172 return merge_settings(settings, arg_oci_bundle);
4173}
4174
3acc84eb 4175static int run_container(
2d845785 4176 DissectedImage *dissected_image,
b0067625
ZJS
4177 bool secondary,
4178 FDSet *fds,
4179 char veth_name[IFNAMSIZ], bool *veth_created,
4180 union in_addr_union *exposed,
3acc84eb 4181 int *master, pid_t *pid, int *ret) {
b0067625
ZJS
4182
4183 static const struct sigaction sa = {
4184 .sa_handler = nop_signal_handler,
e28c7cd0 4185 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
b0067625
ZJS
4186 };
4187
8e766630 4188 _cleanup_(release_lock_file) LockFile uid_shift_lock = LOCK_FILE_INIT;
b0067625
ZJS
4189 _cleanup_close_ int etc_passwd_lock = -1;
4190 _cleanup_close_pair_ int
4191 kmsg_socket_pair[2] = { -1, -1 },
4192 rtnl_socket_pair[2] = { -1, -1 },
4193 pid_socket_pair[2] = { -1, -1 },
4194 uuid_socket_pair[2] = { -1, -1 },
4195 notify_socket_pair[2] = { -1, -1 },
8199d554 4196 uid_shift_socket_pair[2] = { -1, -1 },
3acc84eb 4197 master_pty_socket_pair[2] = { -1, -1 },
8199d554
LP
4198 unified_cgroup_hierarchy_socket_pair[2] = { -1, -1};
4199
3acc84eb 4200 _cleanup_close_ int notify_socket = -1;
b0067625 4201 _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
5773024d 4202 _cleanup_(sd_event_source_unrefp) sd_event_source *notify_event_source = NULL;
b0067625
ZJS
4203 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
4204 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
4205 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
abdb9b08 4206 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
b0067625 4207 ContainerStatus container_status = 0;
b0067625
ZJS
4208 int ifi = 0, r;
4209 ssize_t l;
4210 sigset_t mask_chld;
5b4855ab 4211 _cleanup_close_ int child_netns_fd = -1;
b0067625
ZJS
4212
4213 assert_se(sigemptyset(&mask_chld) == 0);
4214 assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
4215
4216 if (arg_userns_mode == USER_NAMESPACE_PICK) {
4217 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
4218 * check with getpwuid() if the specific user already exists. Note that /etc might be
4219 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
4220 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
4221 * really just an extra safety net. We kinda assume that the UID range we allocate from is
4222 * really ours. */
4223
4224 etc_passwd_lock = take_etc_passwd_lock(NULL);
4225 if (etc_passwd_lock < 0 && etc_passwd_lock != -EROFS)
4226 return log_error_errno(etc_passwd_lock, "Failed to take /etc/passwd lock: %m");
4227 }
4228
4229 r = barrier_create(&barrier);
4230 if (r < 0)
4231 return log_error_errno(r, "Cannot initialize IPC barrier: %m");
4232
4233 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, kmsg_socket_pair) < 0)
4234 return log_error_errno(errno, "Failed to create kmsg socket pair: %m");
4235
4236 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, rtnl_socket_pair) < 0)
4237 return log_error_errno(errno, "Failed to create rtnl socket pair: %m");
4238
4239 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pid_socket_pair) < 0)
4240 return log_error_errno(errno, "Failed to create pid socket pair: %m");
4241
4242 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uuid_socket_pair) < 0)
4243 return log_error_errno(errno, "Failed to create id socket pair: %m");
4244
4245 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, notify_socket_pair) < 0)
4246 return log_error_errno(errno, "Failed to create notify socket pair: %m");
4247
3acc84eb
FB
4248 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, master_pty_socket_pair) < 0)
4249 return log_error_errno(errno, "Failed to create console socket pair: %m");
4250
b0067625
ZJS
4251 if (arg_userns_mode != USER_NAMESPACE_NO)
4252 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, uid_shift_socket_pair) < 0)
4253 return log_error_errno(errno, "Failed to create uid shift socket pair: %m");
4254
8199d554
LP
4255 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN)
4256 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, unified_cgroup_hierarchy_socket_pair) < 0)
4257 return log_error_errno(errno, "Failed to create unified cgroup socket pair: %m");
4258
b0067625
ZJS
4259 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
4260 * parent's blocking calls and give it a chance to call wait() and terminate. */
4261 r = sigprocmask(SIG_UNBLOCK, &mask_chld, NULL);
4262 if (r < 0)
4263 return log_error_errno(errno, "Failed to change the signal mask: %m");
4264
4265 r = sigaction(SIGCHLD, &sa, NULL);
4266 if (r < 0)
4267 return log_error_errno(errno, "Failed to install SIGCHLD handler: %m");
4268
d7bea6b6 4269 if (arg_network_namespace_path) {
5b4855ab
DDM
4270 child_netns_fd = open(arg_network_namespace_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
4271 if (child_netns_fd < 0)
d7bea6b6
DP
4272 return log_error_errno(errno, "Cannot open file %s: %m", arg_network_namespace_path);
4273
5b4855ab 4274 r = fd_is_network_ns(child_netns_fd);
6619ad88
LP
4275 if (r == -EUCLEAN)
4276 log_debug_errno(r, "Cannot determine if passed network namespace path '%s' really refers to a network namespace, assuming it does.", arg_network_namespace_path);
4277 else if (r < 0)
d7bea6b6 4278 return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path);
c6147113
LP
4279 else if (r == 0)
4280 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4281 "Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path);
d7bea6b6
DP
4282 }
4283
b0067625
ZJS
4284 *pid = raw_clone(SIGCHLD|CLONE_NEWNS);
4285 if (*pid < 0)
4286 return log_error_errno(errno, "clone() failed%s: %m",
4287 errno == EINVAL ?
4288 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
4289
4290 if (*pid == 0) {
4291 /* The outer child only has a file system namespace. */
4292 barrier_set_role(&barrier, BARRIER_CHILD);
4293
b0067625
ZJS
4294 kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
4295 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
4296 pid_socket_pair[0] = safe_close(pid_socket_pair[0]);
4297 uuid_socket_pair[0] = safe_close(uuid_socket_pair[0]);
4298 notify_socket_pair[0] = safe_close(notify_socket_pair[0]);
3acc84eb 4299 master_pty_socket_pair[0] = safe_close(master_pty_socket_pair[0]);
b0067625 4300 uid_shift_socket_pair[0] = safe_close(uid_shift_socket_pair[0]);
8199d554 4301 unified_cgroup_hierarchy_socket_pair[0] = safe_close(unified_cgroup_hierarchy_socket_pair[0]);
b0067625
ZJS
4302
4303 (void) reset_all_signal_handlers();
4304 (void) reset_signal_mask();
4305
4306 r = outer_child(&barrier,
4307 arg_directory,
2d845785 4308 dissected_image,
b0067625
ZJS
4309 secondary,
4310 pid_socket_pair[1],
4311 uuid_socket_pair[1],
4312 notify_socket_pair[1],
4313 kmsg_socket_pair[1],
4314 rtnl_socket_pair[1],
4315 uid_shift_socket_pair[1],
3acc84eb 4316 master_pty_socket_pair[1],
8199d554 4317 unified_cgroup_hierarchy_socket_pair[1],
d7bea6b6 4318 fds,
5b4855ab 4319 child_netns_fd);
b0067625
ZJS
4320 if (r < 0)
4321 _exit(EXIT_FAILURE);
4322
4323 _exit(EXIT_SUCCESS);
4324 }
4325
4326 barrier_set_role(&barrier, BARRIER_PARENT);
4327
e4077ff6 4328 fdset_close(fds);
b0067625
ZJS
4329
4330 kmsg_socket_pair[1] = safe_close(kmsg_socket_pair[1]);
4331 rtnl_socket_pair[1] = safe_close(rtnl_socket_pair[1]);
4332 pid_socket_pair[1] = safe_close(pid_socket_pair[1]);
4333 uuid_socket_pair[1] = safe_close(uuid_socket_pair[1]);
4334 notify_socket_pair[1] = safe_close(notify_socket_pair[1]);
3acc84eb 4335 master_pty_socket_pair[1] = safe_close(master_pty_socket_pair[1]);
b0067625 4336 uid_shift_socket_pair[1] = safe_close(uid_shift_socket_pair[1]);
8199d554 4337 unified_cgroup_hierarchy_socket_pair[1] = safe_close(unified_cgroup_hierarchy_socket_pair[1]);
b0067625
ZJS
4338
4339 if (arg_userns_mode != USER_NAMESPACE_NO) {
4340 /* The child just let us know the UID shift it might have read from the image. */
4341 l = recv(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0);
4342 if (l < 0)
4343 return log_error_errno(errno, "Failed to read UID shift: %m");
c6147113
LP
4344 if (l != sizeof arg_uid_shift)
4345 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading UID shift.");
b0067625
ZJS
4346
4347 if (arg_userns_mode == USER_NAMESPACE_PICK) {
4348 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
4349 * image, but if that's already in use, pick a new one, and report back to the child,
4350 * which one we now picked. */
4351
4352 r = uid_shift_pick(&arg_uid_shift, &uid_shift_lock);
4353 if (r < 0)
4354 return log_error_errno(r, "Failed to pick suitable UID/GID range: %m");
4355
4356 l = send(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL);
4357 if (l < 0)
4358 return log_error_errno(errno, "Failed to send UID shift: %m");
c6147113
LP
4359 if (l != sizeof arg_uid_shift)
4360 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing UID shift.");
b0067625
ZJS
4361 }
4362 }
4363
8199d554
LP
4364 if (arg_unified_cgroup_hierarchy == CGROUP_UNIFIED_UNKNOWN) {
4365 /* The child let us know the support cgroup mode it might have read from the image. */
4366 l = recv(unified_cgroup_hierarchy_socket_pair[0], &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), 0);
4367 if (l < 0)
4368 return log_error_errno(errno, "Failed to read cgroup mode: %m");
c6147113
LP
4369 if (l != sizeof(arg_unified_cgroup_hierarchy))
4370 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading cgroup mode (%zu bytes).%s",
4371 l, l == 0 ? " The child is most likely dead." : "");
8199d554
LP
4372 }
4373
b0067625 4374 /* Wait for the outer child. */
d2e0ac3d
LP
4375 r = wait_for_terminate_and_check("(sd-namespace)", *pid, WAIT_LOG_ABNORMAL);
4376 if (r < 0)
4377 return r;
4378 if (r != EXIT_SUCCESS)
4379 return -EIO;
b0067625
ZJS
4380
4381 /* And now retrieve the PID of the inner child. */
4382 l = recv(pid_socket_pair[0], pid, sizeof *pid, 0);
4383 if (l < 0)
4384 return log_error_errno(errno, "Failed to read inner child PID: %m");
c6147113
LP
4385 if (l != sizeof *pid)
4386 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading inner child PID.");
b0067625
ZJS
4387
4388 /* We also retrieve container UUID in case it was generated by outer child */
4389 l = recv(uuid_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0);
4390 if (l < 0)
4391 return log_error_errno(errno, "Failed to read container machine ID: %m");
c6147113
LP
4392 if (l != sizeof(arg_uuid))
4393 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading container machined ID.");
b0067625
ZJS
4394
4395 /* We also retrieve the socket used for notifications generated by outer child */
4396 notify_socket = receive_one_fd(notify_socket_pair[0], 0);
4397 if (notify_socket < 0)
4398 return log_error_errno(notify_socket,
4399 "Failed to receive notification socket from the outer child: %m");
4400
4401 log_debug("Init process invoked as PID "PID_FMT, *pid);
4402
4403 if (arg_userns_mode != USER_NAMESPACE_NO) {
c6147113
LP
4404 if (!barrier_place_and_sync(&barrier)) /* #1 */
4405 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
b0067625
ZJS
4406
4407 r = setup_uid_map(*pid);
4408 if (r < 0)
4409 return r;
4410
4411 (void) barrier_place(&barrier); /* #2 */
4412 }
4413
4414 if (arg_private_network) {
75116558
PS
4415 if (!arg_network_namespace_path) {
4416 /* Wait until the child has unshared its network namespace. */
c6147113
LP
4417 if (!barrier_place_and_sync(&barrier)) /* #3 */
4418 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early");
75116558
PS
4419 }
4420
5b4855ab
DDM
4421 if (child_netns_fd < 0) {
4422 /* Make sure we have an open file descriptor to the child's network
4423 * namespace so it stays alive even if the child exits. */
4424 r = namespace_open(*pid, NULL, NULL, &child_netns_fd, NULL, NULL);
4425 if (r < 0)
4426 return log_error_errno(r, "Failed to open child network namespace: %m");
4427 }
4428
4429 r = move_network_interfaces(child_netns_fd, arg_network_interfaces);
b0067625
ZJS
4430 if (r < 0)
4431 return r;
4432
4433 if (arg_network_veth) {
4434 r = setup_veth(arg_machine, *pid, veth_name,
4435 arg_network_bridge || arg_network_zone);
4436 if (r < 0)
4437 return r;
4438 else if (r > 0)
4439 ifi = r;
4440
4441 if (arg_network_bridge) {
4442 /* Add the interface to a bridge */
4443 r = setup_bridge(veth_name, arg_network_bridge, false);
4444 if (r < 0)
4445 return r;
4446 if (r > 0)
4447 ifi = r;
4448 } else if (arg_network_zone) {
4449 /* Add the interface to a bridge, possibly creating it */
4450 r = setup_bridge(veth_name, arg_network_zone, true);
4451 if (r < 0)
4452 return r;
4453 if (r > 0)
4454 ifi = r;
4455 }
4456 }
4457
4458 r = setup_veth_extra(arg_machine, *pid, arg_network_veth_extra);
4459 if (r < 0)
4460 return r;
4461
4462 /* We created the primary and extra veth links now; let's remember this, so that we know to
4463 remove them later on. Note that we don't bother with removing veth links that were created
4464 here when their setup failed half-way, because in that case the kernel should be able to
4465 remove them on its own, since they cannot be referenced by anything yet. */
4466 *veth_created = true;
4467
4468 r = setup_macvlan(arg_machine, *pid, arg_network_macvlan);
4469 if (r < 0)
4470 return r;
4471
4472 r = setup_ipvlan(arg_machine, *pid, arg_network_ipvlan);
4473 if (r < 0)
4474 return r;
4475 }
4476
abdb9b08
LP
4477 if (arg_register || !arg_keep_unit) {
4478 r = sd_bus_default_system(&bus);
4479 if (r < 0)
4480 return log_error_errno(r, "Failed to open system bus: %m");
e5a2d8b5
LP
4481
4482 r = sd_bus_set_close_on_exit(bus, false);
4483 if (r < 0)
4484 return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
abdb9b08
LP
4485 }
4486
4487 if (!arg_keep_unit) {
4488 /* When a new scope is created for this container, then we'll be registered as its controller, in which
4489 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
4490 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
4491
75152a4d
LP
4492 r = sd_bus_match_signal_async(
4493 bus,
4494 NULL,
4495 "org.freedesktop.systemd1",
4496 NULL,
4497 "org.freedesktop.systemd1.Scope",
4498 "RequestStop",
4499 on_request_stop, NULL, PID_TO_PTR(*pid));
abdb9b08 4500 if (r < 0)
75152a4d 4501 return log_error_errno(r, "Failed to request RequestStop match: %m");
abdb9b08
LP
4502 }
4503
b0067625
ZJS
4504 if (arg_register) {
4505 r = register_machine(
abdb9b08 4506 bus,
b0067625
ZJS
4507 arg_machine,
4508 *pid,
4509 arg_directory,
4510 arg_uuid,
4511 ifi,
4512 arg_slice,
4513 arg_custom_mounts, arg_n_custom_mounts,
4514 arg_kill_signal,
4515 arg_property,
de40a303 4516 arg_property_message,
b0067625
ZJS
4517 arg_keep_unit,
4518 arg_container_service_name);
4519 if (r < 0)
4520 return r;
abdb9b08 4521
cd2dfc6f
LP
4522 } else if (!arg_keep_unit) {
4523 r = allocate_scope(
abdb9b08 4524 bus,
cd2dfc6f
LP
4525 arg_machine,
4526 *pid,
4527 arg_slice,
4528 arg_custom_mounts, arg_n_custom_mounts,
4529 arg_kill_signal,
de40a303
LP
4530 arg_property,
4531 arg_property_message);
cd2dfc6f
LP
4532 if (r < 0)
4533 return r;
4534
4535 } else if (arg_slice || arg_property)
4536 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
b0067625 4537
27da7ef0 4538 r = create_subcgroup(*pid, arg_keep_unit, arg_unified_cgroup_hierarchy);
b0067625
ZJS
4539 if (r < 0)
4540 return r;
4541
27da7ef0 4542 r = sync_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
720f0a2f
LP
4543 if (r < 0)
4544 return r;
b0067625 4545
de54e02d 4546 r = chown_cgroup(*pid, arg_unified_cgroup_hierarchy, arg_uid_shift);
b0067625
ZJS
4547 if (r < 0)
4548 return r;
4549
4550 /* Notify the child that the parent is ready with all
4551 * its setup (including cgroup-ification), and that
4552 * the child can now hand over control to the code to
4553 * run inside the container. */
75116558 4554 (void) barrier_place(&barrier); /* #4 */
b0067625
ZJS
4555
4556 /* Block SIGCHLD here, before notifying child.
4557 * process_pty() will handle it with the other signals. */
4558 assert_se(sigprocmask(SIG_BLOCK, &mask_chld, NULL) >= 0);
4559
4560 /* Reset signal to default */
4561 r = default_signals(SIGCHLD, -1);
4562 if (r < 0)
4563 return log_error_errno(r, "Failed to reset SIGCHLD: %m");
4564
4565 r = sd_event_new(&event);
4566 if (r < 0)
4567 return log_error_errno(r, "Failed to get default event source: %m");
4568
8fd010bb
LP
4569 (void) sd_event_set_watchdog(event, true);
4570
abdb9b08
LP
4571 if (bus) {
4572 r = sd_bus_attach_event(bus, event, 0);
4573 if (r < 0)
4574 return log_error_errno(r, "Failed to attach bus to event loop: %m");
4575 }
4576
5773024d 4577 r = setup_sd_notify_parent(event, notify_socket, PID_TO_PTR(*pid), &notify_event_source);
b0067625
ZJS
4578 if (r < 0)
4579 return r;
4580
4581 /* Let the child know that we are ready and wait that the child is completely ready now. */
c6147113
LP
4582 if (!barrier_place_and_sync(&barrier)) /* #5 */
4583 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early.");
b0067625
ZJS
4584
4585 /* At this point we have made use of the UID we picked, and thus nss-mymachines
4586 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
4587 etc_passwd_lock = safe_close(etc_passwd_lock);
4588
04f590a4
LP
4589 (void) sd_notifyf(false,
4590 "STATUS=Container running.\n"
4591 "X_NSPAWN_LEADER_PID=" PID_FMT, *pid);
b0067625 4592 if (!arg_notify_ready)
919f5ae0 4593 (void) sd_notify(false, "READY=1\n");
b0067625
ZJS
4594
4595 if (arg_kill_signal > 0) {
4596 /* Try to kill the init system on SIGINT or SIGTERM */
919f5ae0
LP
4597 (void) sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, PID_TO_PTR(*pid));
4598 (void) sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, PID_TO_PTR(*pid));
b0067625
ZJS
4599 } else {
4600 /* Immediately exit */
919f5ae0
LP
4601 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
4602 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
b0067625
ZJS
4603 }
4604
6916b164 4605 /* Exit when the child exits */
919f5ae0 4606 (void) sd_event_add_signal(event, NULL, SIGCHLD, on_sigchld, PID_TO_PTR(*pid));
b0067625
ZJS
4607
4608 if (arg_expose_ports) {
4609 r = expose_port_watch_rtnl(event, rtnl_socket_pair[0], on_address_change, exposed, &rtnl);
4610 if (r < 0)
4611 return r;
4612
4613 (void) expose_port_execute(rtnl, arg_expose_ports, exposed);
4614 }
4615
4616 rtnl_socket_pair[0] = safe_close(rtnl_socket_pair[0]);
4617
3acc84eb
FB
4618 if (arg_console_mode != CONSOLE_PIPE) {
4619 _cleanup_close_ int fd = -1;
4620 PTYForwardFlags flags = 0;
de40a303 4621
3acc84eb
FB
4622 /* Retrieve the master pty allocated by inner child */
4623 fd = receive_one_fd(master_pty_socket_pair[0], 0);
4624 if (fd < 0)
4625 return log_error_errno(fd, "Failed to receive master pty from the inner child: %m");
4626
4627 switch (arg_console_mode) {
de40a303 4628
3acc84eb
FB
4629 case CONSOLE_READ_ONLY:
4630 flags |= PTY_FORWARD_READ_ONLY;
4631
4632 _fallthrough_;
4633
4634 case CONSOLE_INTERACTIVE:
4635 flags |= PTY_FORWARD_IGNORE_VHANGUP;
4636
4637 r = pty_forward_new(event, fd, flags, &forward);
4638 if (r < 0)
4639 return log_error_errno(r, "Failed to create PTY forwarder: %m");
4640
4641 if (arg_console_width != (unsigned) -1 || arg_console_height != (unsigned) -1)
4642 (void) pty_forward_set_width_height(forward,
4643 arg_console_width,
4644 arg_console_height);
4645 break;
4646
4647 default:
4648 assert(arg_console_mode == CONSOLE_PASSIVE);
4649 }
4650
4651 *master = TAKE_FD(fd);
de40a303 4652 }
b0067625
ZJS
4653
4654 r = sd_event_loop(event);
4655 if (r < 0)
4656 return log_error_errno(r, "Failed to run event loop: %m");
4657
de40a303
LP
4658 if (forward) {
4659 char last_char = 0;
b0067625 4660
de40a303
LP
4661 (void) pty_forward_get_last_char(forward, &last_char);
4662 forward = pty_forward_free(forward);
b0067625 4663
de40a303
LP
4664 if (!arg_quiet && last_char != '\n')
4665 putc('\n', stdout);
4666 }
b0067625
ZJS
4667
4668 /* Kill if it is not dead yet anyway */
0bb0a9fa
ZJS
4669 if (!arg_register && !arg_keep_unit && bus)
4670 terminate_scope(bus, arg_machine);
b0067625
ZJS
4671
4672 /* Normally redundant, but better safe than sorry */
c67b0082 4673 (void) kill(*pid, SIGKILL);
b0067625 4674
5b4855ab
DDM
4675 if (arg_private_network) {
4676 /* Move network interfaces back to the parent network namespace. We use `safe_fork`
4677 * to avoid having to move the parent to the child network namespace. */
4678 r = safe_fork(NULL, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_WAIT|FORK_LOG, NULL);
4679 if (r < 0)
4680 return r;
4681
4682 if (r == 0) {
4683 _cleanup_close_ int parent_netns_fd = -1;
4684
4685 r = namespace_open(getpid(), NULL, NULL, &parent_netns_fd, NULL, NULL);
4686 if (r < 0) {
4687 log_error_errno(r, "Failed to open parent network namespace: %m");
4688 _exit(EXIT_FAILURE);
4689 }
4690
4691 r = namespace_enter(-1, -1, child_netns_fd, -1, -1);
4692 if (r < 0) {
4693 log_error_errno(r, "Failed to enter child network namespace: %m");
4694 _exit(EXIT_FAILURE);
4695 }
4696
4697 r = move_network_interfaces(parent_netns_fd, arg_network_interfaces);
4698 if (r < 0)
4699 log_error_errno(r, "Failed to move network interfaces back to parent network namespace: %m");
4700
4701 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
4702 }
4703 }
4704
b0067625
ZJS
4705 r = wait_for_container(*pid, &container_status);
4706 *pid = 0;
4707
0bb0a9fa
ZJS
4708 /* Tell machined that we are gone. */
4709 if (bus)
4710 (void) unregister_machine(bus, arg_machine);
4711
b0067625
ZJS
4712 if (r < 0)
4713 /* We failed to wait for the container, or the container exited abnormally. */
4714 return r;
4715 if (r > 0 || container_status == CONTAINER_TERMINATED) {
27e29a1e
ZJS
4716 /* r > 0 → The container exited with a non-zero status.
4717 * As a special case, we need to replace 133 with a different value,
4718 * because 133 is special-cased in the service file to reboot the container.
4719 * otherwise → The container exited with zero status and a reboot was not requested.
4720 */
2a49b612 4721 if (r == EXIT_FORCE_RESTART)
27e29a1e 4722 r = EXIT_FAILURE; /* replace 133 with the general failure code */
b0067625 4723 *ret = r;
b0067625
ZJS
4724 return 0; /* finito */
4725 }
4726
4727 /* CONTAINER_REBOOTED, loop again */
4728
4729 if (arg_keep_unit) {
4730 /* Special handling if we are running as a service: instead of simply
4731 * restarting the machine we want to restart the entire service, so let's
4732 * inform systemd about this with the special exit code 133. The service
4733 * file uses RestartForceExitStatus=133 so that this results in a full
4734 * nspawn restart. This is necessary since we might have cgroup parameters
4735 * set we want to have flushed out. */
2a49b612
ZJS
4736 *ret = EXIT_FORCE_RESTART;
4737 return 0; /* finito */
b0067625
ZJS
4738 }
4739
4740 expose_port_flush(arg_expose_ports, exposed);
4741
4742 (void) remove_veth_links(veth_name, arg_network_veth_extra);
4743 *veth_created = false;
4744 return 1; /* loop again */
4745}
4746
bf428efb 4747static int initialize_rlimits(void) {
bf428efb
LP
4748 /* The default resource limits the kernel passes to PID 1, as per kernel 4.16. Let's pass our container payload
4749 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
4750 * container execution environments. */
4751
4752 static const struct rlimit kernel_defaults[_RLIMIT_MAX] = {
4753 [RLIMIT_AS] = { RLIM_INFINITY, RLIM_INFINITY },
4754 [RLIMIT_CORE] = { 0, RLIM_INFINITY },
4755 [RLIMIT_CPU] = { RLIM_INFINITY, RLIM_INFINITY },
4756 [RLIMIT_DATA] = { RLIM_INFINITY, RLIM_INFINITY },
4757 [RLIMIT_FSIZE] = { RLIM_INFINITY, RLIM_INFINITY },
4758 [RLIMIT_LOCKS] = { RLIM_INFINITY, RLIM_INFINITY },
4759 [RLIMIT_MEMLOCK] = { 65536, 65536 },
4760 [RLIMIT_MSGQUEUE] = { 819200, 819200 },
4761 [RLIMIT_NICE] = { 0, 0 },
4762 [RLIMIT_NOFILE] = { 1024, 4096 },
4763 [RLIMIT_RSS] = { RLIM_INFINITY, RLIM_INFINITY },
4764 [RLIMIT_RTPRIO] = { 0, 0 },
4765 [RLIMIT_RTTIME] = { RLIM_INFINITY, RLIM_INFINITY },
4766 [RLIMIT_STACK] = { 8388608, RLIM_INFINITY },
4767
4768 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
4769 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
4770 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
4771 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
4772 * that PID 1 changes a number of other resource limits during early initialization which is why we
4773 * don't read the other limits from PID 1 but prefer the static table above. */
4774 };
4775
4776 int rl;
4777
4778 for (rl = 0; rl < _RLIMIT_MAX; rl++) {
bf428efb
LP
4779 /* Let's only fill in what the user hasn't explicitly configured anyway */
4780 if ((arg_settings_mask & (SETTING_RLIMIT_FIRST << rl)) == 0) {
4781 const struct rlimit *v;
4782 struct rlimit buffer;
4783
4784 if (IN_SET(rl, RLIMIT_NPROC, RLIMIT_SIGPENDING)) {
4785 /* For these two let's read the limits off PID 1. See above for an explanation. */
4786
4787 if (prlimit(1, rl, NULL, &buffer) < 0)
4788 return log_error_errno(errno, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl));
4789
4790 v = &buffer;
4791 } else
4792 v = kernel_defaults + rl;
4793
4794 arg_rlimit[rl] = newdup(struct rlimit, v, 1);
4795 if (!arg_rlimit[rl])
4796 return log_oom();
4797 }
4798
4799 if (DEBUG_LOGGING) {
4800 _cleanup_free_ char *k = NULL;
4801
4802 (void) rlimit_format(arg_rlimit[rl], &k);
4803 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl), k);
4804 }
4805 }
4806
4807 return 0;
4808}
4809
287b7376
LP
4810static int cant_be_in_netns(void) {
4811 union sockaddr_union sa = {
4812 .un = {
4813 .sun_family = AF_UNIX,
4814 .sun_path = "/run/udev/control",
4815 },
4816 };
4817 char udev_path[STRLEN("/proc//ns/net") + DECIMAL_STR_MAX(pid_t)];
4818 _cleanup_free_ char *udev_ns = NULL, *our_ns = NULL;
4819 _cleanup_close_ int fd = -1;
4820 struct ucred ucred;
4821 int r;
4822
4823 /* Check if we are in the same netns as udev. If we aren't, then device monitoring (and thus waiting
4824 * for loopback block devices) won't work, and we will hang. Detect this case and exit early with a
4825 * nice message. */
4826
4827 if (!arg_image) /* only matters if --image= us used, i.e. we actually need to use loopback devices */
4828 return 0;
4829
4830 fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
4831 if (fd < 0)
4832 return log_error_errno(errno, "Failed to allocate udev control socket: %m");
4833
4834 if (connect(fd, &sa.un, SOCKADDR_UN_LEN(sa.un)) < 0) {
4835
4836 if (errno == ENOENT || ERRNO_IS_DISCONNECT(errno))
4837 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4838 "Sorry, but --image= requires access to the host's /run/ hierarchy, since we need access to udev.");
4839
4840 return log_error_errno(errno, "Failed to connect socket to udev control socket: %m");
4841 }
4842
4843 r = getpeercred(fd, &ucred);
4844 if (r < 0)
4845 return log_error_errno(r, "Failed to determine peer of udev control socket: %m");
4846
4847 xsprintf(udev_path, "/proc/" PID_FMT "/ns/net", ucred.pid);
4848 r = readlink_malloc(udev_path, &udev_ns);
4849 if (r < 0)
4850 return log_error_errno(r, "Failed to read network namespace of udev: %m");
4851
4852 r = readlink_malloc("/proc/self/ns/net", &our_ns);
4853 if (r < 0)
4854 return log_error_errno(r, "Failed to read our own network namespace: %m");
4855
4856 if (!streq(our_ns, udev_ns))
4857 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4858 "Sorry, but --image= is only supported in the main network namespace, since we need access to udev/AF_NETLINK.");
4859 return 0;
4860}
4861
44dbef90 4862static int run(int argc, char *argv[]) {
7bf011e3
LP
4863 bool secondary = false, remove_directory = false, remove_image = false,
4864 veth_created = false, remove_tmprootdir = false;
2d845785 4865 _cleanup_close_ int master = -1;
03cfe0d5 4866 _cleanup_fdset_free_ FDSet *fds = NULL;
2d845785 4867 int r, n_fd_passed, ret = EXIT_SUCCESS;
5aa3eba5 4868 char veth_name[IFNAMSIZ] = "";
03cfe0d5 4869 union in_addr_union exposed = {};
8e766630 4870 _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
c67b0082 4871 char tmprootdir[] = "/tmp/nspawn-root-XXXXXX";
2d845785 4872 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
18b5886e
LP
4873 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
4874 _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL;
7bf011e3 4875 pid_t pid = 0;
03cfe0d5
LP
4876
4877 log_parse_environment();
4878 log_open();
415fc41c 4879
03cfe0d5
LP
4880 r = parse_argv(argc, argv);
4881 if (r <= 0)
4882 goto finish;
4883
fba868fa
LP
4884 r = must_be_root();
4885 if (r < 0)
03cfe0d5 4886 goto finish;
fba868fa 4887
287b7376
LP
4888 r = cant_be_in_netns();
4889 if (r < 0)
4890 goto finish;
4891
bf428efb
LP
4892 r = initialize_rlimits();
4893 if (r < 0)
4894 goto finish;
4895
de40a303
LP
4896 r = load_oci_bundle();
4897 if (r < 0)
4898 goto finish;
4899
f757855e
LP
4900 r = determine_names();
4901 if (r < 0)
4902 goto finish;
4903
4904 r = load_settings();
4905 if (r < 0)
4906 goto finish;
4907
d4d99bc6 4908 r = cg_unified();
5eee8290
LP
4909 if (r < 0) {
4910 log_error_errno(r, "Failed to determine whether the unified cgroups hierarchy is used: %m");
4911 goto finish;
4912 }
4913
f757855e
LP
4914 r = verify_arguments();
4915 if (r < 0)
4916 goto finish;
03cfe0d5 4917
49048684
ZJS
4918 /* Reapply environment settings. */
4919 (void) detect_unified_cgroup_hierarchy_from_environment();
8199d554 4920
2949ff26
LP
4921 /* Ignore SIGPIPE here, because we use splice() on the ptyfwd stuff and that will generate SIGPIPE if
4922 * the result is closed. Note that the container payload child will reset signal mask+handler anyway,
4923 * so just turning this off here means we only turn it off in nspawn itself, not any children. */
4924 (void) ignore_signals(SIGPIPE, -1);
4925
03cfe0d5
LP
4926 n_fd_passed = sd_listen_fds(false);
4927 if (n_fd_passed > 0) {
4928 r = fdset_new_listen_fds(&fds, false);
4929 if (r < 0) {
4930 log_error_errno(r, "Failed to collect file descriptors: %m");
4931 goto finish;
4932 }
4933 }
4934
83e803a9
ZJS
4935 /* The "default" umask. This is appropriate for most file and directory
4936 * operations performed by nspawn, and is the umask that will be used for
4937 * the child. Functions like copy_devnodes() change the umask temporarily. */
4938 umask(0022);
4939
03cfe0d5
LP
4940 if (arg_directory) {
4941 assert(!arg_image);
4942
b35ca61a
LP
4943 /* Safety precaution: let's not allow running images from the live host OS image, as long as
4944 * /var from the host will propagate into container dynamically (because bad things happen if
4945 * two systems write to the same /var). Let's allow it for the special cases where /var is
4946 * either copied (i.e. --ephemeral) or replaced (i.e. --volatile=yes|state). */
4947 if (path_equal(arg_directory, "/") && !(arg_ephemeral || IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_STATE))) {
4948 log_error("Spawning container on root directory is not supported. Consider using --ephemeral, --volatile=yes or --volatile=state.");
03cfe0d5
LP
4949 r = -EINVAL;
4950 goto finish;
4951 }
4952
4953 if (arg_ephemeral) {
4954 _cleanup_free_ char *np = NULL;
4955
8d4aa2bb 4956 r = chase_symlinks_and_update(&arg_directory, 0);
3f342ec4
LP
4957 if (r < 0)
4958 goto finish;
4959
7bf011e3
LP
4960 /* If the specified path is a mount point we generate the new snapshot immediately
4961 * inside it under a random name. However if the specified is not a mount point we
4962 * create the new snapshot in the parent directory, just next to it. */
e1873695 4963 r = path_is_mount_point(arg_directory, NULL, 0);
03cfe0d5
LP
4964 if (r < 0) {
4965 log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
4966 goto finish;
4967 }
4968 if (r > 0)
770b5ce4 4969 r = tempfn_random_child(arg_directory, "machine.", &np);
03cfe0d5 4970 else
770b5ce4 4971 r = tempfn_random(arg_directory, "machine.", &np);
03cfe0d5 4972 if (r < 0) {
0f3be6ca 4973 log_error_errno(r, "Failed to generate name for directory snapshot: %m");
03cfe0d5
LP
4974 goto finish;
4975 }
4976
6992459c 4977 /* We take an exclusive lock on this image, since it's our private, ephemeral copy
162392b7 4978 * only owned by us and no one else. */
6992459c 4979 r = image_path_lock(np, LOCK_EX|LOCK_NB, &tree_global_lock, &tree_local_lock);
03cfe0d5
LP
4980 if (r < 0) {
4981 log_error_errno(r, "Failed to lock %s: %m", np);
4982 goto finish;
4983 }
4984
7bf011e3
LP
4985 {
4986 BLOCK_SIGNALS(SIGINT);
4987 r = btrfs_subvol_snapshot(arg_directory, np,
4988 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
4989 BTRFS_SNAPSHOT_FALLBACK_COPY |
4990 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
4991 BTRFS_SNAPSHOT_RECURSIVE |
4992 BTRFS_SNAPSHOT_QUOTA |
4993 BTRFS_SNAPSHOT_SIGINT);
4994 }
4995 if (r == -EINTR) {
4996 log_error_errno(r, "Interrupted while copying file system tree to %s, removed again.", np);
4997 goto finish;
4998 }
03cfe0d5
LP
4999 if (r < 0) {
5000 log_error_errno(r, "Failed to create snapshot %s from %s: %m", np, arg_directory);
5001 goto finish;
ec16945e
LP
5002 }
5003
1cc6c93a 5004 free_and_replace(arg_directory, np);
17cbb288 5005 remove_directory = true;
30535c16 5006 } else {
cb638b5e 5007 r = chase_symlinks_and_update(&arg_directory, arg_template ? CHASE_NONEXISTENT : 0);
8d4aa2bb
LP
5008 if (r < 0)
5009 goto finish;
5010
30535c16
LP
5011 r = image_path_lock(arg_directory, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
5012 if (r == -EBUSY) {
5013 log_error_errno(r, "Directory tree %s is currently busy.", arg_directory);
5014 goto finish;
5015 }
5016 if (r < 0) {
5017 log_error_errno(r, "Failed to lock %s: %m", arg_directory);
476b8254 5018 goto finish;
30535c16
LP
5019 }
5020
5021 if (arg_template) {
8d4aa2bb 5022 r = chase_symlinks_and_update(&arg_template, 0);
3f342ec4
LP
5023 if (r < 0)
5024 goto finish;
5025
7bf011e3
LP
5026 {
5027 BLOCK_SIGNALS(SIGINT);
5028 r = btrfs_subvol_snapshot(arg_template, arg_directory,
5029 (arg_read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
5030 BTRFS_SNAPSHOT_FALLBACK_COPY |
5031 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
5032 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE |
5033 BTRFS_SNAPSHOT_RECURSIVE |
5034 BTRFS_SNAPSHOT_QUOTA |
5035 BTRFS_SNAPSHOT_SIGINT);
5036 }
ff6c6cc1
LP
5037 if (r == -EEXIST)
5038 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
5039 "Directory %s already exists, not populating from template %s.", arg_directory, arg_template);
7bf011e3
LP
5040 else if (r == -EINTR) {
5041 log_error_errno(r, "Interrupted while copying file system tree to %s, removed again.", arg_directory);
5042 goto finish;
5043 } else if (r < 0) {
83521414 5044 log_error_errno(r, "Couldn't create snapshot %s from %s: %m", arg_directory, arg_template);
30535c16 5045 goto finish;
ff6c6cc1
LP
5046 } else
5047 log_full(arg_quiet ? LOG_DEBUG : LOG_INFO,
5048 "Populated %s from template %s.", arg_directory, arg_template);
30535c16 5049 }
ec16945e
LP
5050 }
5051
7732f92b 5052 if (arg_start_mode == START_BOOT) {
a5201ed6 5053 const char *p;
c9fe05e0 5054
a5201ed6
LP
5055 if (arg_pivot_root_new)
5056 p = prefix_roota(arg_directory, arg_pivot_root_new);
5057 else
5058 p = arg_directory;
c9fe05e0
AR
5059
5060 if (path_is_os_tree(p) <= 0) {
5061 log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p);
ec16945e 5062 r = -EINVAL;
1b9e5b12
LP
5063 goto finish;
5064 }
5065 } else {
c9fe05e0
AR
5066 const char *p, *q;
5067
a5201ed6
LP
5068 if (arg_pivot_root_new)
5069 p = prefix_roota(arg_directory, arg_pivot_root_new);
5070 else
5071 p = arg_directory;
c9fe05e0
AR
5072
5073 q = strjoina(p, "/usr/");
1b9e5b12 5074
c9fe05e0
AR
5075 if (laccess(q, F_OK) < 0) {
5076 log_error("Directory %s doesn't look like it has an OS tree. Refusing.", p);
ec16945e 5077 r = -EINVAL;
1b9e5b12 5078 goto finish;
1b9e5b12
LP
5079 }
5080 }
ec16945e 5081
6b9132a9 5082 } else {
ec16945e
LP
5083 assert(arg_image);
5084 assert(!arg_template);
5085
8d4aa2bb 5086 r = chase_symlinks_and_update(&arg_image, 0);
3f342ec4
LP
5087 if (r < 0)
5088 goto finish;
5089
0f3be6ca
LP
5090 if (arg_ephemeral) {
5091 _cleanup_free_ char *np = NULL;
5092
5093 r = tempfn_random(arg_image, "machine.", &np);
5094 if (r < 0) {
5095 log_error_errno(r, "Failed to generate name for image snapshot: %m");
5096 goto finish;
5097 }
5098
6992459c
LP
5099 /* Always take an exclusive lock on our own ephemeral copy. */
5100 r = image_path_lock(np, LOCK_EX|LOCK_NB, &tree_global_lock, &tree_local_lock);
0f3be6ca
LP
5101 if (r < 0) {
5102 r = log_error_errno(r, "Failed to create image lock: %m");
5103 goto finish;
5104 }
5105
7bf011e3
LP
5106 {
5107 BLOCK_SIGNALS(SIGINT);
5108 r = copy_file(arg_image, np, O_EXCL, arg_read_only ? 0400 : 0600, FS_NOCOW_FL, FS_NOCOW_FL, COPY_REFLINK|COPY_CRTIME|COPY_SIGINT);
5109 }
5110 if (r == -EINTR) {
5111 log_error_errno(r, "Interrupted while copying image file to %s, removed again.", np);
5112 goto finish;
5113 }
0f3be6ca
LP
5114 if (r < 0) {
5115 r = log_error_errno(r, "Failed to copy image file: %m");
5116 goto finish;
5117 }
5118
1cc6c93a 5119 free_and_replace(arg_image, np);
0f3be6ca
LP
5120 remove_image = true;
5121 } else {
5122 r = image_path_lock(arg_image, (arg_read_only ? LOCK_SH : LOCK_EX) | LOCK_NB, &tree_global_lock, &tree_local_lock);
5123 if (r == -EBUSY) {
5124 r = log_error_errno(r, "Disk image %s is currently busy.", arg_image);
5125 goto finish;
5126 }
5127 if (r < 0) {
5128 r = log_error_errno(r, "Failed to create image lock: %m");
5129 goto finish;
5130 }
4623e8e6 5131
78ebe980
LP
5132 if (!arg_root_hash) {
5133 r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size);
5134 if (r < 0) {
5135 log_error_errno(r, "Failed to load root hash file for %s: %m", arg_image);
5136 goto finish;
5137 }
5138 }
30535c16
LP
5139 }
5140
c67b0082 5141 if (!mkdtemp(tmprootdir)) {
0f3be6ca 5142 r = log_error_errno(errno, "Failed to create temporary directory: %m");
6b9132a9 5143 goto finish;
1b9e5b12 5144 }
6b9132a9 5145
c67b0082
LP
5146 remove_tmprootdir = true;
5147
5148 arg_directory = strdup(tmprootdir);
1b9e5b12
LP
5149 if (!arg_directory) {
5150 r = log_oom();
5151 goto finish;
6b9132a9 5152 }
88213476 5153
e08f94ac 5154 r = loop_device_make_by_path(arg_image, arg_read_only ? O_RDONLY : O_RDWR, LO_FLAGS_PARTSCAN, &loop);
2d845785
LP
5155 if (r < 0) {
5156 log_error_errno(r, "Failed to set up loopback block device: %m");
842f3b0f
LP
5157 goto finish;
5158 }
1b9e5b12 5159
4526113f 5160 r = dissect_image_and_warn(
e0f9e7bd 5161 loop->fd,
4526113f 5162 arg_image,
e0f9e7bd 5163 arg_root_hash, arg_root_hash_size,
d4dffb85 5164 DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_RELAX_VAR_CHECK,
e0f9e7bd 5165 &dissected_image);
2d845785 5166 if (r == -ENOPKG) {
4526113f 5167 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
2d845785
LP
5168 log_notice("Note that the disk image needs to\n"
5169 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
5170 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
19ac32cd 5171 " c) or follow https://systemd.io/DISCOVERABLE_PARTITIONS\n"
2d845785
LP
5172 " d) or contain a file system without a partition table\n"
5173 "in order to be bootable with systemd-nspawn.");
1b9e5b12 5174 goto finish;
2d845785 5175 }
4526113f 5176 if (r < 0)
842f3b0f 5177 goto finish;
1b9e5b12 5178
4623e8e6
LP
5179 if (!arg_root_hash && dissected_image->can_verity)
5180 log_notice("Note: image %s contains verity information, but no root hash specified! Proceeding without integrity checking.", arg_image);
5181
5182 r = dissected_image_decrypt_interactively(dissected_image, NULL, arg_root_hash, arg_root_hash_size, 0, &decrypted_image);
1b9e5b12
LP
5183 if (r < 0)
5184 goto finish;
0f3be6ca
LP
5185
5186 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
5187 if (remove_image && unlink(arg_image) >= 0)
5188 remove_image = false;
842f3b0f 5189 }
842f3b0f 5190
86c0dd4a 5191 r = custom_mount_prepare_all(arg_directory, arg_custom_mounts, arg_n_custom_mounts);
5a8af538
LP
5192 if (r < 0)
5193 goto finish;
5194
de40a303
LP
5195 if (arg_console_mode < 0)
5196 arg_console_mode =
5197 isatty(STDIN_FILENO) > 0 &&
5198 isatty(STDOUT_FILENO) > 0 ? CONSOLE_INTERACTIVE : CONSOLE_READ_ONLY;
9c857b9d 5199
de40a303
LP
5200 if (arg_console_mode == CONSOLE_PIPE) /* if we pass STDERR on to the container, don't add our own logs into it too */
5201 arg_quiet = true;
a258bf26 5202
9c857b9d
LP
5203 if (!arg_quiet)
5204 log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
5205 arg_machine, arg_image ?: arg_directory);
5206
72c0a2c2 5207 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
a258bf26 5208
66edd963 5209 if (prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) < 0) {
03cfe0d5
LP
5210 r = log_error_errno(errno, "Failed to become subreaper: %m");
5211 goto finish;
5212 }
5213
d87be9b0 5214 for (;;) {
3acc84eb 5215 r = run_container(dissected_image,
44dbef90
LP
5216 secondary,
5217 fds,
5218 veth_name, &veth_created,
3acc84eb 5219 &exposed, &master,
44dbef90 5220 &pid, &ret);
b0067625 5221 if (r <= 0)
d87be9b0 5222 break;
d87be9b0 5223 }
88213476
LP
5224
5225finish:
04f590a4
LP
5226 (void) sd_notify(false,
5227 r == 0 && ret == EXIT_FORCE_RESTART ? "STOPPING=1\nSTATUS=Restarting..." :
5228 "STOPPING=1\nSTATUS=Terminating...");
af4ec430 5229
9444b1f2 5230 if (pid > 0)
c67b0082 5231 (void) kill(pid, SIGKILL);
88213476 5232
503546da 5233 /* Try to flush whatever is still queued in the pty */
6a0f896b 5234 if (master >= 0) {
1c876927 5235 (void) copy_bytes(master, STDOUT_FILENO, (uint64_t) -1, 0);
6a0f896b
LP
5236 master = safe_close(master);
5237 }
5238
5239 if (pid > 0)
5240 (void) wait_for_terminate(pid, NULL);
503546da 5241
50ebcf6c
LP
5242 pager_close();
5243
17cbb288 5244 if (remove_directory && arg_directory) {
ec16945e
LP
5245 int k;
5246
17cbb288 5247 k = rm_rf(arg_directory, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
ec16945e 5248 if (k < 0)
17cbb288 5249 log_warning_errno(k, "Cannot remove '%s', ignoring: %m", arg_directory);
ec16945e
LP
5250 }
5251
0f3be6ca
LP
5252 if (remove_image && arg_image) {
5253 if (unlink(arg_image) < 0)
5254 log_warning_errno(errno, "Can't remove image file '%s', ignoring: %m", arg_image);
5255 }
5256
c67b0082
LP
5257 if (remove_tmprootdir) {
5258 if (rmdir(tmprootdir) < 0)
5259 log_debug_errno(errno, "Can't remove temporary root directory '%s', ignoring: %m", tmprootdir);
5260 }
5261
785890ac
LP
5262 if (arg_machine) {
5263 const char *p;
5264
63c372cb 5265 p = strjoina("/run/systemd/nspawn/propagate/", arg_machine);
c6878637 5266 (void) rm_rf(p, REMOVE_ROOT);
785890ac
LP
5267 }
5268
7a8f6325 5269 expose_port_flush(arg_expose_ports, &exposed);
7513c5b8
LP
5270
5271 if (veth_created)
5272 (void) remove_veth_links(veth_name, arg_network_veth_extra);
22b28dfd 5273 (void) remove_bridge(arg_network_zone);
f757855e 5274
f757855e
LP
5275 custom_mount_free_all(arg_custom_mounts, arg_n_custom_mounts);
5276 expose_port_free_all(arg_expose_ports);
bf428efb 5277 rlimit_free_all(arg_rlimit);
b2645747 5278 device_node_array_free(arg_extra_nodes, arg_n_extra_nodes);
6d0b55c2 5279
44dbef90
LP
5280 if (r < 0)
5281 return r;
5282
5283 return ret;
88213476 5284}
44dbef90
LP
5285
5286DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);