All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH] Add option to grub-probe to accept system devices as arguments
@ 2008-02-12  9:49 Fabian Greffrath
  2008-02-12 11:28 ` Robert Millan
  0 siblings, 1 reply; 15+ messages in thread
From: Fabian Greffrath @ 2008-02-12  9:49 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 1474 bytes --]

> 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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: grub-probe-20080212-1.diff --]
[-- Type: text/x-patch; name="grub-probe-20080212-1.diff", Size: 3609 bytes --]

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 ();

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [PATCH] Add option to grub-probe to accept system devices as arguments
@ 2008-02-12 13:17 Fabian Greffrath
  2008-02-13 13:03 ` Fabian Greffrath
  2008-02-13 15:42 ` Robert Millan
  0 siblings, 2 replies; 15+ messages in thread
From: Fabian Greffrath @ 2008-02-12 13:17 UTC (permalink / raw)
  To: The development of GRUB 2

Please have a look at the patch I provided in my last mail, too. This is a 
different approach which requires no further command line option but *detects* 
whether the given argument is a path or a device.



^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [PATCH] Add option to grub-probe to accept system devices as arguments
@ 2008-02-11 15:48 Fabian Greffrath
  2008-02-11 16:12 ` Robert Millan
  0 siblings, 1 reply; 15+ messages in thread
From: Fabian Greffrath @ 2008-02-11 15:48 UTC (permalink / raw)
  To: The development of GRUB 2

Hi Robert,

> You need to list every function or variable separately.  [...]
> If you use the -p option to diff it'll be easier to check it just by reading the
> patch.

OK, thanks, I didn't know this. Next time... ;)

> Is there really a need to strdup() it?

Yep. If you return the pointer (i.e. to `path') you'll get memory corruption as 
soon as you try to free (device_name).

> I find it confusing that you change the meaning of the `path' variable without
> renaming it.  Also, the variable that describes what `path' is
> (argument_is_device) is passed separately.

Allright. Maybe the `path' variable should be renamed to `argument'.

> What would you think of a scheme where both are passed as strings and either
> can be NULL? 

Well, I prefer the idea to have the argument stored in one string and have a 
separate boolean switch that tells if the string contains a device name or a 
path. But I am always open to suggestions. ;)

> I suspect advertising it here might lead users to think they can pass a device
> to it, without caring about [...] this option.

Allright, this should be changed to something like this:
"Probe device information for a given path (or device, if the -d option is given)."

Thanks!

Cheers,
Fabian



^ permalink raw reply	[flat|nested] 15+ messages in thread
* [PATCH] Add option to grub-probe to accept system devices as arguments
@ 2008-02-11  8:53 Fabian Greffrath
  2008-02-11 14:39 ` Robert Millan
  0 siblings, 1 reply; 15+ messages in thread
From: Fabian Greffrath @ 2008-02-11  8:53 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 297 bytes --]

The following patch adds to grub-probe the ability to accept system devices as
arguments and e.g. convert between system devices and GRUB drives.

This patch is improved over the one from my previous posting and has some minor
issues fixed. Please use this version instead. It is all my work.





[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: grub-probe.diff --]
[-- Type: text/x-patch; name="grub-probe.diff", Size: 3859 bytes --]

2008-02-11  Fabian Greffrath  <fabian.greffrath@web.de>

	* util/grub-probe.c: Add new parameter '-d, --device'. If this is set,
	grub-probe expects the given argument to be a block device. All of the
	'--target' parameters work with this option as well. If the '--device'
	parameter is not set, grub-probe will work as before.

	* util/getroot.c (grub_util_check_block_device): New public function
	that returns the given argument if it is a block device and returns
	NULL else.

	* util/getroot.h (grub_util_check_block_device): Export function.

diff -Naru grub2-1.96+20080203~/include/grub/util/getroot.h grub2-1.96+20080203/include/grub/util/getroot.h
--- grub2-1.96+20080203~/include/grub/util/getroot.h	2008-01-12 16:11:56.000000000 +0100
+++ grub2-1.96+20080203/include/grub/util/getroot.h	2008-02-08 12:49:52.000000000 +0100
@@ -29,5 +29,6 @@
 char *grub_get_prefix (const char *dir);
 int grub_util_get_dev_abstraction (const char *os_dev);
 char *grub_util_get_grub_dev (const char *os_dev);
+char *grub_util_check_block_device (const char *blk_dev);
 
 #endif /* ! GRUB_UTIL_GETROOT_HEADER */
diff -Naru grub2-1.96+20080203~/util/getroot.c grub2-1.96+20080203/util/getroot.c
--- grub2-1.96+20080203~/util/getroot.c	2008-01-12 16:11:56.000000000 +0100
+++ grub2-1.96+20080203/util/getroot.c	2008-02-08 12:49:52.000000000 +0100
@@ -327,3 +327,17 @@
 
   return grub_dev;
 }
+
+char *
+grub_util_check_block_device (const char *blk_dev)
+{
+  struct stat st;
+
+  if (stat (blk_dev, &st) < 0)
+    grub_util_error ("Cannot stat `%s'", blk_dev);
+
+  if (S_ISBLK (st.st_mode))
+    return strdup (blk_dev);
+  else
+    return 0;
+}
diff -Naru grub2-1.96+20080203~/util/grub-probe.c grub2-1.96+20080203/util/grub-probe.c
--- grub2-1.96+20080203~/util/grub-probe.c	2008-01-25 23:33:57.000000000 +0100
+++ grub2-1.96+20080203/util/grub-probe.c	2008-02-08 12:50:13.000000000 +0100
@@ -50,6 +50,7 @@
 };
 
 int print = PRINT_FS;
