Skip to contentSkip to conversation

Add support for /dev/md/N style mdraid devices

PATCH In review Felix Zielcke 3 participants 7 messages Jul 30, 2008 – Aug 5, 2008 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for /dev/md/N style mdraid devices

Here's a patch to add support for the /dev/md/N form of mdraid devices.

2008-07-30 Felix Zielcke <[email protected]>

* util/getroot.c: Add support for /dev/md/N style mdraid devices.

getroot.c +6 -0
Index: util/getroot.c
===================================================================
@@ -460,6 +460,12 @@
 	  memcpy (grub_dev, os_dev + 5, 7);
 	  grub_dev[7] = '\0';
 	}
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  memcpy (grub_dev, os_dev + 5, 2);
+	  memcpy (grub_dev + 2, os_dev + 8, 5);
+	  grub_dev[7] = '\0';
+	}
       else
 	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
#1 lore raw

getroot.c line 468

Quoted 6 lines · Felix Zielcke
getroot.c +6 -0
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  memcpy (grub_dev, os_dev + 5, 2);
+	  memcpy (grub_dev + 2, os_dev + 8, 5);
+	  grub_dev[7] = '\0';
+	}

This looks fine, but I'd suggest to avoid hardcoding numbers when possible. So if you're adding 5 because that's the size of "/dev/", I think it's a good idea to use sizeof("/dev/")-1, and so on. This way the code is much easier to understand IMHO.

(yes, I know the existing code above yours doesn't do this, but i think it's a good idea to push for higher quality standards anyway ;-))

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#2 lore raw

Am Freitag, den 01.08.2008, 15:12 +0200 schrieb Robert Millan:

Quoted 8 lines

This looks fine, but I'd suggest to avoid hardcoding numbers when possible. So if you're adding 5 because that's the size of "/dev/", I think it's a good idea to use sizeof("/dev/")-1, and so on. This way the code is much easier to understand IMHO.

(yes, I know the existing code above yours doesn't do this, but i think it's a good idea to push for higher quality standards anyway ;-))

Attached are now 2 patches for my added code and the one above. I personally prefer the strcpy one, there's no need to use memcpy, it should never happen that the /dev/md* devices contain a ASCII NUL Maybe there's a way to make it even better so please comment all of you grub-devels :)

getroot.c +7 -1
Index: util/getroot.c
===================================================================
@@ -457,9 +457,15 @@
 	}
       else if (os_dev[7] >= '0' && os_dev[7] <= '9')
 	{
-	  memcpy (grub_dev, os_dev + 5, 7);
+	  memcpy (grub_dev, os_dev + sizeof("/dev/") -1, 7);
 	  grub_dev[7] = '\0';
 	}
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  memcpy (grub_dev, "md", sizeof("md") -1);
+	  memcpy (grub_dev + sizeof ("md") -1, os_dev + sizeof("/dev/md/") -1, 5);
+	  grub_dev[7] = '\0';
+	}
       else
 	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
getroot.c +8 -1
Index: util/getroot.c
===================================================================
@@ -457,9 +457,16 @@
 	}
       else if (os_dev[7] >= '0' && os_dev[7] <= '9')
 	{
-	  memcpy (grub_dev, os_dev + 5, 7);
+	  strcpy (grub_dev,"md");
+	  strncpy (grub_dev, os_dev + sizeof("/dev/md") -1,5);
 	  grub_dev[7] = '\0';
 	}
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  strcpy (grub_dev,"md");
+	  strncpy (grub_dev + sizeof ("md") -1, os_dev + sizeof("/dev/md/") -1, 5);
+	  grub_dev[7] = '\0';
+	}
       else
 	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
#3 lore raw

Am Freitag, den 01.08.2008, 17:09 +0200 schrieb Felix Zielcke:

Quoted 2 lines

Maybe there's a way to make it even better so please comment all of you grub-devels :)

Thanks to Robert who suggested asprintf

I hope the #define _GNU_SOURCE which is needed for it isn't a problem, you use anyway gcc specific extentions and GRUB is a GNU project :)

I hadn't yet an idea how to solve that md_dNNpNN case with it, so I just moved the xmalloc() call down to that.

Please comment which way you mostly like.

getroot.c +9 -6
Index: util/getroot.c
===================================================================
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _GNU_SOURCE
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
@@ -407,17 +408,16 @@
   switch (grub_util_get_dev_abstraction (os_dev))
     {
     case GRUB_DEV_ABSTRACTION_LVM:
-      grub_dev = xmalloc (strlen (os_dev) - 12 + 1);
 
-      strcpy (grub_dev, os_dev + 12);
-
+      asprintf(&grub_dev,"%s",os_dev + strlen("/dev/mapper/") -1);
       break;
 
     case GRUB_DEV_ABSTRACTION_RAID:
-      grub_dev = xmalloc (20);
 
       if (os_dev[7] == '_' && os_dev[8] == 'd')
 	{
+	  grub_dev = xmalloc (20);
+
 	  const char *p;
 
 	  /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
@@ -457,9 +457,12 @@
 	}
       else if (os_dev[7] >= '0' && os_dev[7] <= '9')
 	{
-	  memcpy (grub_dev, os_dev + 5, 7);
-	  grub_dev[7] = '\0';
+	  asprintf(&grub_dev,"md%s",os_dev + sizeof("/dev/md") -1);
 	}
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  asprintf(&grub_dev,"md%s",os_dev + sizeof("/dev/md/") -1);
+	}
       else
 	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
