Chen Hanxiao
2015-Jun-26 07:00 UTC
[Libguestfs] [PATCH v3 0/4] uuid: add btrfs uuid change support and some rework
- Btrfs-progs v4.1 introduced new feature of changing uuid of btrfs partition. This patch add support of this. - uuids.c did a lot of deplicated work for changing uuid of fs. Use existing functions. v3: set errno if feature is not available. Chen Hanxiao (4): 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 daemon/btrfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++ daemon/daemon.h | 5 ++++ daemon/swap.c | 16 +++++++++++ daemon/uuids.c | 48 ++++----------------------------- daemon/xfs.c | 7 +++++ tests/btrfs/test-btrfs-misc.pl | 17 ++++++++++++ 6 files changed, 110 insertions(+), 43 deletions(-) -- 2.1.0
Chen Hanxiao
2015-Jun-26 07:00 UTC
[Libguestfs] [PATCH v3 1/4] 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: 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..80b7d80 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_btrftune_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_btrftune_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 07:00 UTC
[Libguestfs] [PATCH v3 2/4] 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 07:00 UTC
[Libguestfs] [PATCH v3 3/4] 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 07:00 UTC
[Libguestfs] [PATCH v3 4/4] 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:34 UTC
Re: [Libguestfs] [PATCH v3 0/4] uuid: add btrfs uuid change support and some rework
Hi, I found some typos in this series. I'll resend v3.1 soon with set_uuid_random feature. Please ignore this series. Regards, - Chen> -----Original Message----- > From: libguestfs-bounces@redhat.com [mailto:libguestfs-bounces@redhat.com] On > Behalf Of Chen Hanxiao > Sent: Friday, June 26, 2015 3:01 PM > To: libguestfs@redhat.com > Subject: [Libguestfs] [PATCH v3 0/4] uuid: add btrfs uuid change support and some > rework > > - Btrfs-progs v4.1 introduced new feature of changing > uuid of btrfs partition. > This patch add support of this. > > - uuids.c did a lot of deplicated work for changing uuid > of fs. Use existing functions. > > v3: set errno if feature is not available. > > Chen Hanxiao (4): > 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 > > daemon/btrfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++ > daemon/daemon.h | 5 ++++ > daemon/swap.c | 16 +++++++++++ > daemon/uuids.c | 48 ++++----------------------------- > daemon/xfs.c | 7 +++++ > tests/btrfs/test-btrfs-misc.pl | 17 ++++++++++++ > 6 files changed, 110 insertions(+), 43 deletions(-) > > -- > 2.1.0 > > _______________________________________________ > Libguestfs mailing list > Libguestfs@redhat.com > https://www.redhat.com/mailman/listinfo/libguestfs
Apparently Analagous Threads
- [PATCH v3.1 0/9] uuid: add btrfs uuid change support and set_uuid_random
- [PATCH v4 0/7] uuid: add btrfs uuid change support and set_uuid_random
- [PATCH v5 0/3] uuid: add btrfs uuid change support and set_uuid_random
- [PATCH 0/5] uuid: add btrfs uuid change support and some rework
- [PATCH] uuid: add support to change uuid of btrfs partition