About Me

My photo
I know the last digit of PI

Sunday, November 20, 2016

Convert MBR to GPT and BIOS bootable to UEFI bootable without data loss

Convert MBR to GPT

Instructons for Windows users

  1. Clone the disk in order to have backup data. You can skip this step but if somethings goes wrong the data on the disk may be lost
  2. Identify the disk which will be converted. Open Windows Disk Management and look the number (e.g. Disk 0).
  3. Download gptgen from sourceforge http://sourceforge.net/projects/gptgen This tool will allow you to convert the paritition table without loosing any data
  4. Unzip and start gptgen under elevated CMD (via Run as Administrator)
    gptgen.exe -w \\.\physicaldriveX
    Replace physicaldriveX with the disk number (e.g. physicaldrive0)
Now the disk should be converted to GPT. If you face a problem that the disk doesn't have enough space at the end of the disk, then you can shrink the volume (assuming that you have free space on the volume). This can be done with 3d party tools or with diskpart
  1. Open elevated CMD and run
    diskpart
  2. list volume
  3. select volume
    and choose the number of the corresponding volume (e.g. 0)
  4. shrink
  5. exit
  6. Rerun gptgen

Instructions for Linux

  1. Boot and open terminal. You need parted installed
  2. Make backup of the existing MBR partition table:
    parted /dev/sda unit s print > partition_table.txt
    If you are converting the disk with the operating system you are currently running copy the partition_table.txt file to USB drive or upload it to internet, so you can access it later if something goes wrong.
  3. Create new GPT partition table overriding the existing MBR table
    mktable gpt
    When asked choose to ignore the warnings and to continue (with overwriting). We will recreate the existing partitions on exact sectors they have been before, but with new partition table format.
  4. Recrete partitions by using the information from partition_table.txt file and commands
    unit s
    mkpart
    When asked fill the partition type (e.g. ext4) and partition starting/ending sector (see partition_table.txt)
  5. Optionally set the legacy boot flag to the corresponding partition number.
    set 1 legacy_boot on
  6. Print the partition table and compare it with the partition_table.txt file
    print

Convert BIOS bootable to UEFI bootable for GPT partitioned disk

Since it is difficult to do the conversion while running the operating system I suggest you download a Fedora live CD and create bootable USB drive, so you can boot from it. Make sure that the USB is UEFI bootable - this means when you write it with rufus, choose GPT partition table and FAT32 system. If you plan to convert Windows to UEFI, then you need also a Windows installation DVD or USB. Then change the boot type in BIOS from Legacy BIOS to UEFI - see docs from your commputer manifacturer how to do it.

In order for UEFI to work you need a new partition with type C12A7328-F81F-11D2-BA4B-00A0C93EC93B - EFI System Partition (ESP). The new partition should be formatted with FAT32 system. The partition should be the first one. It will contain the files required to boot the operating system(s).

Windows

In GPT Windows needs additional partition called Microsoft Reserved Partition (MSR). In standard MBR partition scheme Windows used hidden sectors to store system data, but GPT doesn't allow hidden sectors, so MSR partition is used instead. It should not be formatted with any file system, because its sectors will be used directly by the operating system. The partition type should be E3C9E316-0B5C-4DB8-817D-F92DF00215AE.

How to create the ESP and MSR under Windows

This guide is adapted from Technet article
  1. Boot from Windows installation DVD or USB
  2. Choose repair and open command prompt
  3. Start diskpart
  4. Select the disk where the ESR and MSR partitions will be created
    list disk
    select disk X
    Replace X with the disk number (e.g. 0)
  5. Usually you have one small bootable partition used to boot Windows. We are going to delete it and use the space to create a new ESR and MSR
    list partition
    select partition 1
    delete partition
  6. Now create ESR
    create partition EFI size=100 offset=1
    format quick fs=fat32 label="System"
    assign letter=S
  7. Now create MSR
    create partition msr size=128 offset=103424
  8. Find out the windows installation drive letter.
    list volume
    You can also reassign it if needed (Usually it should be C:)
    select volume 3
    assign letter=C
  9. Exit disk part
    exit
  10. Generate boot partiton data
    bcdboot c:\windows /s s: /f UEFI
    Replace C: with your Windows installation letter

Linux

TODO : (draft version) 1. Delete /dev/sda1 2. Shring /boot and move it at the end, so the begining of the disk is free space 3. Create ESR at begining 4./etc/fstab -> mount UUID=xxxxx to /boot/efi 5.copy livecd /boot/efi to /dev/sda1 (UUID=xxxx)