How to copy the partition layout of a whole disk using standard tools

You can use sfdisk for this task even in GPT partitioned disks*.

Save:

sfdisk -d /dev/sdX > part_table

Restore keeping the same disk & partition IDs**:

sfdisk /dev/sdX < part_table

Restore generating new disk & partition IDs**:

grep -v ^label-id part_table | sed -e 's/, *uuid=[0-9A-F-]*//' | sfdisk /dev/sdY

Notes

*: For GPT partition tables, this requires sfdisk from util-linux 2.26 or later. It was re-written from scratch on top of libfdisk.

**: by default sfdisk will copy the disk and partition IDs unchanged, rather than generating new ones. So the new disk will be a clone of the original, not just another disk with the same layout. Note that Linux's /dev/disk/by-uuid/ looks at filesystem UUIDs, though, not UUIDs in the partition table. sfdisk will generate new UUIDs if you delete the references to partitions ids (, uuid=...) and the reference to the disk id (label-id: ...) from the dump .


It depends if your source disk uses an MBR (aka "dos" or "msdos") or a GPT (aka "GUID") partition table.

Disks over 2 TB cannot use MBR, so they are GPT.

Disks under 2 TB can use both, so you will have to first find out which it is.

Assuming you are on Linux, use either of these commands to find out which partition table your source disk uses:

disk=/dev/sda

# Always available, but old versions may not recognize gpt
fdisk -l $disk | grep type

# `apt-get install gdisk` or equivalent on non-Debian systems
gdisk -l $disk | grep -A4 'scan'

# `apt-get install parted`
parted $disk print | grep Table

Given

source=/dev/sda
dest=/dev/sdb

For MBR disks

use sfdisk as suggested by Petr Uzel's answer, or this variant:

# Save MBR disks
sfdisk -d $source > /partitions-backup-$(basename $source).sfdisk
sfdisk -d $dest   > /partitions-backup-$(basename $dest).sfdisk

# Copy $source layout to $dest
sfdisk -d $source | sfdisk $dest

For GPT disks

The correct answer was given here and here by Kris Harper.

You need GPT fdisk. Look at the download page or run sudo apt-get install gdisk.

Then use the sgdisk command:

# Save GPT disks
sgdisk --backup=/partitions-backup-$(basename $source).sgdisk $source
sgdisk --backup=/partitions-backup-$(basename $dest).sgdisk $dest

# Copy $source layout to $dest and regenerate GUIDs
sgdisk --replicate=$dest $source
sgdisk -G $dest

The last 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.


Older but still interesting pyparted and python-lvm bindings.

Update:

The previous was posted because the above does not work in many modern situations per the sfdisk man page.

sfdisk doesn't understand GUID Partition Table (GPT) and it is not designed for large partitions. In particular case use more advanced GNU parted(8).

This command however supports >2 TB partitions and LVM.

# parted -ms /dev/sda print > sda.parted

Sample output:

BYT;
/dev/sda:12.9GB:scsi:512:512:msdos:VMware Virtual disk;
1:1049kB:12.9GB:12.9GB:::boot, lvm;