#4 lore raw
Quoted 9 lines · Felix Zielcke

Am Freitag, den 01.08.2008, 17:09 +0200 schrieb Felix Zielcke:

Quoted 2 lines

Maybe there's a way to make it even better so please comment all of you grub-devels :)

Thanks to Robert who suggested asprintf

I hope the #define _GNU_SOURCE which is needed for it isn't a problem, you use anyway gcc specific extentions and GRUB is a GNU project :)

AFAIK it isn't, so better leave it away.

Quoted 2 lines

I hadn't yet an idea how to solve that md_dNNpNN case with it, so I just moved the xmalloc() call down to that.

What's the problem?

Quoted 1 line

Please comment which way you mostly like.

I would prefer a changelog entry while commenting.

Quoted 13 lines

Index: util/getroot.c
===================================================================

getroot.c +1 -0
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _GNU_SOURCE
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>

getroot.c line 412

Quoted 8 lines
getroot.c +1 -3
@@ -407,17 +408,16 @@
   switch (grub_util_get_dev_abstraction (os_dev))
     {
     case GRUB_DEV_ABSTRACTION_LVM:
-      grub_dev = xmalloc (strlen (os_dev) - 12 + 1);
 
-      strcpy (grub_dev, os_dev + 12);
-
+      asprintf(&grub_dev,"%s",os_dev + strlen("/dev/mapper/") -1);

Please add a space between asprintf and (&grub_dev ....

Same for the lines below.

Quoted 31 lines
getroot.c +7 -3
       break;
 
     case GRUB_DEV_ABSTRACTION_RAID:
-      grub_dev = xmalloc (20);
 
       if (os_dev[7] == '_' && os_dev[8] == 'd')
 	{
+	  grub_dev = xmalloc (20);
+
 	  const char *p;
 
 	  /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
@@ -457,9 +457,12 @@
 	}
       else if (os_dev[7] >= '0' && os_dev[7] <= '9')
 	{
-	  memcpy (grub_dev, os_dev + 5, 7);
-	  grub_dev[7] = '\0';
+	  asprintf(&grub_dev,"md%s",os_dev + sizeof("/dev/md") -1);
 	}
+      else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	{
+	  asprintf(&grub_dev,"md%s",os_dev + sizeof("/dev/md/") -1);
+	}
       else
 	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 

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

#6 lore raw
In reply to a message not in the archive

Am Dienstag, den 05.08.2008, 15:03 +0200 schrieb Felix Zielcke:

Quoted 3 lines

I moved it now to include/util/misc.h so in the future it can be used for all utils.

I just recompiled it with an 'svn up' before and a full compile is broken with that way.
I just include config.h now which defines this.

2008-08-05 Felix Zielcke <[email protected]>

* util/getroot.c: Include <config.h>.
(grub_util_get_grub_dev): Rewritten.

getroot.c +27 -53
Index: util/getroot.c
===================================================================
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
@@ -406,67 +407,40 @@
 
   switch (grub_util_get_dev_abstraction (os_dev))
     {
-    case GRUB_DEV_ABSTRACTION_LVM:
-      grub_dev = xmalloc (strlen (os_dev) - 12 + 1);
+      case GRUB_DEV_ABSTRACTION_LVM:
 
-      strcpy (grub_dev, os_dev + 12);
+        asprintf (&grub_dev, "%s", os_dev + sizeof ("/dev/mapper/") -1);
+        break;
 
-      break;
+      case GRUB_DEV_ABSTRACTION_RAID:
 
-    case GRUB_DEV_ABSTRACTION_RAID:
-      grub_dev = xmalloc (20);
+        if (os_dev[7] == '_' && os_dev[8] == 'd')
+	  {
+	    /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
 
-      if (os_dev[7] == '_' && os_dev[8] == 'd')
-	{
-	  const char *p;
+	    char *p;
 
-	  /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
-	  int i;
+	    p = strchr (os_dev, 'p');
+	    if (p)
+	      *p = ',';
 
-	  grub_dev[0] = 'm';
-	  grub_dev[1] = 'd';
-	  i = 2;
-	  
-	  p = os_dev + 9;
-	  while (*p >= '0' && *p <= '9')
-	    {
-	      grub_dev[i] = *p;
-	      i++;
-	      p++;
-	    }
+	    asprintf (grub_dev, "md%s", os_dev + sizeof ("/dev/md_d") -1);
+	  }
+        else if (os_dev[7] >= '0' && os_dev[7] <= '9')
+	  {
+	    asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md") -1);
+	  }
+        else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	  {
+	    asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md/") -1);
+	  }
+        else
+	  grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
-	  if (*p == '\0')
-	    grub_dev[i] = '\0';
-	  else if (*p == 'p')
-	    {
-	      p++;
-	      grub_dev[i] = ',';
-	      i++;
+        break;
 
-	      while (*p >= '0' && *p <= '9')
-		{
-		  grub_dev[i] = *p;
-		  i++;
-		  p++;
-		}
-
-	      grub_dev[i] = '\0';
-	    }
-	  else
-	    grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
-	}
-      else if (os_dev[7] >= '0' && os_dev[7] <= '9')
-	{
-	  memcpy (grub_dev, os_dev + 5, 7);
-	  grub_dev[7] = '\0';
-	}
-      else
-	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
-
-      break;
-
-    default:  /* GRUB_DEV_ABSTRACTION_NONE */
-      grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
+      default:  /* GRUB_DEV_ABSTRACTION_NONE */
+        grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
     }
 
   return grub_dev;
#7 lore raw

Am Dienstag, den 05.08.2008, 15:36 +0200 schrieb Felix Zielcke:

Quoted 10 lines

Am Dienstag, den 05.08.2008, 15:03 +0200 schrieb Felix Zielcke:

Quoted 3 lines

I moved it now to include/util/misc.h so in the future it can be used for all utils.

I just recompiled it with an 'svn up' before and a full compile is broken with that way.
I just include config.h now which defines this.

I should really look more at the code before mailing it. I forgot a & and I've now changed at few lines the spaces to a \t What I don't get it why the .diff looks like there is only one space more between some lines, but vi shows 2 on the real modified file.

2008-08-05 Felix Zielcke <[email protected]>

* util/getroot.c: Include <config.h>.
(grub_util_get_grub_dev): Rewritten.

getroot.c +27 -53
Index: util/getroot.c
===================================================================
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
@@ -406,67 +407,40 @@
 
   switch (grub_util_get_dev_abstraction (os_dev))
     {
-    case GRUB_DEV_ABSTRACTION_LVM:
-      grub_dev = xmalloc (strlen (os_dev) - 12 + 1);
+      case GRUB_DEV_ABSTRACTION_LVM:
 
-      strcpy (grub_dev, os_dev + 12);
+        asprintf (&grub_dev, "%s", os_dev + sizeof ("/dev/mapper/") -1);
+        break;
 
-      break;
+      case GRUB_DEV_ABSTRACTION_RAID:
 
-    case GRUB_DEV_ABSTRACTION_RAID:
-      grub_dev = xmalloc (20);
+	if (os_dev[7] == '_' && os_dev[8] == 'd')
+	  {
+	    /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
 
-      if (os_dev[7] == '_' && os_dev[8] == 'd')
-	{
-	  const char *p;
+	    char *p;
 
-	  /* This a partitionable RAID device of the form /dev/md_dNNpMM. */
-	  int i;
+	    p = strchr (os_dev, 'p');
+	    if (p)
+	      *p = ',';
 
-	  grub_dev[0] = 'm';
-	  grub_dev[1] = 'd';
-	  i = 2;
-	  
-	  p = os_dev + 9;
-	  while (*p >= '0' && *p <= '9')
-	    {
-	      grub_dev[i] = *p;
-	      i++;
-	      p++;
-	    }
+	    asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md_d") -1);
+	  }
+	else if (os_dev[7] >= '0' && os_dev[7] <= '9')
+	  {
+	    asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md") -1);
+	  }
+	else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
+	  {
+	    asprintf (&grub_dev, "md%s", os_dev + sizeof ("/dev/md/") -1);
+	  }
+	else
+	  grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
 
-	  if (*p == '\0')
-	    grub_dev[i] = '\0';
-	  else if (*p == 'p')
-	    {
-	      p++;
-	      grub_dev[i] = ',';
-	      i++;
+        break;
 
-	      while (*p >= '0' && *p <= '9')
-		{
-		  grub_dev[i] = *p;
-		  i++;
-		  p++;
-		}
-
-	      grub_dev[i] = '\0';
-	    }
-	  else
-	    grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
-	}
-      else if (os_dev[7] >= '0' && os_dev[7] <= '9')
-	{
-	  memcpy (grub_dev, os_dev + 5, 7);
-	  grub_dev[7] = '\0';
-	}
-      else
-	grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
-
-      break;
-
-    default:  /* GRUB_DEV_ABSTRACTION_NONE */
-      grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
+      default:  /* GRUB_DEV_ABSTRACTION_NONE */
+	grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
     }
 
   return grub_dev;