Convert MBR partition table to GPT (Ubuntu)

Since I have some big disks I started using GUID partition table (GPT). However, Ubuntu (12.10 in my case) installed itself using a oldschool MBR partition table (or MS-DOS partition table). There are ways to create a GPT while installing, but I missed that point… So I asked myself is it possible to convert the MBR partition table to a GUID partition table? Yes, and its quite easy…

In order to use GPT with nowadays BIOS there need to be a BIOS boot partition. However there are two prerequisites which you should check before converting: There need to be space between the MBR and the first partition (most  modern partition utilities start the first partition on the 1 MiB boundary):

$ sudo fdisk -l /dev/sda
....
Device    Boot    Start          End     Blocks  Id  System
/dev/sda1 *        2048    117186559   58592256  83  Linux

As you can see, my partition table meet this requirement. We use this partition to store the BIOS boot partition in it (Note: It should also work with a BIOS boot partition at another place, I never tried that).

Second, GRUB need to be 1.97~beta1 or newer. Check this by issuing

$ grub-install --version

If your system meets this requirements you’re ready to go. First backup the old MBR (and all important data, if you have any on the system in question), just in case anything goes wrong

sudo dd if=/dev/sda of=/root/sda-mbr.dd bs=512 count=1

Now it’s time for gdisk, the GPT equivalent to fdisk for the old partition tables. Starting this utility on a system with the old MBR partition table converts this automatically:

$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.5

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!
***************************************************************

After startup, the (p)rint command shows on my system that the Linux root partition has the ID 1 and the SWAP partition has the ID 5 (since it was an logical partition). Now I created the new BIOS boot partition by pressing (n)ew. I chose the proposed ID 2, changed the first sector to 34, took the proposed last sector of 2047 and set the type to ef02 (BIOS boot partition). In the end I used the (s)ort command to sort the partition ID’s by partition start sector.

Number  Start (sector)  End (sector)            Size  Code  Name
 1                  34          2047      1007.0 KiB  EF02  BIOS boot partition
 2                2048     117186559        55.9 GiB  8300  Linux filesystem
 3           117188608     136718335         9.3 GiB  8200  Linux swap

The (w)rite command writes the partition table to disk. Don’t reboot your system at this time, since it might not boot anymore!

Now the GRUB boot-loader needs to be recreated. The GRUB boot-loader needs more space for its code than the bytes of the MBR. This code is called called “second stage”. Normally this is directly after the MBR. But since the GPT is stored there, the “second stage” code is gone right now! When reinstalling GRUB with our new GPT, GRUB will write the “second stage” into the BIOS boot partition.

 $ sudo grub-install /dev/sda
Installation finished. No error reported.

Now it’s time to shutdown, and, hopefully successful reboot your system 🙂

Read this link for more Information about GPT on BIOS.

  1. On my system, after using the gdisk to alter the GPT, the kernel doesn’t reload the partition table automatically. I needed to issue the partprobe command:

    $ sudo partprobe

  2. where does 34 come from as the first sector, and why?

  3. Honestly, I don’t remember why I chosed 34 back then. It would leave some space (~16KiB) after the MBR. Usually, at this place the GRUB stage 1.5/2 is located, but I don’t think that was the reason…

  4. falstaf, so after that, you boot your hdd (MBR) normally with
    ubuntu 12.10 now with GPT, without any OS reinstallation?

    Without any problem?

  5. Yes 🙂

  6. INCREDIBLE — I’ve been trying to convert a bootable disk to GPT for 2 days now and your guide worked 100%

    So many guides I’ve read that are flooded with useless jargon and paragraph after paragraph of “background info” and other fluff. This blog post is straight to the point and WORKED!!

    Thank you!! Why do all the other guides have all these crazy steps to re-install GRUB when your guide is a single “grub-install” command. Hats off to you sir.

  7. Just wanted to say like the others have, this is the most straightforward guide ever. The others were filled with page after page of fluff, this worked perfectly.

  8. Re. Sector 34, by default there are two copies of the boot sector (sectors 0 and 1) and the GPT partition table itself is allocated 32 sectors, so the next available sector will be 34. Also, OS partitions are aligned on 2048 boundaries by default, so sectors 34-1027 are “dead” and make a good spot to put the UEFI data.

Leave a Comment