diff --git a/daemon/btrfs.c b/daemon/btrfs.c index 28a48cf15..8fcfd819d 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -872,6 +872,25 @@ 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) + NOT_SUPPORTED(-1, "btrfs filesystems' UUID cannot be changed"); + + 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 f8441d113..a4a436183 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 ext_set_uuid_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,6 +276,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); +extern int btrfs_set_uuid_random (const char *device); /*-- in ntfs.c --*/ extern char *ntfs_get_label (const char *device); diff --git a/daemon/ext2.c b/daemon/ext2.c index 9142a38c8..e8d265521 100644 --- a/daemon/ext2.c +++ b/daemon/ext2.c @@ -159,6 +159,12 @@ do_set_e2uuid (const char *device, const char *uuid) return 0; } +int +ext_set_uuid_random (const char *device) +{ + return do_set_e2uuid (device, "random"); +} + char * do_get_e2uuid (const char *device) { diff --git a/daemon/uuids.c b/daemon/uuids.c index 5238f3ef9..20eabe336 100644 --- a/daemon/uuids.c +++ b/daemon/uuids.c @@ -85,3 +85,35 @@ 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; + + CLEANUP_FREE char *uuid_random = get_random_uuid (); + if (uuid_random == NULL) + return -1; + + if (fstype_is_extfs (vfs_type)) + r = ext_set_uuid_random (device); + + else if (STREQ (vfs_type, "xfs")) + r = xfs_set_uuid_random (device); + + else if (STREQ (vfs_type, "swap")) + r = swap_set_uuid (device, uuid_random); + + else if (STREQ (vfs_type, "btrfs")) + r = btrfs_set_uuid_random (device); + + else + NOT_SUPPORTED(-1, "don't know how to set the random UUID for '%s' filesystems", + vfs_type); + return r; +} diff --git a/daemon/xfs.c b/daemon/xfs.c index fb7acb4a6..2c93311e5 100644 --- a/daemon/xfs.c +++ b/daemon/xfs.c @@ -462,6 +462,13 @@ xfs_set_uuid (const char *device, const char *uuid) return do_xfs_admin (device, 0, 0, 0, 0, 0, NULL, 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, diff --git a/generator/actions.ml b/generator/actions.ml index db628ef1d..92f80d23c 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12636,6 +12636,25 @@ removed from the filesystem. The C needs to be same size or larger than the C. Devices which are currently mounted are never allowed to be used as the C." }; + { defaults with + name = "set_uuid_random"; added = (1, 29, 50); + 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 to a random UUID. +If this fails and the errno is ENOTSUP, +means that there is no support for changing the UUID +for the type of the specified filesystem. + +Only some filesystem types support setting UUIDs. + +To read the UUID on a filesystem, call C." }; + ] (* Non-API meta-commands available only in guestfish. diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 4930863bd..8d38505c1 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 b637b179c..0a8e942ba 100755 --- a/tests/btrfs/test-btrfs-misc.pl +++ b/tests/btrfs/test-btrfs-misc.pl @@ -67,5 +67,19 @@ if ($@) { unless $uuid eq "12345678-1234-1234-1234-123456789012"; } +# Setting btrfs random UUID. +eval { + $g->set_uuid_random ("/dev/sda1") +}; + +if ($@) { + my $err = $g->last_errno (); + if ($err == Errno::ENOTSUP()) { + warn "$0: skipping test for btrfs UUID change feature is not available"; + } else { + die $@; + } +} + $g->shutdown (); $g->close ();