Skip to contentSkip to conversation

update-grub for Cygwin

PATCH In review Christian Franke 3 participants 16 messages Jul 24, 2008 – Aug 6, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 update-grub for Cygwin

This patch allows to run update-grub on Cygwin.

Christian

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

* util/update-grub.in: Add a check for admin group for Cygwin.
Add -f' to rm' and `mv' of grub.cfg to handle the different filesystem semantics on Windows.

util/update-grub.in +16 -3
index c78444e..70607a5 100644
@@ -73,8 +73,20 @@ if [ "x$EUID" = "x" ] ; then
 fi
 
 if [ "$EUID" != 0 ] ; then
-  echo "$0: You must run this as root" >&2
-  exit 1
+  root=f
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)
+      # Cygwin: Assume root if member of admin group
+      for g in `id -G 2>/dev/null` ; do
+	case $g in
+	  0|544) root=t ;;
+	esac
+      done ;;
+  esac
+  if [ $root != t ] ; then
+    echo "$0: You must run this as root" >&2
+    exit 1
+  fi
 fi
 
 set $grub_mkdevicemap dummy
@@ -154,6 +166,7 @@ export GRUB_DEVICE GRUB_DEVICE_UUID GRUB_DEVICE_BOOT GRUB_DEVICE_BOOT_UUID GRUB_
 # These are optional, user-defined variables.
 export GRUB_DEFAULT GRUB_TIMEOUT GRUB_DISTRIBUTOR GRUB_CMDLINE_LINUX GRUB_CMDLINE_LINUX_DEFAULT GRUB_TERMINAL GRUB_SERIAL_COMMAND GRUB_DISABLE_LINUX_UUID
 
+rm -f ${grub_cfg}.new
 exec > ${grub_cfg}.new
 
 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
@@ -187,6 +200,6 @@ for i in ${update_grub_dir}/* ; do
 done
 
 # none of the children aborted with error, install the new grub.cfg
-mv ${grub_cfg}.new ${grub_cfg}
+mv -f ${grub_cfg}.new ${grub_cfg}
 
 echo "done" >&2
#1 lore raw
Quoted 2 lines · Christian Franke

This patch allows to run update-grub on Cygwin.

... and here is a simple example for /etc/grub.d/10_cygwin

Only adds current OS yet.

Christian

util/grub.d/10_cygwin.in new file +48 -0
new file mode 100644
index 0000000..1dd1116
@@ -0,0 +1,48 @@
+#! /bin/sh -e
+
+# update-grub helper script.
+# Copyright (C) 2008  Free Software Foundation, Inc.
+#
+# GRUB is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# GRUB is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+
+
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+sbindir=@sbindir@
+
+grub_probe=${sbindir}/grub-probe
+
+case "`uname 2>/dev/null`" in
+  CYGWIN_NT-5.0) OS="Windows 2000" ;;
+  CYGWIN_NT-5.1) OS="Windows XP" ;;
+  CYGWIN*) OS="Windows" ;;
+  *) exit 0 ;;
+esac
+
+[ "$SYSTEMDRIVE" != "" ] || exit 0
+
+p="`cygpath -a "$SYSTEMDRIVE"`"
+
+[ -f "$p"/ntldr ] || exit 0
+
+d="`${grub_probe} -t drive "$p" 2>/dev/null`" || exit 0
+
+echo "Found $OS on $d" >&2
+cat << EOF
+menuentry "$OS" {
+	root=$d
+	chainloader +1
+}
+EOF
+
#2 lore raw

Review

Quoted 2 lines · Christian Franke
(quoted diff) +2 -0
+
+d="`${grub_probe} -t drive "$p" 2>/dev/null`" || exit 0

Please avoid reliing on '-t drive'. It's based on device.map which just contains guesswork.

prepare_grub_to_access_device() is a much better option.

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 /.)
#3 lore raw
Quoted 12 lines · Robert Millan

Review

Quoted 3 lines · Christian Franke
(quoted diff) +2 -0
+
+d="`${grub_probe} -t drive "$p" 2>/dev/null`" || exit 0

Please avoid reliing on '-t drive'. It's based on device.map which just contains guesswork.

prepare_grub_to_access_device() is a much better option.

Yes. Thanks for the info. New version below.

Christian

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

* conf/common.rmk: Add 10_cygwin' to update-grub_SCRIPTS'.
* util/grub.d/10_cygwin.in: New file.

conf/common.rmk +1 -1
index 7db0b2a..0f74f1a 100644
@@ -120,7 +120,7 @@ CLEANFILES += update-grub_lib
 %: util/grub.d/%.in config.status
 	./config.status --file=$@:$<
 	chmod +x $@
-update-grub_SCRIPTS = 00_header 10_linux 10_hurd 30_os-prober 40_custom
+update-grub_SCRIPTS = 00_header 10_cygwin 10_linux 10_hurd 30_os-prober 40_custom
 CLEANFILES += $(update-grub_SCRIPTS)
 
 update-grub_DATA += util/grub.d/README
util/grub.d/10_cygwin.in new file +52 -0

52 lines not on this page · raw message

#4 lore raw
Quoted 14 lines · Christian Franke
Quoted 12 lines · Robert Millan

Review

Quoted 3 lines · Christian Franke
(quoted diff) +2 -0
+
+d="`${grub_probe} -t drive "$p" 2>/dev/null`" || exit 0

Please avoid reliing on '-t drive'. It's based on device.map which just contains guesswork.

prepare_grub_to_access_device() is a much better option.

Quoted 6 lines

Yes. Thanks for the info. New version below.

Christian

...

Here a more generic version which allows to specifiy windows system dirs by /etc/default/grub:GRUB_WINDOWS_DIRS.

Defaults to current SYSTEMDRIVE on Cygwin, and nothing on other OS.

Christian

2008-08-04 Christian Franke <[email protected]>

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.
* util/update-grub.in: Add export of GRUB_WINDOWS_DIRS.

conf/common.rmk +1 -1
index 3d3cd8a..3d674a6 100644
@@ -120,7 +120,7 @@ CLEANFILES += update-grub_lib
 %: util/grub.d/%.in config.status
 	./config.status --file=$@:$<
 	chmod +x $@
-update-grub_SCRIPTS = 00_header 10_linux 10_hurd 30_os-prober 40_custom
+update-grub_SCRIPTS = 00_header 10_linux 10_hurd 10_windows 30_os-prober 40_custom
 CLEANFILES += $(update-grub_SCRIPTS)
 
 update-grub_DATA += util/grub.d/README
util/grub.d/10_windows.in new file +104 -0

104 lines not on this page · raw message

util/update-grub.in +1 -1
index 8c60c41..eafaa91 100644
@@ -163,7 +163,7 @@ esac
 export GRUB_DEVICE GRUB_DEVICE_UUID GRUB_DEVICE_BOOT GRUB_DEVICE_BOOT_UUID GRUB_FS GRUB_FONT_PATH GRUB_PRELOAD_MODULES
 
 # These are optional, user-defined variables.
-export GRUB_DEFAULT GRUB_TIMEOUT GRUB_DISTRIBUTOR GRUB_CMDLINE_LINUX GRUB_CMDLINE_LINUX_DEFAULT GRUB_TERMINAL GRUB_SERIAL_COMMAND GRUB_DISABLE_LINUX_UUID
+export GRUB_DEFAULT GRUB_TIMEOUT GRUB_DISTRIBUTOR GRUB_CMDLINE_LINUX GRUB_CMDLINE_LINUX_DEFAULT GRUB_TERMINAL GRUB_SERIAL_COMMAND GRUB_DISABLE_LINUX_UUID GRUB_WINDOWS_DIRS
 
 rm -f ${grub_cfg}.new
 exec > ${grub_cfg}.new
#5 lore raw
Quoted 6 lines · Christian Franke

Here a more generic version which allows to specifiy windows system dirs by /etc/default/grub:GRUB_WINDOWS_DIRS.

Defaults to current SYSTEMDRIVE on Cygwin, and nothing on other OS.

Quoted 7 lines

Christian

2008-08-04 Christian Franke <[email protected]>

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.
* util/update-grub.in: Add export of GRUB_WINDOWS_DIRS.

Why is this needed? Can't we do something like 'grub-probe -t device c:/ntldr' or so?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#6 lore raw
Quoted 10 lines · Robert Millan
Quoted 8 lines · Christian Franke

Here a more generic version which allows to specifiy windows system dirs by /etc/default/grub:GRUB_WINDOWS_DIRS.

Defaults to current SYSTEMDRIVE on Cygwin, and nothing on other OS.

Christian

2008-08-04 Christian Franke <[email protected]>

Quoted 9 lines
Quoted 4 lines

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.
* util/update-grub.in: Add export of GRUB_WINDOWS_DIRS.

Why is this needed? Can't we do something like 'grub-probe -t device c:/ntldr' or so?

It is not needed for standard installations with ntldr on SYSTEMDRIVE (C:). The ability to specify GRUB_WINDOWS_DIRS is added to support non-standard installations. It also allows to use 10_windows on other OS if os-prober is not available.

Christian

#7 lore raw
Quoted 23 lines · Christian Franke
Quoted 11 lines · Robert Millan
Quoted 9 lines · Christian Franke

Here a more generic version which allows to specifiy windows system dirs by /etc/default/grub:GRUB_WINDOWS_DIRS.

Defaults to current SYSTEMDRIVE on Cygwin, and nothing on other OS.

Christian

2008-08-04 Christian Franke <[email protected]>

Quoted 10 lines
Quoted 4 lines

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.
* util/update-grub.in: Add export of GRUB_WINDOWS_DIRS.

Why is this needed? Can't we do something like 'grub-probe -t device c:/ntldr'
or so?

Quoted 4 lines

It is not needed for standard installations with ntldr on SYSTEMDRIVE (C:). The ability to specify GRUB_WINDOWS_DIRS is added to support non-standard installations.
It also allows to use 10_windows on other OS if os-prober is not available.

Why not have the user write a custom entry then? I think it clutters the user interface to add options for everything. If a corner case (boot a non native disk, can't use os-prober) can be supported by creating a new config file, why not do that? It was the whole reason for designing update-grub to be easily extensible.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#8 lore raw
Quoted 30 lines · Robert Millan
Quoted 18 lines · Christian Franke
Quoted 16 lines · Robert Millan
Quoted 13 lines · Christian Franke

Here a more generic version which allows to specifiy windows system dirs by /etc/default/grub:GRUB_WINDOWS_DIRS.

Defaults to current SYSTEMDRIVE on Cygwin, and nothing on other OS.

Christian

2008-08-04 Christian Franke <[email protected]>

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.
* util/update-grub.in: Add export of GRUB_WINDOWS_DIRS.

Quoted 9 lines
Quoted 5 lines

Why is this needed? Can't we do something like 'grub-probe -t device c:/ntldr' or so?

It is not needed for standard installations with ntldr on SYSTEMDRIVE (C:). The ability to specify GRUB_WINDOWS_DIRS is added to support non-standard installations. It also allows to use 10_windows on other OS if os-prober is not available.

Quoted 7 lines

Why not have the user write a custom entry then? I think it clutters the user interface to add options for everything. If a corner case (boot a non native disk, can't use os-prober) can be supported by creating a new config file, why not do that? It was the whole reason for designing update-grub to be easily extensible.

Even with the new configuration parameter, user can still decide to leave the parameter unset and write a custom entry. Or the user can set the parameter and so "opt-in" to let the script do the work (or "opt-out" to disable the default work on Cygwin).

But in case the extra 'export' in 'update-grub' is a problem, here a simplified version.

Scans C: and SYSTEMDRIVE on Cygwin, does nothing elsewhere. Cannot be configured.

Christian

2008-08-05 Christian Franke <[email protected]>

* conf/common.rmk: Add 10_windows' to update-grub_SCRIPTS'.
* util/grub.d/10_windows.in: New file.

conf/common.rmk +1 -1
index 3d3cd8a..3d674a6 100644
@@ -120,7 +120,7 @@ CLEANFILES += update-grub_lib
 %: util/grub.d/%.in config.status
 	./config.status --file=$@:$<
 	chmod +x $@
-update-grub_SCRIPTS = 00_header 10_linux 10_hurd 30_os-prober 40_custom
+update-grub_SCRIPTS = 00_header 10_linux 10_hurd 10_windows 30_os-prober 40_custom
 CLEANFILES += $(update-grub_SCRIPTS)
 
 update-grub_DATA += util/grub.d/README
util/grub.d/10_windows.in new file +83 -0

83 lines not on this page · raw message

#9 lore raw
Quoted 10 lines · Christian Franke
Quoted 7 lines

Why not have the user write a custom entry then? I think it clutters the user interface to add options for everything. If a corner case (boot a non native disk, can't use os-prober) can be supported by creating a new config file, why not do that? It was the whole reason for designing update-grub to
be easily extensible.

Even with the new configuration parameter, user can still decide to leave the parameter unset and write a custom entry.

My concern is not with presenting too many options to the user, but with having to support them. In general, if a use case can be supported without adding a new user option, I think it's much better not to add it.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#10 lore raw
Quoted 8 lines · Robert Millan
Quoted 6 lines · Christian Franke
Quoted 6 lines

Why not have the user write a custom entry then? I think it clutters the user interface to add options for everything. If a corner case (boot a non native disk, can't use os-prober) can be supported by creating a new config file, why not do that? It was the whole reason for designing update-grub to be easily extensible.

Quoted 8 lines
Quoted 2 lines

Even with the new configuration parameter, user can still decide to leave the parameter unset and write a custom entry.

My concern is not with presenting too many options to the user, but with having to support them. In general, if a use case can be supported without adding a new user option, I think it's much better not to add it.

OK. I committed the simplified version (no configuration parameter).

Christian

#11 lore raw
In reply to Christian Franke

util/update-grub.in if [ "x$EUID" = "x" ] ; then · line 78

Quoted 2 lines · Christian Franke
util/update-grub.in +2 -0
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)

Could this be done at build time instead? (when generating update-grub from update-grub.in)

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 /.)
#12 lore raw
Quoted 10 lines · Robert Millan

Review

Quoted 3 lines · Christian Franke
(quoted diff) +2 -0
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)

Could this be done at build time instead? (when generating update-grub from update-grub.in)

Yes, of course. Some alternatives:

1.)
The method used by AM_CONDITIONAL from GNU automake is simple and works with config.status without any extra 'shell preprocessor'.

Looks ugly, but may be OK if there are only few changes. See attached patch for a working example.

2.)
Use some shell-preprocessor. A primitive version can be configured by above method:

update-grub.in:
@ifdef CYGWIN
...
@else CYGWIN
...
@endif CYGWIN

shellpp.sh.in:
@CYGWIN_TRUE@ sed '/^@if CYGWIN/,/^@else CYGWIN/d;/^@endif CYGWIN/d' @CYGWIN_FALSE@ sed '/^@if CYGWIN/d;/^@else CYGWIN/,/^@endif CYGWIN/d'

3.)
Move platform specific code to functions in update-grub_lib, e.g. 'check_user_root()' in this case.

Let 'update-grub_lib' include optional platform specific scripts (e.g. 'update-grub-_lib-i386-pc-cygwin'). The existence of the script can be checked by configure. This script can then replace the default implementations if necessary:

Example:

update-grub.in:

check_user_root || exit 1

update-grub-lib.in:

check_user_root()
{
... default implementation, test EUID==0
}

platform_lib=@platform_lib@
test -z "${platform_lib}" || . "${libdir}/${platform_lib}"

update-grub-_lib-i386-pc-cygwin:

check_user_root()
{
... Cygwin specific implementation ...
}

Any method would also help to implement 'make_system_path_relative_to_its_root()' without the need for 'grub-probe -t prefix'.

Christian

aclocal.m4 +14 -0
index ee6c4db..8c44678 100644
@@ -414,3 +414,17 @@ else
   AC_MSG_RESULT([no])
 [fi]
 ])
+
+dnl Set conditional for target dependend sections in scripts.
+dnl Similar to AM_CONDITIONAL from GNU automake.
+AC_DEFUN([grub_CONDITIONAL],[
+AC_SUBST([$1_TRUE])
+AC_SUBST([$1_FALSE])
+if $2 ; then
+  $1_TRUE=
+  $1_FALSE="#"
+else
+  $1_TRUE="#"
+  $1_FALSE=
+fi
+])
configure.ac +3 -0
index 7a5938a..def656d 100644
@@ -351,6 +351,9 @@ CPPFLAGS="$tmp_CPPFLAGS"
 LDFLAGS="$tmp_LDFLAGS"
 LIBS="$tmp_LIBS"
 
+# Set conditionals for target dependend sections in scripts.
+grub_CONDITIONAL(CYGWIN, [test "$target_os" = cygwin])
+
 #
 # Check for options.
 #
util/update-grub.in +17 -3
index c78444e..55aab75 100644
@@ -73,8 +73,21 @@ if [ "x$EUID" = "x" ] ; then
 fi
 
 if [ "$EUID" != 0 ] ; then
-  echo "$0: You must run this as root" >&2
-  exit 1
+@CYGWIN_TRUE@  # Begin Cygwin specific.
+@CYGWIN_TRUE@  # Assume root if member of admin group.
+@CYGWIN_TRUE@  root=f
+@CYGWIN_TRUE@  for g in `id -G 2>/dev/null` ; do
+@CYGWIN_TRUE@    case $g in
+@CYGWIN_TRUE@      0|544) root=t ;;
+@CYGWIN_TRUE@    esac
+@CYGWIN_TRUE@  done
+@CYGWIN_TRUE@  if [ $root != t ] ; then
+@CYGWIN_TRUE@    echo "$0: You must have admin rights to run this" >&2
+@CYGWIN_TRUE@    exit 1
+@CYGWIN_TRUE@  fi
+@CYGWIN_TRUE@  # End Cygwin specific.
+@CYGWIN_FALSE@  echo "$0: You must run this as root" >&2
+@CYGWIN_FALSE@  exit 1
 fi
 
 set $grub_mkdevicemap dummy
@@ -154,6 +167,7 @@ export GRUB_DEVICE GRUB_DEVICE_UUID GRUB_DEVICE_BOOT GRUB_DEVICE_BOOT_UUID GRUB_
 # These are optional, user-defined variables.
 export GRUB_DEFAULT GRUB_TIMEOUT GRUB_DISTRIBUTOR GRUB_CMDLINE_LINUX GRUB_CMDLINE_LINUX_DEFAULT GRUB_TERMINAL GRUB_SERIAL_COMMAND GRUB_DISABLE_LINUX_UUID
 
+rm -f ${grub_cfg}.new
 exec > ${grub_cfg}.new
 
 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
@@ -187,6 +201,6 @@ for i in ${update_grub_dir}/* ; do
 done
 
 # none of the children aborted with error, install the new grub.cfg
-mv ${grub_cfg}.new ${grub_cfg}
+mv -f ${grub_cfg}.new ${grub_cfg}
 
 echo "done" >&2
#13 lore raw
Quoted 7 lines · Christian Franke
Quoted 6 lines · Robert Millan
Quoted 3 lines · Christian Franke
(quoted diff) +2 -0
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)
Quoted 6 lines
Quoted 4 lines

Could this be done at build time instead? (when generating update-grub from update-grub.in)

Yes, of course. Some alternatives:

...

I think it's better to have the check at the run time than any of those alternatives. If we have many OS specific checks, then we can think how to leave the relevant ones. At this point, it's probably not worth the trouble.

Signature · 2 lines
Regards,
Pavel Roskin
#14 lore raw
Quoted 13 lines · Pavel Roskin
Quoted 12 lines · Christian Franke
Quoted 10 lines · Robert Millan

Review

Quoted 3 lines · Christian Franke
(quoted diff) +2 -0
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)

Could this be done at build time instead? (when generating update-grub from update-grub.in)

Quoted 7 lines
Quoted 1 line

Yes, of course. Some alternatives:

...

I think it's better to have the check at the run time than any of those alternatives. If we have many OS specific checks, then we can think how to leave the relevant ones. At this point, it's probably not worth the trouble.

Ok I don't mind. It was just a suggestion.

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 /.)
#15 lore raw
Quoted 22 lines · Robert Millan
Quoted 20 lines · Pavel Roskin
Quoted 16 lines · Christian Franke
Quoted 12 lines · Robert Millan

Review

Quoted 4 lines · Christian Franke
(quoted diff) +2 -0
+  case "`uname 2>/dev/null`" in
+    CYGWIN*)

Could this be done at build time instead? (when generating update-grub from update-grub.in)

Yes, of course. Some alternatives:

...

Quoted 9 lines
Quoted 5 lines

I think it's better to have the check at the run time than any of those alternatives. If we have many OS specific checks, then we can think how to leave the relevant ones. At this point, it's probably not worth the trouble.

Ok I don't mind. It was just a suggestion.

For now, I committed the original version with the run time check.

Christian