}
+static int setup_propagate(const char *root) {
+ const char *p, *q;
+
+ (void) mkdir_p("/run/systemd/nspawn/", 0755);
+ (void) mkdir_p("/run/systemd/nspawn/propagate", 0600);
+ p = strappenda("/run/systemd/nspawn/propagate/", arg_machine);
+ (void) mkdir_p(p, 0600);
+
+ q = strappenda(root, "/run/systemd/nspawn/incoming");
+ mkdir_parents(q, 0755);
+ mkdir_p(q, 0600);
+
+ if (mount(p, q, NULL, MS_BIND, NULL) < 0)
+ return log_error_errno(errno, "Failed to install propagation bind mount.");
+
+ if (mount(NULL, q, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) < 0)
+ return log_error_errno(errno, "Failed to make propagation mount read-only");
+
+ return 0;
+}
+
static int setup_image(char **device_path, int *loop_nr) {
struct loop_info64 info = {
.lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN
}
if (!arg_machine) {
- arg_machine = strdup(basename(arg_image ?: arg_directory));
+ if (arg_directory && path_equal(arg_directory, "/"))
+ arg_machine = gethostname_malloc();
+ else
+ arg_machine = strdup(basename(arg_image ?: arg_directory));
+
if (!arg_machine)
return log_oom();
log_error("Failed to determine machine name automatically, please use -M.");
return -EINVAL;
}
+
+ if (arg_ephemeral) {
+ char *b;
+
+ /* Add a random suffix when this is an
+ * ephemeral machine, so that we can run many
+ * instances at once without manually having
+ * to specify -M each time. */
+
+ if (asprintf(&b, "%s-%016" PRIx64, arg_machine, random_u64()) < 0)
+ return log_oom();
+
+ free(arg_machine);
+ arg_machine = b;
+ }
}
return 0;
goto finish;
}
- pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWNS|
- (arg_share_system ? 0 : CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS)|
- (arg_private_network ? CLONE_NEWNET : 0), NULL);
+ pid = raw_clone(SIGCHLD|CLONE_NEWNS|
+ (arg_share_system ? 0 : CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS)|
+ (arg_private_network ? CLONE_NEWNET : 0), NULL);
if (pid < 0) {
if (errno == EINVAL)
r = log_error_errno(errno, "clone() failed, do you have namespace support enabled in your kernel? (You need UTS, IPC, PID and NET namespacing built in): %m");
dev_setup(arg_directory);
+ if (setup_propagate(arg_directory) < 0)
+ _exit(EXIT_FAILURE);
+
if (setup_seccomp() < 0)
_exit(EXIT_FAILURE);
log_warning_errno(k, "Cannot remove subvolume '%s', ignoring: %m", arg_directory);
}
+ if (arg_machine) {
+ const char *p;
+
+ p = strappenda("/run/systemd/nspawn/propagate", arg_machine);
+ (void) rm_rf(p, false, true, false);
+ }
+
free(arg_directory);
free(arg_template);
free(arg_image);