mkfs-util: Ignore btrfs compression when there is no dir to copy

mkfs.btrfs requires that the --compress option be used together with
--rootdir, as compression only makes sense in that context (because
compression is not a persistent setting).

Right now, If --compress is specified without --rootdir, mkfs.btrfs
fails with:

  ERROR: --compression must be used with --rootdir

This can occur when repart is configured with Compression= but the
partition populate logic doesn't use the --rootdir code path (eg. when
using loop device mounting to copy files after mkfs).

Add a defensive check to skip compression and emit a user-friendly
warning when compression is requested but no root directory is
provided. The warning message references the repart directive names
(Compression= and CopyFiles=) rather than low-level mkfs options to
help users understand the requirement.

This prevents crashes but doesn't enable compression, that requires
ensuring the --rootdir code path is used, which it currently is not and
will be addressed in the next patch.

Fixes: https://github.com/systemd/systemd/issues/39584
This commit is contained in:
Chris Down
2025-11-06 23:11:55 +08:00
parent d8e38d4aaa
commit adf88771ff

View File

@@ -466,17 +466,22 @@ int make_filesystem(
return log_oom();
if (compression) {
_cleanup_free_ char *c = NULL;
if (!root)
log_warning("Btrfs compression setting ignored because no files are being copied. "
"Compression= can only be applied when CopyFiles= is also specified.");
else {
_cleanup_free_ char *c = NULL;
c = strdup(compression);
if (!c)
return log_oom();
c = strdup(compression);
if (!c)
return log_oom();
if (compression_level && !strextend(&c, ":", compression_level))
return log_oom();
if (compression_level && !strextend(&c, ":", compression_level))
return log_oom();
if (strv_extend_many(&argv, "--compress", c) < 0)
return log_oom();
if (strv_extend_many(&argv, "--compress", c) < 0)
return log_oom();
}
}
/* mkfs.btrfs unconditionally warns about several settings changing from v5.15 onwards which