Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 0/9] uuid: add btrfs uuid change support and set_uuid_random
- Btrfs-progs v4.1 introduced new feature of changing uuid of btrfs partition. This patch add support of this. - Introduce set_uuid_random - uuids.c did a lot of deplicated work for changing uuid of fs. Use existing functions. v3.1: fix typos v3: set errno if feature is not available. Chen Hanxiao (9): uuid: add support to change uuid of btrfs partition uuid: use existing function of ext2 uuid: use newly introduced xfs_set_uuid of xfs uuid: use newly introduced swap_set_uuid New API: btrfs_set_uuid_random New API: set_e2uuid_random New API: swap_set_uuid_random New API: xfs_set_uuid_random New API: set_uuid_random daemon/btrfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++ daemon/daemon.h | 10 ++++++ daemon/ext2.c | 15 ++++++++ daemon/swap.c | 48 +++++++++++++++++++++++++ daemon/uuids.c | 71 +++++++++++++++++------------------- daemon/xfs.c | 14 ++++++++ generator/actions.ml | 16 +++++++++ src/MAX_PROC_NR | 2 +- tests/btrfs/test-btrfs-misc.pl | 28 +++++++++++++++ 9 files changed, 245 insertions(+), 40 deletions(-) -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 1/9] uuid: add support to change uuid of btrfs partition
btrfs-progs v4.1 add support to change uuid of btrfs fs. Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- v3.1: fix a typo v3: set errno as ENOTSUP when btrfstune -u is not available v2: put btrfs operation back to daemon/btrfs.c move tests to tests/btrfs daemon/btrfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++ daemon/daemon.h | 1 + daemon/uuids.c | 6 ++--- tests/btrfs/test-btrfs-misc.pl | 17 ++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index 20e5e6b..a69c512 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -790,6 +790,44 @@ do_btrfs_device_delete (char *const *devices, const char *fs) return 0; } + +/* btrfstune command add two new options + * -U UUID change fsid to UUID + * -u change fsid, use a random one + * since v4.1 + * We could check wheter 'btrfstune' support + * '-u' and '-U UUID' option by checking the output of + * 'btrfstune' command. + */ +static int +test_btrfstune_uuid_opt (void) +{ + static int result = -1; + if (result != -1) + return result; + + CLEANUP_FREE char *err = NULL; + + int r = commandr (NULL, &err, str_btrfstune, NULL); + + if (r == -1) { + reply_with_error ("btrfstune: %s", err); + return -1; + } + + /* FIXME currently btrfstune do not support '--help'. + * If got an invalid options, it will print its usage + * in stderr. + * We had to check it there. + */ + if (strstr (err, "-U") == NULL || strstr (err, "-u") == NULL) + result = 0; + else + result = 1; + + return result; +} + int do_btrfs_set_seeding (const char *device, int svalue) { @@ -807,6 +845,28 @@ do_btrfs_set_seeding (const char *device, int svalue) return 0; } +int +btrfs_set_uuid (const char *device, const char *uuid) +{ + 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", uuid, 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 136e9a9..b74ba3c 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -268,6 +268,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); /*-- in ntfs.c --*/ extern char *ntfs_get_label (const char *device); diff --git a/daemon/uuids.c b/daemon/uuids.c index 06b33e9..f98d8e5 100644 --- a/daemon/uuids.c +++ b/daemon/uuids.c @@ -110,10 +110,8 @@ do_set_uuid (const char *device, const char *uuid) else if (STREQ (vfs_type, "swap")) r = swapuuid (device, uuid); - else if (STREQ (vfs_type, "btrfs")) { - reply_with_error ("btrfs filesystems' UUID cannot be changed"); - r = -1; - } + else if (STREQ (vfs_type, "btrfs")) + r = btrfs_set_uuid (device, uuid); else { reply_with_error ("don't know how to set the UUID for '%s' filesystems", diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl index 2fe7c59..b47caab 100755 --- a/tests/btrfs/test-btrfs-misc.pl +++ b/tests/btrfs/test-btrfs-misc.pl @@ -20,6 +20,7 @@ use strict; use warnings; +use Errno; use Sys::Guestfs; @@ -47,5 +48,21 @@ my $label = $g->vfs_label ("/dev/sda1"); die "unexpected label: expecting 'newlabel' but got '$label'" unless $label eq "newlabel"; +# Setting btrfs UUID +eval { + $g->set_uuid ("/dev/sda1", "12345678-1234-1234-1234-123456789012"); +}; +# FIXME: ignore ESRCH +my $err = $g->last_errno (); + +if ($err == 0) { + my $uuid = $g->vfs_uuid ("/dev/sda1"); + die "unexpected uuid expecting + '12345678-1234-1234-1234-123456789012' but got '$uuid'" + unless $uuid eq "12345678-1234-1234-1234-123456789012"; +} elsif ($err == Errno::ENOTSUP()) { + warn "$0: skipping test for btrfs UUID change feature is not available\n"; +} + $g->shutdown (); $g->close (); -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 2/9] uuid: use existing function of ext2
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- v3: improve commit log daemon/uuids.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/daemon/uuids.c b/daemon/uuids.c index f98d8e5..b8b820a 100644 --- a/daemon/uuids.c +++ b/daemon/uuids.c @@ -27,16 +27,12 @@ #include "actions.h" #include "optgroups.h" -GUESTFSD_EXT_CMD(str_tune2fs, tune2fs); GUESTFSD_EXT_CMD(str_xfs_admin, xfs_admin); GUESTFSD_EXT_CMD(str_swaplabel, swaplabel); static int e2uuid (const char *device, const char *uuid) { - int r; - CLEANUP_FREE char *err = NULL; - /* Don't allow the magic values here. If callers want to do this * we'll add alternate set_uuid_* calls. */ @@ -46,13 +42,7 @@ e2uuid (const char *device, const char *uuid) return -1; } - r = command (NULL, &err, str_tune2fs, "-U", uuid, device, NULL); - if (r == -1) { - reply_with_error ("%s", err); - return -1; - } - - return 0; + return do_set_e2uuid (device, uuid); } static int -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 3/9] uuid: use newly introduced xfs_set_uuid of xfs
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- v3: rename do_xfs_admin_uuid to xfs_set_uuid daemon/daemon.h | 1 + daemon/uuids.c | 12 +----------- daemon/xfs.c | 7 +++++++ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/daemon/daemon.h b/daemon/daemon.h index b74ba3c..7d5691f 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -260,6 +260,7 @@ extern int copy_xattrs (const char *src, const char *dest); /*-- in xfs.c --*/ /* Documented in xfs_admin(8). */ #define XFS_LABEL_MAX 12 +extern int xfs_set_uuid (const char *device, const char *uuid); /*-- debug-bmap.c --*/ extern char *debug_bmap (const char *subcmd, size_t argc, char *const *const argv); diff --git a/daemon/uuids.c b/daemon/uuids.c index b8b820a..dce0d60 100644 --- a/daemon/uuids.c +++ b/daemon/uuids.c @@ -27,7 +27,6 @@ #include "actions.h" #include "optgroups.h" -GUESTFSD_EXT_CMD(str_xfs_admin, xfs_admin); GUESTFSD_EXT_CMD(str_swaplabel, swaplabel); static int @@ -48,22 +47,13 @@ e2uuid (const char *device, const char *uuid) static int xfsuuid (const char *device, const char *uuid) { - int r; - CLEANUP_FREE char *err = NULL; - /* Don't allow special values. */ if (STREQ (uuid, "nil") || STREQ (uuid, "generate")) { reply_with_error ("xfs: invalid new UUID"); return -1; } - r = command (NULL, &err, str_xfs_admin, "-U", uuid, device, NULL); - if (r == -1) { - reply_with_error ("%s", err); - return -1; - } - - return 0; + return xfs_set_uuid (device, uuid); } static int diff --git a/daemon/xfs.c b/daemon/xfs.c index 687013b..fb7acb4 100644 --- a/daemon/xfs.c +++ b/daemon/xfs.c @@ -456,6 +456,13 @@ do_xfs_growfs (const char *path, } int +xfs_set_uuid (const char *device, const char *uuid) +{ + optargs_bitmask = GUESTFS_XFS_ADMIN_UUID_BITMASK; + return do_xfs_admin (device, 0, 0, 0, 0, 0, NULL, uuid); +} + +int do_xfs_admin (const char *device, int extunwritten, int imgfile, int v2log, int projid32bit, -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 4/9] uuid: use newly introduced swap_set_uuid
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- v3: move swapuuid to swap.c rename it as swap_set_uuid daemon/daemon.h | 3 +++ daemon/swap.c | 16 ++++++++++++++++ daemon/uuids.c | 18 +----------------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/daemon/daemon.h b/daemon/daemon.h index 7d5691f..d4c8c11 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -274,6 +274,9 @@ extern int btrfs_set_uuid (const char *device, const char *uuid); /*-- 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); + /* ordinary daemon functions use these to indicate errors * NB: you don't need to prefix the string with the current command, * it is added automatically by the client-side RPC stubs. diff --git a/daemon/swap.c b/daemon/swap.c index 83f4d7c..26fe30d 100644 --- a/daemon/swap.c +++ b/daemon/swap.c @@ -33,6 +33,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); /* Confirmed this is true for Linux swap partitions from the Linux sources. */ #define SWAP_LABEL_MAX 16 @@ -239,3 +240,18 @@ do_swapoff_uuid (const char *uuid) { return swaponoff (str_swapoff, "-U", uuid); } + +int +swap_set_uuid (const char *device, const char *uuid) +{ + int r; + CLEANUP_FREE char *err = NULL; + + r = command (NULL, &err, str_swaplabel, "-U", uuid, device, NULL); + if (r == -1) { + reply_with_error ("%s", err); + return -1; + } + + return 0; +} diff --git a/daemon/uuids.c b/daemon/uuids.c index dce0d60..5238f3e 100644 --- a/daemon/uuids.c +++ b/daemon/uuids.c @@ -27,7 +27,6 @@ #include "actions.h" #include "optgroups.h" -GUESTFSD_EXT_CMD(str_swaplabel, swaplabel); static int e2uuid (const char *device, const char *uuid) @@ -56,21 +55,6 @@ xfsuuid (const char *device, const char *uuid) return xfs_set_uuid (device, uuid); } -static int -swapuuid (const char *device, const char *uuid) -{ - int r; - CLEANUP_FREE char *err = NULL; - - r = command (NULL, &err, str_swaplabel, "-U", uuid, device, NULL); - if (r == -1) { - reply_with_error ("%s", err); - return -1; - } - - return 0; -} - int do_set_uuid (const char *device, const char *uuid) { @@ -88,7 +72,7 @@ do_set_uuid (const char *device, const char *uuid) r = xfsuuid (device, uuid); else if (STREQ (vfs_type, "swap")) - r = swapuuid (device, uuid); + r = swap_set_uuid (device, uuid); else if (STREQ (vfs_type, "btrfs")) r = btrfs_set_uuid (device, uuid); -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 5/9] New API: btrfs_set_uuid_random
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/btrfs.c | 21 +++++++++++++++++++++ daemon/daemon.h | 1 + 2 files changed, 22 insertions(+) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index a69c512..a3f3590 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -867,6 +867,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 d4c8c11..5087327 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -270,6 +270,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); -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 6/9] New API: set_e2uuid_random
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/daemon.h | 1 + daemon/ext2.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/daemon/daemon.h b/daemon/daemon.h index 5087327..9c8476c 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -219,6 +219,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); 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) { -- 2.1.0
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 7/9] New API: swap_set_uuid_random
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
Chen Hanxiao
2015-Jun-26 09:35 UTC
[Libguestfs] [PATCH v3.1 8/9] New API: xfs_set_uuid_random
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/daemon.h | 1 + daemon/xfs.c | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/daemon/daemon.h b/daemon/daemon.h index 6c07c6a..6d98eb9 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -262,6 +262,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); 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
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/uuids.c | 31 +++++++++++++++++++++++++++++++ generator/actions.ml | 16 ++++++++++++++++ src/MAX_PROC_NR | 2 +- tests/btrfs/test-btrfs-misc.pl | 11 +++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/daemon/uuids.c b/daemon/uuids.c index 5238f3e..4530491 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 ("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 d5e5ccf..0328858 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12593,6 +12593,22 @@ numbered C<partnum> on device C<device>. It returns C<primary>, C<logical>, or C<extended>." }; + { defaults with + name = "set_uuid_random"; added = (1, 29, 47); + style = RErr, [Device "device"], []; + proc_nr = Some 455; + tests = [ + InitBasicFS, Always, TestRun ( + [["set_uuid_random"; "/dev/sda1"]]), []; + ]; + shortdesc = "set the random filesystem UUID"; + 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 515f19a..4930863 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -454 +455 diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl index b47caab..4982f0d 100755 --- a/tests/btrfs/test-btrfs-misc.pl +++ b/tests/btrfs/test-btrfs-misc.pl @@ -64,5 +64,16 @@ if ($err == 0) { warn "$0: skipping test for btrfs UUID change feature is not available\n"; } +# Setting btrfs random UUID. +eval { + $g->set_uuid_random ("/dev/sda1") +}; +# FIXME: ignore ESRCH +my $err = $g->last_errno (); + +if ($err == Errno::ENOTSUP()) { + warn "$0: skipping test for btrfs UUID change feature is not available\n"; +} + $g->shutdown (); $g->close (); -- 2.1.0
Pino Toscano
2015-Jun-26 12:12 UTC
Re: [Libguestfs] [PATCH v3.1 1/9] uuid: add support to change uuid of btrfs partition
In data venerdì 26 giugno 2015 17:35:36, Chen Hanxiao ha scritto:> btrfs-progs v4.1 add support to change uuid of btrfs fs. > > Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> > --- > v3.1: fix a typo > v3: set errno as ENOTSUP when btrfstune -u is not available > v2: put btrfs operation back to daemon/btrfs.c > move tests to tests/btrfs > > daemon/btrfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++ > daemon/daemon.h | 1 + > daemon/uuids.c | 6 ++--- > tests/btrfs/test-btrfs-misc.pl | 17 ++++++++++++ > 4 files changed, 80 insertions(+), 4 deletions(-) > > diff --git a/daemon/btrfs.c b/daemon/btrfs.c > index 20e5e6b..a69c512 100644 > --- a/daemon/btrfs.c > +++ b/daemon/btrfs.c > @@ -790,6 +790,44 @@ do_btrfs_device_delete (char *const *devices, const char *fs) > return 0; > } > > + > +/* btrfstune command add two new options > + * -U UUID change fsid to UUID > + * -u change fsid, use a random one > + * since v4.1 > + * We could check wheter 'btrfstune' support > + * '-u' and '-U UUID' option by checking the output of > + * 'btrfstune' command. > + */ > +static int > +test_btrfstune_uuid_opt (void) > +{ > + static int result = -1; > + if (result != -1) > + return result; > + > + CLEANUP_FREE char *err = NULL; > + > + int r = commandr (NULL, &err, str_btrfstune, NULL); > + > + if (r == -1) { > + reply_with_error ("btrfstune: %s", err); > + return -1; > + } > + > + /* FIXME currently btrfstune do not support '--help'.So --help should be passed anyway to the btrfstune invocation, so with newer versions hopefully there will not be complaints about it anymore.> + * If got an invalid options, it will print its usage > + * in stderr. > + * We had to check it there. > + */ > + if (strstr (err, "-U") == NULL || strstr (err, "-u") == NULL) > + result = 0; > + else > + result = 1; > + > + return result; > +} > + > int > do_btrfs_set_seeding (const char *device, int svalue) > { > @@ -807,6 +845,28 @@ do_btrfs_set_seeding (const char *device, int svalue) > return 0; > } > > +int > +btrfs_set_uuid (const char *device, const char *uuid) > +{ > + 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", uuid, 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 136e9a9..b74ba3c 100644 > --- a/daemon/daemon.h > +++ b/daemon/daemon.h > @@ -268,6 +268,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); > > /*-- in ntfs.c --*/ > extern char *ntfs_get_label (const char *device); > diff --git a/daemon/uuids.c b/daemon/uuids.c > index 06b33e9..f98d8e5 100644 > --- a/daemon/uuids.c > +++ b/daemon/uuids.c > @@ -110,10 +110,8 @@ do_set_uuid (const char *device, const char *uuid) > else if (STREQ (vfs_type, "swap")) > r = swapuuid (device, uuid); > > - else if (STREQ (vfs_type, "btrfs")) { > - reply_with_error ("btrfs filesystems' UUID cannot be changed"); > - r = -1; > - } > + else if (STREQ (vfs_type, "btrfs")) > + r = btrfs_set_uuid (device, uuid); > > else { > reply_with_error ("don't know how to set the UUID for '%s' filesystems", > diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl > index 2fe7c59..b47caab 100755 > --- a/tests/btrfs/test-btrfs-misc.pl > +++ b/tests/btrfs/test-btrfs-misc.pl > @@ -20,6 +20,7 @@ > > use strict; > use warnings; > +use Errno; > > use Sys::Guestfs; > > @@ -47,5 +48,21 @@ my $label = $g->vfs_label ("/dev/sda1"); > die "unexpected label: expecting 'newlabel' but got '$label'" > unless $label eq "newlabel"; > > +# Setting btrfs UUID > +eval { > + $g->set_uuid ("/dev/sda1", "12345678-1234-1234-1234-123456789012"); > +}; > +# FIXME: ignore ESRCHWhat is ESRCH about?> +my $err = $g->last_errno (); > + > +if ($err == 0) { > + my $uuid = $g->vfs_uuid ("/dev/sda1"); > + die "unexpected uuid expecting > + '12345678-1234-1234-1234-123456789012' but got '$uuid'" > + unless $uuid eq "12345678-1234-1234-1234-123456789012"; > +} elsif ($err == Errno::ENOTSUP()) { > + warn "$0: skipping test for btrfs UUID change feature is not available\n"; > +}IIRC 'warn' prints the newline at the end already. This is missing to check when $err is a valid error. Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-26 13:00 UTC
Re: [Libguestfs] [PATCH v3.1 7/9] New API: swap_set_uuid_random
In data venerdì 26 giugno 2015 17:35:42, Chen Hanxiao ha scritto:> 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; > + > +}The addition of the get_random_uuid() is worth an own commit. Also, it is not specific to swap, so I'd say to put it directly in guestfsd.c, named like uuidgen() (much like the OCaml equivalent in Common_utils). Furthermore, you should make sure uuidgen is actually in the appliance: while in Fedora is in util-linux (already part of the appliance), e.g. in Debian/Ubuntu is in uuid-runtime. Thus you need to add that package to the Debian ones in appliance/packagelist.in.> +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); > +}This is nothing specific for swap, and could just be done directly in the set_uuid_random implementation, for all the filesystems we have tools to change their UUIDs (so swap only so far). In practice something like: else if (STREQ (vfs_type, "swap")) { CLEANUP_FREE char *uuid = uuidgen (); if (uuid == NULL) return -1; r = swap_set_uuid (device, uuid); } Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-26 13:09 UTC
Re: [Libguestfs] [PATCH v3.1 9/9] New API: set_uuid_random
In data venerdì 26 giugno 2015 17:35:44, Chen Hanxiao ha scritto:> Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> > --- > daemon/uuids.c | 31 +++++++++++++++++++++++++++++++ > generator/actions.ml | 16 ++++++++++++++++ > src/MAX_PROC_NR | 2 +- > tests/btrfs/test-btrfs-misc.pl | 11 +++++++++++ > 4 files changed, 59 insertions(+), 1 deletion(-) > > diff --git a/daemon/uuids.c b/daemon/uuids.c > index 5238f3e..4530491 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 ("don't know how to set the random UUID for '%s' filesystems", > + vfs_type); > + r = -1;Being a new API, we could make this return ENOTSUP, documenting this error on unsupported filesystems. This way, users could know an error is due to a filesystem not supported by this API, instead of a real failure of one of the programs used.> + } > + > + return r; > +} > diff --git a/generator/actions.ml b/generator/actions.ml > index d5e5ccf..0328858 100644 > --- a/generator/actions.ml > +++ b/generator/actions.ml > @@ -12593,6 +12593,22 @@ numbered C<partnum> on device C<device>. > > It returns C<primary>, C<logical>, or C<extended>." }; > > + { defaults with > + name = "set_uuid_random"; added = (1, 29, 47); > + style = RErr, [Device "device"], []; > + proc_nr = Some 455; > + tests = [ > + InitBasicFS, Always, TestRun ( > + [["set_uuid_random"; "/dev/sda1"]]), []; > + ]; > + shortdesc = "set the random filesystem UUID";This sounds like it is setting some particular random UUID. I'd be better as "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 515f19a..4930863 100644 > --- a/src/MAX_PROC_NR > +++ b/src/MAX_PROC_NR > @@ -1 +1 @@ > -454 > +455 > diff --git a/tests/btrfs/test-btrfs-misc.pl b/tests/btrfs/test-btrfs-misc.pl > index b47caab..4982f0d 100755 > --- a/tests/btrfs/test-btrfs-misc.pl > +++ b/tests/btrfs/test-btrfs-misc.pl > @@ -64,5 +64,16 @@ if ($err == 0) { > warn "$0: skipping test for btrfs UUID change feature is not available\n"; > } > > +# Setting btrfs random UUID. > +eval { > + $g->set_uuid_random ("/dev/sda1") > +}; > +# FIXME: ignore ESRCHWhat is ESRCH about?> +my $err = $g->last_errno (); > + > +if ($err == Errno::ENOTSUP()) { > + warn "$0: skipping test for btrfs UUID change feature is not available\n"; > +}You need to care in case $err is non-zero, that is a real error. Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-26 13:11 UTC
Re: [Libguestfs] [PATCH v3.1 0/9] uuid: add btrfs uuid change support and set_uuid_random
In data venerdì 26 giugno 2015 17:35:35, Chen Hanxiao ha scritto:> - Btrfs-progs v4.1 introduced new feature of changing > uuid of btrfs partition. > This patch add support of this. > > - Introduce set_uuid_random > > - uuids.c did a lot of deplicated work for changing uuid > of fs. Use existing functions. > > v3.1: fix typos > v3: set errno if feature is not available. > > Chen Hanxiao (9): > [...] > uuid: use existing function of ext2 > uuid: use newly introduced xfs_set_uuid of xfs > uuid: use newly introduced swap_set_uuidThese commits look good.> New API: btrfs_set_uuid_random > New API: set_e2uuid_random > New API: swap_set_uuid_random > New API: xfs_set_uuid_random > New API: set_uuid_randomNew internal functions are not "new API" -- that is used for user-facing APIs, such as set_uuid_random. Could you also please squash them together, since they are not really different changes from the set_uuid_random addition? Commits 5,6,8 look good as well. Thanks, -- Pino Toscano
Chen, Hanxiao
2015-Jun-29 03:48 UTC
Re: [Libguestfs] [PATCH v3.1 0/9] uuid: add btrfs uuid change support and set_uuid_random
> -----Original Message----- > From: libguestfs-bounces@redhat.com [mailto:libguestfs-bounces@redhat.com] On > Behalf Of Pino Toscano > Sent: Friday, June 26, 2015 9:11 PM > To: libguestfs@redhat.com > Subject: Re: [Libguestfs] [PATCH v3.1 0/9] uuid: add btrfs uuid change support and > set_uuid_random > > In data venerdì 26 giugno 2015 17:35:35, Chen Hanxiao ha scritto: > > - Btrfs-progs v4.1 introduced new feature of changing > > uuid of btrfs partition. > > This patch add support of this. > > > > - Introduce set_uuid_random > > > > - uuids.c did a lot of deplicated work for changing uuid > > of fs. Use existing functions. > > > > v3.1: fix typos > > v3: set errno if feature is not available. > > > > Chen Hanxiao (9): > > [...] > > uuid: use existing function of ext2 > > uuid: use newly introduced xfs_set_uuid of xfs > > uuid: use newly introduced swap_set_uuid > > These commits look good. > > > New API: btrfs_set_uuid_random > > New API: set_e2uuid_random > > New API: swap_set_uuid_random > > New API: xfs_set_uuid_random > > New API: set_uuid_random > > New internal functions are not "new API" -- that is used for user-facing > APIs, such as set_uuid_random. > > Could you also please squash them together, since they are not really > different changes from the set_uuid_random addition? > > Commits 5,6,8 look good as well.I'll squash all internal api into one patch and resend. Regards, - Chen
Seemingly Similar Threads
- Re: [PATCH v3.1 7/9] New API: swap_set_uuid_random
- [PATCH v4 6/7] daemon: add functions for setting random uuid of fs
- [PATCH v3.1 0/9] uuid: add btrfs uuid change support and set_uuid_random
- [PATCH v5 3/3] New API: set_uuid_random
- [PATCH v6] New API: set_uuid_random