Difference between revisions of "Master Boot Record"

From ArchWiki
Jump to: navigation, search
(Backup and restoration: correct wrong statement of 446 bits)
m (See also: already linked from the top)
 
(37 intermediate revisions by 14 users not shown)
Line 3: Line 3:
 
[[Category:System recovery]]
 
[[Category:System recovery]]
 
[[es:Master Boot Record]]
 
[[es:Master Boot Record]]
 +
[[hu:Master Boot Record]]
 
[[it:Master Boot Record]]
 
[[it:Master Boot Record]]
 
[[ja:Master Boot Record]]
 
[[ja:Master Boot Record]]
 
[[ru:Master Boot Record]]
 
[[ru:Master Boot Record]]
{{Article summary start}}
+
[[zh-CN:Master Boot Record]]
{{Article summary text|An overview of the Master Boot Record; the first sector of a partitioned data storage device.}}
+
{{Related articles start}}
{{Article summary heading|Overview}}
+
{{Related|Arch boot process}}
{{Article summary text|{{Boot process overview}}}}
+
{{Related|GUID Partition Table}}
{{Article summary heading|Related}}
+
{{Related|Partitioning}}
{{Article summary wiki|GUID Partition Table}}
+
{{Related|Unified Extensible Firmware Interface}}
{{Article summary wiki|Unified Extensible Firmware Interface}}
+
{{Related articles end}}
{{Article summary wiki|Arch Boot Process}}
+
{{Article summary end}}
+
  
The Master Boot Record (MBR) is the first 512 bytes of a storage device. The MBR is not a partition; it is reserved for the operating system's bootloader and the storage device's partition table. An newer alternative to MBR is the [[GUID Partition Table]], which is part of the [[Unified Extensible Firmware Interface]] specification.
+
The [[w:Master Boot Record|Master Boot Record]] (MBR) is the first 512 bytes of a storage device. It contains an operating system bootloader and the storage device's partition table. It plays an important role in the [[boot process]] under [[wikipedia:BIOS|BIOS]] systems.
  
== Boot process ==
+
== Introduction ==
  
