有沒有辦法檢索 GRUB 的「選單項目」標題清單?
我需要一個可以接受的格式的結果grub-set-default
。例如。
"Ubuntu"
"Memory test (memtest86+)"
etc.
答案1
使用awk
awk -F\' '/menuentry / {print $2}' /boot/grub/grub.cfg
為您提供所有選單項目的完整清單grub.cfg
。
答案2
彩色螢幕截圖(簡短版)
使用滑鼠滾輪、Home、End、PgUp、PgDn和↑鍵↓導航選單。
文字螢幕截圖(長版)
bash 腳本使用whiptail
而不是dialog
顯示選單。一個優點是您可以將終端圖像複製到剪貼簿,如下所示
文字並將其作為文字貼到該網站中。其他優點包括:
- 滑鼠滾輪支持
- 更快的性能
dialog
預設情況下,Ubuntu Server 或 Lubuntu 中未安裝。whiptail
預設包含在內。
這是文字螢幕截圖:
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> │
│ │
└──────────────────────────────────────────────────────────────────────────┘
反白顯示條目並按 Enter 鍵
使用導航鍵反白顯示選項,然後按下Enter可查看啟動時載入的預核心驅動程式grub
以及傳遞grub
給核心的啟動參數:
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-menu.sh
bash腳本
grub-menu.sh
只有一個選項可以調整:
# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false
將值設為true
(隱藏額外條目)或false
(列出所有條目)。
使用以下命令呼叫腳本時可以覆蓋預設格式:
grub-menu.sh short
或者:
grub-menu.sh long
代碼:
#!/bin/bash
# NAME: grub-menu.sh
# PATH: $HOME/bin
# DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
# DATE: Apr 5, 2018. Modified: July 27, 2019
# UPDT: Scroll bar was outside of dialog box. Move windo border line.
# $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
# Send output to secondary terminal such that previous history isn't cleared on exit
tput smcup
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 --clear \
--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 80 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
# Restore output to primary terminal
tput rmcup
exit 0
答案3
正是這個問題困擾了我一年多 - 所以我做了一個快速而骯髒的腳本來做到這一點。希望這對其他人有幫助嗎?
這會將名稱堆疊在推送/彈出佇列中,但不會堆疊選單索引,因此它可能會更好,但我已經盡力了。
gawk 'BEGIN {
l=0
menuindex= 0
stack[t=0] = 0
}
function push(x) { stack[t++] = x }
function pop() { if (t > 0) { return stack[--t] } else { return "" } }
{
if( $0 ~ /.*menu.*{.*/ )
{
push( $0 )
l++;
} else if( $0 ~ /.*{.*/ )
{
push( $0 )
} else if( $0 ~ /.*}.*/ )
{
X = pop()
if( X ~ /.*menu.*{.*/ )
{
l--;
match( X, /^[^'\'']*'\''([^'\'']*)'\''.*$/, arr )
if( l == 0 )
{
print menuindex ": " arr[1]
menuindex++
submenu=0
} else
{
print " " (menuindex-1) ">" submenu " " arr[1]
submenu++
}
}
}
}' /boot/grub/grub.cfg
正如 2019/8 的評論中所要求的那樣,我上面提到了進行「臨時啟動」。這個想法最初是我的腳本的開始,臨時啟動方法來自另一篇文章,它是這樣的
- 將 GRUB_DEFAULT 設定為 GRUB_DEFAULT=已儲存
# vi /etc/default/grub
- 更新 /boot 中的 grub 配置
# sudo update-grub
- 設定要載入的預設作業系統(每次重新啟動電腦時都會載入)
# sudo grub-set-default 0
- 當需要載入其他作業系統時(數字是作業系統的選單號,如 /boot/grub/grub.cfg 中所示,這將在下次重新啟動時僅加載其他作業系統一次 - 重新啟動將手動啟動):
# sudo grub-reboot 4
答案4
另一種處理子選單的 awk 單行程式碼:
awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\smenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg
這可能是錯誤的想法,因為 menuentry 關鍵字之前的 \s (任何空白字元)用於標識子選單條目。