Hopefully you are reading this article before you are in trouble. If that's the case, it is a good idea to make a backup of the MBR of your primary hard disk. This sector of only 512 bytes contains information that is vital to boot your computer and by creating a backup, you make it a lot easier to fix any problem that is related to either GRUB or the partition table. If you have such a backup (don't forget to create it again after making major changes!), you just have to restore the backup if you get into serious trouble.
Finding the master boot record on Linux
To make a backup of your MBR, you need to know what the name is of your primary hard disk. On most Linux computers, this will be /dev/sda, but other names may occur as well. You can find out which name is used for your computers primary hard drive by using the
fdisk –l command. The storage device that is listed first is most likely your computer's primary hard disk.
Backing up the MBR using dd
I'll assume that the name of the hard disk that contains the MBR is /dev/sda. You can use the
dd command to make a backup of it. One of the major benefits of
dd, is that it allows you to copy blocks, no matter where on your computer's hard drive these blocks are. The following command for instance would create a backup of the MBR:
dd if=/dev/sda of=/boot/backup_mbr bs=512 count=1
The structure of this command is not too hard to understand. The
dd command always works with two parts, the input file, which is referred to by using the
if parameter, and the output file, which is referred to by using the
of parameter. The nice thing about
dd is that for both parameters, you can use the name of a regular file, but also the name of a block device. So in the example above, the input file is your computer hard drive and the output file is a file with the name backup_mbr, which is created in the directory /boot. Next, the parameter
bs=512 makes sure that a 512 bytes block is written, and by using
count=1 you make sure that only one of those blocks is written.
As you can see, in this example I've chosen to write this backup file to the directory /boot. That is on purpose: /boot is always on a traditional partition and cannot be on an LVM logical volume (this will be discussed later in this series of articles), and by putting the backup file on a traditional partition, you make it much easier to access it later in case of trouble.
Restoring the Linux master boot record
Now if you are having boot problems that are related to the MBR, you can boot your computer from a rescue CD, such as a Knoppix CD. Once you have mounted the /boot partition on the appropriate device, you can use the following command to restore the backup of your MBR to hard disk:
dd if=/boot/backup_mbr of=/dev/sda bs=512 count=1
Troubleshooting is much easier if you take some precautionary measures. One of these is making a backup of the MBR of your computer's primary hard disk. In this article you've read how to do that. This back up comes in handy if you're having problems that are related to the MBR, such as GRUB or partition problems. If that occurs, you just have to restore the backup as described in this article.
ABOUT THE AUTHOR: Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. Vugt is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.
This was first published in July 2009