Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iGLeQ-00086O-D8 for mharc-grub-devel@gnu.org; Fri, 04 Oct 2019 07:21:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50233) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGLeM-00081h-Au for grub-devel@gnu.org; Fri, 04 Oct 2019 07:21:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iGLeK-0000p9-Ak for grub-devel@gnu.org; Fri, 04 Oct 2019 07:21:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51874) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iGLeK-0000ou-28 for grub-devel@gnu.org; Fri, 04 Oct 2019 07:21:28 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BF07B2CD7E5 for ; Fri, 4 Oct 2019 11:21:26 +0000 (UTC) Received: by mail-wr1-f72.google.com with SMTP id f3so2560093wrr.23 for ; Fri, 04 Oct 2019 04:21:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wYE1GY804yfSb+fxz5Cf79JcfaQsAits7HPDNvFFEec=; b=stMDbOQcLDBF+e0IWRHQmgnGz3J9H05js+KqRRQBUfUIKU/QP84z1zhE/umIjkU7b3 3JiFWNgNIxtt1pBaqf4ifCmSnvcPi1GUAnC9hOGg/bHquxMxfBK3dPXneo92eU1JBLxz LDG00kDG0iU9kibcLUBqz8zY7exooTfJ+i0yW5hY/zyW09Vvevk25U3uRwuJPl4wlVcl vDkc3xuI+Va6Zk+Ya7386yzwCwfybZPdyaeEZ9oVaeK9lV1je2ORYjMdybC8epaYgUtK +GJUzPY07o1BFAJHIzmZ1JjmcWhP83iykFURFJyWpqtWFciDEO08EycW6HHE0bQ8QbW7 mImg== X-Gm-Message-State: APjAAAXz/PA/1oIzAuaYUcL0kM+REelj1VlCp84MGleyMPF8KQm3x/Nm toO9p4MUTNOhGabitFn9EVvc5Y8vWNNv5bnkt4LgZKcYl5zv8OmRe9z5hY6DhB3lVgHxbEoyc/C Jf20dsctbujw= X-Received: by 2002:a1c:28d4:: with SMTP id o203mr10828592wmo.142.1570188085027; Fri, 04 Oct 2019 04:21:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqwV+30M3U4mheuLjK0wvRN+LIx4zzcGcs82/I9PXmk4YqIAPzyFCZ388U9x+CccPZy98uBWkQ== X-Received: by 2002:a1c:28d4:: with SMTP id o203mr10828563wmo.142.1570188084534; Fri, 04 Oct 2019 04:21:24 -0700 (PDT) Received: from minerva.redhat.com ([90.168.169.92]) by smtp.gmail.com with ESMTPSA id u68sm11250039wmu.12.2019.10.04.04.21.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2019 04:21:23 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: John Jolly , Javier Martinez Canillas , Vladimir Serbinenko , Michael Chang , Daniel Kiper , Raymund Will Subject: [RFC PATCH] Add support for grub-emu to kexec Linux menu entries Date: Fri, 4 Oct 2019 13:21:12 +0200 Message-Id: <20191004112112.11528-1-javierm@redhat.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Oct 2019 11:21:33 -0000 From: John Jolly The GRUB emulator is used as a debugging utility but it could also be use= d as a user-space bootloader if there is support to boot an operating syste= m. 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 fallbacks to executing the kexec user-space tool directly= . Signed-off-by: John Jolly Signed-off-by: Javier Martinez Canillas --- Hello, This patch has been in the SUSE grub2 package for some time and we have a= lso included in the Fedora grub2 package now. I'm posting it as an RFC becaus= e I think that will need some discussions before being able to merge this pat= ch. Best regards, Javier 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 | 182 +++++++++++++++++++++++++++++++++++ include/grub/emu/exec.h | 4 +- include/grub/emu/hostfile.h | 3 +- include/grub/emu/misc.h | 3 + 8 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 grub-core/loader/emu/linux.c diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am index 3ea8e7ff45f..ede59617078 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am @@ -303,6 +303,7 @@ KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/e= mu/net.h KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/emu/hostdisk.h KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/emu/hostfile.h KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/extcmd.h +KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/emu/exec.h if COND_GRUB_EMU_SDL KERNEL_HEADER_FILES +=3D $(top_srcdir)/include/grub/sdl.h endif diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 26937041756..670b1cc01ff 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1801,9 +1801,9 @@ module =3D { arm64 =3D loader/arm64/linux.c; riscv32 =3D loader/riscv/linux.c; riscv64 =3D loader/riscv/linux.c; + emu =3D loader/emu/linux.c; common =3D loader/linux.c; common =3D lib/cmdline.c; - enable =3D noemu; }; =20 module =3D { diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c index 425bb960347..27edcfc9aee 100644 --- a/grub-core/kern/emu/main.c +++ b/grub-core/kern/emu/main.c @@ -107,6 +107,7 @@ static struct argp_option options[] =3D { N_("use GRUB files in the directory DIR [default=3D%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.")= , 0}, { 0, 0, 0, 0, 0, 0 } }; =20 @@ -164,6 +165,9 @@ argp_parser (int key, char *arg, struct argp_state *s= tate) case 'v': verbosity++; break; + case 'X': + grub_util_set_kexecute (); + break; =20 case ARGP_KEY_ARG: { diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c index 65db79baa10..754d0cda02f 100644 --- a/grub-core/kern/emu/misc.c +++ b/grub-core/kern/emu/misc.c @@ -39,6 +39,7 @@ #include =20 int verbosity; +int kexecute; =20 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 (); } =20 void * @@ -141,6 +142,9 @@ xasprintf (const char *fmt, ...) void grub_exit (void) { +#if defined (GRUB_KERNEL) + grub_reboot (); +#endif exit (1); } #endif @@ -202,3 +206,15 @@ grub_util_load_image (const char *path, char *buf) =20 fclose (fp); } + +void +grub_util_set_kexecute (void) +{ + kexecute++; +} + +int +grub_util_get_kexecute (void) +{ + return kexecute; +} diff --git a/grub-core/loader/emu/linux.c b/grub-core/loader/emu/linux.c new file mode 100644 index 00000000000..020cf56cd19 --- /dev/null +++ b/grub-core/loader/emu/linux.c @@ -0,0 +1,182 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, In= c. + * + * 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 . + */ + +#include +#include +#include +#include + +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static grub_dl_t my_mod; + +static char *kernel_path; +static char *initrd_path; +static char *boot_cmdline; + +static grub_err_t +grub_linux_boot (void) +{ + grub_err_t rc =3D GRUB_ERR_NONE; + char *initrd_param; + const char *kexec[] =3D { "kexec", "-l", kernel_path, boot_cmdline, NU= LL, NULL }; + const char *systemctl[] =3D { "systemctl", "kexec", NULL }; + int kexecute =3D grub_util_get_kexecute (); + + if (initrd_path) + { + initrd_param =3D grub_xasprintf ("--initrd=3D%s", initrd_path); + kexec[3] =3D initrd_param; + kexec[4] =3D boot_cmdline; + } + else + { + initrd_param =3D grub_xasprintf ("%s", ""); + } + + grub_dprintf ("linux", "%serforming 'kexec -l %s %s %s'\n", + (kexecute) ? "P" : "Not p", + kernel_path, initrd_param, boot_cmdline); + + if (kexecute) + rc =3D grub_util_exec (kexec); + + grub_free(initrd_param); + + if (rc !=3D GRUB_ERR_NONE) + { + grub_error (rc, N_("Error trying to perform kexec load operation."= )); + grub_sleep (3); + return rc; + } + + if (kexecute < 1) + grub_fatal (N_("Use '"PACKAGE"-emu --kexec' to force a system restar= t.")); + + grub_dprintf ("linux", "Performing 'systemctl kexec' (%s) ", + (kexecute=3D=3D1) ? "do-or-die" : "just-in-case"); + rc =3D grub_util_exec (systemctl); + + if (rc =3D=3D GRUB_ERR_NONE) + return rc; + + grub_error (rc, N_("Error trying to perform 'systemctl kexec'")); + + /* need to check read-only root before resetting hard!? */ + grub_dprintf ("linux", "Performing 'kexec -e -x'"); + kexec[1] =3D "-e"; + kexec[2] =3D "-x"; + kexec[3] =3D NULL; + rc =3D grub_util_exec (kexec); + if ( rc !=3D GRUB_ERR_NONE ) + grub_fatal (N_("Error trying to directly perform 'kexec -e'.")); + + return rc; +} + +static grub_err_t +grub_linux_unload (void) +{ + grub_dl_unref (my_mod); + if ( boot_cmdline !=3D NULL ) + grub_free (boot_cmdline); + boot_cmdline =3D NULL; + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), int argc, c= har *argv[]) +{ + int i; + char *tempstr; + + grub_dl_ref (my_mod); + + if (argc =3D=3D 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + if ( !grub_util_is_regular (argv[0]) ) + return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find kernel f= ile %s"), argv[0]); + + if ( kernel_path !=3D NULL ) + grub_free (kernel_path); + + kernel_path =3D grub_xasprintf ("%s", argv[0]); + + if ( boot_cmdline !=3D NULL ) + { + grub_free(boot_cmdline); + boot_cmdline =3D NULL; + } + + if ( argc > 1 ) + { + boot_cmdline =3D grub_xasprintf("--command-line=3D%s", argv[1]); + for ( i =3D 2; i < argc; i++ ) + { + tempstr =3D grub_xasprintf("%s %s", boot_cmdline, argv[i]); + grub_free(boot_cmdline); + boot_cmdline =3D tempstr; + } + } + + grub_loader_set (grub_linux_boot, grub_linux_unload, 0); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), int argc, = char *argv[]) +{ + if (argc =3D=3D 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); + + if ( !grub_util_is_regular (argv[0]) ) + return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find initrd f= ile %s"), argv[0]); + + if ( initrd_path !=3D NULL ) + grub_free (initrd_path); + + initrd_path =3D grub_xasprintf("%s", argv[0]); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_command_t cmd_linux, cmd_initrd; + +GRUB_MOD_INIT (linux) +{ + cmd_linux =3D grub_register_command ("linux", grub_cmd_linux, 0, N_("L= oad Linux.")); + cmd_initrd =3D grub_register_command ("initrd", grub_cmd_initrd, 0, N_= ("Load initrd.")); + my_mod =3D mod; + kernel_path =3D NULL; + initrd_path =3D NULL; + boot_cmdline =3D NULL; +} + +GRUB_MOD_FINI (linux) +{ + grub_unregister_command (cmd_linux); + grub_unregister_command (cmd_initrd); +} diff --git a/include/grub/emu/exec.h b/include/grub/emu/exec.h index d1073ef86af..1b61b4a2e5d 100644 --- a/include/grub/emu/exec.h +++ b/include/grub/emu/exec.h @@ -23,6 +23,8 @@ #include =20 #include +#include + 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); diff --git a/include/grub/emu/hostfile.h b/include/grub/emu/hostfile.h index cfb1e2b5661..a61568e36e9 100644 --- a/include/grub/emu/hostfile.h +++ b/include/grub/emu/hostfile.h @@ -22,6 +22,7 @@ #include #include #include +#include #include =20 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); =20 char * grub_util_path_concat (size_t n, ...); diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h index ce464cfd007..5ef4f79e689 100644 --- a/include/grub/emu/misc.h +++ b/include/grub/emu/misc.h @@ -56,6 +56,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)); =20 +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); =20 #ifdef HAVE_DEVICE_MAPPER --=20 2.21.0