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);
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")) {
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;
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)
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();
* 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();
}
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
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
}