GRUB – Why Doesn’t Installing GRUB on MBR Destroy the Partition Table

grubhard drivembr

I have read that my partition table is stored in the MBR. And write operations are performed sector-wise i.e, even if you want to change just a few bytes of a sector you need to overwrite entire contents of the sector. So when installing GRUB on MBR, why doesn't it destroy my partition table?

Best Answer

The software reads the original sector; updates it in memory; then writes out the updated sector.

On Linux, this is actually done by the OS itself, so GRUB doesn't need to worry about sectors – it can just issue a 440-byte write, and the OS will read/modify/write the whole 512-byte sector accordingly. (The job of an OS is to abstract away the inconvenient hardware details.) But if the OS didn't do this, then GRUB could still do the same read/modify/write thing on its own.

This "read/modify/write" pattern is not limited to just the MBR – it's also how you're able to change individual bytes within a file, even though they're also stored in disk sectors. The OS will read the corresponding sector from disk, update it with your changes, then write the new sector back.

Related Question