GRUB consists of several parts: there's a very small primary boot loader that fits wholly inside disk sector 0 (the MBR), a larger secondary loader (anywhere from 30KiB to around 50KiB depending on which modules are included) that understands filesystems and LVM and mdraid and other complicated stuff, and then the various bits and pieces in /boot/grub in the boot filesystem.
Because the primary loader has to fit inside a single 512-byte disk sector that also includes a partition table, there simply isn't room for code that does anything as complex as navigating filesystems. All the primary loader can do is read a sequential range of disk blocks into RAM and jump to the code contained in those. So the secondary loader needs to reside in some such range.
The usual place for the secondary loader on an MBR-partitioned disk is in disk sectors 1..62, usually left otherwise unused by traditional MBR-style partitioning; GRUB calls this "embedding". If the GRUB secondary loader ends up bigger than 31KiB (which is more likely to happen with GRUB2 due to its extra capabilities and modular design), it won't fit in this space and GRUB installation will fail. The best fix is to start the first partition further along the disk than its traditional starting place on sector 63. You
can install GRUB to a partition and avoid embedding, but this relies on /boot/core.img occupying an unchanging range of sequential disk sectors inside the /boot filesystem which is usually pretty hard to guarantee and is, as grub-setup says, a BAD idea.
GPT partitioning uses more space for the partition table than MBR partitioning does. There is still a "protective" MBR on disk sector 0, which has room for GRUB's primary stage as well as a dummy MBR partition table, but the partition table proper occupies disk sectors 1..33 and -33..-1 (where -1 is the last sector on the disk). GRUB's secondary loader can't be embedded starting on sector 1 on a GPT-partitioned disk because doing so would destroy the partition table.
However, GPT also defines a special partition type specifically intended for use by boot loaders. So rather than continue the undisciplined MBR tradition of stuffing the secondary loader into some ill-defined region of unpartitioned space, the GRUB2 installer requires one of those special partitions to be defined when installing to a GPT disk. Note that this is
not the same thing as a /boot partition - you can have one of those as well if you want, or /boot can simply be a directory inside the root filesystem; in any case you still need a chunk of non-filesystem space to hold the GRUB stage responsible for finding /boot in the first place.
Fortunately this is a pretty easy thing to add in most cases, because GPT partitions tend to be built on 1MiB boundaries by default rather than the (fake) track and cylinder boundaries traditional for MBR partitioning, so there's usually some unused space between disk blocks 34 and 2047. You can use parted to check for that and make a little partition in there:
- Code: Select all
sudo parted /dev/sda #substitute your own boot disk's device name here
unit s
print free
This will show you if your existing partition table has the required chunk of free space starting on sector 34. If it does, continue with the parted session:
- Code: Select all
mkpart grub 128s 2047s #might as well align it somewhat
You will probably see parted complain about non-optimal alignment here. It does that any time you create a partition that doesn't start on a 2048 sector (1MiB) boundary. The warning is safe to ignore. I like to start the partition at sector 128 instead of sector 34 because aligning on a 64KiB boundary is nicer for 4KiB-sectored ("Advanced Format") disks and RAID controllers, but you can use 34s instead of 128s if you prefer.
- Code: Select all
print
will now show you the state of the partition table, and you need to note the partition number that's been assigned to your new partition. I'll assume here that it was partition 7:
- Code: Select all
set 7 bios_grub on #mark this as a bootloader partition
print
quit
You should now be able to run
sudo grub-install /dev/sda and have it succeed.
Incidentally, if you use one of the automatic or guided partitioning options in the Debian installer and pick GPT partitioning, the installer will automatically create the required bios_grub partition. You might need to
work around a bug to make the subsequent GRUB installation step succeed, but it does build the partition table properly.