Also introduce get_random_uuid() Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/daemon.h | 2 ++ daemon/swap.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/daemon/daemon.h b/daemon/daemon.h index 9c8476c..6c07c6a 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -278,6 +278,8 @@ 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); +extern char *get_random_uuid (void); /* 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/swap.c b/daemon/swap.c index 26fe30d..923adb2 100644 --- a/daemon/swap.c +++ b/daemon/swap.c @@ -34,6 +34,7 @@ GUESTFSD_EXT_CMD(str_mkswap, mkswap); GUESTFSD_EXT_CMD(str_swapon, swapon); GUESTFSD_EXT_CMD(str_swapoff, swapoff); GUESTFSD_EXT_CMD(str_swaplabel, swaplabel); +GUESTFSD_EXT_CMD(str_uuidgen, uuidgen); /* Confirmed this is true for Linux swap partitions from the Linux sources. */ #define SWAP_LABEL_MAX 16 @@ -255,3 +256,34 @@ swap_set_uuid (const char *device, const char *uuid) return 0; } + +char * +get_random_uuid (void) +{ + int r; + char *out; + CLEANUP_FREE char *err = NULL; + + r = command (&out, &err, str_uuidgen, NULL); + if (r == -1) { + reply_with_error ("%s", err); + return NULL; + } + + /* caller free */ + return out; + +} + +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); +} -- 2.1.0