Skip to contentSkip to conversation

getroot: Skip mount points in grub_find_device

PATCH In review Michael Chang via Grub-devel 2 participants 2 messages Sep 16, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getroot: Skip mount points in grub_find_device

The grub_find_device function scans a starting directory, typically /dev, for device files with matching major and minor numbers. During this process, it recursively descends into subdirectories.

However, this can significantly slow down the scan if a subdirectory is a mount point not related to devtmpfs, especially if it contains a large number of files.

This patch modifies grub_find_device() to skip subdirectories that are mount points. A mount point is detected by comparing the st_dev of the subdirectory against that of the parent or starting directory. While this method does not catch all types of mounts, for eg bind mounts, it is a practical solution that avoids the need to parse /proc/self/mounts.

---
 grub-core/osdep/unix/getroot.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
grub-core/osdep/unix/getroot.c +14 -0
index c7aa202ab..c3fc28ca8 100644
@@ -353,6 +353,7 @@ grub_find_device (const char *dir, dev_t dev)
   DIR *dp;
   struct saved_cwd saved_cwd;
   struct dirent *ent;
+  struct stat st_dir;
 
   if (! dir)
     dir = "/dev";
@@ -361,6 +362,12 @@ grub_find_device (const char *dir, dev_t dev)
   if (! dp)
     return 0;
 
+  if (stat (dir, &st_dir) < 0)
+    {
+      closedir (dp);
+      return 0;
+    }
+
   if (save_cwd (&saved_cwd) < 0)
     {
       grub_util_error ("%s", _("cannot save the original directory"));
@@ -410,6 +417,13 @@ grub_find_device (const char *dir, dev_t dev)
 	  /* Find it recursively.  */
 	  char *res;
 
+	  /* Skip mount point */
+	  if (st.st_dev != st_dir.st_dev)
+	    {
+	      grub_util_info ("skip mount point %s/%s", dir, ent->d_name);
+	      continue;
+	    }
+
 	  res = grub_find_device (ent->d_name, dev);
 
 	  if (res)
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Michael Chang via Grub-devel

The grub_find_device function scans a starting directory, typically /dev, for device files with matching major and minor numbers. During this process, it recursively descends into subdirectories.

However, this can significantly slow down the scan if a subdirectory is a mount point not related to devtmpfs, especially if it contains a large number of files.

This patch modifies grub_find_device() to skip subdirectories that are mount points. A mount point is detected by comparing the st_dev of the subdirectory against that of the parent or starting directory. While this method does not catch all types of mounts, for eg bind mounts, it is a practical solution that avoids the need to parse /proc/self/mounts.

Quoted 5 lines
---
 grub-core/osdep/unix/getroot.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

This looks reasonable to me.

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

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