location of grub boot loader in HDD

I want to know the exact location of grub boot loader inside HDD. I know grub-install /dev/sda to install grub but does that mean it install grub across whole HDD.?

The answer is: it depends. On a traditional MBR disk the first part (boot.img) is written to the boot sector, the next part usually in the sectors between the boot sector and the first partition. There is typically at least 63 sectors of unused space before the first partition. The rest of GRUB is stored in files in /boot/grub. On a GPT (GUID Partition Table) disk, but with a non-UEFI (=BIOS) firmware, a BIOS Boot Partition is used for storing the second part of GRUB. On a UEFI system, GRUB is loaded by the firmware from grubx64.efi on the ESP (EFI System Partition).

Answered By: Johan Myréen

To set context, we’re talking about the first stage of the boot process for Linux. Ultimately, GRUB at this stage is trying to do the minimum required to find and access the boot partition and then load a kernel and point it at the initial ramdisk image (initrd) that contains the heavy lift stuff for the operating system.

If it’s an MBR disk on an MBR system (classic scenario), the first 440 bytes of sector 0 are dedicated to boot code. The tail end of that first sector contains a few other things like the table of partitions. GRUB has a small piece of code in there, but by default it’s going to jump to sectors 1 and beyond for more code.

This area is typically not included in partitions. For legacy reasons (CHS addressing, pre-LBA), systems typically used to place the first partition at sector 63. Later systems started trying to align partitions more optimally for the underlying media instead of assuming every sector was equal. Initially this would have been for RAID arrays and later for the benefit of AF hard disks and page/block sizes in solid-state disks, so that typically became 1 MiB.

The main point is that this typically unused area is used by GRUB. If you were to do an i386-pc install of GRUB on a disk today, it will use the next 109 sectors for code if it’s available. (ie the first 440 bytes of sector 0 + sectors 1 through 110 in their entirety) The exact number of sectors it uses will change as GRUB changes and will also depend on which modules it decides it needs in there (such as a filesystem driver for the boot partition, etc).

If it’s a GPT disk on an EFI system, GRUB doesn’t need to live at all in sector 0. A "protective" MBR is often present, but it isn’t used by the system during startup if it’s running in an increasingly common pure EFI mode. Instead, the system’s BIOS (a misnomer, but it gets the idea across) looks for all disks it knows how to access and then checks them for GPT partition tables, then in those for disks with a partition having the ESP flag set, and of those ESP partitions formatted in a filesystem that it knows how to parse. FAT32 support is mandatory, but sometimes a BIOS will include optional drivers for others like NTFS.

Once it finds an ESP partition it can read, it checks for a subfolder containing some EFI boot files. If it’s there, you’ll get an entry in your boot order list with that folder name. The BIOS will simply execute the bootloader program in that folder. Being that it’s a file on a filesystem, the details of where that bootloader physically lives on the media no longer matter.

Answered By: Particle
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.