How to copy the partition layout of a whole disk using standard tools
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 # backup dd if=partitiontable.bin of=/dev/sda bs=1 seek=446 count=64 # restore
But 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,
ddwill 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
partedis 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.
You can use sfdisk for this task.
sfdisk -d /dev/sda > part_table
sfdisk /dev/sda < part_table
For GPT partition tables, this requires
sfdiskfrom 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.
sfdiskwill 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).
Exactly what I was looking for, thank you! However, the man page says sfdisk is "not designed for large partitions", but does not specify any further what it means by "large". What specific limits does this impose? Will sfdisk tell me if it can't cope?
I don't know for sure, but the only limit that comes to my mind is the 2TB size limit for partition imposed by msdos partition table scheme. To overcome this limit, one can use GPT instead, but AFAIK sfdisk can not work with GPT. I don't know if there is any other limit nor if sfdisk will report if it can't cope.
@Barry and when you say "large", you're referring to the 2TB limit @Petr is talking about?
Sometimes it is useful, to ignore DOS only problems, to add the -L or --linux option: `sfdisk -L /dev/sda < part_table`
I use this in one shot : `sudo sfdisk -L -d /dev/disk/bypid/$SOURCE | sudo sfdisk -L /dev/disk/by-id/$TARGET` Use disk-ids if you're a dizzy head.
@rzr: `-L` is deprecated, but still accepted (as a no-op?) for backward compatibility. But yes, `sfdisk --dump | sfdisk` appears to be all you need. Also, nobody mentioned yet that **`sfdisk` does support GPT partitions**, since util-linux 2.26, where it was re-written from scratch on top of libfdisk.