Skip to contentSkip to conversation

Add support for grub-emu to kexec Linux menu entries

PATCH In review Robbie Harwood 2 participants 3 messages Oct 24, 2022 – Nov 3, 2022

lore
Share & tools
lore
#0 lore raw

v6 cover Add support for grub-emu to kexec Linux menu entries

Address feedback from Daniel's third review (interdiff attached).

Be well,
--Robbie

Raymund Will (1):
Add support for grub-emu to kexec Linux menu entries

 docs/grub.texi               |  30 ++++--
 grub-core/Makefile.am        |   1 +
 grub-core/Makefile.core.def  |   2 +-
 grub-core/kern/emu/main.c    |   4 +
 grub-core/kern/emu/misc.c    |  18 +++-
 grub-core/loader/emu/linux.c | 178 +++++++++++++++++++++++++++++++++++
 include/grub/emu/exec.h      |   4 +-
 include/grub/emu/hostfile.h  |   3 +-
 include/grub/emu/misc.h      |   3 +
 9 files changed, 231 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/loader/emu/linux.c

Interdiff against v5:

docs/grub.texi +7 -3
index 1463e62e6b..1663a076c0 100644
@@ -1,4 +1,4 @@
-i\input texinfo
+\input texinfo
 @c -*-texinfo-*-
 @c %**start of header
 @setfilename grub.info
@@ -962,11 +962,15 @@ information.
 @node Kexec
 @subsection Kexec with grub2-emu
 
-grub2 can be run in userspace by invoking the grub2-emu tool.  It will
+GRUB can be run in userspace by invoking the grub2-emu tool.  It will
 read all configuration scripts as if booting directly (see @xref{Loading
 an operating system directly}).  With the @code{--kexec} flag, and
 kexec(8) support from the operating system, the @command{linux} command
-will directly boot the target image.
+will directly boot the target image.  For systems that lack working
+systemctl(1) support for kexec, passing the @code{--kexec} flag twice
+will fallback to invoking kexec(8) directly; note however that this
+fallback may be unsafe outside read-only environments, as it does not
+invoke shutdown machinery.
 
 
 @node Chain-loading
grub-core/loader/emu/linux.c +9 -14
index f232f195b3..0cf378a376 100644
@@ -38,8 +38,7 @@ grub_linux_boot (void)
 {
   grub_err_t rc = GRUB_ERR_NONE;
   char *initrd_param;
-  const char *kexec[] = {"kexec", "-la", kernel_path, boot_cmdline, NULL,
-	  NULL};
+  const char *kexec[] = {"kexec", "-la", kernel_path, boot_cmdline, NULL, NULL};
   const char *systemctl[] = {"systemctl", "kexec", NULL};
   int kexecute = grub_util_get_kexecute ();
 
@@ -59,7 +58,7 @@ grub_linux_boot (void)
   if (kexecute)
     rc = grub_util_exec (kexec);
 
-  grub_free(initrd_param);
+  grub_free (initrd_param);
 
   if (rc != GRUB_ERR_NONE)
     {
@@ -82,13 +81,12 @@ grub_linux_boot (void)
    * WARNING: forcible reset should only be used in read-only environments.
    * grub-emu cannot check for these - users beware.
    */
-  grub_dprintf ("linux", "Performing 'kexec -e -x'");
-  kexec[1] = "-e";
-  kexec[2] = "-x";
-  kexec[3] = NULL;
+  grub_dprintf ("linux", "Performing 'kexec -ex'");
+  kexec[1] = "-ex";
+  kexec[2] = NULL;
   rc = grub_util_exec (kexec);
-  if ( rc != GRUB_ERR_NONE )
-    grub_fatal (N_("error trying to directly perform 'kexec -e': %d"), rc);
+  if (rc != GRUB_ERR_NONE)
+    grub_fatal (N_("error trying to directly perform 'kexec -ex': %d"), rc);
 
   return rc;
 }
@@ -129,7 +127,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), int argc,
   if (argc > 1)
     {
       boot_cmdline = grub_xasprintf ("--command-line=%s", argv[1]);
-      for ( i = 2; i < argc; i++ )
+      for (i = 2; i < argc; i++)
         {
           tempstr = grub_xasprintf ("%s %s", boot_cmdline, argv[i]);
           grub_free (boot_cmdline);
@@ -154,7 +152,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), int argc,
 		       N_("Cannot find initrd file %s"), argv[0]);
 
   grub_free (initrd_path);
