From 0c8f57cae2984ccc42cc640f2e46e862c98b3f24 Mon Sep 17 00:00:00 2001 From: Totoro Date: Fri, 4 Sep 2026 23:25:39 +0800 Subject: [PATCH] machine: propagate foreign image clone directory errors unprivileged_clone() returns success when mountfsd_make_directory() fails. Consequently, callers can report a successful clone even though the destination directory was never created. Return the original error instead of zero so the failed clone is reported to the caller. Add a regression test that places a regular file without an image suffix at the destination path. Such a file is ignored by image discovery but prevents directory creation. Check that cloning reports failure without changing the file, then succeeds after the conflicting file is removed. --- src/shared/discover-image.c | 2 +- test/units/TEST-13-NSPAWN.machined.sh | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index 419246d733..c88fa84016 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -1725,7 +1725,7 @@ static int unprivileged_clone(Image *i, const char *new_path) { /* flags= */ 0, &new_fd); if (r < 0) - return 0; + return r; /* Mount new image */ _cleanup_close_ int target_fd = -EBADF; diff --git a/test/units/TEST-13-NSPAWN.machined.sh b/test/units/TEST-13-NSPAWN.machined.sh index e0035c4677..2774072699 100755 --- a/test/units/TEST-13-NSPAWN.machined.sh +++ b/test/units/TEST-13-NSPAWN.machined.sh @@ -195,6 +195,24 @@ test -d /var/lib/machines/.hidden1 machinectl clean test ! -d /var/lib/machines/.hidden1 +if can_do_rootless_nspawn; then + mkdir /var/lib/machines/foreign-error-source + chown foreign-0:foreign-0 /var/lib/machines/foreign-error-source + + # A regular file without an image suffix is not discovered as an image, but still prevents + # mountfsd from creating the clone directory. The clone operation must report this failure. + echo "existing file" >/var/lib/machines/foreign-error-target + (! machinectl show-image foreign-error-target) + (! machinectl clone foreign-error-source foreign-error-target) + assert_eq "$(