diff --git a/fixparts.8 b/fixparts.8 new file mode 100644 index 0000000..e11a1a5 --- /dev/null +++ b/fixparts.8 @@ -0,0 +1,282 @@ +.\" Copyright 2011 Roderick W. Smith (rodsmith@rodsbooks.com) +.\" May be distributed under the GNU General Public License +.TH "FIXPARTS" "8" "0.7.0" "Roderick W. Smith" "FixParts Manual" +.SH "NAME" +fixparts \- MBR partition table repair utility +.SH "SYNOPSIS" +.BI "fixparts " +.I device + +.SH "DESCRIPTION" + +FixParts (aka \fBfixparts\fR) is a text\-mode menu\-driven program for +repairing certain types of problems with Master Boot Record (MBR) partition +tables. The program has three design goals, although a few additional +features are supported, as well: + +.TP +.B * +It can remove stray GUID Partition Table (GPT) data, which can be left +behind on a disk that was once used as a GPT disk but then incompletely +converted to the more common (as of 2011) MBR form. + +.TP +.B * +It can repair mis-sized extended partitions \-\- either partitions that +extend beyond the physical end of the disk or that overlap with nearby +primary partitions. FixParts is designed in such a way that this type of +repair occurs automatically, so if it's the only problem with your disk, +you can launch the program and then immediately save the partition table, +making no manual changes, and the program will fix the problem. + +.TP +.B * +You can change primary partitions into logical partitions or vice\-versa, +within constraints imposed by the MBR data structures. + +.PP + +Additional features include the ability to change partition type codes or +boot/active flags, to delete partitions, and to recompute CHS values. With +the possible exception of recomputing CHS values, these secondary features +are better performed with \fBfdisk\fR, because \fBfixpart\fR's design means +that it's likely to alter partition numbering even when such changes are +not requested. + +The \fBfixpart\fR program employs a user interface similar to that of +Linux's \fBfdisk\fR, but \fBfixpart\fR is much more specialized. Most +importantly, you can't create new partitions with \fBfixpart\fR, although +you can change primary/logical assignment. + +In the MBR scheme, partitions come in three varieties: + +.TP +.B primary +These partitions are defined in the first sector of the hard disk and +are limited in number to four. Some OSes, such as Windows and FreeBSD, must +boot from a primary partition. + +.TP +.B extended +Extended partitions are specialized primary partitions. They serve as +holding areas for logical partitions. + +.TP +.B logical +A disk can contain an arbitrary number of logical partitions +(\fBfixpart\fR, however, imposes a limit of 124 logical partitions). All +the logical partitions reside inside a single extended partition, and are +defined using a linked-list data structure. This fact means that every +logical partition be preceded by at least one sector of unallocated space +to hold its defining data structure (an Extended Boot Record, or EBR). + +.PP + +These distinctions mean that primary and logical partitions cannot be +arbitrarily interspersed. A disk can contain one to three primary +partitions, a block of one or more logical partitions, and one to three +more primary partitions (for a total of three primary partitions, not +counting the extended partition). Primary partitions may not be sandwiched +between logical partitions, since this would mean placing a primary +partition within an extended partition (which is just a specific type of +primary partition). + +Unlike most disk utilities, \fBfixpart\fR's user interface ignores extended +partitions. Internally, the program discards the information on the +original extended partition and, when you tell it to save its changes, it +generates a new extended partition to contain the then-defined logical +partitions. This is done because most of the repairs and manipulations the +tool performs require generating a fresh extended partition, so keeping the +original in the user interface would only be a complication. + +Another unusual feature of \fBfixpart\fR's user interface is that partition +numbers do not necessarily correlate with primary/logical status. In most +utilities, partitions 1\-4 correspond to primary partitions, whereas +partitions 5 and up are logical partitions. In \fBfixpart\fR, any partition +number may be assigned primary or logical status, so long as the rules for +layout described earlier are obeyed. When the partition table is saved, +partitions will be assigned appropriately and then tools such as the Linux +kernel and \fBfdisk\fR will give them conventional numbers. + +When it first starts, \fBfixpart\fR performs a scan for GPT data. If the +disk looks like a conventional GPT disk, \fBfixpart\fR refuses to run. If +the disk appears to be a conventional MBR disk but GPT signatures are +present in the GPT primary or secondary header areas, \fBfixpart\fR +offers to delete this extraneous data. If you tell it to do so, the program +immediately wipes the GPT header or headers. (If only one header was found, +only that one header will be erased, to minimize the risk of damaging a +boot loader or other data that might have overwritten just one of the GPT +headers.) + +With the exception of optionally erasing leftover GPT data when it first +starts, \fBfixpart\fR keeps all changes in memory until the user writes +changes with the \fBw\fR command. Thus, you can adjust your partitions in +the user interface and abort those changes by typing \fBq\fR to quit +without saving changes. + +.SH "OPTIONS" + +The \fBfixparts\fR utility supports no command\-line options, except for +specification of the target device. + +Most interactions with \fBfixparts\fR occur with its interactive text\-mode +menu. Specific functions are: + +.TP +.B a +Toggle the active/boot flag. This flag is required by some boot loaders and +OSes. + + +.TP +.B c +Recompute the cylinder/head/sector (CHS) values for all partitions. CHS +addressing mode is largely obsolete, but some OSes and utilities complain +if they don't like the CHS values. Note that \fBfixparts\fR' CHS values are +likely to be incorrect on disks smaller than about 8 GiB except on Linux. + +.TP +.B l +Change a partition's status to logical. This option will only work if the +current partition layout supports such a change. Note that if changing a +partition's status in this way is not currently possible, making some other +change may make it possible. For instance, omitting a partition that +precedes the target partition may enable converting a partition to logical +form if there had been no free sectors between the two partitions. + +.TP +.B o +Omit a partition. Once omitted, the partition will still appear in the +\fBfixpart\fR partition list, but it will be flagged as omitted. You can +subsequently convert it to primary or logical form with the \fBr\fR or +\fBl\fR commands, respectively. When you save your changes with \fBw\fR, +though, the partition will be lost. + +.TP +.B p +Display basic partition summary data. This includes partition's number, the +boot/active flag's status, starting and ending sector numbers, +primary/logical/omitted status, whether or not the partition may be +converted to logical form, and the partition's MBR types code. + +.TP +.B q +Quit from the program \fIwithout saving your changes\fR. +Use this option if you just wanted to view information or if you make a +mistake and want to back out of all your changes. + +.TP +.B r +Change a partition's status to primary. This option will only work if the +current partition layout supports such a change. Note that every partition +can theoretically become a primary partition, although in some +configurations, making this change will require omitting some partitions. +If \fBfixparts\fR refuses to allow changing a partition to primary, you may +need to convert other partitions to logical form or omit them entirely. + +.TP +.B s +Sort partition entries. This option orders partitions in the display to +match their on-disk positions, which can make understanding the disk layout +easier in some cases. This option has no effect on the ultimate ordering of +logical partitions, which are sorted before being saved. The order of +primary partitions in the final saved partition table may be affected by +this option. In both cases, as already noted, the partition numbers +displayed by \fBfixparts\fR may not be the same as those used by the kernel +or displayed by other partitioning tools. + +.TP +.B t +Change a partition's type code. You enter the type code using a one\-byte +hexadecimal number. + +.TP +.B w +Write data. Use this command to save your changes and exit from the program. + +.TP +.B ? +Print the menu. Type this command (or any other unrecognized command) to +see a summary of available options. + +.PP + +.SH "BUGS" +As of March 2011 (version 0.7.0), \fBfixparts\fR +should be considered beta software. Known bugs and limitations include: + +.TP +.B * +The program compiles correctly only on Linux, FreeBSD, Mac OS X, and Windows. +Linux versions for x86\-64 (64\-bit), x86 (32\-bit), and PowerPC (32\-bit) have been +tested, with the x86\-64 version having seen the most testing. Under FreeBSD, +32\-bit (x86) and 64\-bit (x86\-64) versions have been tested. Only 32\-bit +versions for Mac OS X and Windows have been tested. + +.TP +.B * +The FreeBSD version of the program can't write changes to the partition +table to a disk when existing partitions on that disk are mounted. (The +same problem exists with many other FreeBSD utilities, such as +\fBgpt\fR, \fBfdisk\fR, and \fBdd\fR.) This limitation can be overcome +by typing \fBsysctl kern.geom.debugflags=16\fR at a shell prompt. + +.TP +.B * +The program can load only up to 128 partitions (4 primary partitions and +124 logical partitions). This limit can be raised by changing the +\fI#define MAX_MBR_PARTS\fR line in the \fIbasicmbr.h\fR source code file +and recompiling. + +.TP +.B * +The program can read partitions only if the disk has correct LBA partition +descriptors. These descriptors should be present on any disk over 8 GiB in +size or on smaller disks partitioned with any but very ancient software. + +.TP +.B * +The program makes no effort to preserve partition numbers. This can have +consequences for boot loaders and for mounting filesystems via +\fB/etc/fstab\fR. It may be necessary to edit configuration files or even +to re-install your boot loader. + +.TP +.B * + +The program may change the order of partitions in the partition table. + +.PP + +.SH "AUTHORS" +Primary author: Roderick W. Smith (rodsmith@rodsbooks.com) + +Contributors: + +* Yves Blusseau (1otnwmz02@sneakemail.com) + +* David Hubbard (david.c.hubbard@gmail.com) + +* Justin Maggard (justin.maggard@netgear.com) + +* Dwight Schauer (dschauer@ti.com) + +* Florian Zumbiehl (florz@florz.de) + + +.SH "SEE ALSO" +\fBcfdisk (8)\fR, +\fBfdisk (8)\fR, +\fBmkfs (8)\fR, +\fBparted (8)\fR, +\fBsfdisk (8)\fR +\fBgdisk (8)\fR +\fBsgdisk (8)\fR + +\fIhttp://en.wikipedia.org/wiki/Master_boot_record\fR + +\fIhttp://www.rodsbooks.com/fixparts/\fR + +.SH "AVAILABILITY" +The \fBfixparts\fR command is part of the \fIGPT fdisk\fR package and is +available from Rod Smith. diff --git a/fixparts.cc b/fixparts.cc new file mode 100644 index 0000000..39d1fb5 --- /dev/null +++ b/fixparts.cc @@ -0,0 +1,83 @@ +// fixparts +// Program to fix certain types of damaged Master Boot Record (MBR) partition +// tables +// +// Copyright 2011 by Roderick W. Smith +// +// This program is distributed under the terms of the GNU GPL, as described +// in the COPYING file. +// +// Based on C++ classes originally created for GPT fdisk (gdisk and sgdisk) +// programs + +#include +#include +#include +#include +#include +#include "basicmbr.h" +#include "support.h" + +using namespace std; + +int main(int argc, char* argv[]) { + BasicMBRData mbrTable; + char *device = NULL; + int i, doItAgain; + + switch (argc) { + case 1: + cout << "Type device filename, or press to exit: "; + device = new char[255]; + ReadCString(device, 255); + i = strlen(device); + if (i && device[i - 1] == '\n') + device[i - 1] = '\0'; + break; + case 2: + device = new char[strlen(argv[1]) + 1]; + strcpy(device, argv[1]); + break; + default: + cerr << "Usage: " << argv[0] << " device_filename\n"; + exit(1); + } // switch + + cout << "FixParts " << GPTFDISK_VERSION << "\n"; + cout << "\nLoading MBR data from " << device << "\n"; + mbrTable.ReadMBRData(device); + if (mbrTable.CheckForGPT() > 0) { + if ((mbrTable.GetValidity() == hybrid) || (mbrTable.GetValidity() == gpt)) { + cerr << "\nThis disk appears to be a GPT disk. Use GNU Parted or GPT fdisk on it!\n"; + cerr << "Exiting!\n\n"; + exit(1); + } else { + cout << "\nNOTICE: GPT signatures detected on the disk, but no 0xEE protective " + << "partition!\nThe GPT signatures are probably left over from a previous " + << "partition table.\nDo you want to delete them (if you answer 'Y', this " + << "will happen\nimmediately)? "; + if (GetYN() == 'Y') { + cout << "Erasing GPT data!\n"; + if (mbrTable.BlankGPTData() != 1) + cerr << "GPT signature erasure failed!\n"; + } // if + } // if/else + } // if + mbrTable.MakeItLegal(); + do { + doItAgain = 0; + if (mbrTable.DoMenu() > 0) { + cout << "\nFinal checks complete. About to write MBR data. THIS WILL OVERWRITE EXISTING\n" + << "PARTITIONS!!\n\nDo you want to proceed? "; + if (GetYN() == 'Y') { + mbrTable.WriteMBRData(); + mbrTable.DiskSync(); + doItAgain = 0; + } else { + doItAgain = 1; + } // else + } // if + } while (doItAgain); + delete[] device; + return 0; +} // main()