-  initrd_path = grub_xasprintf("%s", argv[0]);
+  initrd_path = grub_xasprintf ("%s", argv[0]);
 
   /* We are done - mark ourselves as on longer in use. */
   grub_dl_unref (my_mod);
@@ -171,9 +169,6 @@ GRUB_MOD_INIT (linux)
   cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
 				      N_("Load initrd."));
   my_mod = mod;
-  kernel_path = NULL;
-  initrd_path = NULL;
-  boot_cmdline = NULL;
 }
 
 GRUB_MOD_FINI (linux)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v6 1/1 Add support for grub-emu to kexec Linux menu entries

From: Raymund Will <[email protected]>

The GRUB emulator is used as a debugging utility but it could also be used as a user-space bootloader if there is support to boot an operating system.

The Linux kernel is already able to (re)boot another kernel via the kexec boot mechanism. So the grub-emu tool could rely on this feature and have linux and initrd commands that are used to pass a kernel, initramfs image and command line parameters to kexec for booting a selected menu entry.

By default the systemctl kexec option is used so systemd can shutdown all of the running services before doing a reboot using kexec. But if this is not present, it can fall back to executing the kexec user-space tool directly. The ability to force a kexec-reboot when systemctl kexec fails must only be used in controlled environments to avoid possible filesystem corruption and data loss.

---
 docs/grub.texi               |  30 ++++--
 grub-core/Makefile.am        |   1 +
 grub-core/Makefile.core.def  |   2 +-
 grub-core/kern/emu/main.c    |   4 +
 grub-core/kern/emu/misc.c    |  18 +++-
 grub-core/loader/emu/linux.c | 178 +++++++++++++++++++++++++++++++++++
 include/grub/emu/exec.h      |   4 +-
 include/grub/emu/hostfile.h  |   3 +-
 include/grub/emu/misc.h      |   3 +
 9 files changed, 231 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/loader/emu/linux.c
docs/grub.texi +22 -8
index af119dea3b..1663a076c0 100644
@@ -923,17 +923,17 @@ magic.
 @node General boot methods
 @section How to boot operating systems
 
-GRUB has two distinct boot methods. One of the two is to load an
-operating system directly, and the other is to chain-load another boot
-loader which then will load an operating system actually. Generally
-speaking, the former is more desirable, because you don't need to
-install or maintain other boot loaders and GRUB is flexible enough to
-load an operating system from an arbitrary disk/partition. However,
-the latter is sometimes required, since GRUB doesn't support all the
-existing operating systems natively.
+GRUB has three distinct boot methods: loading an operating system
+directly, using kexec from userspace, and chainloading another
+bootloader.  Generally speaking, the first two are more desirable
+because you don't need to install or maintain other boot loaders and
+GRUB is flexible enough to load an operating system from an arbitrary
+disk/partition.  However, chainloading is sometimes required, as GRUB
+doesn't support all existing operating systems natively.
 
 @menu
 * Loading an operating system directly::
+* Kexec::
 * Chain-loading::
 @end menu
 
@@ -959,6 +959,20 @@ use more complicated instructions. @xref{DOS/Windows}, for more
 information.
 
 
+@node Kexec
+@subsection Kexec with grub2-emu
+
+GRUB can be run in userspace by invoking the grub2-emu tool.  It will
+read all configuration scripts as if booting directly (see @xref{Loading
+an operating system directly}).  With the @code{--kexec} flag, and
+kexec(8) support from the operating system, the @command{linux} command
+will directly boot the target image.  For systems that lack working
+systemctl(1) support for kexec, passing the @code{--kexec} flag twice
+will fallback to invoking kexec(8) directly; note however that this
+fallback may be unsafe outside read-only environments, as it does not
+invoke shutdown machinery.
+
+
 @node Chain-loading
 @subsection Chain-loading an OS
 
grub-core/Makefile.am +1 -0
index ee88e44e97..80e7a83edf 100644
@@ -307,6 +307,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/net.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostdisk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostfile.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/exec.h
 if COND_GRUB_EMU_SDL
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/sdl.h
 endif
