As promised, tonight we explore boot sector management on X86 style hardware. Anyone who works with PC hardware long enough, and especially those using linux as primary or secondary OS in a dual boot configuration will find this information valuable.
The system boot sector on x86 style hardware is crucial to being able to boot a linux system on this common platform. Occaisonally the boot sector becomes corrupted or needs to be backed up. In the days of MS DOS systems, a command was used to “restore” the boot sector. The command was
Essentially this would re-write the boot sector on the primary hard disk.
The dd command can be used to perform similar functions, however as is usual with Linux, more boot sector related tasks can be accomplished.
First of all lets review the structure of a boot sector or master boot record on a PC hard disk:
Format of the boot sector:
|446||Executable code section|
|4||Optional Disk signature|
The first 446 bytes of the boot sector contain executable code that is loaded by the BIOS and then executed, and is where OS boot loaders and boot managers (such as grub) store their initial code. Its also an area of the disk that can become corrupted, or replaced during operating system installs.
The other part of the boot sector that is significant is the partition table. This is where the disk partition information is stored. This should not be modified by anything other than a disk partitioning utility such as fdisk. It can be backed up for data security reasons though. The total bytes in the master boot record comes to 512. With dd, simply reading or writing the first 446 or 512 bytes of the disk device will read or write the master boot record.
Scenario 1: Backup the boot sector (or MBR)
If the first harddisk in the system is /dev/sda, to backup the boot sector the following command can be used:
# dd if=/dev/sda of=bsbackup.bin bs=512 count=1
Essentially this command will read the first 512 bytes of /dev/sda and write it to the file bsbackup.bin.
Scenario 2: Restore the boot sector from a file:
# dd if=bsbackup.bin of=/dev/sda bs=512 count=1
This will restore the boot sector to /dev/sda that was backed up in Scenario 1.
Scenario 3: Zero out the boot sector (leaving the partition table intact)
Sometimes a virus or other issue can leave a corrupted executable code section in the MBR. I have personally seen a boot sector that would not store grub information (and thus boot linux after its installed) properly until the first 446 bytes were zeroed out and grub re-installed. The following command will do just that:
# dd if=/dev/zero of=/dev/sda bs=446 count=1
Scenario 4: Zero out the entire MBR (this will erase the partition table as well – effectively destroying the ability to easily access data on the drive)
A variation of the last dd command will wipe out the master boot record entirely. You will have to repartition and reformat your hard disk after this:
# dd if=/dev/zero of=/dev/sda bs=512 count=1
In summary, the use of dd for boot sector management is a handy tool to have in your linux arsenal.
Next up are some networking topics, such as SSH tunneling, IPSEC VPNs. Keep watching the site, or subscribe to our RSS Feed.