Skip to contentSkip to conversation

grub-setup: make it possible to specify the root device

PATCH In review Arnout Vandecappelle (Essensium/Mind) 3 participants 5 messages Sep 4, 2012 – Sep 7, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 grub-setup: make it possible to specify the root device

From: "Arnout Vandecappelle (Essensium/Mind)" <[email protected]>

When creating a bootable disk image in a file rather than on a physical device, it is not possible to guess the root partition. Therefore, allow the root partition to be specified on the command line.

---
If I'm violating patch submission policy somewhere, please let me know and I'll try to correct it. I tried to work with a bzr branch but the initial checkout failed (possibly due to an evil corporate firewall).

Note that a big part of the patch is just an indentation change.

---
 util/grub-setup.c |  108 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 65 insertions(+), 43 deletions(-)
util/grub-setup.c +65 -43
index 085e8df..ee1b367 100644
@@ -141,7 +141,7 @@ write_rootdev (char *core_img, grub_device_t root_dev,
 static void
 setup (const char *dir,
        const char *boot_file, const char *core_file,
-       const char *dest, int force,
+       const char *dest, const char* rootarg, int force,
        int fs_probe, int allow_floppy)
 {
   char *boot_path, *core_path, *core_path_dev, *core_path_dev_full;
@@ -253,50 +253,61 @@ setup (const char *dir,
 
   core_dev = dest_dev;
 
-  {
-    char **root_devices = grub_guess_root_devices (dir);
-    char **cur;
-    int found = 0;
+  if (rootarg && *rootarg)
+    {
+      /* Verify if the supplied root is valid */
+      root_dev = grub_device_open (rootarg);
+      if (root_dev)
+	root = xstrdup(rootarg);
+      else
+	grub_util_error ("supplied root device `%s' is invalid, because of `%s'",
+			 rootarg, grub_errmsg);
+    }
+  else
+    {
+      char **root_devices = grub_guess_root_devices (dir);
+      char **cur;
+      int found = 0;
 
-    for (cur = root_devices; *cur; cur++)
-      {
-	char *drive;
-	grub_device_t try_dev;
-
-	drive = grub_util_get_grub_dev (*cur);
-	if (!drive)
-	  continue;
-	try_dev = grub_device_open (drive);
-	if (! try_dev)
-	  continue;
-	if (!found && try_dev->disk->id == dest_dev->disk->id
-	    && try_dev->disk->dev->id == dest_dev->disk->dev->id)
-	  {
-	    if (root_dev)
-	      grub_device_close (root_dev);
-	    free (root);
-	    root_dev = try_dev;
-	    root = drive;
-	    found = 1;
+      for (cur = root_devices; *cur; cur++)
+	{
+	  char *drive;
+	  grub_device_t try_dev;
+
+	  drive = grub_util_get_grub_dev (*cur);
+	  if (!drive)
 	    continue;
-	  }
-	if (!root_dev)
-	  {
-	    root_dev = try_dev;
-	    root = drive;
+	  try_dev = grub_device_open (drive);
+	  if (! try_dev)
 	    continue;
-	  }
-	grub_device_close (try_dev);	
-	free (drive);
-      }
-    if (!root_dev)
-      {
-	grub_util_error ("guessing the root device failed, because of `%s'",
-			 grub_errmsg);
-      }
-    grub_util_info ("guessed root_dev `%s' from "
-		    "dir `%s'", root_dev->disk->name, dir);
-  }
+	  if (!found && try_dev->disk->id == dest_dev->disk->id
+	      && try_dev->disk->dev->id == dest_dev->disk->dev->id)
+	    {
+	      if (root_dev)
+		grub_device_close (root_dev);
+	      free (root);
+	      root_dev = try_dev;
+	      root = drive;
+	      found = 1;
+	      continue;
+	    }
+	  if (!root_dev)
+	    {
+	      root_dev = try_dev;
+	      root = drive;
+	      continue;
+	    }
+	  grub_device_close (try_dev);
+	  free (drive);
+	}
+      if (!root_dev)
+	{
+	  grub_util_error ("guessing the root device failed, because of `%s'",
+			   grub_errmsg);
+	}
+      grub_util_info ("guessed root_dev `%s' from "
+		      "dir `%s'", root_dev->disk->name, dir);
+    }
 
   grub_util_info ("setting the root device to `%s'", root);
   if (grub_env_set ("root", root) != GRUB_ERR_NONE)
@@ -952,6 +963,8 @@ static struct argp_option options[] = {
    N_("use GRUB files in the directory DIR [default=%s]"), 0},
   {"device-map",  'm', N_("FILE"), 0,
    N_("use FILE as the device map [default=%s]"), 0},
+  {"root-partition",  'r', N_("DEV"), 0,
+   N_("use grub device DEV as the root partition within DEVICE [default=probed]"), 0},
   {"force",       'f', 0,      0,
    N_("install even if problems are detected"), 0},
   {"skip-fs-probe",'s',0,      0,
@@ -993,6 +1006,7 @@ struct arguments
   char *core_file;
   char *dir;
   char *dev_map;
+  char *root;
   int  force;
   int  fs_probe;
   int allow_floppy;
@@ -1040,6 +1054,13 @@ argp_parser (int key, char *arg, struct argp_state *state)
         arguments->dev_map = xstrdup (arg);
         break;
 
+      case 'r':
+        if (arguments->root)
+          free (arguments->root);
+
+        arguments->root = xstrdup (arg);
+        break;
+
       case 'f':
         arguments->force = 1;
         break;
@@ -1172,7 +1193,7 @@ main (int argc, char *argv[])
   setup (arguments.dir ? : DEFAULT_DIRECTORY,
 	 arguments.boot_file ? : DEFAULT_BOOT_FILE,
 	 arguments.core_file ? : DEFAULT_CORE_FILE,
-	 dest_dev, arguments.force,
+	 dest_dev, arguments.root, arguments.force,
 	 arguments.fs_probe, arguments.allow_floppy);
 
   /* Free resources.  */
@@ -1184,6 +1205,7 @@ main (int argc, char *argv[])
   free (arguments.dir);
   free (arguments.dev_map);
   free (arguments.device);
+  free (arguments.root);
   free (root_dev);
   free (dest_dev);
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 5 lines · Arnout Vandecappelle (Essensium/Mind)

From: "Arnout Vandecappelle (Essensium/Mind)" <[email protected]>

When creating a bootable disk image in a file rather than on a physical device, it is not possible to guess the root partition. Therefore, allow the root partition to be specified on the command line.

Looks like you just do it wrong. Correct way is to create a loopback and then use kpartx to discover partitions, not use loopback with offset. This "root device" is purely technical information and it should be discovered automatically from the information we already have (we have a directory)

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#2 lore raw
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 5 lines · Arnout Vandecappelle (Essensium/Mind)

From: "Arnout Vandecappelle (Essensium/Mind)"<[email protected]>

When creating a bootable disk image in a file rather than on a physical device, it is not possible to guess the root partition. Therefore, allow the root partition to be specified on the command line.

Quoted 5 lines

Looks like you just do it wrong. Correct way is to create a loopback and then use kpartx to discover partitions, not use loopback with offset. This "root device" is purely technical information and it should be discovered automatically from the information we already have (we have a directory)

I want to create an image file without superuser access. So I can't mount
a loopback device, so the grub directory is not actually inside the image:
it's the directory from which the target filesystem is created.

If there's another way to do that, I'd be glad to hear it.

By the way, I'm also thinking about patching the build infrastructure
to support building a cross-installer (e.g. grub-mkimage as an x86 executable but cross-compiling lib/grub/powerpc-ieee1275/*). Is there an interest for that?

Regards,
Arnout

Signature · 6 lines
Arnout Vandecappelle             arnout dot vandecappelle at essensium dot com
Senior Embedded Software Architect . . +32-16-286540 . +32-478-010353 (mobile)
Essensium, Mind division . . . . . . . . . . . . . . . . . http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium . . . . . . . . BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint: 31BB CF53 8660 6F88 345D 54CC A836 5879 20D7 CF43
#3 lore raw
Quoted 16 lines · Arnout Vandecappelle
Quoted 9 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 5 lines · Arnout Vandecappelle (Essensium/Mind)

From: "Arnout Vandecappelle (Essensium/Mind)"<[email protected]>

When creating a bootable disk image in a file rather than on a physical device, it is not possible to guess the root partition. Therefore, allow the root partition to be specified on the command line.

Quoted 5 lines

Looks like you just do it wrong. Correct way is to create a loopback and then use kpartx to discover partitions, not use loopback with offset. This "root device" is purely technical information and it should be discovered automatically from the information we already have (we have a directory)

Quoted 6 lines

I want to create an image file without superuser access. So I can't mount
a loopback device, so the grub directory is not actually inside the image:
it's the directory from which the target filesystem is created.

If there's another way to do that, I'd be glad to hear it.

grub-mkrescue

Quoted 6 lines

By the way, I'm also thinking about patching the build infrastructure
to support building a cross-installer (e.g. grub-mkimage as an x86 executable
but cross-compiling lib/grub/powerpc-ieee1275/*). Is there an interest for that?

Long since done. It's also use for automated tests.

Quoted 5 lines

Regards,
Arnout

g

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko
#4 lore raw
Quoted 2 lines · Vladimir 'φ-coder/phcoder' Serbinenko

On 04.09.2012 20:44, Arnout Vandecappelle wrote:

[snip]

Quoted 8 lines
Quoted 6 lines

I want to create an image file without superuser access. So I can't mount
a loopback device, so the grub directory is not actually inside the image:
it's the directory from which the target filesystem is created.

If there's another way to do that, I'd be glad to hear it.

grub-mkrescue

Sorry, I should have set: I want to create an *ext4* image file. grub-mkrescue
only makes an iso image and doesn't let me choose a partitioning. I did look at patching grub-mkrescue first, but that just relies on xorriso to do the heavy lifting so that doesn't help.

Granted, I could just dd the grub image into the mbr, but I wasn't entirely sure
how to do that and this patch was simpler.

Quoted 7 lines
Quoted 5 lines

By the way, I'm also thinking about patching the build infrastructure
to support building a cross-installer (e.g. grub-mkimage as an x86 executable
but cross-compiling lib/grub/powerpc-ieee1275/*). Is there an interest for that?

Long since done. It's also use for automated tests.

Yeah, cool! I didn't know I had to supply TARGET_CC - some other configures
use CC_FOR_BUILD - hence the confusion. Thanks!

Regards,
Arnout

Signature · 6 lines
Arnout Vandecappelle             arnout dot vandecappelle at essensium dot com
Senior Embedded Software Architect . . +32-16-286540 . +32-478-010353 (mobile)
Essensium, Mind division . . . . . . . . . . . . . . . . . http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium . . . . . . . . BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint: 31BB CF53 8660 6F88 345D 54CC A836 5879 20D7 CF43