This function does all the needed things for changing uuid.
Signed-off-by: Qu Wenruo <quwenruo@xxxxxxxxxxxxxx>
---
v2:
Move to props.c
Call change_id_prepare() and change_id_done().
---
props.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/props.c b/props.c
index 0a1bc1e..9b2117c 100644
--- a/props.c
+++ b/props.c
@@ -20,12 +20,14 @@
#include <sys/xattr.h>
#include <fcntl.h>
#include <unistd.h>
+#include <uuid/uuid.h>
#include "ctree.h"
#include "commands.h"
#include "utils.h"
#include "props.h"
#include "disk-io.h"
+#include "volumes.h"
#define XATTR_BTRFS_PREFIX "btrfs."
#define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
@@ -383,6 +385,72 @@ static int change_id_done(struct btrfs_fs_info *fs_info)
return write_all_supers(fs_info->tree_root);
}
+static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid,
+ const char *new_chunk_uuid)
+{
+ int ret = 0;
+
+ /* caller should do extra check on passed uuid */
+ if (new_fsid) {
+ /* allocated mem will be freed at close_ctree() */
+ fs_info->new_fsid = malloc(BTRFS_FSID_SIZE);
+ if (!fs_info->new_fsid) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ ret = uuid_parse(new_fsid, fs_info->new_fsid);
+ if (ret < 0)
+ goto out;
+ }
+
+ if (new_chunk_uuid) {
+ /* allocated mem will be freed at close_ctree() */
+ fs_info->new_chunk_tree_uuid = malloc(BTRFS_UUID_SIZE);
+ if (!fs_info->new_chunk_tree_uuid) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ ret = uuid_parse(new_chunk_uuid, fs_info->new_chunk_tree_uuid);
+ if (ret < 0)
+ goto out;
+ }
+
+ /* Now we can begin id change */
+ ret = change_id_prepare(fs_info);
+ if (ret < 0)
+ goto out;
+
+ /* Change extents first */
+ ret = change_extents_uuid(fs_info);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to change UUID of metadata\n");
+ goto out;
+ }
+
+ /* Then devices */
+ ret = change_devices_uuid(fs_info);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to change UUID of devices\n");
+ goto out;
+ }
+
+ /* Last, change fsid in super, only fsid change needs this */
+ if (new_fsid) {
+ memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
+ BTRFS_FSID_SIZE);
+ memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
+ BTRFS_FSID_SIZE);
+ ret = write_all_supers(fs_info->tree_root);
+ if (ret < 0)
+ goto out;
+ }
+
+ /* Now id change is done */
+ ret = change_id_done(fs_info);
+out:
+ return ret;
+}
+
const struct prop_handler prop_handlers[] = {
{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
prop_read_only},
--
2.4.0
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html