Backing up and restoring a master boot record (MBR)

The following will take an image of the master boot record (MBR) of a hard disc (also called the boot sector, partition sector or other name variations). This assumes that /dev/hda is the disk you boot from, and takes account of the fact that the boot sector resides in the first 512 blocks on a hard disk. (For more information, see: http://en.wikipedia.org/wiki/Master_boot_record)

The different examples reference whether you have a boot manager installed on the MBR (master boot record) (i.e. hda) or on a partition’s VBR (volume boot record) (i.e. hda1):

Copy the MBR for the disc hda to a file:

dd if=/dev/hda of=/home/james/boot-sector-hda bs=512 count=1

Copy the VBR for the partition hda1 to a file:

dd if=/dev/hda1 of=/home/james/boot-sect-hda1 bs=512 count=1

Restore a previously backed up copy of the VBR from partition hda1:

dd if=/home/james/boot-sect-hda1 of=/dev/hda1 bs=512 count=1

Within a single disc, it is also possible to swap round the VBRs between different partitions or to copy the MBR code to a VBR.*

Copy the contents of the MBR of hda to the VBR of hda1:

dd if=/dev/hda of=/dev/hda1 bs=512 count=1

Copy the contents of the VBR of hda1 to the VBR of hda2:

dd if=/dev/hda1 of=/dev/hda2 bs=512 count=1

For more information

The following provides more information and additional examples pertaining to system imaging, along with gzip compression of the image:

http://david.codepoets.co.uk/docs/system_imaging/document_view

Boring technical footnote

  • Copying the MBR to VBR copying might be used when installing a second operating system such as Windows NT, which rewrites the MBR with it’s own bootstrap code, making no attempt to ensure that third-party operating systems already on the machine remain bootable. After copying the previous MBR and installing NT, an item could then be added to the NTLDR boot menu manually, allowing it to chainload the other operating system(s)’ bootloader. In practice, the usual solution for implementing dual-boot is to restore grub to the MBR, then set up a menu entry to chainload the NT boot partition’s VBR.

    In theory, you could also copy from VBR to MBR, however bytes 0x018A onwards of the MBR should NOT be overwritten as these contain the partition table and disc signature. Use bs=395 instead of bs=512 in the above syntax when copying anything other than a valid MBR to the MBR area of the disc in order to ensure that this information is preserved. Copying from VBR to MBR will not work where the bootstrap code takes up more than 395 bytes of space.