Where is grub installed and do I need a new one for a separate linux installation?
Naming convention:
GRUB (some of it) stays in the MBR.
GRUB (rest of it) are several files that are loaded, from
/boot/grub
(for example: that nice image that appears as a background in GRUB is not stored on the MBR)
Notes:
The answer is considering an MBR setup, GRUB can be used in other setups.
In an EFI setup things get hairy, GRUB can be used, but so can be the kernel itself as its own EFI stub.
GRUB (some of it) is installed in the MBR. The MBR are the first 512 bytes on a disk. The MBR is also used by the partition table of the disk, therefore GRUB itself has somewhat less space than the 512 bytes.
The GRUB (some of it) inside the MBR loads a more complete GRUB (rest of it) from another part of the disk, which is defined during GRUB installation to the MBR (grub-install
). Since the MBR GRUB needs to find its counterpart on disk, which normally resides on /boot
, the partition where the main GRUB resides cannot be too far from the partition table (often 512MB
but may vary).
It is very useful to have /boot
as its own partition, since then GRUB for the entire disk can be managed from there.
What does it mean:
The GRUB on MBR can only load one GRUB (the rest of it) from disk.
That specific GRUB (the rest of it) on disk must be configured to find all OSes on the machine.
The command grub-mkconfig -o /boot/grub/grub.cfg
runs os-prober
(if it can find it) which scans all partitions and produces a grub.cfg
pointing to all the OSes.
Therefore if you have several partitions with /boot
(or the MS windows equivalents, I do not know them but os-prober
knows) the os-prober
will find them and create grub.cfg
accordingly. Running grub-install
install a GRUB (some of it) on the MBR that points to the GRUB of the current running OS with the current grub.cfg
.
What does this mean:
You only need a single GRUB for the entire system.
You can have different GRUBs on different disks (since they have distinct MBRs) but that only makes sense if you plan to remove the disk.
You can manage the boot of all OSes from a single GRUB installation.
On a single disk you shall always run
grub-install
from a single OS only! That's important, otherwise you will keep overwriting your config.
Just to clarify: You're using grub2
, correct?
In case Grub2 is installed in its own boot partition, you don't need to change anything. When it is not (which would be really strange), then you could technically retain the grub.cfg
, and if your distribution has it, also /etc/grub.d
.
/boot
should be it's own partition, though.
If you run mount
and see something like this:
/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,stripe=4,data=ordered)
You have Grub2 installed in its own partition, as it should be. Normally, when installing a new distro, the wizzard will usually guide you through the installation process, and even figure out what partitions contain what OS-es, if applicable. The process on Arch is a bit more tedious but very well documented.
In case horribleness ensues
Download GParted, put it on a USB stick, boot from it, and mount the /boot
partition.
Then simply run as root (or with sudo
):
grub-mkconfig -o /boot/grub/grub.cfg
Grub2 should be able to autodetect your partitions and act accordingly.
(At this point I'm not sure if partitions need to be all mounted for Grub2 to detect the OS-es on them, though)
If you tried to install Windows in the mean time, it might be Windows overwrote the boot loader. To fix that, simply run as root (or with sudo
):
grub-install /dev/sdX
where /dev/sdX
is the drive where your boot partition resides.
A little bit of explanation:
Grub2 is a Stage-2 boot loader. Meaning it's not exactly an operating system, but it's much more complex, than a simple chainloader, which would fit into the first 446bytes on your primary partition (the rest of the first sector is partition table, flags, etc.).
The MBR contains just the code to execute Grub2 code which resides on your /boot
partition. Grub2 then either chainloads into another partition (for booting windows for instance, as it installs its own boot loader), but it can also run code (things like memtest
), or access and run a kernel and module image (vmlinuz
).
What's important to note, is that in both instances Grub2 is unloaded from memory once a kernel starts running, or another partition has been chainloaded.
Grub is not tied to a particular OS. It can reside on its private partition, though usually it's installed along the main file system. But for it to be able to boot your system(s), it needs to know about them. So you need to update Grub after any important changes in the systems present on your machine like a new OS installed or a kernel upgrade. If you delete the only Grub there is, you will not boot your system(s). There can be many Grubs on one machine, but only one is main, or active. Apart from Grub, there are other boot loaders that can be used (instead of Grub). Windows has its own boot loader, which can be set as the main one for the whole machine.