Outils pour utilisateurs

Outils du site


issue86:grub_2_multibooting

Table des matières

1

In the first part of this mini-series, I explained the basic functionality of GRUB (version 2) and its configuration file grub.cfg. Ultimately, we want to achieve a multibooting system where changes or new installs of operating systems (OSs) don’t mess up GRUB. Your GRUB menu should offer a choice between multiple installed OSs to choose from. A prerequisite is an essential knowledge of how GRUB works. Follow me to find out! For now, let’s consider a normal installation of GRUB on the Master Boot Record (MBR) of your hard drive, which, for example, has been arranged for you when you installed Ubuntu. If you have a spare machine on which you can play with this GRUB installation, then do it! You can learn a lot from experimenting with GRUB (see also the links at the end of this article). If not, just believe the explanations below, or be more careful in your experiments – the exact nature of your experiments will determine not only how GRUB works, but also whether it still works…

Dans la première partie de cette courte série, j'ai expliqué les fonctions de base de GRUB (version 2) et de son fichier de configuration, grub.cfg. Notre objectif final est de créer un système multi-boot où des modifications ou de nouvelles installations de systèmes d'exploitation ne perturbent point GRUB. Le menu de GRUB devrait proposer un choix entre des systèmes d'exploitation multiples qui sont installés sur votre ordinateur. Un pré-requis capital est une bonne connaissance du fonctionnement de GRUB. Suivez-moi pour le découvrir !

Pour le moment, nous allons examiner une installation normale de GRUB sur le Master Boot Record (MBR) de votre disque dur, qui, par exemple, a été confectionné pour vous lors de l'installation d'Ubuntu. Si vous avez une machine de rab, sur laquelle vous pouvez jouer avec cette installation de GRUB, alors faites-le ! Vous pouvez apprendre beaucoup de choses en faisant des expériences avec GRUB (voir aussi les liens à la fin de cet article). Sinon, acceptez tout simplement les explications ci-dessous ou faites plus attention lors des expériences - la nature exacte de ces expériences déterminera non seulement le fonctionnement de GRUB, mais également si GRUB est encore en état de fonctionner…

2

We already learned that GRUB runs from the MBR at hard disk boot, and that it relies on grub.cfg which is in /boot/grub/ on a partition that was specified to GRUB during its installation. Well actually, this “dependence” on grub.cfg is needed only for displaying the user-friendly menu from which you can select an OS using arrow keys and Enter. GRUB itself can thrive without grub.cfg, although it still needs to load specific modules from /boot/grub/ in order to access all of its functionality. You can access 'bare' GRUB from the GRUB menu by pressing 'c' (press Esc to return to the menu). Welcome to the GRUB command prompt! The GRUB operating system is just awaiting your commands now – and that puts you in control! This will be useful when, for any reason, the GRUB menu doesn’t load, or a GRUB menu entry does not work. An in-depth discussion of the possibilities is far beyond the scope of this article, and I refer to the internet links given at the end. I will discuss some introductory commands at the end of the article though, just to get you in play… Yes, ladies and gentlemen, mastering the command line opens a great new world of possibilities – also in GRUB!

Nous avons déjà appris que GRUB s'exécute à partir du MBR lors du démarrage du disque dur et qu'il dépend de grub.cfg qui se trouve dans /boot/grub/ sur une partition qui a été spécifiée à GRUB pendant son installation. Cependant, en fait, cette « dépendance » du fichier grub.cfg est nécessaire uniquement pour l'affichage du menu convivial à partir duquel vous pouvez sélectionner un système d'exploitation avec les touches fléchées et Entrée. GRUB lui-même se porte très bien sans grub.cfg, bien qu'il ait toujours besoin de charger des modules spécifiques à partir de /boot/grub/ afin de pouvoir accéder à toutes ses fonctionnalités. vous pouvez accéder à GRUB « tout court » en appuyant sur « c » (appuyez sur Échap pour revenir au menu). Bienvenue à l'invite de commandes GRUB ! Maintenant, le système d'exploitation GRUB attend vos commandes - et il vous laisse le contrôle ! Ce sera utile quand, pour quelque raison que ce soit, le menu GRUB ne charge pas, ou une entrée dans le menu GRUB ne fonctionne pas. Une discussion approfondie des possibilités excède, et de loin, le champ d'application de cet article et je vous renvoie à nouveau aux liens à la fin. Toutefois, je présenterai quelque commandes de base à la fin de l'article, pour que vous puissiez commencer à jouer… Oui, mesdames et messieurs, la maîtrise de la ligne de commande rend accessible un nouveau monde de possibilités - dans GRUB aussi !

3

The GRUB menu will be our focus now. Its components are controlled by the contents of /boot/grub/grub.cfg. Have a good look at grub.cfg. It is the file that GRUB reads. What is in there? Nothing but GRUB commands! The official manual on gnu.org denotes this as “GRUB's built-in scripting language”. By looking carefully, you may already understand a lot of what is stated there: several modules are loaded with insmod, for example to handle msdos-partitions, or to display png or jpeg images. Further, several so-called ‘menu entries’ (lines of the GRUB menu) are listed by their name of the OS plus kernel version, each followed by the specific instructions that GRUB needs to boot the corresponding OS. Essential instructions appear below each menu-entry in order to boot an OS: the hard drive and partition numbers where that OS resides, and the directory path and name of the kernel to be booted and of the initial ramdisk image (which is loaded first). The GRUB command to point at the Linux kernel is… linux. I’m sure at this stage you do not want to edit grub.cfg, and it is not recommended either. So have no worries: the GRUB configuration file is created for you when you type 'sudo update-grub' in a terminal. The update-grub command is delivered with the grub-pc package, which was also responsible for installing GRUB on the MBR. The update-grub command reads the contents of several files: on the one hand /etc/default/grub, which contains several personal settings, and on the other hand the executable scripts under /etc/grub.d/. We will play with the scripts next time.

Maintenant, nous allons nous concentrer sur le menu GRUB. C'est le contenu de /boot/grub/grub.cfg qui contrôle ces composants. Regardez bien grub.cfg. C'est le fichier que lit GRUB. Qu'y a-t-il dedans ? Rien que des commandes GRUB ! Le manuel officiel sur gnu.org l'appelle « GRUB's built-in scripting language » (le langage pour scripts intégré à GRUB). Si vous regardez avec attention, il se peut que vous compreniez pas mal de choses qui y figurent : plusieurs modules se chargent avec insmod, par exemple, pour gérer des partitions ms-dos ou pour afficher des images png ou jpeg. En outre, plusieurs des soi-disant « entrées du menu » (des lignes du menu GRUB) sont listées par leur nom composé du système d'exploitation plus la version du noyau, suivi des instructions dont GRUB a besoin pour démarrer l'OS correspondant. Des instructions essentielles pour démarrer un OS s'affichent en dessous de chaque entrée dans le menu : le disque dur et la partition où ce système d'exploitation-là réside et le chemin du répertoire et le nom du noyau qui devra être démarré et celui de l'image ramdisk initiale (qui se charge en premier). La commande GRUB qui pointe vers le noyau Linux est… linux.

À ce stade, je suis certain que vous ne voulez pas éditer grub.cfg et ce n'est pas recommandé non plus. Mais pas d'inquiétude à avoir : le fichier de configuration de GRUB est créé pour vous quand vous saisissez « sudo update-grub » dans un terminal. La commande update-grub est livrée avec le paquet grub-pc, celui qui a installé GRUB sur le MBR. la commande update-grub lit le contenu de plusieurs fichiers : d'une part, /etc/default/grub, qui contient plusieurs paramètres personnalisés, et, d'autre part, les scripts exécutables sous /etc/grub.d/. Nous jouerons avec les scripts la prochaine fois.

4

For now, let’s tweak /etc/default/grub. The first part of this file looks like the one shown on the next page, top right. • the menu entry that is selected by default when the GRUB menu shows, is determined by GRUB_DEFAULT=x. You have to fill in the number of the line, counting from… zero. Bummer. • the counter (in seconds) after which the default entry is executed if no key is pressed, is determined by GRUB_TIMEOUT (as seconds). • GRUB_HIDDEN_TIMEOUT=0 should be commented out with a hash (#) if you don't want the GRUB menu to be hidden. This may need to be changed for your first installed Linux distribution if you did not install Windows. • GRUB_CMDLINE_LINUX_DEFAULT sets the kernel parameters to be added to the linux command, for each default menu entry for Linux distros in grub.cfg. Recovery entries excluded. • adding GRUB_BACKGROUND=/path/filename determines the splash image to be displayed as a menu background. Alternatively, just putting a background image (jpeg or png) into /boot/grub should suffice to be picked up by update-grub. Change these settings as you like. Then comes the moment to update /boot/grub/grub.cfg correspondingly. First, backup your existing grub.cfg. Then run this in a terminal: sudo update-grub

Pour le moment, nous ferons des ajustements à /etc/default/grub. La première partie de ce fichier ressemble à ce qui est montré en haut à droite de la page suivante.

• l'entrée du menu qui est sélectionné par défaut quand le menu GRUB s'affiche, est déterminée par GRUB_DEFAULT=x. Il faut y mettre le numéro de la ligne, en comptant à partir de… zéro. Quelle poisse !

• le compteur (en secondes) après lequel l'entrée par défaut est exécutée si aucune touche n'est enfoncée, est déterminé par GRUB_TIMEOUT (en secondes).

• GRUB_HIDDEN_TIMEOUT=0 devrait être commenté avec un dièse (#) si vous ne voulez pas que le menu GRUB soit masqué. Vous devrez éventuellement changer cela pour la première distribution Linux installée si vous n'avez pas installé Windows.

• GRUB_CMDLINE_LINUX_DEFAULT règle les paramètres du noyau qui sera ajouté à la commande linux, pour chaque entrée du menu de distrib. Linux par défaut dans grub.cfg. Les entrées de « recovery mode » (mode restauration) ne sont pas incluses.

• L'ajout de GRUB_BACKGROUND=/path/filename règle l'image de démarrage affichée en tant qu'arrière-plan du menu. Par ailleurs, une image d'arrière-plan (jpeg ou png) insérée dans /boot/grub devrait être reconnue par update-grub.

Changez ces paramètres comme vous voulez. Ensuite, c'est le moment de mettre /boot/grub/grub.cfg en conséquence. Tout d'abord, sauvegardez le grub.cfg existant. Puis lancez ceci dans un terminal :

sudo update-grub

5

You will see some messages during the process, which should last less than a minute. After that, have a look at grub.cfg to check if everything looks OK. Reboot and enjoy your changes! Booting with an erratic (or absent) grub.cfg, will leave you in the GRUB terminal, or – worse – in GRUB rescue mode. So it is a very good idea to be prepared for that (see the online resources). The best way to prepare for this is to run exercises in the GRUB terminal and learn to boot OSs from there. You better do that before experimenting with grub.cfg (so that you still have a working GRUB menu at your disposition). When you see the GRUB menu during booting, press ‘c’ to get at the GRUB prompt (and Esc to turn back to the menu). Now you can learn to manually run GRUB commands (instead of letting the menu do this for you). I list only a few commands here to start with: set pager=1 # to avoid longer GRUB output to roll off your screen help [command] # without argument, lists available commands; with argument, shows help of the specified command set # lists current settings ls <path> # displays contents of a directory cat <file> # displays contents of a file boot # starts the boot sequence; identical to selecting an OS in the GRUB menu with Enter. This command will work only if GRUB has been told where the OS to be booted resides: you find these essential commands under each 'menu entry' in grub.cfg, or just wait till next time!).

Vous verrez quelques messages pendant le processus, qui devrait durer moins d'une minute. Après cela, regardez grub.cfg pour vérifier si tout est comme il faut. Redémarrez et profitez pleinement des modifications !

Si vous démarrez avec un grub.cfg instable (ou absent), vous vous retrouverez dans le terminal GRUB ou, pire, en mode restauration de GRUB. C'est donc une très bonne idée d'être prêt à affronter cela (voir les ressources en ligne). La meilleure façon de s'y préparer est de s'entraîner dans le terminal GRUB et d'apprendre à démarrer des systèmes d'exploitation à partir de là. Il vaudrait mieux le faire avant d'expérimenter avec grub.cfg (car, ainsi, vous avez toujours un menu GRUB qui fonctionne à votre disposition). Quand vous voyez le menu GRUB pendant le démarrage, appuyez sur « c » pour avoir l'invite GRUB (et sur Échap pour revenir au menu). Vous pouvez maintenant apprendre à exécuter des commandes GRUB manuellement (au lieu de dépendre du menu). Je ne vais énumérer que quelques commandes ici pour commencer :

set pager=1 # pour éviter qu'un long menu GRUB ne dépasse l'écran

help [command] # sans argument, liste les commandes disponibles ; avec argument, affiche l'aide de la commande spécifiée

set # liste les paramètres actuels

ls <path> # affiche le contenu d'un répertoire

cat <file> # affiche le contenu d'un fichier

boot # commence la séquence de démarrage ; identique au choix d'un OS dans le menu GRUB avec Entrée.

(Cette commande ne fonctionnera que si vous avez informé GRUB de l'emplacement du système d'exploitation à démarrer : vous trouverez ces commandes essentielles sous chaque « menu entry » dans grub.cfg ou ce sera pour la prochaine fois !)

issue86/grub_2_multibooting.txt · Dernière modification : 2014/12/10 19:44 de d52fr