Linux Format

Grub Custom boot menus.........

Bend your boot menu to your will by customisin­g its entries and appearance.

- Neil Bothwick has a computer in every room, but won’t disclose the location of his central server for security reasons.

Bootloader­s, such as Grub, are software packages that we give little thought to until things go wrong. You turn on your computer, the boot menu (possibly) appears and then your operating system loads. However, there is plenty you can do with the boot menu to tune the way your computer boots and the options available to you.

There are two major versions of Grub still in use. The current version is Grub2 while the original is often referred to as GrubLegacy, because it never reached the version 1.0 milestone; the latest you will find is 0.97. GrubLegacy used a configurat­ion file at /boot/grub/menu.lst that you were required to edit to add or remove options. That was fine for those of us happy to hack at text configurat­ion files, but not so useful for generating boot menus automatica­lly, so Grub2 uses a different system. It still has a configurat­ion file in /boot/grub, now called grub.cfg, but this file can be created automatica­lly – and usually is – based on what is installed on your computer. We will look at how you can influence this automatic configurat­ion process to tweak the boot menu to suit your needs, whether that be in terms of adding options, speeding things up or adding a more attractive menu for less geeky users.

How the menu is built

The main configurat­ion file lives in /boot/grub, but this is usually generated automatica­lly by grub-mkconfig (Ubuntu users have update-grub, which is a script that calls grubmkconf­ig). When you run grub-mkconfig it looks in two places to determine what it will do: /etc/default/grub and /etc/grub.d. The former is a file containing some environmen­t variables that Grub uses, while /etc/grub.d contains a number of shell scripts that are run to generate the configurat­ion file. The most important scripts are 00_ header and 10_linux. The first sets up the Grub configurat­ion, creating the first part of grub.cfg with global settings. Then 10_linux scans your hard drive for Linux systems and creates menu entries for each kernel in each installed distro. If you dual boot with Windows, 30_os-prober then adds menu entries for non-Linux operating systems. These scripts are provided by Grub and there's no need for you to change them, but you can change the way in which they operate by setting or altering variables in /etc/default/grub.

Changing the defaults

The first item you may wish to change is GRUB_TIMEOUT, which sets how many seconds the menu is displayed for before booting the default option. If you almost always take the default option, try reducing this to 2 or 3 to speed up the process. Setting it to 0 makes Grub boot the default option immediatel­y, but that means the options for recovery or other kernels will not be available to you. Setting it to -1 disables the timeout altogether. GRUB_HIDDEN_TIMEOUT performs a similar function when the menu is hidden, as some distros do. This sets how long the boot will pause, waiting for you to press Esc to show the menu. Comment this out if you always want to see the menu. If you don’t use Windows, you can save a little time when generating the menu by setting: GRUB_DISABLE_OS_PROBER=true

Each menu entry will be passed the kernel location, the root filesystem and the location of a matching initrd if one is

found. You can add extra options to the kernel command line by adding them to GRUB_CMDLINE_LINUX or GRUB_

CMDLINE_LINUX_DEFAULT. The first affects all menu entries while the second is not applied to recovery options. Some people dislike the use of UUIDs in /etc/fstab and boot menus, as they are hardly human-readable. You can turn them off with:

GRUB_DISABLE_LINUX_UUID=true

There are other options available here, not all documented in the file's comments. Read the configurat­ion section of the

Grub info pages for a full list. Altering settings in /etc/default/grub changes the appearance or behaviour of the standard entries in the menu, but what if you want to add your own? The simplest way to add an extra entry is by editing /etc/grub.d/40_custom. This is a script, just like the others used to build the menu, but one that currently adds nothing. Add the menu entries you want to this file to have them added to the end of the menu; do not remove the existing content. For example, you could keep a copy of the Rescatux ISO file in /boot and add a menu entry for it by putting this in 40_custom: menuentry "Rescatux 0.32 - 64 bit" { isofile=rescatux-0.32b2.iso loopback loop $isofile linux (loop)/live/vmlinuz1 findiso=$isofile boot=live config quiet splash

initrd (loop)/live/initrd1.img

}

Since the ISO file is referenced twice here, we use a variable to make updating easier. Now when you run grubmkconf­ig, you will have an entry for Rescatux.

The scripts in grub.d are executed in order, so if you want your custom entries added before the Windows options from 30_os-prober, simply rename 40_custom to 15_mystuff. There's also no limit on the number of scripts you can add: just make sure you include the lines from the top of 40_ custom and make your script executable. You can run each script in /etc/grub.d in a terminal to see exactly what it adds to the menu.

Writing your own menu entries by hand adds the possibilit­y to making a syntax error, so always check that the menu file is correct before booting it. If you don't and you have a problem booting from it, you will have to press E at the Grub menu to make changes. Fortunatel­y for us clumsy typists, Grub includes a tool to check your menu: grub2-script-check /boot/grub/grub.cfg

Dropping a menu entry or two into 40_custom is easy, but you need to remember to edit it whenever you update whatever it's running. As the scripts in /etc/grub.d are simply shell scripts that spit out content for grub.cfg, it is possible to make this more sophistica­ted. For example: for ISO in /boot/rescatux*.iso; do

echo menuentry "\"Rescatux 64 bit from $(basename $ISO)\" {" echo -e "\tisofile=$ISO" echo -e "\tloopback loop \$isofile" echo -e "\tlinux (loop)/live/vmlinuz1 findiso=\$isofile boot=live config quiet splash"

echo -e "\tinitrd (loop)/live/initrd1.img"

echo "}" done

Save this in /etc/grub.d, make it executable and run it. It should give the same menu as the static version, but when you add or replace the ISO with a newer version, it will adapt the menu accordingl­y. Of course, you could completely replace the existing scripts with your own if you want a truly custom setup. Don’t be tempted to edit the existing scripts, as your work would be overwritte­n by a package update. Instead, clear the executable bit on the scripts you don't want to run and give yours a unique name.

Using your new menu

Changes in /etc/default/grub and /etc/grub.d are not applied to the Grub menu until you run grub-mkconfig, which uses everything we have looked at to generate the configurat­ion file. It's best to test your menu syntax first with: grub-mkconfig | grub-script-check

If this produces no errors, it is safe to write the configurat­ion to the actual menu file: grub2-mkconfig -o /boot/grub/grub.cfg

This is the normal location for the menu file with BIOS systems. With UEFI you may find that it’s elsewhere, in which case, locate will help you find it.

When you boot your computer, you should now see your custom menu. Grub2 certainly introduced some new ways of working, and a lot more flexibilit­y. Because of this, it’s possible to make your computer truly unique right from the moment you turn it on.

 ??  ??
 ??  ?? All of the settings you need, and then some, are documented in Grub’s comprehens­ive info pages.
All of the settings you need, and then some, are documented in Grub’s comprehens­ive info pages.
 ??  ?? You can completely change the appearance of the boot menu with a theme. You may have seen this one before.
You can completely change the appearance of the boot menu with a theme. You may have seen this one before.

Newspapers in English

Newspapers from Australia