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.

  • 1
    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.) – Peter Cordes Mar 1 '15 at 17:01
  • 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! – Costin Gușă Oct 18 '15 at 2:33
  • 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 – Peter Cordes Feb 27 '16 at 3:21
  • How about dd if=/dev/sda of=/dev/sdb? – juniorRubyist Jun 26 at 21:29
up vote 110 down vote accepted

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.

  • 9
    This information is golden for anyone who wants to replace a failed RAID-1 disk. Thanks! – Christian Oct 22 '12 at 14:30
  • @Christian Yep, that's what I used it for. – Kris Harper Oct 22 '12 at 14:42
  • 8
    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 – Michael Kropat Dec 1 '14 at 6:02
  • 11
    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 Jan 7 '15 at 0:26
  • 2
    @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. – Geoffrey Jan 12 '15 at 10:35

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
  • I found this solution is better, because it can work with non-GPT. I also change the last command to: sgdisk -g /dev/sdb – Locke Dec 7 '13 at 9:09
  • 1
    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. – Geoffrey Jan 7 '15 at 0:27
  • 4
    First backup, then restore. I find this to be more intuitive and less chance to mix the drives up. – Csq Dec 28 '15 at 12:30
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
  • 1
    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. – goertzenator Nov 10 '15 at 2:19
  • 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 Jan 6 '16 at 10:47
  • 1
    @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! – F. Hauri Jan 6 '16 at 10:49

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.

  • 3
    This is exactly what my answer says. – Kris Harper Nov 5 '13 at 21:20

Easiest answer to this is:

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

to copy the 'good' partition table config of A to the replacement drive B (which is the new drive).

  • You missed the part in the question that explains why this isn't an option. – DavidW Dec 29 '16 at 23:08
  • 1
    Your answer does not work for GPT only for non-GPT – Jan Dev Jan 20 at 23:12

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.