Skip to contentSkip to conversation

Add PARTUUID detection support

PATCH Superseded Nicholas Vinson 1 participant 3 messages Aug 7, 2016

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add PARTUUID detection support

Changes from Patch v1:
- Added GRUB_ENABLE_LINUX_PARTUUID variable description to grub.texi

- Removed added gpt_part_guid copy logic from
grub_gpt_partition_map_iterate()

- Removed added NT disk signature copy logic from grub_partition_msdos_iterate()

- Removed modifications to partition number increment logic

- Removed added guid union definition.

- Added GRUB_ENABLE_LINUX_PARTUUID to grub-mkconfig.in export list

- Moved PRINT_GPT_PARTTYPE printing logic to print_gpt_guid() function in grub-probe.c

- Updated PRINT_GPT_PARTTYPE case to call print_gpt_guid() function in grub-probe.c.

- Created probe_partuuid() function in grub-probe.c

- Updated print == PRINT_PARTUUID check logic in probe() to call probe_partuuid().

- Updated UUID logic in 10_linux.in to enable root=PARTUUID feature only if GRUB_DISABLE_LINUX_UUID is not set to true, and GRUB_DEVICE_PARTUUID is not empty, GRUB_ENABLE_LINUX_PARTUUID is set to true.

Hello,

This is a request to add PARTUUID detection support grub-probe for MBR and GPT partition schemes. The Linux kernel supports mounting the root filesystem by Linux device name or by the Partition [GU]UID. GRUB's mkconfig, however, currently only supports specifying the rootfs in the kernel command-line by Linux device name unless an initramfs is also present. When an initramfs is present GRUB's mkconfig will set the kernel's root parameter value to either the Linux device name or to the filesystem [GU]UID.

Therefore, the only way to protect a Linux system from failing to boot when its Linux storage device names change is to either manually edit grub.cfg or /etc/default/grub and append root=PARTUUID=xxx to the command-line or create an initramfs that understands how to mount devices by filesystem [G]UID and let grub-mkconfig pass the filesystem [GU]UID to the initramfs.

The goal of this patch set is to enable root=PARTUUID=xxx support in grub-mkconfig, so that users don't have to manually edit /etc/default/grub or grub.cfg, or create an initramfs for the sole purpose of having a robust bootloader configuration for Linux.

Thanks,
Nicholas Vinson

Nicholas Vinson (2):
Add PARTUUID detection support to grub-probe
Update grub script template files

 docs/grub.texi          | 13 ++++++++
 util/grub-mkconfig.in   |  3 ++
 util/grub-probe.c       | 83 ++++++++++++++++++++++++++++++++++++++++---------
 util/grub.d/10_linux.in | 13 ++++++--
 4 files changed, 95 insertions(+), 17 deletions(-)
Signature · 1 line
2.9.2
#1 lore raw

PATCH v2 1/2 Add PARTUUID detection support to grub-probe

Add PARTUUID detection to grub-probe. The grub-probe utility is used by grub-mkconfig to determine the filesystem [GU]UID, so updating it to be able to return partition [GU]UIDs seemed like the natural choice. The other obvious choice was to rely on Linux userland tools and /dev file structure which would added to the runtime dependencies of grub-probe.

---
 util/grub-probe.c | 83 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 68 insertions(+), 15 deletions(-)
util/grub-probe.c +68 -15
index 8ac527d..97daac6 100644
@@ -62,6 +62,7 @@ enum {
   PRINT_DRIVE,
   PRINT_DEVICE,
   PRINT_PARTMAP,
+  PRINT_PARTUUID,
   PRINT_ABSTRACTION,
   PRINT_CRYPTODISK_UUID,
   PRINT_HINT_STR,
@@ -85,6 +86,7 @@ static const char *targets[] =
     [PRINT_DRIVE]              = "drive",
     [PRINT_DEVICE]             = "device",
     [PRINT_PARTMAP]            = "partmap",
+    [PRINT_PARTUUID]           = "partuuid",
     [PRINT_ABSTRACTION]        = "abstraction",
     [PRINT_CRYPTODISK_UUID]    = "cryptodisk_uuid",
     [PRINT_HINT_STR]           = "hints_string",
@@ -168,6 +170,65 @@ probe_partmap (grub_disk_t disk, char delim)
 }
 
 static void
+print_gpt_guid(const struct grub_gpt_partentry gptdata)
+{
+  grub_gpt_part_type_t gpttype;
+  gpttype.data1 = grub_le_to_cpu32 (gptdata.type.data1);
+  gpttype.data2 = grub_le_to_cpu16 (gptdata.type.data2);
+  gpttype.data3 = grub_le_to_cpu16 (gptdata.type.data3);
+  grub_memcpy (gpttype.data4, gptdata.type.data4, 8);
+
+  grub_printf ("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+	       gpttype.data1, gpttype.data2, gpttype.data3, gpttype.data4[0],
+	       gpttype.data4[1], gpttype.data4[2], gpttype.data4[3],
+	       gpttype.data4[4], gpttype.data4[5], gpttype.data4[6],
+	       gpttype.data4[7]);
+}
+
+static void
+probe_partuuid (grub_disk_t disk, char delim)
+{
+  if (disk->partition)
+    {
+      if (strcmp(disk->partition->partmap->name, "gpt") == 0)
+	{
+	  const int guid_offset = 16;
+
+	  grub_partition_t p = disk->partition;
+	  struct grub_gpt_partentry gptdata;
+
+	  disk->partition = p->parent;
+
+	  if (grub_disk_read (disk, p->offset, p->index + guid_offset,
+			      sizeof(gptdata), &gptdata) == 0)
+	    print_gpt_guid (gptdata);
+
+	  disk->partition = p;
+	}
+      else if (strcmp(disk->partition->partmap->name, "msdos") == 0)
+	{
+	    /*
+	     * The partition GUID for MSDOS is the partition number (starting
+	     * with 1) prepended with the NT disk signature.
+	     */
+	    const int nt_disk_sig_offset = 440;
+	    grub_uint32_t nt_disk_sig;
+	    grub_partition_t p = disk->partition;
+
+	    disk->partition = p->parent;
+
+	    if (grub_disk_read (disk, 0, nt_disk_sig_offset, sizeof(nt_disk_sig),
+				&nt_disk_sig) == 0)
+	      {
+		nt_disk_sig = grub_le_to_cpu32(nt_disk_sig);
+		grub_printf ("%08x-%02x", nt_disk_sig, 1 + p->number);
+	      }
+	    disk->partition = p;
+	}
+    }
+}
+
+static void
 probe_cryptodisk_uuid (grub_disk_t disk, char delim)
 {
   grub_disk_memberlist_t list = NULL, tmp;
@@ -621,6 +682,12 @@ probe (const char *path, char **device_names, char delim)
 	/* Check if dev->disk itself is contained in a partmap.  */
 	probe_partmap (dev->disk, delim);
 
+      else if (print == PRINT_PARTUUID)
+	{
+	  probe_partuuid (dev->disk, delim);
+	  putchar (delim);
+	}
+
       else if (print == PRINT_MSDOS_PARTTYPE)
 	{
 	  if (dev->disk->partition
@@ -641,21 +708,7 @@ probe (const char *path, char **device_names, char delim)
 
               if (grub_disk_read (dev->disk, p->offset, p->index,
                                   sizeof (gptdata), &gptdata) == 0)
-                {
-                  grub_gpt_part_type_t gpttype;
-                  gpttype.data1 = grub_le_to_cpu32 (gptdata.type.data1);
-                  gpttype.data2 = grub_le_to_cpu16 (gptdata.type.data2);
-                  gpttype.data3 = grub_le_to_cpu16 (gptdata.type.data3);
-                  grub_memcpy (gpttype.data4, gptdata.type.data4, 8);
-
-                  grub_printf ("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-                               gpttype.data1, gpttype.data2,
-                               gpttype.data3, gpttype.data4[0], 
-                               gpttype.data4[1], gpttype.data4[2],
-                               gpttype.data4[3], gpttype.data4[4],
-                               gpttype.data4[5], gpttype.data4[6],
-                               gpttype.data4[7]);
-                }
+		print_gpt_guid(gptdata);
               dev->disk->partition = p;
             }
           putchar (delim);
Signature · 1 line
2.9.2
#2 lore raw

PATCH v2 2/2 Update grub script template files

In reply to Nicholas Vinson

Update grub-mkconfig.in and 10_linux.in to support grub-probe's new partuuid target. Update grub.texi documenation.

---
 docs/grub.texi          | 13 +++++++++++++
 util/grub-mkconfig.in   |  3 +++
 util/grub.d/10_linux.in | 13 +++++++++++--
 3 files changed, 27 insertions(+), 2 deletions(-)
docs/grub.texi +13 -0
index 82f6fa4..1f312c6 100644
@@ -1405,6 +1405,19 @@ the Linux kernel, using a @samp{root=UUID=...} kernel parameter.  This is
 usually more reliable, but in some cases it may not be appropriate.  To
 disable the use of UUIDs, set this option to @samp{true}.
 
+@item GRUB_ENABLE_LINUX_PARTUUID
+Normally, when @command{grub-mkconfig} sets the @samp{root=UUID=...} kernel
+parameter, it selects a filesystem UUID.  This works well for Linux systems that
+boot using an initramfs as most implementations support mounting the root
+filesystem this way.  However for Linux systems that do not boot using an
+initramfs, @command{grub-mkconfig} defaults to setting the @samp{root=...}
+kernel parameter to the device name containing the root filesystem.  Setting
+this option to @samp{true} changes the behavior of @command{grub-mkconfig} so
+that it identifies the device containing the root filesystem by the partition
+UUID.  In order to use the @samp{root=PARTUUID=...} kernel parameter, the Linux
+kernel version must be at least 2.6.37 (3.10 for systems using the MSDOS
+partition scheme).
+
 @item GRUB_DISABLE_RECOVERY
 If this option is set to @samp{true}, disable the generation of recovery
 mode menu entries.
util/grub-mkconfig.in +3 -0
index f8496d2..30ead59 100644
@@ -134,6 +134,7 @@ fi
 # Device containing our userland.  Typically used for root= parameter.
 GRUB_DEVICE="`${grub_probe} --target=device /`"
 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
+GRUB_DEVICE_PARTUUID="`${grub_probe} --device ${GRUB_DEVICE} --target=partuuid 2> /dev/null`" || true
 
 # Device containing our /boot partition.  Usually the same as GRUB_DEVICE.
 GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
@@ -182,6 +183,7 @@ if [ "x${GRUB_ACTUAL_DEFAULT}" = "xsaved" ] ; then GRUB_ACTUAL_DEFAULT="`"${grub
 # override them.
 export GRUB_DEVICE \
   GRUB_DEVICE_UUID \
+  GRUB_DEVICE_PARTUUID \
   GRUB_DEVICE_BOOT \
   GRUB_DEVICE_BOOT_UUID \
   GRUB_FS \
@@ -215,6 +217,7 @@ export GRUB_DEFAULT \
   GRUB_TERMINAL_OUTPUT \
   GRUB_SERIAL_COMMAND \
   GRUB_DISABLE_LINUX_UUID \
+  GRUB_ENABLE_LINUX_PARTUUID \
   GRUB_DISABLE_RECOVERY \
   GRUB_VIDEO_BACKEND \
   GRUB_GFXMODE \
util/grub.d/10_linux.in +11 -2
index de9044c..71675fa 100644
@@ -220,8 +220,17 @@ while [ "x$list" != "x" ] ; do
     gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
   elif test -z "${initramfs}" ; then
     # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
-    # no initrd or builtin initramfs, it can't work here.
-    linux_root_device_thisversion=${GRUB_DEVICE}
+    # no initrd or builtin initramfs, it can't work here.  However, if
+    # GRUB_DEVICE_PARTUUID is not empty we can use that here if
+    # GRUD_DISABLE_LINUX_UUID is not set to true and GRUB_ENABLE_LINUX_PARTUUID
+    # is set to true.
+    if [ "x${GRUB_DISABLE_LINUX_UUID}" != "xtrue" ] \
+        && [ "x${GRUB_DEVICE_PARTUUID}" != "x" ]    \
+        && [ "x${GRUB_ENABLE_LINUX_PARTUUID}" = "xtrue" ] ; then
+        linux_root_device_thisversion="PARTUUID=${GRUB_DEVICE_PARTUUID}"
+    else
+        linux_root_device_thisversion=${GRUB_DEVICE}
+    fi
   fi
 
   if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
Signature · 1 line
2.9.2