显示grub菜单和选项而无需重新启动?


12

我想grub从命令行显示菜单。同样要选择一个grub引导菜单选项,然后按Enter以查看加载了哪些内核前驱动程序以及在加载内核时传递了引导参数。

从命令行执行此操作的原因:

  • 重新启动以查看grub菜单需要花费时间。
  • 拍摄grub菜单图片并将其发布在网站上很尴尬。当Ubuntu启动并运行时,抓取屏幕截图会更容易。
  • edit使用GRUB菜单选项e和拍照往往是困难的,因为屏幕是难以阅读。使用此功能,您可以复制和粘贴。
  • 使用此功能显示所有内核版本可能比使用apt list --installed | grep linux-imageor 更为容易ls /boot/vml*
  • 快速查看grub菜单编号对于grub-rebootgrub-set-default命令来说很有价值。

如何从命令行绘制grub菜单,查看内部grub菜单条目号并显示给定选项的启动参数?

Answers:


12

更新于2018年5月7日

开发脚本:用Bash脚本将Ubuntu克隆到新分区以测试18.04 LTS升级我发现您得到了一些冗长的菜单选项,这些菜单选项会导致菜单损坏:

4>8  Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) (on /dev/nvme0n1p8)

今天已通过截断超过68个字符的行来解决此问题。

更新于2018年4月5日

此更新引入grub-menu.sh了比以前的答案更好的版本(仍在下面提供)。新的grub菜单功能:

  • 显示grub 2菜单条目号。即011>01>1... 23
  • 可以设置默认的简短版本,不带(upstart)(recover mode)子菜单选项。
  • 参数1可以作为shortlong覆盖默认值。
  • 列标题基于shortlong设置动态格式化。

彩色屏幕截图(简短版)

grub-menu.sh

文字萤幕撷取画面(加长版)

Grub Version: 2.02~beta2-36ubuntu3.15


        ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
        │ Menu No. --------------- Menu Name ---------------                       │ 
        │                                                                          │ 
        │     0    Ubuntu                                                     ↑    │ 
        │     1    Advanced options for Ubuntu                                ▮    │ 
        │     1>0  Ubuntu, with Linux 4.14.31-041431-generic                  ▒    │ 
        │     1>1  Ubuntu, with Linux 4.14.31-041431-generic (upstart)        ▒    │ 
        │     1>2  Ubuntu, with Linux 4.14.31-041431-generic (recovery mode)  ▒    │ 
        │     1>3  Ubuntu, with Linux 4.14.30-041430-generic                  ▒    │ 
        │     1>4  Ubuntu, with Linux 4.14.30-041430-generic (upstart)        ▒    │ 
        │     1>5  Ubuntu, with Linux 4.14.30-041430-generic (recovery mode)  ▒    │ 
        │     1>6  Ubuntu, with Linux 4.14.27-041427-generic                  ▒    │ 
        │     1>7  Ubuntu, with Linux 4.14.27-041427-generic (upstart)        ▒    │ 
        │     1>8  Ubuntu, with Linux 4.14.27-041427-generic (recovery mode)  ▒    │ 
        │     1>9  Ubuntu, with Linux 4.14.24-041424-generic                  ▒    │ 
        │     1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart)        ▒    │ 
        │     1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode)  ▒    │ 
        │     1>12 Ubuntu, with Linux 4.14.23-041423-generic                  ▒    │ 
        │     1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart)        ↓    │ 
        │                                                                          │ 
        │                                                                          │ 
        │                   <Display Grub Boot>        <Exit>                      │ 
        │                                                                          │ 
        └──────────────────────────────────────────────────────────────────────────┘ 

grub-menu.sh bash脚本

以前的版本,grub-display.sh并且grub-display-lite.sh代码中需要许多调整选项。grub-menu.sh只有一个选项可以调整:

# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false

将值设置为truefalse

使用以下命令调用脚本时,可以覆盖默认格式:

grub-menu.sh short

要么:

grub-menu.sh long

代码:

#!/bin/bash

# NAME: grub-menu.sh
# PATH: $HOME/bin
# DESC: Written for AU Q&A: /ubuntu//q/1019213/307523
# DATE: Apr 5, 2018. Modified: May 7, 2018.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi

