All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabian Greffrath <greffrath@leat.rub.de>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: [PATCH] Add option to grub-probe to accept system devices as arguments
Date: Tue, 12 Feb 2008 10:49:37 +0100	[thread overview]
Message-ID: <47B16BB1.2000006@leat.rub.de> (raw)

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

             reply	other threads:[~2008-02-12  9:49 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-12  9:49 Fabian Greffrath [this message]
2008-02-12 11:28 ` [PATCH] Add option to grub-probe to accept system devices as arguments 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47B16BB1.2000006@leat.rub.de \
    --to=greffrath@leat.rub.de \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.