Blame SOURCES/0004-New-API-Implement-set-uuid-for-ext2-3-4-and-XFS-RHBZ.patch

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