Skip to contentSkip to conversation

grub-probe -t prefix, fix update-grub_lib for Cygwin

PATCH In review Christian Franke 2 participants 6 messages Jul 20, 2008 – Jul 29, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 grub-probe -t prefix, fix update-grub_lib for Cygwin

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX.
(probe): Add PRINT_PREFIX, prints result of grub_get_prefix ().
(usage): Add prefix' to -t' usage text.
Add some '\n' to avoid excess long lines.
(main): Add check for `-t prefix' option.
* util/update-grub_lib.in (make_system_path_relative_to_its_root):
Use result of `grub-probe -t prefix' instead of checking device numbers of parent directories.
The latter does not work on Cygwin.

util/grub-probe.c +20 -2
index a4f51e2..d36c2cf 100644
@@ -46,6 +46,7 @@ enum {
   PRINT_FS,
   PRINT_FS_UUID,
   PRINT_DRIVE,
+  PRINT_PREFIX,
   PRINT_DEVICE,
   PRINT_PARTMAP,
   PRINT_ABSTRACTION,
@@ -113,6 +114,19 @@ probe (const char *path, char *device_name)
   grub_device_t dev = NULL;
   grub_fs_t fs;
   
+  if (print == PRINT_PREFIX)
+    {
+      if (! path)
+        grub_util_error ("cannot find prefix for a device.\n");
+      char * prefix = grub_get_prefix (path);
+      if (! prefix)
+        grub_util_error ("cannot find prefix for %s.\n", path);
+
+      printf ("%s\n", prefix);
+      free (prefix);
+      goto end;
+    }
+
   if (path == NULL)
     {
       if (! grub_util_check_block_device (device_name))
@@ -264,8 +278,10 @@ Probe device information for a given path (or device, if the -d option is given)
 \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|fs_uuid|drive|device|partmap|abstraction)\n\
-                            print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
+  -t, --target=(fs|fs_uuid|drive|prefix|device|partmap|abstraction)\n\
+                            print filesystem module, GRUB drive, path prefix,\n\
+                            system device, partition map module or\n\
+                            abstraction module [default=fs]\n\
   -h, --help                display this message and exit\n\
   -V, --version             print version information and exit\n\
   -v, --verbose             print verbose messages\n\
@@ -313,6 +329,8 @@ main (int argc, char *argv[])
 	      print = PRINT_FS_UUID;
 	    else if (!strcmp (optarg, "drive"))
 	      print = PRINT_DRIVE;
+	    else if (!strcmp (optarg, "prefix"))
+	      print = PRINT_PREFIX;
 	    else if (!strcmp (optarg, "device"))
 	      print = PRINT_DEVICE;
 	    else if (!strcmp (optarg, "partmap"))
util/update-grub_lib.in +6 -16
index c488a85..163b143 100644
@@ -41,25 +41,14 @@ make_system_path_relative_to_its_root ()
   # if not a directory, climb up to the directory containing it
   if test -d $path ; then
     dir=$path
+    file=
   else
     dir=`echo $path | sed -e "s,/[^/]*$,,g"`
+    file=`echo $path | sed -e "s,^.*/,/,g"`
   fi
 
-  num=`stat -c %d $dir`
-
-  # this loop sets $dir to the root directory of the filesystem we're inspecting
-  while : ; do
-    parent=`readlink -f $dir/..`
-    if [ "x`stat -c %d $parent`" = "x$num" ] ; then : ; else
-      # $parent is another filesystem; we found it.
-      break
-    fi
-    if [ "x$dir" = "x/" ] ; then
-      # / is our root.
-      break
-    fi
-    dir=$parent
-  done
+  # get directory prefix relative to its root
+  dir=`${grub_probe} -t prefix "$dir"` || return 1
 
   # This function never prints trailing slashes (so that its output can be
   # appended a slash unconditionally).  Each slash in $dir is considered a
@@ -68,7 +57,8 @@ make_system_path_relative_to_its_root ()
     dir=""
   fi
 
-  echo $path | sed -e "s,^$dir,,g"
+  # re-append file if necessary
+  echo "$dir""$file"
 }
 
 is_path_readable_by_grub ()
#1 lore raw
Quoted 11 lines · Christian Franke

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

Quoted 8 lines

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX. (probe): Add PRINT_PREFIX, prints result of grub_get_prefix ().
(usage): Add prefix' to -t' usage text. Add some '\n' to avoid excess long lines. (main): Add check for `-t prefix' option.

This is different than the usual grub-probe usage in that it doesn't operate on a device. Perhaps it would make sense to add the functionality you want in a separate utility?

I have an increasing feeling that grub-probe is becoming bloated (though I admit, this is mostly my fault)

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What good is a phone call… if you are unable to speak?
(as seen on /.)
#2 lore raw
Quoted 19 lines · Robert Millan
Quoted 7 lines · Christian Franke

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

Quoted 9 lines

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX. (probe): Add PRINT_PREFIX, prints result of grub_get_prefix ().
(usage): Add prefix' to -t' usage text. Add some '\n' to avoid excess long lines. (main): Add check for `-t prefix' option.

