Sometimes you need to clone partitions on your hard drive for various reasons, hd damaged, creating a RAID 1 or something similar, then it is often tedious and impractical to perform the operation using the normal tools like fdisk, in this case you can use sfdisk.
As reported in the manual:
sfdisk is a non-interactive program to edit the partition table, which is useful for creating scripts. The normal use of this service program involves the preparation of a file containing the instructions on the partitions to be created within a disk specified explicitly. Even if there is a special syntax for these instructions, it may be convenient to use since it is obtained from a query with the same sfdisk, as will be shown.
In practice sfdisk is nothing but a tool that allows you to perform simple tasks in Linux on the partition table
In our case through the -d option will be able to clone partitions , in fact, this option create a report on the partitions of the hard disk defined as a parameter.
Bringing the output to a file you can then perform the inverse operation and recreate the partition table to another disk , or even on the same .
Cloning the partition table
For example, assuming that our disk is /dev/sda , to save the partition table we can give the command:
sfdisk -d /dev/sda > partitions.txt
while to restore it, assuming that the destination disk is /dev/sdb and we want to clone the partition table, we can use the command
sfdisk /dev/sdb < partitions.txt
If you get told by sfdisk that “sfdisk: I don’t like these partitions – nothing changed”, it is because the installer for some modern distributions, including CentOS6, create partitions that are the size you specify and do not necessarily end at cylinder boundaries.
This shouldn’t cause any problems during day to day running but does when manually partitioning disks.
Fortunately we can force sfdisk to do this, with the option –force
sfdisk -d /dev/sda | sfdisk --force /dev/sdb
At this point we have two disks with partitions of the same size, of course, the passage of data, if you would like it, or sync the disks or even RAID reconstruction now it’s up to you.