Search the web with Google

Android install

Android installation weblog


Enter your email address:


Jeroen´s weblog

Ubuntu install

Ubuntu installation weblog


Monday, November 2, 2009

GRUB2 title tweaks

This site: Grub 2 Title Tweaks Thread is interesting. I quoted it here:

Grub 2 Title Tweaks

or the


I Don't Want It to Say "Microsoft Windows Vista Home Edition" Thread



Purpose
This thread offers tweaks to the Grub 2 10_linux and 30_os-prober files in /etc/grub.d. The purpose is to change the way Titles are displayed on the Grub 2 menu.

With the exception of the "But First" section, the tweaks in this thread are outside the normal options currently available in Grub 2's /etc/default/grub. As Grub 2 options are incorporated by the developers these scripts will be annotated or removed.

If you are looking for an introduction on how Grub 2 works, please visit Grub 2 (Ubuntu Community Help doc), Grub 2 Basics or Grub 2 Introduction elsewhere on the Ubuntu Forums. This thread also does not discuss custom configuration files such as /etc/grub.d/40_custom although these tweaks could be incorporated therein.

Feel free to post your tweaks but please limit this thread to posts concerning the Grub text you see in the menu. Also suggest improvements to the ones I posted - I'll edit them to keep them udpated. See the end of this post for some suggestions.

Hopefully as the capabilities of Grub 2 are expanded or become better understood many of these tweaks will become unnecessary. In the meantime those truly anal among us who can never leave things alone will have a place of refuge.

Background
Grub 2 displays, unlike Grub, are not controlled by a single file but rather by the settings in /etc/default/grub and a series of script files located in /etc/grub.d/ . These scripts determine, in part, how the menu options are transferred into /boot/grub/grub.cfg and the menu you see on boot.

See Grub 2 Basics for a general overview of Grub 2 and for links to other Grub 2 resources.

