summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel de Perthuis <g2p.code@gmail.com>2024-10-27 14:37:45 +0000
committerGabriel de Perthuis <g2p.code@gmail.com>2024-10-27 14:48:05 +0000
commit8d5f44d3b16cac690f64558161998b75c7066b68 (patch)
tree07c5cedee01e4de9c5aea01ad33f03c644930e84
parent5350fd2b7b63a1e1ef006f2c4ce9fbbec35b9ff9 (diff)
Check for a sufficiently recent libblkid before formatting
This can prevent data loss as libblkid < 2.40.1 can fail to recognize existing bcachefs filesystems. Reported on #bcache. Signed-off-by: Gabriel de Perthuis <g2p.code@gmail.com>
-rw-r--r--c_src/tools-util.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/c_src/tools-util.c b/c_src/tools-util.c
index 43c9b789..3a76a02e 100644
--- a/c_src/tools-util.c
+++ b/c_src/tools-util.c
@@ -185,6 +185,24 @@ unsigned get_blocksize(int fd)
/* Open a block device, do magic blkid stuff to probe for existing filesystems: */
int open_for_format(struct dev_opts *dev, bool force)
{
+ int blkid_version_code = blkid_get_library_version(NULL, NULL);
+ if (blkid_version_code < 2401) {
+ if (force) {
+ fprintf(
+ stderr,
+ "Continuing with out of date libblkid %s because --force was passed.\n",
+ BLKID_VERSION);
+ } else {
+ // Reference for picking 2.40.1:
+ // https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.40/v2.40.1-ReleaseNotes
+ // https://github.com/util-linux/util-linux/issues/3103
+ die(
+ "Refusing to format when using libblkid %s\n"
+ "libblkid >= 2.40.1 is required to check for existing filesystems\n"
+ "Earlier versions may not recognize some bcachefs filesystems.\n", BLKID_VERSION);
+ }
+ }
+
blkid_probe pr;
const char *fs_type = NULL, *fs_label = NULL;
size_t fs_type_len, fs_label_len;