]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mkfs-util: Ignore btrfs compression when there is no dir to copy
authorChris Down <chris@chrisdown.name>
Thu, 6 Nov 2025 15:11:55 +0000 (23:11 +0800)
committerChris Down <chris@chrisdown.name>
Fri, 7 Nov 2025 10:17:08 +0000 (18:17 +0800)
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
src/shared/mkfs-util.c

index 629d02207c14610f63b81c65add1cf4bfc22c637..455c627ff6bb3ebb3f17b490ac838464d0501d30 100644 (file)
@@ -466,17 +466,22 @@ int make_filesystem(
                         return log_oom();
 
                 if (compression) {
-                        _cleanup_free_ char *c = NULL;
-
-                        c = strdup(compression);
-                        if (!c)
-                                return log_oom();
+                        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();
 
-                        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