srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 1 | GPT fdisk (aka gdisk and sgdisk) and FixParts |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2 | by Roderick W. Smith, rodsmith@rodsbooks.com |
| 3 | |
| 4 | Introduction |
| 5 | ------------ |
| 6 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 7 | This package includes the source code for three related disk partitioning |
| 8 | programs: |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 9 | |
| 10 | - gdisk -- This program is modeled after Linux fdisk, but it operates on |
| 11 | GUID Partition Table (GPT) disks rather than the Master Boot Record (MBR) |
| 12 | disks that fdisk modifies. As such, gdisk is an interactive text-mode |
| 13 | tool for manipulating partitions, but it does nothing to the contents of |
| 14 | those partitions (usually filesystems, but sometimes swap space or other |
| 15 | data). |
| 16 | |
| 17 | - sgdisk -- This program is conceptually similar to the Linux sfdisk and |
| 18 | FreeBSD gpt programs, but its operational details differ. It enables |
| 19 | manipulation of GPT disks using command-line options, so it's suitable |
| 20 | for use in scripts or by experts to perform specific tasks that might |
| 21 | take several commands in gdisk to accomplish. |
| 22 | |
| 23 | - fixparts -- This program, unlike the preceding two, operates on MBR |
| 24 | disks. It's intended to fix certain problems that can be created by |
| 25 | various utilities. Specifically, it can fix mis-sized extended partitions |
| 26 | and primary partitions located in the middle of extended partitions. It |
| 27 | also enables changing primary vs. logical partition status (within limits |
| 28 | of what's legal in the MBR scheme) and making a few other minor changes. |
| 29 | It does NOT support creating new partitions; for that, you should use |
| 30 | fdisk, parted, or some other tool. |
| 31 | |
| 32 | More details about the abilities of these tools follows. |
| 33 | |
| 34 | All three programs rely on the same set of underlying code base; they |
| 35 | differ only in their control interfaces (defined in gdisk.cc, sgdisk.cc, |
| 36 | and fixparts.cc, respectively) and in which support code they use. |
| 37 | |
| 38 | GPT fdisk (gdisk and sgdisk) Details |
| 39 | ------------------------------------ |
| 40 | |
| 41 | The gdisk program is intended as a (somewhat) fdisk-workalike program for |
| 42 | GPT-partitioned disks, and sgdisk provides most of gdisk's functionality in |
| 43 | a more script-friendly program. Although libparted and programs that use it |
| 44 | (GNU Parted, gparted, etc.) provide the ability to handle GPT disks, they |
| 45 | have certain limitations that gdisk overcomes. Specific advantages of gdisk |
| 46 | and sgdisk include: |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | |
| 48 | * The ability to convert MBR-partitioned disks in-place to GPT format, |
| 49 | without losing data |
| 50 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 51 | * The ability to convert BSD disklabels in-place to create GPT |
| 52 | partitions, without losing data |
| 53 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 54 | * The ability to convert from GPT format to MBR format without data loss |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 55 | |
| 56 | * More flexible specification of filesystem type code GUIDs, which |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 57 | GNU Parted tends to corrupt |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 58 | |
| 59 | * Clear identification of the number of unallocated sectors on a |
| 60 | disk |
| 61 | |
| 62 | * A user interface that's familiar to long-time users of Linux |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 63 | fdisk (gdisk only) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 64 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 65 | * The MBR boot loader code is left alone |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 66 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 67 | * The ability to create a hybrid MBR, which permits GPT-unaware |
| 68 | OSes to access up to three GPT partitions on the disk |
| 69 | |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 70 | Of course, GPT fdisk isn't without its limitations. Most notably, it lacks |
| 71 | the filesystem awareness and filesystem-related features of GNU Parted. You |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 72 | can't resize a partition's filesystem or create a partition with a |
| 73 | filesystem already in place with gdisk, for instance. There's no GUI |
| 74 | version of gdisk. |
| 75 | |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 76 | The GPT fdisk package provides two program files: the interactive text-mode |
| 77 | gdisk and the command-line-driven sgdisk. The former is intended for use in |
| 78 | manually partitioning disks or changing partitioning details; the latter is |
| 79 | intended for use in scripts to help automate tasks such as disk cloning or |
| 80 | preparing multiple disks for Linux installation. |
| 81 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 82 | FixParts Details |
| 83 | ---------------- |
| 84 | |
| 85 | This program's creation was motivated by cries for help I've seen in online |
| 86 | forums from users who have found their partition tables to be corrupted by |
| 87 | various buggy partitioning tools. Although most OSes can handle the |
| 88 | afflicted disks fine, libparted-based tools (GParted, parted, most Linux |
| 89 | installers, etc.) tend to flake out when presented with these disks. |
| 90 | Typically, the symptom is a disk that appears to hold no partitions; |
| 91 | however, sometimes the libparted tool presents partitions other than those |
| 92 | that the OS sees. |
| 93 | |
| 94 | I've observed four causes of these symptoms, three of which FixParts can |
| 95 | correct: |
| 96 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 97 | * Old GPT data -- If a disk is used as a GPT disk and then re-used as an |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 98 | MBR disk, the GPT data may be incompletely erased. This happens if the |
| 99 | disk is repartitioned with fdisk or the Microsoft Windows installer, for |
| 100 | instance. (Tools based on libparted correctly remove the old GPT data |
| 101 | when converting from GPT to MBR format.) FixParts checks for this problem |
| 102 | when it starts and offers to correct it. If you opt to erase the GPT |
| 103 | data, this erasure occurs immediately, unlike other changes the program |
| 104 | makes. |
| 105 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 106 | * Mis-sized extended partitions -- Some tools create an extended partition |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 107 | that's too large, typically ending after the last sector of the disk. |
| 108 | FixParts automatically corrects this problem (if you use the 'w' option |
| 109 | to save the partition table). |
| 110 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 111 | * Primary partitions inside an extended partition -- Some utilities create |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 112 | or move primary partitions to within the range covered by the extended |
| 113 | partition. FixParts can usually correct this problem by turning the |
| 114 | primary partition into a logical partition or by changing one or more |
| 115 | other logical partitions into primaries. Such corrections aren't always |
| 116 | possible, though, at least not without deleting or resizing other |
| 117 | partitions. |
| 118 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 119 | * Leftover RAID data -- If a disk is used in a RAID array and then re-used |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 120 | as a non-RAID disk, some utilities can become confused and fail to see |
| 121 | the disk. FixParts can NOT correct this problem. You must destroy the old |
| 122 | RAID data, or possibly remove the dmraid package from the system, to fix |
| 123 | this problem. |
| 124 | |
| 125 | When run, FixParts presents an fdisk-like interface, enabling you to adjust |
| 126 | partition types (primary, logical, or omitted), change type codes, change |
| 127 | the bootable flag, and so on. Although you can delete a partition (by |
| 128 | omitting it), you can't create new partitions with the program. If you're |
| 129 | used to partitioning disks, particularly with Linux fdisk, two unusual |
| 130 | features of FixParts require elaboration: |
| 131 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 132 | * No extended partitions -- Internally, FixParts reads the partition table |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 133 | and discards data on any extended partition(s) it finds. When you save |
| 134 | the partition table, the program generates a new extended partition. This |
| 135 | design means that the program automatically corrects many problems |
| 136 | related to the extended partition. It also means that you'll see no |
| 137 | evidence of extended partitions in the FixParts user interface, although |
| 138 | it keeps track of the requirements and prevents you from creating illegal |
| 139 | layouts, such as a primary between two logicals. |
| 140 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 141 | * Partition numbering -- In most Linux tools, partitions 1-4 are primaries |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 142 | and partitions 5 and up are logicals. Although a legal partition table |
| 143 | loaded into FixParts will initially conform to this convention, some |
| 144 | types of damaged table might not, and various changes you make can also |
| 145 | cause deviations. When FixParts writes the partition table, its numbering |
| 146 | will be altered to conform to the standard MBR conventions, but you |
| 147 | should use the explicit labeling of partitions as primary or logical |
| 148 | rather than the partition numbers to determine a partition's status. |
| 149 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 150 | Installing |
| 151 | ---------- |
| 152 | |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 153 | To compile GPT fdisk, you must have appropriate development tools |
| 154 | installed, most notably the GNU Compiler Collection (GCC) and its g++ |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 155 | compiler for C++. (Under Windows, Microsoft Visual C++ 2008 can also be |
| 156 | used.) In addition, note these requirements: |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 157 | |
| 158 | * On Linux, FreeBSD, and OS X, libuuid must be installed. This is the |
| 159 | standard for Linux and OS X, although you may need to install a package |
| 160 | called uuid-dev or something similar to get the headers. On FreeBSD, the |
| 161 | e2fsprogs-libuuid port must be installed. |
| 162 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 163 | * The ICU library (http://site.icu-project.org), which provides support for |
| 164 | Unicode partition names, is recommended on all |
| 165 | platforms except Windows. This library is normally installed in Linux and |
| 166 | OS X, but you may need to install the development headers (libicu-dev or |
| 167 | something similar in Linux; or the libicu36-dev Fink package in OS X). To |
| 168 | compile without ICU support, you must modify the Makefile: Remove the |
| 169 | "-D USE_UTF16" part from the CXXFLAGS line and remove references to |
| 170 | -licuio, -licuuc, -licudata, and -licucore (details vary between |
| 171 | platforms) from the compilation options. Suitable lines are present, but |
| 172 | commented out, in the Makefile, Makefile.mac, and Makefile.bsd files. |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 173 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 174 | * The sgdisk program also requires the popt library and its development |
| 175 | files (headers). Most Linux distributions install popt by default, but |
| 176 | you may need to install a package called popt-dev, popt-devel, or |
| 177 | something similar to obtain the header files. Mac OS users can find a |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 178 | version of popt for Mac OS from Drawin Ports (http://popt.darwinports.com) |
| 179 | or Fink (http://www.finkproject.org); however, you'll first need to |
| 180 | install DarwinPorts or Fink (instructions exist on the relevant projects' |
| 181 | pages). Alternatively, you can compile gdisk alone, without sgdisk; gdisk |
| 182 | doesn't require popt. |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 183 | |
| 184 | When all the necessary development tools and libraries are installed, you |
| 185 | can uncompress the package and type "make" at the command prompt in the |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 186 | resulting directory. (You may need to type "make -f Makefile.mac" on Mac OS |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 187 | X, "make -f Makefile.freebsd" on FreeBSD, or "make -f Makefile.mingw" to |
| 188 | compile using MinGW for Windows.) You may also need to add header (include) |
| 189 | directories or library directories by setting the CXXFLAGS environment |
| 190 | variable or by editing the Makefile. The result should be program files |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 191 | called gdisk, sgdisk, and fixparts. Typing "make gdisk", "make sgdisk", or |
| 192 | "make fixparts" will compile only the requested programs. You can use these |
| 193 | programs in place or copy the files to a suitable directory, such as |
| 194 | /usr/local/sbin. You can copy the man pages (gdisk.8, sgdisk.8, and |
| 195 | fixparts.8) to /usr/local/man/man8 to make them available. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 196 | |
| 197 | Caveats |
| 198 | ------- |
| 199 | |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 200 | THIS SOFTWARE IS BETA SOFTWARE! IF IT WIPES OUT YOUR HARD DISK OR EATS YOUR |
| 201 | CAT, DON'T BLAME ME! To date, I've tested the software on several USB flash |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 202 | drives, physical hard disks, and virtual disks in the QEMU and VirtualBox |
| 203 | environments. Many others have now used the software on their computers, as |
| 204 | well. I believe all data-corruption bugs to be squashed, but I know full well |
| 205 | that the odds of my missing something are high. This is particularly true for |
| 206 | large (over-2TiB) drives; my only direct testing with such disks is with |
| 207 | virtual QEMU and VirtualBox disks. I've received user reports of success with |
| 208 | RAID arrays over 2TiB in size, though. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 209 | |
| 210 | My main development platform is a system running the 64-bit version of |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 211 | Gentoo Linux (previously Ubuntu 8.04). I've also tested on several other |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 212 | 32- and 64-bit Linux distributions, Intel-based Mac OS X 10.5 and 10.6, |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 213 | 64-bit FreeBSD 7.1, and Windows 7. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 214 | |
| 215 | Redistribution |
| 216 | -------------- |
| 217 | |
| 218 | This program is licensed under terms of the GNU GPL (see the file COPYING). |
| 219 | |
| 220 | Acknowledgements |
| 221 | ---------------- |
| 222 | |
| 223 | This code is mostly my own; however, I've used three functions from two |
| 224 | other GPLed programs: |
| 225 | |
| 226 | - The code used to generate CRCs is taken from the efone program by |
| 227 | Krzysztof Dabrowski and ElysiuM deeZine. (See the crc32.h and |
| 228 | crc32.cc source code files.) |
| 229 | |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 230 | - A function to find the disk size is taken from Linux fdisk by A. V. Le |
| 231 | Blanc. This code has subsequently been heavily modified. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 232 | |
| 233 | Additional code contributors include: |
| 234 | |
| 235 | - Yves Blusseau (1otnwmz02@sneakemail.com) |
| 236 | |
srs5694 | 7f244ba | 2009-08-18 14:22:12 -0400 | [diff] [blame] | 237 | - David Hubbard (david.c.hubbard@gmail.com) |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 238 | |
| 239 | - Justin Maggard (justin.maggard@netgear.com) |
| 240 | |
| 241 | - Dwight Schauer (dschauer@ti.com) |
| 242 | |
| 243 | - Florian Zumbiehl (florz@florz.de) |