1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

nspawn: fix --ephemeral with --machine

Follow-up for 2362fdde1b

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.
This commit is contained in:
Luca Boccassi 2022-04-19 12:45:26 +02:00 committed by Yu Watanabe
parent 01d9fbccdd
commit 3603f15171
2 changed files with 19 additions and 20 deletions

View File

@ -230,6 +230,7 @@ static Credential *arg_credentials = NULL;
static size_t arg_n_credentials = 0; static size_t arg_n_credentials = 0;
static char **arg_bind_user = NULL; static char **arg_bind_user = NULL;
static bool arg_suppress_sync = false; static bool arg_suppress_sync = false;
static char *arg_settings_filename = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_directory, freep); STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
STATIC_DESTRUCTOR_REGISTER(arg_template, 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_cpu_set, cpu_set_reset);
STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep); STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, 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) { static int handle_arg_console(const char *arg) {
if (streq(arg, "help")) { if (streq(arg, "help")) {
@ -3046,11 +3048,21 @@ static int determine_names(void) {
if (!hostname_is_valid(arg_machine, 0)) if (!hostname_is_valid(arg_machine, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M."); 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 /* 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. */ * instances at once without manually having to specify -M each time. */
if (arg_ephemeral) if (arg_ephemeral)
if (strextendf(&arg_machine, "-%016" PRIx64, random_u64()) < 0) if (strextendf(&arg_machine, "-%016" PRIx64, random_u64()) < 0)
return log_oom(); return log_oom();
} else {
arg_settings_filename = strjoin(arg_machine, ".nspawn");
if (!arg_settings_filename)
return log_oom();
} }
return 0; return 0;
@ -4604,7 +4616,7 @@ static int merge_settings(Settings *settings, const char *path) {
static int load_settings(void) { static int load_settings(void) {
_cleanup_(settings_freep) Settings *settings = NULL; _cleanup_(settings_freep) Settings *settings = NULL;
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL, *fn = NULL; _cleanup_free_ char *p = NULL;
int r; int r;
if (arg_oci_bundle) if (arg_oci_bundle)
@ -4615,25 +4627,11 @@ static int load_settings(void) {
if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL)) if (FLAGS_SET(arg_settings_mask, _SETTINGS_MASK_ALL))
return 0; 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 */ /* We first look in the admin's directories in /etc and /run */
FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") { FOREACH_STRING(i, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
_cleanup_free_ char *j = NULL; _cleanup_free_ char *j = NULL;
j = path_join(i, fn); j = path_join(i, arg_settings_filename);
if (!j) if (!j)
return log_oom(); return log_oom();
@ -4657,11 +4655,11 @@ static int load_settings(void) {
* actual image we shall boot. */ * actual image we shall boot. */
if (arg_image) { if (arg_image) {
p = file_in_same_dir(arg_image, fn); p = file_in_same_dir(arg_image, arg_settings_filename);
if (!p) if (!p)
return log_oom(); return log_oom();
} else if (arg_directory && !path_equal(arg_directory, "/")) { } 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) if (!p)
return log_oom(); return log_oom();
} }

View File

@ -122,7 +122,6 @@ function check_selinux {
function check_ephemeral_config { function check_ephemeral_config {
# https://github.com/systemd/systemd/issues/13297 # https://github.com/systemd/systemd/issues/13297
local _cmd='test -f /tmp/ephemeral-config'
mkdir -p /run/systemd/nspawn/ mkdir -p /run/systemd/nspawn/
cat >/run/systemd/nspawn/testsuite-13.nc-container.nspawn <<EOF cat >/run/systemd/nspawn/testsuite-13.nc-container.nspawn <<EOF
@ -132,7 +131,9 @@ EOF
touch /tmp/ephemeral-config touch /tmp/ephemeral-config
# /testsuite-13.nc-container is prepared by test.sh # /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 rm -f /run/systemd/nspawn/testsuite-13.nc-container.nspawn
} }