Skip to contentSkip to conversation

search: Support searching for GPT partition label with --part-label

PATCH In review Daniel Wagenknecht 4 participants 8 messages Nov 23, 2020 – Sep 22, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 search: Support searching for GPT partition label with --part-label

GPT partitions have a name property that is useable for identifying the partition in Linux via root=PARTLABEL=<partlabel> boot argument or via /dev/disk/by-partlabel/<partlabel>. Add support for searching for it in GRUB. Compared to the fs label, the partition label does not change when reformatting a partition.

---
 grub-core/Makefile.core.def           |  5 +++
 grub-core/commands/search.c           | 57 +++++++++++++++++++++++++++
 grub-core/commands/search_partlabel.c |  5 +++
 grub-core/commands/search_wrap.c      | 10 ++++-
 include/grub/search.h                 |  2 +
 5 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/search_partlabel.c
grub-core/Makefile.core.def +5 -0
index b5f47fc41..6e858d0f0 100644
@@ -1080,6 +1080,11 @@ module = {
   common = commands/search_label.c;
 };
 
+module = {
+  name = search_part_label;
+  common = commands/search_partlabel.c;
+};
+
 module = {
   name = setpci;
   common = commands/setpci.c;
grub-core/commands/search.c +57 -0

87 lines not on this page · raw message

grub-core/commands/search_partlabel.c new file +5 -0
new file mode 100644
index 000000000..8784d909e
@@ -0,0 +1,5 @@
+#define DO_SEARCH_PART_LABEL 1
+#define FUNC_NAME grub_search_part_label
+#define COMMAND_NAME "search.part_label"
+#define HELP_MESSAGE N_("Search devices by GPT partition label. If VARIABLE is specified, the first device found is set to a variable.")
+#include "search.c"
grub-core/commands/search_wrap.c +8 -2
index 47fc8eb99..3344e78c0 100644
@@ -34,6 +34,8 @@ static const struct grub_arg_option options[] =
     {"file",		'f', 0, N_("Search devices by a file."), 0, 0},
     {"label",		'l', 0, N_("Search devices by a filesystem label."),
      0, 0},
+    {"part-label",	0,   0, N_("Search devices by a GPT partition label."),
+     0, 0},
     {"fs-uuid",		'u', 0, N_("Search devices by a filesystem UUID."),
      0, 0},
     {"set",		's', GRUB_ARG_OPTION_OPTIONAL,
@@ -70,6 +72,7 @@ enum options
   {
     SEARCH_FILE,
     SEARCH_LABEL,
+    SEARCH_PART_LABEL,
     SEARCH_FS_UUID,
     SEARCH_SET,
     SEARCH_NO_FLOPPY,
@@ -183,6 +186,9 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, state[SEARCH_NO_FLOPPY].set, 
 		       hints, nhints);
+  else if (state[SEARCH_PART_LABEL].set)
+    grub_search_part_label (id, var, state[SEARCH_NO_FLOPPY].set,
+		       hints, nhints);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set,
 			 hints, nhints);
@@ -206,8 +212,8 @@ GRUB_MOD_INIT(search)
 			  GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
 			  N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
 			     " NAME"),
