Ceci est une ancienne révision du document !
In previous parts I explained how GRUB (version 2) works and how you can tune its behaviour. The workings of the GRUB menu are determined by the configuration file /boot/grub.grub.cfg. This file is created when you type 'sudo update-grub' in a terminal. One of the files that is read by update-grub is /etc/default/grub, which we discussed last time. Further, we learned that the executable scripts under /etc/grub.d/ determine other aspects of /boot/grub/grub.cfg, and these will be our focus now.
The names of the scripts in /etc/grub.d/ all start with a number, and (only) those which are actually set as ‘executable’ are read by update-grub in the order of their number. What we may want to do here is to disable the executable flag of the scripts according to our choice (with the chmod command) – especially the script ‘40_custom’. You can use 40_custom to add custom menu entries.
This is how a default 40_custom script looks like:
#!/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.
So, except for the exec line, this script is empty (# is for comments). Simply add your custom entries below these lines. ‘Simply’ you say? Well, we’ll do that in a minute. The custom entries you provide here are directly copied into grub.cfg when running 'sudo update-grub'. And custom entries is what we will need – we still need a better look at how menu entries in grub.cfg are composed.
Let’s consider two types of menu entries which you will encounter in a typical grub.cfg on a pc that contains a Linux distribution and Windows (I still have XP). (For Mac, see links at the end.) Copy the entries to 40_custom to experiment with these: these custom menu entries will just appear as extra selectable lines in your GRUB menu, so there’s no harm in doing this. The extracts below are from an unmanipulated grub.cfg that was created by the grub-pc package on a pc. During installation, its contents were automagically tailored for my specific hardware (e.g. taking into account the type of partitioning of the hard disk, e.g. msdos-partitioning if Windows was there first). Also, its specifics will partly depend on the version of the grub-pc package. Consequently, I advise to copy the corresponding parts from your grub.cfg, as this is already tested and proved for your machine.
The menu entry for a Linux distribution (e.g. Bodhi Linux) is shown on the next page (top).
Let's have a look at some essentials here:
• set root=(hdx, y) sets the device and root directory on which grub has to look for the primary files to load when booting this OS (i.e. after this menu entry has been selected in the GRUB menu). Beware of the odd numbering of devices and partitions! Number x refers to the first, second, third, … hard drive and starts at 0. Number y refers to the partition on hdx but starts counting at 1. So the first partition on the first hard drive (/dev/sda1) is (hd0,1), the second partition (/dev/sda2) is (hd0,2) and so on. In Windows partitioning layout, an msdos prefix is used as in my case.
• In the case of a Linux distribution, the files that GRUB must look for on this partition are the kernel and the initial ramdisk image (initrd.img). The initial ramdisk image is loaded by the initrd command (the initial ramdisk is a scheme for loading a temporary root file system into memory in the boot process of the Linux kernel). linux loads the Linux kernel. Two alternatives exist here. The first, as presented, is to provide the direct path to the files themselves, which means their names will need to be changed when a more recent kernel has been installed. This adjustment of grub.cfg is automatically taken care of when a new kernel is installed. The alternative is that you provide the symlinks to your latest kernel and ramdisk (which are often present in /boot/). The command 'sudo update-grub' automatically takes care of these things, with separate entries that point to older installed kernels. Check it out!
• In all cases, it is also possible to ‘chainload’ another boot loader whenever one is installed on the chosen partition. Indeed, boot loaders can also be installed at the beginning of a partition. Chainloading is the loading of a new boot loader from the boot loader you are in. Chainloading a bootloader that resides on the (hdx,y) that is set, is accomplished with the GRUB command ‘chainloader +1’. As Windows needs its own boot loader to boot, chainloading is obligatory to boot Windows. A Linux installation may or may not have a (second) GRUB installation on its own partition (it behaves exactly the same: it refers to /boot/grub/grub.cfg to display its menu). In case it does have a separate GRUB, in the MBR GRUB’s configuration file you can choose whether you want to chainload that distro’s GRUB or directly boot its kernel.
Except for the insmod statements, you may want to experiment by omitting lines in the menuentries that you copied and see if things still work (they may). You can even do this for one session only, without altering grub.cfg. Just press ‘e’ after highlighting the menuentry in the GRUB menu that you want to inspect or change. GRUB will show you the commands from the menu entry; you can safely edit these and execute with Ctrl-X (nothing will be saved to grub.cfg). Also try omitting the ‘quiet’ parameter in the linux statement to see what happens. Those wishing to learn more are referred to the links at the end. But with the above ingredients you will get along.
You may also want to change some text colors in the GRUB menu, especially if you will use a background ‘splash’ image (see previous time). Color settings are present in the script 05_debian_theme or possibly in an extra script like 06_mint_theme (under/etc/grub.d/). You will use such things as set color_normal, color_highlight, menu_color_normal and menu_color_highlight; inspect the online resources for more information. In short, the defaults are black/light-gray for (menu_)color_highlight and white/black for (menu_)color_normal. ‘black/light-gray’ means black text on a light-gray background (for the selected menu entry); ‘white/black’ is white text on a transparent background (black as background color is not black!). That will be important when you use a background image. The following colors are available: black, blue, brown, cyan, dark-gray, green, light-cyan, light-blue, light-green, light-gray, light-magenta, light-red, magenta, red, white and yellow. Have fun with these things. Beforehand, you can test the effect in real-time by running the appropriate GRUB commands in the GRUB terminal (e.g. set color_normal=blue/white), but you will need to insert these settings in 05_debian_theme to get it set automatically through grub.cfg. You can also experiment with grub.cfg and insert the ‘set … color=’ statements directly in there, but beware that grub.cfg is overwritten after update-grub is run.
So, we made alterations to one or two files in /etc/grub.d/ and to /etc/default/grub. Now we update /boot/grub/grub.cfg correspondingly (backup your existing grub.cfg). So run:
sudo update-grub
Don’t forget to have a look at grub.cfg to check if everything looks OK. Your custom entries should be there. Reboot and try them out. GRUB is yours now; enjoy!
Next time will be the final part of this mini-series. I will tell some basics about partitioning – and then we build our multibooting machine…
Interesting links
• Ubuntu documentation on GRUB2 - plus related links at the page bottom, on GRUB terminal (‘troubleshooting’), menu displays and custom menu entries: https://help.ubuntu.com/community/Grub2 • Dedoimedo GRUB2 full tutorial, with very useful hints on GRUB configuration: http://www.dedoimedo.com/computers/grub-2.html
• A short list of kernel parameters, with links to full lists: https://wiki.archlinux.org/index.php/kernel_parameters#Parameter_list
• Booting Mac OS from GRUB: http://leveluplinux.blogspot.be/2012/09/dual-booting-linux-and-os-x-using-grub2.html or http://www.maketecheasier.com/create-a-mac-entry-in-grub2 (not tested)
• The official GRUB manual: http://www.gnu.org/software/grub/manual/