grub-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] Add option to grub-probe to accept system devices as argumen


From: Fabian Greffrath
Subject: Re: [PATCH] Add option to grub-probe to accept system devices as arguments
Date: Tue, 12 Feb 2008 10:49:37 +0100
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

You put that function in a separate file, which indicates it is meant to be
a general-purpose function, but its spec is constrained by the code in
grub-probe (i.e. you strdup() not because it is needed for the purpose of
this function, but because grub-probe already calls free()).

That's right. Consequently there are two alternatives left:

- grub_util_check_block_device() returns the pointer instead of strdup() and the
call to free() in grub-probe is conditional upon if argument_is_device is true
or false; which I consider an ugly hack.

- I keep getroot.{c,h} untouched and include these four lines in grub-probe;
which is a pity because I consider the function quite usefull, though still not
perfect (see above).

In the context of the second alternative, the attached (draft!) patch leads to the following results:

        $ mount
        /dev/hda1 on / type ext3 (rw,errors=remount-ro)
        /dev/hda6 on /home type ext3 (rw)
        $ su
        
        # grub-probe
        No path or device is specified.
        Try ``grub-probe --help'' for more information.
        # grub-probe /home
        ext2
        # grub-probe /homer
        grub-probe: error: cannot stat /homer.
        
        # grub-probe /dev/hda1
        ext2
        # grub-probe --target=drive
        No path or device is specified.
        Try ``grub-probe --help'' for more information.
        # grub-probe --target=drive /home
        (hd0,6)
        # grub-probe --target=drive /homer
        grub-probe: error: cannot stat /homer.
        
        # grub-probe --target=drive /dev/hda1
        (hd0,1)

How do you like that?

Cheers,
Fabian

diff -Nurp grub2-1.96+20080210/util/grub-probe.c 
grub2-1.96+20080210.new/util/grub-probe.c
--- grub2-1.96+20080210/util/grub-probe.c       2008-02-09 11:49:29.000000000 
+0100
+++ grub2-1.96+20080210.new/util/grub-probe.c   2008-02-12 10:16:45.000000000 
+0100
@@ -1,4 +1,4 @@
-/* grub-probe.c - probe device information for a given path */
+/* grub-probe.c - probe device information for a given path or device */
 /*
  *  GRUB  --  GRand Unified Bootloader
  *  Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
@@ -100,7 +100,7 @@ probe_partmap (grub_disk_t disk)
 }
 
 static void
-probe (const char *path)
+probe (const char *argument)
 {
   char *device_name;
   char *drive_name = NULL;
@@ -108,10 +108,18 @@ probe (const char *path)
   char *filebuf_via_grub = NULL, *filebuf_via_sys = NULL;
   int abstraction_type;
   grub_device_t dev = NULL;
-  
-  device_name = grub_guess_root_device (path);
+  struct stat st;
+
+  if (stat (argument, &st) < 0)
+     grub_util_error ("cannot stat %s.\n", argument);
+
+  if (S_ISBLK (st.st_mode))
+    device_name = strdup (argument);
+  else
+    device_name = grub_guess_root_device (argument);
+
   if (! device_name)
-    grub_util_error ("cannot find a device for %s.\n", path);
+    grub_util_error ("cannot find a device for %s.\n", argument);
 
   if (print == PRINT_DEVICE)
     {
@@ -178,21 +186,18 @@ probe (const char *path)
 
   if (print == PRINT_FS)
     {
-      struct stat st;
       grub_fs_t fs;
 
-      stat (path, &st);
-
       if (st.st_mode == S_IFREG)
        {
          /* Regular file.  Verify that we can read it properly.  */
 
          grub_file_t file;
-         grub_util_info ("reading %s via OS facilities", path);
-         filebuf_via_sys = grub_util_read_image (path);
+         grub_util_info ("reading %s via OS facilities", argument);
+         filebuf_via_sys = grub_util_read_image (argument);
          
-         grub_util_info ("reading %s via GRUB facilities", path);
-         asprintf (&grub_path, "(%s)%s", drive_name, path);
+         grub_util_info ("reading %s via GRUB facilities", argument);
+         asprintf (&grub_path, "(%s)%s", drive_name, argument);
          file = grub_file_open (grub_path);
          filebuf_via_grub = xmalloc (file->size);
          grub_file_read (file, filebuf_via_grub, file->size);
@@ -242,9 +247,9 @@ usage (int status)
             "Try ``grub-probe --help'' for more information.\n");
   else
     printf ("\
-Usage: grub-probe [OPTION]... PATH\n\
+Usage: grub-probe [OPTION]... [PATH|DEVICE]\n\
 \n\
-Probe device information for a given path.\n\
+Probe device information for a given path or device.\n\
 \n\
   -m, --device-map=FILE     use FILE as the device map [default=%s]\n\
   -t, --target=(fs|drive|device|partmap|abstraction)\n\
@@ -264,7 +269,7 @@ int
 main (int argc, char *argv[])
 {
   char *dev_map = 0;
-  char *path;
+  char *argument;
   
   progname = "grub-probe";
   
@@ -321,10 +326,10 @@ main (int argc, char *argv[])
   if (verbosity > 1)
     grub_env_set ("debug", "all");
 
-  /* Obtain PATH.  */
+  /* Obtain PATH or DEVICE.  */
   if (optind >= argc)
     {
-      fprintf (stderr, "No path is specified.\n");
+      fprintf (stderr, "No path or device is specified.\n");
       usage (1);
     }
 
@@ -334,7 +339,7 @@ main (int argc, char *argv[])
       usage (1);
     }
 
-  path = argv[optind];
+  argument = argv[optind];
   
   /* Initialize the emulated biosdisk driver.  */
   grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
@@ -343,7 +348,7 @@ main (int argc, char *argv[])
   grub_init_all ();
 
   /* Do it.  */
-  probe (path);
+  probe (argument);
   
   /* Free resources.  */
   grub_fini_all ();

reply via email to

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