Skip to contentSkip to conversation

Require human interaction to go to normal shell if grub.cfg has a problem

PATCH In review Jon McCune 3 participants 9 messages Nov 25, 2013 – Dec 9, 2013

lore
Share & tools
lore

Jon McCune

author
#0 lore raw

PATCH 1/1 Require human interaction to go to normal shell if grub.cfg has a problem

The rescue prompt is very useful for human operators, but not so useful in unattended environments. Add a facility for rebooting after a delay, so that, e.g., the system can still PXE boot if there is no console attached.

---
 grub-core/normal/main.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
grub-core/normal/main.c +25 -0
index ad36273..f8953d5 100644
@@ -32,6 +32,7 @@
 #include <grub/i18n.h>
 #include <grub/charset.h>
 #include <grub/script_sh.h>
+#include <grub/time.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -333,6 +334,21 @@ grub_normal_execute (const char *config, int nested, int batch)
     }
 }
 
+/* Copied from grub-core/commands/sleep.c. */
+static int
+grub_interruptible_millisleep (grub_uint32_t ms)
+{
+  grub_uint64_t start;
+
+  start = grub_get_time_ms ();
+
+  while (grub_get_time_ms () - start < ms)
+    if (grub_getkey_noblock () == GRUB_TERM_ESC)
+      return 1;
+
+  return 0;
+}
+
 /* This starts the normal mode.  */
 void
 grub_enter_normal_mode (const char *config)
@@ -340,6 +356,15 @@ grub_enter_normal_mode (const char *config)
   grub_boot_time ("Entering normal mode");
   nested_level++;
   grub_normal_execute (config, 0, 0);
+  /* Control only returns from grub_normal_execute() if there is some kind of
+   * problem with grub.cfg, like it does not exist.  Reboot by default unless
+   * ESC is pressed within 5 seconds. */
+  grub_printf ("Press ESC in 5 seconds for a rescue shell.\n");
+  if (!grub_interruptible_millisleep (5000))
+    {
+      grub_printf ("Rebooting instead of going to rescue shell.\n");
+      grub_reboot ();
+    }
   grub_boot_time ("Entering shell");
   grub_cmdline_run (0);
   nested_level--;
Signature · 1 line
1.8.4.1
#1 lore raw

Just a wild idea perhaps we should embed osdetect.cfg in normal.mod in some form. It would be pretty much the easy solution for many cases.

Quoted 72 lines · Jon McCune

The rescue prompt is very useful for human operators, but not so useful in unattended environments. Add a facility for rebooting after a delay, so that, e.g., the system can still PXE boot if there is no console attached.

---
 grub-core/normal/main.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
grub-core/normal/main.c +16 -0
index ad36273..f8953d5 100644
@@ -32,6 +32,7 @@
 #include <grub/i18n.h>
 #include <grub/charset.h>
 #include <grub/script_sh.h>
+#include <grub/time.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -333,6 +334,21 @@ grub_normal_execute (const char *config, int nested,
 int batch)
     }
 }
 
+/* Copied from grub-core/commands/sleep.c. */
+static int
+grub_interruptible_millisleep (grub_uint32_t ms)
+{
+  grub_uint64_t start;
+
+  start = grub_get_time_ms ();
+
+  while (grub_get_time_ms () - start < ms)
+    if (grub_getkey_noblock () == GRUB_TERM_ESC)
+      return 1;
+
+  return 0;
+}
+
 /* This starts the normal mode.  */
 void

grub_enter_normal_mode (const char *config)

grub-core/normal/main.c +1 -0
@@ -340,6 +356,15 @@ grub_enter_normal_mode (const char *config)
   grub_boot_time ("Entering normal mode");
   nested_level++;
   grub_normal_execute (config, 0, 0);
+  /* Control only returns from grub_normal_execute() if there is some

kind of
+ * problem with grub.cfg, like it does not exist. Reboot by default
unless

grub-core/normal/main.c +7 -1
+   * ESC is pressed within 5 seconds. */
+  grub_printf ("Press ESC in 5 seconds for a rescue shell.\n");
+  if (!grub_interruptible_millisleep (5000))
+    {
+      grub_printf ("Rebooting instead of going to rescue shell.\n");
+      grub_reboot ();
+    }
   grub_boot_time ("Entering shell");
   grub_cmdline_run (0);
   nested_level--;
--

1.8.4.1

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
In reply to Jon McCune

В Mon, 25 Nov 2013 15:46:48 -0800
Jon McCune <[email protected]> пишет:

grub-core/normal/main.c

Quoted 11 lines

