linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <dsterba@suse.cz>
Subject: [PATCH 3/4] btrfs-progs: Add '-U' and '-u' option for btrfstune.
Date: Fri, 15 May 2015 14:28:25 +0800	[thread overview]
Message-ID: <1431671306-22262-3-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1431671306-22262-1-git-send-email-quwenruo@cn.fujitsu.com>

Add two options, '-U' and '-u' for btrfstune.

For '-u', it is used to change fsid to a random new UUID.
For '-U', it is used to change fsid to a specified UUID.

Both will also change the internal use only chunk_tree_uuid to a random
new UUID.

Although there is a GNU getopt extension "::" to get optional
option-argument, but is forbidden by POSIX.1-2008, so use split options
here.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfstune.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 5 deletions(-)

diff --git a/btrfstune.c b/btrfstune.c
index 1d5af33..e324f83 100644
--- a/btrfstune.c
+++ b/btrfstune.c
@@ -406,23 +406,27 @@ static void print_usage(void)
 	fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
 	fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
 	fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
-	fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
+	fprintf(stderr, "\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
+	fprintf(stderr, "\t-U [uuid]\t\tchange fsid, if uuid is not given, use a random one\n");
 }
 
 int main(int argc, char *argv[])
 {
 	struct btrfs_root *root;
+	enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_WRITES;
 	int success = 0;
 	int total = 0;
 	int extrefs_flag = 0;
 	int seeding_flag = 0;
 	u64 seeding_value = 0;
 	int skinny_flag = 0;
+	int random_fsid = 0;
+	char *new_fsid_str = NULL;
 	int ret;
 
 	optind = 1;
 	while(1) {
-		int c = getopt(argc, argv, "S:rxf");
+		int c = getopt(argc, argv, "S:rxfuU:");
 		if (c < 0)
 			break;
 		switch(c) {
@@ -439,6 +443,14 @@ int main(int argc, char *argv[])
 		case 'f':
 			force = 1;
 			break;
+		case 'U':
+			ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
+			new_fsid_str = optarg;
+			break;
+		case 'u':
+			ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
+			random_fsid = 1;
+			break;
 		default:
 			print_usage();
 			return 1;
@@ -453,13 +465,37 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	if (!(seeding_flag + extrefs_flag + skinny_flag)) {
+	if (random_fsid && new_fsid_str) {
+		fprintf(stderr,
+			"ERROR: Random fsid can't be used with specified fsid\n");
+		return 1;
+	}
+	if (!(seeding_flag + extrefs_flag + skinny_flag) &&
+	    !(random_fsid || new_fsid_str)) {
 		fprintf(stderr,
 			"ERROR: At least one option should be assigned.\n");
 		print_usage();
 		return 1;
 	}
 
+	if (new_fsid_str) {
+		uuid_t tmp;
+
+		ret = uuid_parse(new_fsid_str, tmp);
+		if (ret < 0) {
+			fprintf(stderr,
+				"ERROR: Could not parse UUID: %s\n",
+				new_fsid_str);
+			return 1;
+		}
+		if (!test_uuid_unique(new_fsid_str)) {
+			fprintf(stderr,
+				"ERROR: Fsid %s is not unique\n",
+				new_fsid_str);
+			return 1;
+		}
+	}
+
 	ret = check_mounted(device);
 	if (ret < 0) {
 		fprintf(stderr, "Could not check mount status: %s\n",
@@ -470,7 +506,7 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	root = open_ctree(device, 0, OPEN_CTREE_WRITES);
+	root = open_ctree(device, 0, ctree_flags);
 
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
@@ -483,7 +519,8 @@ int main(int argc, char *argv[])
 			ret = ask_user("We are going to clear the seeding flag, are you sure?");
 			if (!ret) {
 				fprintf(stderr, "Clear seeding flag canceled\n");
-				return 1;
+				ret = 1;
+				goto out;
 			}
 		}
 
@@ -505,6 +542,25 @@ int main(int argc, char *argv[])
 		total++;
 	}
 
+	if (random_fsid || new_fsid_str) {
+		if (!force) {
+			fprintf(stderr,
+				"Warning: It's highly recommended to run 'btrfs check' before this operation\n");
+			fprintf(stderr,
+				"Also canceling running UUID change progress may cause corruption\n");
+			ret = ask_user("We are going to change UUID, are your sure?");
+			if (!ret) {
+				fprintf(stderr, "UUID change canceled\n");
+				ret = 1;
+				goto out;
+			}
+		}
+		ret = change_uuid(root->fs_info, new_fsid_str);
+		if (!ret)
+			success++;
+		total++;
+	}
+
 	if (success == total) {
 		ret = 0;
 	} else {
@@ -512,6 +568,7 @@ int main(int argc, char *argv[])
 		ret = 1;
 		fprintf(stderr, "btrfstune failed\n");
 	}
+out:
 	close_ctree(root);
 
 	return ret;
-- 
2.4.0


  parent reply	other threads:[~2015-05-15  6:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
2015-05-15  6:28 ` [PATCH 2/4] btrfs-progs: Add ability to restore unfinished fsid change Qu Wenruo
2015-05-15  6:28 ` Qu Wenruo [this message]
2015-05-15  6:28 ` [PATCH 4/4] btrfs-progs: Update the Document for offline " Qu Wenruo
2015-05-26 15:24 ` [PATCH 1/4] btrfs-progs: Minor change of change_uuid() David Sterba

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=1431671306-22262-3-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --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).