Skip to contentSkip to conversation

probe: Support probing for GPT partition UUID with -g

PATCH Superseded Jacob Kroon 4 participants 6 messages May 13, 2019 – May 14, 2019

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 probe: Support probing for GPT partition UUID with -g

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.

---
 docs/grub.texi             |  2 +-
 grub-core/commands/probe.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

Changes since v4:
* Do endianess byte-swapping on the fly when formatting string

docs/grub.texi +1 -1
index 308b25074..d85818744 100644
@@ -4771,7 +4771,7 @@ a rest.
 @node probe
 @subsection probe
 
-@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label} device
+@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}|@option{--part-uuid} device
 Retrieve device information. If option @option{--set} is given, assign result
 to variable @var{var}, otherwise print information on the screen.
 @end deffn
grub-core/commands/probe.c +34 -0
index 95d272287..b26cbe2cc 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},
+    {"part-uuid",	'g', 0, N_("Determine GPT partition UUID."), 0, 0},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -98,6 +100,38 @@ 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;
+	  grub_snprintf (val, sizeof(val),
+			 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+			 grub_le_to_cpu32 (guid->data1),
+			 grub_le_to_cpu16 (guid->data2),
+			 grub_le_to_cpu16 (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
#2 lore raw
In reply to Jacob Kroon

Ideally the option shouldn't be specific to GPT as other partmaps also have stable IDs. I think it's better not to have short option at all

Quoted 103 lines · On Mon, 13 May 2019, 20:33 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.

---
 docs/grub.texi             |  2 +-
 grub-core/commands/probe.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

Changes since v4:
* Do endianess byte-swapping on the fly when formatting string

docs/grub.texi +0 -1
index 308b25074..d85818744 100644
@@ -4771,7 +4771,7 @@ a rest.
 @node probe
 @subsection probe
 
-@deffn Command probe [@option{--set} var]
 @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}

device
+@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}|@option{--part-uuid} device
Retrieve device information. If option @option{--set} is given, assign
result
to variable @var{var}, otherwise print information on the screen.
@end deffn

grub-core/commands/probe.c +34 -0
index 95d272287..b26cbe2cc 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},
+    {"part-uuid",      'g', 0, N_("Determine GPT partition UUID."), 0, 0},
     {0, 0, 0, 0, 0, 0}
   };
@@ -98,6 +100,38 @@ 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;
+         grub_snprintf (val, sizeof(val),
+
 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+                        grub_le_to_cpu32 (guid->data1),
+                        grub_le_to_cpu16 (guid->data2),
+                        grub_le_to_cpu16 (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;

--
2.20.1

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

#3 lore raw

Hi Vladimir,

Quoted 3 lines · Vladimir 'phcoder' Serbinenko

Ideally the option shouldn't be specific to GPT as other partmaps also have stable IDs. I think it's better not to have short option at all

Ok, I have some questions below.

Quoted 37 lines · On Mon, 13 May 2019, 20:33 Jacob Kroon, <[email protected]> wrote:
Quoted 36 lines

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.

---
 docs/grub.texi             |  2 +-
 grub-core/commands/probe.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

Changes since v4:
* Do endianess byte-swapping on the fly when formatting string

docs/grub.texi +1 -1
index 308b25074..d85818744 100644
@@ -4771,7 +4771,7 @@ a rest.
 @node probe
 @subsection probe
 
-@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label} device
+@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}|@option{--part-uuid} device
 Retrieve device information. If option @option{--set} is given, assign result
 to variable @var{var}, otherwise print information on the screen.
 @end deffn
grub-core/commands/probe.c +1 -0
index 95d272287..b26cbe2cc 100644
@@ -24,6 +24,7 @@
 #include <grub/device.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
Quoted 8 lines

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

Quoted 8 lines
grub-core/commands/probe.c +1 -0
 #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},
+    {"part-uuid",      'g', 0, N_("Determine GPT partition UUID."), 0, 0},

Is "part-uuid" ok ? I guess I should remove "GPT" from the help text ?

I've seen both 0 and -1 being passed as short option, do they both mean "no short option", or is there a preference which to use in this case ?

/Jacob

#4 lore raw

Hello,

Quoted 49 lines · Jacob Kroon

Hi Vladimir,

Quoted 3 lines · Vladimir 'phcoder' Serbinenko

Ideally the option shouldn't be specific to GPT as other partmaps also have stable IDs. I think it's better not to have short option at all

Ok, I have some questions below.

Quoted 39 lines · On Mon, 13 May 2019, 20:33 Jacob Kroon, <[email protected]> wrote:
Quoted 38 lines

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.

---
 docs/grub.texi             |  2 +-
 grub-core/commands/probe.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

Changes since v4:
* Do endianess byte-swapping on the fly when formatting string

docs/grub.texi +1 -1
index 308b25074..d85818744 100644
@@ -4771,7 +4771,7 @@ a rest.
 @node probe
 @subsection probe
 
-@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label} device
+@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}|@option{--part-uuid} device
 Retrieve device information. If option @option{--set} is given, assign result
 to variable @var{var}, otherwise print information on the screen.
 @end deffn
grub-core/commands/probe.c +1 -0
index 95d272287..b26cbe2cc 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 8 lines
Quoted 6 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},
+    {"part-uuid",      'g', 0, N_("Determine GPT partition UUID."), 0, 0},

Is "part-uuid" ok ? I guess I should remove "GPT" from the help text ?

If I may interfere, lsblk names it PARTUUID (actually the case doesn't matter) so I'd suggest partuuidn withjout a hyphen.

And yes, this field exists also with a 'dos' partition label (or 'msdos" as parted calls it), not only a 'gpt'.

I can't say for other partition labels than those two, having only used these.

Quoted 5 lines

I've seen both 0 and -1 being passed as short option, do they both mean "no short option", or is there a preference which to use in this case ?

/Jacob

Best,

Didier

#5 lore raw

Hi Didier,

Quoted 62 lines · Didier Spaier

Hello,

Quoted 49 lines · Jacob Kroon

Hi Vladimir,

Quoted 3 lines · Vladimir 'phcoder' Serbinenko

Ideally the option shouldn't be specific to GPT as other partmaps also have stable IDs. I think it's better not to have short option at all

Ok, I have some questions below.

Quoted 39 lines · On Mon, 13 May 2019, 20:33 Jacob Kroon, <[email protected]> wrote:
Quoted 38 lines

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.

---
 docs/grub.texi             |  2 +-
 grub-core/commands/probe.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

Changes since v4:
* Do endianess byte-swapping on the fly when formatting string

docs/grub.texi +1 -1
index 308b25074..d85818744 100644
@@ -4771,7 +4771,7 @@ a rest.
 @node probe
 @subsection probe
 
-@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label} device
+@deffn Command probe [@option{--set} var] @option{--driver}|@option{--partmap}|@option{--fs}|@option{--fs-uuid}|@option{--label}|@option{--part-uuid} device
 Retrieve device information. If option @option{--set} is given, assign result
 to variable @var{var}, otherwise print information on the screen.
 @end deffn
grub-core/commands/probe.c +1 -0
index 95d272287..b26cbe2cc 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 8 lines
Quoted 6 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},
+    {"part-uuid",      'g', 0, N_("Determine GPT partition UUID."), 0, 0},

Is "part-uuid" ok ? I guess I should remove "GPT" from the help text ?

Quoted 3 lines

If I may interfere, lsblk names it PARTUUID (actually the case doesn't matter) so I'd suggest partuuidn withjout a hyphen.

I opted for "--part-uuid" since there already is "--fs-uuid", but I don't have a strong opinion on the matter.

Quoted 16 lines

And yes, this field exists also with a 'dos' partition label (or 'msdos" as parted calls it), not only a 'gpt'.

I can't say for other partition labels than those two, having only used these.

Quoted 5 lines

I've seen both 0 and -1 being passed as short option, do they both mean "no short option", or is there a preference which to use in this case ?

/Jacob

Best,

Didier