Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ok9A1-0007D7-IP for mharc-grub-devel@gnu.org; Sun, 16 Oct 2022 15:18:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57366) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ok9A0-0007Ce-2Z for grub-devel@gnu.org; Sun, 16 Oct 2022 15:18:56 -0400 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]:55226) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ok99x-0000yo-HA for grub-devel@gnu.org; Sun, 16 Oct 2022 15:18:55 -0400 Received: by mail-wm1-x32b.google.com with SMTP id iv17so7237039wmb.4 for ; Sun, 16 Oct 2022 12:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date:from:to:cc :subject:date:message-id:reply-to; bh=XKkBBNoxGnm55AqUC2p8Gmjpmekdofzkxv8q6oo6AF0=; b=CGc8zyrqGJ11EJoyA62sYEP2tJaMlEqfm6mFSnAthzu8/XEWx3ihJSON78u6JUEyo9 0ct5r7jm4h7t7pcBY+zNvtAxOch38hKE5RBJLotmYL3CveyaStvfXCEyohe2hDQ0Uo1Y 73mQoP6rkxC/RNqY7WhBmecRDac7ydtdJHvuiZ69mnJ4JnkRurrfczVxav/ldVvD27HK HXdRAg4eLi3Sc9DdjhhVAb45RfP+2Q1QDUfoD+7XK3CnRC90r5hDAmHFX4anOIU4fQtQ 9tfJneujopX2AhqG5VbxU7Rnp2ffu+VIgKNpJZJycodkf9L/jGKWKLtxSiHs5J7CJszU qMQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XKkBBNoxGnm55AqUC2p8Gmjpmekdofzkxv8q6oo6AF0=; b=IJSOhEvt/tJiSRKHzSPbkOJOAX9+Ilt/pGc1yWt+/VTPSdkfW9Qz5BoL5ZmUhrpgC8 5KaLjh/JKONFogQgv7PYWUI61OPZdOlYoWOXlCWcQkDQ6rf1s361urxBydls7+fNIlZ9 iWNBCM/r2Z35hPs5yhkCVzi3AAM5z97WKZFoJ5NkwbKKxgu0MeAHwXO9B0FtyXy0LFC/ 4+YTX1VgS6z+7Y+6VsNseIriHY492B/5svtJcFtXZgnVzQ87XRoIUwrithcBJmNdeuR4 QKee8RnMasjketv8CR5aa3sNSqtq+17ysxhKAEQ4aODAS/6nUT9A0TAL5sEvjLKVlrx4 1VEQ== X-Gm-Message-State: ACrzQf368lip20r6R1INCz2KqAQYr78iyehBt3jmGaXSnEXaVJJdn9Vl du1ABq45CxrGksP51hnV5OxAreelnIbYog== X-Google-Smtp-Source: AMsMyM5kNrTIhJ2EaGFfU4tvkax6uPE+SrVlFSkYDUQZVgc6aqivF2LzcSvXtld3twI2NfXRUKyWVQ== X-Received: by 2002:a05:600c:4611:b0:3c5:e3a3:942a with SMTP id m17-20020a05600c461100b003c5e3a3942amr17259366wmo.82.1665947931076; Sun, 16 Oct 2022 12:18:51 -0700 (PDT) Received: from dj3ntoo (171.sub-72-110-15.myvzw.com. [72.110.15.171]) by smtp.gmail.com with ESMTPSA id d1-20020adff2c1000000b0022ac1be009esm6928600wrp.16.2022.10.16.12.18.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Oct 2022 12:18:50 -0700 (PDT) Date: Sun, 16 Oct 2022 14:18:44 -0500 From: Oskari Pirhonen To: The development of GNU GRUB Cc: Raymund Will , dkiper@net-space.pl, rw@suse.de, John Jolly , Javier Martinez Canillas , Robbie Harwood , base-system@gentoo.org Subject: Re: [PATCH v4 1/1] Add support for grub-emu to kexec Linux menu entries Message-ID: Mail-Followup-To: The development of GNU GRUB , Raymund Will , dkiper@net-space.pl, rw@suse.de, John Jolly , Javier Martinez Canillas , Robbie Harwood , base-system@gentoo.org References: <20221004191648.77723-1-rharwood@redhat.com> <20221004191648.77723-2-rharwood@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="vMOytMh8hy6sRmVH" Content-Disposition: inline In-Reply-To: <20221004191648.77723-2-rharwood@redhat.com> Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=xxc3ncoredxx@gmail.com; helo=mail-wm1-x32b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Oct 2022 19:18:56 -0000 --vMOytMh8hy6sRmVH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Oct 04, 2022 at 15:16:48 -0400, Robbie Harwood wrote: > From: Raymund Will >=20 > 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. >=20 > 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. >=20 > 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. >=20 Can the existence of systemd/systemctl be checked at configure-time? I run OpenRC where systemctl is unavailable, so it looks like it would always hit the error case unless I pass the arg twice. It could also cause confusion to users if they notice it fails to call systemctl when on non-systemd systems. The simplest/safest solution would be to just disable this if the configure check fails until `systemctl kexec`-equivalent code paths are added. I've CC-ed the Gentoo Base System project [1] in case some of them can provide additional input, such as how to get the safe path working with other init systems. - Oskari [1] https://wiki.gentoo.org/wiki/Project:Base > Signed-off-by: Raymund Will > Signed-off-by: John Jolly > Signed-off-by: Javier Martinez Canillas > Signed-off-by: Robbie Harwood > --- > 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 | 181 +++++++++++++++++++++++++++++++++++ > include/grub/emu/exec.h | 4 +- > include/grub/emu/hostfile.h | 3 +- > include/grub/emu/misc.h | 3 + > 8 files changed, 212 insertions(+), 4 deletions(-) > create mode 100644 grub-core/loader/emu/linux.c >=20 > diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am > index ee88e44e97..80e7a83edf 100644 > --- a/grub-core/Makefile.am > +++ b/grub-core/Makefile.am > @@ -307,6 +307,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 7159948721..5350408601 100644 > --- a/grub-core/Makefile.core.def > +++ b/grub-core/Makefile.core.def > @@ -1816,9 +1816,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 44e087e988..855b11c3de 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 vi= a systemctl (pass twice to enable dangerous fallback to non-systemctl)."), = 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 d0e7a107e7..521220b49d 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 * > @@ -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) > =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 0000000000..bdcdbb0ff4 > --- /dev/null > +++ b/grub-core/loader/emu/linux.c > @@ -0,0 +1,181 @@ > +/* > + * 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", "-la", kernel_path, boot_cmdline, N= ULL, 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 -la %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 (kexecute =3D=3D 1) > + grub_fatal (N_("Error trying to perform 'systemctl kexec'")); > + > + /* WARNING: forcible reset should only be used in read-only environmen= ts. > + * grub-emu cannot check for these - users beware. */ > + 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 d1073ef86a..1b61b4a2e5 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 cfb1e2b566..a61568e36e 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 ff9c48a649..01056954b9 100644 > --- a/include/grub/emu/misc.h > +++ b/include/grub/emu/misc.h > @@ -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)); > =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.35.1 >=20 >=20 > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel --vMOytMh8hy6sRmVH Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQQfOU+JeXjo4uxN6vCp8he9GGIfEQUCY0xZDgAKCRCp8he9GGIf EbWVAQDh/ylUzAO2JHWjkKCd+WTs7vwdqhP7hELPI/N7O1E+CwEA8eEBLIJDMuka b43ASyaZRMzAMckWif5+4f4EnpcstgU= =lUEC -----END PGP SIGNATURE----- --vMOytMh8hy6sRmVH--