]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: mkfs: honor SOURCE_DATE_EPOCH for root-item timestamps
authorLeo Martins <loemra.dev@gmail.com>
Thu, 28 May 2026 23:32:20 +0000 (16:32 -0700)
committerQu Wenruo <wqu@suse.com>
Sat, 3 Oct 2026 22:40:52 +0000 (09:10 +1030)
mkfs stamps root-item timestamps with time(NULL), so every run embeds
the current wall clock and no two runs are bit-identical. Honor
SOURCE_DATE_EPOCH when it is set in the environment, the standard
reproducible-builds convention.

Add reproducible_now() in common/reproducible.{c,h}, which parses the
value with parse_u64(). An empty value means unset, per the spec. An
invalid value is rejected with an error and a non-zero exit instead of
falling back, so a typo does not silently produce a non-reproducible
image.

The helper goes in common/ because it is also called from
common/root-tree-utils.c, which is shared with other tools; later
patches add more reproducibility helpers next to it.

Convert the time(NULL) call sites:

  - mkfs/common.c btrfs_create_tree_root(): FS_TREE root_item ctime and
    otime.
  - common/root-tree-utils.c btrfs_make_root_dir(): atime/ctime/mtime/
    otime of the top inode of every root it creates (root_tree_dir,
    fs_tree, each -u subvolume, the data-reloc tree).

Signed-off-by: Leo Martins <loemra.dev@gmail.com>
Makefile
common/reproducible.c [new file with mode: 0644]
common/reproducible.h [new file with mode: 0644]
common/root-tree-utils.c
mkfs/common.c

index e1c03f9a985a26a238b99d3e41661154af8e8b34..78c23ad3c6947cb302f7b7de77db14b4ba920764 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -233,6 +233,7 @@ objects = \
        common/parse-utils.o    \
        common/path-utils.o     \
        common/rbtree-utils.o   \
+       common/reproducible.o   \
        common/send-stream.o    \
        common/send-utils.o     \
        common/sort-utils.o     \
diff --git a/common/reproducible.c b/common/reproducible.c
new file mode 100644 (file)
index 0000000..e0e2790
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include "kerncompat.h"
+#include <limits.h>
+#include <stdlib.h>
+#include <time.h>
+#include "common/messages.h"
+#include "common/parse-utils.h"
+#include "common/reproducible.h"
+
+time_t reproducible_now(void)
+{
+       const char *sde = getenv("SOURCE_DATE_EPOCH");
+       u64 val;
+
+       if (!sde || !*sde)
+               return time(NULL);
+
+       if (parse_u64(sde, &val) < 0 || val > (u64)LONG_MAX) {
+               error("invalid SOURCE_DATE_EPOCH: '%s' is not a non-negative integer fitting in time_t",
+                     sde);
+               exit(1);
+       }
+       return (time_t)val;
+}
diff --git a/common/reproducible.h b/common/reproducible.h
new file mode 100644 (file)
index 0000000..caa02cc
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef __BTRFS_REPRODUCIBLE_H__
+#define __BTRFS_REPRODUCIBLE_H__
+
+#include <time.h>
+
+/*
+ * Return SOURCE_DATE_EPOCH as a time_t, or time(NULL) when it is unset
+ * or empty. Errors out if it is set to something other than a
+ * non-negative integer that fits in time_t.
+ */
+time_t reproducible_now(void);
+
+#endif
index 6a87fb54945cfe9f8f11ae1c9d3612391b286e5d..d09643269c33ef37f45a00bff0acd53dc3ff110b 100644 (file)
 #include "kernel-shared/disk-io.h"
 #include "kernel-shared/uuid-tree.h"
 #include "kernel-shared/transaction.h"
-#include "common/root-tree-utils.h"
 #include "common/messages.h"
+#include "common/reproducible.h"
+#include "common/root-tree-utils.h"
 
 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, u64 objectid)
 {
        int ret;
        struct btrfs_inode_item inode_item;
-       time_t now = time(NULL);
+       time_t now = reproducible_now();
 
        memset(&inode_item, 0, sizeof(inode_item));
        btrfs_set_stack_inode_generation(&inode_item, trans->transid);
index 85cfa60d25aaa21a2d0759eb19f5605e14e5870e..06172e756e9e73cf458413e70cf5a922ffbacf1e 100644 (file)
@@ -36,6 +36,7 @@
 #include "common/messages.h"
 #include "common/device-utils.h"
 #include "common/open-utils.h"
+#include "common/reproducible.h"
 #include "common/string-utils.h"
 #include "mkfs/common.h"
 
@@ -121,7 +122,7 @@ static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
                btrfs_set_item_offset(buf, nritems, itemoff);
                btrfs_set_item_size(buf, nritems, sizeof(root_item));
                if (blk == MKFS_FS_TREE) {
-                       time_t now = time(NULL);
+                       time_t now = reproducible_now();
 
                        uuid_generate(uuid);
                        memcpy(root_item.uuid, uuid, BTRFS_UUID_SIZE);