LinuxPowerUser.com

Tag: disk

Boot Sector Management

by on Jan.27, 2010, under Hardware/Disk Management

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

FDISK /MBR

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:

Size (bytes) Description
446 Executable code section
4 Optional Disk signature
2 Usually nulls
64 Partition table
2 MBR 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.

Leave a Comment :, , more...

Disk and Partition Imaging using dd

by on Jan.26, 2010, under Hardware/Disk Management, Server Administration

Linux provides an abundance of advanced command line tools to manage and modify just about anything on your system.  Today we will explore the use of dd, the primary tool on linux for creating and restoring disk images, among other things.

The dd (diskdump) on Linux can be used to backup an entire disk or partition to an image file. Several caveats apply to this method:

  1. The disk in question can not be in use by an operating system
  2. A destination medium or network resource must be present that is large enough to hold the image.

To backup a disk using dd, the following procedure can be used.

  1. Boot the computer with the disk in question from a Linux Live CD, such as Ubuntu or Knoppix
  2. Mount a destination disk (such as a usb disk drive or nfs mount)
  3. Run dd command to backup disk
  4. Note the size of the disk partition if partitioning a new device is necessary when restoring the image

Here is an example session, to back up a single partition (sda1) containing a Windows XP installation to a USB hard disk mounted at /mnt/sdb1:

As a root user, do the following:

Mount USB disk drive

# mount -t ext3 /dev/sdb1 /mnt/sdb1

Run dd command (piping output through gzip to save space):

# dd if=/dev/sda1 conv=sync,noerror bs=64k | gzip -c > /mnt/sdb1/windowsxp-c.img.gz

Definition of the dd command parameters:

“if=/dev/sda1” is the input file for the dd command, in this case, its linux device sda1
“conv=sync,noerror instructs dd that if it can’t read a block due to an error then it should at least write something to its output of the correct length.

Even if your Hard disk exhibits no errors, dd will read every single block, including any which the OS avoids because it has marked them as bad.

“bs=64k” is the block size of 64 kilobytes. Using a large block size speeds up the copy process. The output of this is then passed to gzip for compression and storage in a file on the destination device.

Noting Partition configuration:

Using the command fdisk -l /dev/<device> where <device> is the device node of the disk being backed up, make note of the number of blocks used to create the partition:

# fdisk -l /dev/sda


Disk /dev/sda: 959.9 GB, 959966085120 bytes
255 heads, 63 sectors/track, 116709 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x2e2d2e2d


Device Boot Start  End   Blocks     Id System
/dev/sda1*      1  15298 122881153+ 7  HPFS/NTFS
/dev/sda2   15299  51771 292969372+ 83 Linux
/dev/sda3   51772  52767   8000370  82 Linux swap / Solaris
/dev/sda4   52768 116709 513614115  83 Linux

The destination disk should have a partition defined identical to the source partition, total number of blocks is the important parameter here.

The partition geometry information can be backed up the the USB hard disk with the following command:

# fdisk -l /dev/sda > /mnt/sdb1/sda_fdisk.txt

Restoring a dd image to a disk/partition

The steps are similar to the backup process:

  1. Boot computer with destination disk from a Linux Live CD
  2. Partition the destination disk, if needed
  3. Mount the source media (usb disk or nfs mount)
  4. Use gunzip and dd to restore image to disk or partition

Here is an example session, to restore the image taken with the above steps:

As a root user, do the following:

Mount image source (USB hard disk at /dev/sdb1)

# mount -t ext3 /dev/sdb1 /mnt/sdb1

Partition destination disk:

# fdisk /dev/<device node in question>; in our case, sda.

<create partition if needed>

Restore image (destination partition is /dev/sda1)

# gunzip -c /mnt/sdb1/windowsxp-c.img.gz | dd of=/dev/sda1 conv=sync,noerror bs=64k

Note: On a fast machine, ie C2Q 6600, and 3ware RAID disk array, a 120GB image takes 25 minutes to create.

In order to have a bootable system, some other configuration may be needed such as restoring a boot block. Check out the next post for details on boot sector management with dd.

A excellent guide on using fdisk for disk partitioning can be found here.

Leave a Comment :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...