mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0094-New-APIs-part-set-gpt-guid-and-part-get-gpt-guid.patch

ffd6ed
From 3aab860529f7d13b00b3689fa79c72b12d2f9770 Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Thu, 5 Feb 2015 08:08:06 +0000
ffd6ed
Subject: [PATCH] New APIs: part-set-gpt-guid and part-get-gpt-guid
ffd6ed
ffd6ed
In GPT, each partition has a GUID assigned randomly.  Allow this GUID
ffd6ed
to be written and read.
ffd6ed
ffd6ed
Note this is different from the GUID type code which is used to
ffd6ed
identify the type of the partition.
ffd6ed
ffd6ed
(cherry picked from commit 40c133b2c81666f6dde43704e66bf59206d5c111)
ffd6ed
---
ffd6ed
 daemon/parted.c      | 33 +++++++++++++++++++++++++++++++++
ffd6ed
 generator/actions.ml | 36 ++++++++++++++++++++++++++++++++++++
ffd6ed
 src/MAX_PROC_NR      |  2 +-
ffd6ed
 3 files changed, 70 insertions(+), 1 deletion(-)
ffd6ed
ffd6ed
diff --git a/daemon/parted.c b/daemon/parted.c
ffd6ed
index 76c0ce9..36844b8 100644
ffd6ed
--- a/daemon/parted.c
ffd6ed
+++ b/daemon/parted.c
ffd6ed
@@ -784,6 +784,32 @@ do_part_set_gpt_type (const char *device, int partnum, const char *guid)
ffd6ed
   return 0;
ffd6ed
 }
ffd6ed
 
ffd6ed
+int
ffd6ed
+do_part_set_gpt_guid (const char *device, int partnum, const char *guid)
ffd6ed
+{
ffd6ed
+  if (partnum <= 0) {
ffd6ed
+    reply_with_error ("partition number must be >= 1");
ffd6ed
+    return -1;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  CLEANUP_FREE char *typecode = NULL;
ffd6ed
+  if (asprintf (&typecode, "%i:%s", partnum, guid) == -1) {
ffd6ed
+    reply_with_perror ("asprintf");
ffd6ed
+    return -1;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  CLEANUP_FREE char *err = NULL;
ffd6ed
+  int r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
ffd6ed
+                    str_sgdisk, device, "-u", typecode, NULL);
ffd6ed
+
ffd6ed
+  if (r == -1) {
ffd6ed
+    reply_with_error ("%s %s -u %s: %s", str_sgdisk, device, typecode, err);
ffd6ed
+    return -1;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  return 0;
ffd6ed
+}
ffd6ed
+
ffd6ed
 static char *
ffd6ed
 sgdisk_info_extract_field (const char *device, int partnum, const char *field,
ffd6ed
                            char *(*extract) (const char *path))
ffd6ed
@@ -894,6 +920,13 @@ do_part_get_gpt_type (const char *device, int partnum)
ffd6ed
 }
ffd6ed
 
ffd6ed
 char *
ffd6ed
+do_part_get_gpt_guid (const char *device, int partnum)
ffd6ed
+{
ffd6ed
+  return sgdisk_info_extract_field (device, partnum,
ffd6ed
+                                    "Partition unique GUID", extract_uuid);
ffd6ed
+}
ffd6ed
+
ffd6ed
+char *
ffd6ed
 do_part_get_name (const char *device, int partnum)
ffd6ed
 {
ffd6ed
   CLEANUP_FREE char *parttype;
ffd6ed
diff --git a/generator/actions.ml b/generator/actions.ml
ffd6ed
index 593e51b..825acf9 100644
ffd6ed
--- a/generator/actions.ml
ffd6ed
+++ b/generator/actions.ml
ffd6ed
@@ -11995,6 +11995,42 @@ This is the same as the C<lstat(2)> system call." };
ffd6ed
     longdesc = "\
ffd6ed
 This is the internal call which implements C<guestfs_lstatnslist>." };
ffd6ed
 
ffd6ed
+  { defaults with
ffd6ed
+    name = "part_set_gpt_guid";
ffd6ed
+    style = RErr, [Device "device"; Int "partnum"; GUID "guid"], [];
ffd6ed
+    proc_nr = Some 446;
ffd6ed
+    optional = Some "gdisk";
ffd6ed
+    tests = [
ffd6ed
+      InitGPT, Always, TestLastFail (
ffd6ed
+        [["part_set_gpt_guid"; "/dev/sda"; "1"; "f"]]), [];
ffd6ed
+      InitGPT, Always, TestResultString (
ffd6ed
+        [["part_set_gpt_guid"; "/dev/sda"; "1";
ffd6ed
+          "01234567-89AB-CDEF-0123-456789ABCDEF"];
ffd6ed
+         ["part_get_gpt_guid"; "/dev/sda"; "1"]],
ffd6ed
+        "01234567-89AB-CDEF-0123-456789ABCDEF"), [];
ffd6ed
+    ];
ffd6ed
+    shortdesc = "set the GUID of a GPT partition";
ffd6ed
+    longdesc = "\
ffd6ed
+Set the GUID of numbered GPT partition C<partnum> to C<guid>.  Return an
ffd6ed
+error if the partition table of C<device> isn't GPT, or if C<guid> is not a
ffd6ed
+valid GUID." };
ffd6ed
+
ffd6ed
+  { defaults with
ffd6ed
+    name = "part_get_gpt_guid";
ffd6ed
+    style = RString "guid", [Device "device"; Int "partnum"], [];
ffd6ed
+    proc_nr = Some 447;
ffd6ed
+    optional = Some "gdisk";
ffd6ed
+    tests = [
ffd6ed
+      InitGPT, Always, TestResultString (
ffd6ed
+        [["part_set_gpt_guid"; "/dev/sda"; "1";
ffd6ed
+          "01234567-89AB-CDEF-0123-456789ABCDEF"];
ffd6ed
+         ["part_get_gpt_guid"; "/dev/sda"; "1"]],
ffd6ed
+        "01234567-89AB-CDEF-0123-456789ABCDEF"), [];
ffd6ed
+    ];
ffd6ed
+    shortdesc = "get the GUID of a GPT partition";
ffd6ed
+    longdesc = "\
ffd6ed
+Return the GUID of numbered GPT partition C<partnum>." };
ffd6ed
+
ffd6ed
 ]
ffd6ed
 
ffd6ed
 (* Non-API meta-commands available only in guestfish.
ffd6ed
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
ffd6ed
index 5721413..e9b7520 100644
ffd6ed
--- a/src/MAX_PROC_NR
ffd6ed
+++ b/src/MAX_PROC_NR
ffd6ed
@@ -1 +1 @@
ffd6ed
-423
ffd6ed
+447
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed