linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com, gpiccoli@igalia.com
Subject: [PATCH 2/2] btrfs-progs: add mkfs -P option for dev_uuid
Date: Thu, 28 Sep 2023 09:09:19 +0800	[thread overview]
Message-ID: <e1bca34459f2f61fc2cf40a930b930b6f33d69a7.1695861950.git.anand.jain@oracle.com> (raw)
In-Reply-To: <cover.1695861950.git.anand.jain@oracle.com>

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];
 	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;
-- 
2.39.3


  parent reply	other threads:[~2023-09-28  1:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28  1:09 [PATCH 0/2] btrfs-progs: mkfs: testing cloned-device Anand Jain
2023-09-28  1:09 ` [PATCH 1/2] btrfs-progs: allow duplicate fsid for single device Anand Jain
2023-10-02 15:22   ` David Sterba
2023-09-28  1:09 ` Anand Jain [this message]
2023-10-02 15:21   ` [PATCH 2/2] btrfs-progs: add mkfs -P option for dev_uuid David Sterba
2023-10-03  3:45     ` Anand Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e1bca34459f2f61fc2cf40a930b930b6f33d69a7.1695861950.git.anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=dsterba@suse.com \
    --cc=gpiccoli@igalia.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).