Blame SOURCES/0116-Add-linux-and-initrd-commands-for-grub-emu.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Michael Chang <mchang@suse.com>
d9d99f
Date: Tue, 6 Feb 2018 09:09:00 +0100
d9d99f
Subject: [PATCH] Add linux and initrd commands for grub-emu
d9d99f
d9d99f
When using grub-emu, the linux and initrd commands are used as arguments
d9d99f
to the kexec command line tool, to allow booting the selected menu entry.
d9d99f
---
d9d99f
 grub-core/Makefile.core.def  |   1 -
d9d99f
 grub-core/kern/emu/main.c    |   4 +
d9d99f
 grub-core/kern/emu/misc.c    |  18 ++++-
d9d99f
 grub-core/loader/emu/linux.c | 172 +++++++++++++++++++++++++++++++++++++++++++
d9d99f
 include/grub/emu/exec.h      |   4 +-
d9d99f
 include/grub/emu/hostfile.h  |   3 +-
d9d99f
 include/grub/emu/misc.h      |   3 +
d9d99f
 grub-core/Makefile.am        |   1 +
d9d99f
 8 files changed, 202 insertions(+), 4 deletions(-)
d9d99f
 create mode 100644 grub-core/loader/emu/linux.c
d9d99f
d9d99f
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
b71686
index e52d77688..067b97a42 100644
d9d99f
--- a/grub-core/Makefile.core.def
d9d99f
+++ b/grub-core/Makefile.core.def
d9d99f
@@ -1710,7 +1710,6 @@ module = {
d9d99f
 
d9d99f
   common = loader/linux.c;
d9d99f
   common = lib/cmdline.c;
d9d99f
-  enable = noemu;
d9d99f
 
d9d99f
   efi = loader/efi/linux.c;
d9d99f
 };
d9d99f
diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c
b71686
index 55ea5a11c..846fe9715 100644
d9d99f
--- a/grub-core/kern/emu/main.c
d9d99f
+++ b/grub-core/kern/emu/main.c
d9d99f
@@ -107,6 +107,7 @@ static struct argp_option options[] = {
d9d99f
    N_("use GRUB files in the directory DIR [default=%s]"), 0},
d9d99f
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
d9d99f
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
d9d99f
+  {"kexec",       'X', 0,      0, N_("try the untryable."), 0},
d9d99f
   { 0, 0, 0, 0, 0, 0 }
d9d99f
 };
d9d99f
 
d9d99f
@@ -164,6 +165,9 @@ argp_parser (int key, char *arg, struct argp_state *state)
d9d99f
     case 'v':
d9d99f
       verbosity++;
d9d99f
       break;
d9d99f
+    case 'X':
d9d99f
+      grub_util_set_kexecute();
d9d99f
+      break;
d9d99f
 
d9d99f
     case ARGP_KEY_ARG:
d9d99f
       {
d9d99f
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
b71686
index 82012a72f..3d3a4a4a9 100644
d9d99f
--- a/grub-core/kern/emu/misc.c
d9d99f
+++ b/grub-core/kern/emu/misc.c
d9d99f
@@ -37,6 +37,7 @@
d9d99f
 #include <grub/emu/misc.h>
d9d99f
 
d9d99f
 int verbosity;
d9d99f
+int kexecute;
d9d99f
 
d9d99f
 void
d9d99f
 grub_util_warn (const char *fmt, ...)
d9d99f
@@ -80,7 +81,7 @@ grub_util_error (const char *fmt, ...)
d9d99f
   vfprintf (stderr, fmt, ap);
d9d99f
   va_end (ap);
d9d99f
   fprintf (stderr, ".\n");
d9d99f
-  exit (1);
d9d99f
+  grub_exit (1);
d9d99f
 }
d9d99f
 
d9d99f
 void *
d9d99f
@@ -140,6 +141,9 @@ void
d9d99f
 __attribute__ ((noreturn))
d9d99f
 grub_exit (int rc)
d9d99f
 {
d9d99f
+#if defined (GRUB_KERNEL)
d9d99f
+  grub_reboot();
d9d99f
+#endif
d9d99f
   exit (rc < 0 ? 1 : rc);
d9d99f
 }
