Skip to contentSkip to conversation

probe: Support probing for GPT partition UUID with -q

PATCH Superseded Jacob Kroon 3 participants 4 messages May 10, 2019 – May 13, 2019

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 probe: Support probing for GPT partition UUID with -q

Linux supports root=PARTUUID=<partuuid> boot argument, so add support for probing it. Compared to the fs UUID, the partition UUID does not change when reformatting a partition.

---
 grub-core/commands/probe.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Changes since v2:

* Add a proper commit message
* Handle endianess in the same way as is currently done in util/grub-probe.c:print_gpt_guid ()

grub-core/commands/probe.c +35 -0
index 95d272287..24742c181 100644
@@ -24,6 +24,7 @@
 #include <grub/device.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
 #include <grub/net.h>
 #include <grub/fs.h>
 #include <grub/file.h>
@@ -45,6 +46,7 @@ static const struct grub_arg_option options[] =
     {"fs",		'f', 0, N_("Determine filesystem type."), 0, 0},
     {"fs-uuid",		'u', 0, N_("Determine filesystem UUID."), 0, 0},
     {"label",		'l', 0, N_("Determine filesystem label."), 0, 0},
+    {"partuuid",	'q', 0, N_("Determine GPT partition uuid."), 0, 0},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -98,6 +100,39 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
       grub_device_close (dev);
       return GRUB_ERR_NONE;
     }
+  if (state[6].set)
+    {
+      /* AAAABBBB-CCCC-DDDD-EEEE-FFFFFFFFFFFF + null terminator */
+      char val[37] = "none";
+      if (dev->disk && dev->disk->partition &&
+	  grub_strcmp(dev->disk->partition->partmap->name, "gpt") == 0)
+	{
+	  struct grub_gpt_partentry entry;
+	  struct grub_partition *p = dev->disk->partition;
+	  grub_disk_t disk = grub_disk_open(dev->disk->name);
+	  if (!disk)
+	    return grub_errno;
+	  if (grub_disk_read(disk, p->offset, p->index, sizeof(entry), &entry))
+	    return grub_errno;
+	  grub_disk_close(disk);
+	  grub_gpt_part_guid_t *guid = &entry.guid;
+	  guid->data1 = grub_le_to_cpu32 (guid->data1);
+	  guid->data2 = grub_le_to_cpu16 (guid->data2);
+	  guid->data3 = grub_le_to_cpu16 (guid->data3);
+	  grub_snprintf (val, sizeof(val),
+			 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+			 guid->data1, guid->data2, guid->data3, guid->data4[0],
+			 guid->data4[1], guid->data4[2], guid->data4[3],
+			 guid->data4[4], guid->data4[5], guid->data4[6],
+			 guid->data4[7]);
+	}
+      if (state[0].set)
+	grub_env_set (state[0].arg, val);
+      else
+	grub_printf ("%s", val);
+      grub_device_close (dev);
+      return GRUB_ERR_NONE;
+    }
   fs = grub_fs_probe (dev);
   if (! fs)
     return grub_errno;
Signature · 1 line
2.20.1
#1 lore raw

Dear Jacob,

Quoted 3 lines · Jacob Kroon

Linux supports root=PARTUUID=<partuuid> boot argument, so add support for probing it. Compared to the fs UUID, the partition UUID does not change when reformatting a partition.

How did you choose the switch name -q? Are other tools using this already, or was it just available?

Quoted 4 lines
---
  grub-core/commands/probe.c | 35 +++++++++++++++++++++++++++++++++++
  1 file changed, 35 insertions(+)

It’d be awesome if you also updated the documentation/manual docs/grub.texi.

Quoted 19 lines

Changes since v2:

* Add a proper commit message
* Handle endianess in the same way as is currently done in util/grub-probe.c:print_gpt_guid ()

grub-core/commands/probe.c +1 -0
index 95d272287..24742c181 100644
@@ -24,6 +24,7 @@
  #include <grub/device.h>
  #include <grub/disk.h>
  #include <grub/partition.h>
+#include <grub/gpt_partition.h>
  #include <grub/net.h>
  #include <grub/fs.h>
  #include <grub/file.h>

grub-core/commands/probe.c static const struct grub_arg_option options[] = · line 49

Quoted 4 lines
grub-core/commands/probe.c +1 -0
@@ -45,6 +46,7 @@ static const struct grub_arg_option options[] =
      {"fs",		'f', 0, N_("Determine filesystem type."), 0, 0},
      {"fs-uuid",		'u', 0, N_("Determine filesystem UUID."), 0, 0},
      {"label",		'l', 0, N_("Determine filesystem label."), 0, 0},
+    {"partuuid",	'q', 0, N_("Determine GPT partition uuid."), 0, 0},

UUID is capitalized in the string above. Do it her too for consistency?

[…]

Kind regards,

Paul

#2 lore raw

Hi Paul,

Quoted 4 lines · Paul Menzel

Dear Jacob,

Quoted 8 lines
Quoted 3 lines · Jacob Kroon

Linux supports root=PARTUUID=<partuuid> boot argument, so add support for probing it. Compared to the fs UUID, the partition UUID does not change when reformatting a partition.

How did you choose the switch name -q? Are other tools using this already, or was it just available?

