btrfs-progs: mkfs: add fallback check for signature

The support to recognize a zoned btrfs in util-linux/blkid may take time
to get updated everywhere. Add a fallback check for the signature to
avoid accidental overwrites.

The following will not succeed on a zoned device:

  $ mkfs.btrfs /dev/zoned1
  $ mkfs.btrfs /dev/zoned1
  WARNING: /dev/zoned1 contains zoned btrfs signature but was not detected by blkid, please update
  ERROR: use the -f option to force overwrite of /dev/zoned1

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2021-05-06 17:41:06 +02:00
parent 06ca2c87fd
commit 21a33e673d
1 changed files with 47 additions and 0 deletions

View File

@ -684,6 +684,36 @@ out:
return ret;
}
/*
* Check for signature at the offset 0 that would be present in case of zoned
* device. Workaround for old blkid that do not recognize the format to avoid
* accidental overwrites.
*/
static int check_btrfs_signature_zoned(const char *device)
{
int fd;
int ret;
char buf[BTRFS_SUPER_INFO_SIZE];
struct btrfs_super_block *sb;
fd = open(device, O_RDONLY);
if (fd < 0)
return -1;
ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, 0);
if (ret < 0) {
ret = -1;
goto out;
}
sb = (struct btrfs_super_block *)buf;
if (btrfs_super_magic(sb) == BTRFS_MAGIC)
ret = 1;
else
ret = 0;
out:
close(fd);
return ret;
}
/*
* Check for existing filesystem or partition table on device.
* Returns:
@ -759,6 +789,23 @@ out:
fprintf(stderr,
"probe of %s failed, cannot detect "
"existing filesystem.\n", device);
/* Either nothing found or there was an error is a reason to double check */
if (ret == 0 || ret == -1) {
ret = check_btrfs_signature_zoned(device);
if (ret > 0) {
warning(
"%s contains zoned btrfs signature but was not detected by blkid, please update",
device);
ret = 1;
} else if (ret < 0) {
warning(
"cannot read superblock on %s, please check manually\n",
device);
ret = -1;
}
}
return ret;
}