The rescue prompt is very useful for human operators, but not so useful in unattended environments. Add a facility for rebooting after a delay, so that, e.g., the system can still PXE boot if there is no console attached.

---
 grub-core/normal/main.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
grub-core/normal/main.c +0 -0

You get rescue prompt when you can *not* launch normal.mod.

Quoted 34 lines

index ad36273..f8953d5 100644

grub-core/normal/main.c +16 -0
@@ -32,6 +32,7 @@
 #include <grub/i18n.h>
 #include <grub/charset.h>
 #include <grub/script_sh.h>
+#include <grub/time.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -333,6 +334,21 @@ grub_normal_execute (const char *config, int nested, int batch)
     }
 }
 
+/* Copied from grub-core/commands/sleep.c. */
+static int
+grub_interruptible_millisleep (grub_uint32_t ms)
+{
+  grub_uint64_t start;
+
+  start = grub_get_time_ms ();
+
+  while (grub_get_time_ms () - start < ms)
+    if (grub_getkey_noblock () == GRUB_TERM_ESC)
+      return 1;
+
+  return 0;
+}
+
 /* This starts the normal mode.  */
 void
 grub_enter_normal_mode (const char *config)

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 361

Quoted 6 lines
grub-core/normal/main.c +3 -0
@@ -340,6 +356,15 @@ grub_enter_normal_mode (const char *config)
   grub_boot_time ("Entering normal mode");
   nested_level++;
   grub_normal_execute (config, 0, 0);
+  /* Control only returns from grub_normal_execute() if there is some kind of
+   * problem with grub.cfg, like it does not exist.  Reboot by default unless
+   * ESC is pressed within 5 seconds. */

grub-mkrescue creates CD that starts normal CLI without grub.cfg. So please - not as default. You can make it dependent on variable which can be set in embedded grub.cfg for the cases when you really need it.

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 362

Quoted 1 line
grub-core/normal/main.c +1 -0
+  grub_printf ("Press ESC in 5 seconds for a rescue shell.\n");

"for a command line". This is not rescue shell.

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 365

Quoted 3 lines
grub-core/normal/main.c +3 -0
+  if (!grub_interruptible_millisleep (5000))
+    {
+      grub_printf ("Rebooting instead of going to rescue shell.\n");

Ditto.

Quoted 5 lines
grub-core/normal/main.c +2 -0
+      grub_reboot ();
+    }
   grub_boot_time ("Entering shell");
   grub_cmdline_run (0);
   nested_level--;
#3 lore raw

Replying here instead of the install_c thread to try to keep it clean. In response to:

Quoted 18 lines · Andrey Borzenkov
Quoted 8 lines · Jonathan McCune
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Hello, all. install_c is feature complete and I intent to make it upstream unless issues are raised until 16 Nov. It's available under
http://git.savannah.gnu.org/cgit/grub.git/log/?h=phcoder/install_c

Quoted 4 lines

I just realized that the option --grub-mkimage is now accepted silently without having any effect in util/grub-install-common.c:grub_install_parse(). This makes it no longer feasible to easily wrap the mkimage command. While most of the

meaningful

Quoted 1 line

reasons to wrap grub-mkimage have now been added to grub-install, I've

run

Quoted 1 line

into something I'm struggling to work around: How do I add custom

Quoted 8 lines

contents

Quoted 3 lines

to a load.cfg, since util/grub-install.c seems to unconditionally grub_util_unlink(load_cfg)?

Could you provide examples of your grub.cfg usage? I think we need to have more real-life use cases to understand better how and when it is used.

The real life use case is a headless / unattended environment where it is better to reboot than to present a console prompt if there is some kind of GRUB problem, since the machine will just sit there until somebody or something notices that it never booted up.

I was thinking that adding something in load.cfg (not grub.cfg) might be a reasonable way to specify the desire to predispose GRUB towards rebooting in the event of problems for use in headless/unattended environments.
Whether that is true or not, I see now that the intention is for the
contents of load.cfg to be generated by the appropriate grub tools, and not from arbitrary shell scripts. No problem.

I'm wondering what is the "right" way to enable rebooting behavior. Flag to grub-install? I also think this new functionality should have some kind of delay and be cancelable at the console (current patch does this).

Thoughts?

Thanks,
-Jon

Quoted 87 lines · Andrey Borzenkov

В Mon, 25 Nov 2013 15:46:48 -0800
Jon McCune <[email protected]> пишет:

grub-core/normal/main.c

Quoted 11 lines

The rescue prompt is very useful for human operators, but not so useful in unattended environments. Add a facility for rebooting after a delay, so that, e.g., the system can still PXE boot if there is no console attached.

---
 grub-core/normal/main.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
grub-core/normal/main.c +0 -0

