I had to replace the hardware of a server that was booting in UEFI mode with a board that only supported legacy booting, so I also needed to convert the system back to MBR.

My Scenario

This machine was previously booting in UEFI mode happily, until the motherboard failed. I had relocated the drive into a spare machine I had, but this one couldn't take enough RAM.

The next-best machine I had could definitely take the RAM, but didn't want to boot the UEFI install of debian 11.

To start with, I had

  • 1x Debian 11 UEFI install
  • 1x ESP partition at the start of the GPT disk
  • 1x LVM volume for the rest of the system (root, data etc.)
  • 1x Motherboard that I thought could do UEFI, but refused to

Steps

  1. Boot a debian netinst disk - this can either be a CD, USB stick, or in my case, PXE booted.
  2. Pick the Rescue mode option under Advanced Options
  3. Proceed through the normal debian-installer default questions. Note that Rescue mode is visible in the top left.
  4. The first question that doesn't look like a normal installer question will be asking where the rootfs is. In my case, the LVM LV was called root, and was an easy choice.
    1. If you're not using LVM, it'd probably be the second partition (/dev/sda2) or onwards, since the first should be the ESP
    2. There's no harm in trying them until it gives you a result.
    3. Mount the /boot partition if prompted - I wasn't since I didn't have one.
  5. Now that the rootfs is mounted, we need to chroot into the offline system. Select enter chrooted shell. This will get us into the "unbootable" system.
  6. First, we need to convert the partition table of the system. Run gdisk /dev/your-system-drive. Install if if necessary via apt-get.
    • If you don't have networking here, you can tether an Android device via USB to get it.
  7. Enter p to print the partition table, and quickly check that you are looking at the right disk. If not, quit out and re-try the last step with the right disk.
  8. Enter r for the MBR recovery menu
  9. Enter g to remove the GPT data and revert back to MBR
  10. Enter w to write the changes to disk
  11. Enter q to quit
  12. Update the running system with the changes we have made: partprobe /dev/your-system-drive
  13. With the partition table changed, now we need to make sure the bootloader is configured correctly. Install the MBR version of grub if it's not present: apt-get install grub-pc If you are asked to pick a drive, read ahead.
  14. Install GRUB to the MBR's boot sector: grub-install /dev/your-system-drive
  15. Configure the grub package with the correct disk: dpkg-reconfigure grub-pc
    1. Select your disk here. Don't select any partitions, just use (ex) /dev/sda directly.
    2. This is important because this will ensure that the boot sector is updated correctly when the package is updated.
  16. Update the GRUB config to make sure it's all correct: grub-update
  17. Optionally, edit /etc/fstab to comment out the ESP. I left it, since there's a possibility that I might have to go back to UEFI booting.

Verification

Once all of the above is done, exit out of the shell and pick the reboot option. You should now be able to boot the system like normal.

Theory

A running Linux system doesn't care about the particular partition table type, as long as it can understand it. Linux can understand both MBR and GPT, so we don't need to worry.

Debian doesn't store the partition type anywhere either, so we don't need to worry about updating system config for the conversion alone.

All we did above, was:

  1. Boot another system that allows us to
  2. chroot into the old system (use the booted system as a "surrogate" and run the tools on the offline system like it was running normally)
  3. Convert the disk to MBR
  4. Re-install the bootloader (since GPT/UEFI and MBR systems fundamentally boot differently)
  5. Update the system config to ensure that the system knows where the bootloader should be installed in future.

If this was just a data disk, we only need step 3 here (6 through to 12 above). We wouldn't even need to unmount the disk first.