grub-core/Makefile.core.def +1 -1
index 7159948721..5350408601 100644
@@ -1816,9 +1816,9 @@ module = {
   arm64 = loader/arm64/linux.c;
   riscv32 = loader/riscv/linux.c;
   riscv64 = loader/riscv/linux.c;
+  emu = loader/emu/linux.c;
   common = loader/linux.c;
   common = lib/cmdline.c;
-  enable = noemu;
 };
 
 module = {
grub-core/kern/emu/main.c +4 -0
index 44e087e988..855b11c3de 100644
@@ -107,6 +107,7 @@ static struct argp_option options[] = {
    N_("use GRUB files in the directory DIR [default=%s]"), 0},
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
+  {"kexec",       'X', 0,      0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
@@ -164,6 +165,9 @@ argp_parser (int key, char *arg, struct argp_state *state)
     case 'v':
       verbosity++;
       break;
+    case 'X':
+      grub_util_set_kexecute ();
+      break;
 
     case ARGP_KEY_ARG:
       {
grub-core/kern/emu/misc.c +17 -1
index d0e7a107e7..521220b49d 100644
@@ -39,6 +39,7 @@
 #include <grub/emu/misc.h>
 
 int verbosity;
+int kexecute;
 
 void
 grub_util_warn (const char *fmt, ...)
@@ -82,7 +83,7 @@ grub_util_error (const char *fmt, ...)
   vfprintf (stderr, fmt, ap);
   va_end (ap);
   fprintf (stderr, ".\n");
-  exit (1);
+  grub_exit ();
 }
 
 void *
@@ -153,6 +154,9 @@ xasprintf (const char *fmt, ...)
 void
 grub_exit (void)
 {
+#if defined (GRUB_KERNEL)
+  grub_reboot ();
+#endif
   exit (1);
 }
 #endif
@@ -214,3 +218,15 @@ grub_util_load_image (const char *path, char *buf)
 
   fclose (fp);
 }
+
+void
+grub_util_set_kexecute (void)
+{
+  kexecute++;
+}
+
+int
+grub_util_get_kexecute (void)
+{
+  return kexecute;
+}
grub-core/loader/emu/linux.c new file +178 -0

178 lines not on this page · raw message

include/grub/emu/exec.h +3 -1
index d1073ef86a..1b61b4a2e5 100644
@@ -23,6 +23,8 @@
 #include <stdarg.h>
 
 #include <sys/types.h>
+#include <grub/symbol.h>
+
 pid_t
 grub_util_exec_pipe (const char *const *argv, int *fd);
 pid_t
@@ -32,7 +34,7 @@ int
 grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file,
 			     const char *stdout_file, const char *stderr_file);
 int
-grub_util_exec (const char *const *argv);
+EXPORT_FUNC(grub_util_exec) (const char *const *argv);
 int
 grub_util_exec_redirect (const char *const *argv, const char *stdin_file,
 			 const char *stdout_file);
include/grub/emu/hostfile.h +2 -1
index cfb1e2b566..a61568e36e 100644
@@ -22,6 +22,7 @@
 #include <grub/disk.h>
 #include <grub/partition.h>
 #include <sys/types.h>
+#include <grub/symbol.h>
 #include <grub/osdep/hostfile.h>
 
 int
@@ -29,7 +30,7 @@ grub_util_is_directory (const char *path);
 int
 grub_util_is_special_file (const char *path);
 int
-grub_util_is_regular (const char *path);
+EXPORT_FUNC(grub_util_is_regular) (const char *path);
 
 char *
 grub_util_path_concat (size_t n, ...);
include/grub/emu/misc.h +3 -0
index ff9c48a649..01056954b9 100644
@@ -57,6 +57,9 @@ void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...) __attribute__ ((format (
 void EXPORT_FUNC(grub_util_info) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
 void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2), noreturn));
 
+void EXPORT_FUNC(grub_util_set_kexecute) (void);
+int EXPORT_FUNC(grub_util_get_kexecute) (void) WARN_UNUSED_RESULT;
+
 grub_uint64_t EXPORT_FUNC (grub_util_get_cpu_time_ms) (void);
 
 #ifdef HAVE_DEVICE_MAPPER
Signature · 1 line
2.35.1
Reviewed-by on v6 1/1
#2 lore raw
Quoted 19 lines · Robbie Harwood

From: Raymund Will <[email protected]>

The GRUB emulator is used as a debugging utility but it could also be used as a user-space bootloader if there is support to boot an operating system.

The Linux kernel is already able to (re)boot another kernel via the kexec boot mechanism. So the grub-emu tool could rely on this feature and have linux and initrd commands that are used to pass a kernel, initramfs image and command line parameters to kexec for booting a selected menu entry.

By default the systemctl kexec option is used so systemd can shutdown all of the running services before doing a reboot using kexec. But if this is not present, it can fall back to executing the kexec user-space tool directly. The ability to force a kexec-reboot when systemctl kexec fails must only be used in controlled environments to avoid possible filesystem corruption and data loss.

Quoted 5 lines

Daniel