AllMenusArr=()      # All menu options.
# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false
if [[ $1 == short ]] ; then
    HideUpstartRecovery=true    # override default with first passed parameter "short"
elif [[ $1 == long ]] ; then
    HideUpstartRecovery=false   # override default with first passed parameter "long"
fi
SkippedMenuEntry=false  # Don't change this value, automatically maintained
InSubMenu=false     # Within a line beginning with `submenu`?
InMenuEntry=false   # Within a line beginning with `menuentry` and ending in `{`?
NextMenuEntryNo=0   # Next grub internal menu entry number to assign
# Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
ThisSubMenuMajorNo=0
NextSubMenuMinorNo=0
CurrTag=""          # Current grub internal menu number, zero based
CurrText=""         # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
SubMenuList=""      # Only supports 10 submenus! Numbered 0 to 9. Future use.

while read -r line; do
    # Example: "           }"
    BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
    if [[ $BlackLine == "}" ]] ; then
        # Add menu option in buffer
        if [[ $SkippedMenuEntry == true ]] ; then
            NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            SkippedMenuEntry=false
            continue
        fi
        if [[ $InMenuEntry == true ]] ; then
            InMenuEntry=false
            if [[ $InSubMenu == true ]] ; then
                NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            else
                NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
            fi
        elif [[ $InSubMenu == true ]] ; then
            InSubMenu=false
            NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
        else
            continue # Future error message?
        fi
        # Set maximum CurrText size to 68 characters.
        CurrText="${CurrText:0:67}"
        AllMenusArr+=($CurrTag "$CurrText")
    fi

    # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
    #          "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
    if [[ $line == submenu* ]] ; then
        # line starts with `submenu`
        InSubMenu=true
        ThisSubMenuMajorNo=$NextMenuEntryNo
        NextSubMenuMinorNo=0
        SubMenuList=$SubMenuList$ThisSubMenuMajorNo
        CurrTag=$NextMenuEntryNo
        CurrText="${line#*\'}"
        CurrText="${CurrText%%\'*}"
        AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

    elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
        # line starts with `menuentry` and ends with `{`
        if [[ $HideUpstartRecovery == true ]] ; then
            if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
                SkippedMenuEntry=true
                continue
            fi
        fi
        InMenuEntry=true
        if [[ $InSubMenu == true ]] ; then
            : # In a submenu, increment minor instead of major which is "sticky" now.
            CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
        else
            CurrTag=$NextMenuEntryNo
        fi
        CurrText="${line#*\'}"
        CurrText="${CurrText%%\'*}"

    else
        continue    # Other stuff - Ignore it.
    fi

done < /boot/grub/grub.cfg

LongVersion=$(grub-install --version)
ShortVersion=$(echo "${LongVersion:20}")
DefaultItem=0

if [[ $HideUpstartRecovery == true ]] ; then
    MenuText="Menu No.     ----------- Menu Name -----------"
else
    MenuText="Menu No. --------------- Menu Name ---------------"
fi

while true ; do

    Choice=$(whiptail \
        --title "Use arrow, page, home & end keys. Tab toggle option" \
        --backtitle "Grub Version: $ShortVersion" \
        --ok-button "Display Grub Boot" \
        --cancel-button "Exit" \
        --default-item "$DefaultItem" \
        --menu "$MenuText" 24 76 16 \
        "${AllMenusArr[@]}" \
        2>&1 >/dev/tty)

    clear
    if [[ $Choice == "" ]]; then break ; fi
    DefaultItem=$Choice

    for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
        if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
            i=$i+1
            MenuEntry="menuentry '"${AllMenusArr[i]}"'"
            break
        fi
    done

    TheGameIsAfoot=false
    while read -r line ; do
        if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
        if [[ $TheGameIsAfoot == true ]]; then
            echo $line
            if [[ $line = *"}"* ]]; then break ; fi
        fi
    done < /boot/grub/grub.cfg

    read -p "Press <Enter> to continue"

done

exit 0

以前的版本(不推荐)

