Skip to content

Installing GRUB2 to a Partition

Albert Huang edited this page Feb 26, 2017 · 2 revisions

Introduction

For some reason, you want to install GRUB2 to a partition. This may be due to:

  • wanting to use NTLDR (Windows Bootloader) as your main system bootloader,
  • wanting to have an isolated GRUB2 setup for each system

Whatever the reason, this guide is for you! I've seen many resources online talk about the error you may get when you try and install GRUB2 to your partition. In particular, it may look like this:

/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.
                        However, blocklists are UNRELIABLE and their use is discouraged.
/sbin/grub-setup: error: will not proceed with blocklists

Many guides and answers will simply say to just add --force to your GRUB2 grub-install command, and be done with it. However, this isn't enough - although your setup will work initially, you'll eventually discover that one day, your GRUB won't even start! Why?

As it turns out, GRUB2 isn't that smart - it can't really find the code it needs to boot unless it's either in:

  • a really easy-to-find place, or
  • a fixed place that is known and won't change

By default, GRUB2 will usually try to install itself onto your whole HDD. When this occurs, GRUB2 will install core code into the "really easy-to-find place" category - a small bit of space after the MBR of your HDD.

However, when you install GRUB2 into a partition, this space no longer exists. Now, GRUB2 has to depend on the former option: "a fixed place that is known and won't change". This is required because GRUB2's boot sector code is very minimal, and does NOT have enough space or information to go hunting for its needed files by itself. The large Linux kernel can do this, but not a barebones GRUB2 boot sector code. Therefore, GRUB2 relies on something called "blocklists", where it can easily locate on your HDD the particular sector where the core code lives. Think of it as an "address" of sorts - where does this file live in the city of other files?

That said, simply moving a file around could cause this location to change, resulting in your computer failing to boot one day! This is why simply adding --force isn't enough to make things work.

Thankfully, there is another way - ensuring that the blocklists won't change for our important GRUB2 core code file. With that, installing GRUB2 to your partition can now be safe and possible!

Instructions

  1. Open a terminal.
  2. Run: chattr -i /boot/grub/i386-pc/core.img. This will unlock the GRUB2 core code file for updating, if it isn't unlocked already.
  3. Then install GRUB2 with: grub-install --target=i386-pc --debug --force /dev/sdaX.
    • Make sure to replace /dev/sdaX with whatever partition you are trying to install to. Use gparted, parted, or fdisk -l to locate your partition if necessary.
    • Warnings may appear, but GRUB2 should still complete the installation. Any errors will likely indicate another problem besides this.
  4. Run: chattr +i /boot/grub/i386-pc/core.img. This will lock the GRUB2 core code file for updating, and prevent it from ever being moved around on the drive.
  5. That's it! Now your partitioned GRUB2 install will work!

As a reference, your GRUB2 installation output should now look like this:

/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk or to a partition. This is a BAD idea.
/sbin/grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.
                        However, blocklists are UNRELIABLE and their use is discouraged.
Installation finished. No error reported.

Reinstalling/Upgrading GRUB2

Again, you need to follow similar steps:

  1. Open a terminal.
  2. Run: chattr -i /boot/grub/i386-pc/core.img
  3. Perform any GRUB2 installation or package upgrade. For direct installation, see above set of instructions.
  4. Run: chattr +i /boot/grub/i386-pc/core.img
  5. That's it! Now your partitioned GRUB2 install will work!

Reference

The one place that got these steps right - Arch Linux's wiki page! This is pretty much a summary of that particular section of Arch's wiki, with some fluff.

You can’t perform that action at this time.