Create a Custom Grub2 Screen that is maintenance free

Introduction

This guide will work with any version of Ubuntu and other distros of Linux. It is great for dual booting with Windows as well as multi-booting. You can have a background picture, colored fonts of your choosing, a timeout before the default is selected and you choose which Operating System will be the default. No further changes will be necessary even when new kernels are installed. The only time any change will be needed is if you want to change the background picture, the font colors, the default Operating System or if you delete or add an Operating System. The only other time you might have to make a change is if one of the grub files gets updated, but that occurs rarely. When you make a change to any grub file it is imperative that you enter sudo update-grub or else the changes will not take effect.

Thanks to Ranch Hand and Drs305 for their help and sharing their valuable knowledge about grub and other things Linux.


Labeling the partitions

Labeling the partitions makes them much easier to identify and use.
Example: sudo tune2fs -L {label} {devicename}

You only need quotes if you have spaces in the name but, you will not be able to use CLI to copy files to and from that partition with a space.
You do not need to label anything other than ext4 file systems.

sudo tune2fs -L Arch-Linux /dev/sda2
sudo tune2fs -L "Xenial Xerus" /dev/sda4
sudo tune2fs -L Zesty /dev/sda6
sudo tune2fs -L Artful /dev/sda7

Possible Grub font colors

You can use black/black and it will produce a black color with a transparent background.
However you only want to use this on a very light picture or it will appear invisible.

Grub2-colors-possibile.png

For All Ubuntu versions.

You can use gksudo <text editor> <file-name> , sudo nano <file-name> ; or whichever editor you feel most comfortable with.
In these examples I use gksudo gedit <file-name> because it was most convenient at the time.

Setting up a Grub background picture

You need to move 1 picture that is the same size as your screen resolution to /boot/grub/ e.g. sudo cp xxxx.png /boot/grub/xxxx.png.
The manual says accepted picture types are *.jpg, *.jpeg, *.png and *.tga pictures.
However, I suggest you use png pictures because they always seem to work.
If you change the picture be sure and remove the previous one because it looks for the first one it finds.
To remove the picture you would enter sudo rm /boot/grub/xxxx.png.

It is because of the existence of the picture that grub uses the font colors in /etc/grub.d/05_debian_theme.

Editing /etc/grub.d/05_debian_theme

You can have 3 font colors: a menu color, menu highlight color and a normal color. The normal color will appear above and below the box. The menu color will appear as the box and the regular line color, while the menu highlight line will appear where the cursor is as the selected line.
Enter gksudo gedit /etc/grub.d/05_debian_theme
For Trusty Tahr 14.04 using Grub version 2.00:
At line 118 where you see this line:
echo "if background_image make_system_path_relative_to_its_root "${1}"; then"
add these three lines below with your color choices after line 118 and save the file:

echo " set color_normal=cyan/black"
echo " set menu_color_normal=yellow/black"
echo " set menu_color_highlight=red/black"

For every version after Trusty Tahr 14.04:
At line 122 where you see this line:
echo "if background_image make_system_path_relative_to_its_root "${1}"; then"
add these three lines below with your color choices after line 122 and save the file:

echo " set color_normal=cyan/black"
echo " set menu_color_normal=yellow/black"
echo " set menu_color_highlight=red/black"

Back to top

Making the custom Grub2 Menu entries

First in Terminal enter sudo blkid -c /dev/null to get the partitions to work with.

/dev/sda1: LABEL="C:" UUID="1CFC7A8DFC7A60C6" TYPE="ntfs" PARTUUID="a55f55ec-01"
/dev/sda2: UUID="97ce09d6-b701-499e-b838-93762e3b005c" TYPE="ext4" PARTUUID="a55f55ec-02"
/dev/sda4: UUID="09320a63-c5e5-4e47-afab-dfa5f9a5d8dc" TYPE="ext4" PTTYPE="dos" PARTUUID="a55f55ec-04"
/dev/sda5: UUID="3e98ebb1-3a5e-46d2-9302-6e4a811e644b" TYPE="swap" PARTUUID="a55f55ec-05"
/dev/sda6: UUID="c1549566-ed28-48f2-a546-b8a5b39729d6" TYPE="ext4" PARTUUID="a55f55ec-06"
/dev/sda7: UUID="1426326a-e0a8-4e19-86a2-0019148b3ac9" TYPE="ext4" PARTUUID="a55f55ec-07"