d9d99f
 #endif
d9d99f
@@ -201,3 +205,15 @@ grub_util_load_image (const char *path, char *buf)
d9d99f
 
d9d99f
   fclose (fp);
d9d99f
 }
d9d99f
+
d9d99f
+void
d9d99f
+grub_util_set_kexecute(void)
d9d99f
+{
d9d99f
+  kexecute++;
d9d99f
+}
d9d99f
+
d9d99f
+int
d9d99f
+grub_util_get_kexecute(void)
d9d99f
+{
d9d99f
+  return kexecute;
d9d99f
+}
d9d99f
diff --git a/grub-core/loader/emu/linux.c b/grub-core/loader/emu/linux.c
d9d99f
new file mode 100644
b71686
index 000000000..fda9e00d2
d9d99f
--- /dev/null
d9d99f
+++ b/grub-core/loader/emu/linux.c
d9d99f
@@ -0,0 +1,172 @@
d9d99f
+/*
d9d99f
+ *  GRUB  --  GRand Unified Bootloader
d9d99f
+ *  Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
d9d99f
+ *
d9d99f
+ *  GRUB is free software: you can redistribute it and/or modify
d9d99f
+ *  it under the terms of the GNU General Public License as published by
d9d99f
+ *  the Free Software Foundation, either version 3 of the License, or
d9d99f
+ *  (at your option) any later version.
d9d99f
+ *
d9d99f
+ *  GRUB is distributed in the hope that it will be useful,
d9d99f
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d9d99f
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d9d99f
+ *  GNU General Public License for more details.
d9d99f
+ *
d9d99f
+ *  You should have received a copy of the GNU General Public License
d9d99f
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d9d99f
+ */
d9d99f
+
d9d99f
+#include <grub/loader.h>
d9d99f
+#include <grub/dl.h>
d9d99f
+#include <grub/command.h>
d9d99f
+#include <grub/time.h>
d9d99f
+
d9d99f
+#include <grub/emu/exec.h>
d9d99f
+#include <grub/emu/hostfile.h>
d9d99f
+#include <grub/emu/misc.h>
d9d99f
+
d9d99f
+GRUB_MOD_LICENSE ("GPLv3+");
d9d99f
+
d9d99f
+static grub_dl_t my_mod;
d9d99f
+
d9d99f
+static char *kernel_path;
d9d99f
+static char *initrd_path;
d9d99f
+static char *boot_cmdline;
d9d99f
+
d9d99f
+static grub_err_t
d9d99f
+grub_linux_boot (void)
d9d99f
+{
d9d99f
+  grub_err_t rc = GRUB_ERR_NONE;
d9d99f
+  char *initrd_param;
d9d99f
+  const char *kexec[] = { "kexec", "-l", kernel_path, boot_cmdline, NULL, NULL };
d9d99f
+  const char *systemctl[] = { "systemctl", "kexec", NULL };
d9d99f
+  int kexecute = grub_util_get_kexecute();
d9d99f
+
d9d99f
+  if (initrd_path) {
d9d99f
+    initrd_param = grub_xasprintf("--initrd=%s", initrd_path);
d9d99f
+    kexec[3] = initrd_param;
d9d99f
+    kexec[4] = boot_cmdline;
d9d99f
+  } else {
d9d99f
+    initrd_param = grub_xasprintf("%s", "");
d9d99f
+  }
d9d99f
+
d9d99f
+  grub_printf("%serforming 'kexec -l %s %s %s'\n",
d9d99f
+	(kexecute) ? "P" : "Not p",
d9d99f
+	kernel_path, initrd_param, boot_cmdline);
d9d99f
+
d9d99f
+  if (kexecute)
d9d99f
+    rc = grub_util_exec(kexec);
d9d99f
+
d9d99f
+  grub_free(initrd_param);
d9d99f
+
d9d99f
+  if (rc != GRUB_ERR_NONE) {
d9d99f
+    grub_error (rc, N_("Error trying to perform kexec load operation."));
d9d99f
+    grub_sleep (3);
d9d99f
+    return rc;
d9d99f
+  }
d9d99f
+  if (kexecute < 1)
d9d99f
+    grub_fatal (N_("Use '"PACKAGE"-emu --kexec' to force a system restart."));
d9d99f
+
d9d99f
+  grub_printf("Performing 'systemctl kexec' (%s) ",
d9d99f
+		(kexecute==1) ? "do-or-die" : "just-in-case");
d9d99f
+  rc = grub_util_exec (systemctl);
d9d99f
+
d9d99f
+  if (kexecute == 1)
d9d99f
+    grub_fatal (N_("Error trying to perform 'systemctl kexec'"));
d9d99f
+
d9d99f
+  /* need to check read-only root before resetting hard!? */
d9d99f
+  grub_printf("Performing 'kexec -e'");
d9d99f
+  kexec[1] = "-e";
d9d99f
+  kexec[2] = NULL;
d9d99f
+  rc = grub_util_exec(kexec);
d9d99f
+  if ( rc != GRUB_ERR_NONE )
d9d99f
+    grub_fatal (N_("Error trying to directly perform 'kexec -e'."));
d9d99f
+
d9d99f
+  return rc;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_err_t
d9d99f
+grub_linux_unload (void)
d9d99f
+{
d9d99f
+  grub_dl_unref (my_mod);
d9d99f
+  if ( boot_cmdline != NULL )
d9d99f
+    grub_free (boot_cmdline);
d9d99f
+  boot_cmdline = NULL;
d9d99f
+  return GRUB_ERR_NONE;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_err_t
d9d99f
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[])
d9d99f
+{
d9d99f
+  int i;
d9d99f
+  char *tempstr;
d9d99f
+
d9d99f
+  grub_dl_ref (my_mod);
d9d99f
+
d9d99f
+  if (argc == 0)
d9d99f
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
d9d99f
+
d9d99f
+  if ( !grub_util_is_regular(argv[0]) )
d9d99f
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find kernel file %s"), argv[0]);
d9d99f
+
d9d99f
+  if ( kernel_path != NULL )
d9d99f
+    grub_free(kernel_path);
d9d99f
+
d9d99f
+  kernel_path = grub_xasprintf("%s", argv[0]);
d9d99f
+
d9d99f
+  if ( boot_cmdline != NULL ) {
d9d99f
+    grub_free(boot_cmdline);
d9d99f
+    boot_cmdline = NULL;
d9d99f
+  }
d9d99f
+
d9d99f
+  if ( argc > 1 )
d9d99f
+  {
d9d99f
+    boot_cmdline = grub_xasprintf("--command-line=%s", argv[1]);
d9d99f
+    for ( i = 2; i < argc; i++ ) {
d9d99f
+      tempstr = grub_xasprintf("%s %s", boot_cmdline, argv[i]);
d9d99f
+      grub_free(boot_cmdline);
d9d99f
+      boot_cmdline = tempstr;
d9d99f
+    }
d9d99f
+  }
d9d99f
+
d9d99f
+  grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
d9d99f
+
d9d99f
+  return GRUB_ERR_NONE;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_err_t
d9d99f
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[])
d9d99f
+{
d9d99f
+  if (argc == 0)
d9d99f
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
d9d99f
+
d9d99f
+  if ( !grub_util_is_regular(argv[0]) )
d9d99f
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find initrd file %s"), argv[0]);
d9d99f
+
d9d99f
+  if ( initrd_path != NULL )
d9d99f
+    grub_free(initrd_path);
d9d99f
+
d9d99f
+  initrd_path = grub_xasprintf("%s", argv[0]);
d9d99f
+
d9d99f
+  grub_dl_unref (my_mod);
d9d99f
+
d9d99f
+  return GRUB_ERR_NONE;
d9d99f
+}
d9d99f
+
d9d99f
+static grub_command_t cmd_linux, cmd_initrd;
d9d99f
+
d9d99f
+GRUB_MOD_INIT(linux)
d9d99f
+{
d9d99f
+  cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0, N_("Load Linux."));
d9d99f
+  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0, N_("Load initrd."));
d9d99f
+  my_mod = mod;
d9d99f
+  kernel_path = NULL;
d9d99f
+  initrd_path = NULL;
d9d99f
+  boot_cmdline = NULL;
d9d99f
+}
d9d99f
+
d9d99f
+GRUB_MOD_FINI(linux)
d9d99f
+{
d9d99f
+  grub_unregister_command (cmd_linux);
d9d99f
+  grub_unregister_command (cmd_initrd);
d9d99f
+}
d9d99f
diff --git a/include/grub/emu/exec.h b/include/grub/emu/exec.h
b71686
index d1073ef86..1b61b4a2e 100644
d9d99f
--- a/include/grub/emu/exec.h
d9d99f
+++ b/include/grub/emu/exec.h
d9d99f
@@ -23,6 +23,8 @@
d9d99f
 #include <stdarg.h>