Before You Start
  1. Make copies of the system files you will be altering.
    Note: If you leave the .bak files executable and in the /etc/grub.d folder they will be run by update-grub.
    Code:
    sudo cp /etc/grub.d/10_linux /etc/grub.d/10_linux.bak
    sudo cp /etc/grub.d/30_os-prober /etc/grub.d/30_os-prober.bak
    sudo chmod -x /etc/grub.d/*.bak
  2. Make sure you are working in the correct file, and in the applicable section of that file.
  3. Run "sudo update-grub" to incorporate the changes into Grub 2's menu.
  4. When defining variables, the ` symbol is not a straight quote symbol but a 'grave'. It is often found near the top left of the keyboard.

But First: Popular "Built-In" Tweaks for the / System Partition
  • Turn Off "MEMTEST86+"
    There is no setting to turn off the "memtest86+" option in /etc/default/grub but you can eliminate these entries by turning off the script that searches for them. The following command removes the "executable" bit from the /etc/grud.d/20_memtest86+ file. To restore the setting, change the "-" to a "+":
    Code:
    sudo chmod -x /etc/grub.d/20_memtest86+
    • This effects only the system partition. If you have multiple partitions with Linux installations there is a tweak later that will turn off memtest86+ on them as well.
    • In the Tweaks section there is an explanation of how to boot into the "Recovery Mode" even without a menu option.
  • Turn Off Recovery Mode
    If you don't want to see the Recovery Mode Options for any item, change the setting in /etc/default/grub. While it may be a good idea to have the "Recovery" mode option displayed, if you don't want it, here is how to get rid of it. You can do this by opening the file as 'root' with any text editor and removing the # symbol from the beginning of the line, or by running the following command.
    Code:
    sudo sed s/'#GRUB_DISABLE_LINUX_RECOVERY="true"'/'GRUB_DISABLE_LINUX_RECOVERY="true"'/g -i /etc/default/grub

Grub 2 Files
The basic configuration settings are made in /etc/default/grub. This thread will not deal with those settings.
The two primary files altered by tweaks in this thread are:
  • /etc/grub.d/10_linux
    • This file contains instructions and scripts which deal with linux kernels on the default system partition.
  • /etc/grub.d/30_os-prober
    • This file contains instructions and scripts to deal with kernels and other operating systems found on other partitions.
    • The file contains four sections. Changes must be made in the correct section, as changes in one section will not effect the scripts in the other sections.
      • The four sections are for Windows, other Linux installations, OSX, and Hurd.


1. /etc/grub.d/10_linux - Changing Ubuntu/Linux Titles (on default partition)

Code:
gksu gedit /etc/grub.d/10_linux
  • Defined Variable Examples: ${OS} Ubuntu, Kubuntu, etc.; ${version} 2.6.31-11, 2.6.31-11-generic, etc.
    Added Variable Example: ${codename} karmic
  • Add new variable codename. This list of variables begins at approximately line 87.
    • Code:
      while [ "x$list" != "x" ] ; do
      linux=`version_find_latest $list`
      echo "Found linux image: $linux" >&2
      basename=`basename $linux`
      dirname=`dirname $linux`
      rel_dirname=`make_system_path_relative_to_its_root $dirname`
      version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
      alt_version=`echo $version | sed -e "s,\.old$,,g"`
      linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
      # User-added variable
      codename="`lsb_release -cs`"
      Original Menu Entry: Ubuntu, Linux 2.6.31-11
      This should be located at approximately line 113.
      Code:
      linux_entry "${OS}, Linux ${version}" \
      "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_EXTRA} ${GRUB_CMDLINE_LINUX_DEFAULT}" \
      quiet
      New Format: Ubuntu 2.6.31-11
      Change the original line above to look like the following by removing ", Linux":
      Code:
      linux_entry "${OS} ${version}" \
      "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_EXTRA} ${GRUB_CMDLINE_LINUX_DEFAULT}" \
      quiet
      New Format: Karmic 2.6.31-11
      Ensure the variable ${codename} has been added into the file's variable list.
      The codename returned in this script will always be the current codename of your present Linux OS (i.e. karmic, jaunty, etc.).
      Change the original line above to look like the following:
      Code:
      linux_entry "${codename} ${version}" \
      "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_EXTRA} ${GRUB_CMDLINE_LINUX_DEFAULT}" \
      quiet
  • Run "sudo update-grub" to update the Grub 2 Menu.

2. /etc/grub.d/30_os-prober - Changing Windows or Other Detected OS Titles (including Linux)
  • This file is divided into sections for various types of operating systems. The first section is for Windows (OS), the second Linux (linux), the third OSX (macosx), and finally Hurd (hurd).

    Code:
    gksu gedit /etc/grub.d/30_os-prober
    A. Changing Windows Entries
    • Changes made via this method are best done to OS or kernels which the user does not expect to change. If the system detects a title change the entry in #3 may no longer be correct.

      Original Menu Entry: Microsoft Windows XP Home Edition (on /dev/sda1)

      1. Run this command to get the current Grub 2 menu entries:
      Code:
      sudo cat /boot/grub/grub.cfg | grep "menuentry" | cut -d '"' -f 2
      New Format: Whatever (on /dev/sda7)
      2. This section appears around line 83 of the file. Replace the section in red with the content in #4.
      Code:
      for OS in ${OSPROBED} ; do
      DEVICE="`echo ${OS} | cut -d ':' -f 1`"
      LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
      LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
      BOOT="`echo ${OS} | cut -d ':' -f 4`"

      if [ -z "${LONGNAME}" ] ; then
      LONGNAME="${LABEL}"
      fi
      3. Copy the exact title you wish to change (Example: Microsoft Windows XP Home Edition ) and place it between the quotes in the first line below.
      4. Enter the desired title between the quotes in the second line below - in this example, "Windows XP" would replace "Enter Desired New Title Here".
      Code:
      # Original
      # if [ -z "${LONGNAME}" ] ; then
      # LONGNAME="${LABEL}"
      # fi

      if [ "${LONGNAME}" = "Enter Exact Title You Just Copied" ] ; then
      LONGNAME="Enter Desired New Title Here"
      elif [ -z "${LONGNAME}" ] ; then
      LONGNAME="${LABEL}"
      fi
      5. Multiple entries can be made in the same section:
      Code:
        if [ "${LONGNAME}" = "Enter Exact Title You Just Copied" ] ; then
      LONGNAME="Enter Desired New Title Here"
      elif [ "${LONGNAME}" = "Enter Second Title You Copied" ] ; then
      LONGNAME="Enter Desired Second Title Here"
      elif [ -z "${LONGNAME}" ] ; then
      LONGNAME="${LABEL}"
      fi

    B. Changing Other Linux Installations
    1. Included in the first codeblock below are all the user-defined variables used in this section of the guide. They apply to the "linux" section of /etc/grub.d/30_os-prober file.
      It is only necessary to insert the ones you wish to use. For simplicity and space-saving, they have all been included in the first codeblock below.
    2. Defined Variables Examples: ${LONGNAME} Microsoft Windows XP Home Edition, Ubuntu 9.04 (9.04); ${DEVICE} sda1, sdb6, etc; ${LLABEL} Ubuntu, linux 2.6.31-11
    3. Find the linux section of the file at approximately line 120 (before other additions).
    4. Add the variables you wish to use from the examples below. Following the added variables are examples of possible returned value:
      ${newtitle} Ubuntu 9.04 ;${shortkernel} 2.6.28-15 ; ${nomemtest} memtest86+ ; ${nosingle} single ; shortdevice (sda5) ; ${hidekernel} 2.6.26
      Code:
          ;;
      linux)
      LINUXPROBED="`linux-boot-prober ${DEVICE} 2> /dev/null | tr ' ' '^' | paste -s -d ' '`"

      for LINUX in ${LINUXPROBED} ; do
      LROOT="`echo ${LINUX} | cut -d ':' -f 1`"
      LBOOT="`echo ${LINUX} | cut -d ':' -f 2`"
      LLABEL="`echo ${LINUX} | cut -d ':' -f 3 | tr '^' ' '`"
      LKERNEL="`echo ${LINUX} | cut -d ':' -f 4`"
      LINITRD="`echo ${LINUX} | cut -d ':' -f 5`"
      LPARAMS="`echo ${LINUX} | cut -d ':' -f 6- | tr '^' ' '`"
      # User-added variables
      newtitle=`echo ${LONGNAME} | cut -d " " -f 1-2`
      shortkernel="`echo ${LKERNEL} | cut -d "-" -f 2-4`"
      shortdevice="`echo ${DEVICE} | cut -d "/" -f 3`"
      nomemtest="`echo ${LLABEL} | cut -d " " -f 1`"
      nosingle="`echo ${LPARAMS} | sed 's/^.* //'`"
      hidekernel="`echo ${LKERNEL} | cut -d'-' -f2`"
      Original Menu Entry: Linux, Ubuntu 9.04 2.6.28-15 (on /dev/sda7)
      Modify the entry in red for each section you want to change (approximately line 135 in original file):
      Code:
              cat << EOF
      menuentry "${LLABEL} (on ${DEVICE})" {
      EOF
      New Format: Ubuntu 9.04 2.6.28-15 (on /dev/sda7)
      Code:
              cat << EOF
      menuentry "${newtitle} ${shortkernel} (on ${DEVICE})" {
      EOF
      New Format: Ubuntu 9.04 2.6.28-15
      Code:
              cat << EOF
      menuentry "${newtitle} ${shortkernel}" {
      EOF

    C. Eliminating or Changing "(on /dev/sdX)"
    • There are specific sections for Windows (OS), Linux (linux), and OSX (macosx). Users must find the section(s) they wish to change or do a universal search/replace.
    • Defined Variables: ${LONGNAME} OS title; ${device} Partition location (e.g. sda1, sdb6, etc)
    • Modify the entry in red for each section you want to change.

      Original Menu Entry: Windows XP Home (on /dev/sda7)
      Approximately line 100.
      Code:
      menuentry "${LONGNAME} (on ${DEVICE})" {
      New Format: Windows XP Home (sda1)
      Code:
      menuentry "${LONGNAME}" (${shortdevice}) {
      New Format: Windows XP Home
      Code:
      menuentry "${LONGNAME}" {
      Original Menu Entry: Linux, Ubuntu 9.04 2.6.28-15 (on /dev/sda7)
      Approximately line 136.
      Code:
      menuentry "${LLABEL} (on ${DEVICE})" {
      New Format: Linux, Ubuntu 9.04 2.6.28-15 (sda7)
      Code:
      menuentry "${LLABEL}" (${shortdevice}) {
      New Format: Linux, Ubuntu 9.04 2.6.28-15
      Code:
      menuentry "${LLABEL}" {

    D. Removing MEMTEST+86 and Single User Entries (from other partitions)
    • Grub 2 provides the ability to hide memtest86+ and recovery mode (single user) entries for the default partition without modifying the 10_linux or 30_os-prober files (see later in this section) . I have not found a similar ability to inhibit the display of these entries found by 30_os-prober on other partitions.
    • Refer to the previous section for information on inserting variables. The two variables associated with hiding the memtest86+ and recovery mode (single-user) are ${nomemtest} and ${nosingle}.
    • The conditional statement below tells Grub to skip an entry if it is either a memtest86+ or recovery mode option. While a bit inelegant, when inserted in 30_os-prober it will effectively prevent the applicable options from appearing in the Grub 2 menu.
    • To remove memtest86+ and recovery mode/single user entries for other partitions found by 30_os-prober:
      • Insert the code in green just after the linux variable definitions and before the first "menuentry" in the linux section (approximately line 130 before any additions).
        Code:
                LINITRD="`echo ${LINUX} | cut -d ':' -f 5`"
        LPARAMS="`echo ${LINUX} | cut -d ':' -f 6- | tr '^' ' '`"
        # User-added variables
        newtitle=`echo ${LONGNAME} | cut -d " " -f 1-2`
        shortkernel="`echo ${LKERNEL} | cut -d "-" -f 2-4`"
        shortdevice="`echo ${DEVICE} | cut -d "/" -f 3`"
        nomemtest="`echo ${LLABEL} | cut -d " " -f 1`"
        nosingle="`echo ${LPARAMS} | sed 's/^.* //'`"
        hidekernel="`echo ${LKERNEL} | cut -d'-' -f2`"



        if [ -z "${LLABEL}" ] ; then
        LLABEL="${LONGNAME}"
        fi


        if [ ${nomemtest} = "Memory" ] || [ ${nosingle} = "single" ]; then
        break
        fi


        cat << EOF
        menuentry "${LLABEL} (on ${DEVICE}" \
        EOF
      • The entry above hides both recovery mode and memtest86+ entries in one line. If desired, they can be divided into 2 separate sections or used singly by removing the applicable conditional statement and the || characters.
    • To inhibit the display of recovery options on the system partition, uncomment or add this line to /etc/default/grub:
      Quote:
      GRUB_DISABLE_LINUX_RECOVERY=true
    • To inhibit the display of memtest86+ entries from the system partition, make the /etc/grub.d/memtest86+ file non-executable by running this command:
      Code:
      sudo chmod -x /etc/grub.d/20_memtest86+
    • Run "sudo update-grub" to incorporate these changes into the Grub menu.


    E. Removing a Specific Kernel (from other partitions)*
    • This section discusses how to hide a specific kernel while allowing os-prober to search other partitions.
      There are two components necessary for this option:
      • The Variable: hidekernel="`echo ${LKERNEL} | cut -d'-' -f2`"
        • Entered into the linux section of /etc/grub.d/30_os-prober. See previous section for location.
      • The Conditional Statement: if [ ${hidekernel} = "X.X.XX" ]; then
        • X.X.XX or X.X.XX-XX example: 2.6.28 or 2.6.28-12
    • To Hide All Instances of a Kernel (Example: 2.6.26):
      • hidekernel="`echo ${LKERNEL} | cut -d'-' -f2`"
      • if [ ${hidekernel} = "2.6.26" ]; then
    • To Hide a Specific Kernel (Example to hide 2.6.26-11):
      • hidekernel="`echo ${LKERNEL} | cut -d'-' -f2-3`"
      • if [ ${hidekernel} = "2.6.26-11" ]; then

      1. Add the variable to the linux variable list (approximately line 130).
      Code:
              hidekernel="`echo ${LKERNEL} | cut -d'-' -f2`"
      2. Determine the kernel you want to hide (Example: 2.6.26 ) and include it in linux section of /etc/grub.d/30_os-prober, at approximately line 140).
      Code:
              if [ -z "${LLABEL}" ] ; then
      LLABEL="${LONGNAME}"
      fi


      if [ ${hidekernel} = "2.6.26" ]; then
      break
      fi


      cat << EOF
      menuentry "${LLABEL} (on ${DEVICE}" \
      EOF
    * Note: os-prober can be disabled in various ways to prevent Grub 2 from searching any secondary partitions for bootable operating systems. See Grub 2 Basics for more information.

    F. Troubleshooting
    If the tweaks in this section do not work there are several things to check.
    • Are you working with the correct file? Make sure you are making the changes to the file which deals with the item you are trying to hide. Check the grub.cfg file to see which section the entry is appearing in (10_, 30_, etc).
    • Is the variable producing the correct result? The easiest way to check is to either echo the variable results for viewing while update-grub is running or insert the variable in the grub.cfg file for review.
      • Example: The memtest86+ option keeps appearing in the menu, even though you have inserted "nomemtest="`echo ${LLABEL} | cut -d " " -f 1`" into 30_os-prober.
        • Check that the undesired 'memtest86+' menu item is in the 30_os-prober section of grub.cfg
        • Find out what value 'LLABEL' and 'nomemtest' are returning.
        • Alter the following line:
          Code:
           cat << EOF
          menuentry "${LLABEL} (on ${DEVICE})" {
          to
          Code:
           cat << EOF
          nomemtest=${nomemtest} LLABEL=${LLABEL}
          menuentry "${LLABEL} (on ${DEVICE}" \
          A sample entry would now look like this, showing you the values:
          Code:
          nomemtest=memtest86+ LLABEL=Debian GNU/Linux, kernel memtest86+
          menuentry "Debian GNU/Linux, kernel memtest86+" (sda8) {
          Now for each entry, you will have a line directly above displaying the values of nomemtest and LLABEL just before that menu entry was created. With that knowledge, you should be able to tweak the variable to ensure it returns the correct value for the current situation.

3. CUSTOM MENU TWEAKS!
Naming your /etc/grub.d custom folder 06_custom will place the custom menu entries at the top of GRUB 2's menu. Menu items placed in 40_custom will appear the the bottom.

If you use a numbered filename less than 30 you may want to check the "DEFAULT=" line in /etc/default/grub after running sudo update-grub to ensure the correct menuentry item is identified.

  1. This custom entry is based on ranch hand's post #26. It provides an entry for the latest kernel on the system and will remain current even after a kernel update. UUIDs may be used to identify root if desired.

    Code:
    menuentry "Latest Kernel" {
    set root=(hdX,Y)
    linux /vmlinuz root=/dev/sdXY ro quiet splash
    initrd /initrd.img
    }
4. POST YOUR TWEAKS!

Thread Recommendations
  • Tweaks that only alter the text on the Grub 2 Menu.
  • If possible, on the first line post the existing title and how it will change.
  • Original Menu Entry: Microsoft Windows XP Home Edition on sda1 >> New: XP

    To achieve this on the first line, copy/paste the code from the last line of this post and then alter it.


    [size="3"][b]Original Menu Entry: [color="DarkRed"]Microsoft Windows XP Home Edition on sda1[/color] >> New: [color="DarkGreen"]XP[/color][/b][/size]
    [/size]

8 comments:

donach said...

I'm trying to edit my Windows entries and I've followed the instructions really carefully. I've edited the 30_os-prober script in /etc/grub.d saved it and then done sudo update-grub in terminal. It builds the a new grub.cfg file but it's exactly the same as the old one. Its not that big a deal as I'm not losing any functionality but I'd like to slag Bill Gates off every time I use his bloated ubiquitous software. (Thankfully less & less often these days.)

jayan said...

marvelous work
Thank you

nthngm said...

[code]This was very helpful. [color="DarkGreen"]Thanks![/color][/code]

nthngm said...

[size="3"][b]Original Menu Entry: [color="DarkRed"]Microsoft Windows Vista (loader) (on /dev/sda1)[/color] >> New: [color="DarkGreen"]Microsoft Windows Vista (loader) (/dev/sda1 "Recovery")[/color][/b][/size]
[/size]

Around line 139, after BOOT variable, add the variable IDLABEL:
[code]IDLABEL="`blkid ${DEVICE} | cut -f2 -d' ' | cut -f2 -d'"'`"[/code]


Alter the following two lines:
[code]echo "Found ${LONGNAME} on ${DEVICE}" >&2
...
menuentry "${LONGNAME} ([color="DarkRed"]on[/color] ${DEVICE})" {[/code]

To read:
[code]echo "Found ${LONGNAME} on ${DEVICE} [color="DarkBlue"]\"${IDLABEL}\"[/color]" >&2
...
menuentry "${LONGNAME} (${DEVICE} [color="DarkBlue"]\"${IDLABEL}\"[/color])" {[/code]

This add the partion label to the entry, which is helpful for identifying what's on it. If the partition has no label, it winds up adding the UUID, which is not exactly helpful, but doesn't hurt.

Hopefully the formatting is legible. Thanks for the great blog post.

purp said...

Yeah, it's sad that grub2 tends to give you about as much freedom in configuration as Windows does... I heard about people who preferred to maintain grub.cfg by hand, because all those tweaks are so tiring.

Anyway, I have another one. I personally sometimes need kernel parameters *only* with the recovery mode. The /etc/default/grub doesn't offer this, it only has GRUB_CMDLINE_LINUX_DEFAULT which goes to standard entries and GRUB_CMDLINE_LINUX which goes to *all* entries. So I added a variable GRUB_CMDLINE_SINGLE there, filled it with the required parameter (rootdelay=10 in my case). Looks like this:
GRUB_CMDLINE_SINGLE="rootdelay=10"

I also had to adapt /etc/grub.d/10_linux: There the line
"single ${GRUB_CMDLINE_LINUX}"
has to be changed to
"single ${GRUB_CMDLINE_SINGLE} ${GRUB_CMDLINE_LINUX}"
(That's still one line, mind.)

But that alone doesn't cut it, you also must adapt the part where variables are exported in /usr/sbin/grub-mkconfig (which is a shell script). There:

GRUB_CMDLINE_LINUX \
GRUB_CMDLINE_LINUX_DEFAULT \

becomes

GRUB_CMDLINE_LINUX \
GRUB_CMDLINE_SINGLE \
GRUB_CMDLINE_LINUX_DEFAULT \

Now an update-grub2 and you have a kernel option for recovery boot only.

Leonardo Lazarte said...

Hi, Jeroen,

Thanks for your excellent post. You really understand what the user expects from Grub.

I wander if you could contact Grub developers to offer them the tweaks you describe to be incorporated in the standard distribution.

Regards,
Leonardo

Zarko Jevtic said...

@donach and others there in Ubuntu 12.04 for changing title on Windows partition for "Enter Exact Title You Just Copied" you need to put title without "(on /dev/hdax)" If you get "Windows 7 (loader) (on /dev/sda1)" you need to put only "Windows 7 (loader)"

jpeg725 said...

To change the title of an ubuntu installation, edit /etc/lsb-release and change the DISTRIB_DESCRIPTION.

Post a Comment