Software & Hardware Mix: > Linux / Unix / Android

reconfigure grub automatically re-add all boot entries


I like the way the add the small script to scan for any mountable devices... Then add them all! I'm testing this on Solux linux but so far so good - up to the reboot :) And...




When you install Windows, Windows assumes it is the only operating system (OS) on the machine, or at least it does not account for Linux. So it replaces GRUB with its own boot loader. What you have to do is replace the Windows boot loader with GRUB. I've seen various instructions for replacing GRUB by mucking around with GRUB commands or some such, but to me the easiest way is to simply chroot into your install and run update-grub. chroot is great because it allows you to work on your actual install, instead of trying to redirect things here and there. It is really clean.

Here's how:

    Boot from the live CD or live USB, in "Try Ubuntu" mode.
    Determine the partition number of your main partition. GParted (which should already be installed, by default, on the live session) can help you here. I'm going to assume in this answer that it's /dev/sda2, but make sure you use the correct partition number for your system!

    Mount your partition:

--- Code: --- sudo mount /dev/sda2 /mnt  #Replace sda2 with your partition number
--- End code ---

    Bind mount some other necessary stuff:

--- Code: --- for i in /sys /proc /run /dev; do sudo mount --bind "$i" "/mnt$i"; done
--- End code ---

    If Ubuntu is installed in EFI mode (see this answer if you're unsure), use GParted to find your EFI partition. It will have a label of EFI. Mount this partition, replacing sdXY with the actual partition number for your system:

--- Code: ---sudo mount /dev/sdXY /mnt/boot/efi
--- End code ---

    chroot into your Ubuntu install:

--- Code: ---sudo chroot /mnt
--- End code ---

    At this point, you're in your install, not the live session, and running as root. Update grub:

--- Code: ---update-grub
--- End code ---

    If you get errors or if going up to step 7 didn't fix your problem, go to step 8. (Otherwise, it is optional.)

    Depending on your situation, you might have to reinstall grub:

--- Code: ---grub-install /dev/sda
--- End code ---
    update-grub # In order to find and add windows to grub menu.

    If everything worked without errors, then you're all set:

--- Code: ---exit
--- End code ---
--- Code: ---sudo reboot
--- End code ---

    At this point, you should be able to boot normally.

If you cannot boot normally, and didn't do step 8 because there were no error messages, try again with step 8.

    Sometimes giving GRUB2 the correct configuration for your partitions is not enough, and you must actually install it (or reinstall it) to the Master Boot Record, which step 8 does. Experience helping users in chat has shown that step 8 is sometimes necessary even when no error messages are shown.

Same as below, but for btrfs root file systems...

 Grub Install and Btrfs Root File System
Sun 17 May 2015
Tags linux grub
Posted by Logan   

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.

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.

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

    Mount the file systems.
    Switch to the root file system with chroot command and fix the problem inside the chroot.
    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.

What is your actual question about the grub boot menu?
I would like to add one more thing.
Every time you update your Windows partition, the Windows boot menu is rewritten and you lose the access to your Linux Distro.
Id that the reason why you posted this post in the first place?


--- Quote from: RuskinF on 15. June 2020., 12:59:27 ---
Id that the reason why you posted this post in the first place?

--- End quote ---

Honest too god... TL;DR xD

Hoa Dao:
Your article is very good and useful, thank you for sharing, bk8 hopes that next time you will have more good articles to send to all readers.


[0] Message Index


Go to full version