1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include <linux/loop.h>
18 #include <selinux/selinux.h>
25 #include <sys/mount.h>
26 #include <sys/personality.h>
27 #include <sys/prctl.h>
28 #include <sys/types.h>
33 #include "sd-daemon.h"
36 #include "alloc-util.h"
38 #include "base-filesystem.h"
39 #include "blkid-util.h"
40 #include "btrfs-util.h"
43 #include "capability-util.h"
44 #include "cgroup-util.h"
46 #include "cpu-set-util.h"
47 #include "dev-setup.h"
48 #include "dissect-image.h"
53 #include "format-util.h"
56 #include "hexdecoct.h"
57 #include "hostname-util.h"
58 #include "id128-util.h"
60 #include "loop-util.h"
61 #include "loopback-setup.h"
62 #include "machine-image.h"
66 #include "mount-util.h"
67 #include "netlink-util.h"
68 #include "nspawn-cgroup.h"
69 #include "nspawn-def.h"
70 #include "nspawn-expose-ports.h"
71 #include "nspawn-mount.h"
72 #include "nspawn-network.h"
73 #include "nspawn-patch-uid.h"
74 #include "nspawn-register.h"
75 #include "nspawn-seccomp.h"
76 #include "nspawn-settings.h"
77 #include "nspawn-setuid.h"
78 #include "nspawn-stub-pid1.h"
80 #include "parse-util.h"
81 #include "path-util.h"
82 #include "process-util.h"
84 #include "random-util.h"
85 #include "raw-clone.h"
86 #include "rlimit-util.h"
88 #include "selinux-util.h"
89 #include "signal-util.h"
90 #include "socket-util.h"
91 #include "stat-util.h"
92 #include "stdio-util.h"
93 #include "string-table.h"
94 #include "string-util.h"
96 #include "terminal-util.h"
97 #include "udev-util.h"
98 #include "umask-util.h"
99 #include "user-util.h"
103 #define STATIC_RESOLV_CONF "/lib/systemd/resolv.conf"
105 #define STATIC_RESOLV_CONF "/usr/lib/systemd/resolv.conf"
108 /* nspawn is listening on the socket at the path in the constant nspawn_notify_socket_path
109 * nspawn_notify_socket_path is relative to the container
110 * the init process in the container pid can send messages to nspawn following the sd_notify(3) protocol */
111 #define NSPAWN_NOTIFY_SOCKET_PATH "/run/systemd/nspawn/notify"
113 #define EXIT_FORCE_RESTART 133
115 typedef enum ContainerStatus
{
116 CONTAINER_TERMINATED
,
120 typedef enum LinkJournal
{
127 static char *arg_directory
= NULL
;
128 static char *arg_template
= NULL
;
129 static char *arg_chdir
= NULL
;
130 static char *arg_pivot_root_new
= NULL
;
131 static char *arg_pivot_root_old
= NULL
;
132 static char *arg_user
= NULL
;
133 static sd_id128_t arg_uuid
= {};
134 static char *arg_machine
= NULL
; /* The name used by the host to refer to this */
135 static char *arg_hostname
= NULL
; /* The name the payload sees by default */
136 static const char *arg_selinux_context
= NULL
;
137 static const char *arg_selinux_apifs_context
= NULL
;
138 static const char *arg_slice
= NULL
;
139 static bool arg_private_network
= false;
140 static bool arg_read_only
= false;
141 static StartMode arg_start_mode
= START_PID1
;
142 static bool arg_ephemeral
= false;
143 static LinkJournal arg_link_journal
= LINK_AUTO
;
144 static bool arg_link_journal_try
= false;
145 static uint64_t arg_caps_retain
=
146 (1ULL << CAP_AUDIT_CONTROL
) |
147 (1ULL << CAP_AUDIT_WRITE
) |
148 (1ULL << CAP_CHOWN
) |
149 (1ULL << CAP_DAC_OVERRIDE
) |
150 (1ULL << CAP_DAC_READ_SEARCH
) |
151 (1ULL << CAP_FOWNER
) |
152 (1ULL << CAP_FSETID
) |
153 (1ULL << CAP_IPC_OWNER
) |
155 (1ULL << CAP_LEASE
) |
156 (1ULL << CAP_LINUX_IMMUTABLE
) |
157 (1ULL << CAP_MKNOD
) |
158 (1ULL << CAP_NET_BIND_SERVICE
) |
159 (1ULL << CAP_NET_BROADCAST
) |
160 (1ULL << CAP_NET_RAW
) |
161 (1ULL << CAP_SETFCAP
) |
162 (1ULL << CAP_SETGID
) |
163 (1ULL << CAP_SETPCAP
) |
164 (1ULL << CAP_SETUID
) |
165 (1ULL << CAP_SYS_ADMIN
) |
166 (1ULL << CAP_SYS_BOOT
) |
167 (1ULL << CAP_SYS_CHROOT
) |
168 (1ULL << CAP_SYS_NICE
) |
169 (1ULL << CAP_SYS_PTRACE
) |
170 (1ULL << CAP_SYS_RESOURCE
) |
171 (1ULL << CAP_SYS_TTY_CONFIG
);
172 static CustomMount
*arg_custom_mounts
= NULL
;
173 static size_t arg_n_custom_mounts
= 0;
174 static char **arg_setenv
= NULL
;
175 static bool arg_quiet
= false;
176 static bool arg_register
= true;
177 static bool arg_keep_unit
= false;
178 static char **arg_network_interfaces
= NULL
;
179 static char **arg_network_macvlan
= NULL
;
180 static char **arg_network_ipvlan
= NULL
;
181 static bool arg_network_veth
= false;
182 static char **arg_network_veth_extra
= NULL
;
183 static char *arg_network_bridge
= NULL
;
184 static char *arg_network_zone
= NULL
;
185 static char *arg_network_namespace_path
= NULL
;
186 static unsigned long arg_personality
= PERSONALITY_INVALID
;
187 static char *arg_image
= NULL
;
188 static VolatileMode arg_volatile_mode
= VOLATILE_NO
;
189 static ExposePort
*arg_expose_ports
= NULL
;
190 static char **arg_property
= NULL
;
191 static UserNamespaceMode arg_userns_mode
= USER_NAMESPACE_NO
;
192 static uid_t arg_uid_shift
= UID_INVALID
, arg_uid_range
= 0x10000U
;
193 static bool arg_userns_chown
= false;
194 static int arg_kill_signal
= 0;
195 static CGroupUnified arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_UNKNOWN
;
196 static SettingsMask arg_settings_mask
= 0;
197 static int arg_settings_trusted
= -1;
198 static char **arg_parameters
= NULL
;
199 static const char *arg_container_service_name
= "systemd-nspawn";
200 static bool arg_notify_ready
= false;
201 static bool arg_use_cgns
= true;
202 static unsigned long arg_clone_ns_flags
= CLONE_NEWIPC
|CLONE_NEWPID
|CLONE_NEWUTS
;
203 static MountSettingsMask arg_mount_settings
= MOUNT_APPLY_APIVFS_RO
;
204 static void *arg_root_hash
= NULL
;
205 static size_t arg_root_hash_size
= 0;
206 static char **arg_syscall_whitelist
= NULL
;
207 static char **arg_syscall_blacklist
= NULL
;
208 static struct rlimit
*arg_rlimit
[_RLIMIT_MAX
] = {};
209 static bool arg_no_new_privileges
= false;
210 static int arg_oom_score_adjust
= 0;
211 static bool arg_oom_score_adjust_set
= false;
212 static cpu_set_t
*arg_cpuset
= NULL
;
213 static unsigned arg_cpuset_ncpus
= 0;
215 static void help(void) {
217 (void) pager_open(false, false);
219 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
220 "Spawn a minimal namespace container for debugging, testing and building.\n\n"
221 " -h --help Show this help\n"
222 " --version Print version string\n"
223 " -q --quiet Do not show status information\n"
224 " -D --directory=PATH Root directory for the container\n"
225 " --template=PATH Initialize root directory from template directory,\n"
227 " -x --ephemeral Run container with snapshot of root directory, and\n"
228 " remove it after exit\n"
229 " -i --image=PATH File system device or disk image for the container\n"
230 " --root-hash=HASH Specify verity root hash\n"
231 " -a --as-pid2 Maintain a stub init as PID1, invoke binary as PID2\n"
232 " -b --boot Boot up full system (i.e. invoke init)\n"
233 " --chdir=PATH Set working directory in the container\n"
234 " --pivot-root=PATH[:PATH]\n"
235 " Pivot root to given directory in the container\n"
236 " -u --user=USER Run the command under specified user or uid\n"
237 " -M --machine=NAME Set the machine name for the container\n"
238 " --hostname=NAME Override the hostname for the container\n"
239 " --uuid=UUID Set a specific machine UUID for the container\n"
240 " -S --slice=SLICE Place the container in the specified slice\n"
241 " --property=NAME=VALUE Set scope unit property\n"
242 " -U --private-users=pick Run within user namespace, autoselect UID/GID range\n"
243 " --private-users[=UIDBASE[:NUIDS]]\n"
244 " Similar, but with user configured UID/GID range\n"
245 " --private-users-chown Adjust OS tree ownership to private UID/GID range\n"
246 " --private-network Disable network in container\n"
247 " --network-interface=INTERFACE\n"
248 " Assign an existing network interface to the\n"
250 " --network-macvlan=INTERFACE\n"
251 " Create a macvlan network interface based on an\n"
252 " existing network interface to the container\n"
253 " --network-ipvlan=INTERFACE\n"
254 " Create a ipvlan network interface based on an\n"
255 " existing network interface to the container\n"
256 " -n --network-veth Add a virtual Ethernet connection between host\n"
258 " --network-veth-extra=HOSTIF[:CONTAINERIF]\n"
259 " Add an additional virtual Ethernet link between\n"
260 " host and container\n"
261 " --network-bridge=INTERFACE\n"
262 " Add a virtual Ethernet connection to the container\n"
263 " and attach it to an existing bridge on the host\n"
264 " --network-zone=NAME Similar, but attach the new interface to an\n"
265 " an automatically managed bridge interface\n"
266 " --network-namespace-path=PATH\n"
267 " Set network namespace to the one represented by\n"
268 " the specified kernel namespace file node\n"
269 " -p --port=[PROTOCOL:]HOSTPORT[:CONTAINERPORT]\n"
270 " Expose a container IP port on the host\n"
271 " -Z --selinux-context=SECLABEL\n"
272 " Set the SELinux security context to be used by\n"
273 " processes in the container\n"
274 " -L --selinux-apifs-context=SECLABEL\n"
275 " Set the SELinux security context to be used by\n"
276 " API/tmpfs file systems in the container\n"
277 " --capability=CAP In addition to the default, retain specified\n"
279 " --drop-capability=CAP Drop the specified capability from the default set\n"
280 " --system-call-filter=LIST|~LIST\n"
281 " Permit/prohibit specific system calls\n"
282 " --rlimit=NAME=LIMIT Set a resource limit for the payload\n"
283 " --oom-score-adjust=VALUE\n"
284 " Adjust the OOM score value for the payload\n"
285 " --cpu-affinity=CPUS Adjust the CPU affinity of the container\n"
286 " --kill-signal=SIGNAL Select signal to use for shutting down PID 1\n"
287 " --link-journal=MODE Link up guest journal, one of no, auto, guest, \n"
288 " host, try-guest, try-host\n"
289 " -j Equivalent to --link-journal=try-guest\n"
290 " --read-only Mount the root directory read-only\n"
291 " --bind=PATH[:PATH[:OPTIONS]]\n"
292 " Bind mount a file or directory from the host into\n"
294 " --bind-ro=PATH[:PATH[:OPTIONS]\n"
295 " Similar, but creates a read-only bind mount\n"
296 " --tmpfs=PATH:[OPTIONS] Mount an empty tmpfs to the specified directory\n"
297 " --overlay=PATH[:PATH...]:PATH\n"
298 " Create an overlay mount from the host to \n"
300 " --overlay-ro=PATH[:PATH...]:PATH\n"
301 " Similar, but creates a read-only overlay mount\n"
302 " -E --setenv=NAME=VALUE Pass an environment variable to PID 1\n"
303 " --register=BOOLEAN Register container as machine\n"
304 " --keep-unit Do not register a scope for the machine, reuse\n"
305 " the service unit nspawn is running in\n"
306 " --volatile[=MODE] Run the system in volatile mode\n"
307 " --settings=BOOLEAN Load additional settings from .nspawn file\n"
308 " --notify-ready=BOOLEAN Receive notifications from the child init process\n"
309 , program_invocation_short_name
);
312 static int custom_mount_check_all(void) {
315 for (i
= 0; i
< arg_n_custom_mounts
; i
++) {
316 CustomMount
*m
= &arg_custom_mounts
[i
];
318 if (path_equal(m
->destination
, "/") && arg_userns_mode
!= USER_NAMESPACE_NO
) {
320 if (arg_userns_chown
) {
321 log_error("--private-users-chown may not be combined with custom root mounts.");
323 } else if (arg_uid_shift
== UID_INVALID
) {
324 log_error("--private-users with automatic UID shift may not be combined with custom root mounts.");
333 static int detect_unified_cgroup_hierarchy_from_environment(void) {
337 /* Allow the user to control whether the unified hierarchy is used */
338 e
= getenv("UNIFIED_CGROUP_HIERARCHY");
340 r
= parse_boolean(e
);
342 return log_error_errno(r
, "Failed to parse $UNIFIED_CGROUP_HIERARCHY.");
344 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_ALL
;
346 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_NONE
;
352 static int detect_unified_cgroup_hierarchy_from_image(const char *directory
) {
355 /* Let's inherit the mode to use from the host system, but let's take into consideration what systemd in the
356 * image actually supports. */
357 r
= cg_all_unified();
359 return log_error_errno(r
, "Failed to determine whether we are in all unified mode.");
361 /* Unified cgroup hierarchy support was added in 230. Unfortunately the detection
362 * routine only detects 231, so we'll have a false negative here for 230. */
363 r
= systemd_installation_has_version(directory
, 230);
365 return log_error_errno(r
, "Failed to determine systemd version in container: %m");
367 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_ALL
;
369 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_NONE
;
370 } else if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
) > 0) {
371 /* Mixed cgroup hierarchy support was added in 233 */
372 r
= systemd_installation_has_version(directory
, 233);
374 return log_error_errno(r
, "Failed to determine systemd version in container: %m");
376 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_SYSTEMD
;
378 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_NONE
;
380 arg_unified_cgroup_hierarchy
= CGROUP_UNIFIED_NONE
;
382 log_debug("Using %s hierarchy for container.",
383 arg_unified_cgroup_hierarchy
== CGROUP_UNIFIED_NONE
? "legacy" :
384 arg_unified_cgroup_hierarchy
== CGROUP_UNIFIED_SYSTEMD
? "hybrid" : "unified");
389 static void parse_share_ns_env(const char *name
, unsigned long ns_flag
) {
392 r
= getenv_bool(name
);
396 log_warning_errno(r
, "Failed to parse %s from environment, defaulting to false.", name
);
397 arg_clone_ns_flags
= (arg_clone_ns_flags
& ~ns_flag
) | (r
> 0 ? 0 : ns_flag
);
400 static void parse_mount_settings_env(void) {
404 e
= getenv("SYSTEMD_NSPAWN_API_VFS_WRITABLE");
408 if (streq(e
, "network")) {
409 arg_mount_settings
|= MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_APIVFS_NETNS
;
413 r
= parse_boolean(e
);
415 log_warning_errno(r
, "Failed to parse SYSTEMD_NSPAWN_API_VFS_WRITABLE from environment, ignoring.");
419 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_APIVFS_RO
, r
== 0);
420 SET_FLAG(arg_mount_settings
, MOUNT_APPLY_APIVFS_NETNS
, false);
423 static int parse_argv(int argc
, char *argv
[]) {
441 ARG_NETWORK_INTERFACE
,
446 ARG_NETWORK_VETH_EXTRA
,
447 ARG_NETWORK_NAMESPACE_PATH
,
457 ARG_PRIVATE_USERS_CHOWN
,
460 ARG_SYSTEM_CALL_FILTER
,
463 ARG_NO_NEW_PRIVILEGES
,
464 ARG_OOM_SCORE_ADJUST
,
468 static const struct option options
[] = {
469 { "help", no_argument
, NULL
, 'h' },
470 { "version", no_argument
, NULL
, ARG_VERSION
},
471 { "directory", required_argument
, NULL
, 'D' },
472 { "template", required_argument
, NULL
, ARG_TEMPLATE
},
473 { "ephemeral", no_argument
, NULL
, 'x' },
474 { "user", required_argument
, NULL
, 'u' },
475 { "private-network", no_argument
, NULL
, ARG_PRIVATE_NETWORK
},
476 { "as-pid2", no_argument
, NULL
, 'a' },
477 { "boot", no_argument
, NULL
, 'b' },
478 { "uuid", required_argument
, NULL
, ARG_UUID
},
479 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
480 { "capability", required_argument
, NULL
, ARG_CAPABILITY
},
481 { "drop-capability", required_argument
, NULL
, ARG_DROP_CAPABILITY
},
482 { "no-new-privileges", required_argument
, NULL
, ARG_NO_NEW_PRIVILEGES
},
483 { "link-journal", required_argument
, NULL
, ARG_LINK_JOURNAL
},
484 { "bind", required_argument
, NULL
, ARG_BIND
},
485 { "bind-ro", required_argument
, NULL
, ARG_BIND_RO
},
486 { "tmpfs", required_argument
, NULL
, ARG_TMPFS
},
487 { "overlay", required_argument
, NULL
, ARG_OVERLAY
},
488 { "overlay-ro", required_argument
, NULL
, ARG_OVERLAY_RO
},
489 { "machine", required_argument
, NULL
, 'M' },
490 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
491 { "slice", required_argument
, NULL
, 'S' },
492 { "setenv", required_argument
, NULL
, 'E' },
493 { "selinux-context", required_argument
, NULL
, 'Z' },
494 { "selinux-apifs-context", required_argument
, NULL
, 'L' },
495 { "quiet", no_argument
, NULL
, 'q' },
496 { "share-system", no_argument
, NULL
, ARG_SHARE_SYSTEM
}, /* not documented */
497 { "register", required_argument
, NULL
, ARG_REGISTER
},
498 { "keep-unit", no_argument
, NULL
, ARG_KEEP_UNIT
},
499 { "network-interface", required_argument
, NULL
, ARG_NETWORK_INTERFACE
},
500 { "network-macvlan", required_argument
, NULL
, ARG_NETWORK_MACVLAN
},
501 { "network-ipvlan", required_argument
, NULL
, ARG_NETWORK_IPVLAN
},
502 { "network-veth", no_argument
, NULL
, 'n' },
503 { "network-veth-extra", required_argument
, NULL
, ARG_NETWORK_VETH_EXTRA
},
504 { "network-bridge", required_argument
, NULL
, ARG_NETWORK_BRIDGE
},
505 { "network-zone", required_argument
, NULL
, ARG_NETWORK_ZONE
},
506 { "network-namespace-path", required_argument
, NULL
, ARG_NETWORK_NAMESPACE_PATH
},
507 { "personality", required_argument
, NULL
, ARG_PERSONALITY
},
508 { "image", required_argument
, NULL
, 'i' },
509 { "volatile", optional_argument
, NULL
, ARG_VOLATILE
},
510 { "port", required_argument
, NULL
, 'p' },
511 { "property", required_argument
, NULL
, ARG_PROPERTY
},
512 { "private-users", optional_argument
, NULL
, ARG_PRIVATE_USERS
},
513 { "private-users-chown", optional_argument
, NULL
, ARG_PRIVATE_USERS_CHOWN
},
514 { "kill-signal", required_argument
, NULL
, ARG_KILL_SIGNAL
},
515 { "settings", required_argument
, NULL
, ARG_SETTINGS
},
516 { "chdir", required_argument
, NULL
, ARG_CHDIR
},
517 { "pivot-root", required_argument
, NULL
, ARG_PIVOT_ROOT
},
518 { "notify-ready", required_argument
, NULL
, ARG_NOTIFY_READY
},
519 { "root-hash", required_argument
, NULL
, ARG_ROOT_HASH
},
520 { "system-call-filter", required_argument
, NULL
, ARG_SYSTEM_CALL_FILTER
},
521 { "rlimit", required_argument
, NULL
, ARG_RLIMIT
},
522 { "oom-score-adjust", required_argument
, NULL
, ARG_OOM_SCORE_ADJUST
},
523 { "cpu-affinity", required_argument
, NULL
, ARG_CPU_AFFINITY
},
529 uint64_t plus
= 0, minus
= 0;
530 bool mask_all_settings
= false, mask_no_settings
= false;
535 while ((c
= getopt_long(argc
, argv
, "+hD:u:abL:M:jS:Z:qi:xp:nUE:", options
, NULL
)) >= 0)
547 r
= parse_path_argument_and_warn(optarg
, false, &arg_directory
);
553 r
= parse_path_argument_and_warn(optarg
, false, &arg_template
);
559 r
= parse_path_argument_and_warn(optarg
, false, &arg_image
);
565 arg_ephemeral
= true;
569 r
= free_and_strdup(&arg_user
, optarg
);
573 arg_settings_mask
|= SETTING_USER
;
576 case ARG_NETWORK_ZONE
: {
579 j
= strappend("vz-", optarg
);
583 if (!ifname_valid(j
)) {
584 log_error("Network zone name not valid: %s", j
);
589 free(arg_network_zone
);
590 arg_network_zone
= j
;
592 arg_network_veth
= true;
593 arg_private_network
= true;
594 arg_settings_mask
|= SETTING_NETWORK
;
598 case ARG_NETWORK_BRIDGE
:
600 if (!ifname_valid(optarg
)) {
601 log_error("Bridge interface name not valid: %s", optarg
);
605 r
= free_and_strdup(&arg_network_bridge
, optarg
);
611 arg_network_veth
= true;
612 arg_private_network
= true;
613 arg_settings_mask
|= SETTING_NETWORK
;
616 case ARG_NETWORK_VETH_EXTRA
:
617 r
= veth_extra_parse(&arg_network_veth_extra
, optarg
);
619 return log_error_errno(r
, "Failed to parse --network-veth-extra= parameter: %s", optarg
);
621 arg_private_network
= true;
622 arg_settings_mask
|= SETTING_NETWORK
;
625 case ARG_NETWORK_INTERFACE
:
627 if (!ifname_valid(optarg
)) {
628 log_error("Network interface name not valid: %s", optarg
);
632 if (strv_extend(&arg_network_interfaces
, optarg
) < 0)
635 arg_private_network
= true;
636 arg_settings_mask
|= SETTING_NETWORK
;
639 case ARG_NETWORK_MACVLAN
:
641 if (!ifname_valid(optarg
)) {
642 log_error("MACVLAN network interface name not valid: %s", optarg
);
646 if (strv_extend(&arg_network_macvlan
, optarg
) < 0)
649 arg_private_network
= true;
650 arg_settings_mask
|= SETTING_NETWORK
;
653 case ARG_NETWORK_IPVLAN
:
655 if (!ifname_valid(optarg
)) {
656 log_error("IPVLAN network interface name not valid: %s", optarg
);
660 if (strv_extend(&arg_network_ipvlan
, optarg
) < 0)
664 case ARG_PRIVATE_NETWORK
:
665 arg_private_network
= true;
666 arg_settings_mask
|= SETTING_NETWORK
;
669 case ARG_NETWORK_NAMESPACE_PATH
:
670 r
= parse_path_argument_and_warn(optarg
, false, &arg_network_namespace_path
);
677 if (arg_start_mode
== START_PID2
) {
678 log_error("--boot and --as-pid2 may not be combined.");
682 arg_start_mode
= START_BOOT
;
683 arg_settings_mask
|= SETTING_START_MODE
;
687 if (arg_start_mode
== START_BOOT
) {
688 log_error("--boot and --as-pid2 may not be combined.");
692 arg_start_mode
= START_PID2
;
693 arg_settings_mask
|= SETTING_START_MODE
;
697 r
= sd_id128_from_string(optarg
, &arg_uuid
);
699 return log_error_errno(r
, "Invalid UUID: %s", optarg
);
701 if (sd_id128_is_null(arg_uuid
)) {
702 log_error("Machine UUID may not be all zeroes.");
706 arg_settings_mask
|= SETTING_MACHINE_ID
;
715 arg_machine
= mfree(arg_machine
);
717 if (!machine_name_is_valid(optarg
)) {
718 log_error("Invalid machine name: %s", optarg
);
722 r
= free_and_strdup(&arg_machine
, optarg
);
730 arg_hostname
= mfree(arg_hostname
);
732 if (!hostname_is_valid(optarg
, false)) {
733 log_error("Invalid hostname: %s", optarg
);
737 r
= free_and_strdup(&arg_hostname
, optarg
);
742 arg_settings_mask
|= SETTING_HOSTNAME
;
746 arg_selinux_context
= optarg
;
750 arg_selinux_apifs_context
= optarg
;
754 arg_read_only
= true;
755 arg_settings_mask
|= SETTING_READ_ONLY
;
759 case ARG_DROP_CAPABILITY
: {
762 _cleanup_free_
char *t
= NULL
;
764 r
= extract_first_word(&p
, &t
, ",", 0);
766 return log_error_errno(r
, "Failed to parse capability %s.", t
);
771 if (streq(t
, "all")) {
772 if (c
== ARG_CAPABILITY
)
773 plus
= (uint64_t) -1;
775 minus
= (uint64_t) -1;
779 cap
= capability_from_name(t
);
781 log_error("Failed to parse capability %s.", t
);
785 if (c
== ARG_CAPABILITY
)
786 plus
|= 1ULL << (uint64_t) cap
;
788 minus
|= 1ULL << (uint64_t) cap
;
792 arg_settings_mask
|= SETTING_CAPABILITY
;
796 case ARG_NO_NEW_PRIVILEGES
:
797 r
= parse_boolean(optarg
);
799 return log_error_errno(r
, "Failed to parse --no-new-privileges= argument: %s", optarg
);
801 arg_no_new_privileges
= r
;
802 arg_settings_mask
|= SETTING_NO_NEW_PRIVILEGES
;
806 arg_link_journal
= LINK_GUEST
;
807 arg_link_journal_try
= true;
810 case ARG_LINK_JOURNAL
:
811 if (streq(optarg
, "auto")) {
812 arg_link_journal
= LINK_AUTO
;
813 arg_link_journal_try
= false;
814 } else if (streq(optarg
, "no")) {
815 arg_link_journal
= LINK_NO
;
816 arg_link_journal_try
= false;
817 } else if (streq(optarg
, "guest")) {
818 arg_link_journal
= LINK_GUEST
;
819 arg_link_journal_try
= false;
820 } else if (streq(optarg
, "host")) {
821 arg_link_journal
= LINK_HOST
;
822 arg_link_journal_try
= false;
823 } else if (streq(optarg
, "try-guest")) {
824 arg_link_journal
= LINK_GUEST
;
825 arg_link_journal_try
= true;
826 } else if (streq(optarg
, "try-host")) {
827 arg_link_journal
= LINK_HOST
;
828 arg_link_journal_try
= true;
830 log_error("Failed to parse link journal mode %s", optarg
);
838 r
= bind_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
, c
== ARG_BIND_RO
);
840 return log_error_errno(r
, "Failed to parse --bind(-ro)= argument %s: %m", optarg
);
842 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
846 r
= tmpfs_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
);
848 return log_error_errno(r
, "Failed to parse --tmpfs= argument %s: %m", optarg
);
850 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
855 r
= overlay_mount_parse(&arg_custom_mounts
, &arg_n_custom_mounts
, optarg
, c
== ARG_OVERLAY_RO
);
856 if (r
== -EADDRNOTAVAIL
)
857 return log_error_errno(r
, "--overlay(-ro)= needs at least two colon-separated directories specified.");
859 return log_error_errno(r
, "Failed to parse --overlay(-ro)= argument %s: %m", optarg
);
861 arg_settings_mask
|= SETTING_CUSTOM_MOUNTS
;
867 if (!env_assignment_is_valid(optarg
)) {
868 log_error("Environment variable assignment '%s' is not valid.", optarg
);
872 n
= strv_env_set(arg_setenv
, optarg
);
876 strv_free_and_replace(arg_setenv
, n
);
877 arg_settings_mask
|= SETTING_ENVIRONMENT
;
885 case ARG_SHARE_SYSTEM
:
886 /* We don't officially support this anymore, except for compat reasons. People should use the
887 * $SYSTEMD_NSPAWN_SHARE_* environment variables instead. */
888 arg_clone_ns_flags
= 0;
892 r
= parse_boolean(optarg
);
894 log_error("Failed to parse --register= argument: %s", optarg
);
902 arg_keep_unit
= true;
905 case ARG_PERSONALITY
:
907 arg_personality
= personality_from_string(optarg
);
908 if (arg_personality
== PERSONALITY_INVALID
) {
909 log_error("Unknown or unsupported personality '%s'.", optarg
);
913 arg_settings_mask
|= SETTING_PERSONALITY
;
919 arg_volatile_mode
= VOLATILE_YES
;
920 else if (streq(optarg
, "help")) {
921 DUMP_STRING_TABLE(volatile_mode
, VolatileMode
, _VOLATILE_MODE_MAX
);
926 m
= volatile_mode_from_string(optarg
);
928 log_error("Failed to parse --volatile= argument: %s", optarg
);
931 arg_volatile_mode
= m
;
934 arg_settings_mask
|= SETTING_VOLATILE_MODE
;
938 r
= expose_port_parse(&arg_expose_ports
, optarg
);
940 return log_error_errno(r
, "Duplicate port specification: %s", optarg
);
942 return log_error_errno(r
, "Failed to parse host port %s: %m", optarg
);
944 arg_settings_mask
|= SETTING_EXPOSE_PORTS
;
948 if (strv_extend(&arg_property
, optarg
) < 0)
953 case ARG_PRIVATE_USERS
: {
958 else if (!in_charset(optarg
, DIGITS
))
959 /* do *not* parse numbers as booleans */
960 boolean
= parse_boolean(optarg
);
962 if (boolean
== false) {
963 /* no: User namespacing off */
964 arg_userns_mode
= USER_NAMESPACE_NO
;
965 arg_uid_shift
= UID_INVALID
;
966 arg_uid_range
= UINT32_C(0x10000);
967 } else if (boolean
== true) {
968 /* yes: User namespacing on, UID range is read from root dir */
969 arg_userns_mode
= USER_NAMESPACE_FIXED
;
970 arg_uid_shift
= UID_INVALID
;
971 arg_uid_range
= UINT32_C(0x10000);
972 } else if (streq(optarg
, "pick")) {
973 /* pick: User namespacing on, UID range is picked randomly */
974 arg_userns_mode
= USER_NAMESPACE_PICK
;
975 arg_uid_shift
= UID_INVALID
;
976 arg_uid_range
= UINT32_C(0x10000);
978 _cleanup_free_
char *buffer
= NULL
;
979 const char *range
, *shift
;
981 /* anything else: User namespacing on, UID range is explicitly configured */
983 range
= strchr(optarg
, ':');
985 buffer
= strndup(optarg
, range
- optarg
);
991 r
= safe_atou32(range
, &arg_uid_range
);
993 return log_error_errno(r
, "Failed to parse UID range \"%s\": %m", range
);
997 r
= parse_uid(shift
, &arg_uid_shift
);
999 return log_error_errno(r
, "Failed to parse UID \"%s\": %m", optarg
);
1001 arg_userns_mode
= USER_NAMESPACE_FIXED
;
1004 if (arg_uid_range
<= 0) {
1005 log_error("UID range cannot be 0.");
1009 arg_settings_mask
|= SETTING_USERNS
;
1014 if (userns_supported()) {
1015 arg_userns_mode
= USER_NAMESPACE_PICK
;
1016 arg_uid_shift
= UID_INVALID
;
1017 arg_uid_range
= UINT32_C(0x10000);
1019 arg_settings_mask
|= SETTING_USERNS
;
1024 case ARG_PRIVATE_USERS_CHOWN
:
1025 arg_userns_chown
= true;
1027 arg_settings_mask
|= SETTING_USERNS
;
1030 case ARG_KILL_SIGNAL
:
1031 if (streq(optarg
, "help")) {
1032 DUMP_STRING_TABLE(signal
, int, _NSIG
);
1036 arg_kill_signal
= signal_from_string(optarg
);
1037 if (arg_kill_signal
< 0) {
1038 log_error("Cannot parse signal: %s", optarg
);
1042 arg_settings_mask
|= SETTING_KILL_SIGNAL
;
1047 /* no → do not read files
1048 * yes → read files, do not override cmdline, trust only subset
1049 * override → read files, override cmdline, trust only subset
1050 * trusted → read files, do not override cmdline, trust all
1053 r
= parse_boolean(optarg
);
1055 if (streq(optarg
, "trusted")) {
1056 mask_all_settings
= false;
1057 mask_no_settings
= false;
1058 arg_settings_trusted
= true;
1060 } else if (streq(optarg
, "override")) {
1061 mask_all_settings
= false;
1062 mask_no_settings
= true;
1063 arg_settings_trusted
= -1;
1065 return log_error_errno(r
, "Failed to parse --settings= argument: %s", optarg
);
1068 mask_all_settings
= false;
1069 mask_no_settings
= false;
1070 arg_settings_trusted
= -1;
1073 mask_all_settings
= true;
1074 mask_no_settings
= false;
1075 arg_settings_trusted
= false;
1081 if (!path_is_absolute(optarg
)) {
1082 log_error("Working directory %s is not an absolute path.", optarg
);
1086 r
= free_and_strdup(&arg_chdir
, optarg
);
1090 arg_settings_mask
|= SETTING_WORKING_DIRECTORY
;
1093 case ARG_PIVOT_ROOT
:
1094 r
= pivot_root_parse(&arg_pivot_root_new
, &arg_pivot_root_old
, optarg
);
1096 return log_error_errno(r
, "Failed to parse --pivot-root= argument %s: %m", optarg
);
1098 arg_settings_mask
|= SETTING_PIVOT_ROOT
;
1101 case ARG_NOTIFY_READY
:
1102 r
= parse_boolean(optarg
);
1104 log_error("%s is not a valid notify mode. Valid modes are: yes, no, and ready.", optarg
);
1107 arg_notify_ready
= r
;
1108 arg_settings_mask
|= SETTING_NOTIFY_READY
;
1111 case ARG_ROOT_HASH
: {
1115 r
= unhexmem(optarg
, strlen(optarg
), &k
, &l
);
1117 return log_error_errno(r
, "Failed to parse root hash: %s", optarg
);
1118 if (l
< sizeof(sd_id128_t
)) {
1119 log_error("Root hash must be at least 128bit long: %s", optarg
);
1124 free(arg_root_hash
);
1126 arg_root_hash_size
= l
;
1130 case ARG_SYSTEM_CALL_FILTER
: {
1134 negative
= optarg
[0] == '~';
1135 items
= negative
? optarg
+ 1 : optarg
;
1138 _cleanup_free_
char *word
= NULL
;
1140 r
= extract_first_word(&items
, &word
, NULL
, 0);
1146 return log_error_errno(r
, "Failed to parse system call filter: %m");
1149 r
= strv_extend(&arg_syscall_blacklist
, word
);
1151 r
= strv_extend(&arg_syscall_whitelist
, word
);
1156 arg_settings_mask
|= SETTING_SYSCALL_FILTER
;
1165 if (streq(optarg
, "help")) {
1166 DUMP_STRING_TABLE(rlimit
, int, _RLIMIT_MAX
);
1170 eq
= strchr(optarg
, '=');
1172 log_error("--rlimit= expects an '=' assignment.");
1176 name
= strndup(optarg
, eq
- optarg
);
1180 rl
= rlimit_from_string_harder(name
);
1182 log_error("Unknown resource limit: %s", name
);
1186 if (!arg_rlimit
[rl
]) {
1187 arg_rlimit
[rl
] = new0(struct rlimit
, 1);
1188 if (!arg_rlimit
[rl
])
1192 r
= rlimit_parse(rl
, eq
+ 1, arg_rlimit
[rl
]);
1194 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
+ 1);
1196 arg_settings_mask
|= SETTING_RLIMIT_FIRST
<< rl
;
1200 case ARG_OOM_SCORE_ADJUST
:
1201 r
= parse_oom_score_adjust(optarg
, &arg_oom_score_adjust
);
1203 return log_error_errno(r
, "Failed to parse --oom-score-adjust= parameter: %s", optarg
);
1205 arg_oom_score_adjust_set
= true;
1206 arg_settings_mask
|= SETTING_OOM_SCORE_ADJUST
;
1209 case ARG_CPU_AFFINITY
: {
1210 _cleanup_cpu_free_ cpu_set_t
*cpuset
= NULL
;
1212 r
= parse_cpu_set(optarg
, &cpuset
);
1214 return log_error_errno(r
, "Failed to parse CPU affinity mask: %s", optarg
);
1217 CPU_FREE(arg_cpuset
);
1219 arg_cpuset
= TAKE_PTR(cpuset
);
1220 arg_cpuset_ncpus
= r
;
1221 arg_settings_mask
|= SETTING_CPU_AFFINITY
;
1229 assert_not_reached("Unhandled option");
1232 /* If --network-namespace-path is given with any other network-related option,
1233 * we need to error out, to avoid conflicts between different network options. */
1234 if (arg_network_namespace_path
&&
1235 (arg_network_interfaces
|| arg_network_macvlan
||
1236 arg_network_ipvlan
|| arg_network_veth_extra
||
1237 arg_network_bridge
|| arg_network_zone
||
1238 arg_network_veth
|| arg_private_network
)) {
1239 log_error("--network-namespace-path cannot be combined with other network options.");
1243 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_IPC", CLONE_NEWIPC
);
1244 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_PID", CLONE_NEWPID
);
1245 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_NS_UTS", CLONE_NEWUTS
);
1246 parse_share_ns_env("SYSTEMD_NSPAWN_SHARE_SYSTEM", CLONE_NEWIPC
|CLONE_NEWPID
|CLONE_NEWUTS
);
1248 if (arg_userns_mode
!= USER_NAMESPACE_NO
)
1249 arg_mount_settings
|= MOUNT_USE_USERNS
;
1251 if (arg_private_network
)
1252 arg_mount_settings
|= MOUNT_APPLY_APIVFS_NETNS
;
1254 parse_mount_settings_env();
1256 if (!(arg_clone_ns_flags
& CLONE_NEWPID
) ||
1257 !(arg_clone_ns_flags
& CLONE_NEWUTS
)) {
1258 arg_register
= false;
1259 if (arg_start_mode
!= START_PID1
) {
1260 log_error("--boot cannot be used without namespacing.");
1265 if (arg_userns_mode
== USER_NAMESPACE_PICK
)
1266 arg_userns_chown
= true;
1268 if (arg_keep_unit
&& arg_register
&& cg_pid_get_owner_uid(0, NULL
) >= 0) {
1269 /* Save the user from accidentally registering either user-$SESSION.scope or user@.service.
1270 * The latter is not technically a user session, but we don't need to labour the point. */
1271 log_error("--keep-unit --register=yes may not be used when invoked from a user session.");
1275 if (arg_directory
&& arg_image
) {
1276 log_error("--directory= and --image= may not be combined.");
1280 if (arg_template
&& arg_image
) {
1281 log_error("--template= and --image= may not be combined.");
1285 if (arg_ephemeral
&& arg_template
&& !arg_directory
) {
1286 /* User asked for ephemeral execution but specified --template= instead of --directory=. Semantically
1287 * such an invocation makes some sense, see https://github.com/systemd/systemd/issues/3667. Let's
1288 * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
1291 arg_directory
= TAKE_PTR(arg_template
);
1294 if (arg_template
&& !(arg_directory
|| arg_machine
)) {
1295 log_error("--template= needs --directory= or --machine=.");
1299 if (arg_ephemeral
&& arg_template
) {
1300 log_error("--ephemeral and --template= may not be combined.");
1304 if (arg_ephemeral
&& !IN_SET(arg_link_journal
, LINK_NO
, LINK_AUTO
)) {
1305 log_error("--ephemeral and --link-journal= may not be combined.");
1309 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& !userns_supported()) {
1310 log_error("--private-users= is not supported, kernel compiled without user namespace support.");
1314 if (arg_userns_chown
&& arg_read_only
) {
1315 log_error("--read-only and --private-users-chown may not be combined.");
1319 if (arg_network_bridge
&& arg_network_zone
) {
1320 log_error("--network-bridge= and --network-zone= may not be combined.");
1324 if (argc
> optind
) {
1325 arg_parameters
= strv_copy(argv
+ optind
);
1326 if (!arg_parameters
)
1329 arg_settings_mask
|= SETTING_START_MODE
;
1332 /* Load all settings from .nspawn files */
1333 if (mask_no_settings
)
1334 arg_settings_mask
= 0;
1336 /* Don't load any settings from .nspawn files */
1337 if (mask_all_settings
)
1338 arg_settings_mask
= _SETTINGS_MASK_ALL
;
1340 arg_caps_retain
= (arg_caps_retain
| plus
| (arg_private_network
? 1ULL << CAP_NET_ADMIN
: 0)) & ~minus
;
1342 r
= cg_unified_flush();
1344 return log_error_errno(r
, "Failed to determine whether the unified cgroups hierarchy is used: %m");
1346 e
= getenv("SYSTEMD_NSPAWN_CONTAINER_SERVICE");
1348 arg_container_service_name
= e
;
1350 r
= getenv_bool("SYSTEMD_NSPAWN_USE_CGNS");
1352 arg_use_cgns
= cg_ns_supported();
1356 r
= custom_mount_check_all();
1363 static int verify_arguments(void) {
1364 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& (arg_mount_settings
& MOUNT_APPLY_APIVFS_NETNS
) && !arg_private_network
) {
1365 log_error("Invalid namespacing settings. Mounting sysfs with --private-users requires --private-network.");
1369 if (arg_userns_mode
!= USER_NAMESPACE_NO
&& !(arg_mount_settings
& MOUNT_APPLY_APIVFS_RO
)) {
1370 log_error("Cannot combine --private-users with read-write mounts.");
1374 if (arg_volatile_mode
!= VOLATILE_NO
&& arg_read_only
) {
1375 log_error("Cannot combine --read-only with --volatile. Note that --volatile already implies a read-only base hierarchy.");
1379 if (arg_expose_ports
&& !arg_private_network
) {
1380 log_error("Cannot use --port= without private networking.");
1385 if (arg_expose_ports
) {
1386 log_error("--port= is not supported, compiled without libiptc support.");
1391 if (arg_start_mode
== START_BOOT
&& arg_kill_signal
<= 0)
1392 arg_kill_signal
= SIGRTMIN
+3;
1397 static int userns_lchown(const char *p
, uid_t uid
, gid_t gid
) {
1400 if (arg_userns_mode
== USER_NAMESPACE_NO
)
1403 if (uid
== UID_INVALID
&& gid
== GID_INVALID
)
1406 if (uid
!= UID_INVALID
) {
1407 uid
+= arg_uid_shift
;
1409 if (uid
< arg_uid_shift
|| uid
>= arg_uid_shift
+ arg_uid_range
)
1413 if (gid
!= GID_INVALID
) {
1414 gid
+= (gid_t
) arg_uid_shift
;
1416 if (gid
< (gid_t
) arg_uid_shift
|| gid
>= (gid_t
) (arg_uid_shift
+ arg_uid_range
))
1420 if (lchown(p
, uid
, gid
) < 0)
1426 static int userns_mkdir(const char *root
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
1430 q
= prefix_roota(root
, path
);
1431 r
= mkdir_errno_wrapper(q
, mode
);
1437 return userns_lchown(q
, uid
, gid
);
1440 static int setup_timezone(const char *dest
) {
1441 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
1442 const char *where
, *check
, *what
;
1448 /* Fix the timezone, if possible */
1449 r
= readlink_malloc("/etc/localtime", &p
);
1451 log_warning("host's /etc/localtime is not a symlink, not updating container timezone.");
1452 /* to handle warning, delete /etc/localtime and replace it
1453 * with a symbolic link to a time zone data file.
1456 * ln -s /usr/share/zoneinfo/UTC /etc/localtime
1461 z
= path_startswith(p
, "../usr/share/zoneinfo/");
1463 z
= path_startswith(p
, "/usr/share/zoneinfo/");
1465 log_warning("/etc/localtime does not point into /usr/share/zoneinfo/, not updating container timezone.");
1469 where
= prefix_roota(dest
, "/etc/localtime");
1470 r
= readlink_malloc(where
, &q
);
1472 y
= path_startswith(q
, "../usr/share/zoneinfo/");
1474 y
= path_startswith(q
, "/usr/share/zoneinfo/");
1476 /* Already pointing to the right place? Then do nothing .. */
1477 if (y
&& streq(y
, z
))
1481 check
= strjoina("/usr/share/zoneinfo/", z
);
1482 check
= prefix_roota(dest
, check
);
1483 if (laccess(check
, F_OK
) < 0) {
1484 log_warning("Timezone %s does not exist in container, not updating container timezone.", z
);
1488 if (unlink(where
) < 0 && errno
!= ENOENT
) {
1489 log_full_errno(IN_SET(errno
, EROFS
, EACCES
, EPERM
) ? LOG_DEBUG
: LOG_WARNING
, /* Don't complain on read-only images */
1491 "Failed to remove existing timezone info %s in container, ignoring: %m", where
);
1495 what
= strjoina("../usr/share/zoneinfo/", z
);
1496 if (symlink(what
, where
) < 0) {
1497 log_full_errno(IN_SET(errno
, EROFS
, EACCES
, EPERM
) ? LOG_DEBUG
: LOG_WARNING
,
1499 "Failed to correct timezone of container, ignoring: %m");
1503 r
= userns_lchown(where
, 0, 0);
1505 return log_warning_errno(r
, "Failed to chown /etc/localtime: %m");
1510 static int resolved_listening(void) {
1511 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1512 _cleanup_free_
char *dns_stub_listener_mode
= NULL
;
1515 /* Check if resolved is listening */
1517 r
= sd_bus_open_system(&bus
);
1521 r
= bus_name_has_owner(bus
, "org.freedesktop.resolve1", NULL
);
1525 r
= sd_bus_get_property_string(bus
,
1526 "org.freedesktop.resolve1",
1527 "/org/freedesktop/resolve1",
1528 "org.freedesktop.resolve1.Manager",
1531 &dns_stub_listener_mode
);
1535 return STR_IN_SET(dns_stub_listener_mode
, "udp", "yes");
1538 static int setup_resolv_conf(const char *dest
) {
1539 _cleanup_free_
char *resolved
= NULL
, *etc
= NULL
;
1545 if (arg_private_network
)
1548 r
= chase_symlinks("/etc", dest
, CHASE_PREFIX_ROOT
, &etc
);
1550 log_warning_errno(r
, "Failed to resolve /etc path in container, ignoring: %m");
1554 where
= strjoina(etc
, "/resolv.conf");
1555 found
= chase_symlinks(where
, dest
, CHASE_NONEXISTENT
, &resolved
);
1557 log_warning_errno(found
, "Failed to resolve /etc/resolv.conf path in container, ignoring: %m");
1561 if (access(STATIC_RESOLV_CONF
, F_OK
) >= 0 &&
1562 resolved_listening() > 0) {
1564 /* resolved is enabled on the host. In this, case bind mount its static resolv.conf file into the
1565 * container, so that the container can use the host's resolver. Given that network namespacing is
1566 * disabled it's only natural of the container also uses the host's resolver. It also has the big
1567 * advantage that the container will be able to follow the host's DNS server configuration changes
1570 if (found
== 0) /* missing? */
1571 (void) touch(resolved
);
1573 r
= mount_verbose(LOG_DEBUG
, STATIC_RESOLV_CONF
, resolved
, NULL
, MS_BIND
, NULL
);
1575 return mount_verbose(LOG_ERR
, NULL
, resolved
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NODEV
, NULL
);
1578 /* If that didn't work, let's copy the file */
1579 r
= copy_file("/etc/resolv.conf", where
, O_TRUNC
|O_NOFOLLOW
, 0644, 0, COPY_REFLINK
);
1581 /* If the file already exists as symlink, let's suppress the warning, under the assumption that
1582 * resolved or something similar runs inside and the symlink points there.
1584 * If the disk image is read-only, there's also no point in complaining.
1586 log_full_errno(IN_SET(r
, -ELOOP
, -EROFS
, -EACCES
, -EPERM
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1587 "Failed to copy /etc/resolv.conf to %s, ignoring: %m", where
);
1591 r
= userns_lchown(where
, 0, 0);
1593 log_warning_errno(r
, "Failed to chown /etc/resolv.conf, ignoring: %m");
1598 static int setup_boot_id(void) {
1599 _cleanup_(unlink_and_freep
) char *from
= NULL
;
1600 _cleanup_free_
char *path
= NULL
;
1601 sd_id128_t rnd
= SD_ID128_NULL
;
1605 /* Generate a new randomized boot ID, so that each boot-up of
1606 * the container gets a new one */
1608 r
= tempfn_random_child(NULL
, "proc-sys-kernel-random-boot-id", &path
);
1610 return log_error_errno(r
, "Failed to generate random boot ID path: %m");
1612 r
= sd_id128_randomize(&rnd
);
1614 return log_error_errno(r
, "Failed to generate random boot id: %m");
1616 r
= id128_write(path
, ID128_UUID
, rnd
, false);
1618 return log_error_errno(r
, "Failed to write boot id: %m");
1620 from
= TAKE_PTR(path
);
1621 to
= "/proc/sys/kernel/random/boot_id";
1623 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
1627 return mount_verbose(LOG_ERR
, NULL
, to
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
1630 static int copy_devnodes(const char *dest
) {
1632 static const char devnodes
[] =
1643 _cleanup_umask_ mode_t u
;
1649 /* Create /dev/net, so that we can create /dev/net/tun in it */
1650 if (userns_mkdir(dest
, "/dev/net", 0755, 0, 0) < 0)
1651 return log_error_errno(r
, "Failed to create /dev/net directory: %m");
1653 NULSTR_FOREACH(d
, devnodes
) {
1654 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
1657 from
= strappend("/dev/", d
);
1658 to
= prefix_root(dest
, from
);
1660 if (stat(from
, &st
) < 0) {
1662 if (errno
!= ENOENT
)
1663 return log_error_errno(errno
, "Failed to stat %s: %m", from
);
1665 } else if (!S_ISCHR(st
.st_mode
) && !S_ISBLK(st
.st_mode
)) {
1667 log_error("%s is not a char or block device, cannot copy.", from
);
1671 if (mknod(to
, st
.st_mode
, st
.st_rdev
) < 0) {
1672 /* Explicitly warn the user when /dev is already populated. */
1673 if (errno
== EEXIST
)
1674 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
);
1676 return log_error_errno(errno
, "mknod(%s) failed: %m", to
);
1678 /* Some systems abusively restrict mknod but
1679 * allow bind mounts. */
1682 return log_error_errno(r
, "touch (%s) failed: %m", to
);
1683 r
= mount_verbose(LOG_DEBUG
, from
, to
, NULL
, MS_BIND
, NULL
);
1685 return log_error_errno(r
, "Both mknod and bind mount (%s) failed: %m", to
);
1688 r
= userns_lchown(to
, 0, 0);
1690 return log_error_errno(r
, "chown() of device node %s failed: %m", to
);
1697 static int setup_pts(const char *dest
) {
1698 _cleanup_free_
char *options
= NULL
;
1703 if (arg_selinux_apifs_context
)
1704 (void) asprintf(&options
,
1705 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT
",context=\"%s\"",
1706 arg_uid_shift
+ TTY_GID
,
1707 arg_selinux_apifs_context
);
1710 (void) asprintf(&options
,
1711 "newinstance,ptmxmode=0666,mode=620,gid=" GID_FMT
,
1712 arg_uid_shift
+ TTY_GID
);
1717 /* Mount /dev/pts itself */
1718 p
= prefix_roota(dest
, "/dev/pts");
1719 r
= mkdir_errno_wrapper(p
, 0755);
1721 return log_error_errno(r
, "Failed to create /dev/pts: %m");
1723 r
= mount_verbose(LOG_ERR
, "devpts", p
, "devpts", MS_NOSUID
|MS_NOEXEC
, options
);
1726 r
= userns_lchown(p
, 0, 0);
1728 return log_error_errno(r
, "Failed to chown /dev/pts: %m");
1730 /* Create /dev/ptmx symlink */
1731 p
= prefix_roota(dest
, "/dev/ptmx");
1732 if (symlink("pts/ptmx", p
) < 0)
1733 return log_error_errno(errno
, "Failed to create /dev/ptmx symlink: %m");
1734 r
= userns_lchown(p
, 0, 0);
1736 return log_error_errno(r
, "Failed to chown /dev/ptmx: %m");
1738 /* And fix /dev/pts/ptmx ownership */
1739 p
= prefix_roota(dest
, "/dev/pts/ptmx");
1740 r
= userns_lchown(p
, 0, 0);
1742 return log_error_errno(r
, "Failed to chown /dev/pts/ptmx: %m");
1747 static int setup_dev_console(const char *dest
, const char *console
) {
1748 _cleanup_umask_ mode_t u
;
1757 r
= chmod_and_chown(console
, 0600, arg_uid_shift
, arg_uid_shift
);
1759 return log_error_errno(r
, "Failed to correct access mode for TTY: %m");
1761 /* We need to bind mount the right tty to /dev/console since
1762 * ptys can only exist on pts file systems. To have something
1763 * to bind mount things on we create a empty regular file. */
1765 to
= prefix_roota(dest
, "/dev/console");
1768 return log_error_errno(r
, "touch() for /dev/console failed: %m");
1770 return mount_verbose(LOG_ERR
, console
, to
, NULL
, MS_BIND
, NULL
);
1773 static int setup_keyring(void) {
1774 key_serial_t keyring
;
1776 /* Allocate a new session keyring for the container. This makes sure the keyring of the session systemd-nspawn
1777 * was invoked from doesn't leak into the container. Note that by default we block keyctl() and request_key()
1778 * anyway via seccomp so doing this operation isn't strictly necessary, but in case people explicitly whitelist
1779 * these system calls let's make sure we don't leak anything into the container. */
1781 keyring
= keyctl(KEYCTL_JOIN_SESSION_KEYRING
, 0, 0, 0, 0);
1782 if (keyring
== -1) {
1783 if (errno
== ENOSYS
)
1784 log_debug_errno(errno
, "Kernel keyring not supported, ignoring.");
1785 else if (IN_SET(errno
, EACCES
, EPERM
))
1786 log_debug_errno(errno
, "Kernel keyring access prohibited, ignoring.");
1788 return log_error_errno(errno
, "Setting up kernel keyring failed: %m");
1794 static int setup_kmsg(int kmsg_socket
) {
1795 _cleanup_(unlink_and_freep
) char *from
= NULL
;
1796 _cleanup_free_
char *fifo
= NULL
;
1797 _cleanup_close_
int fd
= -1;
1798 _cleanup_umask_ mode_t u
;
1802 assert(kmsg_socket
>= 0);
1806 /* We create the kmsg FIFO as as temporary file in /tmp, but immediately delete it after bind mounting it to
1807 * /proc/kmsg. While FIFOs on the reading side behave very similar to /proc/kmsg, their writing side behaves
1808 * differently from /dev/kmsg in that writing blocks when nothing is reading. In order to avoid any problems
1809 * with containers deadlocking due to this we simply make /dev/kmsg unavailable to the container. */
1811 r
= tempfn_random_child(NULL
, "proc-kmsg", &fifo
);
1813 return log_error_errno(r
, "Failed to generate kmsg path: %m");
1815 if (mkfifo(fifo
, 0600) < 0)
1816 return log_error_errno(errno
, "mkfifo() for /run/kmsg failed: %m");
1818 from
= TAKE_PTR(fifo
);
1821 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
1825 fd
= open(from
, O_RDWR
|O_NDELAY
|O_CLOEXEC
);
1827 return log_error_errno(errno
, "Failed to open fifo: %m");
1829 /* Store away the fd in the socket, so that it stays open as long as we run the child */
1830 r
= send_one_fd(kmsg_socket
, fd
, 0);
1832 return log_error_errno(r
, "Failed to send FIFO fd: %m");
1837 static int on_address_change(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1838 union in_addr_union
*exposed
= userdata
;
1844 expose_port_execute(rtnl
, arg_expose_ports
, exposed
);
1848 static int setup_hostname(void) {
1851 if ((arg_clone_ns_flags
& CLONE_NEWUTS
) == 0)
1854 r
= sethostname_idempotent(arg_hostname
?: arg_machine
);
1856 return log_error_errno(r
, "Failed to set hostname: %m");
1861 static int setup_journal(const char *directory
) {
1863 _cleanup_free_
char *d
= NULL
;
1869 /* Don't link journals in ephemeral mode */
1873 if (arg_link_journal
== LINK_NO
)
1876 try = arg_link_journal_try
|| arg_link_journal
== LINK_AUTO
;
1878 r
= sd_id128_get_machine(&this_id
);
1880 return log_error_errno(r
, "Failed to retrieve machine ID: %m");
1882 if (sd_id128_equal(arg_uuid
, this_id
)) {
1883 log_full(try ? LOG_WARNING
: LOG_ERR
,
1884 "Host and machine ids are equal (%s): refusing to link journals", sd_id128_to_string(arg_uuid
, id
));
1890 r
= userns_mkdir(directory
, "/var", 0755, 0, 0);
1892 return log_error_errno(r
, "Failed to create /var: %m");
1894 r
= userns_mkdir(directory
, "/var/log", 0755, 0, 0);
1896 return log_error_errno(r
, "Failed to create /var/log: %m");
1898 r
= userns_mkdir(directory
, "/var/log/journal", 0755, 0, 0);
1900 return log_error_errno(r
, "Failed to create /var/log/journal: %m");
1902 (void) sd_id128_to_string(arg_uuid
, id
);
1904 p
= strjoina("/var/log/journal/", id
);
1905 q
= prefix_roota(directory
, p
);
1907 if (path_is_mount_point(p
, NULL
, 0) > 0) {
1911 log_error("%s: already a mount point, refusing to use for journal", p
);
1915 if (path_is_mount_point(q
, NULL
, 0) > 0) {
1919 log_error("%s: already a mount point, refusing to use for journal", q
);
1923 r
= readlink_and_make_absolute(p
, &d
);
1925 if (IN_SET(arg_link_journal
, LINK_GUEST
, LINK_AUTO
) &&
1928 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
1930 log_warning_errno(r
, "Failed to create directory %s: %m", q
);
1935 return log_error_errno(errno
, "Failed to remove symlink %s: %m", p
);
1936 } else if (r
== -EINVAL
) {
1938 if (arg_link_journal
== LINK_GUEST
&&
1941 if (errno
== ENOTDIR
) {
1942 log_error("%s already exists and is neither a symlink nor a directory", p
);
1945 return log_error_errno(errno
, "Failed to remove %s: %m", p
);
1947 } else if (r
!= -ENOENT
)
1948 return log_error_errno(r
, "readlink(%s) failed: %m", p
);
1950 if (arg_link_journal
== LINK_GUEST
) {
1952 if (symlink(q
, p
) < 0) {
1954 log_debug_errno(errno
, "Failed to symlink %s to %s, skipping journal setup: %m", q
, p
);
1957 return log_error_errno(errno
, "Failed to symlink %s to %s: %m", q
, p
);
1960 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
1962 log_warning_errno(r
, "Failed to create directory %s: %m", q
);
1966 if (arg_link_journal
== LINK_HOST
) {
1967 /* don't create parents here — if the host doesn't have
1968 * permanent journal set up, don't force it here */
1970 r
= mkdir_errno_wrapper(p
, 0755);
1971 if (r
< 0 && r
!= -EEXIST
) {
1973 log_debug_errno(r
, "Failed to create %s, skipping journal setup: %m", p
);
1976 return log_error_errno(r
, "Failed to create %s: %m", p
);
1979 } else if (access(p
, F_OK
) < 0)
1982 if (dir_is_empty(q
) == 0)
1983 log_warning("%s is not empty, proceeding anyway.", q
);
1985 r
= userns_mkdir(directory
, p
, 0755, 0, 0);
1987 return log_error_errno(r
, "Failed to create %s: %m", q
);
1989 r
= mount_verbose(LOG_DEBUG
, p
, q
, NULL
, MS_BIND
, NULL
);
1991 return log_error_errno(errno
, "Failed to bind mount journal from host into guest: %m");
1996 static int drop_capabilities(void) {
1997 return capability_bounding_set_drop(arg_caps_retain
, false);
2000 static int reset_audit_loginuid(void) {
2001 _cleanup_free_
char *p
= NULL
;
2004 if ((arg_clone_ns_flags
& CLONE_NEWPID
) == 0)
2007 r
= read_one_line_file("/proc/self/loginuid", &p
);
2011 return log_error_errno(r
, "Failed to read /proc/self/loginuid: %m");
2013 /* Already reset? */
2014 if (streq(p
, "4294967295"))
2017 r
= write_string_file("/proc/self/loginuid", "4294967295", 0);
2020 "Failed to reset audit login UID. This probably means that your kernel is too\n"
2021 "old and you have audit enabled. Note that the auditing subsystem is known to\n"
2022 "be incompatible with containers on old kernels. Please make sure to upgrade\n"
2023 "your kernel or to off auditing with 'audit=0' on the kernel command line before\n"
2024 "using systemd-nspawn. Sleeping for 5s... (%m)");
2032 static int setup_propagate(const char *root
) {
2036 (void) mkdir_p("/run/systemd/nspawn/", 0755);
2037 (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
2038 p
= strjoina("/run/systemd/nspawn/propagate/", arg_machine
);
2039 (void) mkdir_p(p
, 0600);
2041 r
= userns_mkdir(root
, "/run/systemd", 0755, 0, 0);
2043 return log_error_errno(r
, "Failed to create /run/systemd: %m");
2045 r
= userns_mkdir(root
, "/run/systemd/nspawn", 0755, 0, 0);
2047 return log_error_errno(r
, "Failed to create /run/systemd/nspawn: %m");
2049 r
= userns_mkdir(root
, "/run/systemd/nspawn/incoming", 0600, 0, 0);
2051 return log_error_errno(r
, "Failed to create /run/systemd/nspawn/incoming: %m");
2053 q
= prefix_roota(root
, "/run/systemd/nspawn/incoming");
2054 r
= mount_verbose(LOG_ERR
, p
, q
, NULL
, MS_BIND
, NULL
);
2058 r
= mount_verbose(LOG_ERR
, NULL
, q
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
);
2062 /* machined will MS_MOVE into that directory, and that's only
2063 * supported for non-shared mounts. */
2064 return mount_verbose(LOG_ERR
, NULL
, q
, NULL
, MS_SLAVE
, NULL
);
2067 static int setup_machine_id(const char *directory
) {
2068 const char *etc_machine_id
;
2072 /* If the UUID in the container is already set, then that's what counts, and we use. If it isn't set, and the
2073 * caller passed --uuid=, then we'll pass it in the $container_uuid env var to PID 1 of the container. The
2074 * assumption is that PID 1 will then write it to /etc/machine-id to make it persistent. If --uuid= is not
2075 * passed we generate a random UUID, and pass it via $container_uuid. In effect this means that /etc/machine-id
2076 * in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
2077 * container behaves nicely). */
2079 etc_machine_id
= prefix_roota(directory
, "/etc/machine-id");
2081 r
= id128_read(etc_machine_id
, ID128_PLAIN
, &id
);
2083 if (!IN_SET(r
, -ENOENT
, -ENOMEDIUM
)) /* If the file is missing or empty, we don't mind */
2084 return log_error_errno(r
, "Failed to read machine ID from container image: %m");
2086 if (sd_id128_is_null(arg_uuid
)) {
2087 r
= sd_id128_randomize(&arg_uuid
);
2089 return log_error_errno(r
, "Failed to acquire randomized machine UUID: %m");
2092 if (sd_id128_is_null(id
)) {
2093 log_error("Machine ID in container image is zero, refusing.");
2103 static int recursive_chown(const char *directory
, uid_t shift
, uid_t range
) {
2108 if (arg_userns_mode
== USER_NAMESPACE_NO
|| !arg_userns_chown
)
2111 r
= path_patch_uid(directory
, arg_uid_shift
, arg_uid_range
);
2112 if (r
== -EOPNOTSUPP
)
2113 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.");
2115 return log_error_errno(r
, "Upper 16 bits of root directory UID and GID do not match.");
2117 return log_error_errno(r
, "Failed to adjust UID/GID shift of OS tree: %m");
2119 log_debug("Root directory of image is already owned by the right UID/GID range, skipping recursive chown operation.");
2121 log_debug("Patched directory tree to match UID/GID range.");
2128 * < 0 : wait_for_terminate() failed to get the state of the
2129 * container, the container was terminated by a signal, or
2130 * failed for an unknown reason. No change is made to the
2131 * container argument.
2132 * > 0 : The program executed in the container terminated with an
2133 * error. The exit code of the program executed in the
2134 * container is returned. The container argument has been set
2135 * to CONTAINER_TERMINATED.
2136 * 0 : The container is being rebooted, has been shut down or exited
2137 * successfully. The container argument has been set to either
2138 * CONTAINER_TERMINATED or CONTAINER_REBOOTED.
2140 * That is, success is indicated by a return value of zero, and an
2141 * error is indicated by a non-zero value.
2143 static int wait_for_container(pid_t pid
, ContainerStatus
*container
) {
2147 r
= wait_for_terminate(pid
, &status
);
2149 return log_warning_errno(r
, "Failed to wait for container: %m");
2151 switch (status
.si_code
) {
2154 if (status
.si_status
== 0)
2155 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s exited successfully.", arg_machine
);
2157 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s failed with error code %i.", arg_machine
, status
.si_status
);
2159 *container
= CONTAINER_TERMINATED
;
2160 return status
.si_status
;
2163 if (status
.si_status
== SIGINT
) {
2164 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s has been shut down.", arg_machine
);
2165 *container
= CONTAINER_TERMINATED
;
2168 } else if (status
.si_status
== SIGHUP
) {
2169 log_full(arg_quiet
? LOG_DEBUG
: LOG_INFO
, "Container %s is being rebooted.", arg_machine
);
2170 *container
= CONTAINER_REBOOTED
;
2176 log_error("Container %s terminated by signal %s.", arg_machine
, signal_to_string(status
.si_status
));
2180 log_error("Container %s failed due to unknown reason.", arg_machine
);
2185 static int on_orderly_shutdown(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
2188 pid
= PTR_TO_PID(userdata
);
2190 if (kill(pid
, arg_kill_signal
) >= 0) {
2191 log_info("Trying to halt container. Send SIGTERM again to trigger immediate termination.");
2192 sd_event_source_set_userdata(s
, NULL
);
2197 sd_event_exit(sd_event_source_get_event(s
), 0);
2201 static int on_sigchld(sd_event_source
*s
, const struct signalfd_siginfo
*ssi
, void *userdata
) {
2207 pid
= PTR_TO_PID(userdata
);
2212 if (waitid(P_ALL
, 0, &si
, WNOHANG
|WNOWAIT
|WEXITED
) < 0)
2213 return log_error_errno(errno
, "Failed to waitid(): %m");
2214 if (si
.si_pid
== 0) /* No pending children. */
2216 if (si
.si_pid
== pid
) {
2217 /* The main process we care for has exited. Return from
2218 * signal handler but leave the zombie. */
2219 sd_event_exit(sd_event_source_get_event(s
), 0);
2223 /* Reap all other children. */
2224 (void) waitid(P_PID
, si
.si_pid
, &si
, WNOHANG
|WEXITED
);
2230 static int on_request_stop(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
2235 pid
= PTR_TO_PID(userdata
);
2237 if (arg_kill_signal
> 0) {
2238 log_info("Container termination requested. Attempting to halt container.");
2239 (void) kill(pid
, arg_kill_signal
);
2241 log_info("Container termination requested. Exiting.");
2242 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), 0);
2248 static int determine_names(void) {
2251 if (arg_template
&& !arg_directory
&& arg_machine
) {
2253 /* If --template= was specified then we should not
2254 * search for a machine, but instead create a new one
2255 * in /var/lib/machine. */
2257 arg_directory
= strjoin("/var/lib/machines/", arg_machine
);
2262 if (!arg_image
&& !arg_directory
) {
2264 _cleanup_(image_unrefp
) Image
*i
= NULL
;
2266 r
= image_find(arg_machine
, &i
);
2268 return log_error_errno(r
, "Failed to find image for machine '%s': %m", arg_machine
);
2270 log_error("No image for machine '%s'.", arg_machine
);
2274 if (IN_SET(i
->type
, IMAGE_RAW
, IMAGE_BLOCK
))
2275 r
= free_and_strdup(&arg_image
, i
->path
);
2277 r
= free_and_strdup(&arg_directory
, i
->path
);
2282 arg_read_only
= arg_read_only
|| i
->read_only
;
2284 r
= safe_getcwd(&arg_directory
);
2286 return log_error_errno(r
, "Failed to determine current directory: %m");
2289 if (!arg_directory
&& !arg_image
) {
2290 log_error("Failed to determine path, please use -D or -i.");
2297 if (arg_directory
&& path_equal(arg_directory
, "/"))
2298 arg_machine
= gethostname_malloc();
2303 arg_machine
= strdup(basename(arg_image
));
2305 /* Truncate suffix if there is one */
2306 e
= endswith(arg_machine
, ".raw");
2310 arg_machine
= strdup(basename(arg_directory
));
2315 hostname_cleanup(arg_machine
);
2316 if (!machine_name_is_valid(arg_machine
)) {
2317 log_error("Failed to determine machine name automatically, please use -M.");
2321 if (arg_ephemeral
) {
2324 /* Add a random suffix when this is an
2325 * ephemeral machine, so that we can run many
2326 * instances at once without manually having
2327 * to specify -M each time. */
2329 if (asprintf(&b
, "%s-%016" PRIx64
, arg_machine
, random_u64()) < 0)
2340 static int chase_symlinks_and_update(char **p
, unsigned flags
) {
2349 r
= chase_symlinks(*p
, NULL
, flags
, &chased
);
2351 return log_error_errno(r
, "Failed to resolve path %s: %m", *p
);
2353 free_and_replace(*p
, chased
);
2354 return r
; /* r might be an fd here in case we ever use CHASE_OPEN in flags */
2357 static int determine_uid_shift(const char *directory
) {
2360 if (arg_userns_mode
== USER_NAMESPACE_NO
) {
2365 if (arg_uid_shift
== UID_INVALID
) {
2368 r
= stat(directory
, &st
);
2370 return log_error_errno(errno
, "Failed to determine UID base of %s: %m", directory
);
2372 arg_uid_shift
= st
.st_uid
& UINT32_C(0xffff0000);
2374 if (arg_uid_shift
!= (st
.st_gid
& UINT32_C(0xffff0000))) {
2375 log_error("UID and GID base of %s don't match.", directory
);
2379 arg_uid_range
= UINT32_C(0x10000);
2382 if (arg_uid_shift
> (uid_t
) -1 - arg_uid_range
) {
2383 log_error("UID base too high for UID range.");
2390 static int inner_child(
2392 const char *directory
,
2398 _cleanup_free_
char *home
= NULL
;
2401 const char *envp
[] = {
2402 "PATH=" DEFAULT_PATH_COMPAT
,
2403 NULL
, /* container */
2408 NULL
, /* container_uuid */
2409 NULL
, /* LISTEN_FDS */
2410 NULL
, /* LISTEN_PID */
2411 NULL
, /* NOTIFY_SOCKET */
2414 const char *exec_target
;
2415 _cleanup_strv_free_
char **env_use
= NULL
;
2420 assert(kmsg_socket
>= 0);
2422 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
2423 /* Tell the parent, that it now can write the UID map. */
2424 (void) barrier_place(barrier
); /* #1 */
2426 /* Wait until the parent wrote the UID map */
2427 if (!barrier_place_and_sync(barrier
)) { /* #2 */
2428 log_error("Parent died too early");
2433 r
= reset_uid_gid();
2435 return log_error_errno(r
, "Couldn't become new root: %m");
2438 arg_mount_settings
| MOUNT_IN_USERNS
,
2441 arg_selinux_apifs_context
);
2445 if (!arg_network_namespace_path
&& arg_private_network
) {
2446 r
= unshare(CLONE_NEWNET
);
2448 return log_error_errno(errno
, "Failed to unshare network namespace: %m");
2450 /* Tell the parent that it can setup network interfaces. */
2451 (void) barrier_place(barrier
); /* #3 */
2454 r
= mount_sysfs(NULL
, arg_mount_settings
);
2458 /* Wait until we are cgroup-ified, so that we
2459 * can mount the right cgroup path writable */
2460 if (!barrier_place_and_sync(barrier
)) { /* #4 */
2461 log_error("Parent died too early");
2465 if (arg_use_cgns
&& cg_ns_supported()) {
2466 r
= unshare(CLONE_NEWCGROUP
);
2468 return log_error_errno(errno
, "Failed to unshare cgroup namespace: %m");
2471 arg_unified_cgroup_hierarchy
,
2472 arg_userns_mode
!= USER_NAMESPACE_NO
,
2475 arg_selinux_apifs_context
,
2480 r
= mount_systemd_cgroup_writable("", arg_unified_cgroup_hierarchy
);
2485 r
= setup_boot_id();
2489 r
= setup_kmsg(kmsg_socket
);
2492 kmsg_socket
= safe_close(kmsg_socket
);
2497 return log_error_errno(errno
, "setsid() failed: %m");
2499 if (arg_private_network
)
2502 if (arg_expose_ports
) {
2503 r
= expose_port_send_rtnl(rtnl_socket
);
2506 rtnl_socket
= safe_close(rtnl_socket
);
2509 if (arg_oom_score_adjust_set
) {
2510 r
= set_oom_score_adjust(arg_oom_score_adjust
);
2512 return log_error_errno(r
, "Failed to adjust OOM score: %m");
2516 if (sched_setaffinity(0, CPU_ALLOC_SIZE(arg_cpuset_ncpus
), arg_cpuset
) < 0)
2517 return log_error_errno(errno
, "Failed to set CPU affinity: %m");
2519 r
= drop_capabilities();
2521 return log_error_errno(r
, "drop_capabilities() failed: %m");
2523 (void) setup_hostname();
2525 if (arg_personality
!= PERSONALITY_INVALID
) {
2526 r
= safe_personality(arg_personality
);
2528 return log_error_errno(r
, "personality() failed: %m");
2529 } else if (secondary
) {
2530 r
= safe_personality(PER_LINUX32
);
2532 return log_error_errno(r
, "personality() failed: %m");
2536 if (arg_selinux_context
)
2537 if (setexeccon(arg_selinux_context
) < 0)
2538 return log_error_errno(errno
, "setexeccon(\"%s\") failed: %m", arg_selinux_context
);
2541 r
= change_uid_gid(arg_user
, &home
);
2545 if (arg_no_new_privileges
)
2546 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0) < 0)
2547 return log_error_errno(errno
, "Failed to disable new privileges: %m");
2549 /* LXC sets container=lxc, so follow the scheme here */
2550 envp
[n_env
++] = strjoina("container=", arg_container_service_name
);
2552 envp
[n_env
] = strv_find_prefix(environ
, "TERM=");
2556 if ((asprintf((char**)(envp
+ n_env
++), "HOME=%s", home
? home
: "/root") < 0) ||
2557 (asprintf((char**)(envp
+ n_env
++), "USER=%s", arg_user
? arg_user
: "root") < 0) ||
2558 (asprintf((char**)(envp
+ n_env
++), "LOGNAME=%s", arg_user
? arg_user
: "root") < 0))
2561 assert(!sd_id128_is_null(arg_uuid
));
2563 if (asprintf((char**)(envp
+ n_env
++), "container_uuid=%s", id128_to_uuid_string(arg_uuid
, as_uuid
)) < 0)
2566 if (fdset_size(fds
) > 0) {
2567 r
= fdset_cloexec(fds
, false);
2569 return log_error_errno(r
, "Failed to unset O_CLOEXEC for file descriptors.");
2571 if ((asprintf((char **)(envp
+ n_env
++), "LISTEN_FDS=%u", fdset_size(fds
)) < 0) ||
2572 (asprintf((char **)(envp
+ n_env
++), "LISTEN_PID=1") < 0))
2575 if (asprintf((char **)(envp
+ n_env
++), "NOTIFY_SOCKET=%s", NSPAWN_NOTIFY_SOCKET_PATH
) < 0)
2578 env_use
= strv_env_merge(2, envp
, arg_setenv
);
2582 /* Let the parent know that we are ready and
2583 * wait until the parent is ready with the
2585 if (!barrier_place_and_sync(barrier
)) { /* #5 */
2586 log_error("Parent died too early");
2591 if (chdir(arg_chdir
) < 0)
2592 return log_error_errno(errno
, "Failed to change to specified working directory %s: %m", arg_chdir
);
2594 if (arg_start_mode
== START_PID2
) {
2595 r
= stub_pid1(arg_uuid
);
2600 /* Now, explicitly close the log, so that we then can close all remaining fds. Closing the log explicitly first
2601 * has the benefit that the logging subsystem knows about it, and is thus ready to be reopened should we need
2602 * it again. Note that the other fds closed here are at least the locking and barrier fds. */
2604 log_set_open_when_needed(true);
2606 (void) fdset_close_others(fds
);
2608 if (arg_start_mode
== START_BOOT
) {
2612 /* Automatically search for the init system */
2614 m
= strv_length(arg_parameters
);
2615 a
= newa(char*, m
+ 2);
2616 memcpy_safe(a
+ 1, arg_parameters
, m
* sizeof(char*));
2619 a
[0] = (char*) "/usr/lib/systemd/systemd";
2620 execve(a
[0], a
, env_use
);
2622 a
[0] = (char*) "/lib/systemd/systemd";
2623 execve(a
[0], a
, env_use
);
2625 a
[0] = (char*) "/sbin/init";
2626 execve(a
[0], a
, env_use
);
2628 exec_target
= "/usr/lib/systemd/systemd, /lib/systemd/systemd, /sbin/init";
2629 } else if (!strv_isempty(arg_parameters
)) {
2630 exec_target
= arg_parameters
[0];
2631 execvpe(arg_parameters
[0], arg_parameters
, env_use
);
2634 /* If we cannot change the directory, we'll end up in /, that is expected. */
2635 (void) chdir(home
?: "/root");
2637 execle("/bin/bash", "-bash", NULL
, env_use
);
2638 execle("/bin/sh", "-sh", NULL
, env_use
);
2640 exec_target
= "/bin/bash, /bin/sh";
2643 return log_error_errno(errno
, "execv(%s) failed: %m", exec_target
);
2646 static int setup_sd_notify_child(void) {
2647 static const int one
= 1;
2649 union sockaddr_union sa
= {
2650 .sa
.sa_family
= AF_UNIX
,
2654 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
2656 return log_error_errno(errno
, "Failed to allocate notification socket: %m");
2658 (void) mkdir_parents(NSPAWN_NOTIFY_SOCKET_PATH
, 0755);
2659 (void) unlink(NSPAWN_NOTIFY_SOCKET_PATH
);
2661 strncpy(sa
.un
.sun_path
, NSPAWN_NOTIFY_SOCKET_PATH
, sizeof(sa
.un
.sun_path
)-1);
2662 r
= bind(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
2665 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
2668 r
= userns_lchown(NSPAWN_NOTIFY_SOCKET_PATH
, 0, 0);
2671 return log_error_errno(r
, "Failed to chown " NSPAWN_NOTIFY_SOCKET_PATH
": %m");
2674 r
= setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
2677 return log_error_errno(errno
, "SO_PASSCRED failed: %m");
2683 static int outer_child(
2685 const char *directory
,
2686 const char *console
,
2687 DissectedImage
*dissected_image
,
2695 int uid_shift_socket
,
2696 int unified_cgroup_hierarchy_socket
,
2700 _cleanup_close_
int fd
= -1;
2701 int r
, which_failed
;
2708 assert(pid_socket
>= 0);
2709 assert(uuid_socket
>= 0);
2710 assert(notify_socket
>= 0);
2711 assert(kmsg_socket
>= 0);
2713 if (prctl(PR_SET_PDEATHSIG
, SIGKILL
) < 0)
2714 return log_error_errno(errno
, "PR_SET_PDEATHSIG failed: %m");
2719 terminal
= open_terminal(console
, O_RDWR
);
2721 return log_error_errno(terminal
, "Failed to open console: %m");
2723 /* Make sure we can continue logging to the original stderr, even if stderr points elsewhere now */
2724 r
= log_dup_console();
2726 return log_error_errno(r
, "Failed to duplicate stderr: %m");
2728 r
= rearrange_stdio(terminal
, terminal
, terminal
); /* invalidates 'terminal' on success and failure */
2730 return log_error_errno(r
, "Failed to move console to stdin/stdout/stderr: %m");
2733 r
= reset_audit_loginuid();
2737 /* Mark everything as slave, so that we still
2738 * receive mounts from the real root, but don't
2739 * propagate mounts to the real root. */
2740 r
= mount_verbose(LOG_ERR
, NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
);
2744 if (dissected_image
) {
2745 /* If we are operating on a disk image, then mount its root directory now, but leave out the rest. We
2746 * can read the UID shift from it if we need to. Further down we'll mount the rest, but then with the
2747 * uid shift known. That way we can mount VFAT file systems shifted to the right place right away. This
2748 * makes sure ESP partitions and userns are compatible. */
2750 r
= dissected_image_mount(dissected_image
, directory
, arg_uid_shift
,
2751 DISSECT_IMAGE_MOUNT_ROOT_ONLY
|DISSECT_IMAGE_DISCARD_ON_LOOP
|(arg_read_only
? DISSECT_IMAGE_READ_ONLY
: 0));
2756 r
= determine_uid_shift(directory
);
2760 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
2761 /* Let the parent know which UID shift we read from the image */
2762 l
= send(uid_shift_socket
, &arg_uid_shift
, sizeof(arg_uid_shift
), MSG_NOSIGNAL
);
2764 return log_error_errno(errno
, "Failed to send UID shift: %m");
2765 if (l
!= sizeof(arg_uid_shift
)) {
2766 log_error("Short write while sending UID shift.");
2770 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
2771 /* When we are supposed to pick the UID shift, the parent will check now whether the UID shift
2772 * we just read from the image is available. If yes, it will send the UID shift back to us, if
2773 * not it will pick a different one, and send it back to us. */
2775 l
= recv(uid_shift_socket
, &arg_uid_shift
, sizeof(arg_uid_shift
), 0);
2777 return log_error_errno(errno
, "Failed to recv UID shift: %m");
2778 if (l
!= sizeof(arg_uid_shift
)) {
2779 log_error("Short read while receiving UID shift.");
2784 log_info("Selected user namespace base " UID_FMT
" and range " UID_FMT
".", arg_uid_shift
, arg_uid_range
);
2787 if (dissected_image
) {
2788 /* Now we know the uid shift, let's now mount everything else that might be in the image. */
2789 r
= dissected_image_mount(dissected_image
, directory
, arg_uid_shift
,
2790 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY
|DISSECT_IMAGE_DISCARD_ON_LOOP
|(arg_read_only
? DISSECT_IMAGE_READ_ONLY
: 0));
2795 if (arg_unified_cgroup_hierarchy
== CGROUP_UNIFIED_UNKNOWN
) {
2796 /* OK, we don't know yet which cgroup mode to use yet. Let's figure it out, and tell the parent. */
2798 r
= detect_unified_cgroup_hierarchy_from_image(directory
);
2802 l
= send(unified_cgroup_hierarchy_socket
, &arg_unified_cgroup_hierarchy
, sizeof(arg_unified_cgroup_hierarchy
), MSG_NOSIGNAL
);
2804 return log_error_errno(errno
, "Failed to send cgroup mode: %m");
2805 if (l
!= sizeof(arg_unified_cgroup_hierarchy
)) {
2806 log_error("Short write while sending cgroup mode: %m");
2810 unified_cgroup_hierarchy_socket
= safe_close(unified_cgroup_hierarchy_socket
);
2813 /* Turn directory into bind mount */
2814 r
= mount_verbose(LOG_ERR
, directory
, directory
, NULL
, MS_BIND
|MS_REC
, NULL
);
2818 r
= setup_pivot_root(
2821 arg_pivot_root_old
);
2828 arg_userns_mode
!= USER_NAMESPACE_NO
,
2831 arg_selinux_context
);
2835 r
= setup_volatile_state(
2838 arg_userns_mode
!= USER_NAMESPACE_NO
,
2841 arg_selinux_context
);
2845 /* Mark everything as shared so our mounts get propagated down. This is
2846 * required to make new bind mounts available in systemd services
2847 * inside the containter that create a new mount namespace.
2848 * See https://github.com/systemd/systemd/issues/3860
2849 * Further submounts (such as /dev) done after this will inherit the
2850 * shared propagation mode. */
2851 r
= mount_verbose(LOG_ERR
, NULL
, directory
, NULL
, MS_SHARED
|MS_REC
, NULL
);
2855 r
= recursive_chown(directory
, arg_uid_shift
, arg_uid_range
);
2859 r
= base_filesystem_create(directory
, arg_uid_shift
, (gid_t
) arg_uid_shift
);
2863 if (arg_read_only
) {
2864 r
= bind_remount_recursive(directory
, true, NULL
);
2866 return log_error_errno(r
, "Failed to make tree read-only: %m");
2869 r
= mount_all(directory
,
2873 arg_selinux_apifs_context
);
2877 r
= copy_devnodes(directory
);
2881 dev_setup(directory
, arg_uid_shift
, arg_uid_shift
);
2883 r
= setup_pts(directory
);
2887 r
= setup_propagate(directory
);
2891 r
= setup_dev_console(directory
, console
);
2895 r
= setup_keyring();
2899 r
= setup_seccomp(arg_caps_retain
, arg_syscall_whitelist
, arg_syscall_blacklist
);
2903 r
= setup_timezone(directory
);
2907 r
= setup_resolv_conf(directory
);
2911 r
= setup_machine_id(directory
);
2915 r
= setup_journal(directory
);
2922 arg_n_custom_mounts
,
2923 arg_userns_mode
!= USER_NAMESPACE_NO
,
2926 arg_selinux_apifs_context
);
2930 if (!arg_use_cgns
|| !cg_ns_supported()) {
2933 arg_unified_cgroup_hierarchy
,
2934 arg_userns_mode
!= USER_NAMESPACE_NO
,
2937 arg_selinux_apifs_context
,
2943 r
= mount_move_root(directory
);
2945 return log_error_errno(r
, "Failed to move root directory: %m");
2947 fd
= setup_sd_notify_child();
2951 r
= setrlimit_closest_all((const struct rlimit
*const*) arg_rlimit
, &which_failed
);
2953 return log_error_errno(r
, "Failed to apply resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed
));
2955 pid
= raw_clone(SIGCHLD
|CLONE_NEWNS
|
2956 arg_clone_ns_flags
|
2957 (arg_userns_mode
!= USER_NAMESPACE_NO
? CLONE_NEWUSER
: 0));
2959 return log_error_errno(errno
, "Failed to fork inner child: %m");
2961 pid_socket
= safe_close(pid_socket
);
2962 uuid_socket
= safe_close(uuid_socket
);
2963 notify_socket
= safe_close(notify_socket
);
2964 uid_shift_socket
= safe_close(uid_shift_socket
);
2966 /* The inner child has all namespaces that are
2967 * requested, so that we all are owned by the user if
2968 * user namespaces are turned on. */
2970 if (arg_network_namespace_path
) {
2971 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
2976 r
= inner_child(barrier
, directory
, secondary
, kmsg_socket
, rtnl_socket
, fds
);
2978 _exit(EXIT_FAILURE
);
2980 _exit(EXIT_SUCCESS
);
2983 l
= send(pid_socket
, &pid
, sizeof(pid
), MSG_NOSIGNAL
);
2985 return log_error_errno(errno
, "Failed to send PID: %m");
2986 if (l
!= sizeof(pid
)) {
2987 log_error("Short write while sending PID.");
2991 l
= send(uuid_socket
, &arg_uuid
, sizeof(arg_uuid
), MSG_NOSIGNAL
);
2993 return log_error_errno(errno
, "Failed to send machine ID: %m");
2994 if (l
!= sizeof(arg_uuid
)) {
2995 log_error("Short write while sending machine ID.");
2999 l
= send_one_fd(notify_socket
, fd
, 0);
3001 return log_error_errno(errno
, "Failed to send notify fd: %m");
3003 pid_socket
= safe_close(pid_socket
);
3004 uuid_socket
= safe_close(uuid_socket
);
3005 notify_socket
= safe_close(notify_socket
);
3006 kmsg_socket
= safe_close(kmsg_socket
);
3007 rtnl_socket
= safe_close(rtnl_socket
);
3008 netns_fd
= safe_close(netns_fd
);
3013 static int uid_shift_pick(uid_t
*shift
, LockFile
*ret_lock_file
) {
3014 bool tried_hashed
= false;
3015 unsigned n_tries
= 100;
3020 assert(ret_lock_file
);
3021 assert(arg_userns_mode
== USER_NAMESPACE_PICK
);
3022 assert(arg_uid_range
== 0x10000U
);
3026 (void) mkdir("/run/systemd/nspawn-uid", 0755);
3029 char lock_path
[STRLEN("/run/systemd/nspawn-uid/") + DECIMAL_STR_MAX(uid_t
) + 1];
3030 _cleanup_(release_lock_file
) LockFile lf
= LOCK_FILE_INIT
;
3035 if (candidate
< CONTAINER_UID_BASE_MIN
|| candidate
> CONTAINER_UID_BASE_MAX
)
3037 if ((candidate
& UINT32_C(0xFFFF)) != 0)
3040 xsprintf(lock_path
, "/run/systemd/nspawn-uid/" UID_FMT
, candidate
);
3041 r
= make_lock_file(lock_path
, LOCK_EX
|LOCK_NB
, &lf
);
3042 if (r
== -EBUSY
) /* Range already taken by another nspawn instance */
3047 /* Make some superficial checks whether the range is currently known in the user database */
3048 if (getpwuid(candidate
))
3050 if (getpwuid(candidate
+ UINT32_C(0xFFFE)))
3052 if (getgrgid(candidate
))
3054 if (getgrgid(candidate
+ UINT32_C(0xFFFE)))
3057 *ret_lock_file
= lf
;
3058 lf
= (struct LockFile
) LOCK_FILE_INIT
;
3063 if (arg_machine
&& !tried_hashed
) {
3064 /* Try to hash the base from the container name */
3066 static const uint8_t hash_key
[] = {
3067 0xe1, 0x56, 0xe0, 0xf0, 0x4a, 0xf0, 0x41, 0xaf,
3068 0x96, 0x41, 0xcf, 0x41, 0x33, 0x94, 0xff, 0x72
3071 candidate
= (uid_t
) siphash24(arg_machine
, strlen(arg_machine
), hash_key
);
3073 tried_hashed
= true;
3075 random_bytes(&candidate
, sizeof(candidate
));
3077 candidate
= (candidate
% (CONTAINER_UID_BASE_MAX
- CONTAINER_UID_BASE_MIN
)) + CONTAINER_UID_BASE_MIN
;
3078 candidate
&= (uid_t
) UINT32_C(0xFFFF0000);
3082 static int setup_uid_map(pid_t pid
) {
3083 char uid_map
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(uid_t
) + 1], line
[DECIMAL_STR_MAX(uid_t
)*3+3+1];
3088 xsprintf(uid_map
, "/proc/" PID_FMT
"/uid_map", pid
);
3089 xsprintf(line
, UID_FMT
" " UID_FMT
" " UID_FMT
"\n", 0, arg_uid_shift
, arg_uid_range
);
3090 r
= write_string_file(uid_map
, line
, 0);
3092 return log_error_errno(r
, "Failed to write UID map: %m");
3094 /* We always assign the same UID and GID ranges */
3095 xsprintf(uid_map
, "/proc/" PID_FMT
"/gid_map", pid
);
3096 r
= write_string_file(uid_map
, line
, 0);
3098 return log_error_errno(r
, "Failed to write GID map: %m");
3103 static int nspawn_dispatch_notify_fd(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
3104 char buf
[NOTIFY_BUFFER_MAX
+1];
3106 struct iovec iovec
= {
3108 .iov_len
= sizeof(buf
)-1,
3111 struct cmsghdr cmsghdr
;
3112 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
)) +
3113 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX
)];
3115 struct msghdr msghdr
= {
3118 .msg_control
= &control
,
3119 .msg_controllen
= sizeof(control
),
3121 struct cmsghdr
*cmsg
;
3122 struct ucred
*ucred
= NULL
;
3124 pid_t inner_child_pid
;
3125 _cleanup_strv_free_
char **tags
= NULL
;
3129 inner_child_pid
= PTR_TO_PID(userdata
);
3131 if (revents
!= EPOLLIN
) {
3132 log_warning("Got unexpected poll event for notify fd.");
3136 n
= recvmsg(fd
, &msghdr
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
3138 if (IN_SET(errno
, EAGAIN
, EINTR
))
3141 return log_warning_errno(errno
, "Couldn't read notification socket: %m");
3143 cmsg_close_all(&msghdr
);
3145 CMSG_FOREACH(cmsg
, &msghdr
) {
3146 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
3147 cmsg
->cmsg_type
== SCM_CREDENTIALS
&&
3148 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
))) {
3150 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
3154 if (!ucred
|| ucred
->pid
!= inner_child_pid
) {
3155 log_debug("Received notify message without valid credentials. Ignoring.");
3159 if ((size_t) n
>= sizeof(buf
)) {
3160 log_warning("Received notify message exceeded maximum size. Ignoring.");
3165 tags
= strv_split(buf
, "\n\r");
3169 if (strv_find(tags
, "READY=1"))
3170 sd_notifyf(false, "READY=1\n");
3172 p
= strv_find_startswith(tags
, "STATUS=");
3174 sd_notifyf(false, "STATUS=Container running: %s", p
);
3179 static int setup_sd_notify_parent(sd_event
*event
, int fd
, pid_t
*inner_child_pid
, sd_event_source
**notify_event_source
) {
3182 r
= sd_event_add_io(event
, notify_event_source
, fd
, EPOLLIN
, nspawn_dispatch_notify_fd
, inner_child_pid
);
3184 return log_error_errno(r
, "Failed to allocate notify event source: %m");
3186 (void) sd_event_source_set_description(*notify_event_source
, "nspawn-notify");
3191 static int merge_settings(Settings
*settings
, const char *path
) {
3197 /* Copy over bits from the settings, unless they have been explicitly masked by command line switches. Note
3198 * that this steals the fields of the Settings* structure, and hence modifies it. */
3200 if ((arg_settings_mask
& SETTING_START_MODE
) == 0 &&
3201 settings
->start_mode
>= 0) {
3202 arg_start_mode
= settings
->start_mode
;
3203 strv_free_and_replace(arg_parameters
, settings
->parameters
);
3206 if ((arg_settings_mask
& SETTING_PIVOT_ROOT
) == 0 &&
3207 settings
->pivot_root_new
) {
3208 free_and_replace(arg_pivot_root_new
, settings
->pivot_root_new
);
3209 free_and_replace(arg_pivot_root_old
, settings
->pivot_root_old
);
3212 if ((arg_settings_mask
& SETTING_WORKING_DIRECTORY
) == 0 &&
3213 settings
->working_directory
)
3214 free_and_replace(arg_chdir
, settings
->working_directory
);
3216 if ((arg_settings_mask
& SETTING_ENVIRONMENT
) == 0 &&
3217 settings
->environment
)
3218 strv_free_and_replace(arg_setenv
, settings
->environment
);
3220 if ((arg_settings_mask
& SETTING_USER
) == 0 &&
3222 free_and_replace(arg_user
, settings
->user
);
3224 if ((arg_settings_mask
& SETTING_CAPABILITY
) == 0) {
3227 plus
= settings
->capability
;
3228 if (settings_private_network(settings
))
3229 plus
|= (1ULL << CAP_NET_ADMIN
);
3231 if (!arg_settings_trusted
&& plus
!= 0) {
3232 if (settings
->capability
!= 0)
3233 log_warning("Ignoring Capability= setting, file %s is not trusted.", path
);
3235 arg_caps_retain
|= plus
;
3237 arg_caps_retain
&= ~settings
->drop_capability
;
3240 if ((arg_settings_mask
& SETTING_KILL_SIGNAL
) == 0 &&
3241 settings
->kill_signal
> 0)
3242 arg_kill_signal
= settings
->kill_signal
;
3244 if ((arg_settings_mask
& SETTING_PERSONALITY
) == 0 &&
3245 settings
->personality
!= PERSONALITY_INVALID
)
3246 arg_personality
= settings
->personality
;
3248 if ((arg_settings_mask
& SETTING_MACHINE_ID
) == 0 &&
3249 !sd_id128_is_null(settings
->machine_id
)) {
3251 if (!arg_settings_trusted
)
3252 log_warning("Ignoring MachineID= setting, file %s is not trusted.", path
);
3254 arg_uuid
= settings
->machine_id
;
3257 if ((arg_settings_mask
& SETTING_READ_ONLY
) == 0 &&
3258 settings
->read_only
>= 0)
3259 arg_read_only
= settings
->read_only
;
3261 if ((arg_settings_mask
& SETTING_VOLATILE_MODE
) == 0 &&
3262 settings
->volatile_mode
!= _VOLATILE_MODE_INVALID
)
3263 arg_volatile_mode
= settings
->volatile_mode
;
3265 if ((arg_settings_mask
& SETTING_CUSTOM_MOUNTS
) == 0 &&
3266 settings
->n_custom_mounts
> 0) {
3268 if (!arg_settings_trusted
)
3269 log_warning("Ignoring TemporaryFileSystem=, Bind= and BindReadOnly= settings, file %s is not trusted.", path
);
3271 custom_mount_free_all(arg_custom_mounts
, arg_n_custom_mounts
);
3272 arg_custom_mounts
= TAKE_PTR(settings
->custom_mounts
);
3273 arg_n_custom_mounts
= settings
->n_custom_mounts
;
3274 settings
->n_custom_mounts
= 0;
3278 if ((arg_settings_mask
& SETTING_NETWORK
) == 0 &&
3279 (settings
->private_network
>= 0 ||
3280 settings
->network_veth
>= 0 ||
3281 settings
->network_bridge
||
3282 settings
->network_zone
||
3283 settings
->network_interfaces
||
3284 settings
->network_macvlan
||
3285 settings
->network_ipvlan
||
3286 settings
->network_veth_extra
)) {
3288 if (!arg_settings_trusted
)
3289 log_warning("Ignoring network settings, file %s is not trusted.", path
);
3291 arg_network_veth
= settings_network_veth(settings
);
3292 arg_private_network
= settings_private_network(settings
);
3294 strv_free_and_replace(arg_network_interfaces
, settings
->network_interfaces
);
3295 strv_free_and_replace(arg_network_macvlan
, settings
->network_macvlan
);
3296 strv_free_and_replace(arg_network_ipvlan
, settings
->network_ipvlan
);
3297 strv_free_and_replace(arg_network_veth_extra
, settings
->network_veth_extra
);
3299 free_and_replace(arg_network_bridge
, settings
->network_bridge
);
3300 free_and_replace(arg_network_zone
, settings
->network_zone
);
3304 if ((arg_settings_mask
& SETTING_EXPOSE_PORTS
) == 0 &&
3305 settings
->expose_ports
) {
3307 if (!arg_settings_trusted
)
3308 log_warning("Ignoring Port= setting, file %s is not trusted.", path
);
3310 expose_port_free_all(arg_expose_ports
);
3311 arg_expose_ports
= TAKE_PTR(settings
->expose_ports
);
3315 if ((arg_settings_mask
& SETTING_USERNS
) == 0 &&
3316 settings
->userns_mode
!= _USER_NAMESPACE_MODE_INVALID
) {
3318 if (!arg_settings_trusted
)
3319 log_warning("Ignoring PrivateUsers= and PrivateUsersChown= settings, file %s is not trusted.", path
);
3321 arg_userns_mode
= settings
->userns_mode
;
3322 arg_uid_shift
= settings
->uid_shift
;
3323 arg_uid_range
= settings
->uid_range
;
3324 arg_userns_chown
= settings
->userns_chown
;
3328 if ((arg_settings_mask
& SETTING_NOTIFY_READY
) == 0)
3329 arg_notify_ready
= settings
->notify_ready
;
3331 if ((arg_settings_mask
& SETTING_SYSCALL_FILTER
) == 0) {
3333 if (!arg_settings_trusted
&& !strv_isempty(arg_syscall_whitelist
))
3334 log_warning("Ignoring SystemCallFilter= settings, file %s is not trusted.", path
);
3336 strv_free_and_replace(arg_syscall_whitelist
, settings
->syscall_whitelist
);
3337 strv_free_and_replace(arg_syscall_blacklist
, settings
->syscall_blacklist
);
3341 for (rl
= 0; rl
< _RLIMIT_MAX
; rl
++) {
3342 if ((arg_settings_mask
& (SETTING_RLIMIT_FIRST
<< rl
)))
3345 if (!settings
->rlimit
[rl
])
3348 if (!arg_settings_trusted
) {
3349 log_warning("Ignoring Limit%s= setting, file '%s' is not trusted.", rlimit_to_string(rl
), path
);
3353 free_and_replace(arg_rlimit
[rl
], settings
->rlimit
[rl
]);
3356 if ((arg_settings_mask
& SETTING_HOSTNAME
) == 0 &&
3358 free_and_replace(arg_hostname
, settings
->hostname
);
3360 if ((arg_settings_mask
& SETTING_NO_NEW_PRIVILEGES
) == 0 &&
3361 settings
->no_new_privileges
>= 0)
3362 arg_no_new_privileges
= settings
->no_new_privileges
;
3364 if ((arg_settings_mask
& SETTING_OOM_SCORE_ADJUST
) == 0 &&
3365 settings
->oom_score_adjust_set
) {
3367 if (!arg_settings_trusted
)
3368 log_warning("Ignoring OOMScoreAdjust= setting, file '%s' is not trusted.", path
);
3370 arg_oom_score_adjust
= settings
->oom_score_adjust
;
3371 arg_oom_score_adjust_set
= true;
3375 if ((arg_settings_mask
& SETTING_CPU_AFFINITY
) == 0 &&
3378 if (!arg_settings_trusted
)
3379 log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path
);
3382 CPU_FREE(arg_cpuset
);
3383 arg_cpuset
= TAKE_PTR(settings
->cpuset
);
3384 arg_cpuset_ncpus
= settings
->cpuset_ncpus
;
3391 static int load_settings(void) {
3392 _cleanup_(settings_freep
) Settings
*settings
= NULL
;
3393 _cleanup_fclose_
FILE *f
= NULL
;
3394 _cleanup_free_
char *p
= NULL
;
3398 /* If all settings are masked, there's no point in looking for
3399 * the settings file */
3400 if ((arg_settings_mask
& _SETTINGS_MASK_ALL
) == _SETTINGS_MASK_ALL
)
3403 fn
= strjoina(arg_machine
, ".nspawn");
3405 /* We first look in the admin's directories in /etc and /run */
3406 FOREACH_STRING(i
, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
3407 _cleanup_free_
char *j
= NULL
;
3409 j
= strjoin(i
, "/", fn
);
3417 /* By default, we trust configuration from /etc and /run */
3418 if (arg_settings_trusted
< 0)
3419 arg_settings_trusted
= true;
3424 if (errno
!= ENOENT
)
3425 return log_error_errno(errno
, "Failed to open %s: %m", j
);
3429 /* After that, let's look for a file next to the
3430 * actual image we shall boot. */
3433 p
= file_in_same_dir(arg_image
, fn
);
3436 } else if (arg_directory
) {
3437 p
= file_in_same_dir(arg_directory
, fn
);
3444 if (!f
&& errno
!= ENOENT
)
3445 return log_error_errno(errno
, "Failed to open %s: %m", p
);
3447 /* By default, we do not trust configuration from /var/lib/machines */
3448 if (arg_settings_trusted
< 0)
3449 arg_settings_trusted
= false;
3456 log_debug("Settings are trusted: %s", yes_no(arg_settings_trusted
));
3458 r
= settings_load(f
, p
, &settings
);
3462 return merge_settings(settings
, p
);
3465 static int run(int master
,
3466 const char* console
,
3467 DissectedImage
*dissected_image
,
3471 char veth_name
[IFNAMSIZ
], bool *veth_created
,
3472 union in_addr_union
*exposed
,
3473 pid_t
*pid
, int *ret
) {
3475 static const struct sigaction sa
= {
3476 .sa_handler
= nop_signal_handler
,
3477 .sa_flags
= SA_NOCLDSTOP
|SA_RESTART
,
3480 _cleanup_(release_lock_file
) LockFile uid_shift_lock
= LOCK_FILE_INIT
;
3481 _cleanup_close_
int etc_passwd_lock
= -1;
3482 _cleanup_close_pair_
int
3483 kmsg_socket_pair
[2] = { -1, -1 },
3484 rtnl_socket_pair
[2] = { -1, -1 },
3485 pid_socket_pair
[2] = { -1, -1 },
3486 uuid_socket_pair
[2] = { -1, -1 },
3487 notify_socket_pair
[2] = { -1, -1 },
3488 uid_shift_socket_pair
[2] = { -1, -1 },
3489 unified_cgroup_hierarchy_socket_pair
[2] = { -1, -1};
3491 _cleanup_close_
int notify_socket
= -1;
3492 _cleanup_(barrier_destroy
) Barrier barrier
= BARRIER_NULL
;
3493 _cleanup_(sd_event_source_unrefp
) sd_event_source
*notify_event_source
= NULL
;
3494 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
3495 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
3496 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
3497 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
3498 ContainerStatus container_status
= 0;
3503 _cleanup_close_
int netns_fd
= -1;
3505 assert_se(sigemptyset(&mask_chld
) == 0);
3506 assert_se(sigaddset(&mask_chld
, SIGCHLD
) == 0);
3508 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
3509 /* When we shall pick the UID/GID range, let's first lock /etc/passwd, so that we can safely
3510 * check with getpwuid() if the specific user already exists. Note that /etc might be
3511 * read-only, in which case this will fail with EROFS. But that's really OK, as in that case we
3512 * can be reasonably sure that no users are going to be added. Note that getpwuid() checks are
3513 * really just an extra safety net. We kinda assume that the UID range we allocate from is
3516 etc_passwd_lock
= take_etc_passwd_lock(NULL
);
3517 if (etc_passwd_lock
< 0 && etc_passwd_lock
!= -EROFS
)
3518 return log_error_errno(etc_passwd_lock
, "Failed to take /etc/passwd lock: %m");
3521 r
= barrier_create(&barrier
);
3523 return log_error_errno(r
, "Cannot initialize IPC barrier: %m");
3525 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, kmsg_socket_pair
) < 0)
3526 return log_error_errno(errno
, "Failed to create kmsg socket pair: %m");
3528 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, rtnl_socket_pair
) < 0)
3529 return log_error_errno(errno
, "Failed to create rtnl socket pair: %m");
3531 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pid_socket_pair
) < 0)
3532 return log_error_errno(errno
, "Failed to create pid socket pair: %m");
3534 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, uuid_socket_pair
) < 0)
3535 return log_error_errno(errno
, "Failed to create id socket pair: %m");
3537 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, notify_socket_pair
) < 0)
3538 return log_error_errno(errno
, "Failed to create notify socket pair: %m");
3540 if (arg_userns_mode
!= USER_NAMESPACE_NO
)
3541 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, uid_shift_socket_pair
) < 0)
3542 return log_error_errno(errno
, "Failed to create uid shift socket pair: %m");
3544 if (arg_unified_cgroup_hierarchy
== CGROUP_UNIFIED_UNKNOWN
)
3545 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, unified_cgroup_hierarchy_socket_pair
) < 0)
3546 return log_error_errno(errno
, "Failed to create unified cgroup socket pair: %m");
3548 /* Child can be killed before execv(), so handle SIGCHLD in order to interrupt
3549 * parent's blocking calls and give it a chance to call wait() and terminate. */
3550 r
= sigprocmask(SIG_UNBLOCK
, &mask_chld
, NULL
);
3552 return log_error_errno(errno
, "Failed to change the signal mask: %m");
3554 r
= sigaction(SIGCHLD
, &sa
, NULL
);
3556 return log_error_errno(errno
, "Failed to install SIGCHLD handler: %m");
3558 if (arg_network_namespace_path
) {
3559 netns_fd
= open(arg_network_namespace_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
3561 return log_error_errno(errno
, "Cannot open file %s: %m", arg_network_namespace_path
);
3563 r
= fd_is_network_ns(netns_fd
);
3564 if (r
< 0 && r
!= -ENOTTY
)
3565 return log_error_errno(r
, "Failed to check %s fs type: %m", arg_network_namespace_path
);
3567 log_error("Path %s doesn't refer to a network namespace", arg_network_namespace_path
);
3572 *pid
= raw_clone(SIGCHLD
|CLONE_NEWNS
);
3574 return log_error_errno(errno
, "clone() failed%s: %m",
3576 ", do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in)" : "");
3579 /* The outer child only has a file system namespace. */
3580 barrier_set_role(&barrier
, BARRIER_CHILD
);
3582 master
= safe_close(master
);
3584 kmsg_socket_pair
[0] = safe_close(kmsg_socket_pair
[0]);
3585 rtnl_socket_pair
[0] = safe_close(rtnl_socket_pair
[0]);
3586 pid_socket_pair
[0] = safe_close(pid_socket_pair
[0]);
3587 uuid_socket_pair
[0] = safe_close(uuid_socket_pair
[0]);
3588 notify_socket_pair
[0] = safe_close(notify_socket_pair
[0]);
3589 uid_shift_socket_pair
[0] = safe_close(uid_shift_socket_pair
[0]);
3590 unified_cgroup_hierarchy_socket_pair
[0] = safe_close(unified_cgroup_hierarchy_socket_pair
[0]);
3592 (void) reset_all_signal_handlers();
3593 (void) reset_signal_mask();
3595 r
= outer_child(&barrier
,
3602 uuid_socket_pair
[1],
3603 notify_socket_pair
[1],
3604 kmsg_socket_pair
[1],
3605 rtnl_socket_pair
[1],
3606 uid_shift_socket_pair
[1],
3607 unified_cgroup_hierarchy_socket_pair
[1],
3611 _exit(EXIT_FAILURE
);
3613 _exit(EXIT_SUCCESS
);
3616 barrier_set_role(&barrier
, BARRIER_PARENT
);
3618 fds
= fdset_free(fds
);
3620 kmsg_socket_pair
[1] = safe_close(kmsg_socket_pair
[1]);
3621 rtnl_socket_pair
[1] = safe_close(rtnl_socket_pair
[1]);
3622 pid_socket_pair
[1] = safe_close(pid_socket_pair
[1]);
3623 uuid_socket_pair
[1] = safe_close(uuid_socket_pair
[1]);
3624 notify_socket_pair
[1] = safe_close(notify_socket_pair
[1]);
3625 uid_shift_socket_pair
[1] = safe_close(uid_shift_socket_pair
[1]);
3626 unified_cgroup_hierarchy_socket_pair
[1] = safe_close(unified_cgroup_hierarchy_socket_pair
[1]);
3628 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
3629 /* The child just let us know the UID shift it might have read from the image. */
3630 l
= recv(uid_shift_socket_pair
[0], &arg_uid_shift
, sizeof arg_uid_shift
, 0);
3632 return log_error_errno(errno
, "Failed to read UID shift: %m");
3633 if (l
!= sizeof arg_uid_shift
) {
3634 log_error("Short read while reading UID shift.");
3638 if (arg_userns_mode
== USER_NAMESPACE_PICK
) {
3639 /* If we are supposed to pick the UID shift, let's try to use the shift read from the
3640 * image, but if that's already in use, pick a new one, and report back to the child,
3641 * which one we now picked. */
3643 r
= uid_shift_pick(&arg_uid_shift
, &uid_shift_lock
);
3645 return log_error_errno(r
, "Failed to pick suitable UID/GID range: %m");
3647 l
= send(uid_shift_socket_pair
[0], &arg_uid_shift
, sizeof arg_uid_shift
, MSG_NOSIGNAL
);
3649 return log_error_errno(errno
, "Failed to send UID shift: %m");
3650 if (l
!= sizeof arg_uid_shift
) {
3651 log_error("Short write while writing UID shift.");
3657 if (arg_unified_cgroup_hierarchy
== CGROUP_UNIFIED_UNKNOWN
) {
3658 /* The child let us know the support cgroup mode it might have read from the image. */
3659 l
= recv(unified_cgroup_hierarchy_socket_pair
[0], &arg_unified_cgroup_hierarchy
, sizeof(arg_unified_cgroup_hierarchy
), 0);
3661 return log_error_errno(errno
, "Failed to read cgroup mode: %m");
3662 if (l
!= sizeof(arg_unified_cgroup_hierarchy
)) {
3663 log_error("Short read while reading cgroup mode.");
3668 /* Wait for the outer child. */
3669 r
= wait_for_terminate_and_check("(sd-namespace)", *pid
, WAIT_LOG_ABNORMAL
);
3672 if (r
!= EXIT_SUCCESS
)
3675 /* And now retrieve the PID of the inner child. */
3676 l
= recv(pid_socket_pair
[0], pid
, sizeof *pid
, 0);
3678 return log_error_errno(errno
, "Failed to read inner child PID: %m");
3679 if (l
!= sizeof *pid
) {
3680 log_error("Short read while reading inner child PID.");
3684 /* We also retrieve container UUID in case it was generated by outer child */
3685 l
= recv(uuid_socket_pair
[0], &arg_uuid
, sizeof arg_uuid
, 0);
3687 return log_error_errno(errno
, "Failed to read container machine ID: %m");
3688 if (l
!= sizeof(arg_uuid
)) {
3689 log_error("Short read while reading container machined ID.");
3693 /* We also retrieve the socket used for notifications generated by outer child */
3694 notify_socket
= receive_one_fd(notify_socket_pair
[0], 0);
3695 if (notify_socket
< 0)
3696 return log_error_errno(notify_socket
,
3697 "Failed to receive notification socket from the outer child: %m");
3699 log_debug("Init process invoked as PID "PID_FMT
, *pid
);
3701 if (arg_userns_mode
!= USER_NAMESPACE_NO
) {
3702 if (!barrier_place_and_sync(&barrier
)) { /* #1 */
3703 log_error("Child died too early.");
3707 r
= setup_uid_map(*pid
);
3711 (void) barrier_place(&barrier
); /* #2 */
3714 if (arg_private_network
) {
3716 if (!arg_network_namespace_path
) {
3717 /* Wait until the child has unshared its network namespace. */
3718 if (!barrier_place_and_sync(&barrier
)) { /* #3 */
3719 log_error("Child died too early");
3724 r
= move_network_interfaces(*pid
, arg_network_interfaces
);
3728 if (arg_network_veth
) {
3729 r
= setup_veth(arg_machine
, *pid
, veth_name
,
3730 arg_network_bridge
|| arg_network_zone
);
3736 if (arg_network_bridge
) {
3737 /* Add the interface to a bridge */
3738 r
= setup_bridge(veth_name
, arg_network_bridge
, false);
3743 } else if (arg_network_zone
) {
3744 /* Add the interface to a bridge, possibly creating it */
3745 r
= setup_bridge(veth_name
, arg_network_zone
, true);
3753 r
= setup_veth_extra(arg_machine
, *pid
, arg_network_veth_extra
);
3757 /* We created the primary and extra veth links now; let's remember this, so that we know to
3758 remove them later on. Note that we don't bother with removing veth links that were created
3759 here when their setup failed half-way, because in that case the kernel should be able to
3760 remove them on its own, since they cannot be referenced by anything yet. */
3761 *veth_created
= true;
3763 r
= setup_macvlan(arg_machine
, *pid
, arg_network_macvlan
);
3767 r
= setup_ipvlan(arg_machine
, *pid
, arg_network_ipvlan
);
3772 if (arg_register
|| !arg_keep_unit
) {
3773 r
= sd_bus_default_system(&bus
);
3775 return log_error_errno(r
, "Failed to open system bus: %m");
3778 if (!arg_keep_unit
) {
3779 /* When a new scope is created for this container, then we'll be registered as its controller, in which
3780 * case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
3781 * scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
3783 r
= sd_bus_match_signal_async(
3786 "org.freedesktop.systemd1",
3788 "org.freedesktop.systemd1.Scope",
3790 on_request_stop
, NULL
, PID_TO_PTR(*pid
));
3792 return log_error_errno(r
, "Failed to request RequestStop match: %m");
3797 r
= register_machine(
3805 arg_custom_mounts
, arg_n_custom_mounts
,
3809 arg_container_service_name
);
3813 } else if (!arg_keep_unit
) {
3820 arg_custom_mounts
, arg_n_custom_mounts
,
3826 } else if (arg_slice
|| arg_property
)
3827 log_notice("Machine and scope registration turned off, --slice= and --property= settings will have no effect.");
3829 r
= sync_cgroup(*pid
, arg_unified_cgroup_hierarchy
, arg_uid_shift
);
3833 r
= create_subcgroup(*pid
, arg_keep_unit
, arg_unified_cgroup_hierarchy
);
3837 r
= chown_cgroup(*pid
, arg_unified_cgroup_hierarchy
, arg_uid_shift
);
3841 /* Notify the child that the parent is ready with all
3842 * its setup (including cgroup-ification), and that
3843 * the child can now hand over control to the code to
3844 * run inside the container. */
3845 (void) barrier_place(&barrier
); /* #4 */
3847 /* Block SIGCHLD here, before notifying child.
3848 * process_pty() will handle it with the other signals. */
3849 assert_se(sigprocmask(SIG_BLOCK
, &mask_chld
, NULL
) >= 0);
3851 /* Reset signal to default */
3852 r
= default_signals(SIGCHLD
, -1);
3854 return log_error_errno(r
, "Failed to reset SIGCHLD: %m");
3856 r
= sd_event_new(&event
);
3858 return log_error_errno(r
, "Failed to get default event source: %m");
3860 (void) sd_event_set_watchdog(event
, true);
3863 r
= sd_bus_attach_event(bus
, event
, 0);
3865 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
3868 r
= setup_sd_notify_parent(event
, notify_socket
, PID_TO_PTR(*pid
), ¬ify_event_source
);
3872 /* Let the child know that we are ready and wait that the child is completely ready now. */
3873 if (!barrier_place_and_sync(&barrier
)) { /* #5 */
3874 log_error("Child died too early.");
3878 /* At this point we have made use of the UID we picked, and thus nss-mymachines
3879 * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */
3880 etc_passwd_lock
= safe_close(etc_passwd_lock
);
3883 "STATUS=Container running.\n"
3884 "X_NSPAWN_LEADER_PID=" PID_FMT
, *pid
);
3885 if (!arg_notify_ready
)
3886 (void) sd_notify(false, "READY=1\n");
3888 if (arg_kill_signal
> 0) {
3889 /* Try to kill the init system on SIGINT or SIGTERM */
3890 (void) sd_event_add_signal(event
, NULL
, SIGINT
, on_orderly_shutdown
, PID_TO_PTR(*pid
));
3891 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, on_orderly_shutdown
, PID_TO_PTR(*pid
));
3893 /* Immediately exit */
3894 (void) sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
3895 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
3898 /* Exit when the child exits */
3899 (void) sd_event_add_signal(event
, NULL
, SIGCHLD
, on_sigchld
, PID_TO_PTR(*pid
));
3901 if (arg_expose_ports
) {
3902 r
= expose_port_watch_rtnl(event
, rtnl_socket_pair
[0], on_address_change
, exposed
, &rtnl
);
3906 (void) expose_port_execute(rtnl
, arg_expose_ports
, exposed
);
3909 rtnl_socket_pair
[0] = safe_close(rtnl_socket_pair
[0]);
3911 r
= pty_forward_new(event
, master
,
3912 PTY_FORWARD_IGNORE_VHANGUP
| (interactive
? 0 : PTY_FORWARD_READ_ONLY
),
3915 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
3917 r
= sd_event_loop(event
);
3919 return log_error_errno(r
, "Failed to run event loop: %m");
3921 pty_forward_get_last_char(forward
, &last_char
);
3923 forward
= pty_forward_free(forward
);
3925 if (!arg_quiet
&& last_char
!= '\n')
3928 /* Kill if it is not dead yet anyway */
3929 if (arg_register
&& !arg_keep_unit
&& bus
)
3930 terminate_machine(bus
, *pid
);
3932 /* Normally redundant, but better safe than sorry */
3933 (void) kill(*pid
, SIGKILL
);
3935 r
= wait_for_container(*pid
, &container_status
);
3939 /* We failed to wait for the container, or the container exited abnormally. */
3941 if (r
> 0 || container_status
== CONTAINER_TERMINATED
) {
3942 /* r > 0 → The container exited with a non-zero status.
3943 * As a special case, we need to replace 133 with a different value,
3944 * because 133 is special-cased in the service file to reboot the container.
3945 * otherwise → The container exited with zero status and a reboot was not requested.
3947 if (r
== EXIT_FORCE_RESTART
)
3948 r
= EXIT_FAILURE
; /* replace 133 with the general failure code */
3950 return 0; /* finito */
3953 /* CONTAINER_REBOOTED, loop again */
3955 if (arg_keep_unit
) {
3956 /* Special handling if we are running as a service: instead of simply
3957 * restarting the machine we want to restart the entire service, so let's
3958 * inform systemd about this with the special exit code 133. The service
3959 * file uses RestartForceExitStatus=133 so that this results in a full
3960 * nspawn restart. This is necessary since we might have cgroup parameters
3961 * set we want to have flushed out. */
3962 *ret
= EXIT_FORCE_RESTART
;
3963 return 0; /* finito */
3966 expose_port_flush(arg_expose_ports
, exposed
);
3968 (void) remove_veth_links(veth_name
, arg_network_veth_extra
);
3969 *veth_created
= false;
3970 return 1; /* loop again */
3973 static int initialize_rlimits(void) {
3975 /* The default resource limits the kernel passes to PID 1, as per kernel 4.16. Let's pass our container payload
3976 * the same values as the kernel originally passed to PID 1, in order to minimize differences between host and
3977 * container execution environments. */
3979 static const struct rlimit kernel_defaults
[_RLIMIT_MAX
] = {
3980 [RLIMIT_AS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3981 [RLIMIT_CORE
] = { 0, RLIM_INFINITY
},
3982 [RLIMIT_CPU
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3983 [RLIMIT_DATA
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3984 [RLIMIT_FSIZE
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3985 [RLIMIT_LOCKS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3986 [RLIMIT_MEMLOCK
] = { 65536, 65536 },
3987 [RLIMIT_MSGQUEUE
] = { 819200, 819200 },
3988 [RLIMIT_NICE
] = { 0, 0 },
3989 [RLIMIT_NOFILE
] = { 1024, 4096 },
3990 [RLIMIT_RSS
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3991 [RLIMIT_RTPRIO
] = { 0, 0 },
3992 [RLIMIT_RTTIME
] = { RLIM_INFINITY
, RLIM_INFINITY
},
3993 [RLIMIT_STACK
] = { 8388608, RLIM_INFINITY
},
3995 /* The kernel scales the default for RLIMIT_NPROC and RLIMIT_SIGPENDING based on the system's amount of
3996 * RAM. To provide best compatibility we'll read these limits off PID 1 instead of hardcoding them
3997 * here. This is safe as we know that PID 1 doesn't change these two limits and thus the original
3998 * kernel's initialization should still be valid during runtime — at least if PID 1 is systemd. Note
3999 * that PID 1 changes a number of other resource limits during early initialization which is why we
4000 * don't read the other limits from PID 1 but prefer the static table above. */
4005 for (rl
= 0; rl
< _RLIMIT_MAX
; rl
++) {
4007 /* Let's only fill in what the user hasn't explicitly configured anyway */
4008 if ((arg_settings_mask
& (SETTING_RLIMIT_FIRST
<< rl
)) == 0) {
4009 const struct rlimit
*v
;
4010 struct rlimit buffer
;
4012 if (IN_SET(rl
, RLIMIT_NPROC
, RLIMIT_SIGPENDING
)) {
4013 /* For these two let's read the limits off PID 1. See above for an explanation. */
4015 if (prlimit(1, rl
, NULL
, &buffer
) < 0)
4016 return log_error_errno(errno
, "Failed to read resource limit RLIMIT_%s of PID 1: %m", rlimit_to_string(rl
));
4020 v
= kernel_defaults
+ rl
;
4022 arg_rlimit
[rl
] = newdup(struct rlimit
, v
, 1);
4023 if (!arg_rlimit
[rl
])
4027 if (DEBUG_LOGGING
) {
4028 _cleanup_free_
char *k
= NULL
;
4030 (void) rlimit_format(arg_rlimit
[rl
], &k
);
4031 log_debug("Setting RLIMIT_%s to %s.", rlimit_to_string(rl
), k
);
4038 int main(int argc
, char *argv
[]) {
4040 _cleanup_free_
char *console
= NULL
;
4041 _cleanup_close_
int master
= -1;
4042 _cleanup_fdset_free_ FDSet
*fds
= NULL
;
4043 int r
, n_fd_passed
, ret
= EXIT_SUCCESS
;
4044 char veth_name
[IFNAMSIZ
] = "";
4045 bool secondary
= false, remove_directory
= false, remove_image
= false;
4047 union in_addr_union exposed
= {};
4048 _cleanup_(release_lock_file
) LockFile tree_global_lock
= LOCK_FILE_INIT
, tree_local_lock
= LOCK_FILE_INIT
;
4049 bool interactive
, veth_created
= false, remove_tmprootdir
= false;
4050 char tmprootdir
[] = "/tmp/nspawn-root-XXXXXX";
4051 _cleanup_(loop_device_unrefp
) LoopDevice
*loop
= NULL
;
4052 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
4053 _cleanup_(dissected_image_unrefp
) DissectedImage
*dissected_image
= NULL
;
4055 log_parse_environment();
4058 /* Make sure rename_process() in the stub init process can work */
4062 r
= parse_argv(argc
, argv
);
4070 r
= initialize_rlimits();
4074 r
= determine_names();
4078 r
= load_settings();
4082 r
= verify_arguments();
4086 r
= detect_unified_cgroup_hierarchy_from_environment();
4090 n_fd_passed
= sd_listen_fds(false);
4091 if (n_fd_passed
> 0) {
4092 r
= fdset_new_listen_fds(&fds
, false);
4094 log_error_errno(r
, "Failed to collect file descriptors: %m");
4099 if (arg_directory
) {
4102 if (path_equal(arg_directory
, "/") && !arg_ephemeral
) {
4103 log_error("Spawning container on root directory is not supported. Consider using --ephemeral.");
4108 if (arg_ephemeral
) {
4109 _cleanup_free_
char *np
= NULL
;
4111 r
= chase_symlinks_and_update(&arg_directory
, 0);
4115 /* If the specified path is a mount point we
4116 * generate the new snapshot immediately
4117 * inside it under a random name. However if
4118 * the specified is not a mount point we
4119 * create the new snapshot in the parent
4120 * directory, just next to it. */
4121 r
= path_is_mount_point(arg_directory
, NULL
, 0);
4123 log_error_errno(r
, "Failed to determine whether directory %s is mount point: %m", arg_directory
);
4127 r
= tempfn_random_child(arg_directory
, "machine.", &np
);
4129 r
= tempfn_random(arg_directory
, "machine.", &np
);
4131 log_error_errno(r
, "Failed to generate name for directory snapshot: %m");
4135 r
= image_path_lock(np
, (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
, &tree_global_lock
, &tree_local_lock
);
4137 log_error_errno(r
, "Failed to lock %s: %m", np
);
4141 r
= btrfs_subvol_snapshot(arg_directory
, np
,
4142 (arg_read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
4143 BTRFS_SNAPSHOT_FALLBACK_COPY
|
4144 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
4145 BTRFS_SNAPSHOT_RECURSIVE
|
4146 BTRFS_SNAPSHOT_QUOTA
);
4148 log_error_errno(r
, "Failed to create snapshot %s from %s: %m", np
, arg_directory
);
4152 free_and_replace(arg_directory
, np
);
4154 remove_directory
= true;
4157 r
= chase_symlinks_and_update(&arg_directory
, arg_template
? CHASE_NONEXISTENT
: 0);
4161 r
= image_path_lock(arg_directory
, (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
, &tree_global_lock
, &tree_local_lock
);
4163 log_error_errno(r
, "Directory tree %s is currently busy.", arg_directory
);
4167 log_error_errno(r
, "Failed to lock %s: %m", arg_directory
);
4172 r
= chase_symlinks_and_update(&arg_template
, 0);
4176 r
= btrfs_subvol_snapshot(arg_template
, arg_directory
,
4177 (arg_read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
4178 BTRFS_SNAPSHOT_FALLBACK_COPY
|
4179 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
4180 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
4181 BTRFS_SNAPSHOT_RECURSIVE
|
4182 BTRFS_SNAPSHOT_QUOTA
);
4185 log_info("Directory %s already exists, not populating from template %s.", arg_directory
, arg_template
);
4187 log_error_errno(r
, "Couldn't create snapshot %s from %s: %m", arg_directory
, arg_template
);
4191 log_info("Populated %s from template %s.", arg_directory
, arg_template
);
4196 if (arg_start_mode
== START_BOOT
) {
4199 if (arg_pivot_root_new
)
4200 p
= prefix_roota(arg_directory
, arg_pivot_root_new
);
4204 if (path_is_os_tree(p
) <= 0) {
4205 log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", p
);
4212 if (arg_pivot_root_new
)
4213 p
= prefix_roota(arg_directory
, arg_pivot_root_new
);
4217 q
= strjoina(p
, "/usr/");
4219 if (laccess(q
, F_OK
) < 0) {
4220 log_error("Directory %s doesn't look like it has an OS tree. Refusing.", p
);
4228 assert(!arg_template
);
4230 r
= chase_symlinks_and_update(&arg_image
, 0);
4234 if (arg_ephemeral
) {
4235 _cleanup_free_
char *np
= NULL
;
4237 r
= tempfn_random(arg_image
, "machine.", &np
);
4239 log_error_errno(r
, "Failed to generate name for image snapshot: %m");
4243 r
= image_path_lock(np
, (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
, &tree_global_lock
, &tree_local_lock
);
4245 r
= log_error_errno(r
, "Failed to create image lock: %m");
4249 r
= copy_file(arg_image
, np
, O_EXCL
, arg_read_only
? 0400 : 0600, FS_NOCOW_FL
, COPY_REFLINK
);
4251 r
= log_error_errno(r
, "Failed to copy image file: %m");
4255 free_and_replace(arg_image
, np
);
4257 remove_image
= true;
4259 r
= image_path_lock(arg_image
, (arg_read_only
? LOCK_SH
: LOCK_EX
) | LOCK_NB
, &tree_global_lock
, &tree_local_lock
);
4261 r
= log_error_errno(r
, "Disk image %s is currently busy.", arg_image
);
4265 r
= log_error_errno(r
, "Failed to create image lock: %m");
4269 if (!arg_root_hash
) {
4270 r
= root_hash_load(arg_image
, &arg_root_hash
, &arg_root_hash_size
);
4272 log_error_errno(r
, "Failed to load root hash file for %s: %m", arg_image
);
4278 if (!mkdtemp(tmprootdir
)) {
4279 r
= log_error_errno(errno
, "Failed to create temporary directory: %m");
4283 remove_tmprootdir
= true;
4285 arg_directory
= strdup(tmprootdir
);
4286 if (!arg_directory
) {
4291 r
= loop_device_make_by_path(arg_image
, arg_read_only
? O_RDONLY
: O_RDWR
, &loop
);
4293 log_error_errno(r
, "Failed to set up loopback block device: %m");
4297 r
= dissect_image_and_warn(
4300 arg_root_hash
, arg_root_hash_size
,
4301 DISSECT_IMAGE_REQUIRE_ROOT
,
4304 /* dissected_image_and_warn() already printed a brief error message. Extend on that with more details */
4305 log_notice("Note that the disk image needs to\n"
4306 " a) either contain only a single MBR partition of type 0x83 that is marked bootable\n"
4307 " b) or contain a single GPT partition of type 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
4308 " c) or follow http://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/\n"
4309 " d) or contain a file system without a partition table\n"
4310 "in order to be bootable with systemd-nspawn.");
4316 if (!arg_root_hash
&& dissected_image
->can_verity
)
4317 log_notice("Note: image %s contains verity information, but no root hash specified! Proceeding without integrity checking.", arg_image
);
4319 r
= dissected_image_decrypt_interactively(dissected_image
, NULL
, arg_root_hash
, arg_root_hash_size
, 0, &decrypted_image
);
4323 /* Now that we mounted the image, let's try to remove it again, if it is ephemeral */
4324 if (remove_image
&& unlink(arg_image
) >= 0)
4325 remove_image
= false;
4328 r
= custom_mount_prepare_all(arg_directory
, arg_custom_mounts
, arg_n_custom_mounts
);
4333 isatty(STDIN_FILENO
) > 0 &&
4334 isatty(STDOUT_FILENO
) > 0;
4336 master
= posix_openpt(O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NDELAY
);
4338 r
= log_error_errno(errno
, "Failed to acquire pseudo tty: %m");
4342 r
= ptsname_malloc(master
, &console
);
4344 r
= log_error_errno(r
, "Failed to determine tty name: %m");
4348 if (arg_selinux_apifs_context
) {
4349 r
= mac_selinux_apply(console
, arg_selinux_apifs_context
);
4354 if (unlockpt(master
) < 0) {
4355 r
= log_error_errno(errno
, "Failed to unlock tty: %m");
4360 log_info("Spawning container %s on %s.\nPress ^] three times within 1s to kill container.",
4361 arg_machine
, arg_image
?: arg_directory
);
4363 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
4365 if (prctl(PR_SET_CHILD_SUBREAPER
, 1, 0, 0, 0) < 0) {
4366 r
= log_error_errno(errno
, "Failed to become subreaper: %m");
4374 interactive
, secondary
,
4376 veth_name
, &veth_created
,
4385 r
== 0 && ret
== EXIT_FORCE_RESTART
? "STOPPING=1\nSTATUS=Restarting..." :
4386 "STOPPING=1\nSTATUS=Terminating...");
4389 (void) kill(pid
, SIGKILL
);
4391 /* Try to flush whatever is still queued in the pty */
4393 (void) copy_bytes(master
, STDOUT_FILENO
, (uint64_t) -1, 0);
4394 master
= safe_close(master
);
4398 (void) wait_for_terminate(pid
, NULL
);
4402 if (remove_directory
&& arg_directory
) {
4405 k
= rm_rf(arg_directory
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
4407 log_warning_errno(k
, "Cannot remove '%s', ignoring: %m", arg_directory
);
4410 if (remove_image
&& arg_image
) {
4411 if (unlink(arg_image
) < 0)
4412 log_warning_errno(errno
, "Can't remove image file '%s', ignoring: %m", arg_image
);
4415 if (remove_tmprootdir
) {
4416 if (rmdir(tmprootdir
) < 0)
4417 log_debug_errno(errno
, "Can't remove temporary root directory '%s', ignoring: %m", tmprootdir
);
4423 p
= strjoina("/run/systemd/nspawn/propagate/", arg_machine
);
4424 (void) rm_rf(p
, REMOVE_ROOT
);
4427 expose_port_flush(arg_expose_ports
, &exposed
);
4430 (void) remove_veth_links(veth_name
, arg_network_veth_extra
);
4431 (void) remove_bridge(arg_network_zone
);
4433 free(arg_directory
);
4439 free(arg_pivot_root_new
);
4440 free(arg_pivot_root_old
);
4442 strv_free(arg_setenv
);
4443 free(arg_network_bridge
);
4444 strv_free(arg_network_interfaces
);
4445 strv_free(arg_network_macvlan
);
4446 strv_free(arg_network_ipvlan
);
4447 strv_free(arg_network_veth_extra
);
4448 strv_free(arg_parameters
);
4449 custom_mount_free_all(arg_custom_mounts
, arg_n_custom_mounts
);
4450 expose_port_free_all(arg_expose_ports
);
4451 free(arg_root_hash
);
4452 rlimit_free_all(arg_rlimit
);
4453 arg_cpuset
= cpu_set_mfree(arg_cpuset
);
4455 return r
< 0 ? EXIT_FAILURE
: ret
;