Skip to contentSkip to conversation

btrfs-progs: mkfs: testing cloned-device

PATCH In review Anand Jain 2 participants 6 messages Oct 3, 2023 – Oct 19, 2023

Subsystems (2)
lore
Share & tools
lore

Anand Jain

author
#0 lore raw

v2 cover btrfs-progs: mkfs: testing cloned-device

v2:
Worked on review comments from David.

--- v1 ---
This patchset adds support for testing cloned-device in mkfs. So using mkfs.btrfs both option -U and -P a new device can be created to match the FSID and UUID of an existing device. This is useful for testing cloned-device.

Anand Jain (2):
btrfs-progs: document allowing duplicate fsid
btrfs-progs: add mkfs option for dev_uuid

 Documentation/mkfs.btrfs.rst | 10 ++++++++--
 mkfs/common.c                |  8 +++++++-
 mkfs/common.h                |  5 +++++
 mkfs/main.c                  | 16 +++++++++++++++-
 4 files changed, 35 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH v2 1/2 btrfs-progs: document allowing duplicate fsid

The commit ("btrfs-progs: allow duplicate fsid for single device filesystems") lets the duplicate fsid used for a new mkfs document this.

---

David,
This patch can be folded into patch:
ff4c4a3a00db btrfs-progs: allow duplicate fsid for single device filesystems
in your devel.

 Documentation/mkfs.btrfs.rst | 5 +++--
 mkfs/main.c                  | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
Documentation/mkfs.btrfs.rst +3 -2
index 2ff3ed241533..95ddc5399090 100644
@@ -179,8 +179,9 @@ OPTIONS
         Resets any previous effects of *--verbose*.
 
 -U|--uuid <UUID>
-        Create the filesystem with the given *UUID*. The UUID must not exist on any
-        filesystem currently present.
+        Create the filesystem with the given *UUID*. For a single-device filesystem,
+        you can duplicate the UUID; however, for a multi-device filesystem, the UUID
+        must not already exist on any currently present filesystem.
 
 -v|--verbose
         Increase verbosity level, default is 1.
