1. Overview

In this article, we’ll discuss how we can convert an MBR disk to GPT. We’ll do a step-by-step walkthrough of the conversion process using tools like gdisk and mkfs.

2. Convert MBR Partition to GPT

Before attempting this process, we should back up our data because there is a high risk of losing the data if some of the steps aren’t carried out correctly.

In the following sections, we’ll use the gdisk tool for this purpose because it’s available on most major Linux distributions.

2.1. gdisk

gdisk is an interactive command-line tool for managing the GPT partition table. It should be installed on most Linux distributions by default because it’s part of the fdisk package.

Before we move on, we should first check whether our storage device is already using GPT:

$ gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.9.1

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

Found valid GPT with protective MBR; using GPT.
...

We can notice that GPT is already “present” on the device, so we don’t need to convert it. However, if the device is using MBR, then we’ll be able to convert it to GPT.

2.2. Creating the Partition Table

This step is completely optional if we want to retain the data on our disk. However, if we want to create a clean partition table that uses GPT, we can use the “o” operator to create a new GPT.

Note that this will override the existing partition table scheme and delete all the existing partitions on the disk:

$ gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.9.1

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
...
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Once the partition table is created, we can create the required partitions.

2.3. Creating the BIOS Boot Partition

Let’s start by creating a partition that we’ll use for BIOS boot:

$ gdisk /dev/sda
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (2048-625142414, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-625142414, default = 625142414) or {+-}size{KMGTP}: +1M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): ef02

Let’s examine our interactions with the above gdisk command:

  • We entered the n operator to create a new partition
  • We selected “1” as this is going to be the first partition in the table
  • The first sector of our partition is going to be 2048, which we can select by simply pressing Enter
  • The last sector is going to be 1 MB long, which gdisk will calculate for us
  • The partition type is ef02, which is a short code for “Bios boot partition”

Afterward, we can enter the w operator to write changes to the disk. Once the partition is created, we’ll inform the system of the partition changes to /dev/sda:

$ partprobe /dev/sda

2.4. Installing the GRUB Bootloader

After the successful creation of the partition, we can now re-install the GRUB bootloader on /dev/sda:

$ grub-install /dev/sda

Once GRUB is installed, we can reboot our system to make sure everything is working so far.

At this point, our disk is using GPT without UEFI. So, in the next section, we’ll add UEFI support.

2.5. Adding the EFI System Partition

Now that we have the partition table set up, we can create a new EFI partition for the EFI System (ESP). Usually, we create the EFI System Partition with a size between 128 MB to 512 MB.

Let’s launch gdisk and create the partition:

$ gdisk /dev/sda
...
Command (? for help): n
Partition number (4-128, default 4):
First sector (625141760-625151432, default = 625141760) or {+-}size{KMGTP}:
Last sector (625141760-625151432, default = 625151432) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): ef00
Changed type of partition to 'EFI system partition'

Command (? for help): c
Partition number (1-4): 4
Enter name: EFI-system

Command (? for help): w
  • we created the partition at the end of the table with the remaining available size on the disk
  • we used the ef00 HEX code to change the partition type to EFI System Partition
  • we assigned the “EFI-system” label to the partition using the “c” operator

Finally, we wrote the changes to the disk using the “w” operator. Now, we’ll need to format the partition and add its entry in the /etc/fstab file.

2.6. Building the ESP

We’ll use the mkfs utility to format the partition as vfat:

$ mkfs -t vfat /dev/sda4

Alternatively, we can also use the alias mkfs.vfat. One thing to remember is that this process is irreversible, so make sure to format the correct partition. A good utility for this purpose is lsblk.

2.7. Adding the ESP Entry in fstab

Once the partition has been successfully formatted, we can add the entry for it in the /etc/fstab file:

/dev/disk/by-partlabel/EFI-system  /boot/efi  vfat  defaults  0  2

Here, we used the partition label as the identifier and specified the mount point to be /boot/efi. The mount points are automatically created if they do not exist. Therefore, we don’t need to create it.

Finally, we specified the partition type and defaults for the option.

2.8. Installing the GRUB EFI Bootloader

With the above steps complete, we can now install the GRUB EFI bootloader on the disk. However, we first need to install the grub-efi-amd64 package from the official package repository using a package manager:

$ sudo apt install grub-efi-amd64 -y

The package may be available under a different name on different repositories.

Before we install GRUB EFI on the disk, let’s mount the ESP partition:

$ mount /boot/efi

Now, we install the EFI bootloader:

$ grub-install --target=x86_64-efi --bootloader-id=GRUB --efi-directory=/boot/efi

2.9. Final Steps

We’re ready to reboot our system. However, we need to update our GRUB configuration. For that purpose, we’ll use the grub-mkconfig utility:

$ grub-mkconfig --output=/boot/grub/grub.cfg

If we have any other operating system, we’ll need to install the os-prober package from the repository and re-create the configuration file. It will automatically create GRUB entries for the other operating systems.

With the final step completed, we are ready to reboot our system.

Mind that before booting into the system, we’ll need to change the boot method to UEFI in our system’s BIOS settings.

3. Conclusion

In this article, we took a step-by-step approach to convert an MBR disk to a GPT disk with UEFI support. We used the gdisk utility to create the partition table and the EFI partition.

Additionally, we also covered the installation of the EFI bootloader on our disk.