以下是原始答案,其中菜单项编号遵循grub 1格式。

grub-display.sh 显示grub菜单选项和参数

无需依赖第三方应用程序,您可以使用bash脚本显示grub任何给定选项的菜单和启动参数。引导参数不仅仅是cat /proc/cmdline值。它们还包括在引导Linux之前加载的驱动程序。

grub-display.sh bash脚本

这是您可以复制和粘贴的完整程序清单:

#!/bin/bash

# NAME: grub-display.sh
# PATH: $HOME/bin
# DESC: Written for AU Q&A: /ubuntu//q/1019213/307523
# DATE: Mar 24, 2018. Modified: Mar 26, 2018.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi

# Must have the dialog package. On Servers, not installed by default
command -v dialog >/dev/null 2>&1 || { echo >&2 "dialog package required but it is not installed.  Aborting."; exit 99; }

# Version without upstart and recovery options displayed
#awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
#        | grep -v upstart | grep -v recovery > ~/.grub-display-menu

# Version with upstart and recovery options displayed
awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
        > ~/.grub-display-menu

MenuArr=()

while read -r line; do 
    MenuNmbr=${line%% *}
    MenuName=${line#* }
    MenuArr+=($MenuNmbr "$MenuName")
done < ~/.grub-display-menu
rm ~/.grub-display-menu

LongVersion=$(grub-install --version)
ShortVersion=$(echo "${LongVersion:20}")
DefaultItem=0

while true ; do

    Choice=$(dialog \
        --title "Use arrow, page, home & end keys. Tab toggle option" \
        --backtitle "Grub Version: $ShortVersion" \
        --ok-label "Display Grub Boot" \
        --cancel-label "Exit" \
        --default-item "$DefaultItem" \
        --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \
        "${MenuArr[@]}" \
        >/dev/tty)

    clear
    if [[ $Choice == "" ]]; then break ; fi
    DefaultItem=$Choice

    for (( i=0; i < ${#MenuArr[@]}; i=i+2 )) ; do
        if [[ "${MenuArr[i]}" == $Choice ]] ; then
            i=$i+1
            MenuEntry="menuentry '"${MenuArr[i]}"'"
            break
        fi
    done

    TheGameIsAfoot=false
    while read -r line ; do
        if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
        if [[ $TheGameIsAfoot == true ]]; then
            echo $line
            if [[ $line = *"}"* ]]; then break ; fi
        fi
    done < /boot/grub/grub.cfg

    read -p "Press <Enter> to continue"

done

exit 0

给Ubuntu Server用户的注意事项

这个bash脚本是为Ubuntu桌面设计的。对于dialog默认情况下未安装软件包的Ubuntu Server和其他Linux发行版,grub-display-lite.sh下面包含一个不同的脚本。该版本使用whiptail代替dialog

菜单尺寸减少66%

要缩短显示的grub菜单选项列表,您可以删除(upstart)(recovery)选项。为此,请取消注释以下行:

# Version without upstart and recovery options displayed
awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
        | grep -v upstart | grep -v recovery > ~/.grub-display-menu

然后对这些行应用注释:

# Version with upstart and recovery options displayed
#awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
#        > ~/.grub-display-menu

屏幕截图

这是从命令行调用时的样子。不幸的是,我无法复制和粘贴菜单,不得不使用Print Screen

grub-display.sh.png

关闭鼠标支持复制和粘贴

 Grub Version: 2.02~beta2-36ubuntu3.15
 ──────────────────────────────────────────────────────────────────────────────────────────
       ┌──────────Use arrow, page, home & end keys. Tab toggle option─────────────┐
        Menu Number  ----------- Menu Name ----------                              
        ┌──────────────────────────────────────────────────────────────────────┐   
            0   Ubuntu                                                           
            1   Ubuntu, with Linux 4.14.30-041430-generic                        
            2   Ubuntu, with Linux 4.14.30-041430-generic (upstart)              
            3   Ubuntu, with Linux 4.14.30-041430-generic (recovery mode)        
            4   Ubuntu, with Linux 4.14.27-041427-generic                        
            5   Ubuntu, with Linux 4.14.27-041427-generic (upstart)              
            6   Ubuntu, with Linux 4.14.27-041427-generic (recovery mode)        
            7   Ubuntu, with Linux 4.14.24-041424-generic                        
            8   Ubuntu, with Linux 4.14.24-041424-generic (upstart)              
            9   Ubuntu, with Linux 4.14.24-041424-generic (recovery mode)        
            10  Ubuntu, with Linux 4.14.23-041423-generic                        
            11  Ubuntu, with Linux 4.14.23-041423-generic (upstart)              
            12  Ubuntu, with Linux 4.14.23-041423-generic (recovery mode)        
            13  Ubuntu, with Linux 4.14.21-041421-generic                        
            14  Ubuntu, with Linux 4.14.21-041421-generic (upstart)              
            15  Ubuntu, with Linux 4.14.21-041421-generic (recovery mode)        
        └────↓(+)──────────────────────────────────────────────────────16%─────┘   
                                                                                   
       ├──────────────────────────────────────────────────────────────────────────┤  
                    <Display Grub Boot>       <      Exit       >                  
       └──────────────────────────────────────────────────────────────────────────┘  

启用默认鼠标支持后,您无法将屏幕复制到剪贴板,而必须Print Screen用于图形屏幕快照。为了支持复制和粘贴,您需要通过搜索以下行来禁用鼠标支持:

    --default-item "$DefaultItem" \
    --no-mouse \
    --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \

参数--no-mouse已插入下面--default-item。这意味着您失去了对鼠标的支持,但是通过突出显示文本并按Ctrl+ 来获得更好的分辨率并复制到剪贴板C

显示grub启动参数

使用导航键突出显示一个选项,然后按Enter查看其启动参数:

menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
recordfail
savedefault
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
else
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
fi
echo 'Loading Linux 4.14.27-041427-generic ...'
linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-4.14.27-041427-generic
}
Press <Enter> to continue

Grub菜单项#94

menuentry 'Windows Boot Manager (on /dev/nvme0n1p2)' --class windows --class os $menuentry_id_option 'osprober-efi-D656-F2A8' {
savedefault
insmod part_gpt
insmod fat
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root D656-F2A8
else
search --no-floppy --fs-uuid --set=root D656-F2A8
fi
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}
Press <Enter> to continue

Grub菜单项#96

menuentry 'System setup' $menuentry_id_option 'uefi-firmware' {
fwsetup
}
Press <Enter> to continue

grub-display-lite.sh 适用于Ubuntu Server

dialog默认情况下,Ubuntu Server和Lubuntu没有像Ubuntu Desktop那样安装软件包。根据whiptail大多数Linux发行版默认包含的软件包,为这些用户编写了不同的版本。

缺点whiptail是功能较少,但在这种情况下不使用它们。另一个缺点似乎是颜色较少,但这会使某些人更容易阅读。有优势,whiptaildialog诸如复制到剪贴板,鼠标滚轮的支持和可能更快的处理。

grub-display-lite.sh bash脚本

#!/bin/bash

# NAME: grub-display-lite.sh
# PATH: $HOME/bin
# DESC: Written for AU Q&A: /ubuntu//q/1019213/307523
# DATE: Mar 26, 2018.
# NOTE: "lite" version written for Ubuntu Server and Lubuntu which do
#       not have `dialog` installed by default. `whiptail` is used
#       instead. Nice consequences are better resolution, mouse scroll
#       wheel and copy to clipboard support.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
    notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
    exit 1
fi

# Version without upstart and recovery options displayed
awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
        | grep -v upstart | grep -v recovery > ~/.grub-display-menu

# Version with upstart and recovery options displayed
#awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
#        > ~/.grub-display-menu

MenuArr=()

while read -r line; do 
    MenuNmbr=${line%% *}
    MenuName=${line#* }
    MenuArr+=($MenuNmbr "$MenuName")
done < ~/.grub-display-menu
rm ~/.grub-display-menu

LongVersion=$(grub-install --version)
ShortVersion=$(echo "${LongVersion:20}")
DefaultItem=0

while true ; do

    Choice=$(whiptail \
        --title "Use arrow, page, home & end keys. Tab toggle option" \
        --backtitle "Grub Version: $ShortVersion" \
        --ok-button "Display Grub Boot" \
        --cancel-button "Exit" \
        --default-item "$DefaultItem" \
        --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \
        "${MenuArr[@]}" \
       >/dev/tty)

    clear
    if [[ $Choice == "" ]]; then break ; fi
    DefaultItem=$Choice

    for (( i=0; i < ${#MenuArr[@]}; i=i+2 )) ; do
        if [[ "${MenuArr[i]}" == $Choice ]] ; then
            i=$i+1
            MenuEntry="menuentry '"${MenuArr[i]}"'"
            break
        fi
    done

    TheGameIsAfoot=false
    while read -r line ; do
        if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
        if [[ $TheGameIsAfoot == true ]]; then
            echo $line
            if [[ $line = *"}"* ]]; then break ; fi
        fi
    done < /boot/grub/grub.cfg

    read -p "Press <Enter> to continue"

done

exit 0

grub-display-lite.shbash脚本是基本相同,grub-display.sh除了没有错误消息,如果dialog没有安装。还有一些whiptail参数具有不同的名称。

grub-display-lite.sh 屏幕截图

彩色屏幕似乎比grub-display使用dialog软件包的屏幕更易于阅读:

grub-display-lite.sh

这是基于文本的图像,无需修改即可复制到剪贴板:

Grub Version: 2.02~beta2-36ubuntu3.15


        ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
         Menu Number       ----------- Menu Name ----------                        
                                                                                   
                      55 Ubuntu, with Linux 4.13.9-041309-generic                 
                      58 Ubuntu, with Linux 4.10.0-42-generic                     
                      61 Ubuntu, with Linux 4.10.0-40-generic                     
                      64 Ubuntu, with Linux 4.10.0-38-generic                     
                      67 Ubuntu, with Linux 4.10.0-37-generic                     
                      70 Ubuntu, with Linux 4.10.0-28-generic                     
                      73 Ubuntu, with Linux 4.9.77-040977-generic                 
                      76 Ubuntu, with Linux 4.9.76-040976-generic                 
                      79 Ubuntu, with Linux 4.4.0-104-generic                     
                      82 Ubuntu, with Linux 4.4.0-103-generic                     
                      85 Ubuntu, with Linux 4.4.0-101-generic                     
                      88 Ubuntu, with Linux 4.4.0-98-generic                      
                      91 Ubuntu, with Linux 3.16.53-031653-generic                
                      94 Windows Boot Manager (on /dev/nvme0n1p2)                 
                      95 Windows Boot Manager (on /dev/sda1)                      
                      96 System setup                                             
                                                                                   
                                                                                   
                           <Display Grub Boot>        <Exit>                       
                                                                                   
        └──────────────────────────────────────────────────────────────────────────┘ 

如上所述,删除(upstart)(recovery)菜单选项时,您可以将此处显示的grub菜单的大小减少66%。此处就是这种情况,但结果是细节线变窄了,标题也无法完美地对齐。您可以通过更改以下行来调整列标题:

    --menu "Menu Number       ----------- Menu Name ----------" 24 76 16 \

像这样:

    --menu "      Menu Number ----------- Menu Name ----------" 24 76 16 \

要查看当前信息,请使用cat /proc/cmdline。要查看grub下次更新grub菜单use时将使用的选项grep GRUB_CMDLINE_LINUX /etc/default/grub。这些第二组设置将由apt或update-grub运行时使用。要简单地less /boot/grub/grub.cfg或相似地查看所有选项。

@豹我添加了grub菜单项#94和#96(来自我的系统)以显示更多的实用性。另外要考虑的是它更容易使用的菜单比记忆catgrep对我们大多数人。
WinEunuuchs2Unix

+1。我同意使用文本模式菜单dialog会有所帮助。
sudodus

这个很棒的答案的一个脚注是ubuntu的某些风格不包含对话框lubuntu 16.04默认情况下不包含。
ianorlin '18

@ianorlin刚刚发布了一个更好的版本。
WinEunuuchs2Unix
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.