Blame btrfs-progs-3.14.2-mkfs-set-uuid.patch

Eric Sandeen fd953a
From: Eric Sandeen <sandeen@redhat.com>
Eric Sandeen fd953a
Date: Wed, 14 May 2014 17:39:07 +0000 (-0500)
Eric Sandeen fd953a
Subject: btrfs-progs: mkfs: allow UUID specification at mkfs time
Eric Sandeen fd953a
X-Git-Url: http://repo.or.cz/w/btrfs-progs-unstable/devel.git/commitdiff_plain/a03901849f868c65f845325fcaeb3d25c6896759
Eric Sandeen fd953a
Eric Sandeen fd953a
btrfs-progs: mkfs: allow UUID specification at mkfs time
Eric Sandeen fd953a
Eric Sandeen fd953a
Allow the specification of the filesystem UUID at mkfs time.
Eric Sandeen fd953a
Eric Sandeen fd953a
Non-unique unique IDs are rejected.  This includes attempting
Eric Sandeen fd953a
to re-mkfs with the same UUID; if you really want to do that,
Eric Sandeen fd953a
you can mkfs with a new UUID, then re-mkfs with the one you
Eric Sandeen fd953a
wanted.
Eric Sandeen fd953a
Eric Sandeen fd953a
(Implemented only for mkfs.btrfs, not btrfs-convert).
Eric Sandeen fd953a
Eric Sandeen fd953a
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Eric Sandeen fd953a
[converted help to asciidoc]
Eric Sandeen fd953a
Signed-off-by: David Sterba <dsterba@suse.cz>
Eric Sandeen fd953a
---
Eric Sandeen fd953a
Eric Sandeen fd953a
Index: btrfs-progs-v3.14.2/Documentation/mkfs.btrfs.txt
Eric Sandeen fd953a
===================================================================
Eric Sandeen fd953a
--- btrfs-progs-v3.14.2.orig/Documentation/mkfs.btrfs.txt
Eric Sandeen fd953a
+++ btrfs-progs-v3.14.2/Documentation/mkfs.btrfs.txt
Eric Sandeen fd953a
@@ -21,6 +21,7 @@ $$[-s|--sectorsize <sectorsize>]$$
Eric Sandeen fd953a
 $$[-r|--rootdir <rootdir>]$$
Eric Sandeen fd953a
 $$[-K|--nodiscard]$$
Eric Sandeen fd953a
 $$[-O|--features <feature1>[,<feature2>...]]$$
Eric Sandeen fd953a
+$$[-U|--uuid <UUID>]$$
Eric Sandeen fd953a
 $$[-h]$$
Eric Sandeen fd953a
 $$[-V|--version]$$
Eric Sandeen fd953a
 $$<device> [<device>...]$$
Eric Sandeen fd953a
@@ -109,6 +110,10 @@ supported by old kernels.
Eric Sandeen fd953a
 To see all features run::::
Eric Sandeen fd953a
 mkfs.btrfs -O list-all
Eric Sandeen fd953a
 
Eric Sandeen fd953a
+-U|--uuid <UUID>::
Eric Sandeen fd953a
+Create the filesystem with the specified UUID, which must not already exist on
Eric Sandeen fd953a
+the system.
Eric Sandeen fd953a
+
Eric Sandeen fd953a
 -V|--version::
Eric Sandeen fd953a
 Print the *mkfs.btrfs* version and exit.
Eric Sandeen fd953a
 
Eric Sandeen fd953a
Index: btrfs-progs-v3.14.2/btrfs-convert.c
Eric Sandeen fd953a
===================================================================
Eric Sandeen fd953a
--- btrfs-progs-v3.14.2.orig/btrfs-convert.c
Eric Sandeen fd953a
+++ btrfs-progs-v3.14.2/btrfs-convert.c
Eric Sandeen fd953a
@@ -2240,7 +2240,7 @@ static int do_convert(const char *devnam
Eric Sandeen fd953a
 		goto fail;
Eric Sandeen fd953a
 	}
Eric Sandeen fd953a
 	ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
Eric Sandeen fd953a
-			 blocks, total_bytes, blocksize, blocksize,
Eric Sandeen fd953a
+			 NULL, blocks, total_bytes, blocksize, blocksize,
Eric Sandeen fd953a
 			 blocksize, blocksize, 0);
