]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn: fix --ephemeral with --machine
authorLuca Boccassi <bluca@debian.org>
Tue, 19 Apr 2022 10:45:26 +0000 (12:45 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 19 Apr 2022 17:33:01 +0000 (02:33 +0900)
Follow-up for https://github.com/systemd/systemd/commit/2362fdde1bd4bf54772383ef29431f683729ba76

When --machine is specified with --ephemeral, no random suffix is added, so
the recently added assert would fail.

Add a top-level variable with the expected file name for nspawn files, and
compute it when the rest of the names are computed.

src/nspawn/nspawn.c
test/units/testsuite-13.sh

index 158966574e235c18731710eab134ac71c6a2de59..d2300755655271c64292c06948937b14df365b15 100644 (file)
@@ -230,6 +230,7 @@ static Credential *arg_credentials = NULL;
 static size_t arg_n_credentials = 0;
 static char **arg_bind_user = NULL;
 static bool arg_suppress_sync = false;
+static char *arg_settings_filename = NULL;
 
 STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
 STATIC_DESTRUCTOR_REGISTER(arg_template, freep);
@@ -263,6 +264,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
 STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_reset);
 STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
+STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
 
 static int handle_arg_console(const char *arg) {
         if (streq(arg, "help")) {
@@ -3046,11 +3048,21 @@ static int determine_names(void) {
                 if (!hostname_is_valid(arg_machine, 0))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M.");
 
+                /* Copy the machine name before the random suffix is added below, otherwise we won't be able
+                 * to match fixed config file names. */
+                arg_settings_filename = strjoin(arg_machine, ".nspawn");
+                if (!arg_settings_filename)
+                        return log_oom();
+
                 /* 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 (arg_ephemeral)
                         if (strextendf(&arg_machine, "-%016" PRIx64, random_u64()) < 0)
                                 return log_oom();
+        } else {
+                arg_settings_filename = strjoin(arg_machine, ".nspawn");
+                if (!arg_settings_filename)
+                        return log_oom();
         }
 
         return 0;
@@ -4604,7 +4616,7 @@ static int merge_settings(Settings *settings, const char *path) {
 static int load_settings(void) {
         _cleanup_(settings_freep) Settings *settings = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *p = NULL, *fn = NULL;
+        _cleanup_free_ char *p = NULL;
         int r;
 
         if (arg_oci_bundle)
@@ -4615,25 +4627,11 @@ static int load_settings(void) {
         if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL))
                 return 0;
 
-        /* In ephemeral mode we append '-' and a random 16 characters string to the image name, so fixed
-         * config files are no longer matched. Ignore the random suffix for the purpose of finding files. */
-        if (arg_ephemeral) {
-                fn = strdup(arg_machine);
-                if (!fn)
-                        return log_oom();
-                assert(strlen(fn) > 17); /* Should end with -XXXXXXXXXXXXXXXX */
-                strcpy(fn + strlen(fn) - 17, ".nspawn");
-        } else {
-                fn = strjoin(arg_machine, ".nspawn");
-                if (!fn)
-                        return log_oom();
-        }
-
         /* We first look in the admin's directories in /etc and /run */
         FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
                 _cleanup_free_ char *j = NULL;
 
-                j = path_join(i, fn);
+                j = path_join(i, arg_settings_filename);
                 if (!j)
                         return log_oom();
 
@@ -4657,11 +4655,11 @@ static int load_settings(void) {
                  * actual image we shall boot. */
 
                 if (arg_image) {
-                        p = file_in_same_dir(arg_image, fn);
+                        p = file_in_same_dir(arg_image, arg_settings_filename);
                         if (!p)
                                 return log_oom();
                 } else if (arg_directory && !path_equal(arg_directory, "/")) {
-                        p = file_in_same_dir(arg_directory, fn);
+                        p = file_in_same_dir(arg_directory, arg_settings_filename);
                         if (!p)
                                 return log_oom();
                 }
index 38b6feae78ebfa452828cc8d0c162dab98c13f2a..1fc3d8bee38d255c58cbca100ce1e78f1a61938a 100755 (executable)
@@ -122,7 +122,6 @@ function check_selinux {
 
 function check_ephemeral_config {
     # https://github.com/systemd/systemd/issues/13297
-    local _cmd='test -f /tmp/ephemeral-config'
 
     mkdir -p /run/systemd/nspawn/
     cat >/run/systemd/nspawn/testsuite-13.nc-container.nspawn <<EOF
@@ -132,7 +131,9 @@ EOF
     touch /tmp/ephemeral-config
 
     # /testsuite-13.nc-container is prepared by test.sh
-    systemd-nspawn --register=no -D /testsuite-13.nc-container --ephemeral /bin/sh -x -c "$_cmd"
+    systemd-nspawn --register=no -D /testsuite-13.nc-container --ephemeral /bin/sh -x -c "test -f /tmp/ephemeral-config"
+
+    systemd-nspawn --register=no -D /testsuite-13.nc-container --ephemeral --machine foobar /bin/sh -x -c "! test -f /tmp/ephemeral-config"
 
     rm -f /run/systemd/nspawn/testsuite-13.nc-container.nspawn
 }