Chen Hanxiao
2015-Jun-30 11:23 UTC
[Libguestfs] [PATCH v4 0/7] 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. v4: introduce get_random_uuid improve testcases squash internal API patches v3.1: fix typos v3: set errno if feature is not available. Chen Hanxiao (7): 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: add get_random_uuid daemon: add functions for setting random uuid of fs New API: set_uuid_random appliance/packagelist.in | 1 + daemon/btrfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++ daemon/daemon.h | 11 ++++++ daemon/ext2.c | 15 ++++++++ daemon/guestfsd.c | 19 ++++++++++ daemon/swap.c | 29 +++++++++++++++ daemon/uuids.c | 71 +++++++++++++++++------------------- daemon/xfs.c | 14 ++++++++ generator/actions.ml | 16 +++++++++ src/MAX_PROC_NR | 2 +- tests/btrfs/test-btrfs-misc.pl | 36 +++++++++++++++++++ 11 files changed, 255 insertions(+), 40 deletions(-) -- 2.1.0
Chen Hanxiao
2015-Jun-30 11:23 UTC
[Libguestfs] [PATCH v4 1/7] 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> --- v4: although btrfstune did not had '--help', pass it anyway improve testcases 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 | 21 +++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index cedea31..4cdc6a7 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -797,6 +797,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, "--help", 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) { @@ -814,6 +852,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 e977095..dd7798c 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); /*-- 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..0643eeb 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,25 @@ 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"); +}; + +my $err = $g->last_errno (); + +if ($@) { + if ($err == Errno::ENOTSUP()) { + warn "$0: skipping test for btrfs UUID change feature is not available"; + } elsif ($err) { + die $@; + } +} else { + 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"; +} + $g->shutdown (); $g->close (); -- 2.1.0
Chen Hanxiao
2015-Jun-30 11:23 UTC
[Libguestfs] [PATCH v4 2/7] 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-30 11:23 UTC
[Libguestfs] [PATCH v4 3/7] 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 dd7798c..cbe8ada 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -262,6 +262,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-30 11:23 UTC
[Libguestfs] [PATCH v4 4/7] 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 cbe8ada..d8a5e0b 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -276,6 +276,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-30 11:23 UTC
[Libguestfs] [PATCH v4 5/7] daemon: add get_random_uuid
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- appliance/packagelist.in | 1 + daemon/daemon.h | 2 ++ daemon/guestfsd.c | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/appliance/packagelist.in b/appliance/packagelist.in index 76c7293..d218a37 100644 --- a/appliance/packagelist.in +++ b/appliance/packagelist.in @@ -97,6 +97,7 @@ dnl iproute has been renamed to iproute2 vim-tiny xz-utils zfs-fuse + uuid-runtime ) ifelse(ARCHLINUX,1, diff --git a/daemon/daemon.h b/daemon/daemon.h index d8a5e0b..f8441d1 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -152,6 +152,8 @@ extern void udev_settle (void); extern int random_name (char *template); +extern char *get_random_uuid (void); + /* This just stops gcc from giving a warning about our custom printf * formatters %Q and %R. See guestfs(3)/EXTENDING LIBGUESTFS for more * info about these. In GCC 4.8.0 the warning is even harder to diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 584c685..7f4b2f2 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -57,6 +57,7 @@ #include "daemon.h" GUESTFSD_EXT_CMD(str_udevadm, udevadm); +GUESTFSD_EXT_CMD(str_uuidgen, uuidgen); #ifndef MAX # define MAX(a,b) ((a)>(b)?(a):(b)) @@ -1509,6 +1510,24 @@ udev_settle (void) fprintf (stderr, "warning: udevadm command failed\n"); } +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; + +} + /* Use by the CLEANUP_* macros. Do not call these directly. */ void cleanup_free (void *ptr) -- 2.1.0
Chen Hanxiao
2015-Jun-30 11:23 UTC
[Libguestfs] [PATCH v4 6/7] daemon: add functions for setting random uuid of fs
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/btrfs.c | 21 +++++++++++++++++++++ daemon/daemon.h | 4 ++++ daemon/ext2.c | 15 +++++++++++++++ daemon/swap.c | 13 +++++++++++++ daemon/xfs.c | 7 +++++++ 5 files changed, 60 insertions(+) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index 4cdc6a7..2b0cae9 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -874,6 +874,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 f8441d1..45768bf 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 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..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) { diff --git a/daemon/swap.c b/daemon/swap.c index 26fe30d..84d9606 100644 --- a/daemon/swap.c +++ b/daemon/swap.c @@ -255,3 +255,16 @@ swap_set_uuid (const char *device, const char *uuid) return 0; } + +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); +} 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 | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/daemon/uuids.c b/daemon/uuids.c index 5238f3e..cd5787f 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_errno (ENOTSUP, "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 372e50e..52404ab 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12623,6 +12623,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 0643eeb..224d075 100755 --- a/tests/btrfs/test-btrfs-misc.pl +++ b/tests/btrfs/test-btrfs-misc.pl @@ -68,5 +68,20 @@ if ($@) { unless $uuid eq "12345678-1234-1234-1234-123456789012"; } +# Setting btrfs random UUID. +eval { + $g->set_uuid_random ("/dev/sda1") +}; + +$err = $g->last_errno (); + +if ($@) { + if ($err == Errno::ENOTSUP()) { + warn "$0: skipping test for btrfs UUID change feature is not available"; + } elsif ($err) { + die $@; + } +} + $g->shutdown (); $g->close (); -- 2.1.0
Pino Toscano
2015-Jun-30 12:37 UTC
Re: [Libguestfs] [PATCH v4 1/7] uuid: add support to change uuid of btrfs partition
In data martedì 30 giugno 2015 19:23:08, 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> > --- > [...]> +# Setting btrfs UUID > +eval { > + $g->set_uuid ("/dev/sda1", "12345678-1234-1234-1234-123456789012"); > +}; > + > +my $err = $g->last_errno (); > + > +if ($@) {$@ contains the last Perl error, so it's better to check for it right after the eval block, otherwise other functions might change it. Also, $err can be moved in the if block.> + if ($err == Errno::ENOTSUP()) { > + warn "$0: skipping test for btrfs UUID change feature is not available"; > + } elsif ($err) {"else" is enough here, as $err will surely be different than zero here (otherwise we wouldn't be in this block). Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-30 14:04 UTC
Re: [Libguestfs] [PATCH v4 1/7] uuid: add support to change uuid of btrfs partition
Hi, In data martedì 30 giugno 2015 19:23:08, Chen Hanxiao ha scritto:> +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; > + }Forgot one thing here: you can use a NOT_SUPPORTED(-1, "btrfs ...") macro here. -- Pino Toscano
Pino Toscano
2015-Jun-30 14:05 UTC
Re: [Libguestfs] [PATCH v4 6/7] daemon: add functions for setting random uuid of fs
Hi, In data martedì 30 giugno 2015 19:23:13, Chen Hanxiao ha scritto:> Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> > --- > daemon/btrfs.c | 21 +++++++++++++++++++++ > daemon/daemon.h | 4 ++++ > daemon/ext2.c | 15 +++++++++++++++ > daemon/swap.c | 13 +++++++++++++ > daemon/xfs.c | 7 +++++++ > 5 files changed, 60 insertions(+) > > diff --git a/daemon/btrfs.c b/daemon/btrfs.c > index 4cdc6a7..2b0cae9 100644 > --- a/daemon/btrfs.c > +++ b/daemon/btrfs.c > @@ -874,6 +874,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; > + }You can use a NOT_SUPPORTED(-1, "btrfs ...") macro here.> + > + 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..45768bf 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 set_e2uuid_random (const char *device);I'd say ext_set_uuid_random, fits better the naming.> /*-- 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..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; > +}Just call do_set_e2uuid here.> char * > do_get_e2uuid (const char *device) > { > diff --git a/daemon/swap.c b/daemon/swap.c > index 26fe30d..84d9606 100644 > --- a/daemon/swap.c > +++ b/daemon/swap.c > @@ -255,3 +255,16 @@ swap_set_uuid (const char *device, const char *uuid) > > return 0; > } > + > +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); > +}There is no need for a separate _random of this, as this is basically get_random_uuid + set_uuid. Just inline them into do_set_uuid_random directly.> 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, >Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-30 14:06 UTC
Re: [Libguestfs] [PATCH v4 7/7] New API: set_uuid_random
In data martedì 30 giugno 2015 19:23:14, 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 | 15 +++++++++++++++ > 4 files changed, 63 insertions(+), 1 deletion(-) > > diff --git a/daemon/uuids.c b/daemon/uuids.c > index 5238f3e..cd5787f 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_errno (ENOTSUP, "don't know how to set the random UUID for '%s' filesystems", > + vfs_type);You can use a NOT_SUPPORTED(-1, "...") macro here.> + r = -1; > + } > + > + return r; > +} > diff --git a/generator/actions.ml b/generator/actions.ml > index 372e50e..52404ab 100644 > --- a/generator/actions.ml > +++ b/generator/actions.ml > @@ -12623,6 +12623,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 0643eeb..224d075 100755 > --- a/tests/btrfs/test-btrfs-misc.pl > +++ b/tests/btrfs/test-btrfs-misc.pl > @@ -68,5 +68,20 @@ if ($@) { > unless $uuid eq "12345678-1234-1234-1234-123456789012"; > } > > +# Setting btrfs random UUID. > +eval { > + $g->set_uuid_random ("/dev/sda1") > +}; > + > +$err = $g->last_errno (); > + > +if ($@) { > + if ($err == Errno::ENOTSUP()) { > + warn "$0: skipping test for btrfs UUID change feature is not available"; > + } elsif ($err) { > + die $@; > + } > +}Some note as in patch 1 about $@ and $err. Thanks, -- Pino Toscano
Pino Toscano
2015-Jun-30 14:09 UTC
Re: [Libguestfs] [PATCH v4 0/7] uuid: add btrfs uuid change support and set_uuid_random
In data martedì 30 giugno 2015 19:23:07, 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. > > v4: introduce get_random_uuid > improve testcases > squash internal API patches > v3.1: fix typos > v3: set errno if feature is not available. > > Chen Hanxiao (7): > 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: add get_random_uuid > daemon: add functions for setting random uuid of fs > New API: set_uuid_random > > appliance/packagelist.in | 1 + > daemon/btrfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++ > daemon/daemon.h | 11 ++++++ > daemon/ext2.c | 15 ++++++++ > daemon/guestfsd.c | 19 ++++++++++ > daemon/swap.c | 29 +++++++++++++++ > daemon/uuids.c | 71 +++++++++++++++++------------------- > daemon/xfs.c | 14 ++++++++ > generator/actions.ml | 16 +++++++++ > src/MAX_PROC_NR | 2 +- > tests/btrfs/test-btrfs-misc.pl | 36 +++++++++++++++++++ > 11 files changed, 255 insertions(+), 40 deletions(-)This patch series is getting better now. I pushed patches #2, #3 & #5, as they were okay. #4 is okay too, although it doesn't apply without #1. Also, please squash patches #6 and #7 together, as those implementations are for set_uuid_random. Thanks for the work, -- Pino Toscano
Chen, Hanxiao
2015-Jul-01 08:53 UTC
Re: [Libguestfs] [PATCH v4 0/7] 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: Tuesday, June 30, 2015 10:09 PM > To: libguestfs@redhat.com > Subject: Re: [Libguestfs] [PATCH v4 0/7] uuid: add btrfs uuid change support and > set_uuid_random > > In data martedì 30 giugno 2015 19:23:07, 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. > > > > v4: introduce get_random_uuid > > improve testcases > > squash internal API patches > > v3.1: fix typos > > v3: set errno if feature is not available. > > > > Chen Hanxiao (7): > > 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: add get_random_uuid > > daemon: add functions for setting random uuid of fs > > New API: set_uuid_random > > > > appliance/packagelist.in | 1 + > > daemon/btrfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++ > > daemon/daemon.h | 11 ++++++ > > daemon/ext2.c | 15 ++++++++ > > daemon/guestfsd.c | 19 ++++++++++ > > daemon/swap.c | 29 +++++++++++++++ > > daemon/uuids.c | 71 +++++++++++++++++------------------- > > daemon/xfs.c | 14 ++++++++ > > generator/actions.ml | 16 +++++++++ > > src/MAX_PROC_NR | 2 +- > > tests/btrfs/test-btrfs-misc.pl | 36 +++++++++++++++++++ > > 11 files changed, 255 insertions(+), 40 deletions(-) > > This patch series is getting better now. > I pushed patches #2, #3 & #5, as they were okay. #4 is okay too, > although it doesn't apply without #1. > > Also, please squash patches #6 and #7 together, as those > implementations are for set_uuid_random.Fine, although #6 + #7 is a little big.> > Thanks for the work,Thanks for your comments and patience. Regards, - Chen
Maybe Matching Threads
- [PATCH 1/3] uuid: add support to change uuid of swap partition
- [PATCH v4 3/7] uuid: use newly introduced xfs_set_uuid of xfs
- Re: [PATCH v3.1 7/9] New API: swap_set_uuid_random
- [PATCH v3.1 7/9] New API: swap_set_uuid_random
- [PATCH 4/5] uuid: use existed do_mkswap_U