[Ocfs2-tools-devel] [PATCH 26/32] mkfs.ocfs2: Add -U option to allow custom UUIDs

Sunil Mushran sunil.mushran at oracle.com
Tue Sep 14 15:54:56 PDT 2010


Patch allows user to pass in a custom UUID.

Signed-off-by: Sunil Mushran <sunil.mushran at oracle.com>
---
 mkfs.ocfs2/mkfs.c          |   66 ++++++++++++++++++++++++++++++++++---------
 mkfs.ocfs2/mkfs.h          |    2 +-
 mkfs.ocfs2/mkfs.ocfs2.8.in |    9 ++++++
 3 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/mkfs.ocfs2/mkfs.c b/mkfs.ocfs2/mkfs.c
index 5cc2bdf..39174c0 100644
--- a/mkfs.ocfs2/mkfs.c
+++ b/mkfs.ocfs2/mkfs.c
@@ -70,7 +70,6 @@ static void format_leading_space(State *s);
 static void open_device(State *s);
 static void close_device(State *s);
 static int initial_slots_for_volume(uint64_t size);
-static void generate_uuid(State *s);
 static void create_generation(State *s);
 static void init_record(State *s, SystemFileDiskRecord *rec, int type, int mode);
 static void print_state(State *s);
@@ -146,6 +145,26 @@ static uint64_t align_bytes_to_clusters_ceil(State *s,
 	return ret;
 }
 
