2022-11-30 23:09:02

by Steven Rostedt

[permalink] [raw]
Subject: [PATCH] kest.pl: Fix grub2 menu handling for rebooting

From: Steven Rostedt <[email protected]>

grub2 has submenus where to use grub-reboot, it requires:

grub-reboot X>Y

where X is the main index and Y is the submenu. Thus if you have:

menuentry 'Debian GNU/Linux' --class debian --class gnu-linux ...
[...]
}
submenu 'Advanced options for Debian GNU/Linux' $menuentry_id_option ...
menuentry 'Debian GNU/Linux, with Linux 6.0.0-4-amd64' --class debian --class gnu-linux ...
[...]
}
menuentry 'Debian GNU/Linux, with Linux 6.0.0-4-amd64 (recovery mode)' --class debian --class gnu-linux ...
[...]
}
menuentry 'Debian GNU/Linux, with Linux test' --class debian --class gnu-linux ...
[...]
}

And wanted to boot to the "Linux test" kernel, you need to run:

# grub-reboot 1>2

As 1 is the second top menu (the submenu) and 2 is the third of the sub
menu entries.

Have the grub.cfg parsing for grub2 handle such cases.

Signed-off-by: Steven Rostedt <[email protected]>
---
tools/testing/ktest/ktest.pl | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index 799cfc4aa12b..590518144167 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -1963,7 +1963,7 @@ sub run_scp_mod {

sub _get_grub_index {

- my ($command, $target, $skip) = @_;
+ my ($command, $target, $skip, $submenu) = @_;

return if (defined($grub_number) && defined($last_grub_menu) &&
$last_grub_menu eq $grub_menu && defined($last_machine) &&
@@ -1980,11 +1980,16 @@ sub _get_grub_index {

my $found = 0;

+ my $submenu_number = 0;
+
while (<IN>) {
if (/$target/) {
$grub_number++;
$found = 1;
last;
+ } elsif (defined($submenu) && /$submenu/) {
+ $submenu_number++;
+ $grub_number = -1;
} elsif (/$skip/) {
$grub_number++;
}
@@ -1993,6 +1998,9 @@ sub _get_grub_index {

dodie "Could not find '$grub_menu' through $command on $machine"
if (!$found);
+ if ($submenu_number > 0) {
+ $grub_number = "$submenu_number>$grub_number";
+ }
doprint "$grub_number\n";
$last_grub_menu = $grub_menu;
$last_machine = $machine;
@@ -2003,6 +2011,7 @@ sub get_grub_index {
my $command;
my $target;
my $skip;
+ my $submenu;
my $grub_menu_qt;

if ($reboot_type !~ /^grub/) {
@@ -2017,8 +2026,9 @@ sub get_grub_index {
$skip = '^\s*title\s';
} elsif ($reboot_type eq "grub2") {
$command = "cat $grub_file";
- $target = '^menuentry.*' . $grub_menu_qt;
- $skip = '^menuentry\s|^submenu\s';
+ $target = '^\s*menuentry.*' . $grub_menu_qt;
+ $skip = '^\s*menuentry';
+ $submenu = '^\s*submenu\s';
} elsif ($reboot_type eq "grub2bls") {
$command = $grub_bls_get;
$target = '^title=.*' . $grub_menu_qt;
@@ -2027,7 +2037,7 @@ sub get_grub_index {
return;
}

- _get_grub_index($command, $target, $skip);
+ _get_grub_index($command, $target, $skip, $submenu);
}

sub wait_for_input {
@@ -2090,7 +2100,7 @@ sub reboot_to {
if ($reboot_type eq "grub") {
run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
} elsif (($reboot_type eq "grub2") or ($reboot_type eq "grub2bls")) {
- run_ssh "$grub_reboot $grub_number";
+ run_ssh "$grub_reboot \"'$grub_number'\"";
} elsif ($reboot_type eq "syslinux") {
run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
} elsif (defined $reboot_script) {
--
2.35.1


2022-12-01 00:29:31

by John 'Warthog9' Hawley

[permalink] [raw]
Subject: Re: [PATCH] kest.pl: Fix grub2 menu handling for rebooting

On 11/30/22 14:54, Steven Rostedt wrote:
> From: Steven Rostedt <[email protected]>
>
> grub2 has submenus where to use grub-reboot, it requires:
>
> grub-reboot X>Y
>
> where X is the main index and Y is the submenu. Thus if you have:
>
> menuentry 'Debian GNU/Linux' --class debian --class gnu-linux ...
> [...]
> }
> submenu 'Advanced options for Debian GNU/Linux' $menuentry_id_option ...
> menuentry 'Debian GNU/Linux, with Linux 6.0.0-4-amd64' --class debian --class gnu-linux ...
> [...]
> }
> menuentry 'Debian GNU/Linux, with Linux 6.0.0-4-amd64 (recovery mode)' --class debian --class gnu-linux ...
> [...]
> }
> menuentry 'Debian GNU/Linux, with Linux test' --class debian --class gnu-linux ...
> [...]
> }
>
> And wanted to boot to the "Linux test" kernel, you need to run:
>
> # grub-reboot 1>2
>
> As 1 is the second top menu (the submenu) and 2 is the third of the sub
> menu entries.
>
> Have the grub.cfg parsing for grub2 handle such cases.
>
> Signed-off-by: Steven Rostedt <[email protected]>
> ---
> tools/testing/ktest/ktest.pl | 20 +++++++++++++++-----
> 1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
> index 799cfc4aa12b..590518144167 100755
> --- a/tools/testing/ktest/ktest.pl
> +++ b/tools/testing/ktest/ktest.pl
> @@ -1963,7 +1963,7 @@ sub run_scp_mod {
>
> sub _get_grub_index {
>
> - my ($command, $target, $skip) = @_;
> + my ($command, $target, $skip, $submenu) = @_;
>
> return if (defined($grub_number) && defined($last_grub_menu) &&
> $last_grub_menu eq $grub_menu && defined($last_machine) &&
> @@ -1980,11 +1980,16 @@ sub _get_grub_index {
>
> my $found = 0;
>
> + my $submenu_number = 0;
> +
> while (<IN>) {
> if (/$target/) {
> $grub_number++;
> $found = 1;
> last;
> + } elsif (defined($submenu) && /$submenu/) {
> + $submenu_number++;
> + $grub_number = -1;
> } elsif (/$skip/) {
> $grub_number++;
> }
> @@ -1993,6 +1998,9 @@ sub _get_grub_index {
>
> dodie "Could not find '$grub_menu' through $command on $machine"
> if (!$found);
> + if ($submenu_number > 0) {
> + $grub_number = "$submenu_number>$grub_number";
> + }
> doprint "$grub_number\n";
> $last_grub_menu = $grub_menu;
> $last_machine = $machine;
> @@ -2003,6 +2011,7 @@ sub get_grub_index {
> my $command;
> my $target;
> my $skip;
> + my $submenu;
> my $grub_menu_qt;
>
> if ($reboot_type !~ /^grub/) {
> @@ -2017,8 +2026,9 @@ sub get_grub_index {
> $skip = '^\s*title\s';
> } elsif ($reboot_type eq "grub2") {
> $command = "cat $grub_file";
> - $target = '^menuentry.*' . $grub_menu_qt;
> - $skip = '^menuentry\s|^submenu\s';
> + $target = '^\s*menuentry.*' . $grub_menu_qt;
> + $skip = '^\s*menuentry';
> + $submenu = '^\s*submenu\s';
> } elsif ($reboot_type eq "grub2bls") {
> $command = $grub_bls_get;
> $target = '^title=.*' . $grub_menu_qt;
> @@ -2027,7 +2037,7 @@ sub get_grub_index {
> return;
> }
>
> - _get_grub_index($command, $target, $skip);
> + _get_grub_index($command, $target, $skip, $submenu);
> }
>
> sub wait_for_input {
> @@ -2090,7 +2100,7 @@ sub reboot_to {
> if ($reboot_type eq "grub") {
> run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
> } elsif (($reboot_type eq "grub2") or ($reboot_type eq "grub2bls")) {
> - run_ssh "$grub_reboot $grub_number";
> + run_ssh "$grub_reboot \"'$grub_number'\"";
> } elsif ($reboot_type eq "syslinux") {
> run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
> } elsif (defined $reboot_script) {

Ahhh the fun of submenus!

Reviewed-by: John 'Warthog9' Hawley (VMware) <[email protected]>