You get rescue prompt when you can *not* launch normal.mod.

grub-core/normal/main.c grub_normal_execute (const char *config, int

Quoted 12 lines

index ad36273..f8953d5 100644

grub-core/normal/main.c +1 -0
@@ -32,6 +32,7 @@
 #include <grub/i18n.h>
 #include <grub/charset.h>
 #include <grub/script_sh.h>
+#include <grub/time.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -333,6 +334,21 @@ grub_normal_execute (const char *config, int

nested, int batch)

Quoted 22 lines
grub-core/normal/main.c +15 -0
     }
 }
 
+/* Copied from grub-core/commands/sleep.c. */
+static int
+grub_interruptible_millisleep (grub_uint32_t ms)
+{
+  grub_uint64_t start;
+
+  start = grub_get_time_ms ();
+
+  while (grub_get_time_ms () - start < ms)
+    if (grub_getkey_noblock () == GRUB_TERM_ESC)
+      return 1;
+
+  return 0;
+}
+
 /* This starts the normal mode.  */
 void
 grub_enter_normal_mode (const char *config)

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 359

Quoted 4 lines
grub-core/normal/main.c +1 -0
@@ -340,6 +356,15 @@ grub_enter_normal_mode (const char *config)
   grub_boot_time ("Entering normal mode");
   nested_level++;
   grub_normal_execute (config, 0, 0);
+  /* Control only returns from grub_normal_execute() if there is some

kind of

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 360

Quoted 1 line
grub-core/normal/main.c +1 -0
+   * problem with grub.cfg, like it does not exist.  Reboot by default

unless

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 361

Quoted 1 line
grub-core/normal/main.c +1 -0
+   * ESC is pressed within 5 seconds. */

grub-mkrescue creates CD that starts normal CLI without grub.cfg. So please - not as default. You can make it dependent on variable which can be set in embedded grub.cfg for the cases when you really need it.

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 362

Quoted 1 line
grub-core/normal/main.c +1 -0
+  grub_printf ("Press ESC in 5 seconds for a rescue shell.\n");

"for a command line". This is not rescue shell.

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 365

Quoted 3 lines
grub-core/normal/main.c +3 -0
+  if (!grub_interruptible_millisleep (5000))
+    {
+      grub_printf ("Rebooting instead of going to rescue shell.\n");

Ditto.

grub-core/normal/main.c grub_enter_normal_mode (const char *config) · line 370

Quoted 5 lines
grub-core/normal/main.c +2 -0
+      grub_reboot ();
+    }
   grub_boot_time ("Entering shell");
   grub_cmdline_run (0);
   nested_level--;

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 34 lines · Jonathan McCune

Replying here instead of the install_c thread to try to keep it clean. In response to:

Quoted 24 lines · Andrey Borzenkov
Quoted 8 lines · Jonathan McCune
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko

Hello, all. install_c is feature complete and I intent to make it upstream unless issues are raised until 16 Nov. It's available under
http://git.savannah.gnu.org/cgit/grub.git/log/?h=phcoder/install_c

Quoted 12 lines

I just realized that the option --grub-mkimage is now accepted silently without having any effect in util/grub-install-common.c:grub_install_parse(). This makes it no longer feasible to easily wrap the mkimage command. While most of the meaningful
reasons to wrap grub-mkimage have now been added to grub-install, I've run
into something I'm struggling to work around: How do I add custom contents
to a load.cfg, since util/grub-install.c seems to unconditionally grub_util_unlink(load_cfg)?

Quoted 3 lines

Could you provide examples of your grub.cfg usage? I think we need to have more real-life use cases to understand better how and when it is used.

Quoted 5 lines

The real life use case is a headless / unattended environment where it is better to reboot than to present a console prompt if there is some kind of GRUB problem, since the machine will just sit there until somebody or something notices that it never booted up.

I still do not quite understand how rebooting can fix the problem. The only case it may happen is when you have intermittent network issues where grub fails to read some file.

I have a feeling that you attempt to paper over some problem outside of grub.

#5 lore raw
Quoted 5 lines · Andrey Borzenkov

I still do not quite understand how rebooting can fix the problem. The only case it may happen is when you have intermittent network issues where grub fails to read some file.

Ah, rebooting allows a machine to network boot, e.g., PXE boot using its NIC.

Quoted 3 lines

I have a feeling that you attempt to paper over some problem outside of grub.

This is somewhat true, in that grub's own commands should not get the machine into a state where this functionality is useful. But furthering the real life example, users / administrators might make a mistake and create a broken config. If the machine is unattended, it seems reasonable that the user might prefer for it to reboot. Otherwise, it becomes necessary to somehow cause a reboot out-of-band. These out-of-band solutions are generally proprietary and I think it's a good idea to have support for avoiding them if desired.

