Grub Install and Btrfs Root File System

Recently, the Linux operating system on my laptop no longer boots. It seems that the boot sector for GRUB bootloader is corrupted. I tried to reinstall GRUB with an Ubuntu LiveCD; however, I have encountered some problem related to Btrfs file system (which is the file system for my root mount point /.)

After several attempts, I finally reinstalled GRUB to the hard disk. I feel that it will be good idea to write the steps down, since I can't find any useful suggestion at the time of writing.

Preparation

Before changing anything, it will be better to understand the situation beforehand. Check the status and find the correct device file with sudo fdisk -l command or the GParted GUI tool.

For example, on my laptop, the main hard drive is /dev/SDA, and according to the result of fdisk:

  • /dev/SDA1 is the partition for the EFI firmwares, which is a FAT32 partition.
  • /dev/SDA4 is the root file system for the Linux installation, which is a Btrfs partition.

Besides, I would like to reinstall GRUB to /dev/SDA.

Steps

After gathering the necessary information, we can start our work. In summary, there are three steps:

  1. Mount the file systems.
  2. Switch to the root file system with chroot command and fix the problem inside the chroot.
  3. Unmount the file systems.

I will elobrate them in the following paragraphs.

Mount the File Systems

First, create a directory as a mount point for root file system:

$ sudo mkdir mnt

Second, mount the root file system with:

$ sudo mount -o subvol=@ /dev/SDA4 mnt

Notice that it is required to specify the Btrfs subvolume with the -o subvol= option. We will explain this later. On the default Ubuntu installation, the subvolume for the root file system is named after @.

Third, mount the special file system for the chroot:

$ for i in dev dev/pts sys proc run; do sudo mount --bind /$i mnt/$i; done

Fourth, if you are using UEFI, you should mount the EFI boot partition as well:

$ sudo mount /dev/SDA1 mnt/boot/efi

Change Root and Reinstall GRUB

Now, we can switch to the root file system with the chroot command:

$ sudo chroot mnt

In the chroot, we can reinstall the GRUB bootloader with:

> grub-install /dev/SDA

Remarks: If you see the error messages like "cannot find a device for ...", then it is possible that some partition is not mounted properly. As a result, grub-probe can't detect the devices for the existing configuration.

For example, in my first attempt, I have seen the following error message:

grub-install: error: cannot find a device for /boot/grub (is /dev mounted?)

This is due to the fact that the Btrfs subvolume was mentioned in the configuration file but I didn't specify the subvol= option when I was mounting the root file system in the first attempt. If you encounter similar error messages, double check the /etc/fstab and the mount options.

Now, we can verify the GRUB installation and update the configuration files with:

> grub-install --recheck /dev/SDA

> update-grub

Finally, leave the chroot with:

> exit

Unmount the File Systems

Now, we would like to unmount the file systems so that the changes can be flushed properly.

First, unmount the EFI partition (if available):

$ sudo umount mnt/boot/efi

Second, unmount the special file systems:

$ for i in run proc sys dev/pts dev; do sudo umount mnt/$i; done

Third, unmount the root file systems and delete the mount point:

$ sudo umount mnt

$ sudo rmdir mnt

That's all. You can reboot the computer now! Hoping this will fix your problem.