btrfs-progs v4.1 add support to change uuid of btrfs fs.
Signed-off-by: Chen Hanxiao <chenhanxiao(a)cn.fujitsu.com>
---
v5: use NOT_SUPPORTED macro
improve testcases
v4: although btrfstune did not had '--help', pass it anyway
improve testcases
v3: set errno as ENOTSUP when btrfstune -u is not available
v2: put btrfs operation back to daemon/btrfs.c
move tests to tests/btrfs
daemon/btrfs.c | 58 ++++++++++++++++++++++++++++++++++++++++++
daemon/daemon.h | 1 +
daemon/uuids.c | 6 ++---
tests/btrfs/test-btrfs-misc.pl | 20 +++++++++++++++
4 files changed, 81 insertions(+), 4 deletions(-)
diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index cedea31..28a48cf 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -797,6 +797,44 @@ do_btrfs_device_delete (char *const *devices, const char *fs)
return 0;
}
+
+/* btrfstune command add two new options
+ * -U UUID change fsid to UUID
+ * -u change fsid, use a random one
+ * since v4.1
+ * We could check wheter 'btrfstune' support
+ * '-u' and '-U UUID' option by checking the output of
+ * 'btrfstune' command.
+ */
+static int
+test_btrfstune_uuid_opt (void)
+{
+ static int result = -1;
+ if (result != -1)
+ return result;
+
+ CLEANUP_FREE char *err = NULL;
+
+ int r = commandr (NULL, &err, str_btrfstune, "--help", NULL);
+
+ if (r == -1) {
+ reply_with_error ("btrfstune: %s", err);
+ return -1;
+ }
+
+ /* FIXME currently btrfstune do not support '--help'.
+ * If got an invalid options, it will print its usage
+ * in stderr.
+ * We had to check it there.
+ */
+ if (strstr (err, "-U") == NULL || strstr (err, "-u") == NULL)
+ result = 0;
+ else
+ result = 1;
+
+ return result;
+}
+
int
do_btrfs_set_seeding (const char *device, int svalue)
{
@@ -814,6 +852,26 @@ do_btrfs_set_seeding (const char *device, int svalue)
return 0;
}
+int
+btrfs_set_uuid (const char *device, const char *uuid)
+{
+ CLEANUP_FREE char *err = NULL;
+ int r;
+ int has_uuid_opts = test_btrfstune_uuid_opt ();
+
+ if (has_uuid_opts <= 0)
+ NOT_SUPPORTED(-1, "btrfs filesystems' UUID cannot be changed");
+
+ r = commandr (NULL, &err, str_btrfstune, "-f", "-U", uuid,
device, NULL);
+
+ if (r == -1) {
+ reply_with_error ("%s: %s", device, err);
+ return -1;
+ }
+
+ return 0;
+}
+
/* Takes optional arguments, consult optargs_bitmask. */
int
do_btrfs_fsck (const char *device, int64_t superblock, int repair)
diff --git a/daemon/daemon.h b/daemon/daemon.h
index fce7b57..f9e8d45 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -273,6 +273,7 @@ extern char *debug_bmap_device (const char *subcmd, size_t argc, char
*const *co
/*-- in btrfs.c --*/
extern char *btrfs_get_label (const char *device);
+extern int btrfs_set_uuid (const char *device, const char *uuid);
/*-- in ntfs.c --*/
extern char *ntfs_get_label (const char *device);
diff --git a/daemon/uuids.c b/daemon/uuids.c
index 7635e76..dce0d60 100644
--- a/daemon/uuids.c
+++ b/daemon/uuids.c
@@ -90,10 +90,8 @@ do_set_uuid (const char *device, const char *uuid)
else if (STREQ (vfs_type, "swap"))
r = swapuuid (device, uuid);
- else if (STREQ (vfs_type, "btrfs")) {
- reply_with_error ("btrfs filesystems' UUID cannot be changed");
- r = -1;
- }
+ else if (STREQ (vfs_type, "btrfs"))
+ r = btrfs_set_uuid (device, uuid);
else {
reply_with_error ("don't know how to set the UUID for '%s'
filesystems",
diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl
index 2fe7c59..b637b17 100755
--- a/tests/btrfs/test-btrfs-misc.pl
+++ b/tests/btrfs/test-btrfs-misc.pl
@@ -20,6 +20,7 @@
use strict;
use warnings;
+use Errno;
use Sys::Guestfs;
@@ -47,5 +48,24 @@ my $label = $g->vfs_label ("/dev/sda1");
die "unexpected label: expecting 'newlabel' but got '$label'"
unless $label eq "newlabel";
+# Setting btrfs UUID
+eval {
+ $g->set_uuid ("/dev/sda1",
"12345678-1234-1234-1234-123456789012");
+};
+
+if ($@) {
+ my $err = $g->last_errno ();
+ if ($err == Errno::ENOTSUP()) {
+ warn "$0: skipping test for btrfs UUID change feature is not
available";
+ } else {
+ die $@;
+ }
+} else {
+ my $uuid = $g->vfs_uuid ("/dev/sda1");
+ die "unexpected uuid expecting
+ '12345678-1234-1234-1234-123456789012' but got '$uuid'"
+ unless $uuid eq "12345678-1234-1234-1234-123456789012";
+}
+
$g->shutdown ();
$g->close ();
--
2.1.0