Skip to main content

Restore the Linux bootloader in Rescue

Last update:

The primary boot loader for Linux operating systems is GRUB.If the Linux boot loader has been deleted, corrupted, or stopped working after a disk replacement, you can recover the boot loader.To do this, boot the server in Rescue mode, mount the primary partitions the same way they are mounted on the primary system, and install the GRUB boot loader.

  1. Boot the server in Rescue recovery and diagnostic mode.

  2. Determine the boot mode of the OS:

    [ -d /sys/firmware/efi ] && echo "UEFI" || echo "BIOS"

    The response will show information about the OS boot mode — BIOS or UEFI.

  3. Print information about the partitions on the available disks:

    lsblk -o +FSTYPE

    A list of disks with partitions will appear in the response. For example:

    NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS FSTYPE
    sda 8:0 0 25G 0 disk
    ├─sda1 8:1 0 1M 0 part
    ├─sda2 8:2 0 512M 0 part vfat
    ├─sda3 8:2 0 1G 0 part ext4
    └─sda4 8:3 0 23,5G 0 part ext4
    sdb 8:16 0 8G 0 disk
    ├─sdb1 8:17 0 4G 0 part ext4
    └─sdb2 8:20 0 4G 0 part xfs

    Here sda1, sda2, sda3, sda4, sdb1, sdb2 are partitions on the disks.

  4. Determine the partitions you want to mount:

    • root partition / is usually the largest partition on the disk, in the example in step 3 it is partition sda4;
    • boot partition /boot — usually a partition with ext4 file system and a size of 512 MB to 1 GB. In the example in step 3, this is the sda3 partition;
    • EFI partition /boot/efi — is used when booting the OS in UEFI mode. It is a partition with a vfat file system. In the example in step 3, this is the sda2 partition.
  5. Mount the root file system to the /mnt directory:

    mount /dev/<system_partition> /mnt

    Specify <system_partition> is the root partition / that you selected in step 4, in the example it is sda4.

  6. Mount the boot partition:

    mount /dev/<boot_partition> /mnt/boot

    Specify <boot_partition> is the /boot partition on the disk you selected in step 4, in the example it is sda3.

  7. If you are using a UEFI OS loader, mount the EFI partition:

    mount /dev/<efi_partition> /mnt/boot/efi

    Specify <efi_partition> is the EFI /boot/efi partition you selected in step 4, in the example it is sda2.

  8. Mount the service file systems:

    mount --bind /sys /mnt/sys
    mount --bind /proc /mnt/proc
    mount --bind /dev /mnt/dev
    mount -t devpts devpts /mnt/dev/pts
  9. Connect to the environment:

    chroot /mnt /bin/bash
  10. Export the PATH variable:

export export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH
  1. Install the GRUB boot loader.The command depends on the OS boot loader you defined in step 2:

    • UEFI:
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
    • BIOS:
    grub-install /dev/<disk>

    Specify <disk> is the system disk where the OS is installed and the root partition / is located, in the example in step 3 this is sda.

  2. Create a GRUB configuration file:

    /sbin/grub-mkconfig -o /boot/grub/grub.cfg
  3. Exit the environment when the work is complete:

    exit
  4. Unmount the file system:

    umount /dev/<system_partition> /mnt

    Specify <system_partition> is the root partition / that you mounted in step 5, in the example it is sda4.