Converting between GPT and MBR hard drive without losing data

You can convert from GPT to MBR and MBR to GPT without data loss (I have tried that) with gdisk in Linux.

Use at your own risk

Run command gdisk /dev/sdx with sdx as per your HDD partition

GPT fdisk (gdisk) version 1.0.1

Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
***************************************************************


Command (? for help): 

MBR to GPT

Enter w to write GPT partition on disk.

Press y to confirm your choice.

GPT to MBR

Enter r to enter in recovery and transformation options.

Enter g to convert GPT to MBR partition.

For Information

You can check table by command p.

Warning: You will lose your boot loader (Ex. GRUB)

You can check if your partition is GPT or MBR now gdisk /dev/sdx with sdx as per your HDD partition in Partition table scan(p)


Making your drive bootable

This is an enhancement to the information provided by Krunal and clarkttfu with more details on the steps to create a BIOS boot partition and install grub to it.

If you are changing the partition table on a a boot drive you will need to create a new "BIOS boot partition" for grub to store the bootloader in. These examples use the drive /dev/sda which will usually be the boot drive.

First, validate that there is space before the current first partition to support a boot partition, fisk -l should show that the first partition starts at sector 2048:

johnf@ubuntu:~$ sudo fdisk -l /dev/sda
[...]
Device     Boot  Start       End   Sectors  Size Id Type
/dev/sda1  *      2048    499711    497664  243M 83 Linux
/dev/sda2       501758 125829119 125327362 59.8G  5 Extended
/dev/sda5       501760 125829119 125327360 59.8G 8e Linux LVM

If it does then you have the space required to create the partition. If it doesn't you cannot follow these instructions and have a bootable system.

Use gdisk to convert the partition to gpt, you can now create a new partition for your MBR, run sudo gdisk /dev/sd, enter n to create a new partition, accept the proposed partition number, you should be able to select a first sector of 34, set the partition type of ef02:

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-4294967262, default = 4294922240) or {+-}size{KMGTP}: 34
Last sector (34-2047, default = 2047) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): ef02
Changed type of partition to 'BIOS boot partition'

You can now write your partition table with w. Run partprobe again and then install grub:

johnf@ubuntu:~$ sudo partprobe
johnf@ubuntu:~$ sudo grub-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.

You should now be able to reboot your machine without issue.


As Krunal said, gdisk will ruin your grub, to avoid rescue mode:

If boot from BIOS, you'd better create a BIOS boot partition before you write and quit gdisk. (Otherwise a EFI partition is required if boot from UEFI).

And you grub-install /dev/sda to fix your bootloader then reboot and good luck.

i.e. I used the 2048 sectors between sda1 and sda5, exactly 1MB for BIOS boot partition:

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          999423   487.0 MiB   8300  Linux filesystem
   5         1001472        41940991   19.5 GiB    8E00  Linux LVM