New API: set_uuid_random

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
This commit is contained in:
Chen Hanxiao
2015-07-02 19:52:37 +08:00
committed by Pino Toscano
parent db9bd61585
commit a477e282db
8 changed files with 101 additions and 1 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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)
{

View File

@@ -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;
}

View File

@@ -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,

View File

@@ -12636,6 +12636,25 @@ 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, 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<device> 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<guestfs_vfs_uuid>." };
]
(* Non-API meta-commands available only in guestfish.

View File

@@ -1 +1 @@
455
456

View File

@@ -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 ();