Quoted 5 lines

This is different than the usual grub-probe usage in that it doesn't operate on a device. Perhaps it would make sense to add the functionality you want in a separate utility?

OK, probably /usr/bin/grub-prefix ?

Drawback: getroot.c contains also grub_guess_root_device() etc, so a grub-prefix program would require most of the objects grub-probe requires.

Put grub_get_prefix() in a new util/getprefix.c ?

Or handle those platform dependencies in update-grub_lib.in itself. Can be done by a conditional call to 'cygpath' utility. This conditional must then be removed for the other platforms when generating update-grub_lib.

Christian

#3 lore raw
Quoted 26 lines · Christian Franke
Quoted 19 lines · Robert Millan
Quoted 7 lines · Christian Franke

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

Quoted 9 lines

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX.
(probe): Add PRINT_PREFIX, prints result of
grub_get_prefix ().
(usage): Add prefix' to -t' usage text.
Add some '\n' to avoid excess long lines.
(main): Add check for `-t prefix' option.

Quoted 5 lines

This is different than the usual grub-probe usage in that it doesn't operate on a device. Perhaps it would make sense to add the functionality you want in a separate utility?

Quoted 9 lines

OK, probably /usr/bin/grub-prefix ?

Drawback: getroot.c contains also grub_guess_root_device() etc, so a grub-prefix program would require most of the objects grub-probe requires.

Put grub_get_prefix() in a new util/getprefix.c ?

Or handle those platform dependencies in update-grub_lib.in itself. Can be done by a conditional call to 'cygpath' utility. This conditional must then be removed for the other platforms when generating update-grub_lib.

Any comment on this?

The pe2elf converter and related patches from Bean, and my remaining patches for Cygwin were committed today.

This 'prefix' fix is the last patch which is necessary of out of the box Cygwin support in the next release, otherwise grub-install would not work.

Christian

#4 lore raw
In reply to Robert Millan
Quoted 19 lines · Robert Millan
Quoted 7 lines · Christian Franke

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

Quoted 9 lines

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX. (probe): Add PRINT_PREFIX, prints result of grub_get_prefix ().
(usage): Add prefix' to -t' usage text. Add some '\n' to avoid excess long lines. (main): Add check for `-t prefix' option.

Quoted 8 lines

This is different than the usual grub-probe usage in that it doesn't operate on a device. Perhaps it would make sense to add the functionality you want in a separate utility?

I have an increasing feeling that grub-probe is becoming bloated (though I admit, this is mostly my fault)

Here is a version with works without grub-probe.

Christian

util/update-grub_lib.in +16 -1
index 03a96ce..9d0f0c6 100644
@@ -68,7 +68,22 @@ make_system_path_relative_to_its_root ()
     dir=""
   fi
 
-  echo $path | sed -e "s,^$dir,,g"
+  # XXX: This fails if $dir contains ','.
+  path=`echo "$path" | sed -e "s,^$dir,,g"` || return 1
+
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)
+      # Cygwin: Check if regular or emulated mount.
+      if [ -z "$dir" ] || [ "`stat -c %D "$dir/.."`" != 620000 ] ; then
+        # Reached some mount point not below /cygdrive.
+        # GRUB does not know Cygwin's emulated mounts,
+        # convert to Win32 path and remove drive letter.
+        path=`cygpath -m "$path" | sed -n 's,^[A-Za-z]:,,p'`
+        test ! -z "$path" || return 1
+      fi ;;
+  esac
+
+  echo "$path"
 }
 
 is_path_readable_by_grub ()
#5 lore raw
Quoted 24 lines · Christian Franke
Quoted 23 lines · Robert Millan
Quoted 7 lines · Christian Franke

The shell function make_system_path_relative_to_its_root() does not work on Cygwin due to path mapping (e.g. /boot/grub/ is actually /cygwin/boot/grub).

This patch adds '-t prefix' to grub-probe. It prints result from grub_get_prefix() which is already extended for Cygwin (svn rev 1584). The result is used in make_system_path_relative_to_its_root(). This keeps the platform dependent code in getroot.c.

Christian

Quoted 9 lines

2008-07-20 Christian Franke <[email protected]>

* util/grub-probe.c (enum): Add PRINT PREFIX.
(probe): Add PRINT_PREFIX, prints result of
grub_get_prefix ().
(usage): Add prefix' to -t' usage text.
Add some '\n' to avoid excess long lines.
(main): Add check for `-t prefix' option.

This is different than the usual grub-probe usage in that it doesn't operate on a device. Perhaps it would make sense to add the functionality you want in a separate utility?

Quoted 7 lines
Quoted 4 lines

I have an increasing feeling that grub-probe is becoming bloated (though I admit, this is mostly my fault)

Here is a version with works without grub-probe.

Committed, grub-probe not affected.

Now Cygwin support should be complete.

Christian