[Libguestfs] [PATCH v4 7/7] New API: set_uuid_random

Chen Hanxiao chenhanxiao at cn.fujitsu.com
Tue Jun 30 11:23:14 UTC 2015


Signed-off-by: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>
---
 daemon/uuids.c                 | 31 +++++++++++++++++++++++++++++++
 generator/actions.ml           | 16 ++++++++++++++++
 src/MAX_PROC_NR                |  2 +-
 tests/btrfs/test-btrfs-misc.pl | 15 +++++++++++++++
 4 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/daemon/uuids.c b/daemon/uuids.c
index 5238f3e..cd5787f 100644
--- a/daemon/uuids.c
+++ b/daemon/uuids.c
@@ -85,3 +85,34 @@ do_set_uuid (const char *device, const char *uuid)
 
   return r;
 }
+
+int
+do_set_uuid_random (const char *device)
+{
+  int r;
+
+  /* How we set the UUID depends on the filesystem type. */
+  CLEANUP_FREE char *vfs_type = get_blkid_tag (device, "TYPE");
+  if (vfs_type == NULL)
+    return -1;
+
+  if (fstype_is_extfs (vfs_type))
+    r = set_e2uuid_random (device);
+
+  else if (STREQ (vfs_type, "xfs"))
+    r = xfs_set_uuid_random (device);
+
+  else if (STREQ (vfs_type, "swap"))
+    r = swap_set_uuid_random (device);
+
+  else if (STREQ (vfs_type, "btrfs"))
+    r = btrfs_set_uuid_random (device);
+
+  else {
+    reply_with_error_errno (ENOTSUP, "don't know how to set the random UUID for '%s' filesystems",
+                      vfs_type);
+    r = -1;
+  }
+
+  return r;
+}
diff --git a/generator/actions.ml b/generator/actions.ml
index 372e50e..52404ab 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -12623,6 +12623,22 @@ removed from the filesystem.
 The C<targetdev> needs to be same size or larger than the C<srcdev>. Devices
 which are currently mounted are never allowed to be used as the C<targetdev>." };
 
+  { defaults with
+    name = "set_uuid_random"; added = (1, 29, 48);
+    style = RErr, [Device "device"], [];
+    proc_nr = Some 456;
+    tests = [
+        InitBasicFS, Always, TestRun (
+            [["set_uuid_random"; "/dev/sda1"]]), [];
+      ];
+    shortdesc = "set a random UUID for the filesystem";
+    longdesc = "\
+Set the filesystem UUID on C<device> to a random UUID.
+
+Only some filesystem types support setting UUIDs.
+
+To read the UUID on a filesystem, call C<guestfs_vfs_uuid>." };
+
 ]
 
 (* Non-API meta-commands available only in guestfish.
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 4930863..8d38505 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-455
+456
diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl
index 0643eeb..224d075 100755
--- a/tests/btrfs/test-btrfs-misc.pl
+++ b/tests/btrfs/test-btrfs-misc.pl
@@ -68,5 +68,20 @@ if ($@) {
     unless $uuid eq "12345678-1234-1234-1234-123456789012";
 }
 
+# Setting btrfs random UUID.
+eval {
+    $g->set_uuid_random ("/dev/sda1")
+};
+
+$err = $g->last_errno ();
+
+if ($@) {
+    if ($err == Errno::ENOTSUP()) {
+        warn "$0: skipping test for btrfs UUID change feature is not available";
+    } elsif ($err) {
+        die $@;
+    }
+}
+
 $g->shutdown ();
 $g->close ();
-- 
2.1.0




More information about the Libguestfs mailing list