When I install Ubuntu in my computer aside the other OS that I have, it’s always a pain to re-edit the files that control Grub because the way to edit it tend to change with some version of Ubuntu (although they’re small changes, but it’s always a tiresome task to surf the net and find out the little information you need in this vast internet environment) .
So in this article I collect the minimal knowledges about definitions and changes I need to do on this subject to obtain a satisfactory customized Grub.
Since the information I need are written in a perfect way from the sources from where I read them, instead of rearranging the information, I will just copy and paste them here always inside a quotation block. All the credits go to their creators.
GRUB 2 is the default boot loader and manager for Ubuntu since version 9.10 (Karmic Koala).
To determine the version installed in your computer, you can use the line command “grub-install -v” inside the shell of Ubuntu.
To modify GRUB 2, there are some files that need to be considered:
- /boot/grub/grub.cfg
- /etc/default/grub
- /etc/grub.d/ (directory)
/boot/grub/grub.cfg
This file contains the GRUB 2 menu information but the grub.cfg file is not meant to be edited.
Each section is clearly delineated with “(### BEGIN)” and references the script in the /etc/grub.d directory from which the information was generated.
grub.cfg is updated by running the update-grub command as root.
/etc/default/grub
The entries in this file can be edited by a user with administrator (root) privileges and are incorporated into grub.cfg when it is updated.
/etc/grub.d/ (directory)
The scripts in this directory are read during execution of the update-grub command and their instructions are incorporated into /boot/grub/grub.cfg.
The placement of the menu items in the grub.cfg menu is determined by the order in which the files in this directory are run. Files with a leading numeral are executed first, beginning with the lowest number.
Only executable files generate output to grub.cfg during execution of update-grub.
The major default files in this directory used by Ubuntu are:
00_header Sets initial appearance items such as the graphics mode, default selection, timeout, etc.
05_debian_theme The settings in this file set the GRUB 2 background image, text colors, selection highlighting and themes.
10_linux Identifies kernels on the root device for the operating system in use and creates menu entries.
20_memtest86+ Searches for /boot/memtest86+.bin and includes it as an option on the GRUB 2 boot menu.
30_os-prober This script uses os-prober to search for Linux and other operating systems and place the results in the GRUB 2 menu.
40_custom A template for adding custom menu entries which will be inserted into grub.cfg upon execution of the update-grub command.
After editing /etc/default/grub or the scripts in the /etc/grub.d folder the user should run sudo update-grub to incorporate the changes into the GRUB 2 menu.
To modify the aforementioned files you can use the command line “sudo gedit path_of_the_file_to_edit” inside the shell (example: sudo gedit /etc/default/grub). After you input the root password, it will open the software “text editor” with the privilege as root.
In the following lines I will describe only a small part of editing that is of interest to me. To have a more complete editing knowledges you can read the sources at the end of this article from which I collected these information.
First let’s see what’s inside /etc/default/grub (file)
After you opened this file, the entries that are of interest are :
GRUB_DEFAULT – Sets the default menu entry. Entries may be numeric, a complete menuentry quotation, or “saved”
GRUB_DEFAULT=0 Sets the default menu entry by menu position. Counting of entries is the same as in GRUB – the first “menuentry” in grub.cfg is 0, the second is 1, etc.
Note: Grub 1.99 introduces a submenu menu structure. For a menu item in a submenu, the entry becomes a two-digit entry. The first entry is the position of the submenu title in the main menu. The second entry is the position within the submenu. If the submenu is the 3rd entry in the main entry, and the user wishes to boot the first entry in the submenu, it would be designated as “2>0”GRUB_TIMEOUT=10
Setting this value to -1 will cause the menu to display until the user makes a selection.
The GRUB 2 menu is hidden by default unless another OS is detected by the system. If there is no other OS, this line may be commented out unless the user changes it. To display the menu on each boot, uncomment the line and use a value of 1 or higher.#GRUB_GFXMODE=640×480
You can remove the # symbol to make this line active. The entry sets the resolution of the graphical menu (the menu text size). It provides resolutions supported by the user’s graphics card (e.g. 640×480, 800×600, 1280×1024, etc). The setting applies only to the boot menu display, not the resolution of the operating system that boots.
Tip: Setting the same resolution in GRUB 2 and the operating system will decrease boot times slightly.
The user can also add multiple resolutions. If GRUB 2 cannot use the first entry, it will try the next setting. Settings are separated by a comma. Example: 1280x1024x16,800x600x24,640×480.
If using a splash image, the resolution setting and the splash image size should be compatible for best results.
Resolutions available to GRUB 2 can be displayed by typing vbeinfo in the GRUB 2 command line. The command line is accessed by typing “c” when the main GRUB 2 menu screen is displayed.GRUB_DISABLE_LINUX_RECOVERY=true
Uncomment (remove the # symbol) to prevent the “Recovery” mode kernel options from appearing in the menu. If you want a “Recovery” option for only one kernel, make a special entry in /etc/grub/40_custom.GRUB_BACKGROUND=/path_to_image/filename. The GRUB 2 splash images are controlled by the GRUB_BACKGROUND variable in ‘/etc/default/grub‘. The image must be a .png, .tga, .jpeg, or .jpg image.
By changing the value about these variables you can create a more suitable menu list of your interest.
After turning up the computer I prefer to see a screen selection with only the lines about the different OS that is installed on my PC and in the order that is better to me.
The easiest way to obtain this result is to create a custom menu entries.
GRUB 2 allows users to create customized menu selections which will be automatically added to the main menu when sudo update-grub is executed. A 40_custom file is available in /etc/grub.d/ for use or to serve as an example to create other custom menus.
The file must be made executable: sudo chmod +x /etc/grub.d/filename. However if you are using the default file 40_custom and not the one you created then this is not necessary.
The easiest way to create the content of a custom menu is to copy a working entry from /boot/grub/grub.cfg. Once copied, the contents of 40_custom can be tailored to the user’s desires. The user can copy existing menuentries from the /boot/grub/grub.cfg file.
The part that is needed looks like this:
menuentry ‘Ubuntu 11.10 – Oneiric Ocelot’ –class ubuntu –class gnu-linux –class gnu –class os {
recordfail
set gfxpayload=$linux_gfx_mode
insmod gzio
insmod part_msdos
insmod ext2
set root='(hd0,msdos3)’
search –no-floppy –fs-uuid –set=root cb201140-52f8-4449-9a95-749b27b58ce8
linux /boot/vmlinuz-3.0.0-13-generic-pae root=UUID=cb201140-52f8-4449-9a95-749b27b58ce8 ro quiet splash vt.handoff=7
initrd /boot/initrd.img-3.0.0-13-generic-pae
}
or
menuentry “Windows 7 Professional” –class windows –class os {
insmod part_msdos
insmod ntfs
set root='(hd0,msdos1)’
search –no-floppy –fs-uuid –set=root C406C64E37C181E7
chainloader +1
}
NB: make sure to change the old entry “gfxmode $linux_gfx_mode” in this new one “set gfxpayload=$linux_gfx_mode” otherwise it will give you an error like grub2 – Error: unknown command ‘gfxmode’ on the startup.
After you finished modifying the file 40_custom, you just need to remove all but the following files from the /etc/grub.d folder:
- 00_header,
- 05_debian_theme,
- 40_custom
- and README.
The unnecessary files are:
- 10_linux
- 20_linux_xen
- 20_memtest86+
- 30_os-prober
- 41_custom
Alternatively, you may keep other files in the /etc/grub.d folder if you make them unexecutable (which is the way I prefer).
In this case the command needed is sudo chmod -x /etc/grub.d/filename.
Then you just need to run the command update-grub as root to apply the changes!
This is the basic you need to know to modify the list of the boot loader. In the next post I will describe some way to improve the graphic impact of the boot loader