+static unsigned int argument_is_device = 0;
 
 void
 grub_putchar (int c)
@@ -84,9 +85,18 @@
   int abstraction_type;
   grub_device_t dev = NULL;
   
-  device_name = grub_guess_root_device (path);
+  if (argument_is_device)
+    device_name = grub_util_check_block_device (path);
+  else
+    device_name = grub_guess_root_device (path);
+
   if (! device_name)
-    grub_util_error ("cannot find a device for %s.\n", path);
+    {
+      if (argument_is_device)
+        grub_util_error ("%s is not a block device.\n", path);
+      else
+        grub_util_error ("cannot find a device for %s.\n", path);
+    }
 
   if (print == PRINT_DEVICE)
     {
@@ -201,6 +211,7 @@
 
 static struct option options[] =
   {
+    {"device", no_argument, 0, 'd'},
     {"device-map", required_argument, 0, 'm'},
     {"target", required_argument, 0, 't'},
     {"help", no_argument, 0, 'h'},
@@ -217,10 +228,11 @@
 	     "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\
+  -d, --device              given argument is a system device, not a path\n\
   -m, --device-map=FILE     use FILE as the device map [default=%s]\n\
   -t, --target=(fs|drive|device|partmap|abstraction)\n\
                             print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
@@ -246,13 +258,17 @@
   /* Check for options.  */
   while (1)
     {
-      int c = getopt_long (argc, argv, "m:t:hVv", options, 0);
+      int c = getopt_long (argc, argv, "dm:t:hVv", options, 0);
       
       if (c == -1)
 	break;
       else
 	switch (c)
 	  {
+	  case 'd':
+	    argument_is_device = 1;
+	    break;
+
 	  case 'm':
 	    if (dev_map)
 	      free (dev_map);



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2008-02-20 14:13 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-12  9:49 [PATCH] Add option to grub-probe to accept system devices as arguments Fabian Greffrath
2008-02-12 11:28 ` Robert Millan
  -- strict thread matches above, loose matches on Subject: below --
2008-02-12 13:17 Fabian Greffrath
2008-02-13 13:03 ` Fabian Greffrath
2008-02-13 15:42 ` Robert Millan
2008-02-13 16:51   ` Fabian Greffrath
2008-02-13 19:35     ` Robert Millan
2008-02-14  8:33       ` Fabian Greffrath
2008-02-17 13:42         ` Robert Millan
2008-02-18 12:54           ` Fabian Greffrath
2008-02-20 14:14           ` Fabian Greffrath
2008-02-11 15:48 Fabian Greffrath
2008-02-11 16:12 ` Robert Millan
2008-02-11  8:53 Fabian Greffrath
2008-02-11 14:39 ` Robert Millan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.