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.
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.
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!