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
- Boot a debian netinst disk - this can either be a CD, USB stick, or in my case, PXE booted.
- Pick the Rescue mode option under Advanced Options
- Proceed through the normal debian-installer default questions. Note that Rescue mode is visible in the top left.
- 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.
- If you're not using LVM, it'd probably be the second partition (/dev/sda2) or onwards, since the first should be the ESP
- There's no harm in trying them until it gives you a result.
- Mount the /boot partition if prompted - I wasn't since I didn't have one.
- 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.
- 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.
- 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.
- Enter r for the MBR recovery menu
- Enter g to remove the GPT data and revert back to MBR
- Enter w to write the changes to disk
- Enter q to quit
- Update the running system with the changes we have made: partprobe /dev/your-system-drive
- 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.
Install GRUB to the MBR's boot sector: grub-install /dev/your-system-drive- Configure the grub package with the correct disk: dpkg-reconfigure grub-pc
- Select your disk here. Don't select any partitions, just use (ex) /dev/sda directly.
- This is important because this will ensure that the boot sector is updated correctly when the package is updated.
- Update the GRUB config to make sure it's all correct: grub-update
- 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:
- Boot another system that allows us to
- 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)
- Convert the disk to MBR
- Re-install the bootloader (since GPT/UEFI and MBR systems fundamentally boot differently)
- 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.