return -EINVAL;
}
+ if (arg_ephemeral && !IN_SET(arg_link_journal, LINK_NO, LINK_AUTO)) {
+ log_error("--ephemeral and --link-journal= may not be combined.");
+ return -EINVAL;
+ }
+
if (arg_volatile != VOLATILE_NO && arg_read_only) {
log_error("Cannot combine --read-only with --volatile. Note that --volatile already implies a read-only base hierarchy.");
return -EINVAL;
char *id;
int r;
+ /* Don't link journals in ephemeral mode */
+ if (arg_ephemeral)
+ return 0;
+
p = strappend(directory, "/etc/machine-id");
if (!p)
return log_oom();
"Host and machine ids are equal (%s): refusing to link journals", id);
if (arg_link_journal == LINK_AUTO)
return 0;
- return
- -EEXIST;
+ return -EEXIST;
}
if (arg_link_journal == LINK_NO)
}
+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;
if (arg_directory) {
assert(!arg_image);
- if (path_equal(arg_directory, "/")) {
- log_error("Spawning container on root directory not supported.");
+ if (path_equal(arg_directory, "/") && !arg_ephemeral) {
+ log_error("Spawning container on root directory is not supported. Consider using --ephemeral.");
r = -EINVAL;
goto finish;
}
} else if (arg_ephemeral) {
char *np;
- r = tempfn_random(arg_directory, &np);
+ /* If the specified path is a mount point we
+ * generate the new snapshot immediately
+ * inside it under a random name. However if
+ * the specified is not a mount point we
+ * create the new snapshot in the parent
+ * directory, just next to it. */
+ r = path_is_mount_point(arg_directory, false);
+ if (r < 0) {
+ log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
+ goto finish;
+ }
+ if (r > 0)
+ r = tempfn_random_child(arg_directory, &np);
+ else
+ r = tempfn_random(arg_directory, &np);
if (r < 0) {
log_error_errno(r, "Failed to generate name for snapshot: %m");
goto finish;
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);