Honestly I just picked the first character on the top-left of my keyboard :-) I'm not sure which char to pick, but I guess maybe 'g' as in gpt would make more sense ?
Open to suggestions...

Quoted 8 lines
Quoted 4 lines
---
  grub-core/commands/probe.c | 35 +++++++++++++++++++++++++++++++++++
  1 file changed, 35 insertions(+)

It’d be awesome if you also updated the documentation/manual docs/grub.texi.

Ok, will do.

Quoted 17 lines
Quoted 17 lines

Changes since v2:

* Add a proper commit message
* Handle endianess in the same way as is currently done in util/grub-probe.c:print_gpt_guid ()

grub-core/commands/probe.c +1 -0
index 95d272287..24742c181 100644
@@ -24,6 +24,7 @@
  #include <grub/device.h>
  #include <grub/disk.h>
  #include <grub/partition.h>
+#include <grub/gpt_partition.h>
  #include <grub/net.h>
  #include <grub/fs.h>
Quoted 9 lines

grub-core/commands/probe.c static const struct grub_arg_option options[] = · line 49

Quoted 6 lines
grub-core/commands/probe.c +1 -0
  #include <grub/file.h>
@@ -45,6 +46,7 @@ static const struct grub_arg_option options[] =
      {"fs",          'f', 0, N_("Determine filesystem type."), 0, 0},
      {"fs-uuid",             'u', 0, N_("Determine filesystem UUID."), 0, 0},
      {"label",               'l', 0, N_("Determine filesystem label."), 0, 0},
+    {"partuuid",     'q', 0, N_("Determine GPT partition uuid."), 0, 0},

UUID is capitalized in the string above. Do it her too for consistency?

Yes, will fix.

Thanks for reviewing,
Regards Jacob

#3 lore raw
In reply to Jacob Kroon
Quoted 52 lines · On Fri, 10 May 2019, 19:05 Jacob Kroon, <[email protected]> wrote:

Linux supports root=PARTUUID=<partuuid> boot argument, so add support for probing it. Compared to the fs UUID, the partition UUID does not change when reformatting a partition.

---
 grub-core/commands/probe.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Changes since v2:

* Add a proper commit message
* Handle endianess in the same way as is currently done in util/grub-probe.c:print_gpt_guid ()

grub-core/commands/probe.c +13 -0
index 95d272287..24742c181 100644
@@ -24,6 +24,7 @@
 #include <grub/device.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
 #include <grub/net.h>
 #include <grub/fs.h>
 #include <grub/file.h>
@@ -45,6 +46,7 @@ static const struct grub_arg_option options[] =
     {"fs",             'f', 0, N_("Determine filesystem type."), 0, 0},
     {"fs-uuid",                'u', 0, N_("Determine filesystem UUID."),
 0, 0},
     {"label",          'l', 0, N_("Determine filesystem label."), 0, 0},
+    {"partuuid",       'q', 0, N_("Determine GPT partition uuid."), 0, 0},
     {0, 0, 0, 0, 0, 0}
   };
@@ -98,6 +100,39 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc,
 char **args)
       grub_device_close (dev);
       return GRUB_ERR_NONE;
     }
+  if (state[6].set)
+    {
+      /* AAAABBBB-CCCC-DDDD-EEEE-FFFFFFFFFFFF + null terminator */
+      char val[37] = "none";
+      if (dev->disk && dev->disk->partition &&
+         grub_strcmp(dev->disk->partition->partmap->name, "gpt") == 0)
+       {
+         struct grub_gpt_partentry entry;
+         struct grub_partition *p = dev->disk->partition;
+         grub_disk_t disk = grub_disk_open(dev->disk->name);
+         if (!disk)

grub-core/commands/probe.c grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, · line 124

Quoted 10 lines
grub-core/commands/probe.c +8 -0
+           return grub_errno;
+         if (grub_disk_read(disk, p->offset, p->index, sizeof(entry),
 &entry))
+           return grub_errno;
+         grub_disk_close(disk);
+         grub_gpt_part_guid_t *guid = &entry.guid;
+         guid->data1 = grub_le_to_cpu32 (guid->data1);
+         guid->data2 = grub_le_to_cpu16 (guid->data2);
+         guid->data3 = grub_le_to_cpu16 (guid->data3);
 

Please don't modify this in place. This can easily create hard to track endian-dependent bug. Instead you can transform them right before printing, in the same statement.
Otherwise the patch is fine

Quoted 28 lines
grub-core/commands/probe.c +2 -0
+         grub_snprintf (val, sizeof(val),
+

"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ guid->data1, guid->data2, guid->data3,
guid->data4[0],

grub-core/commands/probe.c +11 -1
+                        guid->data4[1], guid->data4[2], guid->data4[3],
+                        guid->data4[4], guid->data4[5], guid->data4[6],
+                        guid->data4[7]);
+       }
+      if (state[0].set)
+       grub_env_set (state[0].arg, val);
+      else
+       grub_printf ("%s", val);
+      grub_device_close (dev);
+      return GRUB_ERR_NONE;
+    }
   fs = grub_fs_probe (dev);
   if (! fs)
     return grub_errno;
--

2.20.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel