O Carregador de Inicialização GRUB2 – Instalação e Configuração
GRUB2 é a versão mais recente do GNU GRUB, o GRand Unified Bootloader. Um bootloader é o primeiro programa de software que é executado quando um computador é inicializado. Ele é responsável por carregar e transferir o controle para o kernel do sistema operacional. No Fedora, o kernel é o Linux. O kernel, por sua vez, inicializa o restante do sistema operacional.
O GRUB2 é o sucessor da versão anterior GRUB (versão 0.9x). A versão original está disponível com o nome de GRUB Legacy.
Desde o Fedora 16, GRUB2 tem sido o bootloader padrão em sistemas x86 BIOS. Para atualizações de sistemas BIOS, a configuração padrão também é instalar GRUB2, mas você pode optar por pular completamente a configuração do bootloader.
Descobrindo o tipo de firmware
Para descobrir qual firmware sua máquina utiliza, execute o seguinte comando:
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS
A saída retornará apenas UEFI ou BIOS, dependendo do firmware que sua máquina utiliza.
Instalando o GRUB2 em um sistema BIOS
Normalmente, o GRUB2 será instalado e configurado pelo instalador, Anaconda, durante o processo de instalação. Provavelmente, você nunca precisará lidar com a instalação manual do GRUB2. No entanto, em situações específicas, você pode desejar instalar o GRUB2 manualmente, especialmente se precisar reparar uma instalação existente do GRUB2 ou desejar alterar sua configuração.
Este procedimento mostra os passos para instalar o GRUB2 no seu Master Boot Record (MBR) do seu disco rígido primário.
-
Certifique-se de que você tenha os pacotes do GRUB2 e o pacote os-prober instalados no seu sistema.
dnf list installed | grep grub -
Para coletar automaticamente informações sobre seus discos e os sistemas operacionais instalados neles, o pacote
os-proberprecisa estar instalado no seu sistema.
-
List block devices available on the system.
lsblk -
Identify the primary hard disk. Usually, it is the
sdadevice. -
Install GRUB2 in the MBR of the primary hard disk.
grub2-install /dev/sda -
Create a configuration file for GRUB2.
grub2-mkconfig -o /boot/grub2/grub.cfg -
Reboot your computer to boot with the newly installed bootloader.
-
The
grub2-mkconfigcommand creates a new configuration based on the currently running system. It collects information from the/bootpartition (or directory), from the/etc/default/grubfile, and the customizable scripts in/etc/grub.d. -
O formato da configuração está mudando ao longo do tempo, e um novo arquivo de configuração pode se tornar ligeiramente incompatível com as versões mais antigas do bootloader. Sempre execute
grub2-installantes de criar o arquivo de configuração comgrub2-mkconfig. -
No Fedora, geralmente é seguro editar manualmente o arquivo
/boot/grub2/grub.cfg. O Grubby no Fedora faz patch na configuração quando uma atualização do kernel é realizada e tentará não fazer outras alterações além do necessário. As alterações manuais podem ser sobrescritas comgrub2-mkconfigquando o sistema é atualizado com o Anaconda. Personalizações colocadas nos arquivos/etc/grub.d/40_customou/boot/grub2/custom.cfgsobreviverão à execução do comandogrub2-mkconfig.
Instalando o GRUB2 em um sistema UEFI
Normalmente, o GRUB2 será instalado e configurado pelo instalador, Anaconda, durante o processo de instalação. Provavelmente, você nunca precisará lidar com a instalação manual do GRUB2. No entanto, em situações específicas, você pode desejar instalar o GRUB2 manualmente, especialmente se precisar reparar uma instalação existente do GRUB2 ou desejar alterar sua configuração.
This procedure shows the steps to install GRUB2 on a UEFI system on Fedora 18 or newer. The procedure consists of four parts.
Creating an EFI System Partition
The UEFI firmware requires to boot from an EFI System Partition on a disk with a GPT label. To create such a partition:
-
List available block devices to find a place to create your ESP. + [source, bash]
lsblk -f -p
-
Create at least a 128 MiB disk partition using a GPT label on the primary hard disk.
gdisk /dev/sdaFor the sake of this procedure, we assume that the created partition is recognized as
/dev/sda1. -
Format the partition with the FAT32 file system.
mkfs.vfat /dev/sda1 -
Create the
/boot/efidirectory as a mount point for the new partition.mkdir /boot/efi -
Mount the partition to the
/boot/efimount point.mount /dev/sda1 /boot/efi -
Proceed to the next part.
Install the bootloader files
In order to use GRUB2 with on UEFI systems, you need to install necessary packages:
-
Re-install the necessary packages. + [source, bash]
dnf reinstall grub2-efi grub2-efi-modules shim-\*
-
If the above command ends with an error, install the packages.
dnf install grub2-efi grub2-efi-modules shim-\*
-
This installs the signed shim and the GRUB2 binary.
Create a GRUB2 configuration
If you already have a working GRUB2 EFI configuration file, you do not need to do anything else.
Otherwise, create the configuration file using the grub2-mkconfig command.
grub2-mkconfig -o /boot/grub2/grub.cfg
-
Under EFI, GRUB2 looks for its configuration in
/boot/efi/EFI/fedora/grub.cfg, however the postinstall script ofgrub2-commoninstalls a small shim which chains to the standard configuration at/boot/grub2/grub.cfgwhich is generated above. To reset this shim to defaults, delete the existing/boot/efi/EFI/fedora/grub.cfgand then reinstallgrub2-common.rm -f /boot/efi/EFI/fedora/grub.cfg dnf reinstall grub2-common -
For newly installed kernels to work,
grubbyexpects/etc/grub2-efi.cfgto be a symlink to the realgrub.cfg(for example/boot/grub2/grub.cfg).
Solving problems with UEFI bootloader
When you power on your system, your firmware will look for EFI variables that tell it how to boot. On running systems, which have booted into the EFI mode and their EFI runtime services are working correctly, you can configure your boot menu with efibootmgr.
If not, shim can help you bootstrap. The EFI program /boot/efi/EFI/BOOT/fallback.efi will look for files called BOOT.CSV in your ESP and will add boot entries corresponding to them. The shim command provides its own BOOT.CSV file that will add an entry for grub2-efi.
During the boot process, you can use the EFI Shell to invoke the fallback.efi profile to boot the system:
-
Enter the boot partition.
fs0:
-
Navigate into the
EFI\BOOTdirectory.> cd EFI\BOOT
-
Invoke the
fallback.efiprofile.> fallback.efi
-
If you have no boot entries at all, then just booting off your disk in UEFI mode should automatically invoke
/boot/efi/EFI/BOOT/BOOTX64.EFI, which will, in turn, invokefallback.efi. -
If you already have incorrect boot entries, you’ll either need to delete them or to modify
BOOT.CSVto create new entries with different names.
Adding other operating systems to the GRUB2 menu
Normally, GRUB2 is preset to boot multiple operating systems during the Fedora installation process. If you can, it is advisable to install non-Linux operating systems first. Then, during the installation process, all those operating systems and their locations will be discovered and properly set.
Adding other records into the GRUB2 menu only means to run grub2-mkconfig command to regenerate the configuration files. During this process, all operating systems known to the system will be added into the configuration. By reinstalling GRUB2, this configuration will be used for further boots.
-
Make sure that the operating systems are on disks, connected to the system.
-
You have the
os-proberpackage installed.
-
Recreate the GRUB2 configuration file.
grub2-mkconfig -o /boot/grub2/grub.cfg -
Install GRUB2.
-
On UEFI systems.
dnf reinstall shim-\* grub2-efi-\* grub2-common -
On BIOS systems, specify the disk where the bootloader should be installed.
grub2-install /dev/sda
-
-
The
grub2-mkconfigcommand will add entries for all operating systems it can find. -
When problems appear, see the GRUB manual to solve issues with booting secondary operating systems.
Configurando a entrada padrão para o GRUB2
Since grub2-mkconfig (and os-prober) cannot estimate which operating system, of those it finds, is to be marked as default, we usually are unable to predict the order of the entries in /boot/grub2/grub.cfg. To change the default layout, we need to set the default based on the name or title.
-
Open
/etc/default/gruband make sure these lines exist in the file.GRUB_DEFAULT=saved GRUB_SAVEDEFAULT=false -
If you needed to change the content of the
/etc/default/grub, apply the changes togrub.cfg.grub2-mkconfig -o /boot/grub2/grub.cfg
-
List all possible menu entries.
grep -P "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2 -
Select one of the displayed options and use it as an argument to set the default menu entry.
grub2-set-default <menuentry> -
Verify the default menu entry
grub2-editenv - list -
Regenerate the GRUB2 configuration file and reinstall the bootloader into the MBR, as described in Adding other operating systems to the GRUB2 menu.
If you understand the risks involved, you can manually modify the /boot/grub2/grub.cfg file. In that case, set the number of the default operating system using the set default variable.
For example:
set default="5"
|
If you edit the configuration file manually, the settings will be overwritten each time the |
Restoring the bootloader using the Live disk
Sometimes, especially after a secondary operating systems has been installed, the master boot record gets damaged which then prevents the original Linux system from booting.
If this happens, it is necessary to reinstall GRUB2 to recreate the original settings. The process not only discovers all installed operating systems, but usually adds them to the GRUB2 configuration files, so they will all become bootable by GRUB2.
-
Get the Fedora Live ISO from fedoraproject.org.
-
Prepare a bootable device using the downloaded ISO, either a CD or a USB.
-
Boot the Fedora live system from the bootable device you have created.
-
Open the terminal.
-
Examine the partition layout and identify the
/bootand the/rootpartition.lsblk -f -p -
If your
/rootpartition is encrypted by LUKS, it must be decrypted.-
Make sure the crypt module is in use.
modprobe dm-crypt -
Decrypt the
/rootpartition (e.g./dev/sda3).cryptsetup luksOpen /dev/sda3 myvolumeThe decrypted device (i.e.
myvolume) will be accessible under/dev/mapper.
-
-
Follow the BTRFS steps (used by default in Fedora 33 or newer).
-
Mount the
/rootpartition.-
For LUKS.
mount /dev/mapper/myvolume /mnt -o subvol=root -
For non-LUKS.
mount /dev/sda3 /mnt -o subvol=root
-
-
-
Follow the LVM steps (used by default before Fedora 33).
-
Scan the LVM volumes for the volume group corresponding to the
/rootpartition.vgscan -
Activate the volume group (e.g.
fedora).vgchange -ay fedora -
Find the logical volume corresponding to
/root.lvsThe logical volume will be accessible under
/dev/mapper. -
Mount the logical volume (e.g.
/dev/mapper/fedora-root) corresponding to the/rootpartition.mount /dev/mapper/fedora-root /mnt
-
-
Mount the
/bootpartition (e.g./dev/sda2).mount /dev/sda2 /mnt/boot -
Mount system processes and devices into the
/rootfilesystem.mount -o bind /dev /mnt/dev mount -o bind /proc /mnt/proc mount -o bind /sys /mnt/sys mount -o bind /run /mnt/run -
On UEFI systems, bind the
efivarsdirectory and mount the EFI system partition (e.g./dev/sda1).mount -o bind /sys/firmware/efi/efivars /mnt/sys/firmware/efi/efivars mount /dev/sda1 /mnt/boot/efi -
Change your filesystem to the one mounted under
/mnt.chroot /mnt -
Re-install GRUB2.
-
On UEFI systems, several packages are required.
dnf reinstall shim-\* grub2-efi-\* grub2-common -
On BIOS systems, specify the disk (e.g.
/dev/sda) where GRUB2 should be installed.grub2-install /dev/sda
-
-
Re-generate the GRUB2 configuration file.
grub2-mkconfig -o /boot/grub2/grub.cfg -
Sync and exit the chroot.
sync && exit -
Reboot the system.
Usando o prompt de inicialização do GRUB2
If improperly configured, GRUB2 may fail to load and subsequently drop to a boot prompt. To boot into the system, follow the steps below.
-
Load the necessary modules to read your system’s partitions (you will also need to load
part_msdosorpart_gpt, depending on your partition table).-
For BTRFS filesystems (Fedora 33 or newer).
grub> insmod btrfs
-
For LVM filesystems (older than Fedora 33).
grub> insmod xfs grub> insmod lvm
-
-
List the drives which GRUB2 sees.
grub> ls
-
Examine the output to understand the partition table of the
/dev/sdadevice. The following example shows a DOS partition table with three partitions.(hd0) (hd0,msdos3) (hd0,msdos2) (hd0,msdos1)
A GPT partition table of the
/dev/sdadevice with four partitions could look like this.(hd0) (hd0,gpt4) (hd0,gpt3) (hd0,gpt2) (hd0,gpt1)
-
Probe each partition of the drive and locate your
vmlinuzandinitramfsfiles.grub> ls (hd0,1)/
The outcome of the previous command will list the files on
/dev/sda1. The partition that contains the/bootdirectory is the correct one. There you will search for the full names of thevmlinuzandinitramfsfiles. -
Follow the Pre-boot setup for BTRFS or the Pre-boot setup for LVM to recover your system.
-
After the pre-boot setup, boot the system.
grub> boot
-
To restore the bootloader’s functionality, regenerate the GRUB2 configuration file and reinstall the bootloader, as described in Adding other operating systems to the GRUB2 menu.
Pre-boot setup for BTRFS filesystems
-
On BIOS systems.
-
Set GRUB2 root to your
/bootpartition. If your/bootpartition is(hd0,msdos1), the command will be.grub> set root=(hd0,msdos1)
-
Next, select the desired kernel. Set the
/rootpartition (e.g./dev/sda2).grub> linux /vmlinuz-5.14.10-300.fc35.x86_64 root=/dev/sda2 ro rootflags=subvol=root
-
-
On UEFI systems.
-
Set GRUB2 root to your EFI system partition. If your EFI system partition is
(hd0,gpt1), use this command.grub> set root=(hd0,gpt1)
-
Next, select the desired kernel. Find the path to
vmlinuzand set the/rootpartition (e.g./dev/sda3).grub> linux (hd0,gpt2)/vmlinuz-5.14.10-300.fc35.x86_64 root=/dev/sda3 ro rootflags=subvol=root
-
Select the RAM filesystem that will be loaded.
grub> initrd (hd0,gpt2)/initramfs-5.14.10-300.fc35.x86_64.img
-
Pre-boot setup for LVM filesystems
-
On BIOS systems.
-
Set GRUB2 root to your
/bootpartition. If your/bootpartition is(hd0,msdos1), use this command.grub> set root=(hd0,msdos1)
-
Next, select the desired kernel. Set
rootto the logical volume that corresponds to the/rootdirectory.grub> linux /vmlinuz-3.0.0-1.fc16.i686 root=/dev/mapper/fedora-root
-
Select the RAM filesystem that will be loaded.
grub> initrd /initramfs-3.0.0-1.fc16.i686.img
-
-
On UEFI systems.
-
Set GRUB2 root to your EFI system partition. If your EFI system partition is
(hd0,gpt1), use this command.set root=(hd0,gpt1)
-
Next, select the desired kernel. Find the path to
vmlinuzand setrootto the logical volume that corresponds to the/rootdirectory.linux (hd0,gpt2)/vmlinuz-3.0.0-1.fc16.i686 root=/dev/mapper/fedora-root
-
Select the RAM filesystem that will be loaded.
initrd (hd0,gpt2)/initramfs-3.0.0-1.fc16.i686.img
-
Booting the system using a configuration file on a different partition
If you end up in GRUB2 boot prompt, it is also possible to boot using a configfile that’s located on another partition, as is often the case with multi-boot systems containing Ubuntu and Fedora. Follow the below procedure if you need to boot from a configuration file on a different partition.
-
Load the necessary modules to read your system’s partitions (you will also need to load
part_msdosorpart_gpt, depending on your partition table).-
For BTRFS filesystems.
grub> insmod btrfs
-
For LVM filesystems.
grub> insmod xfs grub> insmod lvm
-
-
Set GRUB2 root to your
/bootpartition. On UEFI systems, you should set GRUB2 root to the EFI system partition.grub> set root=(hd0,msdos1)
-
Set the path to the configuration file.
grub> configfile /grub2/grub.cfg
-
The hd0,msdos1 line shows the pertinent
/bootpartition, which holds thegrub.cfgfile. The setting may be different on your system. See also Using the GRUB2 boot prompt for more information.
Setting a password for interactive edit mode
If you wish to protect the GRUB2 interactive edit mode with a password, but allow ordinary users to boot the computer, use the grub2-set-password command. You will be prompted for the password, and then will have to confirm it. The encrypted password will be stored in /boot/grub2/user.cfg.
To remove password protection, simply delete the user.cfg file.
Alternately, you can set this up manually:
-
Create the
/etc/grub.d/01_usersfile and write the following lines into the file.cat << EOF set superusers="root" export superusers password root <password> EOF -
Regenerate the GRUB2 configuration file, as described in Adding other operating systems to the GRUB2 menu.
You can encrypt the password by using pbkdf2. Use grub2-mkpasswd-pbkdf2 to encrypt the password, then replace the password line with:
password_pbkdf2 root grub.pbkdf2.sha512.10000.1B4BD9B60DE889A4C50AA9458C4044CBE129C9607B6231783F7E4E7191D8254C0732F4255178E2677BBE27D03186E44815EEFBAD82737D81C87F5D24313DDDE7.E9AEB53A46A16F30735E2558100D8340049A719474AEEE7E3F44C9C5201E2CA82221DCF2A12C39112A701292BF4AA071EB13E5EC8C8C84CC4B1A83304EA10F74
To remove password protection, simple remove the changes you made to the /etc/grub.d/01_users file and regenerate the GRUB2 configuration file, as before.
More details can be found at Ubuntu Help: GRUB2 Passwords.
|
Starting from Fedora 15, the |
Lidando com o Erro "Disquete Ausente"
Foi relatado por alguns usuários que o GRUB2 pode falhar ao ser instalado no setor de inicialização de uma partição se o controlador de disquete do computador estiver ativado no BIOS sem que uma unidade de disquete real esteja presente. Essas situações resultaram em um erro de Disquete Ausente.
Para contornar esse problema, entre no modo de recuperação e siga o procedimento no Instalando o GRUB2 em um sistema BIOS GRUB2, mas use a opção --no-floppy com o comando grub2-install.
# grub2-install <target device> --no-floppy
Using old graphics modes in bootloader
The terminal device is chosen with GRUB_TERMINAL. For more information, see the Grub manual.
Os nomes válidos para saídas de terminal dependem da plataforma, mas podem incluir console (consoles de PC BIOS e EFI), serial (terminal serial), gfxterm (saída em modo gráfico), ofconsole (console Open Firmware) ou vga_text (saída de texto VGA, principalmente útil com o Coreboot).
The default is to use the platform’s native terminal output.
In Fedora, gfxterm is the default options. To get the legacy graphics modes:
-
Edit the
/etc/default/grubfile. -
Set the
GRUB_TERMINALvariable to one of the above mentioned options. -
Regenerate the GRUB2 configuration file and reinstall the bootloader into the MBR, as described in Adding other operating systems to the GRUB2 menu.
Enabling Serial Console in GRUB2
To enable Serial console in grub:
-
Edit the
/etc/default/grubfile. -
Adjust
baudrate,parity,bits, andflowcontrols to fit your environment and cables, see the example.GRUB_CMDLINE_LINUX='console=tty0 console=ttyS0,115200n8' GRUB_TERMINAL=serial GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
-
Regenerate the GRUB2 configuration file and reinstall the bootloader into the MBR, as described in Adding other operating systems to the GRUB2 menu.
Recursos Adicionais
Want to help? Learn how to contribute to Fedora Docs ›