2012-08-31 09:28:14 +02:00
|
|
|
#!/usr/bin/perl
|
2014-02-21 04:09:36 +01:00
|
|
|
#
|
|
|
|
# (C) 2014 mchang@suse.com
|
|
|
|
#
|
|
|
|
# 2014-02-20 jw@suse.de
|
2012-08-31 09:28:14 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $grub2_dir;
|
|
|
|
my $grub2_reboot;
|
|
|
|
my $show_mapped;
|
|
|
|
my $id_name;
|
|
|
|
my @menuentry;
|
|
|
|
|
|
|
|
sub parse_menuentry {
|
|
|
|
|
|
|
|
my ($parent, $menu) = @_;
|
2015-08-26 11:54:23 +02:00
|
|
|
my @m = $menu =~ /(submenu|menuentry) \s+ (.*?) ( \{ (?: [^{}]* | (?3))* \} )/sxg;
|
2012-08-31 09:28:14 +02:00
|
|
|
|
|
|
|
for (my $i = 0; $i <= $#m; $i += 3) {
|
|
|
|
|
|
|
|
my $type = $m[$i];
|
2015-08-26 11:54:23 +02:00
|
|
|
my $title = `printf "%s\n" $m[$i+1] | head -1 | tr -d '\n'`;
|
2012-08-31 09:28:14 +02:00
|
|
|
my $data = $m[$i+2];
|
|
|
|
my $name = ($parent) ? "$parent>$title" : "$title";
|
|
|
|
|
|
|
|
if ($type eq "menuentry") {
|
|
|
|
push @menuentry, $name;
|
|
|
|
} elsif ($type eq "submenu") {
|
|
|
|
&parse_menuentry ($name, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:58:16 +02:00
|
|
|
# Enable restore grubenv service (bnc#892358)
|
|
|
|
# Restore grubenv settings for booting default entry to workaround the grub2-once cannot
|
|
|
|
# work and function properly on lvm, md and s390.
|
|
|
|
sub enable_restore_grubenv_service {
|
|
|
|
|
|
|
|
my $systemctl="/usr/bin/systemctl";
|
|
|
|
|
|
|
|
if (-x $systemctl) {
|
|
|
|
system "$systemctl --no-reload enable grub2-once >/dev/null 2>&1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 09:28:14 +02:00
|
|
|
$id_name = "";
|
|
|
|
if (@ARGV == 2 && ($ARGV[0] eq "--show-mapped")) {
|
|
|
|
$show_mapped = 1;
|
|
|
|
$id_name = $ARGV[1];
|
|
|
|
} elsif (@ARGV == 1) {
|
|
|
|
$show_mapped = 0;
|
|
|
|
$id_name = $ARGV[0];
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:09:36 +01:00
|
|
|
die "wrong command line options, try --help\n" if ($id_name eq "");
|
2012-08-31 09:28:14 +02:00
|
|
|
|
2014-02-21 04:09:36 +01:00
|
|
|
open(SYSCONF, "</etc/sysconfig/bootloader") || die "cannot read bootloader sysconfig: $!\n";
|
2012-08-31 09:28:14 +02:00
|
|
|
|
|
|
|
$grub2_dir = "";
|
|
|
|
while (<SYSCONF>) {
|
|
|
|
if (/LOADER_TYPE="(.*)"/) {
|
|
|
|
my $bl = $1;
|
2013-01-30 08:29:03 +01:00
|
|
|
if ($bl eq "grub2" || $bl eq "grub2-efi") {
|
2012-08-31 09:28:14 +02:00
|
|
|
$grub2_dir = "/boot/grub2";
|
|
|
|
$grub2_reboot = "/usr/sbin/grub2-reboot";
|
|
|
|
}
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close (SYSCONF);
|
|
|
|
|
2014-02-21 04:09:36 +01:00
|
|
|
if ($id_name eq "--help" or $id_name eq "-h")
|
|
|
|
{
|
|
|
|
print "Usage: grub2-once [--show-mapped ID | --list | ID | NAME_SUBSTRING ]\n";
|
|
|
|
system "$grub2_reboot \"--help\"";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 09:28:14 +02:00
|
|
|
die "no grub2_dir" if ($grub2_dir eq "");
|
|
|
|
|
2014-02-21 04:09:36 +01:00
|
|
|
open(MENU, "<$grub2_dir/grub.cfg") || die "cannot read grub.cfg in $grub2_dir: $!\n";
|
2012-08-31 09:28:14 +02:00
|
|
|
undef $/;
|
|
|
|
|
|
|
|
while (<MENU>) {
|
|
|
|
&parse_menuentry ("", $_);
|
|
|
|
}
|
|
|
|
|
|
|
|
close (MENU);
|
|
|
|
|
|
|
|
my $ret = "";
|
|
|
|
my $name = "";
|
|
|
|
my $id = -1;
|
|
|
|
|
2014-02-21 04:09:36 +01:00
|
|
|
if ($id_name eq '--list')
|
|
|
|
{
|
|
|
|
my $c = 0;
|
|
|
|
foreach my $e (@menuentry)
|
|
|
|
{
|
|
|
|
printf "%6d %s\n", $c, $e;
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2012-08-31 09:28:14 +02:00
|
|
|
if ($id_name =~ m!^[0-9]+$!) {
|
|
|
|
|
|
|
|
if ($id_name < @menuentry) {
|
|
|
|
$id = $id_name;
|
|
|
|
$name = $menuentry[$id];
|
|
|
|
$ret = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
my $i = -1;
|
|
|
|
my $c = 0;
|
|
|
|
|
|
|
|
$name = $id_name;
|
|
|
|
|
|
|
|
foreach my $e (@menuentry) {
|
|
|
|
if ($e =~ qr!\Q$name\E!) {
|
|
|
|
$i = $c;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
} continue {
|
|
|
|
++$c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i >= 0) {
|
|
|
|
$id = $i;
|
|
|
|
$name = $menuentry[$id];
|
|
|
|
$ret = "$id";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($show_mapped > 0) {
|
|
|
|
print $ret;
|
|
|
|
} else {
|
|
|
|
system "$grub2_reboot \"$name\"";
|
2015-04-13 19:58:16 +02:00
|
|
|
enable_restore_grubenv_service;
|
2012-08-31 09:28:14 +02:00
|
|
|
}
|
|
|
|
|