commit 5dcad5c72142bbc53a9ebf488a279724310d9698 Author: Leonid Krivoshein Date: Sat Feb 9 13:38:12 2019 +0300 grub-entries: added script for list grub menu (closes: #36048) diff --git a/.gear/rules b/.gear/rules index 8a73abf..718e4a8 100644 --- a/.gear/rules +++ b/.gear/rules @@ -2,6 +2,8 @@ tar: grub tar: grub-extras copy: grub2-sysconfig copy: grub*-autoupdate +copy: grub-entries +copy: grub-entries.8 copy: *.patch copy: alt/* copy: *.filetrigger diff --git a/grub-entries b/grub-entries new file mode 100755 index 0000000..6439593 --- /dev/null +++ b/grub-entries @@ -0,0 +1,141 @@ +#!/bin/sh -efu +### This file is covered by the GNU General Public License +### version 2 or later. +### +### Copyright (C) 2019 ALT Linux Team +### Author: Leonid Krivoshein + +numbers_only=0 +titles_only=0 +search_item= +progname="${0##*/}" +grubmenu="/boot/grub/grub.cfg" + + +show_help() { + cat <<-EOF +$progname - Show default or specified grub menu. + +Usage: $progname [options] + +Options: + -f, --config= Specify path to the grub config. + -n, --numbers Show only menu item sequence numbers. + -t, --titles Show only menu item names. + -c, --check= Specify menu item to validate. + -V, --version Show script version and exit. + -h, --help Show this help message and exit. +EOF + exit 0 +} + +show_version() { + cat <<-EOF +$progname version 1.0 +Copyright (C) 2019, ALT Linux Team +EOF + exit 0 +} + +show_usage() { + [ -z "$*" ] || echo "$*" >&2 + echo "Try '$progname --help' for more information." >&2 + exit 1 +} + +parse_args() { + local opts="+f:,n,t,c:,V,h" + local longopts="config:,numbers,titles,check:,version,help" + + opts=$(getopt -n "$progname" -o "$opts" -l "$longopts" -- "$@") || + show_usage "Invalid command-line arguments." + eval set -- "$opts" + while [ $# -gt 0 ]; do + case "$1" in + -f|--config) shift; grubmenu="$1";; + -n|--numbers) numbers_only=1; titles_only=0;; + -t|--titles) numbers_only=0; titles_only=1;; + -c|--check) shift; search_item="$1";; + -V|--version) show_version;; + -h|--help) show_help;; + --) shift; break;; + -*) show_usage "Unrecognized option: '$1'";; + *) break;; + esac + shift + done + [ $# -eq 0 ] || show_usage "Unrecognized arguments: '$*'" +} + +show_menu() { + local mode title number prefix entry=0 itemno=0 + + cat "$grubmenu" | sed \ + -re '/^\s*(menuentry\s+|submenu\s+|})/!d' \ + -re "s/^\s*menuentry\s+'([^']+)'.*\$/M\t\1/g" \ + -re 's/^\s*menuentry\s+"([^"]+)".*$/M\t\1/g' \ + -re "s/^\s*submenu\s+'([^']+)'.*\$/S\t\1/g" \ + -re 's/^\s*submenu\s+"([^"]+)".*$/S\t\1/g' \ + -re "s/^\s*}\s*\$/E\t-/g" | + while read mode title; do + case "$mode" in + M) if [ $numbers_only -ne 0 ]; then + echo -e "${number}${itemno}" + elif [ $titles_only -ne 0 ]; then + echo -e "${prefix}${title}" + else + echo -e "${number}${itemno}\t${prefix}${title}" + fi + itemno=$(($itemno + 1)) + entry=1 + ;; + S) number="${number}${itemno}>" + prefix="${prefix}${title}>" + itemno=0 + ;; + *) if [ $entry -ne 0 ]; then + entry=0 + elif [ -n "$number" ]; then + number="${number%>}" + prefix="${prefix%>}" + case "$number" in + *">"*) itemno="${number##*>}" + number="${number%>*}>" + prefix="${prefix%>*}>" + itemno=$(($itemno + 1)) + ;; + *) itemno=$number + itemno=$(($itemno + 1)) + prefix= + number= + ;; + esac + fi + ;; + esac + done +} + +check_item() { + local cnt + + numbers_only=1; titles_only=0 + cnt=$(show_menu | grep -swc "$search_item" ||:) + [ "$cnt" != "1" ] || exit 0 + + numbers_only=0; titles_only=1 + cnt=$(show_menu | grep -swc "$search_item" ||:) + [ "$cnt" != "1" ] || exit 0 + + [ -z "$cnt" -o "$cnt" = "0" ] && + echo "$search_item: menu item not found." >&2 || + echo "$search_item: duplicate items found." >&2 + exit 1 +} + + +parse_args "$@" +[ -r "$grubmenu" ] || show_usage "$grubmenu: config file not found." +[ -z "$search_item" ] || check_item +show_menu + diff --git a/grub-entries.8 b/grub-entries.8 new file mode 100644 index 0000000..e5a8da5 --- /dev/null +++ b/grub-entries.8 @@ -0,0 +1,44 @@ +.\" Copyright 2019 ALT Linux Team +.\" Lincensed under GPLv2+ +.TH GRUB-ENTRIES 8 "Febrary 2019" "grub-entries (ALT Linux Team)" +.SH NAME +grub-entries \- simple script for show grub menu and check menu items +.SH SYNOPSIS +.B grub-entries +[\fIoptions\fP] + +.SH DESCRIPTION +.B grub-entries +parse specified or default grub config file, list all grub menu items in the one of the three forms. +Also can search specified item in menu and returns 0, if this item exists, otherwise returns non-zero +value. By default, \fI/boot/grub/grub.cfg\fP config file used. + +.SH OPTIONS +.TP 5 +.BI \-f "\fR, \fP" \-\-config " filename" +Specify path to the grub config. +.TP 5 +.BI \-n "\fR, \fP" \-\-numbers +Show only menu item sequence numbers. +.TP 5 +.BI \-t "\fR, \fP" \-\-titles +Show only menu item names. +.TP 5 +.BI \-c "\fR, \fP" \-\-check " menuitem" +Specify menu item to validate. +.TP 5 +.BI \-V "\fR, \fP" \-\-version +Show script version and exit. +.TP 5 +.BI \-h "\fR, \fP" \-\-help +Show help message and exit. + +.SH FILES +.PD 0 +.B /boot/grub/grub.cfg +.PD +.SH AUTHORS +Leonid Krivoshein +.SH "SEE ALSO" +.BR grub-reboot (8), +.BR grub-set-default (8) diff --git a/grub.spec b/grub.spec index 9bfd211..a781b50 100644 --- a/grub.spec +++ b/grub.spec @@ -24,6 +24,9 @@ Source9: update-grub.8 Source10: grub-efi-autoupdate Source11: embedded_grub.cfg +Source12: grub-entries +Source13: grub-entries.8 + Patch0: grub-2.02-os-alt.patch Patch1: grub-2.00-sysconfig-path-alt.patch Patch2: grub-2.02-altlinux-theme.patch @@ -300,6 +303,7 @@ mkdir -p %buildroot/boot/grub/fonts install -pD -m755 %SOURCE8 %buildroot%_sbindir/ install -pD -m644 %SOURCE9 %buildroot%_man8dir/update-grub.8 +install -pD -m644 %SOURCE13 %buildroot%_man8dir/grub-entries.8 # TODO: drop the obsolete one (unifont.pf2) %buildroot%_bindir/grub-mkfont -o %buildroot/boot/grub/unifont.pf2 %_datadir/fonts/bitmap/misc/8x13.pcf.gz @@ -314,6 +318,7 @@ sed -i 's,@LOCALEDIR@,%_datadir/locale,g' %buildroot%_sysconfdir/grub.d/* install -pDm755 %SOURCE4 %buildroot%_rpmlibdir/grub.filetrigger install -pDm755 %SOURCE6 %buildroot%_sbindir/grub-autoupdate install -pDm755 %SOURCE10 %buildroot%_sbindir/grub-efi-autoupdate +install -pDm755 %SOURCE12 %buildroot%_sbindir/grub-entries # Ghost config file install -d %buildroot/boot/grub @@ -369,6 +374,7 @@ rm -f %buildroot%_libdir/grub-efi/*/*.h %_sbindir/grub-reboot %_sbindir/grub-set-default %_sbindir/grub-sparc64-setup +%_sbindir/grub-entries %_sbindir/update-grub %_bindir/grub-editenv %_bindir/grub-file