Signed-off-by: Chen Hanxiao <chenhanx...@cn.fujitsu.com> --- v5: 1. improve testcases 2. rename set_uuid_random to ext_set_uuid_random 3. drop swap_set_uuid_random, call swap_set_uuid + get_random_uuid
daemon/btrfs.c | 19 +++++++++++++++++++ daemon/daemon.h | 4 ++++ daemon/ext2.c | 6 ++++++ daemon/uuids.c | 32 ++++++++++++++++++++++++++++++++ daemon/xfs.c | 7 +++++++ generator/actions.ml | 16 ++++++++++++++++ src/MAX_PROC_NR | 2 +- tests/btrfs/test-btrfs-misc.pl | 15 +++++++++++++++ 8 files changed, 100 insertions(+), 1 deletion(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index 28a48cf..8fcfd81 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 f8441d1..377afbc 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_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..8c8bd94 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_e2uuid_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 5238f3e..002f595 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_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 (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 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, diff --git a/generator/actions.ml b/generator/actions.ml index b1865e0..b214c37 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12636,6 +12636,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 b637b17..fa778ab 100755 --- a/tests/btrfs/test-btrfs-misc.pl +++ b/tests/btrfs/test-btrfs-misc.pl @@ -67,5 +67,20 @@ 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 (); -- 2.1.0 _______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://www.redhat.com/mailman/listinfo/libguestfs