chiark / gitweb /
nspawn: allow spawning ephemeral nspawn containers based on the root file system...
[elogind.git] / src / nspawn / nspawn.c
index 8082166ee205f102e77404ce5ae4e93b9452cf1e..651a45126b8585b7e0037e76a53c553318b2fdf5 100644 (file)
@@ -687,6 +687,11 @@ static int parse_argv(int argc, char *argv[]) {
                 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;
@@ -1317,6 +1322,10 @@ static int setup_journal(const char *directory) {
         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();
@@ -1345,8 +1354,7 @@ static int setup_journal(const char *directory) {
                          "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)
@@ -2934,8 +2942,8 @@ int main(int argc, char *argv[]) {
         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;
                 }
@@ -2956,7 +2964,21 @@ int main(int argc, char *argv[]) {
                 } 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;