Recently, while trying to make a USB stick bootable, I inadvertently trashed both the stick and the MBR of my internal hard drive. Luckily, I was able to boot back into Fedora using the DVD in rescue mode. There was a problem with the stick that was causing it not to automount correctly. The MBR needed to be reset to factory and the partition table rebuilt.
Normally, to reset an MBR to factory, you would run fdisk /mbr off a MSDOS boot floppy, or fixmbr off a Windows boot CD. However, many laptops don't have a floppy and are sold with only a Windows restore CD. Besides, if you create a problem within Linux, you should be able to fix it within Linux.
The following Howto contains some simple commands to backup and reset the MBR and partition table of a drive. It should work for internal drives, USB stick, micro SD, floppy, etc. It is assumed that the device file associated with the drive is /dev/sdc. Yours will vary. All commands are executed as root.
If the drive is mounted, unmount it using
Obviously, this is not possible if this is the drive from which you are booting Linux. Not a big problem, as you can boot a rescue CD.
Backup up an MBR using
Code:
$ dd if=/dev/sdc of=mbr.bin bs=512 count=1
Restore the MBR with
Code:
$ dd if=mbr.bin of=/dev/sdc bs=512 count=1
To back up just the partition table instead of the whole MBR, run
Code:
$ dd if=/dev/sdc of=part.bin bs=1 skip=446 count=64
Restore the partition table with
Code:
$ dd if=part.bin of=/dev/sdc bs=1 seek=446 count=64
If you had previously backed up the entire MBR and just wanted to replace the partition table, the syntax is
Code:
$ dd if=mbr.bin of=/dev/sdc bs=1 skip=446 seek=446 count=64
To clean out the MBR, for example, to remove GRUB, LILO, Syslinux, etc., and restore it to factory, erase the executable part of the MBR with
Code:
$ dd if=/dev/zero of=/dev/sdc bs=446 count=1
If for some reason, the partition table is corrupted, zero it with
Code:
$ dd if=/dev/zero of=/dev/sdc bs=1 seek=446 count=64
Of course, this will render any existing data on the drive unreadable. Use /sbin/fdisk to remake the partition table. To avoid losing data, remake the partition(s) the same size as the original with the same starting and ending cylinders.