Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 19 down vote accepted

You can use sfdisk for this task.

Save:

sfdisk -d /dev/sda > part_table

Restore:

sfdisk /dev/sda < part_table
share|improve this answer
    
Exactly what I was looking for, thank you! However, the man page says sfdisk is "not designed for large partitions", but does not specify any further what it means by "large". What specific limits does this impose? Will sfdisk tell me if it can't cope? –  Lauritz V. Thaulow May 11 '11 at 8:48
    
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
1  
sfdisk doesn't work with large volumes or support GPT. –  Barry May 11 '11 at 9:11
    
@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
1  
The correct way is:sudo parted /dev/sda -lm > sda.parted –  Barry Aug 12 '11 at 8:17
add comment

The correct answer, which will also work with GPT disks, 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 like so

sgdisk -R=/dev/sdb /dev/sda
sgdisk -G /dev/sdb

The first command copies the partition table of sda to sdb (be careful not to mix these up). The second 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.

share|improve this answer
add comment

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;
share|improve this answer
3  
How do you restore from the saved sda.parted file to a new disk? –  Avery Chan Jun 20 '12 at 1:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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