Copy Disk Partition with sfdisk

I’ve just learned about a very old but pretty cool Linux command – sfdisk. Somehow I’ve always managed to use fdisk and parted, but sfdisk is also very useful. Specifically, I’ve learned to use it for copying partition of one (old) disk to new (replacement) disk.

Confirm Disk Partition Layout

root@ubuntu:~ # sfdisk -d /dev/sda
 label: dos
 label-id: 0xc45b9a38
 device: /dev/sda
 unit: sectors
 /dev/sda1 : start=        2048, size=   134213632, type=83, bootable

New disk (for this example today) didn’t have any partitions:

root@ubuntu:~ # sfdisk -d /dev/sdb sfdisk: /dev/sdb: does not contain a recognized partition table 

Save Disk Partition Layout into File

Very simple command to save layout into partitions.txt file:

root@ubuntu:~ # sfdisk -d /dev/sda > partitions.txt

Apply Partition Layout from File to Disk

And this is how I can apply the layout from file partitions.txt to the new disk /dev/sdb:

root@ubuntu:~ # sfdisk -f /dev/sdb < partitions.txt
 Checking that no-one is using this disk right now … OK
 Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
 Disk model: Ubuntu Linux-1 S
 Units: sectors of 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes / 4096 bytes
 I/O size (minimum/optimal): 4096 bytes / 4096 bytes
 Disklabel type: dos
 Disk identifier: 0xc45b9a38
 Old situation:
 Device     Boot Start      End  Sectors Size Id Type
 /dev/sdb1  *     2048 10485759 10483712   5G 83 Linux
               Script header accepted.
       Script header accepted.
       Script header accepted.
       Script header accepted.
       Created a new DOS disklabel with disk identifier 0xc45b9a38.
       /dev/sdb1: Created a new partition 1 of type 'Linux' and of size 5 GiB.
       /dev/sdb2: Done.         
 New situation:
 Disklabel type: dos
 Disk identifier: 0xc45b9a38
 Device     Boot Start      End  Sectors Size Id Type
 /dev/sdb1  *     2048 10485759 10483712   5G 83 Linux
 The partition table has been altered.
 Calling ioctl() to re-read partition table.
 Syncing disks.

See Also




Keep Learning

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets