97

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.

Christian
  • 268
Kris Harper
  • 13,477
  • 3
    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 01 '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 02:33
  • 3
    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. http://unix.stackexchange.com/a/12988/79808 – Peter Cordes Feb 27 '16 at 03:21
  • How about dd if=/dev/sda of=/dev/sdb? – Joseph Jun 26 '18 at 21:29

5 Answers5

162

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.

Kris Harper
  • 13,477
  • 14
    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
  • According to the manual this also supports MBR-only disks (sgdisk auto-converts on load), which is pretty great. – Tobu Mar 09 '13 at 23:11
  • 11
    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 01 '14 at 06:02
  • If you do screw up your GPT partition table (like I did), take a look at testdisk(1) – Michael Kropat Dec 01 '14 at 06:07
  • 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 07 '15 at 00:26
  • @Geoffrey I see what you're saying, but that goes against the man page entry, which lists the usage as sgdisk [ options ] device. – Kris Harper Jan 07 '15 at 14:14
  • 4
    @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
  • This was fantastically useful in getting my software RAID to boot again. Thank you! – Mike Turley May 13 '18 at 21:28
  • 3
    A bit nicer if the example could be edited to read sgdisk /dev/src -R /dev/dest – Kurt May 22 '18 at 20:47
  • 2
    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. – Martin Pecka Jun 14 '19 at 10:23
  • @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. – Holger Böhnke Feb 24 '21 at 09:20
43

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
Kris Harper
  • 13,477
Serafin Rusu
  • 566
  • 4
  • 3
  • 1
    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 07 '13 at 09: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 07 '15 at 00:27
  • 10
    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
9
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
Petroff
  • 91
  • 1
  • 1
3

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.

al3xxx
  • 65
3

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.