Eric Sandeen fd953a
 	if (ret) {
Eric Sandeen fd953a
 		fprintf(stderr, "unable to create initial ctree: %s\n",
Eric Sandeen fd953a
Index: btrfs-progs-v3.14.2/mkfs.c
Eric Sandeen fd953a
===================================================================
Eric Sandeen fd953a
--- btrfs-progs-v3.14.2.orig/mkfs.c
Eric Sandeen fd953a
+++ btrfs-progs-v3.14.2/mkfs.c
Eric Sandeen fd953a
@@ -288,6 +288,7 @@ static void print_usage(void)
Eric Sandeen fd953a
 	fprintf(stderr, "\t -r --rootdir the source directory\n");
Eric Sandeen fd953a
 	fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
Eric Sandeen fd953a
 	fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
Eric Sandeen fd953a
+	fprintf(stderr, "\t -U --uuid specify the filesystem UUID\n");
Eric Sandeen fd953a
 	fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
Eric Sandeen fd953a
 	fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
Eric Sandeen fd953a
 	exit(1);
Eric Sandeen fd953a
@@ -351,6 +352,7 @@ static struct option long_options[] = {
Eric Sandeen fd953a
 	{ "rootdir", 1, NULL, 'r' },
Eric Sandeen fd953a
 	{ "nodiscard", 0, NULL, 'K' },
Eric Sandeen fd953a
 	{ "features", 1, NULL, 'O' },
Eric Sandeen fd953a
+	{ "uuid", 0, NULL, 'U' },
Eric Sandeen fd953a
 	{ NULL, 0, NULL, 0}
Eric Sandeen fd953a
 };
Eric Sandeen fd953a
 
Eric Sandeen fd953a
@@ -1273,11 +1275,12 @@ int main(int ac, char **av)
Eric Sandeen fd953a
 	int dev_cnt = 0;
Eric Sandeen fd953a
 	int saved_optind;
Eric Sandeen fd953a
 	char estr[100];
Eric Sandeen fd953a
+	char *fs_uuid = NULL;
Eric Sandeen fd953a
 	u64 features = DEFAULT_MKFS_FEATURES;
Eric Sandeen fd953a
 
Eric Sandeen fd953a
 	while(1) {
Eric Sandeen fd953a
 		int c;
Eric Sandeen fd953a
-		c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
Eric Sandeen fd953a
+		c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMK",
Eric Sandeen fd953a
 				long_options, &option_index);
Eric Sandeen fd953a
 		if (c < 0)
Eric Sandeen fd953a
 			break;
Eric Sandeen fd953a
@@ -1346,6 +1349,9 @@ int main(int ac, char **av)
Eric Sandeen fd953a
 				source_dir = optarg;
Eric Sandeen fd953a
 				source_dir_set = 1;
Eric Sandeen fd953a
 				break;
Eric Sandeen fd953a
+			case 'U':
Eric Sandeen fd953a
+				fs_uuid = optarg;
Eric Sandeen fd953a
+				break;
Eric Sandeen fd953a
 			case 'K':
Eric Sandeen fd953a
 				discard = 0;
Eric Sandeen fd953a
 				break;
Eric Sandeen fd953a
@@ -1368,6 +1374,20 @@ int main(int ac, char **av)
Eric Sandeen fd953a
 			"The -r option is limited to a single device\n");
Eric Sandeen fd953a
 		exit(1);
Eric Sandeen fd953a
 	}
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+	if (fs_uuid) {
Eric Sandeen fd953a
+		uuid_t dummy_uuid;
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+		if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
Eric Sandeen fd953a
+			fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
Eric Sandeen fd953a
+			exit(1);
Eric Sandeen fd953a
+		}
Eric Sandeen fd953a
+		if (!test_uuid_unique(fs_uuid)) {
Eric Sandeen fd953a
+			fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
Eric Sandeen fd953a
+			exit(1);
Eric Sandeen fd953a
+		}
Eric Sandeen fd953a
+	}
Eric Sandeen fd953a
+	
Eric Sandeen fd953a
 	while (dev_cnt-- > 0) {
Eric Sandeen fd953a
 		file = av[optind++];
Eric Sandeen fd953a
 		if (is_block_device(file))
Eric Sandeen fd953a
@@ -1514,7 +1534,7 @@ int main(int ac, char **av)
Eric Sandeen fd953a
 
Eric Sandeen fd953a
 	process_fs_features(features);
Eric Sandeen fd953a
 
Eric Sandeen fd953a
-	ret = make_btrfs(fd, file, label, blocks, dev_block_count,
Eric Sandeen fd953a
+	ret = make_btrfs(fd, file, label, fs_uuid, blocks, dev_block_count,
Eric Sandeen fd953a
 			 nodesize, leafsize,
Eric Sandeen fd953a
 			 sectorsize, stripesize, features);
Eric Sandeen fd953a
 	if (ret) {
Eric Sandeen fd953a
Index: btrfs-progs-v3.14.2/utils.c
Eric Sandeen fd953a
===================================================================
Eric Sandeen fd953a
--- btrfs-progs-v3.14.2.orig/utils.c
Eric Sandeen fd953a
+++ btrfs-progs-v3.14.2/utils.c
Eric Sandeen fd953a
@@ -93,12 +93,41 @@ static u64 reference_root_table[] = {
Eric Sandeen fd953a
 	[6] =	BTRFS_CSUM_TREE_OBJECTID,
Eric Sandeen fd953a
 };
Eric Sandeen fd953a
 
Eric Sandeen fd953a
-int make_btrfs(int fd, const char *device, const char *label,
Eric Sandeen fd953a
+int test_uuid_unique(char *fs_uuid)
Eric Sandeen fd953a
+{
Eric Sandeen fd953a
+	int unique = 1;
Eric Sandeen fd953a
+	blkid_dev_iterate iter = NULL;
Eric Sandeen fd953a
+	blkid_dev dev = NULL;
Eric Sandeen fd953a
+	blkid_cache cache = NULL;
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+	if (blkid_get_cache(&cache, 0) < 0) {
Eric Sandeen fd953a
+		printf("ERROR: lblkid cache get failed\n");
Eric Sandeen fd953a
+		return 1;
Eric Sandeen fd953a
+	}
Eric Sandeen fd953a
+	blkid_probe_all(cache);
Eric Sandeen fd953a
+	iter = blkid_dev_iterate_begin(cache);
Eric Sandeen fd953a
+	blkid_dev_set_search(iter, "UUID", fs_uuid);
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+	while (blkid_dev_next(iter, &dev) == 0) {
Eric Sandeen fd953a
+		dev = blkid_verify(cache, dev);
Eric Sandeen fd953a
+		if (dev) {
Eric Sandeen fd953a
+			unique = 0;
Eric Sandeen fd953a
+			break;
Eric Sandeen fd953a
+		}
Eric Sandeen fd953a
+	}
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+	blkid_dev_iterate_end(iter);
Eric Sandeen fd953a
+	blkid_put_cache(cache);
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+	return unique;
Eric Sandeen fd953a
+}
Eric Sandeen fd953a
+
Eric Sandeen fd953a
+int make_btrfs(int fd, const char *device, const char *label, char *fs_uuid,
Eric Sandeen fd953a
 	       u64 blocks[7], u64 num_bytes, u32 nodesize,
Eric Sandeen fd953a
 	       u32 leafsize, u32 sectorsize, u32 stripesize, u64 features)
Eric Sandeen fd953a
 {
Eric Sandeen fd953a
 	struct btrfs_super_block super;
Eric Sandeen fd953a
-	struct extent_buffer *buf;
Eric Sandeen fd953a
+	struct extent_buffer *buf = NULL;
Eric Sandeen fd953a
 	struct btrfs_root_item root_item;
Eric Sandeen fd953a
 	struct btrfs_disk_key disk_key;
Eric Sandeen fd953a
 	struct btrfs_extent_item *extent_item;
Eric Sandeen fd953a
@@ -125,7 +154,20 @@ int make_btrfs(int fd, const char *devic
Eric Sandeen fd953a
 	memset(&super, 0, sizeof(super));
Eric Sandeen fd953a
 
Eric Sandeen fd953a
 	num_bytes = (num_bytes / sectorsize) * sectorsize;
Eric Sandeen fd953a
-	uuid_generate(super.fsid);
Eric Sandeen fd953a
+	if (fs_uuid) {
Eric Sandeen fd953a
+		if (uuid_parse(fs_uuid, super.fsid) != 0) {
Eric Sandeen fd953a
+			fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
Eric Sandeen fd953a
+			ret = -EINVAL;
Eric Sandeen fd953a
+			goto out;
Eric Sandeen fd953a
+		}
Eric Sandeen fd953a
+		if (!test_uuid_unique(fs_uuid)) {
Eric Sandeen fd953a
+			fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
Eric Sandeen fd953a
+			ret = -EBUSY;
Eric Sandeen fd953a
+			goto out;
Eric Sandeen fd953a
+		}
Eric Sandeen fd953a
+	} else {
Eric Sandeen fd953a
+		uuid_generate(super.fsid);
Eric Sandeen fd953a
+	}
Eric Sandeen fd953a
 	uuid_generate(super.dev_item.uuid);
Eric Sandeen fd953a
 	uuid_generate(chunk_tree_uuid);
Eric Sandeen fd953a
 
Eric Sandeen fd953a
Index: btrfs-progs-v3.14.2/utils.h
Eric Sandeen fd953a
===================================================================
Eric Sandeen fd953a
--- btrfs-progs-v3.14.2.orig/utils.h
Eric Sandeen fd953a
+++ btrfs-progs-v3.14.2/utils.h
Eric Sandeen fd953a
@@ -40,7 +40,7 @@
Eric Sandeen fd953a
 #define BTRFS_UUID_UNPARSED_SIZE	37
Eric Sandeen fd953a
 
Eric Sandeen fd953a
 int make_btrfs(int fd, const char *device, const char *label,
Eric Sandeen fd953a
-	       u64 blocks[6], u64 num_bytes, u32 nodesize,
Eric Sandeen fd953a
+	       char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize,
Eric Sandeen fd953a
 	       u32 leafsize, u32 sectorsize, u32 stripesize, u64 features);
Eric Sandeen fd953a
 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
Eric Sandeen fd953a
 			struct btrfs_root *root, u64 objectid);
Eric Sandeen fd953a
@@ -101,5 +101,5 @@ int get_btrfs_mount(const char *dev, cha
Eric Sandeen fd953a
 int find_mount_root(const char *path, char **mount_root);
Eric Sandeen fd953a
 int get_device_info(int fd, u64 devid,
Eric Sandeen fd953a
 		struct btrfs_ioctl_dev_info_args *di_args);
Eric Sandeen fd953a
-
Eric Sandeen fd953a
+int test_uuid_unique(char *fs_uuid);
Eric Sandeen fd953a
 #endif