d9d99f
 
d9d99f
 #include <sys/types.h>
d9d99f
+#include <grub/symbol.h>
d9d99f
+
d9d99f
 pid_t
d9d99f
 grub_util_exec_pipe (const char *const *argv, int *fd);
d9d99f
 pid_t
d9d99f
@@ -32,7 +34,7 @@ int
d9d99f
 grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file,
d9d99f
 			     const char *stdout_file, const char *stderr_file);
d9d99f
 int
d9d99f
-grub_util_exec (const char *const *argv);
d9d99f
+EXPORT_FUNC(grub_util_exec) (const char *const *argv);
d9d99f
 int
d9d99f
 grub_util_exec_redirect (const char *const *argv, const char *stdin_file,
d9d99f
 			 const char *stdout_file);
d9d99f
diff --git a/include/grub/emu/hostfile.h b/include/grub/emu/hostfile.h
b71686
index 8e37d5acb..12c937a1a 100644
d9d99f
--- a/include/grub/emu/hostfile.h
d9d99f
+++ b/include/grub/emu/hostfile.h
d9d99f
@@ -22,6 +22,7 @@
d9d99f
 #include <grub/disk.h>
d9d99f
 #include <grub/partition.h>
d9d99f
 #include <sys/types.h>
d9d99f
+#include <grub/symbol.h>
d9d99f
 #include <grub/osdep/hostfile.h>
