diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 158966574e2..d2300755655 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -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(); } diff --git a/test/units/testsuite-13.sh b/test/units/testsuite-13.sh index 38b6feae78e..1fc3d8bee38 100755 --- a/test/units/testsuite-13.sh +++ b/test/units/testsuite-13.sh @@ -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 <