Tuesday, May 18, 2010

Customizing grub, Part 3

Here I am again with grub 2. In Part 2, I was cleaning up the boot menu that chooses which OS or kernel you want to use. But, that time, I only showed how to modify entries for OSes besides the one you are currently booted into. (Which is Linux, right? Right.)

This time, I'll show you how to modify /etc/grub.d/10_linux to clean up the menu entries for the OS that you will be running update-grub from.


Opening that file, it looks like the default behavior is to rely on variables provided by grub itself to generate the menu entry. Unfortunately, those strings are ugly. In case you don't remember from Part 2, the format I used for Linux entries was:

Ubuntu 9.10 (2.6.31-21-generic)
Ubuntu 9.10 (2.6.31-21-generic, recovery mode)

We can't use os-prober or linux-boot-prober to get the OS string, because those only work on disks that you haven't booted from. Instead, we will get the info we need from /etc/lsb-release, which looks like this:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04 LTS"

That last line has what we want, so we'll put this line into 10_linux to extract the info we want and store it to a variable:

args="$4"
LONGNAME="`grep DESCRIPTION /etc/lsb-release | cut -d '"' -f 2`"

The script already has an if-statement that detects whether this is a recovery image we are dealing with, so we'll just change that if-statement to handle recovery systems the way we want:

if ${recovery} ; then
  title="${LONGNAME} (${version}, recovery mode)"
else
  title="${LONGNAME} (${version})"
fi

And that should do it! All the menu entries are now nicely formatted.

If you want to go further, you can use the shell's printf to organize the system info into columns. How about adding a column showing which disk and partition each OS is on? Now you can do it!

Happy hacking!

No comments:

Post a Comment