Outils pour utilisateurs

Outils du site


issue87:grub2_et_multibooting

Table des matières

1

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.

Dans les parties précédentes de cette série, j'ai expliqué comment fonctionne GRUB (la version 2) et comment vous pouvez ajuster son comportement. Le fonctionnement du menu GRUB est déterminé par le fichier de configuration /boot/grub/grub.cfg. Ce fichier est créé quand vous saisissez « sudo update-grub » dans un terminal. Un des fichiers lus par update-grub est /etc/default/grub, dont j'ai parlé la dernière fois. En outre, nous avons appris que les scripts exécutables sous /etc/grub.d/ déterminent d'autres aspects de /boot/grub/grub.cfg et nous allons y regarder de plus près maintenant.

Le nom des scripts dans /etc/grub.d/ commence sans exception par un numéro et (seulement) ceux qui sont « exécutables » sont lus par update-grub dans l'ordre de leur numéro. Ce que nous pouvons vouloir faire ici est de rendre les scripts non exécutables (avec la commande chmod) selon notre choix, et surtout le script « 40_custom ». Vous pouvez utiliser 40_custom pour ajouter des entrées personnalisées au menu.

Voici à quoi ressemble un script 40_custom par défaut :

#!/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. [Ce fichier fournit une méthode facile pour l'ajout d'entrées personnalisées au menu. Il suffit de taper les entrées de menu que vous voulez ajouter après ce commentaire. Assurez-vous de ne pas changer la ligne « exec tail » ci-dessus.]

Ainsi, sauf pour ce qui concerne la ligne exec, ce script est vide (# signifie des commentaires). Vous ajoutez tout simplement vos entrées personnalisées sous ces lignes. « Tout simplement » vous dites ? Eh bien, nous allons le faire dans une minute. Les entrées personnalisées que vous fournissez ici sont copiées directement dans grub.cfg quand vous lancez « sudo update-grub ». Et des entrées personnalisées sont ce dont nous aurons besoin. Avant, nous devons bien regarder la forme des entrées de menu dans grub.cfg.

2

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.

Examinons deux types d'entrées menu que vous rencontrerez dans un grub.cfg typique sur un PC avec une distribution Linux et Windows (j'ai encore XP). (Pour Mac, voyez les liens à la fin.) Copiez les entrées vers 40_custom pour faire des tests avec : ces entrées dans le menu custom apparaîtront simplement comme des lignes supplémentaires que vous pourriez choisir ; il n'y a donc pas de risque à le faire. Les extraits ci-dessous viennent d'un grub.cfg d'origine, créé par le paquet grub-pc sur un PC. Pendant l'installation, le contenu fut « automagiquement » adapté au matériel (par exemple, en prenant en compte le type de partitionnement du disque dur et notamment msdos-partitioning si Windows y était en premier). Ses caractéristiques dépendront en partie de la version du paquet grub-pc aussi. Par conséquent, je vous conseille de copier les parties correspondantes à partir de votre grub.cfg, puisque cela a déjà été testé et a fait ses preuves sur votre machine.

L'entrée menu pour une distribution Linux (dans ce cas, Bodhi Linux) se trouve en haut de la page suivante.

Regardons tout cela de plus près :

• set root=(hdx, y) indique le dispositif et le répertoire root où grub doit chercher les fichiers principaux à charger quand cet OS démarre (c'est-à-dire, après la sélection de cette entrée menu dans le menu GRUB). Méfiez-vous de la numérotation bizarre des dispositifs et des partitions. Le numéro x se réfère au premier, second, troisième…, disque dur et commence par 0. Le numéro y se réfère à la partition sur hdx, mais commence le comptage à 1. Ainsi, la première partition sur le premier disque dur (/dev/sda1) est (hd0,1), la seconde partition (/dev/sda2) est (hd0,2) et ainsi de suite. Dans le partitionnement Windows un préfixe msdos est utilisé, comme dans mon cas.

3

• 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.

• Dans le cas d'une distribution Linux, les fichiers que GRUB doit chercher sur cette partition sont le noyau et l'image ramdisk initiale (initrd.img). L'image ramdisk initiale est chargée avec la commande initrd (c'est avec le ramdisk initial qu'un système de fichiers root temporaire est chargé en mémoire pendant le processus du démarrage du noyau Linux. linux charge le noyau Linux. Il y a deux alternatives ici. La première (montrée) fournit un chemin direct vers les fichiers eux-mêmes, ce qui veut dire qu'il faudra changer leur nom quand un noyau plus récent aura été installé. Cet ajustement de grub.cfg se fait automatiquement quand un nouveau noyau est installé. L'alternative est que vous fournissez les liens symboliques vers votre dernier noyau et ramdisk (souvent présent dans /boot/). La commande « sudo update-grub » fait tout ceci automatiquement, avec des entrées distinctes qui pointent vers des noyaux installés moins récents. Jetez-y un œil !

• Dans tous les cas, il est également possible de « chainload » (charger en chaîne) un autre bootloader s'il y en a un qui est installé sur la partition choisie. En effet, des bootloaders peuvent également être installés au début d'une partition. Chainloading signifie charger un nouveau bootloader à partir du bootloader actuel. Le chargement en chaîne d'un bootloader qui réside sur le (hdx, y) s'accomplit avec la commande GRUB « chainloader + ». Puisque Windows a besoin de son propre bootloader pour démarrer, le chargement en chaîne est nécessaire pour démarrer Windows. Une installation Linux peut avoir ou non une (seconde) installation GRUB sur sa propre partition (il se comporte exactement de la même façon : il se réfère à /boot/grub/grub.cfg pour afficher son menu). Dans le cas où il a un GRUB séparé, dans le fichier de configuration GRUB du MBR, vous pouvez choisir si vous voulez chainload le GRUB de cette distribution-là ou démarrer son noyau directement.

4

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.

Sauf pour ce qui concerne les commandes insmod, vous pourriez vouloir faire des expériences en omettant des lignes dans les entrées menu que vous avez copiées pour voir si cela fonctionne toujours (il se peut que oui). Vous pouvez même le faire pour une seule session, sans modifier grub.cfg. Il suffit d'appuyer sur « e » après avoir sélectionné l'entrée menu dans le menu GRUB que vous voulez examiner ou changer. GRUB affichera les commandes de l'entrée menu ; vous pouvez les modifier en toute sécurité, puis les exécuter avec CTRL-X (rien ne sera sauvegardé sur grub.cfg). Omettez également le paramètre « quiet » dans la déclaration Linux pour voir ce qui se passe alors. Ceux qui veulent en apprendre davantage peuvent suivre les liens à la fin. Mais avec les ingrédients ci-dessus, ça ira.

Vous pouvez vouloir aussi changer quelques couleurs du texte dans le menu GRUB, surtout si vous allez utiliser une image de fond « splash » (Cf l'article précédent). Des paramètres couleurs se trouvent dans le script 05_debian_theme ou peut-être dans un script supplémentaire comme 06_mint_theme (dans /etc/grub.d/). Vous utiliserez des éléments tels que paramétrer color_normal, color_highlight, menu_color_normal et menu_color_highlight ; regardez les ressources en ligne pour plus de détails. Pour résumer : les défauts sont noir/gris-clair pour (menu_)color_highlight et blanc/noir pour (menu_)color_normal. « black/light-gray » veut dire du texte noir sur un fond gris clair (pour l'entrée du menu sélectionnée) ; « white/black » est du texte blanc sur un fond transparent (noir en tant que couleur de fond n'est pas black !). Cela aura son importance quand vous utiliserez une image de fond. Les couleurs suivantes sont disponibles : noir, bleu, marron, cyan, gris foncé, vert, cyan clair, bleu clair, vert clair, gris clair, magenta clair, rouge clair, magenta, rouge, blanc et jaune. Amusez-vous bien avec tout ceci. Auparavant, vous pourrez tester l'effet en temps réel en exécutant les commandes GRUB appropriées dans le terminal GRUB (par exemple set color_normal=bleu/blanc), mais vous devrez insérer ces paramètres dans 05-debian-theme pour qu'il soit paramétré automatiquement par grub.cfg. Vous pouvez aussi faire des expériences avec grub.cfg et y insérer directement les déclarations « set … color= », mais sachez que grub.cfg est écrasé après l'exécution de update-grub.

5

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/

Bon. Nous avons fait des modifications dans un ou deux fichiers dans /etc/grub.d/ et dans /etc/default/grub. Maintenant, nous allons mettre à jour /boot/grub/grub.cfg pour qu'elles soient prises en compte (sauvegardez votre grub.cfg existant). Lancez donc :

sudo update-grub

N'oubliez pas de jeter un œil à grub.cfg pour vous assurer que tout à l'air OK. Vos entrées personnalisées devraient y être. Redémarrez et essayez-les. GRUB est à vous maintenant ; faites-vous plaisir !

La prochaine fois, nous aurons la dernière partie de cette mini-série. Je vous donnerai quelques bases sur le partitionnement et nous construirons la machine qui fait du multibooting…

Liens intéressants

• La documentation Ubuntu sur GRUB2, plus des liens en bas de page, sur GRUB terminal (« troubleshooting » ou dépannage), l'affichage des menus et les entrées menu personnalisées : https://help.ubuntu.com/community/Grub2 • Un tutoriel complet sur GRUB2 chez Dedoimedo, avec des trucs très utiles concernant la configuration de GRUB : http://www.dedoimedo.com/computers/grub-2.html

• Une courte liste de paramètres du noyau avec des liens vers des listes complètes : https://wiki.archlinux.org/index.php/kernel_parameters#Parameter_list

• Démarrer Mac OS à partir de GRUB : http://leveluplinux.blogspot.be/2012/09/dual-booting-linux-and-os-x-using-grub2.html ou http://www.maketecheasier.com/create-a-mac-entry-in-grub2 (je ne l'ai pas essayé).

• Le manuel officiel de GRUB : http://www.gnu.org/software/grub/manual/

issue87/grub2_et_multibooting.txt · Dernière modification : 2015/01/08 17:51 de frangi