HOW TO CLONE PARTITION TABLES (GPT & MBR)
##########################################

First read this on how to create partitions with sgdisk (GPT) and sfdisk (MBR): Creating partitions with sfdisk or sgdisk

Cloning partition tables, first make sure to use the right tool. If the disks came with GPT then use sgdisk, if they came with MBR then use sfdisk.

Here im showing the examples with sgdisk and sfdisk, obviously there are other commands that this might be possible with

MBR commands & the command to download it:
* fdisk – command line MBR partitioning tool (apt-get update && apt-get install util-linux)
* sfdisk – command line MBR partitioning tool (apt-get update && apt-get install util-linux)
* cfdisk – interactive MBR partitioning tool (apt-get update && apt-get install cfdisk-udeb)

GPT commands & the command to download it:
* sgdisk – command line GPT partitioning tool (apt-get update && apt-get install gdisk)
* cgdisk – interactive GPT partitioning tool (apt-get update && apt-get install gdisk)

How to find out if GPT or MBR. Run this command and at the top you will see a WARNING if its GPT:

Example:  sfdisk -d /dev/sda

Example Output:
WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util sfdisk doesn't support GPT. Use GNU Parted.

Therefore we know /dev/sda is GPT

NOTE: sfdisk’s d option is for dump, its output can be used to restore/copy partitions (As you will see later)

INFO FOR COMMANDS BELOW
—————————————-

First list all of the partitions and drives with:

Where you see SOURCE, DESTINATION, or DRIVE replace with the correct drive such as /dev/sda or /dev/sdb. Make sure not put a number at the end, as a number denotes a partition – as we need the full drive and not the partition. So dont put /dev/sda3 but just use /dev/sda.

Find out which drives are what using their serial numbers:

Example:  hdparm -I /dev/sda | grep Serial

Where you see part_table, you can replace with any filename of your choosing. part_table will get created and it will contain the partition table of the drive.

So to recap:
[SOURCE] – Put the drive that will be the template for copying the partition table. This drives partition table will be copied to the DESTINATION drive.
[DESTINATION] – Put the drive that is missing the good partition table layout. This drive will get the same partition as that of the SOURCE drives
[part_table] – This will save the SOURCE drives partition table in sgdisk or sfdisks format

NOTE: there is more then 1 way to do the same thing; sometimes in linux one of the correct ways might not work, so try one of the other commands. If one way fails try another.

SFDISK, Copy partitions (MBR – drives less then or eq 2TB)
###############################################

2 Method here that are really useful

1.

Save:

Restore 2 new or same drive:

Example:  sfdisk -d /dev/sda > /root/table_of_a Example:  sfdisk /dev/sdb < root/table_of_a

NOTE: can manually edit the file part_file before restore. Can make partition tables on the fly by hacking up a good part_table file and then restoring it. “cat part_table” to check it out.

2.

Since we used < and > we can obviously tie those with a pipe

Save and restore to new or same drive:

Example:  sfdisk -d /dev/sda | sfdisk /dev/sdb

SGDISK, Copy partitions (GPT – drives bigger then 2TB or any size)
####################################################

4 methods here. Each one does the same thing.

Here are the options that will be seen sgdisk’s -b option is for backing up partitions to a file, sgdisk’s -l option is for loading backup files, sgdisk’s -G is to create new GUIDS, sgdisk’s -g is to convert from MBR to GPT (if needed, if already at GPT, it just skips this option, so it doesnt hurt to include). sgdisk’s -R command is to copy partition tables (including the GUID, so that needs to be randomized after on the destination)

1.

First save to a file the correct partition table (from source disk), then copy from that partition table file (part_table) to the destination drive:

Example:  sgdisk -b - /dev/sda > /root/table_of_a Example:  sgdisk -l - Gg /dev/sdb < /root/table_of_a

NOTE: copying the partition table to the destination also copies the GUIDS, but we need each drive to have different guids, so we follow up with the -G and -g command which generate new random GUIDs

2.

This should do the same thing with less typing:

Example:  sgdisk -b /root/table_of_a /dev/sda Example:  sgdisk -l /root/table_of_a -Gg /dev/sdb

NOTE: you might need to remove the space between -l & -b and [part_table], example below.
Example:  sgdisk -b/root/table_of_a /dev/sda Example:  sgdisk -l/root/table_of_a -Gg /dev/sdb

3.

Or do the same thing with 1 command:

Example:  sgdisk -b - /dev/sda | sgdisk -l - -Gg /dev/sdb

4.

Or my favorite (this just copies the partition table with the first command, and then randomizes the GUIDs after):

NOTE: Notice that its DESTINATION and then SOURCE, which might throw some people off, as we are all used to the common SOURCE followed by DESTINATION (to the right of SOURCE). In this case DESTINATION is on the left and comes first.
NOTE: skip the space between -R and [Destination], I noticed it doesnt take if you put a space there. The below example shows the correct way to use it:

Example:  sgdisk -R/dev/sdb /dev/sda Example:  sgdisk -Gg /dev/sdb

 

4 thoughts on “HOW TO CLONE PARTITION TABLES (GPT & MBR) – sfdisk & sgdisk

Leave a Reply

Your email address will not be published. Required fields are marked *