1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/loop.h>
9 #include <sys/keyctl.h>
10 #include <sys/mount.h>
11 #include <sys/personality.h>
12 #include <sys/prctl.h>
17 #include <selinux/selinux.h>
21 #include "sd-daemon.h"
24 #include "sd-netlink.h"
26 #include "alloc-util.h"
28 #include "base-filesystem.h"
29 #include "btrfs-util.h"
31 #include "bus-error.h"
32 #include "bus-locator.h"
34 #include "capability-list.h"
35 #include "capability-util.h"
36 #include "cgroup-setup.h"
38 #include "common-signal.h"
39 #include "constants.h"
41 #include "cpu-set-util.h"
42 #include "daemon-util.h"
43 #include "dev-setup.h"
44 #include "devnum-util.h"
45 #include "discover-image.h"
46 #include "dissect-image.h"
49 #include "ether-addr-util.h"
50 #include "extract-word.h"
54 #include "format-util.h"
57 #include "hexdecoct.h"
58 #include "hostname-setup.h"
59 #include "hostname-util.h"
60 #include "id128-util.h"
61 #include "image-policy.h"
62 #include "in-addr-util.h"
65 #include "loop-util.h"
66 #include "loopback-setup.h"
67 #include "machine-bind-user.h"
68 #include "machine-credential.h"
69 #include "main-func.h"
71 #include "mount-util.h"
72 #include "mountpoint-util.h"
73 #include "namespace-util.h"
74 #include "notify-recv.h"
75 #include "nspawn-bind-user.h"
76 #include "nspawn-cgroup.h"
77 #include "nspawn-expose-ports.h"
78 #include "nspawn-mount.h"
79 #include "nspawn-network.h"
80 #include "nspawn-oci.h"
81 #include "nspawn-register.h"
82 #include "nspawn-seccomp.h"
83 #include "nspawn-settings.h"
84 #include "nspawn-setuid.h"
85 #include "nspawn-stub-pid1.h"
87 #include "nsresource.h"
89 #include "osc-context.h"
91 #include "parse-argument.h"
92 #include "parse-util.h"
93 #include "path-util.h"
95 #include "polkit-agent.h"
96 #include "pretty-print.h"
97 #include "process-util.h"
99 #include "random-util.h"
100 #include "raw-clone.h"
101 #include "resolve-util.h"
102 #include "rlimit-util.h"
104 #include "runtime-scope.h"
105 #include "seccomp-util.h"
106 #include "shift-uid.h"
107 #include "signal-util.h"
108 #include "siphash24.h"
109 #include "socket-util.h"
110 #include "stat-util.h"
111 #include "stdio-util.h"
112 #include "string-table.h"
113 #include "string-util.h"
115 #include "sysctl-util.h"
116 #include "terminal-util.h"
117 #include "tmpfile-util.h"
118 #include "uid-classification.h"
119 #include "umask-util.h"
120 #include "unit-name.h"
121 #include "user-record.h"
122 #include "user-util.h"
125 /* The notify socket inside the container it can use to talk to nspawn using the sd_notify(3) protocol */
126 #define NSPAWN_NOTIFY_SOCKET_PATH "/run/host/notify"
127 #define NSPAWN_MOUNT_TUNNEL "/run/host/incoming"
129 #define EXIT_FORCE_RESTART 133
131 typedef enum ContainerStatus
{
132 CONTAINER_TERMINATED
,
136 static char *arg_directory
= NULL
;
137 static char *arg_template
= NULL
;
138 static char *arg_chdir
= NULL
;
139 static char *arg_pivot_root_new
= NULL
;
140 static char *arg_pivot_root_old
= NULL
;
141 static char *arg_user
= NULL
;
142 static uid_t arg_uid
= UID_INVALID
;
143 static gid_t arg_gid
= GID_INVALID
;
144 static gid_t
* arg_supplementary_gids
= NULL
;
145 static size_t arg_n_supplementary_gids
= 0;
146 static sd_id128_t arg_uuid
= {};
147 static char *arg_machine
= NULL
; /* The name used by the host to refer to this */
148 static char *arg_hostname
= NULL
; /* The name the payload sees by default */
149 static const char *arg_selinux_context
= NULL
;
150 static const char *arg_selinux_apifs_context
= NULL
;
151 static char *arg_slice
= NULL
;
152 static bool arg_private_network
; /* initialized depending on arg_privileged in run() */
153 static bool arg_read_only
= false;
154 static StartMode arg_start_mode
= START_PID1
;
155 static bool arg_ephemeral
= false;
156 static LinkJournal arg_link_journal
= LINK_AUTO
;
157 static bool arg_link_journal_try
= false;
158 static uint64_t arg_caps_retain
=
159 (1ULL << CAP_AUDIT_CONTROL
) |
160 (1ULL << CAP_AUDIT_WRITE
) |
161 (1ULL << CAP_CHOWN
) |
162 (1ULL << CAP_DAC_OVERRIDE
) |
163 (1ULL << CAP_DAC_READ_SEARCH
) |
164 (1ULL << CAP_FOWNER
) |
165 (1ULL << CAP_FSETID
) |
166 (1ULL << CAP_IPC_OWNER
) |
168 (1ULL << CAP_LEASE
) |
169 (1ULL << CAP_LINUX_IMMUTABLE
) |
170 (1ULL << CAP_MKNOD
) |
171 (1ULL << CAP_NET_BIND_SERVICE
) |
172 (1ULL << CAP_NET_BROADCAST
) |
173 (1ULL << CAP_NET_RAW
) |
174 (1ULL << CAP_SETFCAP
) |
175 (1ULL << CAP_SETGID
) |
176 (1ULL << CAP_SETPCAP
) |
177 (1ULL << CAP_SETUID
) |
178 (1ULL << CAP_SYS_ADMIN
) |
179 (1ULL << CAP_SYS_BOOT
) |
180 (1ULL << CAP_SYS_CHROOT
) |
181 (1ULL << CAP_SYS_NICE
) |
182 (1ULL << CAP_SYS_PTRACE
) |
183 (1ULL << CAP_SYS_RESOURCE
) |
184 (1ULL << CAP_SYS_TTY_CONFIG
);
185 static uint64_t arg_caps_ambient
= 0;
186 static CapabilityQuintet arg_full_capabilities
= CAPABILITY_QUINTET_NULL
;
187 static CustomMount
*arg_custom_mounts
= NULL
;
188 static size_t arg_n_custom_mounts
= 0;
189 static char **arg_setenv
= NULL
;
190 static bool arg_quiet
= false;
191 static bool arg_register
= true;
192 static bool arg_keep_unit
= false;
193 static char **arg_network_interfaces
= NULL
;
194 static char **arg_network_macvlan
= NULL
;
195 static char **arg_network_ipvlan
= NULL
;
196 static bool arg_network_veth
= false;
197 static char **arg_network_veth_extra
= NULL
;
198 static char *arg_network_bridge
= NULL
;
199 static char *arg_network_zone
= NULL
;
200 static char *arg_network_namespace_path
= NULL
;
201 struct ether_addr arg_network_provided_mac
= {};
202 static PagerFlags arg_pager_flags
= 0;
203 static unsigned long arg_personality
= PERSONALITY_INVALID
;
204 static char *arg_image
= NULL
;
205 static char *arg_oci_bundle
= NULL
;
206 static VolatileMode arg_volatile_mode
= VOLATILE_NO
;
207 static ExposePort
*arg_expose_ports
= NULL
;
208 static char **arg_property
= NULL
;
209 static sd_bus_message
*arg_property_message
= NULL
;
210 static UserNamespaceMode arg_userns_mode
; /* initialized depending on arg_privileged in run() */
211 static uid_t arg_uid_shift
= UID_INVALID
, arg_uid_range
= 0x10000U
;
212 static UserNamespaceOwnership arg_userns_ownership
= _USER_NAMESPACE_OWNERSHIP_INVALID
;
213 static int arg_kill_signal
= 0;
214 static SettingsMask arg_settings_mask
= 0;
215 static int arg_settings_trusted
= -1;
216 static char **arg_parameters
= NULL
;
217 static const char *arg_container_service_name
= "systemd-nspawn";
218 static bool arg_notify_ready
= false;
219 static bool arg_use_cgns
= true;
220 static unsigned long arg_clone_ns_flags
= CLONE_NEWIPC
|CLONE_NEWPID
|CLONE_NEWUTS
;
221 static MountSettingsMask arg_mount_settings
= MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_TMPFS_TMP
;
222 static VeritySettings arg_verity_settings
= VERITY_SETTINGS_DEFAULT
;
223 static char **arg_syscall_allow_list
= NULL
;
224 static char **arg_syscall_deny_list
= NULL
;
226 static scmp_filter_ctx arg_seccomp
= NULL
;
228 static struct rlimit
*arg_rlimit
[_RLIMIT_MAX
] = {};
229 static bool arg_no_new_privileges
= false;
230 static int arg_oom_score_adjust
= 0;
231 static bool arg_oom_score_adjust_set
= false;
232 static CPUSet arg_cpu_set
= {};
233 static ResolvConfMode arg_resolv_conf
= RESOLV_CONF_AUTO
;
234 static TimezoneMode arg_timezone
= TIMEZONE_AUTO
;
235 static unsigned arg_console_width
= UINT_MAX
, arg_console_height
= UINT_MAX
;
236 static DeviceNode
* arg_extra_nodes
= NULL
;
237 static size_t arg_n_extra_nodes
= 0;
238 static char **arg_sysctl
= NULL
;
239 static ConsoleMode arg_console_mode
= _CONSOLE_MODE_INVALID
;
240 static MachineCredentialContext arg_credentials
= {};
241 static char **arg_bind_user
= NULL
;
242 static char *arg_bind_user_shell
= NULL
;
243 static bool arg_bind_user_shell_copy
= false;
244 static bool arg_suppress_sync
= false;
245 static char *arg_settings_filename
= NULL
;
246 static Architecture arg_architecture
= _ARCHITECTURE_INVALID
;
247 static ImagePolicy
*arg_image_policy
= NULL
;
248 static char *arg_background
= NULL
;
249 static bool arg_privileged
= false;
250 static bool arg_cleanup
= false;
251 static bool arg_ask_password
= true;
253 STATIC_DESTRUCTOR_REGISTER(arg_directory
, freep
);
254 STATIC_DESTRUCTOR_REGISTER(arg_template
, freep
);
255 STATIC_DESTRUCTOR_REGISTER(arg_chdir
, freep
);
256 STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_new
, freep
);
257 STATIC_DESTRUCTOR_REGISTER(arg_pivot_root_old
, freep
);
258 STATIC_DESTRUCTOR_REGISTER(arg_user
, freep
);
259 STATIC_DESTRUCTOR_REGISTER(arg_supplementary_gids
, freep
);
260 STATIC_DESTRUCTOR_REGISTER(arg_machine
, freep
);
261 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
262 STATIC_DESTRUCTOR_REGISTER(arg_slice
, freep
);
263 STATIC_DESTRUCTOR_REGISTER(arg_setenv
, strv_freep
);
264 STATIC_DESTRUCTOR_REGISTER(arg_network_interfaces
, strv_freep
);
265 STATIC_DESTRUCTOR_REGISTER(arg_network_macvlan
, strv_freep
);
266 STATIC_DESTRUCTOR_REGISTER(arg_network_ipvlan
, strv_freep
);
267 STATIC_DESTRUCTOR_REGISTER(arg_network_veth_extra
, strv_freep
);
268 STATIC_DESTRUCTOR_REGISTER(arg_network_bridge
, freep
);
269 STATIC_DESTRUCTOR_REGISTER(arg_network_zone
, freep
);
270 STATIC_DESTRUCTOR_REGISTER(arg_network_namespace_path
, freep
);
271 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
272 STATIC_DESTRUCTOR_REGISTER(arg_oci_bundle
, freep
);
273 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
274 STATIC_DESTRUCTOR_REGISTER(arg_property_message
, sd_bus_message_unrefp
);
275 STATIC_DESTRUCTOR_REGISTER(arg_parameters
, strv_freep
);
276 STATIC_DESTRUCTOR_REGISTER(arg_verity_settings
, verity_settings_done
);
277 STATIC_DESTRUCTOR_REGISTER(arg_syscall_allow_list
, strv_freep
);
278 STATIC_DESTRUCTOR_REGISTER(arg_syscall_deny_list
, strv_freep
);
280 STATIC_DESTRUCTOR_REGISTER(arg_seccomp
, seccomp_releasep
);
282 STATIC_DESTRUCTOR_REGISTER(arg_credentials
, machine_credential_context_done
);
283 STATIC_DESTRUCTOR_REGISTER(arg_cpu_set
, cpu_set_done
);
284 STATIC_DESTRUCTOR_REGISTER(arg_sysctl
, strv_freep
);
285 STATIC_DESTRUCTOR_REGISTER(arg_bind_user
, strv_freep
);
286 STATIC_DESTRUCTOR_REGISTER(arg_bind_user_shell
, freep
);
287 STATIC_DESTRUCTOR_REGISTER(arg_settings_filename
, freep
);
288 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
289 STATIC_DESTRUCTOR_REGISTER(arg_background
, freep
);
291 static int handle_arg_console(const char *arg
) {
292 if (streq(arg
, "help")) {
301 if (streq(arg
, "interactive"))
302 arg_console_mode
= CONSOLE_INTERACTIVE
;
303 else if (streq(arg
, "read-only"))
304 arg_console_mode
= CONSOLE_READ_ONLY
;
305 else if (streq(arg
, "passive"))
306 arg_console_mode
= CONSOLE_PASSIVE
;
307 else if (streq(arg
, "pipe")) {
308 if (isatty_safe(STDIN_FILENO
) && isatty_safe(STDOUT_FILENO
))
309 log_full(arg_quiet
? LOG_DEBUG
: LOG_NOTICE
,
310 "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. "
311 "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. "
312 "Proceeding anyway.");
314 arg_console_mode
= CONSOLE_PIPE
;
315 } else if (streq(arg
, "autopipe")) {
316 if (isatty_safe(STDIN_FILENO
) && isatty_safe(STDOUT_FILENO
))
317 arg_console_mode
= CONSOLE_INTERACTIVE
;
319 arg_console_mode
= CONSOLE_PIPE
;
321 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown console mode: %s", optarg
);
323 arg_settings_mask
|= SETTING_CONSOLE_MODE
;
327 static int help(void) {
328 _cleanup_free_
char *link
= NULL
;
331 pager_open(arg_pager_flags
);
333 r
= terminal_urlify_man("systemd-nspawn", "1", &link
);
337 printf("%1$s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
338 "%5$sSpawn a command or OS in a lightweight container.%6$s\n\n"
339 " -h --help Show this help\n"
340 " --version Print version string\n"
341 " -q --quiet Do not show status information\n"
342 " --no-pager Do not pipe output into a pager\n"
343 " --settings=BOOLEAN Load additional settings from .nspawn file\n"
344 " --cleanup Clean up left-over mounts and underlying mount\n"
345 " points used by the container\n"
346 " --no-ask-password Do not prompt for password\n"
348 " -D --directory=PATH Root directory for the container\n"
349 " --template=PATH Initialize root directory from template directory,\n"
351 " -x --ephemeral Run container with snapshot of root directory, and\n"
352 " remove it after exit\n"
353 " -i --image=PATH Root file system disk image (or device node) for\n"
355 " --image-policy=POLICY Specify disk image dissection policy\n"
356 " --oci-bundle=PATH OCI bundle directory\n"
357 " --read-only Mount the root directory read-only\n"
358 " --volatile[=MODE] Run the system in volatile mode\n"
359 " --root-hash=HASH Specify verity root hash for root disk image\n"
360 " --root-hash-sig=SIG Specify pkcs7 signature of root hash for verity\n"
361 " as a DER encoded PKCS7, either as a path to a file\n"
362 " or as an ASCII base64 encoded string prefixed by\n"
364 " --verity-data=PATH Specify hash device for verity\n"
365 " --pivot-root=PATH[:PATH]\n"
366 " Pivot root to given directory in the container\n"
367 "\n%3$sExecution:%4$s\n"
368 " -a --as-pid2 Maintain a stub init as PID1, invoke binary as PID2\n"
369 " -b --boot Boot up full system (i.e. invoke init)\n"
370 " --chdir=PATH Set working directory in the container\n"
371 " -E --setenv=NAME[=VALUE] Pass an environment variable to PID 1\n"
372 " -u --user=USER Run the command under specified user or UID\n"
373 " --kill-signal=SIGNAL Select signal to use for shutting down PID 1\n"
374 " --notify-ready=BOOLEAN Receive notifications from the child init process\n"
375 " --suppress-sync=BOOLEAN\n"
376 " Suppress any form of disk data synchronization\n"
377 "\n%3$sSystem Identity:%4$s\n"
378 " -M --machine=NAME Set the machine name for the container\n"
379 " --hostname=NAME Override the hostname for the container\n"
380 " --uuid=UUID Set a specific machine UUID for the container\n"
381 "\n%3$sProperties:%4$s\n"
382 " -S --slice=SLICE Place the container in the specified slice\n"
383 " --property=NAME=VALUE Set scope unit property\n"
384 " --register=BOOLEAN Register container as machine\n"
385 " --keep-unit Do not register a scope for the machine, reuse\n"
386 " the service unit nspawn is running in\n"
387 "\n%3$sUser Namespacing:%4$s\n"
388 " --private-users=no Run without user namespacing\n"
389 " --private-users=yes|pick|identity|managed\n"
390 " Run within user namespace, autoselect UID/GID range\n"
391 " --private-users=UIDBASE[:NUIDS]\n"
392 " Similar, but with user configured UID/GID range\n"
393 " --private-users-ownership=MODE\n"
394 " Adjust ('chown') or map ('map') OS tree ownership\n"
395 " to private UID/GID range\n"
396 " -U Equivalent to --private-users=pick and\n"
397 " --private-users-ownership=auto\n"
398 "\n%3$sNetworking:%4$s\n"
399 " --private-network Disable network in container\n"
400 " --network-interface=HOSTIF[:CONTAINERIF]\n"
401 " Assign an existing network interface to the\n"
403 " --network-macvlan=HOSTIF[:CONTAINERIF]\n"
404 " Create a macvlan network interface based on an\n"
405 " existing network interface to the container\n"
406 " --network-ipvlan=HOSTIF[:CONTAINERIF]\n"
407 " Create an ipvlan network interface based on an\n"
408 " existing network interface to the container\n"
409 " -n --network-veth Add a virtual Ethernet connection between host\n"
411 " --network-veth-extra=HOSTIF[:CONTAINERIF]\n"
412 " Add an additional virtual Ethernet link between\n"
413 " host and container\n"
414 " --network-bridge=INTERFACE\n"
415 " Add a virtual Ethernet connection to the container\n"
416 " and attach it to an existing bridge on the host\n"
417 " --network-zone=NAME Similar, but attach the new interface to an\n"
418 " automatically managed bridge interface\n"
419 " --network-namespace-path=PATH\n"
420 " Set network namespace to the one represented by\n"
421 " the specified kernel namespace file node\n"
422 " -p --port=[PROTOCOL:]HOSTPORT[:CONTAINERPORT]\n"
423 " Expose a container IP port on the host\n"
424 "\n%3$sSecurity:%4$s\n"
425 " --capability=CAP In addition to the default, retain specified\n"
427 " --drop-capability=CAP Drop the specified capability from the default set\n"
428 " --ambient-capability=CAP\n"
429 " Sets the specified capability for the started\n"
430 " process. Not useful if booting a machine.\n"
431 " --no-new-privileges Set PR_SET_NO_NEW_PRIVS flag for container payload\n"
432 " --system-call-filter=LIST|~LIST\n"
433 " Permit/prohibit specific system calls\n"
434 " -Z --selinux-context=SECLABEL\n"
435 " Set the SELinux security context to be used by\n"
436 " processes in the container\n"
437 " -L --selinux-apifs-context=SECLABEL\n"
438 " Set the SELinux security context to be used by\n"
439 " API/tmpfs file systems in the container\n"
440 "\n%3$sResources:%4$s\n"
441 " --rlimit=NAME=LIMIT Set a resource limit for the payload\n"
442 " --oom-score-adjust=VALUE\n"
443 " Adjust the OOM score value for the payload\n"
444 " --cpu-affinity=CPUS Adjust the CPU affinity of the container\n"
445 " --personality=ARCH Pick personality for this container\n"
446 "\n%3$sIntegration:%4$s\n"
447 " --resolv-conf=MODE Select mode of /etc/resolv.conf initialization\n"
448 " --timezone=MODE Select mode of /etc/localtime initialization\n"
449 " --link-journal=MODE Link up guest journal, one of no, auto, guest, \n"
450 " host, try-guest, try-host\n"
451 " -j Equivalent to --link-journal=try-guest\n"
452 "\n%3$sMounts:%4$s\n"
453 " --bind=PATH[:PATH[:OPTIONS]]\n"
454 " Bind mount a file or directory from the host into\n"
456 " --bind-ro=PATH[:PATH[:OPTIONS]\n"
457 " Similar, but creates a read-only bind mount\n"
458 " --inaccessible=PATH Over-mount file node with inaccessible node to mask\n"
460 " --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
461 " --overlay=PATH[:PATH...]:PATH\n"
462 " Create an overlay mount from the host to \n"
464 " --overlay-ro=PATH[:PATH...]:PATH\n"
465 " Similar, but creates a read-only overlay mount\n"
466 " --bind-user=NAME Bind user from host to container\n"
467 "\n%3$sInput/Output:%4$s\n"
468 " --console=MODE Select how stdin/stdout/stderr and /dev/console are\n"
469 " set up for the container.\n"
470 " -P --pipe Equivalent to --console=pipe\n"
471 " --background=COLOR Set ANSI color for background\n"
472 "\n%3$sCredentials:%4$s\n"
473 " --set-credential=ID:VALUE\n"
474 " Pass a credential with literal value to container.\n"
475 " --load-credential=ID:PATH\n"
476 " Load credential to pass to container from file or\n"
477 " AF_UNIX stream socket.\n"
478 "\nSee the %2$s for details.\n",
479 program_invocation_short_name
,
489 static int custom_mount_check_all(void) {
490 FOREACH_ARRAY(m
, arg_custom_mounts
, arg_n_custom_mounts
)
491 if (path_equal(m
->destination
, "/") && arg_userns_mode
!= USER_NAMESPACE_NO
) {
492 if (arg_userns_ownership
!= USER_NAMESPACE_OWNERSHIP_OFF
)
493 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
494 "--private-users-ownership=%s may not be combined with custom root mounts.",
495 user_namespace_ownership_to_string(arg_userns_ownership
));
496 if (arg_uid_shift
== UID_INVALID
)
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
498 "--private-users with automatic UID shift may not be combined with custom root mounts.");
504 static int parse_capability_spec(const char *spec
, uint64_t *ret_mask
) {
509 _cleanup_free_
char *t
= NULL
;
511 r
= extract_first_word(&spec
, &t
, ",", 0);
513 return log_error_errno(r
, "Failed to parse capability %s.", t
);
517 if (streq(t
, "help")) {
518 for (int i
= 0; i
< capability_list_length(); i
++) {
521 name
= capability_to_name(i
);
532 r
= capability_from_name(t
);
534 return log_error_errno(r
, "Failed to parse capability %s.", t
);
541 return 1; /* continue */
544 static int parse_share_ns_env(const char *name
, unsigned long ns_flag
) {
547 r
= getenv_bool(name
);
551 return log_error_errno(r
, "Failed to parse $%s: %m", name
);
553 arg_clone_ns_flags
= (arg_clone_ns_flags
& ~ns_flag
) | (r
> 0 ? 0 : ns_flag
);
554 arg_settings_mask
|= SETTING_CLONE_NS_FLAGS
;
558 static int parse_mount_settings_env(void) {
562 r
= getenv_bool("SYSTEMD_NSPAWN_TMPFS_TMP");
563 if (r
< 0 && r
!= -ENXIO
)
564 return log_error_errno(r
, "Failed to parse $SYSTEMD_NSPAWN_TMPFS_TMP: %m");
566 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_TMPFS_TMP
, r
> 0);
568 e
= getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
569 if (streq_ptr(e
, "network"))
570 arg_mount_settings
|= MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_APIVFS_NETNS
;
572 r
= parse_boolean(e
);
574 return log_error_errno(r
, "Failed to parse $SYSTEMD_NSPAWN_API_VFS_WRITABLE: %m");
576 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_APIVFS_RO
, r
== 0);
577 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_APIVFS_NETNS
, false);
583 static int parse_environment(void) {
587 r
= parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC
);
590 r
= parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID
);
593 r
= parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS
);
596 r
= parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC
|CLONE_NEWPID
|CLONE_NEWUTS
);
600 r
= parse_mount_settings_env();
604 /* SYSTEMD_NSPAWN_USE_CGNS=0 can be used to disable CLONE_NEWCGROUP use,
605 * even if it is supported. If not supported, it has no effect. */
606 if (!namespace_type_supported(NAMESPACE_CGROUP
))
607 arg_use_cgns
= false;
609 r
= getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
612 return log_error_errno(r
, "Failed to parse $SYSTEMD_NSPAWN_USE_CGNS: %m");
616 arg_use_cgns
= r
> 0;
617 arg_settings_mask
|= SETTING_USE_CGNS
;
621 e
= getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
623 arg_container_service_name
= e
;
625 e
= getenv("SYSTEMD_NSPAWN_NETWORK_MAC");
627 r
= parse_ether_addr(e
, &arg_network_provided_mac
);
629 return log_error_errno(r
, "Failed to parse provided MAC address via environment variable");
632 r
= getenv_bool("SYSTEMD_SUPPRESS_SYNC");
634 arg_suppress_sync
= r
;
635 else if (r
!= -ENXIO
)
636 log_debug_errno(r
, "Failed to parse $SYSTEMD_SUPPRESS_SYNC, ignoring: %m");
641 static int parse_argv(int argc
, char *argv
[]) {
648 ARG_AMBIENT_CAPABILITY
,
660 ARG_NETWORK_INTERFACE
,
665 ARG_NETWORK_VETH_EXTRA
,
666 ARG_NETWORK_NAMESPACE_PATH
,
676 ARG_PRIVATE_USERS_CHOWN
,
677 ARG_PRIVATE_USERS_OWNERSHIP
,
682 ARG_SYSTEM_CALL_FILTER
,
685 ARG_NO_NEW_PRIVILEGES
,
686 ARG_OOM_SCORE_ADJUST
,
705 static const struct option options
[] = {
706 { "help", no_argument
, NULL
, 'h' },
707 { "version", no_argument
, NULL
, ARG_VERSION
},
708 { "directory", required_argument
, NULL
, 'D' },
709 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
710 { "ephemeral", no_argument
, NULL
, 'x' },
711 { "user", required_argument
, NULL
, 'u' },
712 { "private-network", no_argument
, NULL
, ARG_PRIVATE_NETWORK
},
713 { "as-pid2", no_argument
, NULL
, 'a' },
714 { "boot", no_argument
, NULL
, 'b' },
715 { "uuid", required_argument
, NULL
, ARG_UUID
},
716 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
717 { "capability", required_argument
, NULL
, ARG_CAPABILITY
},
718 { "ambient-capability", required_argument
, NULL
, ARG_AMBIENT_CAPABILITY
},
719 { "drop-capability", required_argument
, NULL
, ARG_DROP_CAPABILITY
},
720 { "no-new-privileges", required_argument
, NULL
, ARG_NO_NEW_PRIVILEGES
},
721 { "link-journal", required_argument
, NULL
, ARG_LINK_JOURNAL
},
722 { "bind", required_argument
, NULL
, ARG_BIND
},
723 { "bind-ro", required_argument
, NULL
, ARG_BIND_RO
},
724 { "tmpfs", required_argument
, NULL
, ARG_TMPFS
},
725 { "overlay", required_argument
, NULL
, ARG_OVERLAY
},
726 { "overlay-ro", required_argument
, NULL
, ARG_OVERLAY_RO
},
727 { "inaccessible", required_argument
, NULL
, ARG_INACCESSIBLE
},
728 { "machine", required_argument
, NULL
, 'M' },
729 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
730 { "slice", required_argument
, NULL
, 'S' },
731 { "setenv", required_argument
, NULL
, 'E' },
732 { "selinux-context", required_argument
, NULL
, 'Z' },
733 { "selinux-apifs-context", required_argument
, NULL
, 'L' },
734 { "quiet", no_argument
, NULL
, 'q' },
735 { "share-system", no_argument
, NULL
, ARG_SHARE_SYSTEM
}, /* not documented */
736 { "register", required_argument
, NULL
, ARG_REGISTER
},
737 { "keep-unit", no_argument
, NULL
, ARG_KEEP_UNIT
},
738 { "network-interface", required_argument
, NULL
, ARG_NETWORK_INTERFACE
},
739 { "network-macvlan", required_argument
, NULL
, ARG_NETWORK_MACVLAN
},
740 { "network-ipvlan", required_argument
, NULL
, ARG_NETWORK_IPVLAN
},
741 { "network-veth", no_argument
, NULL
, 'n' },
742 { "network-veth-extra", required_argument
, NULL
, ARG_NETWORK_VETH_EXTRA
},
743 { "network-bridge", required_argument
, NULL
, ARG_NETWORK_BRIDGE
},
744 { "network-zone", required_argument
, NULL
, ARG_NETWORK_ZONE
},
745 { "network-namespace-path", required_argument
, NULL
, ARG_NETWORK_NAMESPACE_PATH
},
746 { "personality", required_argument
, NULL
, ARG_PERSONALITY
},
747 { "image", required_argument
, NULL
, 'i' },
748 { "volatile", optional_argument
, NULL
, ARG_VOLATILE
},
749 { "port", required_argument
, NULL
, 'p' },
750 { "property", required_argument
, NULL
, ARG_PROPERTY
},
751 { "private-users", optional_argument
, NULL
, ARG_PRIVATE_USERS
},
752 { "private-users-chown", optional_argument
, NULL
, ARG_PRIVATE_USERS_CHOWN
}, /* obsolete */
753 { "private-users-ownership",required_argument
, NULL
, ARG_PRIVATE_USERS_OWNERSHIP
},
754 { "kill-signal", required_argument
, NULL
, ARG_KILL_SIGNAL
},
755 { "settings", required_argument
, NULL
, ARG_SETTINGS
},
756 { "chdir", required_argument
, NULL
, ARG_CHDIR
},
757 { "pivot-root", required_argument
, NULL
, ARG_PIVOT_ROOT
},
758 { "notify-ready", required_argument
, NULL
, ARG_NOTIFY_READY
},
759 { "root-hash", required_argument
, NULL
, ARG_ROOT_HASH
},
760 { "root-hash-sig", required_argument
, NULL
, ARG_ROOT_HASH_SIG
},
761 { "verity-data", required_argument
, NULL
, ARG_VERITY_DATA
},
762 { "system-call-filter", required_argument
, NULL
, ARG_SYSTEM_CALL_FILTER
},
763 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
764 { "oom-score-adjust", required_argument
, NULL
, ARG_OOM_SCORE_ADJUST
},
765 { "cpu-affinity", required_argument
, NULL
, ARG_CPU_AFFINITY
},
766 { "resolv-conf", required_argument
, NULL
, ARG_RESOLV_CONF
},
767 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
768 { "console", required_argument
, NULL
, ARG_CONSOLE
},
769 { "pipe", no_argument
, NULL
, ARG_PIPE
},
770 { "oci-bundle", required_argument
, NULL
, ARG_OCI_BUNDLE
},
771 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
772 { "set-credential", required_argument
, NULL
, ARG_SET_CREDENTIAL
},
773 { "load-credential", required_argument
, NULL
, ARG_LOAD_CREDENTIAL
},
774 { "bind-user", required_argument
, NULL
, ARG_BIND_USER
},
775 { "bind-user-shell", required_argument
, NULL
, ARG_BIND_USER_SHELL
},
776 { "suppress-sync", required_argument
, NULL
, ARG_SUPPRESS_SYNC
},
777 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
778 { "background", required_argument
, NULL
, ARG_BACKGROUND
},
779 { "cleanup", no_argument
, NULL
, ARG_CLEANUP
},
780 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
785 uint64_t plus
= 0, minus
= 0;
786 bool mask_all_settings
= false, mask_no_settings
= false;
791 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
792 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
794 while ((c
= getopt_long(argc
, argv
, "+hD:u:abL:M:jS:Z:qi:xp:nUE:P", options
, NULL
)) >= 0)
804 r
= parse_path_argument(optarg
, false, &arg_directory
);
808 arg_settings_mask
|= SETTING_DIRECTORY
;
812 r
= parse_path_argument(optarg
, false, &arg_template
);
816 arg_settings_mask
|= SETTING_DIRECTORY
;
820 r
= parse_path_argument(optarg
, false, &arg_image
);
824 arg_settings_mask
|= SETTING_DIRECTORY
;
828 r
= parse_path_argument(optarg
, false, &arg_oci_bundle
);
835 arg_ephemeral
= true;
836 arg_settings_mask
|= SETTING_EPHEMERAL
;
840 r
= free_and_strdup(&arg_user
, optarg
);
844 arg_settings_mask
|= SETTING_USER
;
847 case ARG_NETWORK_ZONE
: {
848 _cleanup_free_
char *j
= NULL
;
850 j
= strjoin("vz-", optarg
);
854 if (!ifname_valid(j
))
855 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
856 "Network zone name not valid: %s", j
);
858 free_and_replace(arg_network_zone
, j
);
860 arg_network_veth
= true;
861 arg_private_network
= true;
862 arg_settings_mask
|= SETTING_NETWORK
;
866 case ARG_NETWORK_BRIDGE
:
868 if (!ifname_valid(optarg
))
869 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
870 "Bridge interface name not valid: %s", optarg
);
872 r
= free_and_strdup(&arg_network_bridge
, optarg
);
878 arg_network_veth
= true;
879 arg_private_network
= true;
880 arg_settings_mask
|= SETTING_NETWORK
;
883 case ARG_NETWORK_VETH_EXTRA
:
884 r
= veth_extra_parse(&arg_network_veth_extra
, optarg
);
886 return log_error_errno(r
, "Failed to parse --network-veth-extra= parameter: %s", optarg
);
888 arg_private_network
= true;
889 arg_settings_mask
|= SETTING_NETWORK
;
892 case ARG_NETWORK_INTERFACE
:
893 r
= interface_pair_parse(&arg_network_interfaces
, optarg
);
897 arg_private_network
= true;
898 arg_settings_mask
|= SETTING_NETWORK
;
901 case ARG_NETWORK_MACVLAN
:
902 r
= macvlan_pair_parse(&arg_network_macvlan
, optarg
);
906 arg_private_network
= true;
907 arg_settings_mask
|= SETTING_NETWORK
;
910 case ARG_NETWORK_IPVLAN
:
911 r
= ipvlan_pair_parse(&arg_network_ipvlan
, optarg
);
916 case ARG_PRIVATE_NETWORK
:
917 arg_private_network
= true;
918 arg_settings_mask
|= SETTING_NETWORK
;
921 case ARG_NETWORK_NAMESPACE_PATH
:
922 r
= parse_path_argument(optarg
, false, &arg_network_namespace_path
);
926 arg_settings_mask
|= SETTING_NETWORK
;
930 if (arg_start_mode
== START_PID2
)
931 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
932 "--boot and --as-pid2 may not be combined.");
934 arg_start_mode
= START_BOOT
;
935 arg_settings_mask
|= SETTING_START_MODE
;
939 if (arg_start_mode
== START_BOOT
)
940 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
941 "--boot and --as-pid2 may not be combined.");
943 arg_start_mode
= START_PID2
;
944 arg_settings_mask
|= SETTING_START_MODE
;
948 r
= id128_from_string_nonzero(optarg
, &arg_uuid
);
950 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
951 "Machine UUID may not be all zeroes.");
953 return log_error_errno(r
, "Invalid UUID: %s", optarg
);
955 arg_settings_mask
|= SETTING_MACHINE_ID
;
959 _cleanup_free_
char *mangled
= NULL
;
961 r
= unit_name_mangle_with_suffix(optarg
, NULL
, UNIT_NAME_MANGLE_WARN
, ".slice", &mangled
);
965 free_and_replace(arg_slice
, mangled
);
966 arg_settings_mask
|= SETTING_SLICE
;
972 arg_machine
= mfree(arg_machine
);
974 if (!hostname_is_valid(optarg
, 0))
975 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
976 "Invalid machine name: %s", optarg
);
978 r
= free_and_strdup(&arg_machine
, optarg
);
986 arg_hostname
= mfree(arg_hostname
);
988 if (!hostname_is_valid(optarg
, 0))
989 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
990 "Invalid hostname: %s", optarg
);
992 r
= free_and_strdup(&arg_hostname
, optarg
);
997 arg_settings_mask
|= SETTING_HOSTNAME
;
1001 arg_selinux_context
= optarg
;
1005 arg_selinux_apifs_context
= optarg
;
1009 arg_read_only
= true;
1010 arg_settings_mask
|= SETTING_READ_ONLY
;
1013 case ARG_AMBIENT_CAPABILITY
: {
1015 r
= parse_capability_spec(optarg
, &m
);
1018 arg_caps_ambient
|= m
;
1019 arg_settings_mask
|= SETTING_CAPABILITY
;
1022 case ARG_CAPABILITY
:
1023 case ARG_DROP_CAPABILITY
: {
1025 r
= parse_capability_spec(optarg
, &m
);
1029 if (c
== ARG_CAPABILITY
)
1033 arg_settings_mask
|= SETTING_CAPABILITY
;
1036 case ARG_NO_NEW_PRIVILEGES
:
1037 r
= parse_boolean_argument("--no-new-privileges=", optarg
, &arg_no_new_privileges
);
1041 arg_settings_mask
|= SETTING_NO_NEW_PRIVILEGES
;
1045 arg_link_journal
= LINK_GUEST
;
1046 arg_link_journal_try
= true;
1047 arg_settings_mask
|= SETTING_LINK_JOURNAL
;
1050 case ARG_LINK_JOURNAL
:
1051 r
= parse_link_journal(optarg
, &arg_link_journal
, &arg_link_journal_try
);
1053 return log_error_errno(r
, "Failed to parse link journal mode %s", optarg
);
1055 arg_settings_mask
|= SETTING_LINK_JOURNAL
;
1060 r
= bind_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
, c
== ARG_BIND_RO
);
1062 return log_error_errno(r
, "Failed to parse --bind(-ro)= argument %s: %m", optarg
);
1064 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
1068 r
= tmpfs_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
);
1070 return log_error_errno(r
, "Failed to parse --tmpfs= argument %s: %m", optarg
);
1072 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
1076 case ARG_OVERLAY_RO
:
1077 r
= overlay_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
, c
== ARG_OVERLAY_RO
);
1078 if (r
== -EADDRNOTAVAIL
)
1079 return log_error_errno(r
, "--overlay(-ro)= needs at least two colon-separated directories specified.");
1081 return log_error_errno(r
, "Failed to parse --overlay(-ro)= argument %s: %m", optarg
);
1083 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
1086 case ARG_INACCESSIBLE
:
1087 r
= inaccessible_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
);
1089 return log_error_errno(r
, "Failed to parse --inaccessible= argument %s: %m", optarg
);
1091 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
1095 r
= strv_env_replace_strdup_passthrough(&arg_setenv
, optarg
);
1097 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
1099 arg_settings_mask
|= SETTING_ENVIRONMENT
;
1106 case ARG_SHARE_SYSTEM
:
1107 /* We don't officially support this anymore, except for compat reasons. People should use the
1108 * $SYSTEMD_NSPAWN_SHARE_* environment variables instead. */
1109 log_warning("Please do not use --share-system anymore, use $SYSTEMD_NSPAWN_SHARE_* instead.");
1110 arg_clone_ns_flags
= 0;
1114 r
= parse_boolean_argument("--register=", optarg
, &arg_register
);
1121 arg_keep_unit
= true;
1124 case ARG_PERSONALITY
:
1126 arg_personality
= personality_from_string(optarg
);
1127 if (arg_personality
== PERSONALITY_INVALID
)
1128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1129 "Unknown or unsupported personality '%s'.", optarg
);
1131 arg_settings_mask
|= SETTING_PERSONALITY
;
1137 arg_volatile_mode
= VOLATILE_YES
;
1138 else if (streq(optarg
, "help")) {
1139 DUMP_STRING_TABLE(volatile_mode
, VolatileMode
, _VOLATILE_MODE_MAX
);
1144 m
= volatile_mode_from_string(optarg
);
1146 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1147 "Failed to parse --volatile= argument: %s", optarg
);
1149 arg_volatile_mode
= m
;
1152 arg_settings_mask
|= SETTING_VOLATILE_MODE
;
1156 r
= expose_port_parse(&arg_expose_ports
, optarg
);
1158 return log_error_errno(r
, "Duplicate port specification: %s", optarg
);
1160 return log_error_errno(r
, "Failed to parse host port %s: %m", optarg
);
1162 arg_settings_mask
|= SETTING_EXPOSE_PORTS
;
1166 if (strv_extend(&arg_property
, optarg
) < 0)
1171 case ARG_PRIVATE_USERS
: {
1176 else if (!in_charset(optarg
, DIGITS
))
1177 /* do *not* parse numbers as booleans */
1178 boolean
= parse_boolean(optarg
);
1183 /* no: User namespacing off */
1184 arg_userns_mode
= USER_NAMESPACE_NO
;
1185 arg_uid_shift
= UID_INVALID
;
1186 arg_uid_range
= UINT32_C(0x10000);
1187 } else if (boolean
> 0) {
1188 /* yes: User namespacing on, UID range is read from root dir */
1189 arg_userns_mode
= USER_NAMESPACE_FIXED
;
1190 arg_uid_shift
= UID_INVALID
;
1191 arg_uid_range
= UINT32_C(0x10000);
1192 } else if (streq(optarg
, "pick")) {
1193 /* pick: User namespacing on, UID range is picked randomly */
1194 arg_userns_mode
= USER_NAMESPACE_PICK
; /* Note that arg_userns_ownership is
1195 * implied by USER_NAMESPACE_PICK
1197 arg_uid_shift
= UID_INVALID
;
1198 arg_uid_range
= UINT32_C(0x10000);
1200 } else if (streq(optarg
, "identity")) {
1201 /* identity: User namespaces on, UID range is map of the 0…0xFFFF range to
1202 * itself, i.e. we don't actually map anything, but do take benefit of
1203 * isolation of capability sets. */
1204 arg_userns_mode
= USER_NAMESPACE_FIXED
;
1206 arg_uid_range
= UINT32_C(0x10000);
1207 } else if (streq(optarg
, "managed")) {
1208 /* managed: User namespace on, and acquire it from systemd-nsresourced */
1209 arg_userns_mode
= USER_NAMESPACE_MANAGED
;
1210 arg_uid_shift
= UID_INVALID
;
1211 arg_uid_range
= UINT32_C(0x10000);
1213 /* anything else: User namespacing on, UID range is explicitly configured */
1214 r
= parse_userns_uid_range(optarg
, &arg_uid_shift
, &arg_uid_range
);
1217 arg_userns_mode
= USER_NAMESPACE_FIXED
;
1220 arg_settings_mask
|= SETTING_USERNS
;
1225 if (userns_supported()) {
1226 /* Note that arg_userns_ownership is implied by USER_NAMESPACE_PICK further down. */
1227 arg_userns_mode
= arg_privileged
? USER_NAMESPACE_PICK
: USER_NAMESPACE_MANAGED
;
1228 arg_uid_shift
= UID_INVALID
;
1229 arg_uid_range
= UINT32_C(0x10000);
1231 arg_settings_mask
|= SETTING_USERNS
;
1236 case ARG_PRIVATE_USERS_CHOWN
:
1237 arg_userns_ownership
= USER_NAMESPACE_OWNERSHIP_CHOWN
;
1239 arg_settings_mask
|= SETTING_USERNS
;
1242 case ARG_PRIVATE_USERS_OWNERSHIP
:
1243 if (streq(optarg
, "help")) {
1244 DUMP_STRING_TABLE(user_namespace_ownership
, UserNamespaceOwnership
, _USER_NAMESPACE_OWNERSHIP_MAX
);
1248 arg_userns_ownership
= user_namespace_ownership_from_string(optarg
);
1249 if (arg_userns_ownership
< 0)
1250 return log_error_errno(arg_userns_ownership
, "Cannot parse --user-namespace-ownership= value: %s", optarg
);
1252 arg_settings_mask
|= SETTING_USERNS
;
1255 case ARG_KILL_SIGNAL
:
1256 if (streq(optarg
, "help")) {
1257 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1261 arg_kill_signal
= signal_from_string(optarg
);
1262 if (arg_kill_signal
< 0)
1263 return log_error_errno(arg_kill_signal
, "Cannot parse signal: %s", optarg
);
1265 arg_settings_mask
|= SETTING_KILL_SIGNAL
;
1270 /* no → do not read files
1271 * yes → read files, do not override cmdline, trust only subset
1272 * override → read files, override cmdline, trust only subset
1273 * trusted → read files, do not override cmdline, trust all
1276 r
= parse_boolean(optarg
);
1278 if (streq(optarg
, "trusted")) {
1279 mask_all_settings
= false;
1280 mask_no_settings
= false;
1281 arg_settings_trusted
= true;
1283 } else if (streq(optarg
, "override")) {
1284 mask_all_settings
= false;
1285 mask_no_settings
= true;
1286 arg_settings_trusted
= -1;
1288 return log_error_errno(r
, "Failed to parse --settings= argument: %s", optarg
);
1291 mask_all_settings
= false;
1292 mask_no_settings
= false;
1293 arg_settings_trusted
= -1;
1296 mask_all_settings
= true;
1297 mask_no_settings
= false;
1298 arg_settings_trusted
= false;
1304 _cleanup_free_
char *wd
= NULL
;
1306 if (!path_is_absolute(optarg
))
1307 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1308 "Working directory %s is not an absolute path.", optarg
);
1310 r
= path_simplify_alloc(optarg
, &wd
);
1312 return log_error_errno(r
, "Failed to simplify path %s: %m", optarg
);
1314 if (!path_is_normalized(wd
))
1315 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Working directory path is not normalized: %s", wd
);
1317 if (path_below_api_vfs(wd
))
1318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Working directory is below API VFS, refusing: %s", wd
);
1320 free_and_replace(arg_chdir
, wd
);
1321 arg_settings_mask
|= SETTING_WORKING_DIRECTORY
;
1325 case ARG_PIVOT_ROOT
:
1326 r
= pivot_root_parse(&arg_pivot_root_new
, &arg_pivot_root_old
, optarg
);
1328 return log_error_errno(r
, "Failed to parse --pivot-root= argument %s: %m", optarg
);
1330 arg_settings_mask
|= SETTING_PIVOT_ROOT
;
1333 case ARG_NOTIFY_READY
:
1334 r
= parse_boolean_argument("--notify-ready=", optarg
, &arg_notify_ready
);
1338 arg_settings_mask
|= SETTING_NOTIFY_READY
;
1341 case ARG_ROOT_HASH
: {
1342 _cleanup_free_
void *k
= NULL
;
1345 r
= unhexmem(optarg
, &k
, &l
);
1347 return log_error_errno(r
, "Failed to parse root hash: %s", optarg
);
1348 if (l
< sizeof(sd_id128_t
))
1349 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Root hash must be at least 128-bit long: %s", optarg
);
1351 free_and_replace(arg_verity_settings
.root_hash
, k
);
1352 arg_verity_settings
.root_hash_size
= l
;
1356 case ARG_ROOT_HASH_SIG
: {
1361 if ((value
= startswith(optarg
, "base64:"))) {
1362 r
= unbase64mem(value
, &p
, &l
);
1364 return log_error_errno(r
, "Failed to parse root hash signature '%s': %m", optarg
);
1367 r
= read_full_file(optarg
, (char**) &p
, &l
);
1369 return log_error_errno(r
, "Failed parse root hash signature file '%s': %m", optarg
);
1372 free_and_replace(arg_verity_settings
.root_hash_sig
, p
);
1373 arg_verity_settings
.root_hash_sig_size
= l
;
1377 case ARG_VERITY_DATA
:
1378 r
= parse_path_argument(optarg
, false, &arg_verity_settings
.data_path
);
1383 case ARG_SYSTEM_CALL_FILTER
: {
1387 negative
= optarg
[0] == '~';
1388 items
= negative
? optarg
+ 1 : optarg
;
1391 _cleanup_free_
char *word
= NULL
;
1393 r
= extract_first_word(&items
, &word
, NULL
, 0);
1399 return log_error_errno(r
, "Failed to parse system call filter: %m");
1402 r
= strv_extend(&arg_syscall_deny_list
, word
);
1404 r
= strv_extend(&arg_syscall_allow_list
, word
);
1409 arg_settings_mask
|= SETTING_SYSCALL_FILTER
;
1415 _cleanup_free_
char *name
= NULL
;
1418 if (streq(optarg
, "help")) {
1419 DUMP_STRING_TABLE(rlimit
, int, _RLIMIT_MAX
);
1423 eq
= strchr(optarg
, '=');
1425 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1426 "--rlimit= expects an '=' assignment.");
1428 name
= strndup(optarg
, eq
- optarg
);
1432 rl
= rlimit_from_string_harder(name
);
1434 return log_error_errno(rl
, "Unknown resource limit: %s", name
);
1436 if (!arg_rlimit
[rl
]) {
1437 arg_rlimit
[rl
] = new0(struct rlimit
, 1);
1438 if (!arg_rlimit
[rl
])
1442 r
= rlimit_parse(rl
, eq
+ 1, arg_rlimit
[rl
]);
1444 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
+ 1);
1446 arg_settings_mask
|= SETTING_RLIMIT_FIRST
<< rl
;
1450 case ARG_OOM_SCORE_ADJUST
:
1451 r
= parse_oom_score_adjust(optarg
, &arg_oom_score_adjust
);
1453 return log_error_errno(r
, "Failed to parse --oom-score-adjust= parameter: %s", optarg
);
1455 arg_oom_score_adjust_set
= true;
1456 arg_settings_mask
|= SETTING_OOM_SCORE_ADJUST
;
1459 case ARG_CPU_AFFINITY
: {
1462 r
= parse_cpu_set(optarg
, &cpuset
);
1464 return log_error_errno(r
, "Failed to parse CPU affinity mask %s: %m", optarg
);
1466 cpu_set_done_and_replace(arg_cpu_set
, cpuset
);
1467 arg_settings_mask
|= SETTING_CPU_AFFINITY
;
1471 case ARG_RESOLV_CONF
:
1472 if (streq(optarg
, "help")) {
1473 DUMP_STRING_TABLE(resolv_conf_mode
, ResolvConfMode
, _RESOLV_CONF_MODE_MAX
);
1477 arg_resolv_conf
= resolv_conf_mode_from_string(optarg
);
1478 if (arg_resolv_conf
< 0)
1479 return log_error_errno(arg_resolv_conf
,
1480 "Failed to parse /etc/resolv.conf mode: %s", optarg
);
1482 arg_settings_mask
|= SETTING_RESOLV_CONF
;
1486 if (streq(optarg
, "help")) {
1487 DUMP_STRING_TABLE(timezone_mode
, TimezoneMode
, _TIMEZONE_MODE_MAX
);
1491 arg_timezone
= timezone_mode_from_string(optarg
);
1492 if (arg_timezone
< 0)
1493 return log_error_errno(arg_timezone
,
1494 "Failed to parse /etc/localtime mode: %s", optarg
);
1496 arg_settings_mask
|= SETTING_TIMEZONE
;
1500 r
= handle_arg_console(optarg
);
1507 r
= handle_arg_console("pipe");
1513 arg_pager_flags
|= PAGER_DISABLE
;
1516 case ARG_SET_CREDENTIAL
:
1517 r
= machine_credential_set(&arg_credentials
, optarg
);
1521 arg_settings_mask
|= SETTING_CREDENTIALS
;
1524 case ARG_LOAD_CREDENTIAL
:
1525 r
= machine_credential_load(&arg_credentials
, optarg
);
1529 arg_settings_mask
|= SETTING_CREDENTIALS
;
1533 if (!valid_user_group_name(optarg
, 0))
1534 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid user name to bind: %s", optarg
);
1536 if (strv_extend(&arg_bind_user
, optarg
) < 0)
1539 arg_settings_mask
|= SETTING_BIND_USER
;
1542 case ARG_BIND_USER_SHELL
: {
1545 r
= parse_user_shell(optarg
, &sh
, ©
);
1549 return log_error_errno(r
, "Invalid user shell to bind: %s", optarg
);
1551 free_and_replace(arg_bind_user_shell
, sh
);
1552 arg_bind_user_shell_copy
= copy
;
1554 arg_settings_mask
|= SETTING_BIND_USER_SHELL
;
1558 case ARG_SUPPRESS_SYNC
:
1559 r
= parse_boolean_argument("--suppress-sync=", optarg
, &arg_suppress_sync
);
1563 arg_settings_mask
|= SETTING_SUPPRESS_SYNC
;
1566 case ARG_IMAGE_POLICY
:
1567 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
1572 case ARG_BACKGROUND
:
1573 r
= free_and_strdup_warn(&arg_background
, optarg
);
1582 case ARG_NO_ASK_PASSWORD
:
1583 arg_ask_password
= false;
1590 assert_not_reached();
1593 if (argc
> optind
) {
1594 strv_free(arg_parameters
);
1595 arg_parameters
= strv_copy(argv
+ optind
);
1596 if (!arg_parameters
)
1599 arg_settings_mask
|= SETTING_START_MODE
;
1602 if (arg_ephemeral
&& arg_template
&& !arg_directory
)
1603 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1604 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1605 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1607 arg_directory
= TAKE_PTR(arg_template
);
1609 arg_caps_retain
|= plus
;
1610 arg_caps_retain
|= arg_private_network
? UINT64_C(1) << CAP_NET_ADMIN
: 0;
1611 arg_caps_retain
&= ~minus
;
1613 /* Make sure to parse environment before we reset the settings mask below */
1614 r
= parse_environment();
1618 /* Load all settings from .nspawn files */
1619 if (mask_no_settings
)
1620 arg_settings_mask
= 0;
1622 /* Don't load any settings from .nspawn files */
1623 if (mask_all_settings
)
1624 arg_settings_mask
= _SETTINGS_MASK_ALL
;
1629 static int verify_arguments(void) {
1632 SET_FLAG(arg_mount_settings
, MOUNT_UNMANAGED
, arg_userns_mode
!= USER_NAMESPACE_MANAGED
);
1634 /* We can mount selinuxfs only if we are privileged and can do so before userns. In managed mode we
1635 * have to enter the userns earlier, hence cannot do that. */
1636 /* SET_FLAG(arg_mount_settings, MOUNT_PRIVILEGED, arg_privileged); */
1637 SET_FLAG(arg_mount_settings
, MOUNT_PRIVILEGED
, arg_userns_mode
!= USER_NAMESPACE_MANAGED
);
1639 SET_FLAG(arg_mount_settings
, MOUNT_USE_USERNS
, arg_userns_mode
!= USER_NAMESPACE_NO
);
1641 if (arg_private_network
)
1642 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_APIVFS_NETNS
, arg_private_network
);
1644 if (!arg_privileged
&& arg_userns_mode
!= USER_NAMESPACE_MANAGED
)
1645 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unprivileged operation requires managed user namespaces, as otherwise no UID range can be acquired.");
1647 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
&& !arg_private_network
)
1648 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Managed user namespace operation requires private networking, as otherwise /sys/ may not be mounted.");
1650 if (!(arg_clone_ns_flags
& CLONE_NEWPID
) ||
1651 !(arg_clone_ns_flags
& CLONE_NEWUTS
)) {
1652 arg_register
= false;
1653 if (arg_start_mode
!= START_PID1
)
1654 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--boot cannot be used without namespacing.");
1657 if (arg_userns_ownership
< 0)
1658 arg_userns_ownership
=
1659 IN_SET(arg_userns_mode
, USER_NAMESPACE_PICK
, USER_NAMESPACE_MANAGED
) ? USER_NAMESPACE_OWNERSHIP_AUTO
: USER_NAMESPACE_OWNERSHIP_OFF
;
1661 if (arg_start_mode
== START_BOOT
&& arg_kill_signal
<= 0)
1662 arg_kill_signal
= SIGRTMIN
+3;
1664 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 */
1665 arg_read_only
= true;
1667 if (has_custom_root_mount(arg_custom_mounts
, arg_n_custom_mounts
))
1668 arg_read_only
= true;
1670 if (arg_directory
&& arg_image
)
1671 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--directory= and --image= may not be combined.");
1673 if (arg_template
&& arg_image
)
1674 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--template= and --image= may not be combined.");
1676 if (arg_template
&& !(arg_directory
|| arg_machine
))
1677 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--template= needs --directory= or --machine=.");
1679 if (arg_ephemeral
&& arg_template
)
1680 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--ephemeral and --template= may not be combined.");
1682 /* Permit --ephemeral with --link-journal=try-* to satisfy principle of the least astonishment
1683 * (by common sense, "try" means "do not fail if not possible") */
1684 if (arg_ephemeral
&& !IN_SET(arg_link_journal
, LINK_NO
, LINK_AUTO
) && !arg_link_journal_try
)
1685 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--ephemeral and --link-journal={host,guest} may not be combined.");
1687 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& !userns_supported())
1688 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "--private-users= is not supported, kernel compiled without user namespace support.");
1690 if (arg_userns_ownership
== USER_NAMESPACE_OWNERSHIP_CHOWN
&& arg_read_only
)
1691 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1692 "--read-only and --private-users-ownership=chown may not be combined.");
1694 /* We don't support --private-users-ownership=chown together with any of the volatile modes since we
1695 * couldn't change the read-only part of the tree (i.e. /usr) anyway, or because it would trigger a
1696 * massive copy-up (in case of overlay) making the entire exercise pointless. */
1697 if (arg_userns_ownership
== USER_NAMESPACE_OWNERSHIP_CHOWN
&& arg_volatile_mode
!= VOLATILE_NO
)
1698 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--volatile= and --private-users-ownership=chown may not be combined.");
1700 /* If --network-namespace-path is given with any other network-related option (except --private-network),
1701 * we need to error out, to avoid conflicts between different network options. */
1702 if (arg_network_namespace_path
&&
1703 (arg_network_interfaces
|| arg_network_macvlan
||
1704 arg_network_ipvlan
|| arg_network_veth_extra
||
1705 arg_network_bridge
|| arg_network_zone
||
1707 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "--network-namespace-path= cannot be combined with other network options.");
1709 if (arg_network_bridge
&& arg_network_zone
)
1710 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1711 "--network-bridge= and --network-zone= may not be combined.");
1713 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& (arg_mount_settings
& MOUNT_APPLY_APIVFS_NETNS
) && !arg_private_network
)
1714 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
1716 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& !(arg_mount_settings
& MOUNT_APPLY_APIVFS_RO
))
1717 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot combine --private-users with read-write API VFS mounts.");
1719 if (arg_expose_ports
&& !arg_private_network
)
1720 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot use --port= without private networking.");
1722 if (arg_caps_ambient
) {
1723 if (arg_caps_ambient
== UINT64_MAX
)
1724 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "AmbientCapability= does not support the value all.");
1726 if ((arg_caps_ambient
& arg_caps_retain
) != arg_caps_ambient
)
1727 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "AmbientCapability= setting is not fully covered by Capability= setting.");
1729 if (arg_start_mode
== START_BOOT
)
1730 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "AmbientCapability= setting is not useful for boot mode.");
1733 /* Drop duplicate --bind-user= entries */
1734 strv_uniq(arg_bind_user
);
1736 if (arg_bind_user_shell
&& strv_isempty(arg_bind_user
))
1737 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot use --bind-user-shell= without --bind-user=");
1739 r
= custom_mount_check_all();
1746 static int verify_network_interfaces_initialized(void) {
1748 r
= test_network_interfaces_initialized(arg_network_interfaces
);
1752 r
= test_network_interfaces_initialized(arg_network_macvlan
);
1756 r
= test_network_interfaces_initialized(arg_network_ipvlan
);
1763 static int in_child_chown(void) {
1764 /* Returns true when chown()ing inodes we create inside the outer child is required. Basically, we
1765 * need the chowning when we implement userns ourselves. If userns is off we don#t need to chown(),
1766 * obviously. And if we are in managed mode we already entered the userns, and hence don#t need to
1767 * manually chown either. */
1768 return IN_SET(arg_userns_mode
, USER_NAMESPACE_PICK
, USER_NAMESPACE_FIXED
);
1771 static int userns_chown_at(int fd
, const char *fname
, uid_t uid
, gid_t gid
, int flags
) {
1772 assert(fd
>= 0 || fd
== AT_FDCWD
);
1774 if (!in_child_chown())
1777 if (uid
== UID_INVALID
&& gid
== GID_INVALID
)
1780 if (uid
!= UID_INVALID
) {
1781 uid
+= arg_uid_shift
;
1783 if (uid
< arg_uid_shift
|| uid
>= arg_uid_shift
+ arg_uid_range
)
1787 if (gid
!= GID_INVALID
) {
1788 gid
+= (gid_t
) arg_uid_shift
;
1790 if (gid
< (gid_t
) arg_uid_shift
|| gid
>= (gid_t
) (arg_uid_shift
+ arg_uid_range
))
1794 return RET_NERRNO(fchownat(fd
, strempty(fname
), uid
, gid
, flags
));
1797 int userns_lchown(const char *path
, uid_t uid
, gid_t gid
) {
1798 return userns_chown_at(AT_FDCWD
, path
, uid
, gid
, AT_SYMLINK_NOFOLLOW
);
1801 int userns_mkdir(const char *root
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
1806 _cleanup_close_
int parent_fd
= -EBADF
;
1807 _cleanup_free_
char *dname
= NULL
;
1808 r
= chase(path
, root
, CHASE_PARENT
|CHASE_PREFIX_ROOT
|CHASE_EXTRACT_FILENAME
, &dname
, &parent_fd
);
1812 _cleanup_close_
int dir_fd
= open_mkdir_at(parent_fd
, dname
, O_EXCL
|O_CLOEXEC
, mode
);
1813 if (dir_fd
== -EEXIST
)
1818 return userns_chown_at(dir_fd
, /* fname= */ NULL
, uid
, gid
, AT_SYMLINK_NOFOLLOW
|AT_EMPTY_PATH
);
1821 static const char *timezone_from_path(const char *path
) {
1822 return PATH_STARTSWITH_SET(
1824 "../usr/share/zoneinfo/",
1825 "/usr/share/zoneinfo/");
1828 static bool etc_writable(void) {
1829 return !arg_read_only
|| IN_SET(arg_volatile_mode
, VOLATILE_YES
, VOLATILE_OVERLAY
);
1832 static int setup_timezone(const char *dest
) {
1833 _cleanup_free_
char *p
= NULL
, *etc
= NULL
;
1834 const char *where
, *check
;
1840 if (IN_SET(arg_timezone
, TIMEZONE_AUTO
, TIMEZONE_SYMLINK
)) {
1841 r
= readlink_malloc("/etc/localtime", &p
);
1842 if (r
== -ENOENT
&& arg_timezone
== TIMEZONE_AUTO
)
1843 m
= etc_writable() ? TIMEZONE_DELETE
: TIMEZONE_OFF
;
1844 else if (r
== -EINVAL
&& arg_timezone
== TIMEZONE_AUTO
) /* regular file? */
1845 m
= etc_writable() ? TIMEZONE_COPY
: TIMEZONE_BIND
;
1847 log_warning_errno(r
, "Failed to read host's /etc/localtime symlink, not updating container timezone: %m");
1848 /* To handle warning, delete /etc/localtime and replace it with a symbolic link to a time zone data
1852 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1855 } else if (arg_timezone
== TIMEZONE_AUTO
)
1856 m
= etc_writable() ? TIMEZONE_SYMLINK
: TIMEZONE_BIND
;
1862 if (m
== TIMEZONE_OFF
)
1865 r
= chase("/etc", dest
, CHASE_PREFIX_ROOT
, &etc
, NULL
);
1867 log_warning_errno(r
, "Failed to resolve /etc path in container, ignoring: %m");
1871 where
= strjoina(etc
, "/localtime");
1875 case TIMEZONE_DELETE
:
1876 if (unlink(where
) < 0)
1877 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
, "Failed to remove '%s', ignoring: %m", where
);
1881 case TIMEZONE_SYMLINK
: {
1882 _cleanup_free_
char *q
= NULL
;
1883 const char *z
, *what
;
1885 z
= timezone_from_path(p
);
1887 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
1891 r
= readlink_malloc(where
, &q
);
1892 if (r
>= 0 && streq_ptr(timezone_from_path(q
), z
))
1893 return 0; /* Already pointing to the right place? Then do nothing .. */
1895 check
= strjoina(dest
, "/usr/share/zoneinfo/", z
);
1896 r
= chase(check
, dest
, 0, NULL
, NULL
);
1898 log_debug_errno(r
, "Timezone %s does not exist (or is not accessible) in container, not creating symlink: %m", z
);
1900 if (unlink(where
) < 0 && errno
!= ENOENT
) {
1901 log_full_errno(ERRNO_IS_FS_WRITE_REFUSED(errno
) ? LOG_DEBUG
: LOG_WARNING
, /* Don't complain on read-only images */
1902 errno
, "Failed to remove existing timezone info %s in container, ignoring: %m", where
);
1906 what
= strjoina("../usr/share/zoneinfo/", z
);
1907 if (symlink(what
, where
) < 0) {
1908 log_full_errno(ERRNO_IS_FS_WRITE_REFUSED(errno
) ? LOG_DEBUG
: LOG_WARNING
,
1909 errno
, "Failed to correct timezone of container, ignoring: %m");
1919 case TIMEZONE_BIND
: {
1920 _cleanup_free_
char *resolved
= NULL
;
1923 found
= chase(where
, dest
, CHASE_NONEXISTENT
, &resolved
, NULL
);
1925 log_warning_errno(found
, "Failed to resolve /etc/localtime path in container, ignoring: %m");
1929 if (found
== 0) /* missing? */
1930 (void) touch(resolved
);
1932 r
= mount_nofollow_verbose(LOG_WARNING
, "/etc/localtime", resolved
, NULL
, MS_BIND
, NULL
);
1934 return mount_nofollow_verbose(LOG_ERR
, NULL
, resolved
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NODEV
, NULL
);
1940 /* If mounting failed, try to copy */
1941 r
= copy_file_atomic("/etc/localtime", where
, 0644, COPY_REFLINK
|COPY_REPLACE
);
1943 log_full_errno(ERRNO_IS_NEG_FS_WRITE_REFUSED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1944 "Failed to copy /etc/localtime to %s, ignoring: %m", where
);
1951 assert_not_reached();
1954 /* Fix permissions of the symlink or file copy we just created */
1955 r
= userns_lchown(where
, 0, 0);
1957 log_warning_errno(r
, "Failed to chown /etc/localtime, ignoring: %m");
1962 static int have_resolv_conf(const char *path
) {
1965 if (access(path
, F_OK
) < 0) {
1966 if (errno
== ENOENT
)
1969 return log_debug_errno(errno
, "Failed to determine whether '%s' is available: %m", path
);
1975 static int resolved_listening(void) {
1976 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1977 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1978 _cleanup_free_
char *dns_stub_listener_mode
= NULL
;
1981 /* Check if resolved is listening */
1983 r
= sd_bus_open_system(&bus
);
1985 return log_debug_errno(r
, "Failed to open system bus: %m");
1987 r
= bus_name_has_owner(bus
, "org.freedesktop.resolve1", NULL
);
1989 return log_debug_errno(r
, "Failed to check whether the 'org.freedesktop.resolve1' bus name is taken: %m");
1993 r
= bus_get_property_string(bus
, bus_resolve_mgr
, "DNSStubListener", &error
, &dns_stub_listener_mode
);
1995 return log_debug_errno(r
, "Failed to query DNSStubListener property: %s", bus_error_message(&error
, r
));
1997 return STR_IN_SET(dns_stub_listener_mode
, "udp", "yes");
2000 static int setup_resolv_conf(const char *dest
) {
2001 _cleanup_free_
char *etc
= NULL
;
2002 const char *where
, *what
;
2008 if (arg_resolv_conf
== RESOLV_CONF_AUTO
) {
2009 if (arg_private_network
)
2010 m
= RESOLV_CONF_OFF
;
2011 else if (have_resolv_conf(PRIVATE_STUB_RESOLV_CONF
) > 0 && resolved_listening() > 0)
2012 m
= etc_writable() ? RESOLV_CONF_COPY_STUB
: RESOLV_CONF_BIND_STUB
;
2013 else if (have_resolv_conf("/etc/resolv.conf") > 0)
2014 m
= etc_writable() ? RESOLV_CONF_COPY_HOST
: RESOLV_CONF_BIND_HOST
;
2016 m
= etc_writable() ? RESOLV_CONF_DELETE
: RESOLV_CONF_OFF
;
2019 m
= arg_resolv_conf
;
2021 if (m
== RESOLV_CONF_OFF
)
2024 r
= chase("/etc", dest
, CHASE_PREFIX_ROOT
, &etc
, NULL
);
2026 log_warning_errno(r
, "Failed to resolve /etc path in container, ignoring: %m");
2030 where
= strjoina(etc
, "/resolv.conf");
2032 if (m
== RESOLV_CONF_DELETE
) {
2033 if (unlink(where
) < 0)
2034 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
, "Failed to remove '%s', ignoring: %m", where
);
2039 if (IN_SET(m
, RESOLV_CONF_BIND_STATIC
, RESOLV_CONF_REPLACE_STATIC
, RESOLV_CONF_COPY_STATIC
))
2040 what
= PRIVATE_STATIC_RESOLV_CONF
;
2041 else if (IN_SET(m
, RESOLV_CONF_BIND_UPLINK
, RESOLV_CONF_REPLACE_UPLINK
, RESOLV_CONF_COPY_UPLINK
))
2042 what
= PRIVATE_UPLINK_RESOLV_CONF
;
2043 else if (IN_SET(m
, RESOLV_CONF_BIND_STUB
, RESOLV_CONF_REPLACE_STUB
, RESOLV_CONF_COPY_STUB
))
2044 what
= PRIVATE_STUB_RESOLV_CONF
;
2046 what
= "/etc/resolv.conf";
2048 if (IN_SET(m
, RESOLV_CONF_BIND_HOST
, RESOLV_CONF_BIND_STATIC
, RESOLV_CONF_BIND_UPLINK
, RESOLV_CONF_BIND_STUB
)) {
2049 _cleanup_free_
char *resolved
= NULL
;
2052 found
= chase(where
, dest
, CHASE_NONEXISTENT
|CHASE_NOFOLLOW
, &resolved
, NULL
);
2054 log_warning_errno(found
, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
2058 if (found
== 0) /* missing? */
2059 (void) touch(resolved
);
2061 r
= mount_nofollow_verbose(LOG_WARNING
, what
, resolved
, NULL
, MS_BIND
, NULL
);
2063 return mount_nofollow_verbose(LOG_ERR
, NULL
, resolved
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NODEV
, NULL
);
2065 /* If that didn't work, let's copy the file */
2068 if (IN_SET(m
, RESOLV_CONF_REPLACE_HOST
, RESOLV_CONF_REPLACE_STATIC
, RESOLV_CONF_REPLACE_UPLINK
, RESOLV_CONF_REPLACE_STUB
))
2069 r
= copy_file_atomic(what
, where
, 0644, COPY_REFLINK
|COPY_REPLACE
);
2071 r
= copy_file(what
, where
, O_TRUNC
|O_NOFOLLOW
, 0644, COPY_REFLINK
);
2073 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
2074 * resolved or something similar runs inside and the symlink points there.
2076 * If the disk image is read-only, there's also no point in complaining.
2078 log_full_errno(!IN_SET(RESOLV_CONF_COPY_HOST
, RESOLV_CONF_COPY_STATIC
, RESOLV_CONF_COPY_UPLINK
, RESOLV_CONF_COPY_STUB
) &&
2079 (r
== -ELOOP
|| ERRNO_IS_NEG_FS_WRITE_REFUSED(r
)) ? LOG_DEBUG
: LOG_WARNING
, r
,
2080 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where
);
2084 r
= userns_lchown(where
, 0, 0);
2086 log_warning_errno(r
, "Failed to chown /etc/resolv.conf, ignoring: %m");
2091 static int setup_boot_id(void) {
2092 _cleanup_(unlink_and_freep
) char *from
= NULL
;
2093 _cleanup_free_
char *path
= NULL
;
2094 sd_id128_t rnd
= SD_ID128_NULL
;
2098 /* Generate a new randomized boot ID, so that each boot-up of the container gets a new one */
2100 r
= tempfn_random_child("/run", "proc-sys-kernel-random-boot-id", &path
);
2102 return log_error_errno(r
, "Failed to generate random boot ID path: %m");
2104 r
= sd_id128_randomize(&rnd
);
2106 return log_error_errno(r
, "Failed to generate random boot id: %m");
2108 r
= id128_write(path
, ID128_FORMAT_UUID
, rnd
);
2110 return log_error_errno(r
, "Failed to write boot id: %m");
2112 from
= TAKE_PTR(path
);
2113 to
= "/proc/sys/kernel/random/boot_id";
2115 r
= mount_nofollow_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
2119 return mount_nofollow_verbose(LOG_ERR
, NULL
, to
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
2122 static int bind_mount_devnode(const char *from
, const char *to
) {
2130 return log_debug_errno(r
, "Failed to touch %s: %m", to
);
2132 r
= mount_nofollow_verbose(LOG_DEBUG
, from
, to
, NULL
, MS_BIND
, NULL
);
2135 return log_error_errno(r
, "Failed to bind mount %s to %s: %m", from
, to
);
2141 static int copy_devnode_one(const char *dest
, const char *node
, bool check
) {
2145 assert(!isempty(node
));
2147 BLOCK_WITH_UMASK(0000);
2149 _cleanup_free_
char *from
= path_join("/dev/", node
);
2154 /* If 'check' is true, create /dev/fuse only when it is accessible. The check is necessary,
2155 * as some custom service units that invoke systemd-nspawn may enable DevicePolicy= without
2156 * DeviceAllow= for the device node. */
2157 _cleanup_close_
int fd
= open(from
, O_CLOEXEC
|O_RDWR
);
2159 log_debug_errno(errno
,
2160 "Failed to open %s, skipping creation of the device node in the container, ignoring: %m",
2166 _cleanup_free_
char *to
= path_join(dest
, from
);
2171 if (stat(from
, &st
) < 0) {
2172 if (errno
!= ENOENT
)
2173 return log_error_errno(errno
, "Failed to stat %s: %m", from
);
2175 log_debug_errno(errno
, "Device node %s does not exist, ignoring.", from
);
2178 if (!S_ISCHR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
2179 return log_error_errno(SYNTHETIC_ERRNO(ESTALE
), "%s is not a device node.", from
);
2181 /* Create the parent directory of the device node. Here, we assume that the path has at most one
2182 * subdirectory under /dev/, e.g. /dev/net/tun. */
2183 _cleanup_free_
char *parent
= NULL
;
2184 r
= path_extract_directory(from
, &parent
);
2186 return log_error_errno(r
, "Failed to extract directory from %s: %m", from
);
2187 r
= userns_mkdir(dest
, parent
, 0755, 0, 0);
2189 return log_error_errno(r
, "Failed to create directory %s: %m", parent
);
2191 r
= RET_NERRNO(mknod(to
, st
.st_mode
, st
.st_rdev
));
2193 /* Explicitly warn the user when /dev/ is already populated. */
2195 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
);
2197 /* If arg_uid_shift != 0, then we cannot fall back to use bind mount. */
2198 if (!(arg_userns_mode
== USER_NAMESPACE_NO
||
2199 (arg_userns_mode
== USER_NAMESPACE_FIXED
&& arg_uid_shift
== 0))) {
2201 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
|| !ERRNO_IS_NEG_PRIVILEGE(r
))
2202 return log_error_errno(r
, "Failed to mknod(%s): %m", to
);
2204 log_debug_errno(r
, "Failed to create device node '%s' and running in managed mode, resorting to bind mount: %m", to
);
2207 /* Some systems abusively restrict mknod but allow bind mounts. */
2208 if (bind_mount_devnode(from
, to
) < 0)
2209 /* use the original error code. */
2210 return log_error_errno(r
, "Both mknod() and bind mount %s failed: %m", to
);
2212 /* mknod() succeeds, chown() it if necessary. */
2213 r
= userns_lchown(to
, 0, 0);
2215 return log_error_errno(r
, "chown() of device node %s failed: %m", to
);
2218 _cleanup_free_
char *dn
= path_join("/dev", S_ISCHR(st
.st_mode
) ? "char" : "block");
2222 r
= userns_mkdir(dest
, dn
, 0755, 0, 0);
2224 return log_error_errno(r
, "Failed to create '%s': %m", dn
);
2226 _cleanup_free_
char *sl
= NULL
;
2227 if (asprintf(&sl
, "%s/" DEVNUM_FORMAT_STR
, dn
, DEVNUM_FORMAT_VAL(st
.st_rdev
)) < 0)
2230 _cleanup_free_
char *prefixed
= path_join(dest
, sl
);
2234 _cleanup_free_
char *t
= path_join("..", node
);
2238 if (symlink(t
, prefixed
) < 0)
2239 log_debug_errno(errno
, "Failed to symlink '%s' to '%s', ignoring: %m", t
, prefixed
);
2244 static int copy_devnodes(const char *dest
) {
2249 /* Required basic device nodes. */
2250 FOREACH_STRING(node
, "null", "zero", "full", "random", "urandom", "tty") {
2251 r
= copy_devnode_one(dest
, node
, /* check = */ false);
2256 /* Optional device nodes. */
2257 FOREACH_STRING(node
, "fuse", "net/tun") {
2258 r
= copy_devnode_one(dest
, node
, /* check = */ true);
2266 static int make_extra_nodes(const char *dest
) {
2269 BLOCK_WITH_UMASK(0000);
2271 FOREACH_ARRAY(node
, arg_extra_nodes
, arg_n_extra_nodes
) {
2272 _cleanup_free_
char *path
= NULL
;
2274 path
= path_join(dest
, node
->path
);
2278 dev_t dev
= S_ISCHR(node
->mode
) || S_ISBLK(node
->mode
) ? makedev(node
->major
, node
->minor
) : 0;
2279 if (mknod(path
, node
->mode
, dev
) < 0)
2280 return log_error_errno(errno
, "Failed to create device node '%s': %m", path
);
2282 r
= chmod_and_chown(path
, node
->mode
, node
->uid
, node
->gid
);
2284 return log_error_errno(r
, "Failed to adjust device node ownership of '%s': %m", path
);
2290 static int setup_pts(const char *dest
, uid_t chown_uid
) {
2291 _cleanup_free_
char *options
= NULL
;
2295 if (arg_selinux_apifs_context
)
2296 (void) asprintf(&options
,
2297 "newinstance,ptmxmode=0666,mode=" STRINGIFY(TTY_MODE
) ",gid=" GID_FMT
",context=\"%s\"",
2298 chown_uid
+ TTY_GID
,
2299 arg_selinux_apifs_context
);
2302 (void) asprintf(&options
,
2303 "newinstance,ptmxmode=0666,mode=" STRINGIFY(TTY_MODE
) ",gid=" GID_FMT
,
2304 chown_uid
+ TTY_GID
);
2309 /* Mount /dev/pts itself */
2310 _cleanup_free_
char *p
= path_join(dest
, "/dev/pts");
2314 r
= RET_NERRNO(mkdir(p
, 0755));
2316 return log_error_errno(r
, "Failed to create /dev/pts: %m");
2318 r
= mount_nofollow_verbose(LOG_ERR
, "devpts", p
, "devpts", MS_NOSUID
|MS_NOEXEC
, options
);
2321 r
= userns_lchown(p
, 0, 0);
2323 return log_error_errno(r
, "Failed to chown /dev/pts: %m");
2325 /* Create /dev/ptmx symlink */
2327 p
= path_join(dest
, "/dev/ptmx");
2331 if (symlink("pts/ptmx", p
) < 0)
2332 return log_error_errno(errno
, "Failed to create /dev/ptmx symlink: %m");
2333 r
= userns_lchown(p
, 0, 0);
2335 return log_error_errno(r
, "Failed to chown /dev/ptmx: %m");
2337 /* And fix /dev/pts/ptmx ownership */
2339 p
= path_join(dest
, "/dev/pts/ptmx");
2343 r
= userns_lchown(p
, 0, 0);
2345 return log_error_errno(r
, "Failed to chown /dev/pts/ptmx: %m");
2350 static int setup_stdio_as_dev_console(void) {
2351 _cleanup_close_
int terminal
= -EBADF
;
2354 /* We open the TTY in O_NOCTTY mode, so that we do not become controller yet. We'll do that later
2355 * explicitly, if we are configured to. */
2356 terminal
= open_terminal("/dev/console", O_RDWR
|O_NOCTTY
);
2358 return log_error_errno(terminal
, "Failed to open console: %m");
2360 /* Make sure we can continue logging to the original stderr, even if
2361 * stderr points elsewhere now */
2362 r
= log_dup_console();
2364 return log_error_errno(r
, "Failed to duplicate stderr: %m");
2366 /* invalidates 'terminal' on success and failure */
2367 r
= rearrange_stdio(terminal
, terminal
, terminal
);
2370 return log_error_errno(r
, "Failed to move console to stdin/stdout/stderr: %m");
2375 static int setup_dev_console(const char *console
) {
2376 _cleanup_free_
char *p
= NULL
;
2379 /* Create /dev/console symlink */
2380 r
= path_make_relative("/dev", console
, &p
);
2382 return log_error_errno(r
, "Failed to create relative path: %m");
2384 if (symlink(p
, "/dev/console") < 0)
2385 return log_error_errno(errno
, "Failed to create /dev/console symlink: %m");
2390 static int setup_keyring(void) {
2391 key_serial_t keyring
;
2393 /* Allocate a new session keyring for the container. This makes sure the keyring of the session
2394 * systemd-nspawn was invoked from doesn't leak into the container. Note that by default we block
2395 * keyctl() and request_key() anyway via seccomp so doing this operation isn't strictly necessary,
2396 * but in case people explicitly allow-list these system calls let's make sure we don't leak anything
2397 * into the container. */
2399 keyring
= keyctl(KEYCTL_JOIN_SESSION_KEYRING
, 0, 0, 0, 0);
2400 if (keyring
== -1) {
2401 if (errno
== ENOSYS
)
2402 log_debug_errno(errno
, "Kernel keyring not supported, ignoring.");
2403 else if (ERRNO_IS_PRIVILEGE(errno
))
2404 log_debug_errno(errno
, "Kernel keyring access prohibited, ignoring.");
2406 return log_error_errno(errno
, "Setting up kernel keyring failed: %m");
2412 int make_run_host(const char *root
) {
2417 r
= userns_mkdir(root
, "/run/host", 0755, 0, 0);
2419 return log_error_errno(r
, "Failed to create /run/host/: %m");
2424 static int setup_credentials(const char *root
) {
2425 bool world_readable
= false;
2428 if (arg_credentials
.n_credentials
== 0)
2431 /* If starting a single-process container as a non-root user, the uid will only be resolved after we
2432 * are inside the inner child, when credential directories and files are already read-only, so they
2433 * are unusable as the single process won't have access to them. We also don't have access to the
2434 * uid that will actually be used from here, as we are setting credentials up from the outer child.
2435 * In order to make them usable as requested by the configuration, make them world readable in that
2436 * case, as by definition there are no other processes in that case besides the one being started,
2437 * which is being configured to be able to access credentials, and any of its children which will
2438 * inherit its privileges anyway. To ensure this, also enforce (and document) that
2439 * --no-new-privileges is necessary for this combination to work. */
2440 if (arg_no_new_privileges
&& !isempty(arg_user
) && !STR_IN_SET(arg_user
, "root", "0") &&
2441 arg_start_mode
== START_PID1
)
2442 world_readable
= true;
2444 r
= make_run_host(root
);
2448 r
= userns_mkdir(root
, "/run/host/credentials", world_readable
? 0777 : 0700, 0, 0);
2450 return log_error_errno(r
, "Failed to create /run/host/credentials: %m");
2452 _cleanup_free_
char *q
= path_join(root
, "/run/host/credentials");
2456 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, q
, "ramfs", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, "mode=0700");
2460 FOREACH_ARRAY(cred
, arg_credentials
.credentials
, arg_credentials
.n_credentials
) {
2461 _cleanup_free_
char *j
= NULL
;
2462 _cleanup_close_
int fd
= -EBADF
;
2464 j
= path_join(q
, cred
->id
);
2468 fd
= open(j
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOFOLLOW
, world_readable
? 0666 : 0600);
2470 return log_error_errno(errno
, "Failed to create credential file %s: %m", j
);
2472 r
= loop_write(fd
, cred
->data
, cred
->size
);
2474 return log_error_errno(r
, "Failed to write credential to file %s: %m", j
);
2476 if (fchmod(fd
, world_readable
? 0444 : 0400) < 0)
2477 return log_error_errno(errno
, "Failed to adjust access mode of %s: %m", j
);
2479 if (in_child_chown())
2480 if (fchown(fd
, arg_uid_shift
, arg_uid_shift
) < 0)
2481 return log_error_errno(errno
, "Failed to adjust ownership of %s: %m", j
);
2484 if (chmod(q
, world_readable
? 0555 : 0500) < 0)
2485 return log_error_errno(errno
, "Failed to adjust access mode of %s: %m", q
);
2487 r
= userns_lchown(q
, 0, 0);
2491 /* Make both mount and superblock read-only now */
2492 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, q
, NULL
, MS_REMOUNT
|MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
2496 return mount_nofollow_verbose(LOG_ERR
, NULL
, q
, NULL
, MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, "mode=0500");
2499 static int setup_kmsg(int fd_inner_socket
) {
2500 _cleanup_(unlink_and_freep
) char *from
= NULL
;
2501 _cleanup_free_
char *fifo
= NULL
;
2502 _cleanup_close_
int fd
= -EBADF
;
2505 assert(fd_inner_socket
>= 0);
2507 BLOCK_WITH_UMASK(0000);
2509 /* We create the kmsg FIFO as a temporary file in /run, but immediately delete it after bind mounting it to
2510 * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
2511 * differently from /dev/kmsg in that writing blocks when nothing is reading. In order to avoid any problems
2512 * with containers deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
2514 r
= tempfn_random_child("/run", "proc-kmsg", &fifo
);
2516 return log_error_errno(r
, "Failed to generate kmsg path: %m");
2518 if (mkfifo(fifo
, 0600) < 0)
2519 return log_error_errno(errno
, "mkfifo() for /run/kmsg failed: %m");
2521 from
= TAKE_PTR(fifo
);
2523 r
= mount_nofollow_verbose(LOG_ERR
, from
, "/proc/kmsg", NULL
, MS_BIND
, NULL
);
2527 fd
= open(from
, O_RDWR
|O_NONBLOCK
|O_CLOEXEC
);
2529 return log_error_errno(errno
, "Failed to open fifo: %m");
2531 /* Store away the fd in the socket, so that it stays open as long as we run the child */
2532 r
= send_one_fd(fd_inner_socket
, fd
, 0);
2534 return log_error_errno(r
, "Failed to send FIFO fd: %m");
2540 union in_addr_union address4
;
2541 union in_addr_union address6
;
2542 struct FirewallContext
*fw_ctx
;
2545 static int on_address_change(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2546 struct ExposeArgs
*args
= ASSERT_PTR(userdata
);
2551 (void) expose_port_execute(rtnl
, &args
->fw_ctx
, arg_expose_ports
, AF_INET
, &args
->address4
);
2552 (void) expose_port_execute(rtnl
, &args
->fw_ctx
, arg_expose_ports
, AF_INET6
, &args
->address6
);
2556 static int setup_hostname(void) {
2559 if ((arg_clone_ns_flags
& CLONE_NEWUTS
) == 0)
2562 r
= sethostname_idempotent(arg_hostname
?: arg_machine
);
2564 return log_error_errno(r
, "Failed to set hostname: %m");
2569 static int setup_journal(const char *directory
) {
2570 _cleanup_free_
char *d
= NULL
;
2575 /* Don't link journals in ephemeral mode */
2579 if (arg_link_journal
== LINK_NO
)
2582 try = arg_link_journal_try
|| arg_link_journal
== LINK_AUTO
;
2584 r
= sd_id128_get_machine(&this_id
);
2586 return log_error_errno(r
, "Failed to retrieve machine ID: %m");
2588 if (sd_id128_equal(arg_uuid
, this_id
)) {
2589 log_full(try ? LOG_WARNING
: LOG_ERR
,
2590 "Host and machine ids are equal (%s): refusing to link journals", SD_ID128_TO_STRING(arg_uuid
));
2596 FOREACH_STRING(dirname
, "/var", "/var/log", "/var/log/journal") {
2597 r
= userns_mkdir(directory
, dirname
, 0755, 0, 0);
2599 bool ignore
= r
== -EROFS
&& try;
2600 log_full_errno(ignore
? LOG_DEBUG
: LOG_ERR
, r
,
2601 "Failed to create %s%s: %m", dirname
, ignore
? ", ignoring" : "");
2602 return ignore
? 0 : r
;
2606 _cleanup_free_
char *p
= path_join("/var/log/journal/", SD_ID128_TO_STRING(arg_uuid
));
2610 _cleanup_free_
char *q
= path_join(directory
, p
);
2614 if (path_is_mount_point(p
) > 0) {
2618 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
),
2619 "%s: already a mount point, refusing to use for journal", p
);
2622 if (path_is_mount_point(q
) > 0) {
2626 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
),
2627 "%s: already a mount point, refusing to use for journal", q
);
2630 r
= readlink_and_make_absolute(p
, &d
);
2632 if (IN_SET(arg_link_journal
, LINK_GUEST
, LINK_AUTO
) &&
2635 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
2637 log_warning_errno(r
, "Failed to create directory %s: %m", q
);
2642 return log_error_errno(errno
, "Failed to remove symlink %s: %m", p
);
2643 } else if (r
== -EINVAL
) {
2645 if (arg_link_journal
== LINK_GUEST
&&
2648 if (errno
== ENOTDIR
) {
2649 log_error("%s already exists and is neither a symlink nor a directory", p
);
2652 return log_error_errno(errno
, "Failed to remove %s: %m", p
);
2654 } else if (r
!= -ENOENT
)
2655 return log_error_errno(r
, "readlink(%s) failed: %m", p
);
2657 if (arg_link_journal
== LINK_GUEST
) {
2659 if (symlink(q
, p
) < 0) {
2661 log_debug_errno(errno
, "Failed to symlink %s to %s, skipping journal setup: %m", q
, p
);
2664 return log_error_errno(errno
, "Failed to symlink %s to %s: %m", q
, p
);
2667 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
2669 log_warning_errno(r
, "Failed to create directory %s: %m", q
);
2673 if (arg_link_journal
== LINK_HOST
) {
2674 /* don't create parents here — if the host doesn't have
2675 * permanent journal set up, don't force it here */
2677 r
= RET_NERRNO(mkdir(p
, 0755));
2678 if (r
< 0 && r
!= -EEXIST
) {
2680 log_debug_errno(r
, "Failed to create %s, skipping journal setup: %m", p
);
2683 return log_error_errno(r
, "Failed to create %s: %m", p
);
2686 } else if (access(p
, F_OK
) < 0)
2689 if (dir_is_empty(q
, /* ignore_hidden_or_backup= */ false) == 0)
2690 log_warning("%s is not empty, proceeding anyway.", q
);
2692 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
2694 return log_error_errno(r
, "Failed to create %s: %m", q
);
2696 r
= mount_nofollow_verbose(LOG_DEBUG
, p
, q
, NULL
, MS_BIND
, NULL
);
2698 return log_error_errno(r
, "Failed to bind mount journal from host into guest: %m");
2703 static int drop_capabilities(uid_t uid
) {
2704 CapabilityQuintet q
;
2706 /* Let's initialize all five capability sets to something valid. If the quintet was configured via
2707 * OCI use that, but fill in missing bits. If it wasn't then derive the quintet in full from
2708 * arg_caps_retain. */
2710 if (capability_quintet_is_set(&arg_full_capabilities
)) {
2711 q
= arg_full_capabilities
;
2713 if (q
.bounding
== UINT64_MAX
)
2714 q
.bounding
= uid
== 0 ? arg_caps_retain
: 0;
2716 if (q
.effective
== UINT64_MAX
)
2717 q
.effective
= uid
== 0 ? q
.bounding
: 0;
2719 if (q
.inheritable
== UINT64_MAX
)
2720 q
.inheritable
= uid
== 0 ? q
.bounding
: arg_caps_ambient
;
2722 if (q
.permitted
== UINT64_MAX
)
2723 q
.permitted
= uid
== 0 ? q
.bounding
: arg_caps_ambient
;
2725 if (q
.ambient
== UINT64_MAX
)
2726 q
.ambient
= arg_caps_ambient
;
2728 if (capability_quintet_mangle(&q
))
2729 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Cannot set capabilities that are not in the current bounding set.");
2732 q
= (CapabilityQuintet
) {
2733 .bounding
= arg_caps_retain
,
2734 .effective
= uid
== 0 ? arg_caps_retain
: 0,
2735 .inheritable
= uid
== 0 ? arg_caps_retain
: arg_caps_ambient
,
2736 .permitted
= uid
== 0 ? arg_caps_retain
: arg_caps_ambient
,
2737 .ambient
= arg_caps_ambient
,
2740 /* If we're not using OCI, proceed with mangled capabilities (so we don't error out)
2741 * in order to maintain the same behavior as systemd < 242. */
2742 if (capability_quintet_mangle(&q
))
2743 log_full(arg_quiet
? LOG_DEBUG
: LOG_WARNING
,
2744 "Some capabilities will not be set because they are not in the current bounding set.");
2748 return capability_quintet_enforce(&q
);
2751 static int reset_audit_loginuid(void) {
2752 _cleanup_free_
char *p
= NULL
;
2755 if ((arg_clone_ns_flags
& CLONE_NEWPID
) == 0)
2758 /* if we are in managed userns mode, then we are already in our userns, hence we cannot reset the
2759 * loginuid anyway, hence don't bother */
2760 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
)
2763 r
= read_virtual_file("/proc/self/loginuid", SIZE_MAX
, &p
, /* ret_size= */ NULL
);
2767 return log_error_errno(r
, "Failed to read /proc/self/loginuid: %m");
2769 /* Already reset? */
2770 if (streq(p
, "4294967295"))
2773 r
= write_string_file("/proc/self/loginuid", "4294967295", WRITE_STRING_FILE_DISABLE_BUFFER
);
2776 "Failed to reset audit login UID. This probably means that your kernel is too\n"
2777 "old and you have audit enabled. Note that the auditing subsystem is known to\n"
2778 "be incompatible with containers on old kernels. Please make sure to upgrade\n"
2779 "your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
2780 "using systemd-nspawn. Sleeping for 5s... (%m)");
2788 static int mount_tunnel_dig(const char *root
) {
2791 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
2792 log_debug("Not digging mount tunnel, because running in managed user namespace mode.");
2796 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2797 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
2798 _cleanup_free_
char *p
= path_join("/run/systemd/nspawn/propagate/", arg_machine
);
2802 (void) mkdir_p(p
, 0600);
2804 r
= make_run_host(root
);
2808 r
= userns_mkdir(root
, NSPAWN_MOUNT_TUNNEL
, 0600, 0, 0);
2810 return log_error_errno(r
, "Failed to create "NSPAWN_MOUNT_TUNNEL
": %m");
2812 _cleanup_free_
char *q
= path_join(root
, NSPAWN_MOUNT_TUNNEL
);
2816 r
= mount_nofollow_verbose(LOG_ERR
, p
, q
, NULL
, MS_BIND
, NULL
);
2820 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, q
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
);
2827 static int mount_tunnel_open(void) {
2830 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
2831 log_debug("Not opening up mount tunnel, because running in managed user namespace mode.");
2835 r
= mount_follow_verbose(LOG_ERR
, NULL
, NSPAWN_MOUNT_TUNNEL
, NULL
, MS_SLAVE
, NULL
);
2842 static int setup_machine_id(const char *directory
) {
2845 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2846 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2847 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2848 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2849 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2850 * container behaves nicely). */
2852 r
= id128_get_machine(directory
, &arg_uuid
);
2853 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r
)) {
2854 /* If the file is missing, empty, or uninitialized, we don't mind */
2855 if (sd_id128_is_null(arg_uuid
)) {
2856 r
= sd_id128_randomize(&arg_uuid
);
2858 return log_error_errno(r
, "Failed to acquire randomized machine UUID: %m");
2861 return log_error_errno(r
, "Failed to read machine ID from container image: %m");
2866 static int recursive_chown(const char *directory
, uid_t shift
, uid_t range
) {
2871 if (arg_userns_mode
== USER_NAMESPACE_NO
|| arg_userns_ownership
!= USER_NAMESPACE_OWNERSHIP_CHOWN
)
2874 r
= path_patch_uid(directory
, arg_uid_shift
, arg_uid_range
);
2875 if (r
== -EOPNOTSUPP
)
2876 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.");
2878 return log_error_errno(r
, "Upper 16 bits of root directory UID and GID do not match.");
2880 return log_error_errno(r
, "Failed to adjust UID/GID shift of OS tree: %m");
2882 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2884 log_debug("Patched directory tree to match UID/GID range.");
2891 * < 0 : wait_for_terminate() failed to get the state of the
2892 * container, the container was terminated by a signal, or
2893 * failed for an unknown reason. No change is made to the
2894 * container argument.
2895 * > 0 : The program executed in the container terminated with an
2896 * error. The exit code of the program executed in the
2897 * container is returned. The container argument has been set
2898 * to CONTAINER_TERMINATED.
2899 * 0 : The container is being rebooted, has been shut down or exited
2900 * successfully. The container argument has been set to either
2901 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
2903 * That is, success is indicated by a return value of zero, and an
2904 * error is indicated by a non-zero value.
2906 static int wait_for_container(PidRef
*pid
, ContainerStatus
*container
) {
2910 assert(pidref_is_set(pid
));
2912 r
= pidref_wait_for_terminate(pid
, &status
);
2914 return log_warning_errno(r
, "Failed to wait for container: %m");
2918 switch (status
.si_code
) {
2921 if (status
.si_status
== 0)
2922 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s exited successfully.", arg_machine
);
2924 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s failed with error code %i.", arg_machine
, status
.si_status
);
2926 *container
= CONTAINER_TERMINATED
;
2927 return status
.si_status
;
2930 if (status
.si_status
== SIGINT
) {
2931 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s has been shut down.", arg_machine
);
2932 *container
= CONTAINER_TERMINATED
;
2935 } else if (status
.si_status
== SIGHUP
) {
2936 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s is being rebooted.", arg_machine
);
2937 *container
= CONTAINER_REBOOTED
;
2943 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
2944 "Container %s terminated by signal %s.", arg_machine
, signal_to_string(status
.si_status
));
2947 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
2948 "Container %s failed due to unknown reason.", arg_machine
);
2952 static int on_orderly_shutdown(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
2953 PidRef
*pid
= ASSERT_PTR(userdata
);
2957 if (pidref_is_set(pid
))
2958 if (pidref_kill(pid
, arg_kill_signal
) >= 0) {
2959 log_info("Trying to halt container by sending %s to container PID 1. Send SIGTERM again to trigger immediate termination.",
2960 signal_to_string(si
->ssi_signo
));
2961 sd_event_source_set_userdata(s
, NULL
);
2962 sd_notify(/* unset_environment= */ false, NOTIFY_STOPPING_MESSAGE
);
2966 log_debug("Got %s, exiting.", signal_to_string(si
->ssi_signo
));
2968 sd_event_exit(sd_event_source_get_event(s
), 0);
2972 static int on_sigchld(sd_event_source
*s
, const struct signalfd_siginfo
*ssi
, void *userdata
) {
2977 PidRef
*pid
= ASSERT_PTR(userdata
);
2981 if (waitid(P_ALL
, 0, &si
, WNOHANG
|WNOWAIT
|WEXITED
) < 0)
2982 return log_error_errno(errno
, "Failed to waitid(): %m");
2983 if (si
.si_pid
== 0) /* No pending children. */
2985 if (si
.si_pid
== pid
->pid
) {
2986 /* The main process we care for has exited. Return from
2987 * signal handler but leave the zombie. */
2988 sd_event_exit(sd_event_source_get_event(s
), 0);
2992 /* Reap all other children. */
2993 (void) waitid(P_PID
, si
.si_pid
, &si
, WNOHANG
|WEXITED
);
2999 static int on_request_stop(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
3000 PidRef
*pid
= ASSERT_PTR(userdata
);
3004 if (arg_kill_signal
> 0) {
3005 log_info("Container termination requested. Attempting to halt container.");
3006 (void) pidref_kill(pid
, arg_kill_signal
);
3008 log_info("Container termination requested. Exiting.");
3009 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), 0);
3015 static int pick_paths(void) {
3018 if (arg_directory
) {
3019 _cleanup_(pick_result_done
) PickResult result
= PICK_RESULT_NULL
;
3020 PickFilter filter
= pick_filter_image_dir
;
3022 filter
.architecture
= arg_architecture
;
3024 r
= path_pick_update_warn(
3027 PICK_ARCHITECTURE
|PICK_TRIES
,
3030 /* Accept ENOENT here so that the --template= logic can work */
3034 arg_architecture
= result
.architecture
;
3038 _cleanup_(pick_result_done
) PickResult result
= PICK_RESULT_NULL
;
3039 PickFilter filter
= pick_filter_image_raw
;
3041 filter
.architecture
= arg_architecture
;
3043 r
= path_pick_update_warn(
3046 PICK_ARCHITECTURE
|PICK_TRIES
,
3051 arg_architecture
= result
.architecture
;
3055 _cleanup_(pick_result_done
) PickResult result
= PICK_RESULT_NULL
;
3056 PickFilter filter
= pick_filter_image_dir
;
3058 filter
.architecture
= arg_architecture
;
3060 r
= path_pick_update_warn(
3068 arg_architecture
= result
.architecture
;
3074 static int determine_names(void) {
3077 if (arg_template
&& !arg_directory
&& arg_machine
) {
3079 /* If --template= was specified then we should not search for a machine, but instead create a
3080 * new one in /var/lib/machine. */
3082 arg_directory
= path_join("/var/lib/machines", arg_machine
);
3087 if (!arg_image
&& !arg_directory
) {
3089 _cleanup_(image_unrefp
) Image
*i
= NULL
;
3091 r
= image_find(arg_privileged
? RUNTIME_SCOPE_SYSTEM
: RUNTIME_SCOPE_USER
,
3092 IMAGE_MACHINE
, arg_machine
, NULL
, &i
);
3094 return log_error_errno(r
, "No image for machine '%s'.", arg_machine
);
3096 return log_error_errno(r
, "Failed to find image for machine '%s': %m", arg_machine
);
3098 if (IN_SET(i
->type
, IMAGE_RAW
, IMAGE_BLOCK
))
3099 r
= free_and_strdup(&arg_image
, i
->path
);
3101 r
= free_and_strdup(&arg_directory
, i
->path
);
3106 arg_read_only
= arg_read_only
|| i
->read_only
;
3108 r
= safe_getcwd(&arg_directory
);
3110 return log_error_errno(r
, "Failed to determine current directory: %m");
3113 if (!arg_directory
&& !arg_image
)
3114 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine path, please use -D or -i.");
3118 if (arg_directory
&& path_equal(arg_directory
, "/")) {
3119 arg_machine
= gethostname_malloc();
3122 } else if (arg_image
) {
3125 r
= path_extract_filename(arg_image
, &arg_machine
);
3127 return log_error_errno(r
, "Failed to extract file name from '%s': %m", arg_image
);
3129 /* Truncate suffix if there is one */
3130 e
= endswith(arg_machine
, ".raw");
3134 r
= path_extract_filename(arg_directory
, &arg_machine
);
3136 return log_error_errno(r
, "Failed to extract file name from '%s': %m", arg_directory
);
3139 hostname_cleanup(arg_machine
);
3140 if (!hostname_is_valid(arg_machine
, 0))
3141 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to determine machine name automatically, please use -M.");
3143 /* Copy the machine name before the random suffix is added below, otherwise we won't be able
3144 * to match fixed config file names. */
3145 arg_settings_filename
= strjoin(arg_machine
, ".nspawn");
3146 if (!arg_settings_filename
)
3149 /* Add a random suffix when this is an ephemeral machine, so that we can run many
3150 * instances at once without manually having to specify -M each time. */
3152 if (strextendf(&arg_machine
, "-%016" PRIx64
, random_u64()) < 0)
3155 arg_settings_filename
= strjoin(arg_machine
, ".nspawn");
3156 if (!arg_settings_filename
)
3163 static int chase_and_update(char **p
, unsigned flags
) {
3172 r
= chase(*p
, NULL
, flags
, &chased
, NULL
);
3174 return log_error_errno(r
, "Failed to resolve path %s: %m", *p
);
3176 return free_and_replace(*p
, chased
);
3179 static int determine_uid_shift(const char *directory
) {
3182 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
3183 /* In managed mode we should already know the UID shift */
3184 assert(uid_is_valid(arg_uid_shift
));
3188 if (arg_userns_mode
== USER_NAMESPACE_NO
) {
3193 if (arg_uid_shift
== UID_INVALID
) {
3196 /* Read the UID shift off the image. Maybe we can reuse this to avoid chowning. */
3198 if (stat(directory
, &st
) < 0)
3199 return log_error_errno(errno
, "Failed to determine UID base of %s: %m", directory
);
3201 arg_uid_shift
= st
.st_uid
& UINT32_C(0xffff0000);
3203 if (arg_uid_shift
!= (st
.st_gid
& UINT32_C(0xffff0000)))
3204 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3205 "UID and GID base of %s don't match.", directory
);
3207 arg_uid_range
= UINT32_C(0x10000);
3209 if (arg_uid_shift
!= 0) {
3210 /* If the image is shifted already, then we'll fall back to classic chowning, for
3211 * compatibility (and simplicity), or refuse if mapping is explicitly requested. */
3213 if (arg_userns_ownership
== USER_NAMESPACE_OWNERSHIP_AUTO
) {
3214 log_debug("UID base of %s is non-zero, not using UID mapping.", directory
);
3215 arg_userns_ownership
= USER_NAMESPACE_OWNERSHIP_CHOWN
;
3216 } else if (arg_userns_ownership
== USER_NAMESPACE_OWNERSHIP_MAP
)
3217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3218 "UID base of %s is not zero, UID mapping not supported.", directory
);
3222 if (!userns_shift_range_valid(arg_uid_shift
, arg_uid_range
))
3223 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "UID base too high for UID range.");
3228 static unsigned long effective_clone_ns_flags(void) {
3229 unsigned long flags
= arg_clone_ns_flags
;
3231 if (arg_private_network
)
3232 flags
|= CLONE_NEWNET
;
3234 flags
|= CLONE_NEWCGROUP
;
3235 if (arg_userns_mode
!= USER_NAMESPACE_NO
)
3236 flags
|= CLONE_NEWUSER
;
3241 static int patch_sysctl(void) {
3243 /* This table is inspired by runc's sysctl() function */
3244 static const struct {
3247 unsigned long clone_flags
;
3249 { "kernel.hostname", false, CLONE_NEWUTS
},
3250 { "kernel.domainname", false, CLONE_NEWUTS
},
3251 { "kernel.msgmax", false, CLONE_NEWIPC
},
3252 { "kernel.msgmnb", false, CLONE_NEWIPC
},
3253 { "kernel.msgmni", false, CLONE_NEWIPC
},
3254 { "kernel.sem", false, CLONE_NEWIPC
},
3255 { "kernel.shmall", false, CLONE_NEWIPC
},
3256 { "kernel.shmmax", false, CLONE_NEWIPC
},
3257 { "kernel.shmmni", false, CLONE_NEWIPC
},
3258 { "fs.mqueue.", true, CLONE_NEWIPC
},
3259 { "net.", true, CLONE_NEWNET
},
3262 unsigned long flags
;
3265 flags
= effective_clone_ns_flags();
3267 STRV_FOREACH_PAIR(k
, v
, arg_sysctl
) {
3270 FOREACH_ELEMENT(i
, safe_sysctl
) {
3271 if (!FLAGS_SET(flags
, i
->clone_flags
))
3275 good
= startswith(*k
, i
->key
);
3277 good
= streq(*k
, i
->key
);
3284 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k
);
3286 r
= sysctl_write(*k
, *v
);
3288 return log_error_errno(r
, "Failed to write sysctl '%s': %m", *k
);
3294 static int inner_child(
3296 int fd_inner_socket
,
3298 char **os_release_pairs
) {
3300 _cleanup_free_
char *home
= NULL
;
3303 (char*) "PATH=" DEFAULT_PATH_COMPAT
,
3304 NULL
, /* container */
3306 NULL
, /* COLORTERM */
3307 NULL
, /* NO_COLOR */
3311 NULL
, /* container_uuid */
3312 NULL
, /* LISTEN_FDS */
3313 NULL
, /* LISTEN_PID */
3314 NULL
, /* NOTIFY_SOCKET */
3315 NULL
, /* CREDENTIALS_DIRECTORY */
3319 const char *exec_target
;
3320 _cleanup_strv_free_
char **env_use
= NULL
;
3321 int r
, which_failed
;
3323 /* This is the "inner" child process, i.e. the one forked off by the "outer" child process, which is the one
3324 * the container manager itself forked off. At the time of clone() it gained its own CLONE_NEWNS, CLONE_NEWPID,
3325 * CLONE_NEWUTS, CLONE_NEWIPC, CLONE_NEWUSER namespaces. Note that it has its own CLONE_NEWNS namespace,
3326 * separate from the CLONE_NEWNS created for the "outer" child, and also separate from the host's CLONE_NEWNS
3327 * namespace. The reason for having two levels of CLONE_NEWNS namespaces is that the "inner" one is owned by
3328 * the CLONE_NEWUSER namespace of the container, while the "outer" one is owned by the host's CLONE_NEWUSER
3331 * Note at this point we have no CLONE_NEWNET namespace yet. We'll acquire that one later through
3332 * unshare(). See below. */
3335 assert(fd_inner_socket
>= 0);
3337 log_debug("Inner child is initializing.");
3339 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
3340 /* Tell the parent, that it now can write the UID map. */
3341 (void) barrier_place(barrier
); /* #1 */
3343 /* Wait until the parent wrote the UID map */
3344 if (!barrier_place_and_sync(barrier
)) /* #2 */
3345 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Parent died too early");
3347 /* Become the new root user inside our namespace */
3348 r
= reset_uid_gid();
3350 return log_error_errno(r
, "Couldn't become new root: %m");
3352 /* Creating a new user namespace means all MS_SHARED mounts become MS_SLAVE. Let's put them
3353 * back to MS_SHARED here, since that's what we want as defaults. (This will not reconnect
3354 * propagation, but simply create new peer groups for all our mounts). */
3355 r
= mount_follow_verbose(LOG_ERR
, NULL
, "/", NULL
, MS_SHARED
|MS_REC
, NULL
);
3360 r
= mount_all(/* dest= */ NULL
,
3361 arg_mount_settings
| MOUNT_IN_USERNS
,
3363 arg_selinux_apifs_context
);
3367 if (!arg_network_namespace_path
&& arg_private_network
) {
3368 _cleanup_close_
int netns_fd
= -EBADF
;
3370 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
)
3371 if (unshare(CLONE_NEWNET
) < 0)
3372 return log_error_errno(errno
, "Failed to unshare network namespace: %m");
3374 netns_fd
= namespace_open_by_type(NAMESPACE_NET
);
3376 return log_error_errno(netns_fd
, "Failed to open newly allocate network namespace: %m");
3378 r
= send_one_fd(fd_inner_socket
, netns_fd
, 0);
3380 return log_error_errno(r
, "Failed to send network namespace to supervisor: %m");
3382 /* Tell the parent that it can setup network interfaces. */
3383 (void) barrier_place(barrier
); /* #3 */
3386 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
3387 r
= mount_sysfs(NULL
, arg_mount_settings
| MOUNT_IN_USERNS
);
3392 /* Wait until we are cgroup-ified, so that we can mount the right cgroup path writable */
3393 if (!barrier_place_and_sync(barrier
)) /* #4 */
3394 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
),
3395 "Parent died too early");
3398 r
= unshare(CLONE_NEWCGROUP
);
3400 return log_error_errno(errno
, "Failed to unshare cgroup namespace: %m");
3402 r
= mount_cgroups(/* dest = */ NULL
, /* accept_existing = */ false);
3404 r
= bind_mount_cgroup_hierarchy();
3408 r
= setup_boot_id();
3412 r
= setup_kmsg(fd_inner_socket
);
3419 arg_n_custom_mounts
,
3422 arg_selinux_apifs_context
,
3423 MOUNT_NON_ROOT_ONLY
| MOUNT_IN_USERNS
);
3428 return log_error_errno(errno
, "setsid() failed: %m");
3430 if (arg_private_network
)
3431 (void) loopback_setup();
3433 if (arg_expose_ports
) {
3434 r
= expose_port_send_rtnl(fd_inner_socket
);
3439 if (arg_console_mode
!= CONSOLE_PIPE
) {
3440 _cleanup_close_
int master
= -EBADF
;
3441 _cleanup_free_
char *console
= NULL
;
3443 /* Allocate a pty and make it available as /dev/console. */
3444 master
= openpt_allocate(O_RDWR
|O_NONBLOCK
, &console
);
3446 return log_error_errno(master
, "Failed to allocate a pty: %m");
3448 r
= setup_dev_console(console
);
3450 return log_error_errno(r
, "Failed to set up /dev/console: %m");
3452 r
= send_one_fd(fd_inner_socket
, master
, 0);
3454 return log_error_errno(r
, "Failed to send master fd: %m");
3456 r
= setup_stdio_as_dev_console();
3465 if (arg_oom_score_adjust_set
) {
3466 r
= set_oom_score_adjust(arg_oom_score_adjust
);
3468 return log_error_errno(r
, "Failed to adjust OOM score: %m");
3471 if (arg_cpu_set
.set
)
3472 if (sched_setaffinity(0, arg_cpu_set
.allocated
, arg_cpu_set
.set
) < 0)
3473 return log_error_errno(errno
, "Failed to set CPU affinity: %m");
3475 (void) setup_hostname();
3477 if (arg_personality
!= PERSONALITY_INVALID
) {
3478 r
= safe_personality(arg_personality
);
3480 return log_error_errno(r
, "personality() failed: %m");
3481 #ifdef ARCHITECTURE_SECONDARY
3482 } else if (arg_architecture
== ARCHITECTURE_SECONDARY
) {
3483 r
= safe_personality(PER_LINUX32
);
3485 return log_error_errno(r
, "personality() failed: %m");
3487 } else if (!arg_quiet
&& arg_architecture
>= 0 && arg_architecture
!= native_architecture())
3488 log_notice("Selected architecture '%s' not supported natively on the local CPU, assuming "
3489 "invocation with qemu userspace emulator (or equivalent) in effect.",
3490 architecture_to_string(arg_architecture
));
3492 r
= setrlimit_closest_all((const struct rlimit
*const*) arg_rlimit
, &which_failed
);
3494 return log_error_errno(r
, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed
));
3499 if (is_seccomp_available()) {
3500 r
= seccomp_load(arg_seccomp
);
3501 if (ERRNO_IS_NEG_SECCOMP_FATAL(r
))
3502 return log_error_errno(r
, "Failed to install seccomp filter: %m");
3504 log_debug_errno(r
, "Failed to install seccomp filter: %m");
3509 r
= setup_seccomp(arg_caps_retain
, arg_syscall_allow_list
, arg_syscall_deny_list
);
3514 if (arg_suppress_sync
) {
3516 r
= seccomp_suppress_sync();
3518 log_debug_errno(r
, "Failed to install sync() suppression seccomp filter, ignoring: %m");
3520 log_debug("systemd is built without SECCOMP support. Ignoring --suppress-sync= command line option and SuppressSync= setting.");
3525 if (arg_selinux_context
)
3526 if (setexeccon(arg_selinux_context
) < 0)
3527 return log_error_errno(errno
, "setexeccon(\"%s\") failed: %m", arg_selinux_context
);
3530 /* Make sure we keep the caps across the uid/gid dropping, so that we can retain some selected caps
3531 * if we need to later on. */
3532 if (prctl(PR_SET_KEEPCAPS
, 1) < 0)
3533 return log_error_errno(errno
, "Failed to set PR_SET_KEEPCAPS: %m");
3535 if (uid_is_valid(arg_uid
) || gid_is_valid(arg_gid
))
3536 r
= change_uid_gid_raw(arg_uid
, arg_gid
, arg_supplementary_gids
, arg_n_supplementary_gids
, arg_console_mode
!= CONSOLE_PIPE
);
3538 r
= change_uid_gid(arg_user
, arg_console_mode
!= CONSOLE_PIPE
, &home
);
3542 r
= drop_capabilities(getuid());
3544 return log_error_errno(r
, "Dropping capabilities failed: %m");
3546 if (arg_no_new_privileges
)
3547 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0)
3548 return log_error_errno(errno
, "Failed to disable new privileges: %m");
3550 /* LXC sets container=lxc, so follow the scheme here */
3551 envp
[n_env
++] = strjoina("container=", arg_container_service_name
);
3553 /* Propagate $TERM & Co. unless we are invoked in pipe mode and stdin/stdout/stderr don't refer to a TTY */
3554 if (arg_console_mode
!= CONSOLE_PIPE
|| on_tty()) {
3555 FOREACH_STRING(v
, "TERM=", "COLORTERM=", "NO_COLOR=") {
3556 char *t
= strv_find_prefix(environ
, v
);
3563 envp
[n_env
++] = (char*) "TERM=dumb";
3565 if (home
|| !uid_is_valid(arg_uid
) || arg_uid
== 0)
3566 if (asprintf(envp
+ n_env
++, "HOME=%s", home
?: "/root") < 0)
3569 if (arg_user
|| !uid_is_valid(arg_uid
) || arg_uid
== 0)
3570 if (asprintf(envp
+ n_env
++, "USER=%s", arg_user
?: "root") < 0 ||
3571 asprintf(envp
+ n_env
++, "LOGNAME=%s", arg_user
?: "root") < 0)
3574 assert(!sd_id128_is_null(arg_uuid
));
3576 if (asprintf(envp
+ n_env
++, "container_uuid=%s", SD_ID128_TO_UUID_STRING(arg_uuid
)) < 0)
3579 if (!fdset_isempty(fds
)) {
3580 r
= fdset_cloexec(fds
, false);
3582 return log_error_errno(r
, "Failed to unset O_CLOEXEC for file descriptors.");
3584 if ((asprintf(envp
+ n_env
++, "LISTEN_FDS=%u", fdset_size(fds
)) < 0) ||
3585 (asprintf(envp
+ n_env
++, "LISTEN_PID=1") < 0))
3588 if (asprintf(envp
+ n_env
++, "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH
) < 0)
3591 if (arg_credentials
.n_credentials
> 0) {
3592 envp
[n_env
] = strdup("CREDENTIALS_DIRECTORY=/run/host/credentials");
3598 if (arg_start_mode
!= START_BOOT
) {
3599 envp
[n_env
] = strdup("LANG=" SYSTEMD_NSPAWN_LOCALE
);
3605 env_use
= strv_env_merge(envp
, os_release_pairs
, arg_setenv
);
3609 /* Let the parent know that we are ready and wait until the parent is ready with the setup, too... */
3610 if (!barrier_place_and_sync(barrier
)) /* #5 */
3611 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Parent died too early");
3613 /* Note, this should be done this late (💣 and not moved earlier! 💣), so that all namespacing
3614 * changes are already in effect by now, so that any resolved paths here definitely reference
3615 * resources inside the container, and not outside of them. */
3617 if (chdir(arg_chdir
) < 0)
3618 return log_error_errno(errno
, "Failed to change to specified working directory %s: %m", arg_chdir
);
3620 if (arg_start_mode
== START_PID2
) {
3621 r
= stub_pid1(arg_uuid
);
3626 if (arg_console_mode
!= CONSOLE_PIPE
) {
3627 /* So far our pty wasn't controlled by any process. Finally, it's time to change that, if we
3628 * are configured for that. Acquire it as controlling tty. */
3629 if (ioctl(STDIN_FILENO
, TIOCSCTTY
) < 0)
3630 return log_error_errno(errno
, "Failed to acquire controlling TTY: %m");
3633 log_debug("Inner child finished, invoking payload.");
3635 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
3636 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
3637 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
3639 log_set_open_when_needed(true);
3640 log_settle_target();
3642 (void) fdset_close_others(fds
);
3644 if (arg_start_mode
== START_BOOT
) {
3648 /* Automatically search for the init system */
3650 m
= strv_length(arg_parameters
);
3651 a
= newa(char*, m
+ 2);
3652 memcpy_safe(a
+ 1, arg_parameters
, m
* sizeof(char*));
3655 FOREACH_STRING(init
,
3656 "/usr/lib/systemd/systemd",
3657 "/lib/systemd/systemd",
3659 a
[0] = (char*) init
;
3660 execve(a
[0], a
, env_use
);
3663 exec_target
= "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
3664 } else if (!strv_isempty(arg_parameters
)) {
3665 const char *dollar_path
;
3667 exec_target
= arg_parameters
[0];
3669 /* Use the user supplied search $PATH if there is one, or DEFAULT_PATH_COMPAT if not to search the
3671 dollar_path
= strv_env_get(env_use
, "PATH");
3673 if (setenv("PATH", dollar_path
, 1) < 0)
3674 return log_error_errno(errno
, "Failed to update $PATH: %m");
3677 execvpe(arg_parameters
[0], arg_parameters
, env_use
);
3680 /* If we cannot change the directory, we'll end up in /, that is expected. */
3681 (void) chdir(home
?: "/root");
3683 execle(DEFAULT_USER_SHELL
, "-" DEFAULT_USER_SHELL_NAME
, NULL
, env_use
);
3684 if (!streq(DEFAULT_USER_SHELL
, "/bin/bash"))
3685 execle("/bin/bash", "-bash", NULL
, env_use
);
3686 if (!streq(DEFAULT_USER_SHELL
, "/bin/sh"))
3687 execle("/bin/sh", "-sh", NULL
, env_use
);
3689 exec_target
= DEFAULT_USER_SHELL
", /bin/bash, /bin/sh";
3692 return log_error_errno(errno
, "execv(%s) failed: %m", exec_target
);
3695 static int setup_notify_child(const void *directory
) {
3696 _cleanup_close_
int fd
= -EBADF
;
3697 _cleanup_free_
char *j
= NULL
;
3698 union sockaddr_union sa
= {
3699 .un
.sun_family
= AF_UNIX
,
3703 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
3705 return log_error_errno(errno
, "Failed to allocate notification socket: %m");
3708 j
= path_join(directory
, NSPAWN_NOTIFY_SOCKET_PATH
);
3713 r
= sockaddr_un_set_path(&sa
.un
, j
?: NSPAWN_NOTIFY_SOCKET_PATH
);
3715 return log_error_errno(r
, "Failed to set AF_UNIX path to %s: %m", j
?: NSPAWN_NOTIFY_SOCKET_PATH
);
3717 (void) mkdir_parents(sa
.un
.sun_path
, 0755);
3718 (void) sockaddr_un_unlink(&sa
.un
);
3720 WITH_UMASK(0577) { /* only set "w" bit, which is all that's necessary for connecting from the container */
3721 r
= bind(fd
, &sa
.sa
, sockaddr_un_len(&sa
.un
));
3723 return log_error_errno(errno
, "bind(" NSPAWN_NOTIFY_SOCKET_PATH
") failed: %m");
3726 r
= userns_lchown(sa
.un
.sun_path
, 0, 0);
3728 return log_error_errno(r
, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH
": %m");
3730 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
3732 return log_error_errno(r
, "Failed to enable SO_PASSCRED: %m");
3734 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSPIDFD
, true);
3736 log_debug_errno(r
, "Failed to enable SO_PASSPIDFD, ignoring: %m");
3738 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSRIGHTS
, false);
3740 log_debug_errno(r
, "Failed to turn off SO_PASSRIGHTS, ignoring: %m");
3745 static int setup_unix_export_dir_outside(char **ret
) {
3750 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
3751 log_debug("Not digging socket tunnel, because running in managed user namespace mode.");
3755 _cleanup_free_
char *p
= NULL
;
3756 p
= path_join("/run/systemd/nspawn/unix-export", arg_machine
);
3760 r
= path_is_mount_point(p
);
3762 return log_error_errno(SYNTHETIC_ERRNO(EEXIST
), "Mount point '%s' exists already, refusing.", p
);
3763 if (r
< 0 && r
!= -ENOENT
)
3764 return log_error_errno(r
, "Failed to detect if '%s' is a mount point: %m", p
);
3766 r
= mkdir_p(p
, 0755);
3768 return log_error_errno(r
, "Failed to create '%s': %m", p
);
3770 _cleanup_(rmdir_and_freep
) char *q
= TAKE_PTR(p
);
3772 /* Mount the "unix export" directory really tiny, just 64 inodes. We mark the superblock writable
3773 * (since the container shall bind sockets into it). */
3774 r
= mount_nofollow_verbose(
3779 MS_NODEV
|MS_NOEXEC
|MS_NOSUID
|ms_nosymfollow_supported(),
3780 "size=4M,nr_inodes=64,mode=0755");
3784 _cleanup_(umount_and_rmdir_and_freep
) char *w
= TAKE_PTR(q
);
3786 /* After creating the superblock we change the bind mount to be read-only. This means that the fs
3787 * itself is writable, but not through the mount accessible from the host. */
3788 r
= mount_nofollow_verbose(
3793 MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NODEV
|MS_NOEXEC
|MS_NOSUID
|ms_nosymfollow_supported(),
3794 /* options= */ NULL
);
3802 static int setup_unix_export_host_inside(const char *directory
, const char *unix_export_path
) {
3807 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
)
3810 assert(unix_export_path
);
3812 r
= make_run_host(directory
);
3816 _cleanup_free_
char *p
= path_join(directory
, "run/host/unix-export");
3820 if (mkdir(p
, 0755) < 0)
3821 return log_error_errno(errno
, "Failed to create '%s': %m", p
);
3823 r
= mount_nofollow_verbose(
3829 /* options= */ NULL
);
3833 r
= mount_nofollow_verbose(
3838 MS_BIND
|MS_REMOUNT
|MS_NODEV
|MS_NOEXEC
|MS_NOSUID
|ms_nosymfollow_supported(),
3839 /* options= */ NULL
);
3843 r
= userns_lchown(p
, 0, 0);
3845 return log_error_errno(r
, "Failed to chown '%s': %m", p
);
3850 static DissectImageFlags
determine_dissect_image_flags(void) {
3852 DISSECT_IMAGE_GENERIC_ROOT
|
3853 DISSECT_IMAGE_REQUIRE_ROOT
|
3854 DISSECT_IMAGE_RELAX_VAR_CHECK
|
3855 DISSECT_IMAGE_USR_NO_ROOT
|
3856 DISSECT_IMAGE_DISCARD_ON_LOOP
|
3857 DISSECT_IMAGE_ADD_PARTITION_DEVICES
|
3858 DISSECT_IMAGE_PIN_PARTITION_DEVICES
|
3859 (arg_read_only
? DISSECT_IMAGE_READ_ONLY
: DISSECT_IMAGE_FSCK
|DISSECT_IMAGE_GROWFS
) |
3860 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY
|
3861 (arg_console_mode
== CONSOLE_INTERACTIVE
&& arg_ask_password
? DISSECT_IMAGE_ALLOW_INTERACTIVE_AUTH
: 0) |
3862 ((arg_userns_ownership
== USER_NAMESPACE_OWNERSHIP_FOREIGN
) ? DISSECT_IMAGE_FOREIGN_UID
:
3863 (arg_userns_ownership
!= USER_NAMESPACE_OWNERSHIP_AUTO
) ? DISSECT_IMAGE_IDENTITY_UID
: 0);
3866 static int outer_child(
3868 const char *directory
,
3870 DissectedImage
*dissected_image
,
3871 int fd_outer_socket
,
3872 int fd_inner_socket
,
3875 const char *unix_export_path
) {
3877 _cleanup_strv_free_
char **os_release_pairs
= NULL
;
3882 /* This is the "outer" child process, i.e the one forked off by the container manager itself. Its
3883 * namespace situation is:
3885 * - CLONE_NEWUSER : if not in USER_NAMESPACE_MANAGED mode: still in the host's
3886 * if USER_NAMESPACE_MANAGED mode: already has its own (created by nsresource_allocate_userns()->setns(userns_fd))
3887 * - CLONE_NEWNS : already has its own (created by clone() if not USER_NAMESPACE_MANAGED, or unshare() otherwise)
3888 * - CLONE_NEWPID : still in the host's
3889 * - CLONE_NEWUTS : still in the host's
3890 * - CLONE_NEWIPC : still in the host's
3891 * - CLONE_NEWNET : still in the host's
3893 * After it completed a number of initializations a second child (the "inner" one) is forked off it,
3898 assert(fd_outer_socket
>= 0);
3899 assert(fd_inner_socket
>= 0);
3901 log_debug("Outer child is initializing.");
3903 r
= load_os_release_pairs_with_prefix("/", "container_host_", &os_release_pairs
);
3905 log_debug_errno(r
, "Failed to read os-release from host for container, ignoring: %m");
3907 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
) < 0)
3908 return log_error_errno(errno
, "PR_SET_PDEATHSIG failed: %m");
3910 r
= reset_audit_loginuid();
3914 /* Mark everything as slave, so that we still receive mounts from the real root, but don't propagate
3915 * mounts to the real root. */
3916 r
= mount_follow_verbose(LOG_ERR
, NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
);
3920 /* Put the root dir into the target directory now. One of three mechanisms is provided: either we
3921 * have a single mount fd (typically unprivileged --directory= mode) or we have a fully dissected
3922 * image (--image= mode), or we have a regular path. */
3923 if (mount_fd
>= 0) {
3924 assert(arg_directory
);
3927 if (move_mount(mount_fd
, "", AT_FDCWD
, directory
, MOVE_MOUNT_F_EMPTY_PATH
) < 0)
3928 return log_error_errno(errno
, "Failed to attach root directory: %m");
3930 mount_fd
= safe_close(mount_fd
);
3931 log_debug("Successfully attached root directory to '%s'.", directory
);
3933 } else if (dissected_image
) {
3934 assert(!arg_directory
);
3937 /* If we are operating on a disk image, then mount its root directory now, but leave out the
3938 * rest. We can read the UID shift from it if we need to. Further down we'll mount the rest,
3939 * but then with the uid shift known. That way we can mount VFAT file systems shifted to the
3940 * right place right away. This makes sure ESP partitions and userns are compatible. */
3942 r
= dissected_image_mount_and_warn(
3947 /* userns_fd= */ -EBADF
,
3948 determine_dissect_image_flags()|
3949 DISSECT_IMAGE_MOUNT_ROOT_ONLY
|
3950 (arg_start_mode
== START_BOOT
? DISSECT_IMAGE_VALIDATE_OS
: 0));
3954 assert(arg_directory
);
3957 r
= mount_nofollow_verbose(LOG_ERR
, arg_directory
, directory
, /* fstype= */ NULL
, MS_BIND
|MS_REC
, /* options= */ NULL
);
3962 r
= determine_uid_shift(directory
);
3966 /* If we do userns on our own, we need to chown() all files ourselves before. Otherwise, if userns is
3967 * off or we are in managed mode we already have the userns applied, hence don't need to chown
3969 uid_t chown_uid
, chown_range
;
3970 if (in_child_chown()) {
3971 chown_uid
= arg_uid_shift
;
3972 chown_range
= arg_uid_range
;
3975 chown_range
= UINT32_C(0x10000);
3978 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
3979 _cleanup_close_
int mntns_fd
= -EBADF
;
3981 mntns_fd
= namespace_open_by_type(NAMESPACE_MOUNT
);
3983 return log_error_errno(mntns_fd
, "Failed to pin outer mount namespace: %m");
3985 l
= send_one_fd(fd_outer_socket
, mntns_fd
, 0);
3987 return log_error_errno(l
, "Failed to send outer mount namespace fd: %m");
3988 mntns_fd
= safe_close(mntns_fd
);
3990 /* Let the parent know which UID shift we read from the image */
3991 l
= send(fd_outer_socket
, &arg_uid_shift
, sizeof(arg_uid_shift
), MSG_NOSIGNAL
);
3993 return log_error_errno(errno
, "Failed to send UID shift: %m");
3994 if (l
!= sizeof(arg_uid_shift
))
3995 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
3996 "Short write while sending UID shift.");
3998 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
3999 /* When we are supposed to pick the UID shift, the parent will check now whether the
4000 * UID shift we just read from the image is available. If yes, it will send the UID
4001 * shift back to us, if not it will pick a different one, and send it back to us. */
4003 l
= recv(fd_outer_socket
, &arg_uid_shift
, sizeof(arg_uid_shift
), 0);
4005 return log_error_errno(errno
, "Failed to recv UID shift: %m");
4006 if (l
!= sizeof(arg_uid_shift
))
4007 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
4008 "Short read while receiving UID shift.");
4010 if (in_child_chown())
4011 chown_uid
= arg_uid_shift
;
4014 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
,
4015 "Selected user namespace base " UID_FMT
" and range " UID_FMT
".", arg_uid_shift
, arg_uid_range
);
4018 /* So the whole tree is now MS_SLAVE, i.e. we'll still receive mount/umount events from the host
4019 * mount namespace. For the directory we are going to run our container let's turn this off, so that
4020 * we'll live in our own little world from now on, and propagation from the host may only happen via
4021 * the mount tunnel dir, or not at all. */
4022 r
= mount_follow_verbose(LOG_ERR
, NULL
, directory
, NULL
, MS_PRIVATE
|MS_REC
, NULL
);
4026 r
= setup_pivot_root(
4029 arg_pivot_root_old
);
4033 r
= setup_volatile_mode(
4037 arg_selinux_apifs_context
);
4041 _cleanup_(machine_bind_user_context_freep
) MachineBindUserContext
*bind_user_context
= NULL
;
4042 r
= machine_bind_user_prepare(
4045 arg_bind_user_shell
,
4046 arg_bind_user_shell_copy
,
4047 &bind_user_context
);
4051 if (bind_user_context
)
4052 FOREACH_ARRAY(bind_user
, bind_user_context
->data
, bind_user_context
->n_data
) {
4053 _cleanup_free_
char *sm
= strdup(user_record_home_directory(bind_user
->host_user
));
4057 _cleanup_free_
char *sd
= strdup(user_record_home_directory(bind_user
->payload_user
));
4061 if (!GREEDY_REALLOC(arg_custom_mounts
, arg_n_custom_mounts
+ 1))
4064 char *options
= strdup("owneridmap");
4068 arg_custom_mounts
[arg_n_custom_mounts
++] = (CustomMount
) {
4069 .type
= CUSTOM_MOUNT_BIND
,
4070 .source
= TAKE_PTR(sm
),
4071 .destination
= TAKE_PTR(sd
),
4072 .options
= TAKE_PTR(options
),
4073 .destination_uid
= bind_user
->payload_user
->uid
,
4080 arg_n_custom_mounts
,
4083 arg_selinux_apifs_context
,
4088 if (!IN_SET(arg_userns_mode
, USER_NAMESPACE_NO
, USER_NAMESPACE_MANAGED
) &&
4089 IN_SET(arg_userns_ownership
, USER_NAMESPACE_OWNERSHIP_MAP
, USER_NAMESPACE_OWNERSHIP_FOREIGN
, USER_NAMESPACE_OWNERSHIP_AUTO
) &&
4091 _cleanup_strv_free_
char **dirs
= NULL
;
4092 RemountIdmapping mapping
;
4094 switch (arg_userns_ownership
) {
4095 case USER_NAMESPACE_OWNERSHIP_MAP
:
4096 mapping
= REMOUNT_IDMAPPING_HOST_ROOT
;
4099 case USER_NAMESPACE_OWNERSHIP_FOREIGN
:
4100 mapping
= REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT
;
4103 case USER_NAMESPACE_OWNERSHIP_AUTO
: {
4106 if (lstat(directory
, &st
) < 0)
4107 return log_error_errno(errno
, "Failed to stat() container root directory '%s': %m", directory
);
4109 r
= stat_verify_directory(&st
);
4111 return log_error_errno(r
, "Container root directory '%s' is not a directory: %m", directory
);
4113 mapping
= uid_is_foreign(st
.st_uid
) ?
4114 REMOUNT_IDMAPPING_FOREIGN_WITH_HOST_ROOT
:
4115 REMOUNT_IDMAPPING_HOST_ROOT
;
4120 assert_not_reached();
4123 if (arg_volatile_mode
!= VOLATILE_YES
) {
4124 r
= strv_extend(&dirs
, directory
);
4129 if ((dissected_image
&& dissected_image
->partitions
[PARTITION_USR
].found
) ||
4130 arg_volatile_mode
== VOLATILE_YES
) {
4131 char *s
= path_join(directory
, "/usr");
4135 r
= strv_consume(&dirs
, s
);
4144 /* host_owner= */ UID_INVALID
,
4145 /* dest_owner= */ UID_INVALID
,
4147 if (r
== -EINVAL
|| ERRNO_IS_NEG_NOT_SUPPORTED(r
)) {
4148 /* This might fail because the kernel or file system doesn't support idmapping. We
4149 * can't really distinguish this nicely, nor do we have any guarantees about the
4150 * error codes we see, could be EOPNOTSUPP or EINVAL. */
4151 if (arg_userns_ownership
!= USER_NAMESPACE_OWNERSHIP_AUTO
)
4152 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
4153 "ID mapped mounts are apparently not available, sorry.");
4155 log_debug("ID mapped mounts are apparently not available on this kernel or for the selected file system, reverting to recursive chown()ing.");
4156 arg_userns_ownership
= USER_NAMESPACE_OWNERSHIP_CHOWN
;
4158 return log_error_errno(r
, "Failed to set up ID mapped mounts: %m");
4160 log_debug("ID mapped mounts available, making use of them.");
4165 r
= setup_volatile_mode_after_remount_idmap(
4169 arg_selinux_apifs_context
);
4173 if (dissected_image
) {
4174 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
4175 r
= dissected_image_mount_and_warn(
4180 /* userns_fd= */ -EBADF
,
4181 determine_dissect_image_flags()|
4182 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY
|
4183 (idmap
? DISSECT_IMAGE_MOUNT_IDMAPPED
: 0));
4188 r
= recursive_chown(directory
, chown_uid
, chown_range
);
4192 r
= base_filesystem_create(directory
, chown_uid
, (gid_t
) chown_uid
);
4196 if (arg_read_only
&& arg_volatile_mode
== VOLATILE_NO
&&
4197 !has_custom_root_mount(arg_custom_mounts
, arg_n_custom_mounts
)) {
4198 r
= bind_remount_recursive(directory
, MS_RDONLY
, MS_RDONLY
, NULL
);
4200 return log_error_errno(r
, "Failed to make tree read-only: %m");
4203 r
= mount_all(directory
,
4206 arg_selinux_apifs_context
);
4210 r
= copy_devnodes(directory
);
4214 r
= make_extra_nodes(directory
);
4218 (void) dev_setup(directory
, chown_uid
, chown_uid
);
4220 _cleanup_free_
char *p
= path_join(directory
, "/run/host");
4224 (void) make_inaccessible_nodes(p
, chown_uid
, chown_uid
);
4226 r
= setup_unix_export_host_inside(directory
, unix_export_path
);
4230 r
= setup_pts(directory
, chown_uid
);
4234 r
= mount_tunnel_dig(directory
);
4238 r
= setup_keyring();
4242 r
= setup_credentials(directory
);
4246 r
= bind_user_setup(bind_user_context
, directory
);
4253 arg_n_custom_mounts
,
4256 arg_selinux_apifs_context
,
4257 MOUNT_NON_ROOT_ONLY
);
4261 r
= setup_timezone(directory
);
4265 r
= setup_resolv_conf(directory
);
4269 r
= setup_machine_id(directory
);
4273 r
= setup_journal(directory
);
4277 /* The same stuff as the $container env var, but nicely readable for the entire payload */
4279 p
= path_join(directory
, "/run/host/container-manager");
4283 (void) write_string_file(p
, arg_container_service_name
, WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_MODE_0444
);
4285 /* The same stuff as the $container_uuid env var */
4287 p
= path_join(directory
, "/run/host/container-uuid");
4291 (void) write_string_filef(p
, WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_MODE_0444
, SD_ID128_UUID_FORMAT_STR
, SD_ID128_FORMAT_VAL(arg_uuid
));
4293 if (!arg_use_cgns
) {
4294 r
= mount_cgroups(directory
, /* accept_existing = */ true);
4299 /* We have different codepaths here for privileged and non-privileged mode. In privileged mode we'll
4300 * now switch into the target directory, and then do the final setup from there. If a user namespace
4301 * is then allocated for the container, the root mount and everything else will be out of reach for
4302 * it. For unprivileged containers we cannot do that however, since we couldn't mount a sysfs and
4303 * procfs then anymore, since that only works if there's an unobstructed instance currently
4304 * visible. Hence there we do it the other way round: we first allocate a new set of namespaces
4305 * (and fork for it) for which we then mount sysfs/procfs, and only then switch root. */
4307 _cleanup_close_
int notify_fd
= -EBADF
;
4308 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
4309 /* Mark everything as shared so our mounts get propagated down. This is required to make new
4310 * bind mounts available in systemd services inside the container that create a new mount
4311 * namespace. See https://github.com/systemd/systemd/issues/3860 Further submounts (such as
4312 * /dev/) done after this will inherit the shared propagation mode.
4314 * IMPORTANT: Do not overmount the root directory anymore from now on to enable moving the root
4315 * directory mount to root later on.
4316 * https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
4318 r
= mount_switch_root(directory
, MS_SHARED
);
4320 return log_error_errno(r
, "Failed to move root directory: %m");
4322 /* We finished setting up the rootfs which is a shared mount. The mount tunnel needs to be a
4323 * dependent mount otherwise we can't MS_MOVE mounts that were propagated from the host into
4325 r
= mount_tunnel_open();
4329 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
4330 /* In order to mount procfs and sysfs in an unprivileged container the kernel
4331 * requires that a fully visible instance is already present in the target mount
4332 * namespace. Mount one here so the inner child can mount its own instances. Later
4333 * we umount the temporary instances created here before we actually exec the
4334 * payload. Since the rootfs is shared the umount will propagate into the container.
4335 * Note, the inner child wouldn't be able to unmount the instances on its own since
4336 * it doesn't own the originating mount namespace. IOW, the outer child needs to do
4338 r
= pin_fully_visible_api_fs();
4343 notify_fd
= setup_notify_child(NULL
);
4345 notify_fd
= setup_notify_child(directory
);
4349 pid_t pid
= raw_clone(SIGCHLD
|CLONE_NEWNS
|
4350 arg_clone_ns_flags
|
4351 (IN_SET(arg_userns_mode
, USER_NAMESPACE_FIXED
, USER_NAMESPACE_PICK
) ? CLONE_NEWUSER
: 0) |
4352 ((arg_private_network
&& arg_userns_mode
== USER_NAMESPACE_MANAGED
) ? CLONE_NEWNET
: 0));
4354 return log_error_errno(errno
, "Failed to fork inner child: %m");
4356 fd_outer_socket
= safe_close(fd_outer_socket
);
4358 /* The inner child has all namespaces that are requested, so that we all are owned by the
4359 * user if user namespaces are turned on. */
4361 if (arg_network_namespace_path
) {
4362 r
= namespace_enter(/* pidns_fd = */ -EBADF
,
4363 /* mntns_fd = */ -EBADF
,
4365 /* userns_fd = */ -EBADF
,
4366 /* root_fd = */ -EBADF
);
4368 return log_error_errno(r
, "Failed to join network namespace: %m");
4371 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
4372 /* In managed usernamespace operation, sysfs + procfs are special, we'll have to
4373 * mount them inside the inner namespaces, but before we switch root. Hence do so
4375 _cleanup_free_
char *j
= path_join(directory
, "/proc");
4379 r
= mount_follow_verbose(LOG_ERR
, "proc", j
, "proc", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
4383 r
= mount_sysfs(directory
, arg_mount_settings
);
4387 r
= mount_switch_root(directory
, MS_SHARED
);
4389 return log_error_errno(r
, "Failed to move root directory: %m");
4392 r
= inner_child(barrier
, fd_inner_socket
, fds
, os_release_pairs
);
4394 _exit(EXIT_FAILURE
);
4396 _exit(EXIT_SUCCESS
);
4399 l
= send(fd_outer_socket
, &pid
, sizeof(pid
), MSG_NOSIGNAL
);
4401 return log_error_errno(errno
, "Failed to send PID: %m");
4402 if (l
!= sizeof(pid
))
4403 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
4404 "Short write while sending PID.");
4406 l
= send(fd_outer_socket
, &arg_uuid
, sizeof(arg_uuid
), MSG_NOSIGNAL
);
4408 return log_error_errno(errno
, "Failed to send machine ID: %m");
4409 if (l
!= sizeof(arg_uuid
))
4410 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
4411 "Short write while sending machine ID.");
4413 l
= send_one_fd(fd_outer_socket
, notify_fd
, 0);
4415 return log_error_errno(l
, "Failed to send notify fd: %m");
4417 fd_outer_socket
= safe_close(fd_outer_socket
);
4418 fd_inner_socket
= safe_close(fd_inner_socket
);
4419 netns_fd
= safe_close(netns_fd
);
4424 static int uid_shift_pick(uid_t
*shift
, LockFile
*ret_lock_file
) {
4425 bool tried_hashed
= false;
4426 unsigned n_tries
= 100;
4431 assert(ret_lock_file
);
4432 assert(arg_userns_mode
== USER_NAMESPACE_PICK
);
4433 assert(arg_uid_range
== 0x10000U
);
4437 (void) mkdir("/run/systemd/nspawn-uid", 0755);
4440 char lock_path
[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t
) + 1];
4441 _cleanup_(release_lock_file
) LockFile lf
= LOCK_FILE_INIT
;
4446 if (candidate
< CONTAINER_UID_BASE_MIN
|| candidate
> CONTAINER_UID_BASE_MAX
)
4448 if ((candidate
& UINT32_C(0xFFFF)) != 0)
4451 xsprintf(lock_path
, "/run/systemd/nspawn-uid/" UID_FMT
, candidate
);
4452 r
= make_lock_file(lock_path
, LOCK_EX
|LOCK_NB
, &lf
);
4453 if (r
== -EBUSY
) /* Range already taken by another nspawn instance */
4458 /* Make some superficial checks whether the range is currently known in the user database */
4459 if (getpwuid_malloc(candidate
, /* ret= */ NULL
) >= 0)
4461 if (getpwuid_malloc(candidate
+ UINT32_C(0xFFFE), /* ret= */ NULL
) >= 0)
4463 if (getgrgid_malloc(candidate
, /* ret= */ NULL
) >= 0)
4465 if (getgrgid_malloc(candidate
+ UINT32_C(0xFFFE), /* ret= */ NULL
) >= 0)
4468 *ret_lock_file
= lf
;
4469 lf
= (struct LockFile
) LOCK_FILE_INIT
;
4474 if (arg_machine
&& !tried_hashed
) {
4475 /* Try to hash the base from the container name */
4477 static const uint8_t hash_key
[] = {
4478 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
4479 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
4482 candidate
= (uid_t
) siphash24(arg_machine
, strlen(arg_machine
), hash_key
);
4484 tried_hashed
= true;
4486 random_bytes(&candidate
, sizeof(candidate
));
4488 candidate
= (candidate
% (CONTAINER_UID_BASE_MAX
- CONTAINER_UID_BASE_MIN
)) + CONTAINER_UID_BASE_MIN
;
4489 candidate
&= (uid_t
) UINT32_C(0xFFFF0000);
4493 static int setup_uid_map(
4495 const uid_t bind_user_uid
[],
4496 size_t n_bind_user_uid
) {
4498 char uid_map
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t
) + 1];
4499 _cleanup_free_
char *s
= NULL
;
4502 assert(pidref_is_set(pid
));
4503 assert(pid
->pid
> 1);
4505 if (asprintf(&s
, "0 " UID_FMT
" " UID_FMT
"\n", arg_uid_shift
, arg_uid_range
) < 0)
4508 xsprintf(uid_map
, "/proc/" PID_FMT
"/uid_map", pid
->pid
);
4509 r
= write_string_file(uid_map
, s
, WRITE_STRING_FILE_DISABLE_BUFFER
);
4511 return log_error_errno(r
, "Failed to write UID map: %m");
4513 xsprintf(uid_map
, "/proc/" PID_FMT
"/gid_map", pid
->pid
);
4514 r
= write_string_file(uid_map
, s
, WRITE_STRING_FILE_DISABLE_BUFFER
);
4516 return log_error_errno(r
, "Failed to write GID map: %m");
4521 static int nspawn_dispatch_notify_fd(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
4522 PidRef
*inner_child_pid
= ASSERT_PTR(userdata
);
4527 _cleanup_(pidref_done
) PidRef sender_pid
= PIDREF_NULL
;
4528 _cleanup_strv_free_
char **tags
= NULL
;
4529 r
= notify_recv_strv(fd
, &tags
, /* ret_ucred= */ NULL
, &sender_pid
);
4535 if (!pidref_equal(&sender_pid
, inner_child_pid
)) {
4536 log_debug("Received notify message from process that is not the payload's PID 1. Ignoring.");
4540 if (DEBUG_LOGGING
) {
4541 _cleanup_free_
char *joined
= strv_join(tags
, " ");
4544 _cleanup_free_
char *j
= cescape(joined
);
4545 free_and_replace(joined
, j
);
4548 log_debug("Got sd_notify() message: %s", strnull(joined
));
4551 char *status
= strv_find_startswith(tags
, "STATUS=");
4553 (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running: %s", status
);
4555 if (strv_contains(tags
, "READY=1")) {
4556 r
= sd_notify(/* unset_environment= */ false, "READY=1\n");
4558 log_warning_errno(r
, "Failed to send readiness notification, ignoring: %m");
4561 (void) sd_notifyf(/* unset_environment= */ false, "STATUS=Container running.");
4567 static int setup_notify_parent(sd_event
*event
, int fd
, PidRef
*inner_child_pid
, sd_event_source
**notify_event_source
) {
4573 r
= sd_event_add_io(event
, notify_event_source
, fd
, EPOLLIN
, nspawn_dispatch_notify_fd
, inner_child_pid
);
4575 return log_error_errno(r
, "Failed to allocate notify event source: %m");
4577 (void) sd_event_source_set_description(*notify_event_source
, "nspawn-notify");
4582 static int ptyfwd_hotkey(PTYForward
*f
, char c
, void *userdata
) {
4583 PidRef
*pid
= ASSERT_PTR(userdata
);
4601 log_info("Unknown hotkey sequence ^]^]%c, ignoring.", c
);
4605 r
= pidref_kill(pid
, sig
);
4607 log_error_errno(r
, "Failed to send %s (%s request) to PID 1 of container: %m", signal_to_string(sig
), word
);
4609 log_info("Sent %s (%s request) to PID 1 of container.", signal_to_string(sig
), word
);
4614 static int merge_settings(Settings
*settings
, const char *path
) {
4620 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
4621 * that this steals the fields of the Settings* structure, and hence modifies it. */
4623 if ((arg_settings_mask
& SETTING_START_MODE
) == 0 &&
4624 settings
->start_mode
>= 0) {
4625 arg_start_mode
= settings
->start_mode
;
4626 strv_free_and_replace(arg_parameters
, settings
->parameters
);
4629 if ((arg_settings_mask
& SETTING_EPHEMERAL
) == 0 &&
4630 settings
->ephemeral
>= 0)
4631 arg_ephemeral
= settings
->ephemeral
;
4633 if ((arg_settings_mask
& SETTING_DIRECTORY
) == 0 &&
4636 if (!arg_settings_trusted
)
4637 log_warning("Ignoring root directory setting, file %s is not trusted.", path
);
4639 free_and_replace(arg_directory
, settings
->root
);
4642 if ((arg_settings_mask
& SETTING_PIVOT_ROOT
) == 0 &&
4643 settings
->pivot_root_new
) {
4644 free_and_replace(arg_pivot_root_new
, settings
->pivot_root_new
);
4645 free_and_replace(arg_pivot_root_old
, settings
->pivot_root_old
);
4648 if ((arg_settings_mask
& SETTING_WORKING_DIRECTORY
) == 0 &&
4649 settings
->working_directory
)
4650 free_and_replace(arg_chdir
, settings
->working_directory
);
4652 if ((arg_settings_mask
& SETTING_ENVIRONMENT
) == 0 &&
4653 settings
->environment
)
4654 strv_free_and_replace(arg_setenv
, settings
->environment
);
4656 if ((arg_settings_mask
& SETTING_USER
) == 0) {
4659 free_and_replace(arg_user
, settings
->user
);
4661 if (uid_is_valid(settings
->uid
))
4662 arg_uid
= settings
->uid
;
4663 if (gid_is_valid(settings
->gid
))
4664 arg_gid
= settings
->gid
;
4665 if (settings
->n_supplementary_gids
> 0) {
4666 free_and_replace(arg_supplementary_gids
, settings
->supplementary_gids
);
4667 arg_n_supplementary_gids
= settings
->n_supplementary_gids
;
4671 if ((arg_settings_mask
& SETTING_CAPABILITY
) == 0) {
4672 uint64_t plus
, minus
;
4673 uint64_t network_minus
= 0;
4676 /* Note that we copy both the simple plus/minus caps here, and the full quintet from the
4677 * Settings structure */
4679 plus
= settings
->capability
;
4680 minus
= settings
->drop_capability
;
4682 if ((arg_settings_mask
& SETTING_NETWORK
) == 0 &&
4683 settings_network_configured(settings
)) {
4684 if (settings_private_network(settings
))
4685 plus
|= UINT64_C(1) << CAP_NET_ADMIN
;
4687 network_minus
|= UINT64_C(1) << CAP_NET_ADMIN
;
4690 if (!arg_settings_trusted
&& plus
!= 0) {
4691 if (settings
->capability
!= 0)
4692 log_warning("Ignoring Capability= setting, file %s is not trusted.", path
);
4694 arg_caps_retain
&= ~network_minus
;
4695 arg_caps_retain
|= plus
;
4698 arg_caps_retain
&= ~minus
;
4700 /* Copy the full capabilities over too */
4701 if (capability_quintet_is_set(&settings
->full_capabilities
)) {
4702 if (!arg_settings_trusted
)
4703 log_warning("Ignoring capability settings, file %s is not trusted.", path
);
4705 arg_full_capabilities
= settings
->full_capabilities
;
4708 ambient
= settings
->ambient_capability
;
4709 if (!arg_settings_trusted
&& ambient
!= 0)
4710 log_warning("Ignoring AmbientCapability= setting, file %s is not trusted.", path
);
4712 arg_caps_ambient
|= ambient
;
4715 if ((arg_settings_mask
& SETTING_KILL_SIGNAL
) == 0 &&
4716 settings
->kill_signal
> 0)
4717 arg_kill_signal
= settings
->kill_signal
;
4719 if ((arg_settings_mask
& SETTING_PERSONALITY
) == 0 &&
4720 settings
->personality
!= PERSONALITY_INVALID
)
4721 arg_personality
= settings
->personality
;
4723 if ((arg_settings_mask
& SETTING_MACHINE_ID
) == 0 &&
4724 !sd_id128_is_null(settings
->machine_id
)) {
4726 if (!arg_settings_trusted
)
4727 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path
);
4729 arg_uuid
= settings
->machine_id
;
4732 if ((arg_settings_mask
& SETTING_READ_ONLY
) == 0 &&
4733 settings
->read_only
>= 0)
4734 arg_read_only
= settings
->read_only
;
4736 if ((arg_settings_mask
& SETTING_VOLATILE_MODE
) == 0 &&
4737 settings
->volatile_mode
!= _VOLATILE_MODE_INVALID
)
4738 arg_volatile_mode
= settings
->volatile_mode
;
4740 if ((arg_settings_mask
& SETTING_CUSTOM_MOUNTS
) == 0 &&
4741 settings
->n_custom_mounts
> 0) {
4743 if (!arg_settings_trusted
)
4744 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path
);
4746 custom_mount_free_all(arg_custom_mounts
, arg_n_custom_mounts
);
4747 arg_custom_mounts
= TAKE_PTR(settings
->custom_mounts
);
4748 arg_n_custom_mounts
= settings
->n_custom_mounts
;
4749 settings
->n_custom_mounts
= 0;
4753 if ((arg_settings_mask
& SETTING_NETWORK
) == 0 &&
4754 settings_network_configured(settings
)) {
4756 if (!arg_settings_trusted
)
4757 log_warning("Ignoring network settings, file %s is not trusted.", path
);
4759 arg_network_veth
= settings_network_veth(settings
);
4760 arg_private_network
= settings_private_network(settings
);
4762 strv_free_and_replace(arg_network_interfaces
, settings
->network_interfaces
);
4763 strv_free_and_replace(arg_network_macvlan
, settings
->network_macvlan
);
4764 strv_free_and_replace(arg_network_ipvlan
, settings
->network_ipvlan
);
4765 strv_free_and_replace(arg_network_veth_extra
, settings
->network_veth_extra
);
4767 free_and_replace(arg_network_bridge
, settings
->network_bridge
);
4768 free_and_replace(arg_network_zone
, settings
->network_zone
);
4770 free_and_replace(arg_network_namespace_path
, settings
->network_namespace_path
);
4774 if ((arg_settings_mask
& SETTING_EXPOSE_PORTS
) == 0 &&
4775 settings
->expose_ports
) {
4777 if (!arg_settings_trusted
)
4778 log_warning("Ignoring Port= setting, file %s is not trusted.", path
);
4780 expose_port_free_all(arg_expose_ports
);
4781 arg_expose_ports
= TAKE_PTR(settings
->expose_ports
);
4785 if ((arg_settings_mask
& SETTING_USERNS
) == 0 &&
4786 settings
->userns_mode
!= _USER_NAMESPACE_MODE_INVALID
) {
4788 if (!arg_settings_trusted
)
4789 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path
);
4791 arg_userns_mode
= settings
->userns_mode
;
4792 arg_uid_shift
= settings
->uid_shift
;
4793 arg_uid_range
= settings
->uid_range
;
4794 arg_userns_ownership
= settings
->userns_ownership
;
4798 if ((arg_settings_mask
& SETTING_BIND_USER
) == 0 &&
4799 !strv_isempty(settings
->bind_user
))
4800 strv_free_and_replace(arg_bind_user
, settings
->bind_user
);
4802 if (!FLAGS_SET(arg_settings_mask
, SETTING_BIND_USER_SHELL
) &&
4803 settings
->bind_user_shell_set
) {
4804 free_and_replace(arg_bind_user_shell
, settings
->bind_user_shell
);
4805 arg_bind_user_shell_copy
= settings
->bind_user_shell_copy
;
4808 if ((arg_settings_mask
& SETTING_NOTIFY_READY
) == 0 &&
4809 settings
->notify_ready
>= 0)
4810 arg_notify_ready
= settings
->notify_ready
;
4812 if ((arg_settings_mask
& SETTING_SYSCALL_FILTER
) == 0) {
4814 if (!strv_isempty(settings
->syscall_allow_list
) || !strv_isempty(settings
->syscall_deny_list
)) {
4815 if (!arg_settings_trusted
&& !strv_isempty(settings
->syscall_allow_list
))
4816 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path
);
4818 strv_free_and_replace(arg_syscall_allow_list
, settings
->syscall_allow_list
);
4819 strv_free_and_replace(arg_syscall_deny_list
, settings
->syscall_deny_list
);
4824 if (settings
->seccomp
) {
4825 if (!arg_settings_trusted
)
4826 log_warning("Ignoring SECCOMP filter, file %s is not trusted.", path
);
4828 seccomp_release(arg_seccomp
);
4829 arg_seccomp
= TAKE_PTR(settings
->seccomp
);
4835 for (rl
= 0; rl
< _RLIMIT_MAX
; rl
++) {
4836 if ((arg_settings_mask
& (SETTING_RLIMIT_FIRST
<< rl
)))
4839 if (!settings
->rlimit
[rl
])
4842 if (!arg_settings_trusted
) {
4843 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl
), path
);
4847 free_and_replace(arg_rlimit
[rl
], settings
->rlimit
[rl
]);
4850 if ((arg_settings_mask
& SETTING_HOSTNAME
) == 0 &&
4852 free_and_replace(arg_hostname
, settings
->hostname
);
4854 if ((arg_settings_mask
& SETTING_NO_NEW_PRIVILEGES
) == 0 &&
4855 settings
->no_new_privileges
>= 0)
4856 arg_no_new_privileges
= settings
->no_new_privileges
;
4858 if ((arg_settings_mask
& SETTING_OOM_SCORE_ADJUST
) == 0 &&
4859 settings
->oom_score_adjust_set
) {
4861 if (!arg_settings_trusted
)
4862 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path
);
4864 arg_oom_score_adjust
= settings
->oom_score_adjust
;
4865 arg_oom_score_adjust_set
= true;
4869 if ((arg_settings_mask
& SETTING_CPU_AFFINITY
) == 0 &&
4870 settings
->cpu_set
.set
) {
4872 if (!arg_settings_trusted
)
4873 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path
);
4875 cpu_set_done_and_replace(arg_cpu_set
, settings
->cpu_set
);
4878 if ((arg_settings_mask
& SETTING_RESOLV_CONF
) == 0 &&
4879 settings
->resolv_conf
!= _RESOLV_CONF_MODE_INVALID
)
4880 arg_resolv_conf
= settings
->resolv_conf
;
4882 if ((arg_settings_mask
& SETTING_LINK_JOURNAL
) == 0 &&
4883 settings
->link_journal
!= _LINK_JOURNAL_INVALID
) {
4885 if (!arg_settings_trusted
)
4886 log_warning("Ignoring journal link setting, file '%s' is not trusted.", path
);
4888 arg_link_journal
= settings
->link_journal
;
4889 arg_link_journal_try
= settings
->link_journal_try
;
4893 if ((arg_settings_mask
& SETTING_TIMEZONE
) == 0 &&
4894 settings
->timezone
!= _TIMEZONE_MODE_INVALID
)
4895 arg_timezone
= settings
->timezone
;
4897 if ((arg_settings_mask
& SETTING_SLICE
) == 0 &&
4900 if (!arg_settings_trusted
)
4901 log_warning("Ignoring slice setting, file '%s' is not trusted.", path
);
4903 free_and_replace(arg_slice
, settings
->slice
);
4906 if ((arg_settings_mask
& SETTING_USE_CGNS
) == 0 &&
4907 settings
->use_cgns
>= 0) {
4909 if (!arg_settings_trusted
)
4910 log_warning("Ignoring cgroup namespace setting, file '%s' is not trusted.", path
);
4912 arg_use_cgns
= settings
->use_cgns
;
4915 if ((arg_settings_mask
& SETTING_CLONE_NS_FLAGS
) == 0 &&
4916 settings
->clone_ns_flags
!= ULONG_MAX
) {
4918 if (!arg_settings_trusted
)
4919 log_warning("Ignoring namespace setting, file '%s' is not trusted.", path
);
4921 arg_clone_ns_flags
= settings
->clone_ns_flags
;
4924 if ((arg_settings_mask
& SETTING_CONSOLE_MODE
) == 0 &&
4925 settings
->console_mode
>= 0) {
4927 if (!arg_settings_trusted
)
4928 log_warning("Ignoring console mode setting, file '%s' is not trusted.", path
);
4930 arg_console_mode
= settings
->console_mode
;
4933 if ((arg_settings_mask
& SETTING_SUPPRESS_SYNC
) == 0 &&
4934 settings
->suppress_sync
>= 0)
4935 arg_suppress_sync
= settings
->suppress_sync
;
4937 /* The following properties can only be set through the OCI settings logic, not from the command line, hence we
4938 * don't consult arg_settings_mask for them. */
4940 sd_bus_message_unref(arg_property_message
);
4941 arg_property_message
= TAKE_PTR(settings
->properties
);
4943 arg_console_width
= settings
->console_width
;
4944 arg_console_height
= settings
->console_height
;
4946 device_node_array_free(arg_extra_nodes
, arg_n_extra_nodes
);
4947 arg_extra_nodes
= TAKE_PTR(settings
->extra_nodes
);
4948 arg_n_extra_nodes
= settings
->n_extra_nodes
;
4949 settings
->n_extra_nodes
= 0;
4954 static int load_settings(void) {
4955 _cleanup_(settings_freep
) Settings
*settings
= NULL
;
4956 _cleanup_fclose_
FILE *f
= NULL
;
4957 _cleanup_free_
char *p
= NULL
;
4963 /* If all settings are masked, there's no point in looking for
4964 * the settings file */
4965 if (FLAGS_SET(arg_settings_mask
, _SETTINGS_MASK_ALL
))
4968 /* We first look in the admin's directories in /etc and /run */
4970 FOREACH_STRING(i
, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
4971 _cleanup_free_
char *j
= NULL
;
4973 j
= path_join(i
, arg_settings_filename
);
4981 /* By default, we trust configuration from /etc and /run */
4982 if (arg_settings_trusted
< 0)
4983 arg_settings_trusted
= true;
4988 if (errno
!= ENOENT
)
4989 return log_error_errno(errno
, "Failed to open %s: %m", j
);
4993 /* After that, let's look for a file next to the
4994 * actual image we shall boot. */
4997 r
= file_in_same_dir(arg_image
, arg_settings_filename
, &p
);
4999 return log_error_errno(r
, "Failed to generate settings path from image path: %m");
5000 } else if (arg_directory
) {
5001 r
= file_in_same_dir(arg_directory
, arg_settings_filename
, &p
);
5002 if (r
< 0 && r
!= -EADDRNOTAVAIL
) /* if directory is root fs, don't complain */
5003 return log_error_errno(r
, "Failed to generate settings path from directory path: %m");
5008 if (!f
&& errno
!= ENOENT
)
5009 return log_error_errno(errno
, "Failed to open %s: %m", p
);
5011 /* By default, we do not trust configuration from /var/lib/machines */
5012 if (arg_settings_trusted
< 0)
5013 arg_settings_trusted
= false;
5020 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted
));
5022 r
= settings_load(f
, p
, &settings
);
5026 return merge_settings(settings
, p
);
5029 static int load_oci_bundle(void) {
5030 _cleanup_(settings_freep
) Settings
*settings
= NULL
;
5033 if (!arg_oci_bundle
)
5036 /* By default let's trust OCI bundles */
5037 if (arg_settings_trusted
< 0)
5038 arg_settings_trusted
= true;
5040 r
= oci_load(NULL
, arg_oci_bundle
, &settings
);
5044 return merge_settings(settings
, arg_oci_bundle
);
5047 static int run_container(
5048 const char *directory
,
5050 DissectedImage
*dissected_image
,
5053 char veth_name
[IFNAMSIZ
],
5055 struct ExposeArgs
*expose_args
,
5060 _cleanup_(release_lock_file
) LockFile uid_shift_lock
= LOCK_FILE_INIT
;
5061 _cleanup_close_
int etc_passwd_lock
= -EBADF
;
5062 _cleanup_close_pair_
int
5063 fd_inner_socket_pair
[2] = EBADF_PAIR
,
5064 fd_outer_socket_pair
[2] = EBADF_PAIR
;
5066 _cleanup_close_
int notify_socket
= -EBADF
, mntns_fd
= -EBADF
, fd_kmsg_fifo
= -EBADF
;
5067 _cleanup_(barrier_destroy
) Barrier barrier
= BARRIER_NULL
;
5068 _cleanup_(sd_event_source_unrefp
) sd_event_source
*notify_event_source
= NULL
;
5069 _cleanup_(umount_and_rmdir_and_freep
) char *unix_export_host_dir
= NULL
;
5070 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
5071 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
5072 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
5073 _cleanup_free_ uid_t
*bind_user_uid
= NULL
;
5074 size_t n_bind_user_uid
= 0;
5075 ContainerStatus container_status
= 0;
5079 _cleanup_close_
int child_netns_fd
= -EBADF
;
5081 assert_se(sigemptyset(&mask_chld
) == 0);
5082 assert_se(sigaddset(&mask_chld
, SIGCHLD
) == 0);
5084 /* Set up the unix export host directory on the host first */
5085 r
= setup_unix_export_dir_outside(&unix_export_host_dir
);
5089 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
5090 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
5091 * check with getpwuid() if the specific user already exists. Note that /etc might be
5092 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
5093 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
5094 * really just an extra safety net. We kinda assume that the UID range we allocate from is
5097 etc_passwd_lock
= take_etc_passwd_lock(NULL
);
5098 if (etc_passwd_lock
< 0 && etc_passwd_lock
!= -EROFS
)
5099 return log_error_errno(etc_passwd_lock
, "Failed to take /etc/passwd lock: %m");
5102 r
= barrier_create(&barrier
);
5104 return log_error_errno(r
, "Cannot initialize IPC barrier: %m");
5106 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, fd_inner_socket_pair
) < 0)
5107 return log_error_errno(errno
, "Failed to create inner socket pair: %m");
5109 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, fd_outer_socket_pair
) < 0)
5110 return log_error_errno(errno
, "Failed to create outer socket pair: %m");
5112 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
5113 * parent's blocking calls and give it a chance to call wait() and terminate. */
5114 r
= sigprocmask(SIG_UNBLOCK
, &mask_chld
, NULL
);
5116 return log_error_errno(errno
, "Failed to change the signal mask: %m");
5118 r
= sigaction(SIGCHLD
, &sigaction_nop_nocldstop
, NULL
);
5120 return log_error_errno(errno
, "Failed to install SIGCHLD handler: %m");
5122 if (arg_network_namespace_path
) {
5123 child_netns_fd
= open(arg_network_namespace_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
5124 if (child_netns_fd
< 0)
5125 return log_error_errno(errno
, "Cannot open file %s: %m", arg_network_namespace_path
);
5127 r
= fd_is_namespace(child_netns_fd
, NAMESPACE_NET
);
5129 return log_error_errno(r
, "Failed to check %s fs type: %m", arg_network_namespace_path
);
5131 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
5132 "Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path
);
5136 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
5137 assert(userns_fd
< 0);
5138 /* If we have no user namespace then we'll clone and create a new mount namespace right-away. */
5140 pid_t _pid
= raw_clone(SIGCHLD
|CLONE_NEWNS
);
5142 return log_error_errno(errno
, "clone() failed%s: %m",
5144 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
5146 r
= pidref_set_pid(pid
, _pid
);
5148 return log_error_errno(r
, "Failed to allocate pidfd: %m");
5151 in_child
= _pid
== 0;
5153 assert(userns_fd
>= 0);
5154 /* If we have a user namespace then we'll clone() first, and then join the user namespace,
5155 * and then open the mount namespace, so that it is owned by the user namespace */
5157 pid_t _pid
= raw_clone(SIGCHLD
);
5159 return log_error_errno(errno
, "clone() failed: %m");
5162 r
= pidref_set_pid(pid
, _pid
);
5164 return log_error_errno(r
, "Failed to allocate pidfd: %m");
5167 in_child
= _pid
== 0;
5169 if (setns(userns_fd
, CLONE_NEWUSER
) < 0) {
5170 log_error_errno(errno
, "Failed to join allocate user namespace: %m");
5171 _exit(EXIT_FAILURE
);
5174 r
= reset_uid_gid();
5176 log_error_errno(r
, "Failed to reset UID/GID to root: %m");
5177 _exit(EXIT_FAILURE
);
5180 if (unshare(CLONE_NEWNS
) < 0) {
5181 log_error_errno(errno
, "Failed to unshare file system namespace: %m");
5182 _exit(EXIT_FAILURE
);
5188 /* The outer child only has a file system namespace. */
5189 barrier_set_role(&barrier
, BARRIER_CHILD
);
5191 fd_inner_socket_pair
[0] = safe_close(fd_inner_socket_pair
[0]);
5192 fd_outer_socket_pair
[0] = safe_close(fd_outer_socket_pair
[0]);
5194 (void) reset_all_signal_handlers();
5195 (void) reset_signal_mask();
5197 r
= outer_child(&barrier
,
5201 fd_outer_socket_pair
[1],
5202 fd_inner_socket_pair
[1],
5205 unix_export_host_dir
);
5207 _exit(EXIT_FAILURE
);
5209 _exit(EXIT_SUCCESS
);
5212 barrier_set_role(&barrier
, BARRIER_PARENT
);
5214 fdset_close(fds
, /* async= */ false);
5216 fd_inner_socket_pair
[1] = safe_close(fd_inner_socket_pair
[1]);
5217 fd_outer_socket_pair
[1] = safe_close(fd_outer_socket_pair
[1]);
5219 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
5220 mntns_fd
= receive_one_fd(fd_outer_socket_pair
[0], 0);
5222 return log_error_errno(mntns_fd
, "Failed to receive mount namespace fd from outer child: %m");
5224 /* The child just let us know the UID shift it might have read from the image. */
5225 l
= recv(fd_outer_socket_pair
[0], &arg_uid_shift
, sizeof arg_uid_shift
, 0);
5227 return log_error_errno(errno
, "Failed to read UID shift: %m");
5228 if (l
!= sizeof arg_uid_shift
)
5229 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading UID shift.");
5231 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
5232 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
5233 * image, but if that's already in use, pick a new one, and report back to the child,
5234 * which one we now picked. */
5236 r
= uid_shift_pick(&arg_uid_shift
, &uid_shift_lock
);
5238 return log_error_errno(r
, "Failed to pick suitable UID/GID range: %m");
5240 l
= send(fd_outer_socket_pair
[0], &arg_uid_shift
, sizeof arg_uid_shift
, MSG_NOSIGNAL
);
5242 return log_error_errno(errno
, "Failed to send UID shift: %m");
5243 if (l
!= sizeof arg_uid_shift
)
5244 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short write while writing UID shift.");
5248 /* Wait for the outer child. */
5249 r
= pidref_wait_for_terminate_and_check("(sd-namespace)", pid
, WAIT_LOG_ABNORMAL
);
5253 if (r
!= EXIT_SUCCESS
)
5256 /* And now retrieve the PID of the inner child. */
5258 l
= recv(fd_outer_socket_pair
[0], &_pid
, sizeof _pid
, 0);
5260 return log_error_errno(errno
, "Failed to read inner child PID: %m");
5261 if (l
!= sizeof _pid
)
5262 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading inner child PID.");
5264 r
= pidref_set_pid(pid
, _pid
);
5266 return log_error_errno(r
, "Failed to allocate pidfd: %m");
5268 /* We also retrieve container UUID in case it was generated by outer child */
5269 l
= recv(fd_outer_socket_pair
[0], &arg_uuid
, sizeof arg_uuid
, 0);
5271 return log_error_errno(errno
, "Failed to read container machine ID: %m");
5272 if (l
!= sizeof(arg_uuid
))
5273 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short read while reading container machined ID.");
5275 /* We also retrieve the socket used for notifications generated by outer child */
5276 notify_socket
= receive_one_fd(fd_outer_socket_pair
[0], 0);
5277 if (notify_socket
< 0)
5278 return log_error_errno(notify_socket
,
5279 "Failed to receive notification socket from the outer child: %m");
5281 log_debug("Init process invoked as PID "PID_FMT
, pid
->pid
);
5283 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
5284 if (!barrier_place_and_sync(&barrier
)) /* #1 */
5285 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Child died too early.");
5287 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
5288 r
= setup_uid_map(pid
, bind_user_uid
, n_bind_user_uid
);
5293 (void) barrier_place(&barrier
); /* #2 */
5296 if (arg_private_network
) {
5297 if (!arg_network_namespace_path
) {
5298 /* Wait until the child has unshared its network namespace. */
5299 if (!barrier_place_and_sync(&barrier
)) /* #3 */
5300 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Child died too early");
5302 /* Make sure we have an open file descriptor to the child's network namespace so it
5303 * stays alive even if the child exits. */
5304 assert(child_netns_fd
< 0);
5305 child_netns_fd
= receive_one_fd(fd_inner_socket_pair
[0], 0);
5306 if (child_netns_fd
< 0)
5307 return log_error_errno(r
, "Failed to receive child network namespace: %m");
5310 r
= move_network_interfaces(child_netns_fd
, arg_network_interfaces
);
5314 if (arg_network_veth
) {
5315 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
5316 r
= setup_veth(arg_machine
, pid
, veth_name
,
5317 arg_network_bridge
|| arg_network_zone
, &arg_network_provided_mac
);
5323 _cleanup_free_
char *host_ifname
= NULL
;
5325 r
= nsresource_add_netif_veth(userns_fd
, child_netns_fd
, /* namespace_ifname= */ NULL
, &host_ifname
, /* ret_namespace_ifname= */ NULL
);
5327 return log_error_errno(r
, "Failed to add network interface to container: %m");
5329 ifi
= if_nametoindex(host_ifname
);
5331 return log_error_errno(errno
, "Failed to resolve interface '%s': %m", host_ifname
);
5333 if (strlen(host_ifname
) >= IFNAMSIZ
)
5334 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Host interface name too long?");
5336 strcpy(veth_name
, host_ifname
);
5339 if (arg_network_bridge
) {
5340 /* Add the interface to a bridge */
5341 r
= setup_bridge(veth_name
, arg_network_bridge
, false);
5346 } else if (arg_network_zone
) {
5347 /* Add the interface to a bridge, possibly creating it */
5348 r
= setup_bridge(veth_name
, arg_network_zone
, true);
5356 r
= setup_veth_extra(arg_machine
, pid
, arg_network_veth_extra
);
5360 /* We created the primary and extra veth links now; let's remember this, so that we know to
5361 remove them later on. Note that we don't bother with removing veth links that were created
5362 here when their setup failed half-way, because in that case the kernel should be able to
5363 remove them on its own, since they cannot be referenced by anything yet. */
5364 *veth_created
= true;
5366 r
= setup_macvlan(arg_machine
, pid
, arg_network_macvlan
);
5370 r
= setup_ipvlan(arg_machine
, pid
, arg_network_ipvlan
);
5375 /* Registration always happens on the system bus */
5376 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*system_bus
= NULL
;
5377 if (arg_register
|| arg_privileged
) {
5378 r
= sd_bus_default_system(&system_bus
);
5380 return log_error_errno(r
, "Failed to open system bus: %m");
5382 r
= sd_bus_set_close_on_exit(system_bus
, false);
5384 return log_error_errno(r
, "Failed to disable close-on-exit behaviour: %m");
5386 (void) sd_bus_set_allow_interactive_authorization(system_bus
, arg_ask_password
);
5389 /* Scope allocation happens on the user bus if we are unpriv, otherwise system bus. */
5390 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*user_bus
= NULL
;
5391 _cleanup_(sd_bus_unrefp
) sd_bus
*runtime_bus
= NULL
;
5393 runtime_bus
= sd_bus_ref(system_bus
);
5395 r
= sd_bus_default_user(&user_bus
);
5397 return log_error_errno(r
, "Failed to open user bus: %m");
5399 r
= sd_bus_set_close_on_exit(user_bus
, false);
5401 return log_error_errno(r
, "Failed to disable close-on-exit behaviour: %m");
5403 runtime_bus
= sd_bus_ref(user_bus
);
5406 if (!arg_keep_unit
) {
5407 /* When a new scope is created for this container, then we'll be registered as its controller, in which
5408 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
5409 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
5411 r
= sd_bus_match_signal_async(
5414 "org.freedesktop.systemd1",
5416 "org.freedesktop.systemd1.Scope",
5419 /* install_callback= */ NULL
,
5422 return log_error_errno(r
, "Failed to request RequestStop match: %m");
5425 if (arg_keep_unit
) {
5426 /* If we are not supposed to allocate a unit, then let's move the process now, so that we can
5427 * register things while being in the right cgroup location already. Otherwise, let's move
5428 * the process later, once we have unit and hence cgroup. */
5429 r
= create_subcgroup(
5439 bool scope_allocated
= false;
5440 if (!arg_keep_unit
&& (!arg_register
|| !arg_privileged
)) {
5441 AllocateScopeFlags flags
= ALLOCATE_SCOPE_ALLOW_PIDFD
;
5447 arg_custom_mounts
, arg_n_custom_mounts
,
5450 arg_property_message
,
5456 scope_allocated
= true;
5459 bool registered
= false;
5461 RegisterMachineFlags flags
= 0;
5462 SET_FLAG(flags
, REGISTER_MACHINE_KEEP_UNIT
, arg_keep_unit
|| !arg_privileged
);
5463 r
= register_machine(
5471 arg_custom_mounts
, arg_n_custom_mounts
,
5474 arg_property_message
,
5475 arg_container_service_name
,
5484 if (arg_keep_unit
&& (arg_slice
|| arg_property
))
5485 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
5487 if (!arg_keep_unit
) {
5488 r
= create_subcgroup(
5498 /* Notify the child that the parent is ready with all its setup (including cgroup-ification), and
5499 * that the child can now hand over control to the code to run inside the container. */
5500 (void) barrier_place(&barrier
); /* #4 */
5502 /* Block SIGCHLD here, before notifying child.
5503 * process_pty() will handle it with the other signals. */
5504 assert_se(sigprocmask(SIG_BLOCK
, &mask_chld
, NULL
) >= 0);
5506 /* Reset signal to default */
5507 r
= default_signals(SIGCHLD
);
5509 return log_error_errno(r
, "Failed to reset SIGCHLD: %m");
5511 r
= sd_event_new(&event
);
5513 return log_error_errno(r
, "Failed to get default event source: %m");
5515 (void) sd_event_set_watchdog(event
, true);
5518 r
= sd_bus_attach_event(system_bus
, event
, 0);
5520 return log_error_errno(r
, "Failed to attach system bus to event loop: %m");
5524 r
= sd_bus_attach_event(user_bus
, event
, 0);
5526 return log_error_errno(r
, "Failed to attach user bus to event loop: %m");
5529 r
= setup_notify_parent(event
, notify_socket
, pid
, ¬ify_event_source
);
5533 /* Wait that the child is completely ready now, and has mounted their own copies of procfs and so on,
5534 * before we take the fully visible instances away. */
5535 if (!barrier_sync(&barrier
)) /* #5.1 */
5536 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Child died too early.");
5538 if (!IN_SET(arg_userns_mode
, USER_NAMESPACE_NO
, USER_NAMESPACE_MANAGED
)) {
5539 r
= wipe_fully_visible_api_fs(mntns_fd
);
5542 mntns_fd
= safe_close(mntns_fd
);
5545 /* And now let the child know that we completed removing the procfs instances, and it can start the
5547 if (!barrier_place(&barrier
)) /* #5.2 */
5548 return log_error_errno(SYNTHETIC_ERRNO(ESRCH
), "Child died too early.");
5550 /* At this point we have made use of the UID we picked, and thus nss-systemd/systemd-machined.service
5551 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
5552 etc_passwd_lock
= safe_close(etc_passwd_lock
);
5554 (void) sd_notifyf(/* unset_environment= */ false,
5555 "STATUS=Container started.\n"
5556 "X_NSPAWN_LEADER_PID=" PID_FMT
, pid
->pid
);
5557 if (!arg_notify_ready
) {
5558 r
= sd_notify(/* unset_environment= */ false, "READY=1\n");
5560 log_warning_errno(r
, "Failed to send readiness notification, ignoring: %m");
5563 /* All operations that might need Polkit authorizations (i.e. userns registration, mounts, and
5564 * machine registration are complete now, get rid of the agent again, so that we retain exclusive
5565 * control of the TTY from now on. */
5566 polkit_agent_close();
5568 /* Note: we do not use SD_EVENT_SIGNAL_PROCMASK or sd_event_set_signal_exit(), since we want the
5569 * signals to be block continuously, even if we destroy the event loop and allocate a new one on
5570 * container reboot. */
5572 if (arg_kill_signal
> 0) {
5573 /* Try to kill the init system on SIGINT or SIGTERM */
5574 (void) sd_event_add_signal(event
, NULL
, SIGINT
, on_orderly_shutdown
, pid
);
5575 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, on_orderly_shutdown
, pid
);
5577 /* Immediately exit */
5578 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
5579 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
5582 (void) sd_event_add_signal(event
, NULL
, SIGRTMIN
+18, sigrtmin18_handler
, NULL
);
5584 r
= sd_event_add_memory_pressure(event
, NULL
, NULL
, NULL
);
5586 log_debug_errno(r
, "Failed allocate memory pressure event source, ignoring: %m");
5588 /* Exit when the child exits */
5589 (void) sd_event_add_signal(event
, NULL
, SIGCHLD
, on_sigchld
, pid
);
5591 /* Retrieve the kmsg fifo allocated by inner child */
5592 fd_kmsg_fifo
= receive_one_fd(fd_inner_socket_pair
[0], 0);
5593 if (fd_kmsg_fifo
< 0)
5594 return log_error_errno(fd_kmsg_fifo
, "Failed to receive kmsg fifo from inner child: %m");
5596 if (arg_expose_ports
) {
5597 r
= expose_port_watch_rtnl(event
, fd_inner_socket_pair
[0], on_address_change
, expose_args
, &rtnl
);
5601 (void) expose_port_execute(rtnl
, &expose_args
->fw_ctx
, arg_expose_ports
, AF_INET
, &expose_args
->address4
);
5602 (void) expose_port_execute(rtnl
, &expose_args
->fw_ctx
, arg_expose_ports
, AF_INET6
, &expose_args
->address6
);
5605 _cleanup_(osc_context_closep
) sd_id128_t osc_context_id
= SD_ID128_NULL
;
5606 if (IN_SET(arg_console_mode
, CONSOLE_INTERACTIVE
, CONSOLE_READ_ONLY
) && !terminal_is_dumb()) {
5607 r
= osc_context_open_container(arg_machine
, /* ret_seq= */ NULL
, &osc_context_id
);
5612 if (arg_console_mode
!= CONSOLE_PIPE
) {
5613 _cleanup_close_
int fd
= -EBADF
;
5614 PTYForwardFlags flags
= 0;
5616 /* Retrieve the master pty allocated by inner child */
5617 fd
= receive_one_fd(fd_inner_socket_pair
[0], 0);
5619 return log_error_errno(fd
, "Failed to receive master pty from the inner child: %m");
5621 switch (arg_console_mode
) {
5623 case CONSOLE_READ_ONLY
:
5624 flags
|= PTY_FORWARD_READ_ONLY
;
5628 case CONSOLE_INTERACTIVE
:
5629 flags
|= PTY_FORWARD_IGNORE_VHANGUP
;
5631 r
= pty_forward_new(event
, fd
, flags
, &forward
);
5633 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
5635 if (arg_console_width
!= UINT_MAX
|| arg_console_height
!= UINT_MAX
)
5636 (void) pty_forward_set_width_height(
5639 arg_console_height
);
5641 if (!arg_background
&& shall_tint_background()) {
5642 _cleanup_free_
char *bg
= NULL
;
5644 r
= terminal_tint_color(220 /* blue */, &bg
);
5646 log_debug_errno(r
, "Failed to determine terminal background color, not tinting.");
5648 (void) pty_forward_set_background_color(forward
, bg
);
5649 } else if (!isempty(arg_background
))
5650 (void) pty_forward_set_background_color(forward
, arg_background
);
5652 (void) pty_forward_set_window_title(forward
, GLYPH_BLUE_CIRCLE
, /* hostname = */ NULL
,
5653 STRV_MAKE("Container", arg_machine
));
5655 pty_forward_set_hotkey_handler(forward
, ptyfwd_hotkey
, pid
);
5659 assert(arg_console_mode
== CONSOLE_PASSIVE
);
5662 *master
= TAKE_FD(fd
);
5665 fd_inner_socket_pair
[0] = safe_close(fd_inner_socket_pair
[0]);
5667 r
= sd_event_loop(event
);
5669 return log_error_errno(r
, "Failed to run event loop: %m");
5671 /* Kill if it is not dead yet anyway */
5672 if (scope_allocated
)
5673 terminate_scope(runtime_bus
, arg_machine
);
5675 /* Normally redundant, but better safe than sorry */
5676 (void) pidref_kill(pid
, SIGKILL
);
5678 fd_kmsg_fifo
= safe_close(fd_kmsg_fifo
);
5680 if (arg_private_network
&& arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
5681 r
= move_back_network_interfaces(child_netns_fd
, arg_network_interfaces
);
5685 r
= remove_macvlan(child_netns_fd
, arg_network_macvlan
);
5690 r
= wait_for_container(pid
, &container_status
);
5692 /* Tell machined that we are gone. */
5694 (void) unregister_machine(system_bus
, arg_machine
);
5697 /* We failed to wait for the container, or the container exited abnormally. */
5699 if (r
> 0 || container_status
== CONTAINER_TERMINATED
) {
5700 /* r > 0 → The container exited with a non-zero status.
5701 * As a special case, we need to replace 133 with a different value,
5702 * because 133 is special-cased in the service file to reboot the container.
5703 * otherwise → The container exited with zero status and a reboot was not requested.
5705 if (r
== EXIT_FORCE_RESTART
)
5706 r
= EXIT_FAILURE
; /* replace 133 with the general failure code */
5708 return 0; /* finito */
5711 /* CONTAINER_REBOOTED, loop again */
5713 if (arg_keep_unit
) {
5714 /* Special handling if we are running as a service: instead of simply
5715 * restarting the machine we want to restart the entire service, so let's
5716 * inform systemd about this with the special exit code 133. The service
5717 * file uses RestartForceExitStatus=133 so that this results in a full
5718 * nspawn restart. This is necessary since we might have cgroup parameters
5719 * set we want to have flushed out. */
5720 *ret
= EXIT_FORCE_RESTART
;
5721 return 0; /* finito */
5724 expose_port_flush(&expose_args
->fw_ctx
, arg_expose_ports
, AF_INET
, &expose_args
->address4
);
5725 expose_port_flush(&expose_args
->fw_ctx
, arg_expose_ports
, AF_INET6
, &expose_args
->address6
);
5727 (void) remove_veth_links(veth_name
, arg_network_veth_extra
);
5728 *veth_created
= false;
5729 return 1; /* loop again */
5732 static int initialize_rlimits(void) {
5733 /* The default resource limits the kernel passes to PID 1, as per kernel 5.16. Let's pass our container payload
5734 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
5735 * container execution environments. */
5737 static const struct rlimit kernel_defaults
[_RLIMIT_MAX
] = {
5738 [RLIMIT_AS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5739 [RLIMIT_CORE
] = { 0, RLIM_INFINITY
},
5740 [RLIMIT_CPU
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5741 [RLIMIT_DATA
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5742 [RLIMIT_FSIZE
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5743 [RLIMIT_LOCKS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5744 [RLIMIT_MEMLOCK
] = { DEFAULT_RLIMIT_MEMLOCK
, DEFAULT_RLIMIT_MEMLOCK
},
5745 [RLIMIT_MSGQUEUE
] = { 819200, 819200 },
5746 [RLIMIT_NICE
] = { 0, 0 },
5747 [RLIMIT_NOFILE
] = { 1024, 4096 },
5748 [RLIMIT_RSS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5749 [RLIMIT_RTPRIO
] = { 0, 0 },
5750 [RLIMIT_RTTIME
] = { RLIM_INFINITY
, RLIM_INFINITY
},
5751 [RLIMIT_STACK
] = { 8388608, RLIM_INFINITY
},
5753 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
5754 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
5755 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
5756 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
5757 * that PID 1 changes a number of other resource limits during early initialization which is why we
5758 * don't read the other limits from PID 1 but prefer the static table above. */
5763 for (rl
= 0; rl
< _RLIMIT_MAX
; rl
++) {
5764 /* Let's only fill in what the user hasn't explicitly configured anyway */
5765 if ((arg_settings_mask
& (SETTING_RLIMIT_FIRST
<< rl
)) == 0) {
5766 const struct rlimit
*v
;
5767 struct rlimit buffer
;
5769 if (IN_SET(rl
, RLIMIT_NPROC
, RLIMIT_SIGPENDING
)) {
5770 /* For these two let's read the limits off PID 1. See above for an explanation. */
5772 r
= pid_getrlimit(1, rl
, &buffer
);
5774 return log_error_errno(r
, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl
));
5777 } else if (rl
== RLIMIT_NOFILE
) {
5778 /* We nowadays bump RLIMIT_NOFILE's hard limit early in PID 1 for all
5779 * userspace. Given that nspawn containers are often run without our PID 1,
5780 * let's grant the containers a raised RLIMIT_NOFILE hard limit by default,
5781 * so that container userspace gets similar resources as host userspace
5783 buffer
= kernel_defaults
[rl
];
5784 buffer
.rlim_max
= MIN((rlim_t
) read_nr_open(), (rlim_t
) HIGH_RLIMIT_NOFILE
);
5787 v
= kernel_defaults
+ rl
;
5789 arg_rlimit
[rl
] = newdup(struct rlimit
, v
, 1);
5790 if (!arg_rlimit
[rl
])
5794 if (DEBUG_LOGGING
) {
5795 _cleanup_free_
char *k
= NULL
;
5797 (void) rlimit_format(arg_rlimit
[rl
], &k
);
5798 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl
), k
);
5805 static int cant_be_in_netns(void) {
5806 _cleanup_close_
int fd
= -EBADF
;
5810 /* Check if we are in the same netns as udev. If we aren't, then device monitoring (and thus waiting
5811 * for loopback block devices) won't work, and we will hang. Detect this case and exit early with a
5814 if (!arg_image
) /* only matters if --image= us used, i.e. we actually need to use loopback devices */
5817 fd
= socket(AF_UNIX
, SOCK_SEQPACKET
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
5819 return log_error_errno(errno
, "Failed to allocate udev control socket: %m");
5821 r
= connect_unix_path(fd
, AT_FDCWD
, "/run/udev/control");
5822 if (r
== -ENOENT
|| ERRNO_IS_NEG_DISCONNECT(r
))
5823 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
5824 "Sorry, but --image= requires access to the host's /run/ hierarchy, since we need access to udev.");
5825 if (ERRNO_IS_NEG_PRIVILEGE(r
)) {
5826 log_debug_errno(r
, "Can't connect to udev control socket, assuming we are in same netns.");
5830 return log_error_errno(r
, "Failed to connect socket to udev control socket: %m");
5832 r
= getpeercred(fd
, &ucred
);
5834 return log_error_errno(r
, "Failed to determine peer of udev control socket: %m");
5836 r
= in_same_namespace(ucred
.pid
, 0, NAMESPACE_NET
);
5838 return log_error_errno(r
, "Failed to determine network namespace of udev: %m");
5840 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
5841 "Sorry, but --image= is only supported in the main network namespace, since we need access to udev/AF_NETLINK.");
5845 static void initialize_defaults(void) {
5846 arg_privileged
= getuid() == 0;
5848 /* If running unprivileged default to systemd-nsresourced operation */
5849 arg_userns_mode
= arg_privileged
? USER_NAMESPACE_NO
: USER_NAMESPACE_MANAGED
;
5851 /* Imply private networking for unprivileged operation, since kernel otherwise refuses mounting sysfs */
5852 arg_private_network
= !arg_privileged
;
5855 static void cleanup_propagation_and_export_directories(void) {
5858 if (!arg_machine
|| !arg_privileged
)
5861 p
= strjoina("/run/systemd/nspawn/propagate/", arg_machine
);
5862 (void) rm_rf(p
, REMOVE_ROOT
);
5864 p
= strjoina("/run/systemd/nspawn/unix-export/", arg_machine
);
5865 (void) umount2(p
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
5869 static int do_cleanup(void) {
5873 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Cannot specify --ephemeral with --cleanup.");
5875 r
= determine_names();
5879 cleanup_propagation_and_export_directories();
5883 static int run(int argc
, char *argv
[]) {
5884 bool remove_directory
= false, remove_image
= false, veth_created
= false;
5885 _cleanup_close_
int master
= -EBADF
, userns_fd
= -EBADF
, mount_fd
= -EBADF
;
5886 _cleanup_fdset_free_ FDSet
*fds
= NULL
;
5887 int r
, ret
= EXIT_SUCCESS
;
5888 char veth_name
[IFNAMSIZ
] = "";
5889 struct ExposeArgs expose_args
= {};
5890 _cleanup_(release_lock_file
) LockFile tree_global_lock
= LOCK_FILE_INIT
, tree_local_lock
= LOCK_FILE_INIT
;
5891 _cleanup_(rmdir_and_freep
) char *rootdir
= NULL
;
5892 _cleanup_(loop_device_unrefp
) LoopDevice
*loop
= NULL
;
5893 _cleanup_(dissected_image_unrefp
) DissectedImage
*dissected_image
= NULL
;
5894 _cleanup_(fw_ctx_freep
) FirewallContext
*fw_ctx
= NULL
;
5895 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
5899 initialize_defaults();
5901 r
= parse_argv(argc
, argv
);
5906 return do_cleanup();
5908 r
= cg_has_legacy();
5912 r
= log_error_errno(SYNTHETIC_ERRNO(EPROTO
),
5913 "Detected host uses legacy cgroup v1 hierarchy, refusing.");
5917 r
= cant_be_in_netns();
5921 r
= initialize_rlimits();
5925 r
= load_oci_bundle();
5933 r
= determine_names();
5937 r
= load_settings();
5941 /* If we're not unsharing the network namespace and are unsharing the user namespace, we won't have
5942 * permissions to bind ports in the container, so let's drop the CAP_NET_BIND_SERVICE capability to
5944 if (!arg_private_network
&& arg_userns_mode
!= USER_NAMESPACE_NO
&& arg_uid_shift
> 0)
5945 arg_caps_retain
&= ~(UINT64_C(1) << CAP_NET_BIND_SERVICE
);
5947 r
= verify_arguments();
5951 r
= resolve_network_interface_names(arg_network_interfaces
);
5955 r
= verify_network_interfaces_initialized();
5959 /* Ignore SIGPIPE here, because we use splice() on the ptyfwd stuff and that will generate SIGPIPE if
5960 * the result is closed. Note that the container payload child will reset signal mask+handler anyway,
5961 * so just turning this off here means we only turn it off in nspawn itself, not any children. */
5962 (void) ignore_signals(SIGPIPE
);
5964 r
= fdset_new_listen_fds(&fds
, /* unset = */ false);
5966 log_error_errno(r
, "Failed to collect file descriptors: %m");
5970 /* The "default" umask. This is appropriate for most file and directory
5971 * operations performed by nspawn, and is the umask that will be used for
5972 * the child. Functions like copy_devnodes() change the umask temporarily. */
5975 if (arg_console_mode
< 0)
5976 arg_console_mode
= isatty_safe(STDIN_FILENO
) && isatty_safe(STDOUT_FILENO
) ?
5977 CONSOLE_INTERACTIVE
: CONSOLE_READ_ONLY
;
5979 if (arg_console_mode
== CONSOLE_PIPE
) /* if we pass STDERR on to the container, don't add our own logs into it too */
5982 polkit_agent_open();
5984 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
5985 /* Let's allocate a 64K userns first, if managed mode is chosen */
5987 _cleanup_free_
char *userns_name
= NULL
;
5988 if (asprintf(&userns_name
, "nspawn-" PID_FMT
"-%s", getpid_cached(), arg_machine
) < 0) {
5993 userns_fd
= nsresource_allocate_userns(userns_name
, UINT64_C(0x10000));
5994 if (userns_fd
< 0) {
5995 r
= log_error_errno(userns_fd
, "Failed to allocate user namespace with 64K users: %m");
5999 r
= userns_get_base_uid(userns_fd
, &arg_uid_shift
, /* ret_gid= */ NULL
);
6001 log_error_errno(r
, "Failed to determine UID shift from userns: %m");
6005 arg_uid_range
= UINT32_C(0x10000);
6008 if (arg_directory
) {
6011 /* Safety precaution: let's not allow running images from the live host OS image, as long as
6012 * /var from the host will propagate into container dynamically (because bad things happen if
6013 * two systems write to the same /var). Let's allow it for the special cases where /var is
6014 * either copied (i.e. --ephemeral) or replaced (i.e. --volatile=yes|state). */
6015 if (path_equal(arg_directory
, "/") && !(arg_ephemeral
|| IN_SET(arg_volatile_mode
, VOLATILE_YES
, VOLATILE_STATE
))) {
6016 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
6017 "Spawning container on root directory is not supported. Consider using --ephemeral, --volatile=yes or --volatile=state.");
6021 if (arg_ephemeral
) {
6022 _cleanup_free_
char *np
= NULL
;
6024 r
= chase_and_update(&arg_directory
, 0);
6028 /* If the specified path is a mount point we generate the new snapshot immediately
6029 * inside it under a random name. However if the specified is not a mount point we
6030 * create the new snapshot in the parent directory, just next to it. */
6031 r
= path_is_mount_point(arg_directory
);
6033 log_error_errno(r
, "Failed to determine whether directory %s is mount point: %m", arg_directory
);
6037 r
= tempfn_random_child(arg_directory
, "machine.", &np
);
6039 r
= tempfn_random(arg_directory
, "machine.", &np
);
6041 log_error_errno(r
, "Failed to generate name for directory snapshot: %m");
6045 /* We take an exclusive lock on this image, since it's our private, ephemeral copy
6046 * only owned by us and no one else. */
6047 r
= image_path_lock(
6050 arg_privileged
? &tree_global_lock
: NULL
,
6053 log_error_errno(r
, "Failed to lock %s: %m", np
);
6058 BLOCK_SIGNALS(SIGINT
);
6059 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, arg_directory
, AT_FDCWD
, np
,
6060 (arg_read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
6061 BTRFS_SNAPSHOT_FALLBACK_COPY
|
6062 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
6063 BTRFS_SNAPSHOT_RECURSIVE
|
6064 BTRFS_SNAPSHOT_QUOTA
|
6065 BTRFS_SNAPSHOT_SIGINT
);
6068 log_error_errno(r
, "Interrupted while copying file system tree to %s, removed again.", np
);
6072 log_error_errno(r
, "Failed to create snapshot %s from %s: %m", np
, arg_directory
);
6076 free_and_replace(arg_directory
, np
);
6077 remove_directory
= true;
6079 r
= chase_and_update(&arg_directory
, arg_template
? CHASE_NONEXISTENT
: 0);
6083 r
= image_path_lock(
6085 (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
,
6086 arg_privileged
? &tree_global_lock
: NULL
,
6089 log_error_errno(r
, "Directory tree %s is currently busy.", arg_directory
);
6093 log_error_errno(r
, "Failed to lock %s: %m", arg_directory
);
6098 r
= chase_and_update(&arg_template
, 0);
6103 BLOCK_SIGNALS(SIGINT
);
6104 r
= btrfs_subvol_snapshot_at(AT_FDCWD
, arg_template
, AT_FDCWD
, arg_directory
,
6105 (arg_read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
6106 BTRFS_SNAPSHOT_FALLBACK_COPY
|
6107 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
6108 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
6109 BTRFS_SNAPSHOT_RECURSIVE
|
6110 BTRFS_SNAPSHOT_QUOTA
|
6111 BTRFS_SNAPSHOT_SIGINT
);
6114 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
,
6115 "Directory %s already exists, not populating from template %s.", arg_directory
, arg_template
);
6116 else if (r
== -EINTR
) {
6117 log_error_errno(r
, "Interrupted while copying file system tree to %s, removed again.", arg_directory
);
6120 log_error_errno(r
, "Couldn't create snapshot %s from %s: %m", arg_directory
, arg_template
);
6123 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
,
6124 "Populated %s from template %s.", arg_directory
, arg_template
);
6128 if (arg_start_mode
== START_BOOT
) {
6129 _cleanup_free_
char *b
= NULL
;
6131 int check_os_release
, is_os_tree
;
6133 if (arg_pivot_root_new
) {
6134 b
= path_join(arg_directory
, arg_pivot_root_new
);
6144 check_os_release
= getenv_bool("SYSTEMD_NSPAWN_CHECK_OS_RELEASE");
6145 if (check_os_release
< 0 && check_os_release
!= -ENXIO
) {
6146 r
= log_error_errno(check_os_release
, "Failed to parse $SYSTEMD_NSPAWN_CHECK_OS_RELEASE: %m");
6150 is_os_tree
= path_is_os_tree(p
);
6151 if (is_os_tree
== 0 && check_os_release
== 0)
6152 log_debug("Directory %s is missing an os-release file, continuing anyway.", p
);
6153 else if (is_os_tree
<= 0) {
6154 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
6155 "Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p
);
6159 _cleanup_free_
char *p
= NULL
;
6161 if (arg_pivot_root_new
)
6162 p
= path_join(arg_directory
, arg_pivot_root_new
, "/usr/");
6164 p
= path_join(arg_directory
, "/usr/");
6170 r
= access_nofollow(p
, F_OK
);
6172 log_error_errno(r
, "Directory %s doesn't look like it has an OS tree (/usr/ directory is missing). Refusing.", arg_directory
);
6175 log_error_errno(r
, "Unable to determine if %s looks like it has an OS tree (i.e. whether /usr/ exists): %m", arg_directory
);
6180 if (arg_userns_mode
== USER_NAMESPACE_MANAGED
) {
6181 r
= mountfsd_mount_directory(
6184 determine_dissect_image_flags(),
6190 DissectImageFlags dissect_image_flags
=
6191 determine_dissect_image_flags();
6194 assert(!arg_template
);
6196 r
= chase_and_update(&arg_image
, 0);
6200 if (arg_ephemeral
) {
6201 _cleanup_free_
char *np
= NULL
;
6203 r
= tempfn_random(arg_image
, "machine.", &np
);
6205 log_error_errno(r
, "Failed to generate name for image snapshot: %m");
6209 /* Always take an exclusive lock on our own ephemeral copy. */
6210 r
= image_path_lock(
6213 arg_privileged
? &tree_global_lock
: NULL
,
6216 log_error_errno(r
, "Failed to create image lock: %m");
6221 BLOCK_SIGNALS(SIGINT
);
6222 r
= copy_file(arg_image
, np
, O_EXCL
, arg_read_only
? 0400 : 0600,
6223 COPY_REFLINK
|COPY_CRTIME
|COPY_SIGINT
|COPY_NOCOW_AFTER
);
6226 log_error_errno(r
, "Interrupted while copying image file to %s, removed again.", np
);
6230 r
= log_error_errno(r
, "Failed to copy image file: %m");
6234 free_and_replace(arg_image
, np
);
6235 remove_image
= true;
6237 r
= image_path_lock(
6239 (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
,
6240 arg_privileged
? &tree_global_lock
: NULL
,
6243 log_error_errno(r
, "Disk image %s is currently busy.", arg_image
);
6247 log_error_errno(r
, "Failed to create image lock: %m");
6251 r
= verity_settings_load(
6252 &arg_verity_settings
,
6253 arg_image
, NULL
, NULL
);
6255 log_error_errno(r
, "Failed to read verity artefacts for %s: %m", arg_image
);
6259 if (arg_verity_settings
.data_path
)
6260 dissect_image_flags
|= DISSECT_IMAGE_NO_PARTITION_TABLE
;
6263 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
6264 r
= loop_device_make_by_path(
6266 arg_read_only
? O_RDONLY
: O_RDWR
,
6267 /* sector_size= */ UINT32_MAX
,
6268 FLAGS_SET(dissect_image_flags
, DISSECT_IMAGE_NO_PARTITION_TABLE
) ? 0 : LO_FLAGS_PARTSCAN
,
6272 log_error_errno(r
, "Failed to set up loopback block device: %m");
6276 r
= dissect_loop_device_and_warn(
6278 &arg_verity_settings
,
6279 /* mount_options= */ NULL
,
6280 arg_image_policy
?: &image_policy_container
,
6281 /* image_filter= */ NULL
,
6282 dissect_image_flags
,
6285 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
6286 log_notice("Note that the disk image needs to\n"
6287 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
6288 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
6289 " c) or follow https://uapi-group.org/specifications/specs/discoverable_partitions_specification\n"
6290 " d) or contain a file system without a partition table\n"
6291 "in order to be bootable with systemd-nspawn.");
6297 r
= dissected_image_load_verity_sig_partition(
6300 &arg_verity_settings
);
6302 log_error_errno(r
, "Failed to load Verity signature partition: %m");
6306 r
= dissected_image_guess_verity_roothash(
6308 &arg_verity_settings
);
6310 log_error_errno(r
, "Failed to guess Verity root hash: %m");
6314 if (dissected_image
->has_verity
&& !arg_verity_settings
.root_hash
)
6315 log_notice("Note: image %s contains verity information, but no root hash specified and no embedded "
6316 "root hash signature found! Proceeding without integrity checking.", arg_image
);
6318 r
= dissected_image_decrypt_interactively(
6321 &arg_verity_settings
,
6322 dissect_image_flags
);
6326 r
= mountfsd_mount_image(
6330 dissect_image_flags
,
6336 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
6337 if (remove_image
&& unlink(arg_image
) >= 0)
6338 remove_image
= false;
6340 if (arg_architecture
< 0)
6341 arg_architecture
= dissected_image_architecture(dissected_image
);
6344 /* Create a temporary place to mount stuff. */
6345 r
= mkdtemp_malloc("/tmp/nspawn-root-XXXXXX", &rootdir
);
6347 log_error_errno(r
, "Failed to create temporary directory: %m");
6351 r
= custom_mount_prepare_all(rootdir
, arg_custom_mounts
, arg_n_custom_mounts
);
6356 const char *t
= arg_image
?: arg_directory
;
6357 _cleanup_free_
char *u
= NULL
;
6358 (void) terminal_urlify_path(t
, t
, &u
);
6360 log_info("%s %sSpawning container %s on %s.%s",
6361 glyph(GLYPH_LIGHT_SHADE
), ansi_grey(), arg_machine
, u
?: t
, ansi_normal());
6363 if (arg_console_mode
== CONSOLE_INTERACTIVE
)
6364 log_info("%s %sPress %sCtrl-]%s three times within 1s to kill container; two times followed by %sr%s\n"
6365 "%s %sto reboot container; two times followed by %sp%s to poweroff container.%s",
6366 glyph(GLYPH_LIGHT_SHADE
), ansi_grey(), ansi_highlight(), ansi_grey(), ansi_highlight(), ansi_normal(),
6367 glyph(GLYPH_LIGHT_SHADE
), ansi_grey(), ansi_highlight(), ansi_grey(), ansi_normal());
6370 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGWINCH
, SIGTERM
, SIGINT
, SIGRTMIN
+18) >= 0);
6372 r
= make_reaper_process(true);
6374 log_error_errno(r
, "Failed to become subreaper: %m");
6378 if (arg_expose_ports
) {
6379 r
= fw_ctx_new(&fw_ctx
);
6381 log_error_errno(r
, "Cannot expose configured ports, firewall initialization failed: %m");
6384 expose_args
.fw_ctx
= fw_ctx
;
6394 veth_name
, &veth_created
,
6395 &expose_args
, &master
,
6402 (void) sd_notify(false,
6403 r
== 0 && ret
== EXIT_FORCE_RESTART
? "STOPPING=1\nSTATUS=Restarting..." :
6404 "STOPPING=1\nSTATUS=Terminating...");
6406 if (pidref_is_set(&pid
))
6407 (void) pidref_kill(&pid
, SIGKILL
);
6409 /* Try to flush whatever is still queued in the pty */
6411 (void) copy_bytes(master
, STDOUT_FILENO
, UINT64_MAX
, 0);
6412 master
= safe_close(master
);
6415 if (pidref_is_set(&pid
)) {
6416 (void) pidref_wait_for_terminate(&pid
, NULL
);
6422 if (remove_directory
&& arg_directory
) {
6425 k
= rm_rf(arg_directory
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
6427 log_warning_errno(k
, "Cannot remove '%s', ignoring: %m", arg_directory
);
6430 if (remove_image
&& arg_image
) {
6431 if (unlink(arg_image
) < 0)
6432 log_warning_errno(errno
, "Can't remove image file '%s', ignoring: %m", arg_image
);
6435 if (arg_machine
&& arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
6438 p
= strjoina("/run/systemd/nspawn/propagate/", arg_machine
);
6439 (void) rm_rf(p
, REMOVE_ROOT
);
6441 p
= strjoina("/run/systemd/nspawn/unix-export/", arg_machine
);
6442 (void) umount2(p
, MNT_DETACH
|UMOUNT_NOFOLLOW
);
6446 cleanup_propagation_and_export_directories();
6448 expose_port_flush(&fw_ctx
, arg_expose_ports
, AF_INET
, &expose_args
.address4
);
6449 expose_port_flush(&fw_ctx
, arg_expose_ports
, AF_INET6
, &expose_args
.address6
);
6451 if (arg_userns_mode
!= USER_NAMESPACE_MANAGED
) {
6453 (void) remove_veth_links(veth_name
, arg_network_veth_extra
);
6454 (void) remove_bridge(arg_network_zone
);
6457 custom_mount_free_all(arg_custom_mounts
, arg_n_custom_mounts
);
6458 expose_port_free_all(arg_expose_ports
);
6459 rlimit_free_all(arg_rlimit
);
6460 device_node_array_free(arg_extra_nodes
, arg_n_extra_nodes
);
6468 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);