Save and restore partition tables with sfdisk

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: 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

..and save it to the replaced sdb

sfdisk /dev/sdb < sda_partitions

You can also do it all in one go

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

As usual, RTM for more options 🙂

2 Comments

  • me says:

    thanksssss 😉

  • sysadmin says:

    sfdisk is a nice tool, I always use it while installing centos on raid-1 built with mdadm.

    I also very like this tiny command for creating one partition per disk using all available space:
    echo “0” | /sbin/sfdisk –force /dev/sda

Leave a Reply

Your email address will not be published. Required fields are marked *