mkfs/main.c +1 -1
index ead1edded9bf..ccab3c31b2f5 100644
@@ -430,7 +430,7 @@ static const char * const mkfs_usage[] = {
 	OPTLINE("-s|--sectorsize SIZE", "data block size (may not be mountable by current kernel)"),
 	OPTLINE("-O|--features LIST", "comma separated list of filesystem features (use '-O list-all' to list features)"),
 	OPTLINE("-L|--label LABEL", "set the filesystem label"),
-	OPTLINE("-U|--uuid UUID", "specify the filesystem UUID (must be unique)"),
+	OPTLINE("-U|--uuid UUID", "Specify the filesystem UUID (must be unique for a filesystem with multiple devices)"),
 	"Creation:",
 	OPTLINE("-b|--byte-count SIZE", "set size of each device to SIZE (filesystem size is sum of all device sizes)"),
 	OPTLINE("-r|--rootdir DIR", "copy files from DIR to the image root directory"),
Signature · 1 line
2.39.3
author
#2 lore raw

PATCH v2 2/2 btrfs-progs: add mkfs option for dev_uuid

In reply to Anand Jain

Add an option to specify the device uuid for mkfs. This is useful for creating a filesystem with a specific device uuid. This is useful for testing.

---
v2:
Added --help.
Added comments for the dev_uuid and fs_uuid.
Removed short -P option instead using only long option --device-uuid. Add error if --device-uuid is used with multi-device.
For example:

   $ mkfs.btrfs -f --device-uuid 29e708ad-6fe5-447d-8910-a9b9f79854b5 /dev/sdc5 /dev/sdc6
     btrfs-progs v6.5.1 
     See https://btrfs.readthedocs.io for more information.

ERROR: the option --device-uuid is limited to a single device

 Documentation/mkfs.btrfs.rst |  5 +++++
 mkfs/common.c                |  8 +++++++-
 mkfs/common.h                |  5 +++++
 mkfs/main.c                  | 14 ++++++++++++++
 4 files changed, 31 insertions(+), 1 deletion(-)
Documentation/mkfs.btrfs.rst +5 -0
index 95ddc5399090..919bb19c3e0a 100644
@@ -183,6 +183,11 @@ OPTIONS
         you can duplicate the UUID; however, for a multi-device filesystem, the UUID
         must not already exist on any currently present filesystem.
 
+--device-uuid <UUID>
+        Create the filesystem with the given device-uuid (also known as sub-uuid) *UUID*.
+        For a single-device filesystem, you can duplicate the device-uuid; however, used
+        for a multi-device filesystem this option will not apply presently.
+
 -v|--verbose
         Increase verbosity level, default is 1.
 
mkfs/common.c +7 -1
index d400413c7d41..e9a2fd5e4d3e 100644
@@ -340,6 +340,7 @@ static void mkfs_blocks_remove(enum btrfs_mkfs_block *blocks, int *blocks_nr,
 
 /*
  * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
+ * @dev_uuid - if NULL, generates a UUID, returns back the new device UUID
  *
  * The superblock signature is not valid, denotes a partially created
  * filesystem, needs to be finalized.
@@ -435,7 +436,12 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 	} else {
 		uuid_parse(cfg->fs_uuid, super.fsid);
 	}
-	uuid_generate(super.dev_item.uuid);
+	if (!*cfg->dev_uuid) {
+		uuid_generate(super.dev_item.uuid);
+		uuid_unparse(super.dev_item.uuid, cfg->dev_uuid);
+	} else {
+		uuid_parse(cfg->dev_uuid, super.dev_item.uuid);
+	}
 	uuid_generate(chunk_tree_uuid);
 
 	for (i = 0; i < blocks_nr; i++) {
mkfs/common.h +5 -0
index 06ddc926390f..554693b52aff 100644
@@ -90,7 +90,12 @@ struct btrfs_mkfs_config {
 
 	/* Logical addresses of superblock [0] and other tree roots */
 	u64 blocks[MKFS_BLOCK_COUNT + 1];
+
+	/* btrfs_super_block filesystem uuid */
 	char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
+
+	/* btrfs_dev_item device uuid */
+	char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
 	char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
 
 	/* Superblock offset after make_btrfs */
mkfs/main.c +14 -0
index ccab3c31b2f5..e92b43846217 100644
@@ -431,6 +431,7 @@ static const char * const mkfs_usage[] = {
 	OPTLINE("-O|--features LIST", "comma separated list of filesystem features (use '-O list-all' to list features)"),
 	OPTLINE("-L|--label LABEL", "set the filesystem label"),
 	OPTLINE("-U|--uuid UUID", "Specify the filesystem UUID (must be unique for a filesystem with multiple devices)"),
+	OPTLINE("--device-uuid UUID", "Specify the filesystem device UUID (a.k.a sub-uuid) (for single device filesystem only)"),
 	"Creation:",
 	OPTLINE("-b|--byte-count SIZE", "set size of each device to SIZE (filesystem size is sum of all device sizes)"),
 	OPTLINE("-r|--rootdir DIR", "copy files from DIR to the image root directory"),
@@ -1161,6 +1162,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 	struct btrfs_mkfs_features features = btrfs_mkfs_default_features;
 	enum btrfs_csum_type csum_type = BTRFS_CSUM_TYPE_CRC32;
 	char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
+	char dev_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
 	u32 nodesize = 0;
 	bool nodesize_forced = false;
 	u32 sectorsize = 0;
@@ -1187,6 +1189,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 			GETOPT_VAL_SHRINK = GETOPT_VAL_FIRST,
 			GETOPT_VAL_CHECKSUM,
 			GETOPT_VAL_GLOBAL_ROOTS,
+			GETOPT_VAL_DEVICE_UUID,
 		};
 		static const struct option long_options[] = {
 			{ "byte-count", required_argument, NULL, 'b' },
@@ -1208,6 +1211,8 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 			{ "features", required_argument, NULL, 'O' },
 			{ "runtime-features", required_argument, NULL, 'R' },
 			{ "uuid", required_argument, NULL, 'U' },
+			{ "device-uuid", required_argument, NULL, \
+						GETOPT_VAL_DEVICE_UUID },
 			{ "quiet", 0, NULL, 'q' },
 			{ "verbose", 0, NULL, 'v' },
 			{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
@@ -1331,6 +1336,10 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 			case 'q':
 				bconf_be_quiet();
 				break;
+			case GETOPT_VAL_DEVICE_UUID:
+				strncpy(dev_uuid, optarg,
+					BTRFS_UUID_UNPARSED_SIZE - 1);
+				break;
 			case GETOPT_VAL_SHRINK:
 				shrink_rootdir = true;
 				break;
@@ -1372,6 +1381,10 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 		error("the option -r is limited to a single device");
 		goto error;
 	}
+	if (strlen(dev_uuid) != 0 && device_count > 1) {
+		error("the option --device-uuid is limited to a single device");
+		goto error;
+	}
 	if (shrink_rootdir && source_dir == NULL) {
 		error("the option --shrink must be used with --rootdir");
 		goto error;
@@ -1730,6 +1743,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 
 	mkfs_cfg.label = label;
 	memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
+	memcpy(mkfs_cfg.dev_uuid, dev_uuid, sizeof(mkfs_cfg.dev_uuid));
 	mkfs_cfg.num_bytes = dev_block_count;
 	mkfs_cfg.nodesize = nodesize;
 	mkfs_cfg.sectorsize = sectorsize;
Signature · 1 line
2.39.3
#3 lore raw
In reply to Anand Jain
Quoted 9 lines · Anand Jain

v2:
Worked on review comments from David.

--- v1 ---
This patchset adds support for testing cloned-device in mkfs. So using mkfs.btrfs both option -U and -P a new device can be created to match the FSID and UUID of an existing device. This is useful for testing cloned-device.

Quoted 3 lines

Anand Jain (2):
btrfs-progs: document allowing duplicate fsid
btrfs-progs: add mkfs option for dev_uuid

Added to devel, thanks. I did some fixups, the device uuid is printed in the summary and validation of the device uuid.

author
#4 lore raw
Quoted 4 lines · David Sterba
Quoted 3 lines · Anand Jain

v2:
Worked on review comments from David.

Quoted 8 lines
Quoted 8 lines

--- v1 ---
This patchset adds support for testing cloned-device in mkfs. So using mkfs.btrfs both option -U and -P a new device can be created to match the FSID and UUID of an existing device. This is useful for testing cloned-device.

Anand Jain (2):
btrfs-progs: document allowing duplicate fsid

+ must not already exist on any currently present filesystem.

There is a white-space error at the end of the line mentioned above in the devel branch.

Quoted 4 lines
Quoted 1 line

btrfs-progs: add mkfs option for dev_uuid

Added to devel, thanks. I did some fixups, the device uuid is printed in the summary and validation of the device uuid.

Looks good. Thanks.

- Anand

#5 lore raw
Quoted 13 lines · Anand Jain
Quoted 10 lines · David Sterba
Quoted 9 lines · Anand Jain

v2:
Worked on review comments from David.

--- v1 ---
This patchset adds support for testing cloned-device in mkfs. So using mkfs.btrfs both option -U and -P a new device can be created to match the FSID and UUID of an existing device. This is useful for testing cloned-device.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Anand Jain (2):
btrfs-progs: document allowing duplicate fsid

+ must not already exist on any currently present filesystem.

Fixed.