-			  N_("Search devices by file, filesystem label"
-			     " or filesystem UUID."
+			  N_("Search devices by file, filesystem label,"
+			     " GPT partition label or filesystem UUID."
 			     " If --set is specified, the first device found is"
 			     " set to a variable. If no variable name is"
 			     " specified, `root' is used."),
include/grub/search.h +2 -0
index d80347df3..4ae723bb2 100644
@@ -25,5 +25,7 @@ void grub_search_fs_uuid (const char *key, const char *var, int no_floppy,
 			  char **hints, unsigned nhints);
 void grub_search_label (const char *key, const char *var, int no_floppy,
 			char **hints, unsigned nhints);
+void grub_search_part_label (const char *key, const char *var, int no_floppy,
+			char **hints, unsigned nhints);
 
 #endif
Signature · 1 line
2.25.1
#1 lore raw

FYI
Previous related/similar patch
https://lists.gnu.org/archive/html/grub-devel/2020-11/msg00011.html

Thanks,

Dmitry

вт, 24 нояб. 2020 г. в 01:08, Daniel Wagenknecht <[email protected]>:

Quoted 202 lines

GPT partitions have a name property that is useable for identifying the partition in Linux via root=PARTLABEL=<partlabel> boot argument or via /dev/disk/by-partlabel/<partlabel>. Add support for searching for it in GRUB. Compared to the fs label, the partition label does not change when reformatting a partition.

---
 grub-core/Makefile.core.def           |  5 +++
 grub-core/commands/search.c           | 57 +++++++++++++++++++++++++++
 grub-core/commands/search_partlabel.c |  5 +++
 grub-core/commands/search_wrap.c      | 10 ++++-
 include/grub/search.h                 |  2 +
 5 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/search_partlabel.c
grub-core/Makefile.core.def +5 -0
index b5f47fc41..6e858d0f0 100644
@@ -1080,6 +1080,11 @@ module = {
   common = commands/search_label.c;
 };
 
+module = {
+  name = search_part_label;
+  common = commands/search_partlabel.c;
+};
+
 module = {
   name = setpci;
   common = commands/setpci.c;
grub-core/commands/search.c +57 -0
index ed090b3af..eb048886f 100644
@@ -30,6 +30,8 @@
 #include <grub/i18n.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
+#include <grub/charset.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -54,6 +56,29 @@ struct search_ctx
   int is_cache;
 };
 
+/* helper for grub_search_part_label */
+static char *
+get_utf8 (grub_uint8_t *in, grub_size_t len)
+{
+  grub_uint8_t *buf;
+  grub_uint16_t *tmp;
+  grub_size_t i;
+
+  buf = grub_calloc (len, GRUB_MAX_UTF8_PER_UTF16 + 1);
+  tmp = grub_calloc (len, sizeof (tmp[0]));
+  if (!buf || !tmp)
+    {
+      grub_free (buf);
+      grub_free (tmp);
+      return NULL;
+    }
+  for (i = 0; i < len; i++)
+    tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));
+  *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
+  grub_free (tmp);
+  return (char *) buf;
+}
+
 /* Helper for FUNC_NAME.  */
 static int
 iterate_device (const char *name, void *data)
@@ -90,6 +115,34 @@ iterate_device (const char *name, void *data)
        }
       grub_free (buf);
     }
