Welcome Buddy!

Get Started
Linux

Grub: Custom Boot Menus, How to Change Default Boot Menu As You Wish

cnwintech grub create your own 1 2

Bootloaders, 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 Grub 2 while the original is often referred to as Grub Legacy, because it never reached the version 1.0 milestone; the latest you will find is 0.97. Grub Legacy used a configuration 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 configuration files, but not so useful for generating boot menus automatically, so Grub 2 uses a different system. It still has a configuration file in /boot/grub, now called grub.cfg, but this file can be created automatically – and usually is – based on what is installed on your computer. We will look at how you can influence this automatic configuration 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 configuration file lives in /boot/grub, but this is usually generated automatically by grub-mkconfig (Ubuntu users have update-grub, which is a script that calls grubmkconfig). 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 environment variables that Grub uses, while /etc/grub.d contains a number of shell scripts that are run to generate the configuration file. The most important scripts are 00_header and 10_linux. The first sets up the Grub configuration, 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 immediately, 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

1demo banner 468x60

There are other options available here, not all documented in the file’s comments. Read the configuration section of the Grub info pages for a full list.

Adding themes

We’ve only looked at altering the content of the boot menu, but Grub also lets you change its appearance. You can set a graphical background by setting GRUB_BACKGROUND to the full path to a PNG or JPEG file, or you can theme everything on the display.

Themes are contained in directories in /boot/grub/themes and you select one by setting GRUB_THEME to the name of a theme directory (not the full path) in /etc/default/grub. The default theme is Starfield, although this is packaged separately in some distros so you may need to install it. Themes are installed to /usr/share/grub/themes. Copy any you want to use to /boot/grub/themes, set GRUB_THEME accordingly and reboot. If you do want to experiment, start with an existing one and modify it. The Grub info page documents the components.

Custom menu entries

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 grubmkconfig, 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 possibility 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. Fortunately 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 sophisticated. 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 accordingly. 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 overwritten by a package update. Instead, clear the executable bit on the scripts you don’t want to run and give yours a unique name.

Grub or Grub 2? There is still some inconsistency between distros regarding the names of commands and where the files are stored. It was common to add a 2 to the names of Grub 2 commands, so that both versions of Grub could be installed. For example, grub-mkconfig became grub2-mkconfig. Some distros still do this so check in /usr/bin to see which naming convention is used. Similarly, grub.cfg could be in /boot/grub or /boot/grub2.

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 configuration 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 configuration to the actual menu file:

grub2-mkconfig -o /boot/grub/grub.cfg

cnwintech grub create your own 2 2

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. Grub 2 certainly introduced some new ways of working, and a lot more flexibility. Because of this, it’s possible to make your computer truly unique right from the moment you turn it on.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Close
Subscribe Now
Advertisement
Back to top button
Close

Adblock Detected!

If you enjoy our content, please support our site by disabling your ad blocker or whitelisting us. We use ads to keep our content happy and free.