First just let’s make sure we know how to back up the MBR of a disk. So, here, we will back up the MBR of a device /dev/sda.

 dd if=/dev/sda of=/tmp/sda.mbr bs=512 count=1 

The command dd is a very simple bit imaging program, it does not care about anything other than the raw data and will copy it. We do not want to image the whole disk so we tell it to image the first ( count = 1 ) 512 bytes ( bs=512 ). The default block size for dd is 512 bytes, however, I would always prefer to add the option so I can see exactly what is happening.

da-oes-a:~ # dd if=/dev/sda of=/tmp/sda.mbr count=1 bs=512
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000317 seconds, 1.6 MB/s
da-oes-a:~ #

To recover from a corrupt or erroneous MBR you can image the backup file back.

 dd if=/tmp/sda.mbr of=/dev/sda 

The code is more simple to restore as there is no need to say how much to restore as you are limited to the 512 bytes in the file. By default you would start at the beginning of the drive.

Well now we know what we are facing. This is easy on one machine where I know exactly what disk or disks I have but is not really a framework to manage all of your servers / desktops. So we need to build a little logic into the process and this is where bash scripting comes in.

So lets just take a peek at the script and then we can run through its components:

declare -a disks=($(echo /dev/[hs]d?))

for disk in "${disks[@]}"
	dd if=$disk of=/tmp/$(basename $disk).mbr count=1 bs=512

Looking at the code the first line really to do anything is line 2 where we create an array called “disks”. An array is just a multi-valued variable. This array is populated with the output ( $(…..) ) of the command echo /dev/[hs]d? . Now that may look at little strange but it simply lists files in the /dev directory that start with either “h” or “s”, the second character of the name will be a “d” and followed by a single character. So this can list sda, sdb, hda etc. But NOT their partitions such as sda1.

Have a look at the output of the command on my system that has a SCSI disk and an IDE CDROM.

da-oes-a:~ # echo /dev/[hs]d?
/dev/hdc /dev/sda

So, as you can see I have executed just the echo command to give you an idea of what’s displayed; this information then is fed into the array to supply it with two elements. Running the script on this system would give us:

  • disks(0) with a value of /dev/hdc
  • disks(1) with a value of /dev/sda

The “for loop” structure iterates each element of the array so we then end up backing up the MBR of both disks. The command basename extracts the filename from its full path, so /dev/sda becomes just sda.

Sure we may not want it or the CDROM but it is of no harm and 512 bytes is not an issue for storage.

(Visited 1 times, 1 visits today)
Tags: , ,
Category: SUSE Linux Enterprise Desktop, Technical Solutions
This entry was posted Wednesday, 5 August, 2009 at 4:01 pm
You can follow any responses to this entry via RSS.

Leave a Reply

Your email address will not be published. Required fields are marked *

No comments yet