d9d99f
 
d9d99f
 int
d9d99f
@@ -29,7 +30,7 @@ grub_util_is_directory (const char *path);
d9d99f
 int
d9d99f
 grub_util_is_special_file (const char *path);
d9d99f
 int
d9d99f
-grub_util_is_regular (const char *path);
d9d99f
+EXPORT_FUNC(grub_util_is_regular) (const char *path);
d9d99f
 
d9d99f
 char *
d9d99f
 grub_util_path_concat (size_t n, ...);
d9d99f
diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h
b71686
index df6085bcb..a653132e3 100644
d9d99f
--- a/include/grub/emu/misc.h
d9d99f
+++ b/include/grub/emu/misc.h
d9d99f
@@ -60,6 +60,9 @@ void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...) __attribute__ ((format (
d9d99f
 void EXPORT_FUNC(grub_util_info) (const char *fmt, ...) __attribute__ ((format (__printf__, 1, 2)));
d9d99f
 void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((format (__printf__, 1, 2), noreturn));
d9d99f
 
d9d99f
+void EXPORT_FUNC(grub_util_set_kexecute) (void);
d9d99f
+int EXPORT_FUNC(grub_util_get_kexecute) (void) WARN_UNUSED_RESULT;
d9d99f
+
d9d99f
 grub_uint64_t EXPORT_FUNC (grub_util_get_cpu_time_ms) (void);
d9d99f
 
d9d99f
 #ifdef HAVE_DEVICE_MAPPER
d9d99f
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
b71686
index 9c69aa886..0108c0d42 100644
d9d99f
--- a/grub-core/Makefile.am
d9d99f
+++ b/grub-core/Makefile.am
d9d99f
@@ -274,6 +274,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/net.h
d9d99f
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostdisk.h
d9d99f
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostfile.h
d9d99f
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
d9d99f
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/exec.h
d9d99f
 if COND_GRUB_EMU_SDL
d9d99f
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/sdl.h
d9d99f
 endif