Copying a partition table to another disk
This week I added a new hard drive to a server, meant as a spare drive for the RAID 1 array (linux software RAID). To activate the partitions as spares, I needed to recreate exactly the same partitions as the other disks (same size).
I first tried doing that manually with GNU parted, but that turned out to be hard to do, it seems I could never get the partitions to have exactly the same size…
So I searched a bit on Google, and found a way to make a backup of the partition table of one disk, and copy it to another disk very easily. As usual, the dd command is always a great tool for that kind of job.
First, here what you need to do to create a backup of the MBR (which contains the partition table), and save it to a file.
# dd if=/dev/sda of=mbr.bin bs=512 count=1
This will copy the first 512 bytes of your disk (/dev/sda), which happen to be where the MBR is located. The “of=” (output file) parameter means that the MBR will be saved in the file mbr.bin of the current directory. Then, this is what you need to do to restore it, this time on /dev/sdc :
# dd if=mbr.bin of=/dev/sdc bs=512 count=1
You can also replace only the partition table, but not the code area, which contains the boot loader such as GRUB. The code area uses the first 446 bytes at most, while the partition table is in the next 66 bytes of the hard disk (64 bytes + 2 bytes signature). So by skipping the first 446 bytes, you will only get the partition table from your backup, and keep the rest intact :
# dd if=mbr.bin of=/dev/sdc bs=1 count=64 skip=446 seek=446
That’s all! See the Wikipedia article on Master boot record for more information about what is in the MBR.
Re-read partition table
After altering the MBR, you will probably need to tell Linux to reload the partition table in order to see the changes. GNU parted provides a simple tool for that, partprobe. Just execute “partprobe” as root and you should be OK.
Verifying your partition table
To be sure you partition table is OK and exactly the same between /dev/sda and /dev/sdc, just look at the special file /proc/partitions
# cat /proc/partitions
major minor #blocks name 8 0 488386584 sda 8 1 514048 sda1 8 2 487869952 sda2 8 16 488386584 sdb 8 17 514048 sdb1 8 18 487869952 sdb2 8 32 488386584 sdc 8 33 514048 sdc1 8 34 487869952 sdc2
Tags: backup, linux, mbr, partitions