grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Arnout Vandecappelle (Essensium/Mind)
Subject: [PATCH] grub-setup: make it possible to specify the root device
Date: Tue, 4 Sep 2012 16:32:48 +0200

From: "Arnout Vandecappelle (Essensium/Mind)" <address@hidden>

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.

Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <address@hidden>
---
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(-)

diff --git a/util/grub-setup.c b/util/grub-setup.c
index 085e8df..ee1b367 100644
--- a/util/grub-setup.c
+++ b/util/grub-setup.c
@@ -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);
 
-- 
1.7.10.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]