---
title: "Restore the Linux bootloader in Rescue"
sidebar_label: "Restore the Linux bootloader in Rescue"
sidebar_position: 8
description: "How to restore the Linux bootloader in Rescue recovery and diagnostics mode"
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'

# Restore the Linux bootloader in Rescue

The main bootloader for Linux operating systems is GRUB. If the Linux bootloader has been deleted, corrupted, or stopped working after a disk replacement, you can restore it. To do this, [boot the server in Rescue mode](/dedicated/troubleshooting/boot-to-recovery.mdx), mount the main partitions in the same way they are mounted in the main system, and install the GRUB bootloader.

<Tabs queryString="mount-manually">
  <TabItem value="no-software-raid" default>
    <TabItemLabel>
      Disk partitioning without software RAID
    </TabItemLabel>

    1. [Boot the server into Rescue recovery and diagnostics mode](/dedicated/troubleshooting/boot-to-recovery.mdx).

    2. Determine the OS boot mode:

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

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

    3. Output information about the partitions on the disks:

       ```bash
       lsblk -o +FSTYPE
       ```

       The response will contain information about the partitions on the disks. For example:

       ```bash
       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 disk partitions. In your OS, the partition names may differ.

    4. Identify the partitions that need to be mounted:

       * root partition `/` — usually the largest partition on the disk; in the example in step 3, this is partition `sda4`;
       * boot partition `/boot` — usually a partition with the ext4 filesystem and a size of 512 MB — 1 GB. In the example in step 3, this is partition `sda3`;
       * EFI partition `/boot/efi` — used when booting the OS in UEFI mode. This is a partition with the vfat filesystem. In the example in step 3, this is partition `sda2`.

    5. Mount the root filesystem to the `/mnt` directory:

       ```bash
       mount /dev/<system_partition> /mnt
       ```

       Specify `<system_partition>` — the root partition `/` that you selected in step 4; in the example, this is `sda4`.

    6. Mount the boot partition:

       ```bash
       mount /dev/<boot_partition> /mnt/boot
       ```

       Specify `<boot_partition>` — the boot partition `/boot` on the disk that you selected in step 4; in the example, this is `sda3`.

    7. If using the OS UEFI bootloader, mount the EFI partition:

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

       Specify `<efi_partition>` — the EFI partition `/boot/efi` that you selected in step 4; in the example, this is `sda2`.

    8. Mount the system filesystems:

       ```bash
       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:

       ```bash
       chroot /mnt /bin/bash
       ```

    10. Export the PATH variable:

    ```bash
    export export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH
    ```

    11. Install the GRUB bootloader. The command depends on the OS bootloader you identified in step 2:

        * UEFI:

        ```bash
        grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
        ```

        * BIOS:

        ```bash
        grub-install /dev/<disk>
        ```

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

    12. Create the GRUB configuration file:

        ```bash
        /sbin/grub-mkconfig -o /boot/grub/grub.cfg
        ```

    13. After completing the work, exit the environment:

        ```bash
        exit
        ```

    14. Unmount the system filesystems:

        ```bash
        umount -t devpts devpts /mnt/dev/pts
        umount --bind /dev /mnt/dev
        umount -t sysfs /sys /mnt/sys
        umount -t proc /proc /mnt/proc
        ```

    15. Unmount the filesystem:

        ```bash
        umount /dev/<system_partition> /mnt
        ```

        Specify `<system_partition>` — the root partition `/` that you mounted in step 5; in the example, this is `sda4`.
  </TabItem>

  <TabItem value="disk-with-software-raid">
    <TabItemLabel>
      Disk partitioning with software RAID
    </TabItemLabel>

    1. [Boot the server into Rescue recovery and diagnostics mode](/dedicated/troubleshooting/boot-to-recovery.mdx).

    2. Determine the OS boot mode:

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

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

    3. Check if the system has detected RAID arrays; to do this, output information about the partitions on the disks:

       ```bash
       lsblk -o +FSTYPE
       ```

       The response will contain information about the partitions on the disks and the RAID array configuration. For example:

       ```bash
       NAME    MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT  FSTYPE
       sda       8:0    0 447.6G  0 disk
       ├─sda1    8:1    0   512M  0 part              vfat
       ├─sda2    8:2    0   500M  0 part
       │ └─md0   9:0    0   500M  0 raid1             ext4
       └─sda3    8:3    0 446.6G  0 part
         └─md1   9:1    0 446.6G  0 raid1             ext4
       sdb       8:16   0 447.6G  0 disk
       ├─sdb1    8:17   0   512M  0 part              
       ├─sdb2    8:18   0   500M  0 part
       │ └─md0   9:0    0   500M  0 raid1             ext4
       └─sdb3    8:19   0 446.6G  0 part
         └─md1   9:1    0 446.6G  0 raid1             ext4
       ```

       Here `md0`, `md1` are RAID arrays on disk partitions. In your OS, the RAID array names may differ.

    4. If the output of `lsblk` in step 3 shows RAID arrays, skip this step. If there are no RAID arrays, run the assembly of RAID arrays:

       ```bash
       mdadm --assemble --scan
       ```

    5. Output information about the partitions on the disks and the RAID array configuration:

       ```bash
       lsblk -o +FSTYPE
       ```

       The response will contain information about the partitions on the disks and the RAID array configuration. For example:

       ```bash
       NAME    MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT  FSTYPE
       sda       8:0    0 447.6G  0 disk
       ├─sda1    8:1    0   512M  0 part              vfat
       ├─sda2    8:2    0   500M  0 part
       │ └─md0   9:0    0   500M  0 raid1             ext4
       └─sda3    8:3    0 446.6G  0 part
         └─md1   9:1    0 446.6G  0 raid1             ext4
       sdb       8:16   0 447.6G  0 disk
       ├─sdb1    8:17   0   512M  0 part              
       ├─sdb2    8:18   0   500M  0 part
       │ └─md0   9:0    0   500M  0 raid1             ext4
       └─sdb3    8:19   0 446.6G  0 part
         └─md1   9:1    0 446.6G  0 raid1             ext4
       ```

       Here `md0`, `md1` are RAID arrays on disk partitions. In your OS, the RAID array names may differ.

    6. Identify the RAID partitions that need to be mounted:

       * root RAID partition `/` — usually the largest partition on the disk; in the example in step 5, this is array `md1`;
       * boot RAID partition `/boot` — usually a partition with the ext4 filesystem and a size of 512 MB — 1 GB. In the example in step 5, this is array `md0`;
       * EFI partition `/boot/efi` — used when booting the OS in UEFI mode. This is a partition with the vfat filesystem. In the example in step 5, this is partition `sda1` and, if the EFI partition was cloned to the second disk, `sdb1`.

    7. Mount the root filesystem to the `/mnt` directory:

       ```bash
       mount /dev/<system_raid_array> /mnt
       ```

       Here `<system_raid_array>` — the root RAID partition that you selected in step 6; in the example, this is `md1`.

    8. Mount the boot RAID partition:

       ```bash
       mount /dev/<boot_raid_array> /mnt/boot
       ```

       Specify `<boot_raid_array>` — the boot RAID array `/boot` that you selected in step 6; in the example, this is `md0`.

    9. If using the OS UEFI bootloader, mount the EFI partition:

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

       Specify `<efi_partition>` — the EFI partition `/boot/efi` that you selected in step 6; in the example, this is `sda1`.

    10. Mount the system filesystems:

        ```bash
        mount --bind /sys /mnt/sys
        mount --bind /proc /mnt/proc
        mount --bind /dev /mnt/dev
        mount -t devpts devpts /mnt/dev/pts
        ```

    11. Connect to the environment:

        ```bash
        chroot /mnt /bin/bash
        ```

    12. Export the PATH variable:

        ```bash
        export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
        ```

    13. Install the GRUB bootloader. The command depends on the OS bootloader you identified in step 2:

        * UEFI:

        ```bash
        grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
        ```

        * BIOS — install the bootloader on each disk from which the OS will boot:

        ```bash
        grub-install /dev/<disk_1>
        grub-install /dev/<disk_2>
        ```

        Specify `<disk_1>`, `<disk_2>` as the system disks where the OS is installed and the root partition `/` is located; in the example in step 5, these are `sda` and `sdb`. Depending on the number of devices in the RAID, there may be more disks.

    14. Create the GRUB configuration file:

        ```bash
        /sbin/grub-mkconfig -o /boot/grub/grub.cfg
        ```

    15. After completing the work, exit the environment:

        ```bash
        exit
        ```

    16. If your system has only one EFI partition, skip this step. If the EFI partition on your system is cloned across several disks, install the GRUB bootloader on each disk:

        16.1. Unmount the EFI partition:

        ```bash
        umount /mnt/boot/efi
        ```

        16.2. Mount the EFI partition:

        ```bash
        mount /dev/<efi_partition_2> /mnt/efi2
        ```

        Specify `<efi_partition_2>` — the EFI partition on the second disk; in the example in step 5, this is `sdb1`.

        16.3. Mount the system filesystems:

        ```bash
        mount --bind /sys /mnt/sys
        mount --bind /proc /mnt/proc
        mount --bind /dev /mnt/dev
        mount -t devpts devpts /mnt/dev/pts
        ```

        16.4. Connect to the environment:

        ```bash
        chroot /mnt /bin/bash
        ```

        16.5. Install the GRUB bootloader:

        ```bash
        grub-install --target=x86_64-efi --efi-directory=/efi2 --bootloader-id=GRUB
        ```

        16.6. Create the GRUB configuration file:

        ```bash
        /sbin/grub-mkconfig -o /boot/grub/grub.cfg
        ```

        16.7. Exit the environment after work is complete:

        ```bash
        exit
        ```

    17. Unmount the system filesystems:

        ```bash
        umount -t devpts devpts /mnt/dev/pts
        umount --bind /dev /mnt/dev
        umount -t sysfs /sys /mnt/sys
        umount -t proc /proc /mnt/proc
        ```

    18. Unmount the filesystem:

        ```bash
        umount /dev/<system_raid_array> /mnt
        ```

        Specify `<system_raid_array>` — the root RAID partition `/` that you mounted in step 7; in the example, this is `md1`.
  </TabItem>
</Tabs>

<Formbricks />