+#elif defined (DO_SEARCH_PART_LABEL)
+    {
+      grub_device_t dev;
+      char *buf;
+
+      dev = grub_device_open (name);
+      if (dev && dev->disk && dev->disk->partition)
+       {
+         grub_partition_t p = dev->disk->partition;
+         grub_disk_t disk = grub_disk_open(dev->disk->name);
+
+         if (!disk)
+           return 1;
+         if (grub_strcmp (p->partmap->name, "gpt") == 0)
+           {
+             struct grub_gpt_partentry gptdata;
+
+             if (grub_disk_read (disk, p->offset, p->index, sizeof (gptdata), &gptdata))
+               return 1;
+             buf = get_utf8(gptdata.name, 36);
+             if (compare_fn (buf, ctx->key) == 0)
+               found = 1;
+             grub_free (buf);
+           }
+         grub_disk_close(disk);
+         grub_device_close (dev);
+       }
+    }
 #else
     {
       /* SEARCH_FS_UUID or SEARCH_LABEL */
@@ -313,6 +366,8 @@ static grub_command_t cmd;
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_INIT(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_INIT(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_INIT(search_fs_uuid)
 #else
@@ -327,6 +382,8 @@ GRUB_MOD_INIT(search_label)
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_FINI(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_FINI(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_FINI(search_fs_uuid)
 #else
grub-core/commands/search_partlabel.c new file +5 -0
new file mode 100644
index 000000000..8784d909e
@@ -0,0 +1,5 @@
+#define DO_SEARCH_PART_LABEL 1
+#define FUNC_NAME grub_search_part_label
+#define COMMAND_NAME "search.part_label"
+#define HELP_MESSAGE N_("Search devices by GPT partition label. If VARIABLE is specified, the first device found is set to a variable.")
+#include "search.c"
grub-core/commands/search_wrap.c +8 -2
index 47fc8eb99..3344e78c0 100644
@@ -34,6 +34,8 @@ static const struct grub_arg_option options[] =
     {"file",           'f', 0, N_("Search devices by a file."), 0, 0},
     {"label",          'l', 0, N_("Search devices by a filesystem label."),
      0, 0},
+    {"part-label",     0,   0, N_("Search devices by a GPT partition label."),
+     0, 0},
     {"fs-uuid",                'u', 0, N_("Search devices by a filesystem UUID."),
      0, 0},
     {"set",            's', GRUB_ARG_OPTION_OPTIONAL,
@@ -70,6 +72,7 @@ enum options
   {
     SEARCH_FILE,
     SEARCH_LABEL,
+    SEARCH_PART_LABEL,
     SEARCH_FS_UUID,
     SEARCH_SET,
     SEARCH_NO_FLOPPY,
@@ -183,6 +186,9 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, state[SEARCH_NO_FLOPPY].set,
                       hints, nhints);
+  else if (state[SEARCH_PART_LABEL].set)
+    grub_search_part_label (id, var, state[SEARCH_NO_FLOPPY].set,
+                      hints, nhints);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set,
                         hints, nhints);
@@ -206,8 +212,8 @@ GRUB_MOD_INIT(search)
                          GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
                          N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
                             " NAME"),
-                         N_("Search devices by file, filesystem label"
-                            " or filesystem UUID."
+                         N_("Search devices by file, filesystem label,"
+                            " GPT partition label or filesystem UUID."
                             " If --set is specified, the first device found is"
                             " set to a variable. If no variable name is"
                             " specified, `root' is used."),
include/grub/search.h +2 -1
index d80347df3..4ae723bb2 100644
@@ -25,5 +25,7 @@ void grub_search_fs_uuid (const char *key, const char *var, int no_floppy,
                          char **hints, unsigned nhints);
 void grub_search_label (const char *key, const char *var, int no_floppy,
                        char **hints, unsigned nhints);
+void grub_search_part_label (const char *key, const char *var, int no_floppy,
+                       char **hints, unsigned nhints);
 
 #endif
--

2.25.1

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

#2 lore raw

Hi,

Quoted 2 lines

Previous related/similar patch

Quoted 1 line

https://lists.gnu.org/archive/html/grub-devel/2020-11/msg00011.html

thanks! I saw that one and adapted my patch to mostly match that one, so it can be rebased and maybe unified like done for the search functions for fs_label/fs_uuid.

I'd be interested in feedback on where the get_utf8 helper function should go. It is copied without modification from grub-core/fs/ntfs.c.

Daniel Wagenknecht

#3 lore raw

Hi,

I have rebased this to grub-2.06.

Still seems to function, any plans to apply this?

/Sean

#4 lore raw
Quoted 5 lines · On Tue, 22 Feb 2022 16:16:09 +0100 Sean Nyekjaer <[email protected]> wrote:

Hi,

I have rebased this to grub-2.06.

Still seems to function, any plans to apply this?

This patch now needs to be rebased on to latest with minor updates to reflect changes in the search code. I don't think Daniel K saw this because he was not a direct recipient of this patch (in the TO or CC).

Daniel W, would you mind resubmitting this patch after rebasing with necessary changes and include Daniel K? I would like to see this functionality included in master and I don't see any objection to that.

Glenn

Quoted 7 lines

/Sean

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

#5 lore raw

PATCH v2 1/1 search: Support searching for GPT partition label

GPT partitions have a name property that is useable for identifying the partition in Linux via root=PARTLABEL=<partlabel> boot argument or via /dev/disk/by-partlabel/<partlabel>. Add support for searching for it in GRUB. Compared to the fs label, the partition label does not change when reformatting a partition.

---

This is a resend of a patch from 2020. Glenn Washburn suggested to rebase and resend with Daniel Kiper explicitely added to the recipients.

The rebase required some changes to adapt to the changes in no_floppy search flags but is otherwise unchanged.

Tested by manually searching for a partition by GPT partition label in grub in qemu.

Cheers
Daniel Wagenknecht

 grub-core/Makefile.core.def           |  5 +++
 grub-core/commands/search.c           | 57 +++++++++++++++++++++++++++
 grub-core/commands/search_partlabel.c |  5 +++
 grub-core/commands/search_wrap.c      |  9 ++++-
 include/grub/search.h                 |  3 ++
 5 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/search_partlabel.c
grub-core/Makefile.core.def +5 -0
index 5212dfab1..d76c47113 100644
@@ -1084,6 +1084,11 @@ module = {
   common = commands/search_label.c;
 };
 
+module = {
+  name = search_part_label;
+  common = commands/search_partlabel.c;
+};
+
 module = {
   name = setpci;
   common = commands/setpci.c;
grub-core/commands/search.c +57 -0

87 lines not on this page · raw message

grub-core/commands/search_partlabel.c new file +5 -0
new file mode 100644
index 000000000..8784d909e
@@ -0,0 +1,5 @@
+#define DO_SEARCH_PART_LABEL 1
+#define FUNC_NAME grub_search_part_label
+#define COMMAND_NAME "search.part_label"
+#define HELP_MESSAGE N_("Search devices by GPT partition label. If VARIABLE is specified, the first device found is set to a variable.")
+#include "search.c"
grub-core/commands/search_wrap.c +7 -2
index 318581f3b..6d1935bed 100644
@@ -34,6 +34,8 @@ static const struct grub_arg_option options[] =
     {"file",		'f', 0, N_("Search devices by a file."), 0, 0},
     {"label",		'l', 0, N_("Search devices by a filesystem label."),
      0, 0},
+    {"part-label",	0,   0, N_("Search devices by a GPT partition label."),
+     0, 0},
     {"fs-uuid",		'u', 0, N_("Search devices by a filesystem UUID."),
      0, 0},
     {"set",		's', GRUB_ARG_OPTION_OPTIONAL,
@@ -71,6 +73,7 @@ enum options
   {
     SEARCH_FILE,
     SEARCH_LABEL,
+    SEARCH_PART_LABEL,
     SEARCH_FS_UUID,
     SEARCH_SET,
     SEARCH_NO_FLOPPY,
@@ -191,6 +194,8 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, flags, hints, nhints);
+  else if (state[SEARCH_PART_LABEL].set)
+    grub_search_part_label (id, var, flags, hints, nhints);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, flags, hints, nhints);
   else if (state[SEARCH_FILE].set)
@@ -212,8 +217,8 @@ GRUB_MOD_INIT(search)
 			  GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
 			  N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
 			     " NAME"),
-			  N_("Search devices by file, filesystem label"
-			     " or filesystem UUID."
+			  N_("Search devices by file, filesystem label,"
+			     " GPT partition label or filesystem UUID."
 			     " If --set is specified, the first device found is"
 			     " set to a variable. If no variable name is"
 			     " specified, `root' is used."),
include/grub/search.h +3 -0
index ffd2411ca..ce61078a0 100644
@@ -35,5 +35,8 @@ void grub_search_fs_uuid (const char *key, const char *var,
 void grub_search_label (const char *key, const char *var,
 			enum search_flags flags,
 			char **hints, unsigned nhints);
+void grub_search_part_label (const char *key, const char *var,
+			enum search_flags flags,
+			char **hints, unsigned nhints);
 
 #endif
Signature · 1 line
2.36.0
Tested-by on v2 1/1
#6 lore raw

Re: [PATCH v2] search: Support searching for GPT partition label

Quoted 7 lines · Daniel Wagenknecht

GPT partitions have a name property that is useable for identifying the partition in Linux via root=PARTLABEL=<partlabel> boot argument or via /dev/disk/by-partlabel/<partlabel>. Add support for searching for it in GRUB. Compared to the fs label, the partition label does not change when reformatting a partition.

Quoted 203 lines

---

This is a resend of a patch from 2020. Glenn Washburn suggested to rebase and resend with Daniel Kiper explicitely added to the recipients.

The rebase required some changes to adapt to the changes in no_floppy search flags but is otherwise unchanged.

Tested by manually searching for a partition by GPT partition label in grub in qemu.

Cheers
Daniel Wagenknecht

 grub-core/Makefile.core.def           |  5 +++
 grub-core/commands/search.c           | 57 +++++++++++++++++++++++++++
 grub-core/commands/search_partlabel.c |  5 +++
 grub-core/commands/search_wrap.c      |  9 ++++-
 include/grub/search.h                 |  3 ++
 5 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/search_partlabel.c
grub-core/Makefile.core.def +5 -0
index 5212dfab1..d76c47113 100644
@@ -1084,6 +1084,11 @@ module = {
   common = commands/search_label.c;
 };
 
+module = {
+  name = search_part_label;
+  common = commands/search_partlabel.c;
+};
+
 module = {
   name = setpci;
   common = commands/setpci.c;
grub-core/commands/search.c +57 -0
index 263f1501c..56aa97278 100644
@@ -30,6 +30,8 @@
 #include <grub/i18n.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
+#include <grub/charset.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -54,6 +56,29 @@ struct search_ctx
   int is_cache;
 };
 
+/* helper for grub_search_part_label */
+static char *
+get_utf8 (grub_uint8_t *in, grub_size_t len)
+{
+  grub_uint8_t *buf;
+  grub_uint16_t *tmp;
+  grub_size_t i;
+
+  buf = grub_malloc (len *  GRUB_MAX_UTF8_PER_UTF16 + 1);
+  tmp = grub_malloc (len * sizeof (tmp[0]));
+  if (!buf || !tmp)
+    {
+      grub_free (buf);
+      grub_free (tmp);
+      return NULL;
+    }
+  for (i = 0; i < len; i++)
+    tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));
+  *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
+  grub_free (tmp);
+  return (char *) buf;
+}
+
 /* Helper for FUNC_NAME.  */
 static int
 iterate_device (const char *name, void *data)
@@ -110,6 +135,34 @@ iterate_device (const char *name, void *data)
 	}
       grub_free (buf);
     }
+#elif defined (DO_SEARCH_PART_LABEL)
+    {
+      grub_device_t dev;
+      char *buf;
+
+      dev = grub_device_open (name);
+      if (dev && dev->disk && dev->disk->partition)
+	{
+	  grub_partition_t p = dev->disk->partition;
+	  grub_disk_t disk = grub_disk_open(dev->disk->name);
+
+	  if (!disk)
+	    return 1;
+	  if (grub_strcmp (p->partmap->name, "gpt") == 0)
+	    {
+	      struct grub_gpt_partentry gptdata;
+
+	      if (grub_disk_read (disk, p->offset, p->index, sizeof (gptdata), &gptdata))
+		return 1;
+	      buf = get_utf8(gptdata.name, 36);
+	      if (compare_fn (buf, ctx->key) == 0)
+		found = 1;
+	      grub_free (buf);
+	    }
+	  grub_disk_close(disk);
+	  grub_device_close (dev);
+	}
+    }
 #else
     {
       /* SEARCH_FS_UUID or SEARCH_LABEL */
@@ -333,6 +386,8 @@ static grub_command_t cmd;
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_INIT(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_INIT(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_INIT(search_fs_uuid)
 #else
@@ -347,6 +402,8 @@ GRUB_MOD_INIT(search_label)
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_FINI(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_FINI(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_FINI(search_fs_uuid)
 #else
grub-core/commands/search_partlabel.c new file +5 -0
new file mode 100644
index 000000000..8784d909e
@@ -0,0 +1,5 @@
+#define DO_SEARCH_PART_LABEL 1
+#define FUNC_NAME grub_search_part_label
+#define COMMAND_NAME "search.part_label"
+#define HELP_MESSAGE N_("Search devices by GPT partition label. If VARIABLE is specified, the first device found is set to a variable.")
+#include "search.c"
grub-core/commands/search_wrap.c +7 -2
index 318581f3b..6d1935bed 100644
@@ -34,6 +34,8 @@ static const struct grub_arg_option options[] =
     {"file",		'f', 0, N_("Search devices by a file."), 0, 0},
     {"label",		'l', 0, N_("Search devices by a filesystem label."),
      0, 0},
+    {"part-label",	0,   0, N_("Search devices by a GPT partition label."),
+     0, 0},
     {"fs-uuid",		'u', 0, N_("Search devices by a filesystem UUID."),
      0, 0},
     {"set",		's', GRUB_ARG_OPTION_OPTIONAL,
@@ -71,6 +73,7 @@ enum options
   {
     SEARCH_FILE,
     SEARCH_LABEL,
+    SEARCH_PART_LABEL,
     SEARCH_FS_UUID,
     SEARCH_SET,
     SEARCH_NO_FLOPPY,
@@ -191,6 +194,8 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, flags, hints, nhints);
+  else if (state[SEARCH_PART_LABEL].set)
+    grub_search_part_label (id, var, flags, hints, nhints);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, flags, hints, nhints);
   else if (state[SEARCH_FILE].set)
@@ -212,8 +217,8 @@ GRUB_MOD_INIT(search)
 			  GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
 			  N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
 			     " NAME"),
-			  N_("Search devices by file, filesystem label"
-			     " or filesystem UUID."
+			  N_("Search devices by file, filesystem label,"
+			     " GPT partition label or filesystem UUID."
 			     " If --set is specified, the first device found is"
 			     " set to a variable. If no variable name is"
 			     " specified, `root' is used."),
include/grub/search.h +3 -0
index ffd2411ca..ce61078a0 100644
@@ -35,5 +35,8 @@ void grub_search_fs_uuid (const char *key, const char *var,
 void grub_search_label (const char *key, const char *var,
 			enum search_flags flags,
 			char **hints, unsigned nhints);
+void grub_search_part_label (const char *key, const char *var,
+			enum search_flags flags,
+			char **hints, unsigned nhints);
 
 #endif
Signature · 1 line
2.36.0
Signature · 10 lines
Best regards
Sean Nyekjær
Embedded Linux Consultant

+45 42427326
[email protected]

Geanix ApS
https://geanix.com
DK39600706
#7 lore raw

Re: [PATCH v2] search: Support searching for GPT partition label

In reply to Daniel Wagenknecht
Quoted 11 lines · On Fri, 9 Sep 2022 11:49:13 +0200 Daniel Wagenknecht <[email protected]>…

GPT partitions have a name property that is useable for identifying the partition in Linux via root=PARTLABEL=<partlabel> boot argument or via /dev/disk/by-partlabel/<partlabel>. Add support for searching for it in GRUB. Compared to the fs label, the partition label does not change when reformatting a partition.

---

This is a resend of a patch from 2020. Glenn Washburn suggested to rebase and resend with Daniel Kiper explicitely added to the recipients.

Thanks Daniel. Following are some suggestions.

Quoted 51 lines

The rebase required some changes to adapt to the changes in no_floppy search flags but is otherwise unchanged.

Tested by manually searching for a partition by GPT partition label in grub in qemu.

Cheers
Daniel Wagenknecht

 grub-core/Makefile.core.def           |  5 +++
 grub-core/commands/search.c           | 57 +++++++++++++++++++++++++++
 grub-core/commands/search_partlabel.c |  5 +++
 grub-core/commands/search_wrap.c      |  9 ++++-
 include/grub/search.h                 |  3 ++
 5 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/search_partlabel.c
grub-core/Makefile.core.def +5 -0
index 5212dfab1..d76c47113 100644
@@ -1084,6 +1084,11 @@ module = {
   common = commands/search_label.c;
 };
 
+module = {
+  name = search_part_label;
+  common = commands/search_partlabel.c;
+};
+
 module = {
   name = setpci;
   common = commands/setpci.c;
grub-core/commands/search.c +2 -0
index 263f1501c..56aa97278 100644
@@ -30,6 +30,8 @@
 #include <grub/i18n.h>
 #include <grub/disk.h>
 #include <grub/partition.h>
+#include <grub/gpt_partition.h>
+#include <grub/charset.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -54,6 +56,29 @@ struct search_ctx
   int is_cache;
 };

grub-core/commands/search.c struct search_ctx · line 67

Quoted 10 lines
grub-core/commands/search.c +9 -0
 
+/* helper for grub_search_part_label */
+static char *
+get_utf8 (grub_uint8_t *in, grub_size_t len)
+{
+  grub_uint8_t *buf;
+  grub_uint16_t *tmp;
+  grub_size_t i;
+
+  buf = grub_malloc (len *  GRUB_MAX_UTF8_PER_UTF16 + 1);

Use explicit parenthesis here and remove the extra space. I also don't like the use of GRUB_MAX_UTF8_PER_UTF16 because its wasteful. It should be half of what it is. But I'll let it pass because that code should be fixed as part of a different series addressing this.

grub-core/commands/search.c struct search_ctx · line 76

Quoted 9 lines
grub-core/commands/search.c +9 -0
+  tmp = grub_malloc (len * sizeof (tmp[0]));
+  if (!buf || !tmp)
+    {
+      grub_free (buf);
+      grub_free (tmp);
+      return NULL;
+    }
+  for (i = 0; i < len; i++)
+    tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));

I think this should be wrapped in an inline function in charset.h, perhaps called grub_utf16le_to_utf16be. If you can think of a better name, let me know.

Quoted 20 lines
grub-core/commands/search.c +13 -0
+  *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
+  grub_free (tmp);
+  return (char *) buf;
+}
+
 /* Helper for FUNC_NAME.  */
 static int
 iterate_device (const char *name, void *data)
@@ -110,6 +135,34 @@ iterate_device (const char *name, void *data)
 	}
       grub_free (buf);
     }
+#elif defined (DO_SEARCH_PART_LABEL)
+    {
+      grub_device_t dev;
+      char *buf;
+
+      dev = grub_device_open (name);
+      if (dev && dev->disk && dev->disk->partition)
+	{

grub-core/commands/search.c iterate_device (const char *name, void *data) · line 155

Quoted 10 lines
grub-core/commands/search.c +10 -0
+	  grub_partition_t p = dev->disk->partition;
+	  grub_disk_t disk = grub_disk_open(dev->disk->name);
+
+	  if (!disk)
+	    return 1;
+	  if (grub_strcmp (p->partmap->name, "gpt") == 0)
+	    {
+	      struct grub_gpt_partentry gptdata;
+
+	      if (grub_disk_read (disk, p->offset, p->index, sizeof (gptdata), &gptdata))

This should probably be using partition entry size from the GPT header instead of "sizeof (gptdata)" [1]. It looks like you probably took this code from other places in GRUB that do exactly this. The reason that it works there and not here (at least in the 3 places that I found) is that those other uses do not use the name field.

According to the UEFI 2.9 spec section 5.3.3 page 125, the partition name field is a "Null-terminated string containing a human-readable name of the partition." So I can accept the code as is, if we only read the name up to the lower of first wide NULL byte (2 NULLs in a row) or 72 bytes.

[1] https://wiki.osdev.org/GPT#LBA_2:_Partition_Entries

grub-core/commands/search.c iterate_device (const char *name, void *data) · line 157

Quoted 2 lines
grub-core/commands/search.c +2 -0
+		return 1;
+	      buf = get_utf8(gptdata.name, 36);

So the literal 36 should instead be something like "grub_min (grub_utf16_strlen (gptdata.name), sizeof (gptdata.name) / 2)". And an inline function grub_utf16_strlen should be created in include/grub/charset.h.

Quoted 86 lines
grub-core/commands/search.c +12 -0
+	      if (compare_fn (buf, ctx->key) == 0)
+		found = 1;
+	      grub_free (buf);
+	    }
+	  grub_disk_close(disk);
+	  grub_device_close (dev);
+	}
+    }
 #else
     {
       /* SEARCH_FS_UUID or SEARCH_LABEL */
@@ -333,6 +386,8 @@ static grub_command_t cmd;
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_INIT(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_INIT(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_INIT(search_fs_uuid)
 #else
@@ -347,6 +402,8 @@ GRUB_MOD_INIT(search_label)
 
 #ifdef DO_SEARCH_FILE
 GRUB_MOD_FINI(search_fs_file)
+#elif defined (DO_SEARCH_PART_LABEL)
+GRUB_MOD_FINI(search_part_label)
 #elif defined (DO_SEARCH_FS_UUID)
 GRUB_MOD_FINI(search_fs_uuid)
 #else
grub-core/commands/search_partlabel.c new file +5 -0
new file mode 100644
index 000000000..8784d909e
@@ -0,0 +1,5 @@
+#define DO_SEARCH_PART_LABEL 1
+#define FUNC_NAME grub_search_part_label
+#define COMMAND_NAME "search.part_label"
+#define HELP_MESSAGE N_("Search devices by GPT partition label. If VARIABLE is specified, the first device found is set to a variable.")
+#include "search.c"
grub-core/commands/search_wrap.c +7 -2
index 318581f3b..6d1935bed 100644
@@ -34,6 +34,8 @@ static const struct grub_arg_option options[] =
     {"file",		'f', 0, N_("Search devices by a file."), 0, 0},
     {"label",		'l', 0, N_("Search devices by a filesystem label."),
      0, 0},
+    {"part-label",	0,   0, N_("Search devices by a GPT partition label."),
+     0, 0},
     {"fs-uuid",		'u', 0, N_("Search devices by a filesystem UUID."),
      0, 0},
     {"set",		's', GRUB_ARG_OPTION_OPTIONAL,
@@ -71,6 +73,7 @@ enum options
   {
     SEARCH_FILE,
     SEARCH_LABEL,
+    SEARCH_PART_LABEL,
     SEARCH_FS_UUID,
     SEARCH_SET,
     SEARCH_NO_FLOPPY,
@@ -191,6 +194,8 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, flags, hints, nhints);
+  else if (state[SEARCH_PART_LABEL].set)
+    grub_search_part_label (id, var, flags, hints, nhints);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, flags, hints, nhints);
   else if (state[SEARCH_FILE].set)
@@ -212,8 +217,8 @@ GRUB_MOD_INIT(search)
 			  GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
 			  N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
 			     " NAME"),
-			  N_("Search devices by file, filesystem label"
-			     " or filesystem UUID."
+			  N_("Search devices by file, filesystem label,"
+			     " GPT partition label or filesystem UUID."
 			     " If --set is specified, the first device found is"
 			     " set to a variable. If no variable name is"
 			     " specified, `root' is used."),
include/grub/search.h +0 -0
index ffd2411ca..ce61078a0 100644

include/grub/search.h void grub_search_fs_uuid (const char *key, const char *var, · line 40

Quoted 6 lines
include/grub/search.h +3 -0
@@ -35,5 +35,8 @@ void grub_search_fs_uuid (const char *key, const char *var,
 void grub_search_label (const char *key, const char *var,
 			enum search_flags flags,
 			char **hints, unsigned nhints);
+void grub_search_part_label (const char *key, const char *var,
+			enum search_flags flags,
+			char **hints, unsigned nhints);

The indention of the above two lines should be aligned to the first const in the 3rd line up, using GRUB's spacing policy.

With a little more work this will be ready for Daniel to take a look at.

Glenn

Quoted 2 lines
include/grub/search.h +0 -0
 #endif