[Libguestfs] [PATCH v4 6/7] daemon: add functions for setting random uuid of fs

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


Signed-off-by: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>
---
 daemon/btrfs.c  | 21 +++++++++++++++++++++
 daemon/daemon.h |  4 ++++
 daemon/ext2.c   | 15 +++++++++++++++
 daemon/swap.c   | 13 +++++++++++++
 daemon/xfs.c    |  7 +++++++
 5 files changed, 60 insertions(+)

diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index 4cdc6a7..2b0cae9 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -874,6 +874,27 @@ btrfs_set_uuid (const char *device, const char *uuid)
   return 0;
 }
 
+int
+btrfs_set_uuid_random (const char *device)
+{
+  CLEANUP_FREE char *err = NULL;
+  int r;
+  int has_uuid_opts = test_btrfstune_uuid_opt();
+
+  if (has_uuid_opts <= 0) {
+    reply_with_error_errno (ENOTSUP, "btrfs filesystems' UUID cannot be changed");
+    return -1;
+  }
+
+  r = commandr (NULL, &err, str_btrfstune, "-f", "-u", 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 f8441d1..45768bf 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -223,6 +223,7 @@ extern int sync_disks (void);
 /* Confirmed this is true up to ext4 from the Linux sources. */
 #define EXT2_LABEL_MAX 16
 extern int fstype_is_extfs (const char *fstype);
+extern int set_e2uuid_random (const char *device);
 
 /*-- in blkid.c --*/
 extern char *get_blkid_tag (const char *device, const char *tag);
@@ -265,6 +266,7 @@ extern int copy_xattrs (const char *src, const char *dest);
 /* Documented in xfs_admin(8). */
 #define XFS_LABEL_MAX 12
 extern int xfs_set_uuid (const char *device, const char *uuid);
+extern int xfs_set_uuid_random (const char *device);
 
 /*-- debug-bmap.c --*/
 extern char *debug_bmap (const char *subcmd, size_t argc, char *const *const argv);
@@ -274,12 +276,14 @@ 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);
+extern int btrfs_set_uuid_random (const char *device);
 
 /*-- in ntfs.c --*/
 extern char *ntfs_get_label (const char *device);
 
 /*-- in swap.c --*/
 extern int swap_set_uuid (const char *device, const char *uuid);
+extern int swap_set_uuid_random (const char *device);
 
 /* ordinary daemon functions use these to indicate errors
  * NB: you don't need to prefix the string with the current command,
diff --git a/daemon/ext2.c b/daemon/ext2.c
index 8ef6d5f..625e5ae 100644
--- a/daemon/ext2.c
+++ b/daemon/ext2.c
@@ -159,6 +159,21 @@ do_set_e2uuid (const char *device, const char *uuid)
   return 0;
 }
 
+int
+set_e2uuid_random (const char *device)
+{
+  int r;
+  CLEANUP_FREE char *err = NULL;
+
+  r = command (NULL, &err, str_tune2fs, "-U", "random", device, NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    return -1;
+  }
+
+  return 0;
+}
+
 char *
 do_get_e2uuid (const char *device)
 {
diff --git a/daemon/swap.c b/daemon/swap.c
index 26fe30d..84d9606 100644
--- a/daemon/swap.c
+++ b/daemon/swap.c
@@ -255,3 +255,16 @@ swap_set_uuid (const char *device, const char *uuid)
 
   return 0;
 }
+
+int
+swap_set_uuid_random (const char *device)
+{
+  CLEANUP_FREE char *err = NULL;
+  CLEANUP_FREE char *uuid_random = NULL;
+
+  uuid_random = get_random_uuid ();
+  if (uuid_random == NULL)
+    return -1;
+
+  return swap_set_uuid (device, uuid_random);
+}
diff --git a/daemon/xfs.c b/daemon/xfs.c
index fb7acb4..2c93311 100644
--- a/daemon/xfs.c
+++ b/daemon/xfs.c
@@ -463,6 +463,13 @@ xfs_set_uuid (const char *device, const char *uuid)
 }
 
 int
+xfs_set_uuid_random (const char *device)
+{
+  optargs_bitmask = GUESTFS_XFS_ADMIN_UUID_BITMASK;
+  return do_xfs_admin (device, 0, 0, 0, 0, 0, NULL, "generate");
+}
+
+int
 do_xfs_admin (const char *device,
               int extunwritten, int imgfile, int v2log,
               int projid32bit,
-- 
2.1.0




More information about the Libguestfs mailing list