MBR partition maintenance
On the i386 and other architectures, sector 0 of a bootable hard disk must contain MBR bootcode, the MBR partition table containing 4 slots, and a specific magic number (0xAA55). The 4 slots in the MBR partition table allow a disk drive to be divided into chunks known as MBR partitions.
On the i386, the BIOS loads sector 0 of the boot disk into memory,
verifies the magic number, and begins executing the MBR bootcode at the
first byte. The MBR bootcode then searches the MBR partition table for an
“active” MBR partition (indicated by a
*’ in the first column), and if one
is found, the boot block from that MBR partition is loaded and executed in
place of the original (MBR) boot block.
Some other architectures (like the zaurus), consider sector 0 of the disk to contain the MBR partition table, but do not use the MBR bootcode at all.
Upon first access to a disk, the partition information is retrieved, typically in disklabel(5) format. The location of the disklabel can vary from architecture to architecture, but if one is not found the existence of an MBR partition table will create a spoofed prototypical disklabel which can be viewed using disklabel(8). This spoofing mechanism is useful for permitting partition access for devices which would not normally have a disklabel(5) sector.
The options are as follows:
- Specifies an alternate BIOS geometry for
fdiskto use. By default, an automatic calculation of disk size will be built using heuristics. These figures are taken from the in-core disklabel (see disklabel(8)), or values that /boot has passed to the kernel.
- Use the
fdiskinteractive editor to modify an MBR partition table. The editor permits configuration of the MBR partition, as well as extended MBR partitions. See COMMAND MODE, below, for more information.
- Specifies an alternate MBR template file. The default file is /usr/mdec/mbr.
- Requests that the MBR partition data be re-initialized. In this mode,
fdiskwill completely overwrite the primary MBR bootcode and MBR partition table using the default MBR template /usr/mdec/mbr (or the one optionally specified by the
-fflag). In the default template, MBR partition number 3 will be configured as an OpenBSD MBR partition spanning the entire disk, except for a zone left at the start for booting. This mode is designed to initialize the MBR the very first time.
- Update MBR bootcode, preserving existing MBR partition table. The MBR
bootcode extends from offset 0x000 to the start of the MBR partition table
at offset 0x1BE. It is similar to the
-iflag, except the existing MBR partition table is preserved. This is useful for writing new MBR bootcode onto an existing drive, and is equivalent to the DOS command “FDISK /MBR”. Note that this option will overwrite the NT disk signature, if present.
- Avoid asking yes/no questions when not desirable.
- Specify the disk to operate on. It can be specified either by its full pathname or an abbreviated disk form. In its abbreviated form, the path to the device, the ‘r’ denoting "raw device", and the partition letter, can all be omitted. For example, the first IDE disk can be specified as either /dev/rwd0c, /dev/wd0c, or wd0.
When called with no special flags,
prints the MBR partition table of the specified disk:
# fdisk sd0 Disk: sd0 geometry: 121601/255/63 [1953525168 Sectors] Offset: 0 Signature: 0xAA55 Starting Ending LBA Info: #: id C H S - C H S [ start: size ] ------------------------------------------------------------------------ 0: 0B 0 1 1 - 26108 0 63 [ 63: 419425020 ] Win95 FAT-32 1: 00 0 0 0 - 0 0 0 [ 0: 0 ] unused 2: 00 0 0 0 - 0 0 0 [ 0: 0 ] unused *3: A6 26108 1 1 - 121600 254 63 [ 419425083: 1534094982 ] OpenBSD
This 1953525168 sector (931GB) disk drive is divided into two MBR partitions that span the whole disk. The first MBR partition is a 200GB FAT-32 partition, the second is a 731GB OpenBSD MBR partition using the remainder of the disk. The fields of the output are:
- Number identifying each MBR partition table entry. There are a total of four slots. ‘*’ denotes the MBR partition which is declared bootable.
- MBR partition type identifier. OpenBSD reserves the magic number hexadecimal A6 (166 decimal).
- These fields provide the starting and ending address of the MBR partition in BIOS geometry.
- These fields provide the starting sector and size in sectors of the MBR partition in linear block addresses.
NOTE: The BIOS geometry sectors field (C/H/S) is “1 based”, but the LBA "start" field is “0 based”.
The CHS values will need to be in the BIOS's geometry for the system to be able to boot and use the drive correctly. These values must be kept correctly synchronized or a variety of problems develop which are very difficult to diagnose.
The OpenBSD MBR partition shown above is subdivided further using the functionality provided by disklabel(8), which provides OpenBSD partitions.
# /dev/rsd0c: type: SCSI disk: SCSI disk label: WDC WD10EADS-65L duid: 085ef8d68623f5b3 flags: bytes/sector: 512 sectors/track: 63 tracks/cylinder: 255 sectors/cylinder: 16065 cylinders: 121601 total sectors: 1953525168 boundstart: 419425083 boundend: 1953520065 drivedata: 0 16 partitions: # size offset fstype [fsize bsize cpg] a: 2097125 419425083 4.2BSD 2048 16384 1 b: 4715520 421522208 swap c: 1953525168 0 unused d: 8388608 426237728 4.2BSD 2048 16384 1 e: 16771072 434626336 4.2BSD 2048 16384 1 f: 4194304 451397408 4.2BSD 2048 16384 1 g: 2097152 455591712 4.2BSD 2048 16384 1 h: 20971520 457688864 4.2BSD 2048 16384 1 i: 419425020 63 MSDOS j: 4194304 478660384 4.2BSD 2048 16384 1 k: 4194304 482854688 4.2BSD 2048 16384 1 l: 629145536 487049024 4.2BSD 4096 32768 1
These OpenBSD partitions are then mounted as follows using /etc/fstab:
/dev/sd0a / ffs rw,softdep 1 1 /dev/sd0d /tmp ffs rw,softdep,nodev,nosuid 1 2 /dev/sd0e /var ffs rw,softdep,nodev,nosuid 1 2 /dev/sd0f /usr ffs rw,softdep,nodev 1 2 /dev/sd0g /usr/X11R6 ffs rw,softdep,nodev 1 2 /dev/sd0h /usr/local ffs rw,softdep,nodev 1 2 /dev/sd0i /mnt/example msdos rw,nodev,nosuid 1 2 /dev/sd0j /usr/src ffs rw,softdep,nodev,nosuid 1 2 /dev/sd0k /usr/obj ffs rw,softdep,nodev,nosuid 1 2 /dev/sd0l /home ffs rw,softdep,nodev,nosuid 1 2
-e flag causes
fdisk to enter an interactive command mode. The
prompt contains information about the state of the edit process.
‘*’ means that the in-memory copy of the boot block has been modified, but not yet written to disk.
1 is the disk offset of the currently selected boot block being edited. This number will be 2 when editing an extended MBR partition, 3 when editing an extended MBR partition within an extended MBR partition, and so on.
The list of commands and their explanations are given below. Commands may be abbreviated provided enough characters are given to ensure unambiguity.
- A synonym for
- Display a list of commands that
fdiskunderstands in the interactive edit mode.
- Display this manual page.
- Initialize the currently selected, in-memory copy of the boot block.
- Display the current drive geometry that
fdiskprobed using kernel provided information and various heuristics. The disk geometry may be changed at this point.
- Edit a given table entry in the memory copy of the current boot block. Sizes may be adjusted in BIOS geometry mode or using sector offsets and sizes. A unit ‘b’, ‘k’, ‘m’, or ‘g’ may be appended to indicate bytes, kilobytes, megabytes, or gigabytes. The special size value ‘*’ will cause the partition to be sized to use the remainder of the disk.
- Make the given MBR partition table entry bootable and mark all others as not bootable (only one entry can be marked bootable). If a value of 0 is given, the MBR partition is marked as not bootable, but no other MBR partitions are touched.
- Update the machine MBR bootcode and 0xAA55 signature in the memory copy of the currently selected boot block. Note that this option will overwrite an NT disk signature, if present.
- Select and load into memory the boot block pointed to by the extended MBR partition table entry in the current boot block.
- Change the MBR partition identifier of the given MBR partition table entry. This command is particularly useful for reassigning an existing MBR partition to OpenBSD.
- Swap two MBR entries.
- Print the currently selected in-memory copy of the boot block and its MBR table to the terminal. A unit ‘b’, ‘k’, ‘m’, or ‘g’ may be appended to indicate bytes, kilobytes, megabytes, or gigabytes. Otherwise the number of sectors is printed.
- Write the in-memory copy of the boot block to disk.
- Exit the current level of
fdisk, either returning to the previously selected in-memory copy of a boot block, or exiting the program if there is none.
- Exit the current level of
fdisk, either returning to the previously selected in-memory copy of a boot block, or exiting the program if there is none. Unlike exit it does write the modified block out.
- Quit program without saving current changes.
- default MBR template
fstab(5), boot_amd64(8), boot_armish(8), boot_i386(8), boot_landisk(8), boot_macppc(8), boot_zaurus(8), disklabel(8)
Hand crafted disk layouts are highly error prone. It is common practice, though by no means required, that MBR partitions start on a cylinder boundary (generally head 0, sector 1, but head 1, sector 1 for track 0), and that MBR partitions also end at cylinder boundaries.