Aggiornato il 7 maggio 2018
Sviluppare lo script: script Bash per clonare Ubuntu su una nuova partizione per testare l'aggiornamento LTS 18.04 Ho scoperto che hai alcune opzioni di menu ridicolmente lunghe che causano il malignamento del menu:
4>8 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) (on /dev/nvme0n1p8)
Ciò è stato risolto oggi troncando le linee più lunghe di 68 caratteri.
Aggiornato il 5 aprile 2018
Questo aggiornamento introduce grub-menu.shuna versione molto superiore alla risposta precedente (ancora disponibile di seguito). Le nuove funzioni del menu di grub:
- Visualizza i numeri delle voci di menu di grub 2. vale a dire
0, 1, 1>0, 1>1... 2,3
- È possibile impostare la versione breve predefinita senza
(upstart)e le (recover mode)opzioni del sottomenu.
- Il parametro 1 può essere passato come
shorto longper sovrascrivere il valore predefinito.
- intestazioni di colonna formattate dinamicamente in base a
shorto longimpostazione.
Schermata a colori (versione corta)

Schermata di testo (versione lunga)
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 script bash
Versioni precedenti grub-display.she grub-display-lite.shrichiedevano molte opzioni di modifica nel codice. grub-menu.shha solo un'opzione per modificare:
# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false
Impostare il valore su trueo false.
Il formato predefinito può essere sostituito quando si chiama lo script usando:
grub-menu.sh short
o:
grub-menu.sh long
Il codice:
#!/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
Versioni precedenti (non raccomandato)
Di seguito è la risposta originale in cui i numeri delle voci di menu hanno seguito il formato grub 1.
grub-display.sh visualizza le opzioni e i parametri del menu di grub
Senza fare affidamento su applicazioni di terze parti è possibile utilizzare uno script bash per visualizzare il grubmenu e i parametri di avvio per una determinata opzione. I parametri di avvio sono più che semplici cat /proc/cmdlinevalori. Includono anche i driver caricati prima dell'avvio di Linux.
grub-display.sh script bash
Ecco l'elenco completo del programma che puoi copiare e incollare:
#!/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
Nota per gli utenti di Ubuntu Server
Questo script bash è stato progettato per Ubuntu Desktop. Per Ubuntu Server e altre distribuzioni Linux che non hanno un dialogpacchetto installato per impostazione predefinita, di grub-display-lite.shseguito è incluso un diverso script chiamato . Quella versione utilizza whiptailinvece di dialog.
Ridurre le dimensioni del menu del 66%
Per abbreviare l'elenco delle opzioni del menu di grub visualizzato, è possibile rimuovere le opzioni (upstart)e (recovery). Per fare questo commento queste righe:
# 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
Quindi applicare i commenti a queste righe:
# Version with upstart and recovery options displayed
#awk -F\' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg \
# > ~/.grub-display-menu
Screenshots
Ecco come appare quando viene richiamato dalla riga di comando. Purtroppo non sono stato in grado di copiare e incollare il menu e ho dovuto usare Print Screen:

Disattiva il supporto del mouse per copia e incolla
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 > │
└──────────────────────────────────────────────────────────────────────────┘
Quando il supporto del mouse predefinito è abilitato, non è possibile copiare lo schermo negli Appunti ma è necessario utilizzarlo Print Screenper un'istantanea dello schermo grafico. Per supportare il copia e incolla è necessario disabilitare il supporto del mouse cercando queste righe:
--default-item "$DefaultItem" \
--no-mouse \
--menu "Menu Number ----------- Menu Name ----------" 24 76 16 \
L'argomento --no-mouseè stato inserito di seguito --default-item. Ciò significa che perdi il supporto del mouse ma ottieni una migliore risoluzione e copia negli appunti evidenziando il testo e premendo Ctrl+ C.
Visualizza i parametri di avvio di grub
Utilizzare i tasti di navigazione per evidenziare un'opzione e premere Enterper visualizzare i parametri di avvio per essa:
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
Voce di menu 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
Voce del menu Grub # 96
menuentry 'System setup' $menuentry_id_option 'uefi-firmware' {
fwsetup
}
Press <Enter> to continue
grub-display-lite.sh per Ubuntu Server
Ubuntu Server e Lubuntu non hanno un dialogpacchetto installato di default come ha fatto Ubuntu Desktop. Una versione diversa è stata scritta per questi utenti in base al whiptailpacchetto che è incluso di default sulla maggior parte delle distribuzioni Linux.
Lo svantaggio di whiptailè meno funzioni ma in questo caso non vengono utilizzate. Un altro svantaggio sembra essere meno colori, ma ciò può rendere più facile la lettura per alcune persone. Ci sono vantaggi whiptailoltre a dialogquelli come copia negli appunti, supporto per la rotellina del mouse e probabilmente un'elaborazione più veloce.
grub-display-lite.sh script 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
Lo grub-display-lite.shscript bash è sostanzialmente lo stesso di grub-display.shtranne se non è presente alcun messaggio di errore se dialognon è installato. Inoltre alcuni whiptailargomenti hanno nomi diversi.
grub-display-lite.sh screenshot
Lo schermo a colori sembra essere più facile da leggere rispetto a quello grub-displayche utilizza il dialogpacchetto:

Ecco l'immagine basata su testo che non necessita di modifiche da copiare negli Appunti:
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> │
│ │
└──────────────────────────────────────────────────────────────────────────┘
Come accennato in precedenza, è possibile ridurre le dimensioni del menu di visualizzazione visualizzato qui del 66% durante la rimozione (upstart)e (recovery)le opzioni di menu. Questo è il caso qui, ma di conseguenza le linee di dettaglio si restringono e le intestazioni non si allineano perfettamente. Puoi modificare le intestazioni di colonna modificando questa riga:
--menu "Menu Number ----------- Menu Name ----------" 24 76 16 \
a qualcosa del genere:
--menu " Menu Number ----------- Menu Name ----------" 24 76 16 \
cat /proc/cmdline. Per visualizzare le opzioni che Grub utilizzerà la prossima volta che aggiorni il menu di Grub, utilizzagrep GRUB_CMDLINE_LINUX /etc/default/grub. Quella seconda serie di impostazioni verrà utilizzata da apt o ogni volta cheupdate-grubviene eseguita. Per vedere tutte le opzioni semplicementeless /boot/grub/grub.cfgo simili.