diff mbox series

[2/2] btrfs-progs: add mkfs -P option for dev_uuid

Message ID e1bca34459f2f61fc2cf40a930b930b6f33d69a7.1695861950.git.anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: mkfs: testing cloned-device | expand

Commit Message

Anand Jain Sept. 28, 2023, 1:09 a.m. UTC
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.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 mkfs/common.c | 8 +++++++-
 mkfs/common.h | 1 +
 mkfs/main.c   | 9 ++++++++-
 3 files changed, 16 insertions(+), 2 deletions(-)

Comments

David Sterba Oct. 2, 2023, 3:21 p.m. UTC | #1
On Thu, Sep 28, 2023 at 09:09:19AM +0800, Anand Jain wrote:
> 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.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
>  mkfs/common.c | 8 +++++++-
>  mkfs/common.h | 1 +
>  mkfs/main.c   | 9 ++++++++-
>  3 files changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/mkfs/common.c b/mkfs/common.c
> index d400413c7d41..e9a2fd5e4d3e 100644
> --- a/mkfs/common.c
> +++ b/mkfs/common.c
> @@ -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++) {
> diff --git a/mkfs/common.h b/mkfs/common.h
> index 06ddc926390f..d512ed853987 100644
> --- a/mkfs/common.h
> +++ b/mkfs/common.h
> @@ -91,6 +91,7 @@ struct btrfs_mkfs_config {
>  	/* Logical addresses of superblock [0] and other tree roots */
>  	u64 blocks[MKFS_BLOCK_COUNT + 1];
>  	char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
> +	char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];

Please add a comment what this uuid means.

>  	char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
>  
>  	/* Superblock offset after make_btrfs */
> diff --git a/mkfs/main.c b/mkfs/main.c
> index 68ff5d7785d3..f0ff1d6cc936 100644
> --- a/mkfs/main.c
> +++ b/mkfs/main.c
> @@ -1097,6 +1097,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;
> @@ -1144,6 +1145,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
>  			{ "features", required_argument, NULL, 'O' },
>  			{ "runtime-features", required_argument, NULL, 'R' },
>  			{ "uuid", required_argument, NULL, 'U' },
> +			{ "uuid", required_argument, NULL, 'P' },

You can't add 2 long optiosn with the same name, also, don't add the
short option for now. And the help text is also missing.

>  			{ "quiet", 0, NULL, 'q' },
>  			{ "verbose", 0, NULL, 'v' },
>  			{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
> @@ -1154,7 +1156,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
>  			{ NULL, 0, NULL, 0}
>  		};
>  
> -		c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:R:O:r:U:VvMKq",
> +		c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:R:O:r:U:P:VvMKq",
>  				long_options, NULL);
>  		if (c < 0)
>  			break;
> @@ -1262,6 +1264,10 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
>  				strncpy(fs_uuid, optarg,
>  					BTRFS_UUID_UNPARSED_SIZE - 1);
>  				break;
> +			case 'P':
> +				strncpy(dev_uuid, optarg,
> +					BTRFS_UUID_UNPARSED_SIZE - 1);
> +				break;
>  			case 'K':
>  				opt_discard = false;
>  				break;
> @@ -1667,6 +1673,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;
> -- 
> 2.39.3
Anand Jain Oct. 3, 2023, 3:45 a.m. UTC | #2
Thanks for the comment. I have fixed them (+ more) in v2.
diff mbox series

Patch

diff --git a/mkfs/common.c b/mkfs/common.c
index d400413c7d41..e9a2fd5e4d3e 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -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++) {
diff --git a/mkfs/common.h b/mkfs/common.h
index 06ddc926390f..d512ed853987 100644
--- a/mkfs/common.h
+++ b/mkfs/common.h
@@ -91,6 +91,7 @@  struct btrfs_mkfs_config {
 	/* Logical addresses of superblock [0] and other tree roots */
 	u64 blocks[MKFS_BLOCK_COUNT + 1];
 	char fs_uuid[BTRFS_UUID_UNPARSED_SIZE];
+	char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
 	char chunk_uuid[BTRFS_UUID_UNPARSED_SIZE];
 
 	/* Superblock offset after make_btrfs */
diff --git a/mkfs/main.c b/mkfs/main.c
index 68ff5d7785d3..f0ff1d6cc936 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -1097,6 +1097,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;
@@ -1144,6 +1145,7 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 			{ "features", required_argument, NULL, 'O' },
 			{ "runtime-features", required_argument, NULL, 'R' },
 			{ "uuid", required_argument, NULL, 'U' },
+			{ "uuid", required_argument, NULL, 'P' },
 			{ "quiet", 0, NULL, 'q' },
 			{ "verbose", 0, NULL, 'v' },
 			{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
@@ -1154,7 +1156,7 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 			{ NULL, 0, NULL, 0}
 		};
 
-		c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:R:O:r:U:VvMKq",
+		c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:R:O:r:U:P:VvMKq",
 				long_options, NULL);
 		if (c < 0)
 			break;
@@ -1262,6 +1264,10 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 				strncpy(fs_uuid, optarg,
 					BTRFS_UUID_UNPARSED_SIZE - 1);
 				break;
+			case 'P':
+				strncpy(dev_uuid, optarg,
+					BTRFS_UUID_UNPARSED_SIZE - 1);
+				break;
 			case 'K':
 				opt_discard = false;
 				break;
@@ -1667,6 +1673,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;