96

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.

4
  • 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.) Mar 1, 2015 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! Oct 18, 2015 at 2: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. unix.stackexchange.com/a/12988/79808 Feb 27, 2016 at 3:21
  • How about dd if=/dev/sda of=/dev/sdb?
    – Joseph
    Jun 26, 2018 at 21:29

5 Answers 5

161

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.

12
  • 14
    This information is golden for anyone who wants to replace a failed RAID-1 disk. Thanks!
    – Christian
    Oct 22, 2012 at 14:30
  • 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 Dec 1, 2014 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, 2015 at 0:26
  • 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, 2015 at 10:35
  • 3
    A bit nicer if the example could be edited to read sgdisk /dev/src -R /dev/dest
    – Kurt
    May 22, 2018 at 20:47
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
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 7, 2013 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, 2015 at 0:27
  • 10
    First backup, then restore. I find this to be more intuitive and less chance to mix the drives up.
    – Csq
    Dec 28, 2015 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
5
  • 2
    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. Nov 10, 2015 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! Jan 6, 2016 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! Jan 6, 2016 at 10:49
  • You never used B in the top section? Oct 5, 2018 at 1:13
  • B is a value used to figure out A. Feb 1, 2019 at 17:02
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.

1
  • 4
    This is exactly what my answer says. Nov 5, 2013 at 21:20
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.

4
  • You missed the part in the question that explains why this isn't an option.
    – DavidW
    Dec 29, 2016 at 23:08
  • 2
    Your answer does not work for GPT only for non-GPT
    – user636290
    Jan 20, 2018 at 23:12
  • 3
    On the current version of sfdisk this does work. Nov 13, 2018 at 15:53
  • 1
    This works, but it unfortunately does not create a new disk ID Mar 22, 2021 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .