I want to take a backup of the whole partition layout of a hard drive, including logical drives, so that I can restore that layout to another disk. I do not want to copy the contents of the partitions, only the layout. For the primary and extended partitions, it's easy:dd if=/dev/sda of=partitiontable.bin bs=1 skip=446 count=64 # backupdd if=partitiontable.bin of=/dev/sda bs=1 seek=446 count=64 # restoreBut when it comes to the layout of the logical partitions, I wonder if there exists among the standard tools a similar way of saving the layout? I guess the main problem is finding the offsets to the locations of the EBRs, because with that, dd will do the rest. Keep in mind I need to be able to put everything back to a (possibly) blank disk and thereby restore the same layout. Using partitioning tools like fdisk or parted is fine, but I must be able to automate their use (scripting) and they should not depend on any X-related packages -- command line only.My backup plan is doing it manually in a little python script using the struct module, but I rather hoped there was an easier way.
How to copy the partition layout of a whole disk using standard tools
command line;partition
You can use sfdisk for this task.Save:sfdisk -d /dev/sda > part_tableRestore:sfdisk /dev/sda < part_tableFor GPT partition tables, this requires sfdisk from util-linux 2.26 or later.  It was re-written from scratch on top of libfdisk.This copies the UUIDs unchanged, rather than generating new ones.  So the new disk is 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 edit out the UUIDs from the dump (per-partition and the UUID for the partition table itself near the start of the file).