You can see Windows is on sda1 and the rest of the labels have been removed.
Labeling the partitions is very useful at this point.
Arch Linux is on sda2 which is drive 1 (a) partition 2, which will be depicted as (hd0,2) as the disk drive numbers start with zero.
If you have Windows installed, make note of the UUID for use in the custom entry.

Enter gksudo gedit /etc/grub.d/40_custom in Terminal.
All you will see is 5 lines:

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

Although it says not to, you will need to change
exec tail -n +3 $0 to
exec tail -n +4 $0
As well as adding the 2nd line below listing your Operating systems:

#!/bin/sh
echo 1>&2 "Adding Xenial Xerus 16.04, Ubuntu Version name nn.nn and Windows 10"
exec tail -n +4 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.

Whatever you put between the quotes is totally up to you. This is what will display when you enter sudo update-grub
What you put between the double quotes beside each menuentry line is what will display on the Grub2 screen at boot time.

Here is what the first entry will look like (copied and pasted below the 6 lines above)

menuentry "Xubuntu Xenial Xerus 16.04 LTS" {
    set root=(hd0,2)
        linux /vmlinuz root=/dev/sda2 ro quiet splash
        initrd /initrd.img
}

Second entry for Xenial to boot into the backup kernel:

menuentry "Xubuntu Xenial Xerus 16.04 LTS (backup kernel)" {
    set root=(hd0,2)
        linux /vmlinuz.old root=/dev/sda2 ro quiet splash
        initrd /initrd.img.old
}

Third entry for Xenial Recovery:

menuentry "Xubuntu Xenial Xerus 16.04 LTS (Recovery Mode)" {
    set root=(hd0,2)
        linux /vmlinuz root=/dev/sda2 ro recovery nomodeset
        initrd /initrd.img
}

Any other versions are the same:

menuentry "Ubuntu Version name nn.nn" {
    set root=(hd0,3)
        linux /vmlinuz root=/dev/sda3 ro quiet splash
        initrd /initrd.img
}

menuentry "Ubuntu Version name nn.nn (backup kernel)" {
    set root=(hd0,3)
        linux /vmlinuz.old root=/dev/sda3 ro quiet splash
        initrd /initrd.img.old
}

menuentry "Ubuntu Version name nn.nn (Recovery Mode)" {
    set root=(hd0,3)
        linux /vmlinuz root=/dev/sda3 ro recovery nomodeset
        initrd /initrd.img
}

Add as many of the triple entries above one for regular boot, one to boot into the backup kernel and one for recovery as you have systems on your computer. It is up to you if you want to have the ability to boot into the backup kernel. If you chose not to just omit that part.

Entry for Windows - match your UUID to what is beside --set (if you have Windows): All Windows versions work the same.

menuentry "Windows 10" {
    insmod ntfs
    set root='(hd0,1)'
    search --no-floppy --fs-uuid --set 1CFC7A8DFC7A60C6
    chainloader +1
}

DO NOT CLICK SAVE! Click file, save as and save it as 06_custom and not 40_custom.
This way the custom entries will be display at the top which is the goal.

Find where it is saved. If it is saved in /etc/grub.d/ you are good to go.
If it is in the home directory, enter this in Terminal sudo cp ~/06_custom /etc/grub.d/ and enter your password if asked.

Enter sudo chmod +x /etc/grub.d/06_custom to make it executable.

Then always remember to enter sudo update-grub or sudo update-grub2 and reboot to view the changes.
If you forget to enter sudo update-grub your changes will not "stick" and you will see that when you reboot.

In the 06_custom file there are no blanks between the lines.

Back to top

Editing /etc/default/grub

Now change the default line number and the timeout.

To determine which number to put in GRUB_DEFAULT= You can enter this command in terminal and use the number you want:

grep -e "menuentry " -e "submenu" /boot/grub/grub.cfg | sed 's/^[ \t]*//' | cut -d "{" -f1 | nl --starting-line-number=0

This will list all menu entries that grub will see.
Then enter this in terminal gksudo gedit /etc/default/grub
GRUB_DEFAULT=0 means to default on the 1st line at the top. The numbering starts with 0.
If you only have one Ubuntu installation, this should already be set to 0.
If you dual boot, or multi-boot set the number to which ever line you want to be default.
The default will never change unless you change it.

GRUB_TIMEOUT=60 means Grub2 screen displays for 60 seconds before defaulting. Set this to whatever number you want.

Back to top

Example pictures

Picture of Xenial Xerus 16.04 LTS and Windows 10:

Xenial-1024x768.png

Picture of Zesty Zapus 17.04, Xenial Xerus 16.04 LTS, Arch Linux and Windows 10:

Zesty-1024x768.png

Picture of Artful Aardvark 17.10, Zesty Zapus 17.04, Xenial Xerus 16.04 LTS, Arch Linux and Windows 10:

Artful-1024x768.png

Back to top

Final changes

Once you are confident that all of the custom entries work as they should. You can eliminate the other menu entries below these custom entries.
sudo chmod -x /etc/grub.d/20_memtest86+ will make memtest unexecutable and will not show in the menu.
sudo chmod -x /etc/grub.d/10_linux will make the list of kernels not display.
sudo chmod -x /etc/grub.d/30_os-prober will make any other Linux systems and Windows entries not display.
Then always enter sudo update-grub when finished making changes.
Only having the custom menu displayed (as displayed in the example picture above), this is the output of sudo update-grub:

Generating grub configuration file ...
Found background image: blueeyedcat.png
Adding Arch Linux, Xubuntu Xenial Xerus 16.04 LTS, Xubuntu Zesty Zapus 17.04, Artful Aardvark 17.10 and Windows 10
done

Back to top

Installing or re-installing more than one Ubuntu system

You should first make sure that Grub2 is not installed on the system that you are removing or re-installing as you may be looking at grub rescue if so.

I recommend installing Grub2 on another Ubuntu if you have one by booting into that Ubuntu and entering
sudo grub-install /dev/sdx where x is the hard drive (e.g. sda).

If you install a 2nd or 3rd Ubuntu etc. or if you re-install one you may need to edit fstab for that system.

Boot into the new or re-installed Ubuntu and enter sudo blkid -c /dev/null and then cat /etc/fsab.
What appears in fstab should match the output of sudo blkid -c /dev/null.
If it does not enter gksudo gedit /etc/fstab make any changes to UUID and delete any extra lines then save the file.

Just in Case

If you need to access an older kernel or for any reason need to see the regular menu entries.
(If you are multi-booting more than one Ubuntu and the one you want to see the other kernels is not where your Grub is installed.
Login to that Ubuntu and if it is not possible to get into the system normally, Recovery for that system will do the job.
Choose "root console" from the Recovery menu.)
Enter sudo grub-install /dev/sda (where sda is the disk it is installed on) sudo blkid -c /dev/null will show the correct one if it is not known.

Enter this:
sudo chmod +x /etc/grub.d/10_linux && sudo update-grub and/or
sudo chmod +x /etc/grub.d/30_os-prober && sudo update-grub, reboot and the menu entries will be restored.

If you multiboot mutiple Linux installs and want one Grub to control all of your OSs

The MBR (Master Boot Record) is where your system boots from. Each partition has what is called a PBR (Partition Boot Record)
When you add a 2nd or 3rd Linux system you can opt to install the grub to the PBR instead of the MBR.
This way your grub will not move from one partition to another each time any grub is updated on other partitions.

Select manual partitioning to install grub to the PBR (e.g. sda2) instead of the MBR (e.g. sda).

If you have already installed each grub on the MBR you can fix it as above with these commands:
For non efi systems:

sudo dpkg-reconfigure grub-pc

Or on efi systems use this command:

sudo dpkg-reconfigure grub-efi-amd64

Enter thru first pages, spacebar to choose/unchoose drive, enter to accept.

Just make sure you remember which install (MBR) your grub is installed on.

If you ever change your mind, when you are on the system you want to install grub on just enter sudo grub-install /dev/sda.

Back to top

Undo the changes made by this tutorial

Enter the following commands in terminal:
sudo chmod +x /etc/grub.d/20_memtest86+
sudo chmod +x /etc/grub.d/30_os-prober
sudo chmod +x /etc/grub.d/10_linux

Then you can either make the custom file unexecutable if you think you will use it in the future:
Enter in terminal sudo chmod -x /etc/grub.d/06_custom
Or you can simply delete the file: sudo rm /etc/grub.d/06_custom
Delete any picture(s) in /boot/grub/. sudo rm /boot/grub/blue-sea.jpg
Enter gksudo gedit /etc/default/grub in terminal and change the GRUB_DEFAULT= to 0.
Then enter sudo update-grub in terminal, reboot and you are back to normal.

Where to post questions, problems, etc

How to have a custom Grub2 menu that is maintenance free

Reference

Original post The Ubuntu Forums (ubuntuforums.org)


Links

1. Grub2

2. GNU GRUB2 Manual

MaintenanceFreeCustomGrub2Screen (last edited 2017-09-29 20:02:02 by cavsfan)