Table des matières
- 2026:
- 2025:
1 billet(s) pour août 2026
| Programmation par contrat | 2026/08/01 00:34 | Jean-Baptiste |
Rescue a Non-Booting System from the Grub2 Command Prompt
Overview
GRUB2 is the bootloader responsible for finding the Linux kernel and booting the operating system. GRUB2 has a powerful command line that can be used to rescue a non-booting system. In this assignment you will use GRUB2 commands to boot a CentOS 7 installation.
Assignment Objectives
When you have successfully completed this assignment you will be able to :
- Install CentOS 7.
- Identify the GRUB2 configuration files.
- Access the GRUB2 prompt.
- Use GRUB commands to find and boot the Linux kernel.
3 Terms You Should Know
/etc/default/grub \ Editable GRUB2 configuration file.
/etc/grub.d/ \ Directory for customization files.
/boot/grub/grub.cfg \ Main GRUB2 configuration file. Do not edit directly.
Initial Ram File System \ Provides an initial filesystem until the root partition can be accessed.
GRUB2 \ The GRand Unified Bootloader
Preparation
For this assignment you need to install CentOS 7 Minimal as a virtual machine.
The GRUB2 Prompt
- After installation reboot the virtual machine.
- As soon as the GRUB menu appears press c to access the GRUB2 prompt.
- Press Tab to list available commands.
- GRUB2 supports tab completion for commands and file names.
Loading Modules
CentOS uses Logical Volume Management (LVM). Before GRUB2 can access files on this filesystems a kernel module needs to be loaded.
grub> set pager=1 grub> insmod lvm
Listing Partitions and LVM Volumes
Use the ls command to list the devices on the virtual machine. Make a note of the LVM logical volume names as they may be different on your virtual machine.
grub> ls (hd0) (hd0,msdos2) (hd0,msdos1) (lvm/centos-root) (lvm/centos-swap) (hd0,msdos1) and (hd0,msdos2) refer to /dev/sda1 and /dev/sda2. The other entries refer to LVM Logical Volumes. GRUB2 uses the drive names provided by the BIOS.
Listing Files on the First Partition
Use ls to list the files on the /boot partition.
grub> ls (hd0,1)/
List Files on the Root Partition
Root is mounted as a LVM logical volume. Use this command to list the files.
grub> ls (lvm/centos-root)/
Rescue the System
Now that you know where to find the Linux kernel and the root partition you can run commands to boot the system. Tab completion makes it easy to find the correct kernel and matching initial ram filesystem. Use tab completion to find a matching kernel and initramfs.
grub> set root=(hd0,1) grub> linux /vmlinuz-<version>-<arch> root=/dev/mapper/centos-root grub> initrd /initramfs-<version>-<arch>.img grub> boot
What to Submit
Submit a screenshot of the GRUB2 commands you used to boot the system.
Resources
Resources
- man grub2-mkconfig
License
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
Date: 2015-04-27T12:11-0700
Author: Dennis Kibbe
Grub mot de passe
Protéger GRUB par un mot de passe
RedHat
grub2-set-password
Ce script bash va appeler grub2-mkpasswd-pbkdf2
Ce qui crée le fichier suivant
/boot/grub2/user.cfg
GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.92613724ED40696F3D7917C8F257901BDB39790E6F7DB78830E094D8A60EA6E1DAE7366A64866BCBCD8F9A8FAA6A508DAA560DB3B89A682F58E8D8EB56FC5DE7.95FD391CF51F57EA5CCDC6143B5F9262785DA96857368E40FE4C4710D19D07F513333568B7897B75EF9375FA13395335A823AAEE35A89964022810A72446679C
L'utilisateur est : root
Note : pbkdf2 voir ⇒ https://mjg59.dreamwidth.org/66429.html If the PBKDF is either “pbkdf2” or “argon2i” you should convert to “argon2id”
==== Debian Jessie (8) ====
Générer le hash du mot de passe
<code bash>
grub-mkpasswd-pbkdf2
</code>
<code>
Entrez le mot de passe :
Entrez de nouveau le mot de passe :
Le hachage PBKDF2 du mot de passe est grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931
</code>
Ajouter les lignes suivantes
/etc/grub.d/40_custom
<code>
set superusers=“admin”
password_pbkdf2 admin grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931
</code>
Mise à jour la conf
<code bash>
update-grub
</code>
==== Debian Wheezy (7) ====
A tester la même manip que pour Debian Jessie. Mais apparemment la manip diffère.
/etc/grub.d/00_header
<code bash>
cat « EOF
set superusers=“admin”
password_pbkdf2 admin grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931
EOF
</code>
Mise à jour la conf
<code bash>
update-grub
</code>
===== Reset password =====
==== RedHat / CentOS ====
====== Rebooter le serveur. ======
====== Sélectionner la ligne du grub souhaité en la mettant en surbrillance. ======
====== Éditer le grub en tapant e. ======
====== Modifier la ligne de contenant Linux16**, ajouter rd.break enforcing=0 à la fin de la ligne
Lancer l’exécution de la ligne modifié en tapant ctrl + x
A l'invite root, remonter /sysroot en lecture/ecriture : ''%%mount -o remount,rw /sysroot%%''
''%%chroot /sysroot%%''
''%%passwd root%%'' (pour changer le mot de passe)
''%%touch /.autorelabel%%'' (créer autorelabel caché pour reetiqueter les contextes selinux correctement).
''%%/sbin/reboot -f%%''
Grub efi2bios boot RedHat7 UEFI EFI migration vers BIOS P2V
Voilà que je “déghost” ma belle image système dans une VM et ca ne boot pas.
La machine était en UEFI avec des partitions GPT.
Solution : Garder GPT (ne pas passer en MBR), install Grub2 (en plus de Grug2-efi)
Faire l'inverse : (passer à UEFI) :
Depuis une machine en UEFI
yum install grub2 #cp -p /boot/efi/EFI/redhat/grub.cfg /boot/grub2/grub.cfg grub2-mkconfig -o /boot/grub2/grub.cfg sed -i -e 's/linuxefi/linux/g' /boot/grub2/grub.cfg sed -i -e 's/initrdefi/initrd/g' /boot/grub2/grub.cfg grub2-install --modules=part_gpt --target=i386-pc --recheck --boot-directory=/boot /dev/sda --force
Depuis une machine en mode BIOS
Par exemple, sur la VM en bootant sur le DVD de RedHat en mode rescue
rpm -Uvh grub2-2.02-0.29.el7.x86_64.rpm #cp -p /boot/efi/EFI/redhat/grub.cfg /boot/grub2/grub.cfg grub2-mkconfig -o /boot/grub2/grub.cfg grub2-install --modules=part_gpt --target=i386-pc --recheck --boot-directory=/boot /dev/sda --force
Grub boot migration BIOS vers UEFI EFI en MBR
Voir :
Voir aussi :
- systemd-boot (Remplace Grub pour les machines compatibles EFI)
Avant toute chose sauvegarder !
Il existe une version de Grub pour DOS/MBR : grub-pc\ Il existe une version de Grub pour EFI/MBR : grub-efi\ Les deux peuvent être installés sur la même machine.
Testé sous Debian
Création d'une partition /boot/efi en FAT de type “ef” Sur une partition primaire (je n'ai pas testé sur une partition logique) (Taille minimum recommandée : 1MB)
Installation prérequis
#apt-get install efivar syslinux-efi apt-get install efibootmgr grub-efi dosfstools
Pour Debian Wheezy, une façon de faire est de mettre à jour Grub en prenant les paquets de Jessie :
- grub2-common
- grub-common
- grub-efi
- grub-efi-amd64
- grub-efi-amd64-bin
Partitionnement
Créer une partion de VFAT de 20M (1M minimum conseillé) de type “ef” Pour cela il peut être nécessaire de réduire la partition principale à l'aide de GParted (inclus dans systemrescuecd, taper startx pour lancer l’interface graphique)
Ou avec fdisk
fdisk /dev/sda # Part type id (Hex code) : "ef" EFI (FAT-12/16/
Formatage / Montage partition EFI
mkfs.vfat /dev/sda4 mkdir -p /boot/efi
Trouver le UUID de le partition EFI.
fdisk -l | grep EFI blkid /dev/sda4
Remplacer B24E-9BC6 par votre UUID /etc/fstab
#/dev/sda4 #UUID=B24E-9BC6 /boot/efi vfat defaults,uid=0,gid=0,umask=0077,shortname=winnt 0 0 UUID=8343-3610 /boot/efi vfat umask=0077 0 1
Montage de la partition EFI crée.
mount /boot/efi
Installation Grub
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda
Boot auto
Pour booter automatiquement sur Grub
Par défaut le système s'attend à démarrer sur :
- /boot/efi/EFI/boot/bootx64.efi
- /boot/efi/EFI/BOOT/BOOTX64.efi
cd /boot/efi/EFI mkdir boot cp -p debian/grubx64.efi boot/bootx64.efi
Autres
A tester
mkdir -p /boot/grub/locale cp /usr/share/locale/en\@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo
Pb
Err failed to register the EFI boot entry: Invalid argument.
# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda Installing for x86_64-efi platform. grub-install: warning: Cannot read EFI Boot* variables. grub-install: warning: efivarfs_get_variable: read failed: Invalid argument. grub-install: warning: efi_get_variable: ops->get_variable failed: Invalid argument. grub-install: error: failed to register the EFI boot entry: Invalid argument.
Solution
Ajouter --no-nvram ou --removable à grub-install
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda --removable
Grep trouver tous les fichiers ne contenant pas un motif
find . -type f -iname "*.j2" -exec grep -L -e "ansible_managed" {} \;