Thanks,
-Jon

#6 lore raw

В Mon, 2 Dec 2013 12:49:03 -0800
Jonathan McCune <[email protected]> пишет:

Quoted 11 lines · Andrey Borzenkov
Quoted 6 lines

I still do not quite understand how rebooting can fix the problem. The only case it may happen is when you have intermittent network issues where grub fails to read some file.

Ah, rebooting allows a machine to network boot, e.g., PXE boot using its NIC.

Hmm ... how does it work? After reboot you use the same default boot order and end up in the same non-working grub, right? May be you want to not reboot but simply exit grub letting firmware to continue with next boot choice; I'm not sure whether this works reliably in case of BIOS.

And I still do not really see how useful it is. Booting from network will presumably land you into some sort of remote installation environment. How does it help? How do know it landed there in the first place?

Quoted 5 lines
Quoted 4 lines

I have a feeling that you attempt to paper over some problem outside of grub.

Quoted 9 lines

This is somewhat true, in that grub's own commands should not get the machine into a state where this functionality is useful. But furthering the real life example, users / administrators might make a mistake and create a broken config. If the machine is unattended, it seems reasonable that the user might prefer for it to reboot. Otherwise, it becomes necessary to somehow cause a reboot out-of-band. These out-of-band solutions are generally proprietary and I think it's a good idea to have support for avoiding them if desired.

Well, I spent last 10+ years doing remote maintenance and I know pretty well, that if you do not have remote access to console and possibility to remotely trigger reboot, you will get in trouble in any case. There are much more situations that require it and they are far more probable than grub misconfiguration.

#7 lore raw

Inline...

Quoted 16 lines · Andrey Borzenkov

В Mon, 2 Dec 2013 12:49:03 -0800
Jonathan McCune <[email protected]> пишет:

Quoted 4 lines
Quoted 1 line · On Mon, Dec 2, 2013 at 12:38 PM, Andrey Borzenkov <[email protected] wrote:
Quoted 8 lines
Quoted 4 lines

I still do not quite understand how rebooting can fix the problem. The only case it may happen is when you have intermittent network issues where grub fails to read some file.

Ah, rebooting allows a machine to network boot, e.g., PXE boot using its NIC.

Quoted 6 lines

Hmm ... how does it work? After reboot you use the same default boot order and end up in the same non-working grub, right? May be you want to not reboot but simply exit grub letting firmware to continue with next boot choice; I'm not sure whether this works reliably in case of BIOS.

Good point. This is another interesting option. I'm not sure how generally reliable it is for BIOS systems either, but it seems like a case worth considering.

Quoted 5 lines

And I still do not really see how useful it is. Booting from network will presumably land you into some sort of remote installation environment. How does it help? How do know it landed there in the first place?

I agree that a naive always-netboot-to-reinstall is not a useful configuration, but DHCP servers can be easily configured to enable or disable netboot for individual machines. Sometimes this can be a nice control point for diagnosing or reconfiguring machines.

Quoted 15 lines
Quoted 8 lines
Quoted 3 lines

I have a feeling that you attempt to paper over some problem outside of grub.

This is somewhat true, in that grub's own commands should not get the machine into a state where this functionality is useful. But furthering the real life example, users / administrators might make a mistake and create a broken config. If the machine is unattended, it seems

reasonable

Quoted 5 lines

that the user might prefer for it to reboot. Otherwise, it becomes necessary to somehow cause a reboot out-of-band. These out-of-band solutions are generally proprietary and I think it's a good idea to have support for avoiding them if desired.

Quoted 6 lines

Well, I spent last 10+ years doing remote maintenance and I know pretty well, that if you do not have remote access to console and possibility to remotely trigger reboot, you will get in trouble in any case. There are much more situations that require it and they are far more probable than grub misconfiguration.

I concede that some problems inevitably require manual intervention, but the probabilities of different misconfigurations seem likely to be
deployment-specific. Remote console / serial concentrators / remote
reboot / humans to press buttons / etc are neither instantaneous nor free.
Anyways, I will try my hand at another iteration on this patch.

-Jon

#8 lore raw
Quoted 3 lines · Jonathan McCune

Good point. This is another interesting option. I'm not sure how generally reliable it is for BIOS systems either, but it seems like a case worth considering.

The call has been around for a long time. I think originally it launched BASIC in ROM.
It hasn't been used by microsoft so may not be reliable.
On the other hand it's used by option roms to indicate to proceed to next boot option so I expect it to work as long as next boot option is defined.