nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0177-ieee1275-powerpc-enables-device-mapper-discovery.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Diego Domingos <diegodo@br.ibm.com>
8e15ce
Date: Mon, 14 Dec 2020 17:47:16 +0100
8e15ce
Subject: [PATCH] ieee1275/powerpc: enables device mapper discovery
8e15ce
8e15ce
this patch enables the device mapper discovery on ofpath.c. Currently,
8e15ce
when we are dealing with a device like /dev/dm-* the ofpath returns null
8e15ce
since there is no function implemented to handle this case.
8e15ce
8e15ce
This patch implements a function that will look into /sys/block/dm-*
8e15ce
devices and search recursively inside slaves directory to find the root
8e15ce
disk.
8e15ce
8e15ce
Signed-off-by: Diego Domingos <diegodo@br.ibm.com>
8e15ce
---
8e15ce
 grub-core/osdep/linux/ofpath.c | 64 +++++++++++++++++++++++++++++++++++++++++-
8e15ce
 1 file changed, 63 insertions(+), 1 deletion(-)
8e15ce
8e15ce
diff --git a/grub-core/osdep/linux/ofpath.c b/grub-core/osdep/linux/ofpath.c
8e15ce
index 0f5d54e9f2d..cc849d9c94c 100644
8e15ce
--- a/grub-core/osdep/linux/ofpath.c
8e15ce
+++ b/grub-core/osdep/linux/ofpath.c
8e15ce
@@ -37,6 +37,7 @@
8e15ce
 #include <fcntl.h>
8e15ce
 #include <errno.h>
8e15ce
 #include <ctype.h>
8e15ce
+#include <dirent.h>
8e15ce
 
8e15ce
 #ifdef __sparc__
8e15ce
 typedef enum
8e15ce
@@ -755,13 +756,74 @@ strip_trailing_digits (const char *p)
8e15ce
   return new;
8e15ce
 }
8e15ce
 
8e15ce
+static char *
8e15ce
+get_slave_from_dm(const char * device){
8e15ce
+  char *curr_device, *tmp;
8e15ce
+  char *directory;
8e15ce
+  char *ret = NULL;
8e15ce
+
8e15ce
+  directory = grub_strdup (device);
8e15ce
+  tmp = get_basename(directory);
8e15ce
+  curr_device = grub_strdup (tmp);
8e15ce
+  *tmp = '\0';
8e15ce
+
8e15ce
+  /* Recursively check for slaves devices so we can find the root device */
8e15ce
+  while ((curr_device[0] == 'd') && (curr_device[1] == 'm') && (curr_device[2] == '-')){
8e15ce
+    DIR *dp;
8e15ce
+    struct dirent *ep;
8e15ce
+    char* device_path;
8e15ce
+
8e15ce
+    device_path = grub_xasprintf ("/sys/block/%s/slaves", curr_device);
8e15ce
+    dp = opendir(device_path);
8e15ce
+    free(device_path);
8e15ce
+
8e15ce
+    if (dp != NULL)
8e15ce
+    {
8e15ce
+      ep = readdir (dp);
8e15ce
+      while (ep != NULL){
8e15ce
+
8e15ce
+	/* avoid some system directories */
8e15ce
+        if (!strcmp(ep->d_name,"."))
8e15ce
+            goto next_dir;
8e15ce
+        if (!strcmp(ep->d_name,".."))
8e15ce
+            goto next_dir;
8e15ce
+
8e15ce
+	free (curr_device);
8e15ce
+	free (ret);
8e15ce
+	curr_device = grub_strdup (ep->d_name);
8e15ce
+	ret = grub_xasprintf ("%s%s", directory, curr_device);
8e15ce
+	break;
8e15ce
+
8e15ce
+        next_dir:
8e15ce
+         ep = readdir (dp);
8e15ce
+         continue;
8e15ce
+      }
8e15ce
+      closedir (dp);
8e15ce
+    }
8e15ce
+    else
8e15ce
+      grub_util_warn (_("cannot open directory `%s'"), device_path);
8e15ce
+  }
8e15ce
+
8e15ce
+  free (directory);
8e15ce
+  free (curr_device);
8e15ce
+
8e15ce
+  return ret;
8e15ce
+}
8e15ce
+
8e15ce
 char *
8e15ce
 grub_util_devname_to_ofpath (const char *sys_devname)
8e15ce
 {
8e15ce
-  char *name_buf, *device, *devnode, *devicenode, *ofpath;
8e15ce
+  char *name_buf, *device, *devnode, *devicenode, *ofpath, *realname;
8e15ce
 
8e15ce
   name_buf = xrealpath (sys_devname);
8e15ce
 
8e15ce
+  realname = get_slave_from_dm (name_buf);
8e15ce
+  if (realname)
8e15ce
+    {
8e15ce
+      free (name_buf);
8e15ce
+      name_buf = realname;
8e15ce
+    }
8e15ce
+
8e15ce
   device = get_basename (name_buf);
8e15ce
   devnode = strip_trailing_digits (name_buf);
8e15ce
   devicenode = strip_trailing_digits (device);