80

I want to take a backup of the whole partition layout of a hard drive, including logical drives, so that I can restore that layout to another disk. I do not want to copy the contents of the partitions, only the layout. For the primary and extended partitions, it's easy:

dd if=/dev/sda of=partitiontable.bin bs=1 skip=446 count=64 # backup
dd if=partitiontable.bin of=/dev/sda bs=1 seek=446 count=64 # restore

But when it comes to the layout of the logical partitions, I wonder if there exists among the standard tools a similar way of saving the layout? I guess the main problem is finding the offsets to the locations of the EBRs, because with that, dd will do the rest. Keep in mind I need to be able to put everything back to a (possibly) blank disk and thereby restore the same layout. Using partitioning tools like fdisk or parted is fine, but I must be able to automate their use (scripting) and they should not depend on any X-related packages -- command line only.

My backup plan is doing it manually in a little python script using the struct module, but I rather hoped there was an easier way.

86

You can use sfdisk for this task.

Save:

sfdisk -d /dev/sda > part_table

Restore:

sfdisk /dev/sda < part_table

For GPT partition tables, this requires sfdisk from util-linux 2.26 or later. It was re-written from scratch on top of libfdisk.

This copies the UUIDs unchanged, rather than generating new ones. So the new disk is a clone of the original, not just another disk with the same layout. Note that Linux's /dev/disk/by-uuid/ looks at filesystem UUIDs, though, not UUIDs in the partition table. sfdisk will generate new UUIDs if you edit out the UUIDs from the dump (per-partition and the UUID for the partition table itself near the start of the file).

  • 1
    I don't know for sure, but the only limit that comes to my mind is the 2TB size limit for partition imposed by msdos partition table scheme. To overcome this limit, one can use GPT instead, but AFAIK sfdisk can not work with GPT. I don't know if there is any other limit nor if sfdisk will report if it can't cope. – Petr Uzel May 11 '11 at 9:05
  • 2
    sfdisk doesn't work with large volumes or support GPT. – dhchdhd May 11 '11 at 9:11
  • 1
    @Barry and when you say "large", you're referring to the 2TB limit @Petr is talking about? – Lauritz V. Thaulow May 11 '11 at 9:18
  • 5
    The correct way is:sudo parted /dev/sda -lm > sda.parted – dhchdhd Aug 12 '11 at 8:17
  • 1
    Sometimes it is useful, to ignore DOS only problems, to add the -L or --linux option: sfdisk -L /dev/sda < part_table – Diego Jul 15 '14 at 10:17
50

It depends if your source disk uses an MBR (aka "dos" or "msdos") or a GPT (aka "GUID") partition table.

Disks over 2 TB cannot use MBR, so they are GPT.

Disks under 2 TB can use both, so you will have to first find out which it is.

Assuming you are on Linux, use either of these commands to find out which partition table your source disk uses:

disk=/dev/sda

# Always available, but old versions may not recognize gpt
fdisk -l $disk | grep type

# `apt-get install gdisk` or equivalent on non-Debian systems
gdisk -l $disk | grep -A4 'scan'

# `apt-get install parted`
parted $disk print | grep Table

Given

source=/dev/sda
dest=/dev/sdb

For MBR disks

use sfdisk as suggested by Petr Uzel's answer, or this variant:

# Save MBR disks
sfdisk -d $source > /partitions-backup-$(basename $source).sfdisk
sfdisk -d $dest   > /partitions-backup-$(basename $dest).sfdisk

# Copy $source layout to $dest
sfdisk -d $source | sfdisk $dest

For GPT disks

The correct answer was given here and here by Kris Harper.

You need GPT fdisk. Look at the download page or run sudo apt-get install gdisk.

Then use the sgdisk command:

# Save MBR disks
sgdisk --backup=/partitions-backup-$(basename $source).sgdisk $source
sgdisk --backup=/partitions-backup-$(basename $dest).sgdisk $dest

# Copy $source layout to $dest and regenerate GUIDs
sgdisk --replicate=$dest $source
sgdisk -G $dest

The last command randomizes the GUID on the disk and all the partitions. This is only necessary if the disks are to be used in the same machine, otherwise it's unnecessary.

  • 10
    I'm always scared I'll mix the two parameters, so a good trick is to use --backup=File to export a partition table from the source drive and --load-backup=File to restore in on the destination drive. – zidarsk8 Jul 24 '14 at 9:56
  • Is there any risk to the existing data on the disks and subsequent partitions when you move the partition table and then randomize the GUID of the disks and partitions as you describe above? Thanks! – EngBIRD Aug 22 '16 at 0:36
  • @EngBIRD: As long as you don't mixup $source and $dest, there cannot be risk to data, since there is no data on the destination. You only copy the partition layout, and still need to format and copy the data afterwards. – mivk Aug 22 '16 at 10:00
  • The reason I asked is in fact I'm not starting from a blank disk, I'm swapping two disks around... – EngBIRD Aug 22 '16 at 13:53
5

Older but still interesting pyparted and python-lvm bindings.

Update:

The previous was posted because the above does not work in many modern situations per the sfdisk man page.

sfdisk doesn't understand GUID Partition Table (GPT) and it is not designed for large partitions. In particular case use more advanced GNU parted(8).

This command however supports >2 TB partitions and LVM.

# parted -ms /dev/sda print > sda.parted

Sample output:

BYT;
/dev/sda:12.9GB:scsi:512:512:msdos:VMware Virtual disk;
1:1049kB:12.9GB:12.9GB:::boot, lvm;
  • 13
    How do you restore from the saved sda.parted file to a new disk? – Avery Chan Jun 20 '12 at 1:46
  • 4
    This answer is useless because the question was about copying the partition layout to a new drive. The answer is just about dumping. How to import ? Please improve. – itsafire Oct 8 '15 at 12:31
  • Apparently at this time, there is no good answer: serverfault.com/questions/709582/… – Otheus May 30 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.