How can I quickly copy a GPT partition scheme from one hard drive to another?
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.
Kris Harper
Updated on September 18, 2022Comments
-
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 about 9 yearsThe util-linux partitioning tools (including
sfdisk
) were rewritten to include GPT support for util-linux 2.26.sfdisk
differs fromgdisk
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 over 8 yearsWhen 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 about 8 yearsupdate 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 almost 6 yearsHow about
dd if=/dev/sda of=/dev/sdb
?
-
-
illya over 11 yearsThis information is golden for anyone who wants to replace a failed RAID-1 disk. Thanks!
-
Kris Harper over 11 years@Christian Yep, that's what I used it for.
-
Tobu about 11 yearsAccording to the manual this also supports MBR-only disks (sgdisk auto-converts on load), which is pretty great.
-
Kris Harper over 10 yearsThis is exactly what my answer says.
-
Locke over 10 yearsI 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 over 9 yearsBefore making any destructive changes, be sure to take a backup with:
sgdisk --backup=/some/safe/location/sdX.gpt /dev/sdX
andsgdisk --backup=/some/safe/location/sdY.gpt /dev/sdY
-
Donesa Rucci over 9 yearsIf you do screw up your GPT partition table (like I did), take a look at testdisk(1)
-
Geoffrey over 9 yearsThis 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 over 9 yearsThe 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 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 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 over 8 yearsThis 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 over 8 yearsFirst backup, then restore. I find this to be more intuitive and less chance to mix the drives up.
-
F. Hauri over 8 yearsI like this way! but prefer to write:
sed '$s/:.*//p;d'
insteadtail -n1 | cut -b1
as this will fail if you have more than 9 partitions! -
F. Hauri over 8 years@goertzenator You're right, for this you may run regular
parted
tool, do something (like set any unset flag tono
), this will re-write partition table on both ends! -
DavidW over 7 yearsYou missed the part in the question that explains why this isn't an option.
-
user636290 over 6 yearsYour answer does not work for GPT only for non-GPT
-
Mike Turley almost 6 yearsThis was fantastically useful in getting my software RAID to boot again. Thank you!
-
Kurt almost 6 yearsA bit nicer if the example could be edited to read
sgdisk /dev/src -R /dev/dest
-
Aaron Franke over 5 yearsYou never used
B
in the top section? -
Jacob Rodrigues over 5 yearsOn the current version of
sfdisk
this does work. -
dodexahedron over 5 yearsB is a value used to figure out A.
-
Martin Pecka almost 5 yearsThis 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 runningsgdisk -G
afterwards will fix the remaining issues. This is great for migrating your notebook from HDD to a smaller SSD. -
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 about 3 yearsThis works, but it unfortunately does not create a new disk ID