C-NET :: PROJECTS

cat save-and-restore-partition-tables-with-sfdisk.md

Save and restore partition tables with sfdisk

Posted
Updated
Filed under
Linux · Reference

In my raid setup, I set up the partitions a certain way for running several mdadm raids on the same drive.

My server currently boots off of two drives, sda and sdb, where sda1 and sdb1 are the root file system in raid 1 (mirror) and sda2 and sdb2 are (encrypted) swap in raid 0 (striped). This is useful to have the redundancy of raid for booting, but none of that redundancy for the volatile swap partition. Instead I get higher write speed, which is always a bonus.

Note

Note: Most Linux distros would actually stripe the swap anyway, given two swap partitions with equal priority, but I chose to do it this way.

Anyway, when one of the drives went for a one-way trip to electronic oblivion, the need arose to configure my new drive exactly as the old ones. Mind you, mdadm can actually handle non-equal partition sizes in a raid, but I'm kind of anal about this.

sfdisk to the rescue!

First, of course, I replaced the faulty drive (sdb) with one of the same brand and equal size. You don't necessarily need the same brand, but that's what I had.

Then, to export and save the partition table from sda (which was still working)

sfdisk -d /dev/sda > sda_partitions
bash

..and save it to the replaced sdb

sfdisk /dev/sdb < sda_partitions
bash

You can also do it all in one go

sfdisk -d /dev/sda | sfdisk /dev/sdb
bash

As usual, RTM for more options :)

Comments