From b2f75a3168d2d84ecf76c2544e23514ac8cc13a3 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Mon, 22 Jul 2013 12:41:47 +0100
Subject: [PATCH] New API: Implement set-uuid for ext2/3/4 and XFS
(RHBZ#986877).
Also includes tests.
(cherry picked from commit 8580ef7d0f2aac2118fa7b4055f3eb28cd127093)
---
daemon/Makefile.am | 1 +
daemon/blkid.c | 2 +-
daemon/daemon.h | 3 ++
daemon/uuids.c | 101 +++++++++++++++++++++++++++++++++++++++++++++
generator/actions.ml | 23 ++++++++++-
po/POTFILES | 1 +
src/MAX_PROC_NR | 2 +-
tests/xfs/test-xfs-misc.pl | 7 ++++
8 files changed, 136 insertions(+), 4 deletions(-)
create mode 100644 daemon/uuids.c
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 7889aec..d077dab 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -168,6 +168,7 @@ guestfsd_SOURCES = \
upload.c \
utimens.c \
utsname.c \
+ uuids.c \
wc.c \
xattr.c \
xfs.c \
diff --git a/daemon/blkid.c b/daemon/blkid.c
index 01ee044..d52f6c5 100644
--- a/daemon/blkid.c
+++ b/daemon/blkid.c
@@ -29,7 +29,7 @@
GUESTFSD_EXT_CMD(str_blkid, blkid);
-static char *
+char *
get_blkid_tag (const char *device, const char *tag)
{
char *out;
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 9a32bea..c93d620 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -209,6 +209,9 @@ extern int sync_disks (void);
#define EXT2_LABEL_MAX 16
extern int fstype_is_extfs (const char *fstype);
+/*-- in blkid.c --*/
+extern char *get_blkid_tag (const char *device, const char *tag);
+
/*-- in lvm.c --*/
extern int lv_canonical (const char *device, char **ret);
diff --git a/daemon/uuids.c b/daemon/uuids.c
new file mode 100644
index 0000000..672f3db
--- /dev/null
+++ b/daemon/uuids.c
@@ -0,0 +1,101 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2013 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "daemon.h"
+#include "actions.h"
+#include "optgroups.h"
+
+GUESTFSD_EXT_CMD(str_tune2fs, tune2fs);
+GUESTFSD_EXT_CMD(str_xfs_admin, xfs_admin);
+
+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.
+ */
+ if (STREQ (uuid, "clear") || STREQ (uuid, "random") ||
+ STREQ (uuid, "time")) {
+ reply_with_error ("e2: invalid new 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;
+}
+
+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;
+}
+
+int
+do_set_uuid (const char *device, const char *uuid)
+{
+ 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 = e2uuid (device, uuid);
+
+ else if (STREQ (vfs_type, "xfs"))
+ r = xfsuuid (device, uuid);
+
+ else {
+ reply_with_error ("don't know how to set the UUID for '%s' filesystems",
+ vfs_type);
+ r = -1;
+ }
+
+ return r;
+}
diff --git a/generator/actions.ml b/generator/actions.ml
index eb09aa7..3b6e098 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -4742,6 +4742,7 @@ C<device>." };
name = "set_e2uuid";
style = RErr, [Device "device"; String "uuid"], [];
proc_nr = Some 82;
+ deprecated_by = Some "set_uuid";
tests =
(let uuid = uuidgen () in [
InitBasicFS, Always, TestResultString (
@@ -4764,8 +4765,8 @@ C<device> to C<uuid>. The format of the UUID and alternatives
such as C<clear>, C<random> and C<time> are described in the
L<tune2fs(8)> manpage.
-You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
-to return the existing UUID of a filesystem." };
+You can use C<guestfs_vfs_uuid> to return the existing UUID
+of a filesystem." };
{ defaults with
name = "get_e2uuid";
@@ -11300,6 +11301,24 @@ is useful when you don't want to preserve permissions, because
the target filesystem does not support it (primarily when
writing to DOS FAT filesystems)." };
+ { defaults with
+ name = "set_uuid";
+ style = RErr, [Device "device"; String "uuid"], [];
+ proc_nr = Some 403;
+ tests =
+ (let uuid = uuidgen () in [
+ InitBasicFS, Always, TestResultString (
+ [["set_uuid"; "/dev/sda1"; uuid];
+ ["vfs_uuid"; "/dev/sda1"]], uuid), [];
+ ]);
+ shortdesc = "set the filesystem UUID";
+ longdesc = "\
+Set the filesystem UIUD on C<device> to C<label>.
+
+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/po/POTFILES b/po/POTFILES
index e2e63a2..a88707a 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -92,6 +92,7 @@ daemon/umask.c
daemon/upload.c
daemon/utimens.c
daemon/utsname.c
+daemon/uuids.c
daemon/wc.c
daemon/xattr.c
daemon/xfs.c
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 066cbfe..e1a29c1 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-401
+403
diff --git a/tests/xfs/test-xfs-misc.pl b/tests/xfs/test-xfs-misc.pl
index 4036708..1d09474 100755
--- a/tests/xfs/test-xfs-misc.pl
+++ b/tests/xfs/test-xfs-misc.pl
@@ -46,5 +46,12 @@ my $label = $g->vfs_label ("/dev/sda1");
die "unexpected label: expecting 'newlabel' but got '$label'"
unless $label eq "newlabel";
+# Setting UUID.
+my $newuuid = "01234567-0123-0123-0123-0123456789ab";
+$g->set_uuid ("/dev/sda1", $newuuid);
+my $uuid = $g->vfs_uuid ("/dev/sda1");
+die "unexpected UUID: expecting '$newuuid' but got '$uuid'"
+ unless $uuid eq $newuuid;
+
$g->shutdown ();
$g->close ();
--
1.8.3.1