Booting is a multi-stage process. Most PCs today initialize system devices with firmware called the [http://en.wikipedia.org/wiki/BIOS BIOS] (Basic Input/Output System), which is typically stored in a dedicated ROM chip on the system board. After system devices have been initialized, the BIOS looks for the bootloader on the MBR of the first recognized storage device (hard disk drive, solid state drive, CD/DVD drive, USB drive...) or the first partition of the device. It then executes that program. The bootloader reads the partition table, and is then capable of loading the operating system(s). Common GNU/Linux bootloaders include [[GRUB]] and [[Syslinux]].
+
The MBR partition table stores the partitions info in the first sector of a hard disk as follows:
  
== History ==  
+
{| class="wikitable"
 +
! Location in the HDD  !! Purpose of the Code
 +
|-
 +
| {{ic|001-440 bytes}}  || MBR boot code that is launched by the BIOS.
 +
|-
 +
| {{ic|441-446 bytes}} || MBR disk signature.
 +
|-
 +
| {{ic|447-510 bytes}} || Partition table (of primary and extended partitions, not logical).
 +
|-
 +
| {{ic|511-512 bytes}} || MBR boot signature 0xAA55.
 +
|}
 +
 
 +
The entire information about the primary partitions is limited to the 64 bytes allotted. To extend this, extended partitions were used. An extended partition is simply a primary partition in the MBR which acts like a container for other partitions called logical partitions. So one is limited to either 4 primary partitions, or 3 primary and 1 extended partitions with many logical partitions inside it.
 +
 
 +
=== Problems with MBR ===
 +
 
 +
# Only 4 primary partitions or 3 primary + 1 extended partitions (with arbitrary number of logical partitions within the extended partition) can be defined. If you have 3 primary + 1 extended partitions, and you have some free space outside the extended partition area, you cannot create a new partition over that free space.
 +
# Within the extended partition, the logical partitions' meta-data is stored in a linked-list structure. If one link is lost, all the logical partitions following that metadata are lost.
 +
# MBR supports only 1 byte partition type codes which leads to many collisions.
 +
# MBR stores partition sector information using 32-bit LBA values. This LBA length along with 512 byte sector size (more commonly used) limits the maximum addressable size of the disk to be 2 [[Wikipedia:TiB|TiB]]. Any space beyond 2 TiB cannot be defined as a partition if MBR partitioning is used.
 +
 
 +
[[GUID Partition Table]] is the next generation partitioning scheme designed to succeed the Master Boot Record partitioning scheme method to fix above problems.
 +
 
 +
== History ==
 +
 
 +
{{Remove|Not even close to being a description of MBR's ''history''.}}
  
 
The MBR consists of a short piece of assembly code (the initial bootloader – 446 bytes), a partition table for the 4 primary partitions (16 bytes each) and a ''sentinel'' (0xAA55).
 
The MBR consists of a short piece of assembly code (the initial bootloader – 446 bytes), a partition table for the 4 primary partitions (16 bytes each) and a ''sentinel'' (0xAA55).
Line 36: Line 60:
 
To backup the MBR:
 
To backup the MBR:
  
  dd if=/dev/sda of=/path/mbr-backup bs=512 count=1
+
  # dd if=/dev/sda of=/path/mbr-backup bs=512 count=1
  
 
Restore the MBR:
 
Restore the MBR:
  
  dd if=/path/mbr-backup of=/dev/sda bs=512 count=1
+
  # dd if=/path/mbr-backup of=/dev/sda bs=512 count=1
  
{{Warning|Restoring the MBR with a mismatching partition table will make your data unreadable and nearly impossible to recover. If you simply need to reinstall the bootloader see [[GRUB]] or [[Syslinux]].}}
+
{{Warning|Restoring the MBR with a mismatching partition table will make your data unreadable and nearly impossible to recover. If you simply need to reinstall the bootloader see their respective pages as they also employ the [http://www.pixelbeat.org/docs/disk/ DOS compatibility region]: [[GRUB]] or [[Syslinux]].}}
  
 
To erase the MBR (may be useful if you have to do a full reinstall of another operating system) only the first 446 bytes are zeroed because the rest of the data contains the partition table:
 
To erase the MBR (may be useful if you have to do a full reinstall of another operating system) only the first 446 bytes are zeroed because the rest of the data contains the partition table:
  
  dd if=/dev/zero of=/dev/sda bs=446 count=1
+
  # dd if=/dev/zero of=/dev/sda bs=446 count=1
 +
 
 +
See also [[fdisk#Backup and restore]].
  
 
== Restoring a Windows boot record ==
 
== Restoring a Windows boot record ==
Line 58: Line 84:
 
First, write the partition info (table) again by:
 
First, write the partition info (table) again by:
  
  ms-sys --partition /dev/sda1
+
  # ms-sys --partition /dev/sda1
  
 
Next, write a Windows 2000/XP/2003 MBR:
 
Next, write a Windows 2000/XP/2003 MBR:
  
  ms-sys --mbr /dev/sda  # Read options for different versions
+
  # ms-sys --mbr /dev/sda  # Read options for different versions
  
 
Then, write the new boot sector (boot record):
 
Then, write the new boot sector (boot record):
  
  ms-sys -(1-6)          # Read options to discover the correct FAT record type
+
  # ms-sys -(1-6)          # Read options to discover the correct FAT record type
  
 
{{Ic|ms-sys}} can also write Windows 98, ME, Vista, and 7 MBRs as well, see {{Ic|ms-sys -h}}.
 
{{Ic|ms-sys}} can also write Windows 98, ME, Vista, and 7 MBRs as well, see {{Ic|ms-sys -h}}.
 +
 +
==TestDisk MBRCode==
 +
 +
{{Pkg|testdisk}} from the [[official repositories]] can write the MBR with its own [http://www.cgsecurity.org/wiki/Menu_MBRCode code] (which should be able to boot Windows). The package is also included in the installation media.
  
 
==See also==
 
==See also==
  
 
* [http://kb.iu.edu/data/aijw.html What is a Master Boot Record (MBR)?]
 
* [http://kb.iu.edu/data/aijw.html What is a Master Boot Record (MBR)?]

Latest revision as of 08:20, 31 March 2016

The Master Boot Record (MBR) is the first 512 bytes of a storage device. It contains an operating system bootloader and the storage device's partition table. It plays an important role in the boot process under BIOS systems.

Introduction

The MBR partition table stores the partitions info in the first sector of a hard disk as follows:

Location in the HDD Purpose of the Code
001-440 bytes MBR boot code that is launched by the BIOS.
441-446 bytes MBR disk signature.
447-510 bytes Partition table (of primary and extended partitions, not logical).
511-512 bytes MBR boot signature 0xAA55.

The entire information about the primary partitions is limited to the 64 bytes allotted. To extend this, extended partitions were used. An extended partition is simply a primary partition in the MBR which acts like a container for other partitions called logical partitions. So one is limited to either 4 primary partitions, or 3 primary and 1 extended partitions with many logical partitions inside it.

Problems with MBR

  1. Only 4 primary partitions or 3 primary + 1 extended partitions (with arbitrary number of logical partitions within the extended partition) can be defined. If you have 3 primary + 1 extended partitions, and you have some free space outside the extended partition area, you cannot create a new partition over that free space.
  2. Within the extended partition, the logical partitions' meta-data is stored in a linked-list structure. If one link is lost, all the logical partitions following that metadata are lost.
  3. MBR supports only 1 byte partition type codes which leads to many collisions.
  4. MBR stores partition sector information using 32-bit LBA values. This LBA length along with 512 byte sector size (more commonly used) limits the maximum addressable size of the disk to be 2 TiB. Any space beyond 2 TiB cannot be defined as a partition if MBR partitioning is used.

GUID Partition Table is the next generation partitioning scheme designed to succeed the Master Boot Record partitioning scheme method to fix above problems.

History

Tango-edit-cut.pngThis section is being considered for removal.Tango-edit-cut.png

Reason: Not even close to being a description of MBR's history. (Discuss in Talk:Master Boot Record#)

The MBR consists of a short piece of assembly code (the initial bootloader – 446 bytes), a partition table for the 4 primary partitions (16 bytes each) and a sentinel (0xAA55).

The "Conventional" Windows/DOS MBR bootloader code will check the partition table for one and only one active partition, read X sectors from this partition and then transfer control to the operating system. The Windows/DOS bootloader can not boot an Arch Linux partition because it is not designed to load the Linux kernel, and it can only cater for an active, primary partition (which GRUB safely ignores).

The GRand Unified Bootloader (GRUB) is the de facto standard bootloader for GNU/Linux, and users are recommended to install it on the MBR to allow booting from any partition, whether it be primary or logical.

Backup and restoration

Because the MBR is located on the disk it can be backed up and later recovered.

To backup the MBR:

# dd if=/dev/sda of=/path/mbr-backup bs=512 count=1

Restore the MBR:

# dd if=/path/mbr-backup of=/dev/sda bs=512 count=1
Warning: Restoring the MBR with a mismatching partition table will make your data unreadable and nearly impossible to recover. If you simply need to reinstall the bootloader see their respective pages as they also employ the DOS compatibility region: GRUB or Syslinux.

To erase the MBR (may be useful if you have to do a full reinstall of another operating system) only the first 446 bytes are zeroed because the rest of the data contains the partition table:

# dd if=/dev/zero of=/dev/sda bs=446 count=1

See also fdisk#Backup and restore.

Restoring a Windows boot record

By convention (and for ease of installation), Windows is usually installed on the first partition and installs its partition table and reference to its bootloader to the first sector of that partition. If you accidentally install a bootloader like GRUB to the Windows partition or damage the boot record in some other way, you will need to use a utility to repair it. Microsoft includes a boot sector fix utility FIXBOOT and an MBR fix utility called FIXMBR on their recovery discs, or sometimes on their install discs. Using this method, you can fix the reference on the boot sector of the first partition to the bootloader file and fix the reference on the MBR to the first partition, respectively. After doing this you will have to reinstall GRUB to the MBR as was originally intended (that is, the GRUB bootloader can be assigned to chainload the Windows bootloader).

If you wish to revert back to using Windows, you can use the FIXBOOT command which chains from the MBR to the boot sector of the first partition to restore normal, automatic loading of the Windows operating system.

Of note, there is a Linux utility called ms-sys (package ms-sysAUR in AUR) that can install MBR's. However, this utility is only currently capable of writing new MBRs (all OS's and file systems supported) and boot sectors (a.k.a. boot record; equivalent to using FIXBOOT) for FAT file systems. Most LiveCDs do not have this utility by default, so it will need to be installed first, or you can look at a rescue CD that does have it, such as Parted Magic.

First, write the partition info (table) again by:

# ms-sys --partition /dev/sda1

Next, write a Windows 2000/XP/2003 MBR:

# ms-sys --mbr /dev/sda  # Read options for different versions

Then, write the new boot sector (boot record):

# ms-sys -(1-6)          # Read options to discover the correct FAT record type

ms-sys can also write Windows 98, ME, Vista, and 7 MBRs as well, see ms-sys -h.

TestDisk MBRCode

testdisk from the official repositories can write the MBR with its own code (which should be able to boot Windows). The package is also included in the installation media.

See also