How can I quickly copy a GPT partition scheme from one hard drive to another?

161,765

Solution 1

Install gdisk which is available in the Ubuntu Universe repositories.

Then use the sgdisk command (man page here) to replicate the partition table:

sgdisk /dev/sdX -R /dev/sdY 
sgdisk -G /dev/sdY

The first command copies the partition table of sdX to sdY (be careful not to mix these up). The second command randomizes the GUID on the disk and all the partitions. This is only necessary if the disks are to be used in the same machine, otherwise it's unnecessary.

Solution 2

I tried and it didn't work for me. The solution that I found is:

sgdisk --backup=table /dev/sda
sgdisk --load-backup=table /dev/sdb
sgdisk -G /dev/sdb

Solution 3

dd if=/dev/sda of=GPT_TABLE bs=1 count=A
dd if=GPT_TABLE of=/dev/sdb bs=1 count=A
partprobe /dev/sdb

where A is:

A=(128*B)+1024
B=parted -ms /dev/sda print |tail -1|cut -b1

Solution 4

I just tried replication with sgdisk and it works just fine - you just have to follow readline syntax rules:

   sgdisk --replicate=/dev/target /dev/source

or

   sgdisk -R/dev/target /dev/source

and everything works.

Solution 5

The manpage of sfdisk says:

Since version 2.26 sfdisk supports MBR (DOS), GPT, SUN and SGI disk labels

So

sudo sfdisk -d /dev/sda | sudo  sfdisk /dev/sdb

will work with sfdisk version 2.26 and higher.

Share:
161,765
Kris Harper
Author by

Kris Harper

Updated on September 18, 2022

Comments

  • Kris Harper
    Kris Harper over 1 year

    On a non GPT partition table I can do

    sfdisk -d /dev/sda | sfdisk /dev/sdb.

    But sfdisk doesn't support GPT partition tables. What can I use instead?

    I'm looking for a one or two command solution, not just using GNU parted to output the partition sizes and then manually making them again.

    • Admin
      Admin about 9 years
      The util-linux partitioning tools (including sfdisk) were rewritten to include GPT support for util-linux 2.26. sfdisk differs from gdisk in that it doesn't support putting a small boot partition before 1MiB, though, so it choked on my config. (bug reported upstream already.)
    • Admin
      Admin over 8 years
      When I clone a MBR disk to a smaller disk, in addition to sfdisk -d I also edit the dump and modify start/end sectors. How do I do this with sgdisk for GPT disks? -R clones without intermediary backup file and -b creates a binary backup, not human readable/editable like sfdisk does!
    • Admin
      Admin about 8 years
      update on this: sfdisk now accepts whatever you give it when used this way, including a small boot partition following the GPT, ending at 1MB. unix.stackexchange.com/a/12988/79808
    • Admin
      Admin almost 6 years
      How about dd if=/dev/sda of=/dev/sdb?
  • illya
    illya over 11 years
    This information is golden for anyone who wants to replace a failed RAID-1 disk. Thanks!
  • Kris Harper
    Kris Harper over 11 years
    @Christian Yep, that's what I used it for.
  • Tobu
    Tobu about 11 years
    According to the manual this also supports MBR-only disks (sgdisk auto-converts on load), which is pretty great.
  • Kris Harper
    Kris Harper over 10 years
    This is exactly what my answer says.
  • Locke
    Locke over 10 years
    I found this solution is better, because it can work with non-GPT. I also change the last command to: sgdisk -g /dev/sdb
  • Donesa Rucci
    Donesa Rucci over 9 years
    Before making any destructive changes, be sure to take a backup with: sgdisk --backup=/some/safe/location/sdX.gpt /dev/sdX and sgdisk --backup=/some/safe/location/sdY.gpt /dev/sdY
  • Donesa Rucci
    Donesa Rucci over 9 years
    If you do screw up your GPT partition table (like I did), take a look at testdisk(1)
  • Geoffrey
    Geoffrey over 9 years
    This command works but it should be noted that the drive ordering is backwards in the example. A more obvious way to write this is sgdisk /dev/sdX -R /dev/sdY
  • Geoffrey
    Geoffrey over 9 years
    The above does work, you need to pay attention to the fact that the example is a little backwards (although correct). sgdisk /dev/sdX -R /dev/sdY is more obvious.
  • Kris Harper
    Kris Harper over 9 years
    @Geoffrey I see what you're saying, but that goes against the man page entry, which lists the usage as sgdisk [ options ] device.
  • Geoffrey
    Geoffrey over 9 years
    @KrisHarper: Indeed it does, but since the program uses getopt to parse the command line arguments the ordering does not matter squat. The man page should be updated.
  • goertzenator
    goertzenator over 8 years
    This also clones all the disk and partition GUIDs which may not be what you want. Also, it doesn't install the backup table at the end of the disk.
  • Csq
    Csq over 8 years
    First backup, then restore. I find this to be more intuitive and less chance to mix the drives up.
  • F. Hauri
    F. Hauri over 8 years
    I like this way! but prefer to write: sed '$s/:.*//p;d' instead tail -n1 | cut -b1 as this will fail if you have more than 9 partitions!
  • F. Hauri
    F. Hauri over 8 years
    @goertzenator You're right, for this you may run regular parted tool, do something (like set any unset flag to no), this will re-write partition table on both ends!
  • DavidW
    DavidW over 7 years
    You missed the part in the question that explains why this isn't an option.
  • user636290
    user636290 over 6 years
    Your answer does not work for GPT only for non-GPT
  • Mike Turley
    Mike Turley almost 6 years
    This was fantastically useful in getting my software RAID to boot again. Thank you!
  • Kurt
    Kurt almost 6 years
    A bit nicer if the example could be edited to read sgdisk /dev/src -R /dev/dest
  • Aaron Franke
    Aaron Franke over 5 years
    You never used B in the top section?
  • Jacob Rodrigues
    Jacob Rodrigues over 5 years
    On the current version of sfdisk this does work.
  • dodexahedron
    dodexahedron over 5 years
    B is a value used to figure out A.
  • Martin Pecka
    Martin Pecka almost 5 years
    This even works if the target drive is smaller than the source. Just make sure all partitions are squeezed to the beginning of the source disk and do not extend past the size of the target disk. sgdisk will issue a warning about this, but will work, and running sgdisk -G afterwards will fix the remaining issues. This is great for migrating your notebook from HDD to a smaller SSD.
  • Holger Böhnke
    Holger Böhnke about 3 years
    @KrisHarper Maybe you want to put the backup part into your answer. It's crucial. I once killed a disk partition because I had the parameters reversed. Only thing that saved me was that I hat the source partition table still printed on the terminal.
  • forresthopkinsa
    forresthopkinsa about 3 years
    This works, but it unfortunately does not create a new disk ID