+/*
+ *	Translate 32 bytes uuid to 36 bytes uuid format.
+ *	for example:
+ *	32 bytes uuid: 178BDC83D50241EF94EB474A677D498B
+ *	36 bytes uuid: 178BDC83-D502-41EF-94EB-474A677D498B
+ */
+static void translate_uuid(char *uuid_32, char *uuid_36)
+{
+	int i;
+	char *cp = uuid_32;
+
+	for (i = 0; i < 36; i++) {
+		if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
+			uuid_36[i] = '-';
+			continue;
+		}
+		uuid_36[i] = *cp++;
+	}
+}
+
 static void
 handle_signal (int sig)
 {
@@ -524,8 +543,6 @@ main(int argc, char **argv)
 
 	fill_defaults(s);
 
-	generate_uuid (s);
-
 	create_generation(s);
 
 	print_state (s);
@@ -791,6 +808,7 @@ get_state(int argc, char **argv)
 	int verbose = 0, quiet = 0, force = 0, xtool = 0, hb_dev = 0;
 	int show_version = 0, dry_run = 0;
 	char *device_name;
+	char *uuid = NULL, uuid_36[37] = {'\0'}, *uuid_p;
 	int ret;
 	uint64_t val;
 	uint64_t journal_size_in_bytes = 0;
@@ -829,8 +847,8 @@ get_state(int argc, char **argv)
 		progname = strdup("mkfs.ocfs2");
 
 	while (1) {
-		c = getopt_long(argc, argv, "b:C:L:N:J:M:vnqVFHxT:", long_options,
-				NULL);
+		c = getopt_long(argc, argv, "b:C:L:N:J:M:vnqVFHxT:U:",
+				long_options, NULL);
 
 		if (c == -1)
 			break;
@@ -923,6 +941,10 @@ get_state(int argc, char **argv)
 					   &journal64);
 			break;
 
+		case 'U':
+			uuid = strdup(optarg);
+			break;
+
 		case 'H':
 			hb_dev = 1;
 			break;
@@ -1110,6 +1132,28 @@ get_state(int argc, char **argv)
 	else
 		s->dx_dirs = 0;
 
+	/* uuid */
+	if (!uuid)
+		uuid_generate(s->uuid);
+	else {
+		if (strlen(uuid) == 32) {
+			translate_uuid(uuid, uuid_36);
+			uuid_p = uuid_36;
+		} else
+			uuid_p = uuid;
+
+		/*uuid_parse only support 36 bytes uuid*/
+		if (uuid_parse(uuid_p, s->uuid)) {
+			com_err(s->progname, 0, "Invalid UUID specified");
+			exit(1);
+		}
+		printf("\nWARNING!!! OCFS2 uses the UUID to uniquely identify "
+		       "a file system.\nHaving two OCFS2 file systems with "
+		       "the same UUID could, in the least,\ncause erratic "
+		       "behavior, and if unlucky, cause file system damage.\n"
+		       "Please choose the UUID with care.\n\n");
+		free(uuid);
+	}
 
 	/* Here if the user set these flags explicitly, we will use them and
 	 * discard the setting in the features set.
@@ -1279,7 +1323,8 @@ usage(const char *progname)
 {
 	fprintf(stderr, "usage: %s [-b block-size] [-C cluster-size] "
 		"[-J journal-options]\n\t\t[-L volume-label] [-M mount-type] "
-		"[-N number-of-node-slots]\n\t\t[-T filesystem-type] [-HFqvV] "
+		"[-N number-of-node-slots]\n\t\t[-T filesystem-type] [-U uuid]"
+		"[-HFqvV] "
 		"\n\t\t[--fs-feature-level=[default|max-compat|max-features]] "
 		"\n\t\t[--fs-features=[[no]sparse,...]] [--global-heartbeat]"
 		"\n\t\t[--cluster-stack=stackname] [--cluster-name=clustername]"
@@ -2323,7 +2368,7 @@ format_superblock(State *s, SystemFileDiskRecord *rec,
 	di->id2.i_super.s_feature_ro_compat = s->feature_flags.opt_ro_compat;
 
 	strcpy((char *)di->id2.i_super.s_label, s->vol_label);
-	memcpy(di->id2.i_super.s_uuid, s->uuid, 16);
+	memcpy(di->id2.i_super.s_uuid, s->uuid, OCFS2_VOL_UUID_LEN);
 
 	/* s_uuid_hash is also used by Indexed Dirs */
 	if (s->feature_flags.opt_incompat & OCFS2_FEATURE_INCOMPAT_XATTR ||
@@ -2686,13 +2731,6 @@ initial_slots_for_volume(uint64_t size)
 		return 16;
 }
 
-static void
-generate_uuid(State *s)
-{
-	s->uuid = do_malloc(s, OCFS2_VOL_UUID_LEN);
-	uuid_generate(s->uuid);
-}
-
 static void create_generation(State *s)
 {
 	int randfd = 0;
diff --git a/mkfs.ocfs2/mkfs.h b/mkfs.ocfs2/mkfs.h
index eeb5bcc..5ad59de 100644
--- a/mkfs.ocfs2/mkfs.h
+++ b/mkfs.ocfs2/mkfs.h
@@ -215,7 +215,7 @@ struct _State {
 
 	char *vol_label;
 	char *device_name;
-	unsigned char *uuid;
+	unsigned char uuid[OCFS2_VOL_UUID_LEN];
 	char *cluster_stack;
 	char *cluster_name;
 	uint8_t stack_flags;
diff --git a/mkfs.ocfs2/mkfs.ocfs2.8.in b/mkfs.ocfs2/mkfs.ocfs2.8.in
index df4e6e5..9ebd5a2 100644
--- a/mkfs.ocfs2/mkfs.ocfs2.8.in
+++ b/mkfs.ocfs2/mkfs.ocfs2.8.in
@@ -234,6 +234,15 @@ Display the heuristically determined values without overwriting the existing fil
 Quiet mode.
 
 .TP
+\fB\-U\fR \fIuuid\fR
+Specify a custom UUID in the plain (2A4D1C581FAA42A1A41D26EFC90C1315) or
+traditional (2a4d1c58-1faa-42a1-a41d-26efc90c1315) format. This option in
+\fBnot\fR recommended because the file system uses the UUID to uniquely identify
+a file system. \fIIf more than one file system were to have the same UUID, one
+is very likely to encounter erratic behavior, if not, outright file system
+corruption.\fR 
+
+.TP
 \fB\-v, \-\-verbose\fR
 Verbose mode.
 
-- 
1.7.0.4




More information about the Ocfs2-tools-devel mailing list