1cf200d32Sopenharmony_ciGPT fdisk (aka gdisk, cgdisk, and sgdisk) and FixParts 2cf200d32Sopenharmony_ciby Roderick W. Smith, rodsmith@rodsbooks.com 3cf200d32Sopenharmony_ci 4cf200d32Sopenharmony_ciIntroduction 5cf200d32Sopenharmony_ci------------ 6cf200d32Sopenharmony_ci 7cf200d32Sopenharmony_ciThis package includes the source code for four related disk partitioning 8cf200d32Sopenharmony_ciprograms: 9cf200d32Sopenharmony_ci 10cf200d32Sopenharmony_ci- gdisk -- This program is modeled after Linux fdisk, but it operates on 11cf200d32Sopenharmony_ci GUID Partition Table (GPT) disks rather than the Master Boot Record (MBR) 12cf200d32Sopenharmony_ci disks that fdisk modifies. As such, gdisk is an interactive text-mode 13cf200d32Sopenharmony_ci tool for manipulating partitions, but it does nothing to the contents of 14cf200d32Sopenharmony_ci those partitions (usually filesystems, but sometimes swap space or other 15cf200d32Sopenharmony_ci data). 16cf200d32Sopenharmony_ci 17cf200d32Sopenharmony_ci- cgdisk -- This program is modeled after Linux cfdisk, but it operates on 18cf200d32Sopenharmony_ci GPT disks rather than the MBR disks that cfdisk modifies. As such, cgdisk 19cf200d32Sopenharmony_ci is a curses-based text-mode tool for manipulating partitions, which is to 20cf200d32Sopenharmony_ci say that it uses an interface that relies on arrow keys and a dynamic 21cf200d32Sopenharmony_ci display rather than the command letters and a scrolling display like 22cf200d32Sopenharmony_ci gdisk uses. 23cf200d32Sopenharmony_ci 24cf200d32Sopenharmony_ci- sgdisk -- This program is conceptually similar to the Linux sfdisk and 25cf200d32Sopenharmony_ci FreeBSD gpt programs, but its operational details differ. It enables 26cf200d32Sopenharmony_ci manipulation of GPT disks using command-line options, so it's suitable 27cf200d32Sopenharmony_ci for use in scripts or by experts to perform specific tasks that might 28cf200d32Sopenharmony_ci take several commands in gdisk to accomplish. 29cf200d32Sopenharmony_ci 30cf200d32Sopenharmony_ci- fixparts -- This program, unlike the preceding three, operates on MBR 31cf200d32Sopenharmony_ci disks. It's intended to fix certain problems that can be created by 32cf200d32Sopenharmony_ci various utilities. Specifically, it can fix mis-sized extended partitions 33cf200d32Sopenharmony_ci and primary partitions located in the middle of extended partitions. It 34cf200d32Sopenharmony_ci also enables changing primary vs. logical partition status (within limits 35cf200d32Sopenharmony_ci of what's legal in the MBR scheme) and making a few other minor changes. 36cf200d32Sopenharmony_ci It does NOT support creating new partitions; for that, you should use 37cf200d32Sopenharmony_ci fdisk, parted, or some other tool. 38cf200d32Sopenharmony_ci 39cf200d32Sopenharmony_ciMore details about the abilities of these tools follows. 40cf200d32Sopenharmony_ci 41cf200d32Sopenharmony_ciAll four programs rely on the same set of underlying code base; they differ 42cf200d32Sopenharmony_cionly in their control interfaces (defined in gdisk.cc, cgdisk.cc, 43cf200d32Sopenharmony_cisgdisk.cc, and fixparts.cc, respectively) and in which support code they 44cf200d32Sopenharmony_ciuse. 45cf200d32Sopenharmony_ci 46cf200d32Sopenharmony_ciGPT fdisk (gdisk, cgdisk, and sgdisk) Details 47cf200d32Sopenharmony_ci--------------------------------------------- 48cf200d32Sopenharmony_ci 49cf200d32Sopenharmony_ciThe gdisk program is intended as a (somewhat) fdisk-workalike program for 50cf200d32Sopenharmony_ciGPT-partitioned disks, cgdisk is similarly a workalike for fdisk, and 51cf200d32Sopenharmony_cisgdisk provides most of gdisk's functionality in a more script-friendly 52cf200d32Sopenharmony_ciprogram. Although libparted and programs that use it (GNU Parted, gparted, 53cf200d32Sopenharmony_cietc.) provide the ability to handle GPT disks, they have certain 54cf200d32Sopenharmony_cilimitations that gdisk overcomes. Specific advantages of gdisk, cgdisk, and 55cf200d32Sopenharmony_cisgdisk include: 56cf200d32Sopenharmony_ci 57cf200d32Sopenharmony_ci* The ability to convert MBR-partitioned disks in-place to GPT format, 58cf200d32Sopenharmony_ci without losing data 59cf200d32Sopenharmony_ci 60cf200d32Sopenharmony_ci* The ability to convert BSD disklabels in-place to create GPT 61cf200d32Sopenharmony_ci partitions, without losing data 62cf200d32Sopenharmony_ci 63cf200d32Sopenharmony_ci* The ability to convert from GPT format to MBR format without data loss 64cf200d32Sopenharmony_ci (gdisk and sgdisk only) 65cf200d32Sopenharmony_ci 66cf200d32Sopenharmony_ci* More flexible specification of filesystem type code GUIDs, which 67cf200d32Sopenharmony_ci GNU Parted tends to corrupt 68cf200d32Sopenharmony_ci 69cf200d32Sopenharmony_ci* Clear identification of the number of unallocated sectors on a 70cf200d32Sopenharmony_ci disk 71cf200d32Sopenharmony_ci 72cf200d32Sopenharmony_ci* A user interface that's familiar to long-time users of Linux 73cf200d32Sopenharmony_ci fdisk and cfdisk (gdisk and cgdisk only) 74cf200d32Sopenharmony_ci 75cf200d32Sopenharmony_ci* The MBR boot loader code is left alone 76cf200d32Sopenharmony_ci 77cf200d32Sopenharmony_ci* The ability to create a hybrid MBR, which permits GPT-unaware OSes to 78cf200d32Sopenharmony_ci access up to three GPT partitions on the disk (gdisk and sgdisk only) 79cf200d32Sopenharmony_ci 80cf200d32Sopenharmony_ciOf course, GPT fdisk isn't without its limitations. Most notably, it lacks 81cf200d32Sopenharmony_cithe filesystem awareness and filesystem-related features of GParted. You 82cf200d32Sopenharmony_cican't resize a partition's filesystem or create a partition with a 83cf200d32Sopenharmony_cifilesystem already in place with gdisk, for instance. There's no GUI 84cf200d32Sopenharmony_civersion of gdisk. 85cf200d32Sopenharmony_ci 86cf200d32Sopenharmony_ciThe GPT fdisk package provides three program files: the interactive 87cf200d32Sopenharmony_citext-mode gdisk, the curses-based interactive cgdisk, and the 88cf200d32Sopenharmony_cicommand-line-driven sgdisk. The first two are intended for use in manually 89cf200d32Sopenharmony_cipartitioning disks or changing partitioning details; sgdisk is intended for 90cf200d32Sopenharmony_ciuse in scripts to help automate tasks such as disk cloning or preparing 91cf200d32Sopenharmony_cimultiple disks for Linux installation. 92cf200d32Sopenharmony_ci 93cf200d32Sopenharmony_ciFixParts Details 94cf200d32Sopenharmony_ci---------------- 95cf200d32Sopenharmony_ci 96cf200d32Sopenharmony_ciThis program's creation was motivated by cries for help I've seen in online 97cf200d32Sopenharmony_ciforums from users who have found their partition tables to be corrupted by 98cf200d32Sopenharmony_civarious buggy partitioning tools. Although most OSes can handle the 99cf200d32Sopenharmony_ciafflicted disks fine, libparted-based tools (GParted, parted, most Linux 100cf200d32Sopenharmony_ciinstallers, etc.) tend to flake out when presented with these disks. 101cf200d32Sopenharmony_ciTypically, the symptom is a disk that appears to hold no partitions; 102cf200d32Sopenharmony_cihowever, sometimes the libparted tool presents partitions other than those 103cf200d32Sopenharmony_cithat the OS sees. 104cf200d32Sopenharmony_ci 105cf200d32Sopenharmony_ciI've observed four causes of these symptoms, three of which FixParts can 106cf200d32Sopenharmony_cicorrect: 107cf200d32Sopenharmony_ci 108cf200d32Sopenharmony_ci* Old GPT data -- If a disk is used as a GPT disk and then re-used as an 109cf200d32Sopenharmony_ci MBR disk, the GPT data may be incompletely erased. This happens if the 110cf200d32Sopenharmony_ci disk is repartitioned with fdisk or the Microsoft Windows installer, for 111cf200d32Sopenharmony_ci instance. (Tools based on libparted correctly remove the old GPT data 112cf200d32Sopenharmony_ci when converting from GPT to MBR format.) FixParts checks for this problem 113cf200d32Sopenharmony_ci when it starts and offers to correct it. If you opt to erase the GPT 114cf200d32Sopenharmony_ci data, this erasure occurs immediately, unlike other changes the program 115cf200d32Sopenharmony_ci makes. 116cf200d32Sopenharmony_ci 117cf200d32Sopenharmony_ci* Mis-sized extended partitions -- Some tools create an extended partition 118cf200d32Sopenharmony_ci that's too large, typically ending after the last sector of the disk. 119cf200d32Sopenharmony_ci FixParts automatically corrects this problem (if you use the 'w' option 120cf200d32Sopenharmony_ci to save the partition table). 121cf200d32Sopenharmony_ci 122cf200d32Sopenharmony_ci* Primary partitions inside an extended partition -- Some utilities create 123cf200d32Sopenharmony_ci or move primary partitions to within the range covered by the extended 124cf200d32Sopenharmony_ci partition. FixParts can usually correct this problem by turning the 125cf200d32Sopenharmony_ci primary partition into a logical partition or by changing one or more 126cf200d32Sopenharmony_ci other logical partitions into primaries. Such corrections aren't always 127cf200d32Sopenharmony_ci possible, though, at least not without deleting or resizing other 128cf200d32Sopenharmony_ci partitions. 129cf200d32Sopenharmony_ci 130cf200d32Sopenharmony_ci* Leftover RAID data -- If a disk is used in a RAID array and then re-used 131cf200d32Sopenharmony_ci as a non-RAID disk, some utilities can become confused and fail to see 132cf200d32Sopenharmony_ci the disk. FixParts can NOT correct this problem. You must destroy the old 133cf200d32Sopenharmony_ci RAID data, or possibly remove the dmraid package from the system, to fix 134cf200d32Sopenharmony_ci this problem. 135cf200d32Sopenharmony_ci 136cf200d32Sopenharmony_ciWhen run, FixParts presents an fdisk-like interface, enabling you to adjust 137cf200d32Sopenharmony_cipartition types (primary, logical, or omitted), change type codes, change 138cf200d32Sopenharmony_cithe bootable flag, and so on. Although you can delete a partition (by 139cf200d32Sopenharmony_ciomitting it), you can't create new partitions with the program. If you're 140cf200d32Sopenharmony_ciused to partitioning disks, particularly with Linux fdisk, two unusual 141cf200d32Sopenharmony_cifeatures of FixParts require elaboration: 142cf200d32Sopenharmony_ci 143cf200d32Sopenharmony_ci* No extended partitions -- Internally, FixParts reads the partition table 144cf200d32Sopenharmony_ci and discards data on any extended partition(s) it finds. When you save 145cf200d32Sopenharmony_ci the partition table, the program generates a new extended partition. This 146cf200d32Sopenharmony_ci design means that the program automatically corrects many problems 147cf200d32Sopenharmony_ci related to the extended partition. It also means that you'll see no 148cf200d32Sopenharmony_ci evidence of extended partitions in the FixParts user interface, although 149cf200d32Sopenharmony_ci it keeps track of the requirements and prevents you from creating illegal 150cf200d32Sopenharmony_ci layouts, such as a primary between two logicals. 151cf200d32Sopenharmony_ci 152cf200d32Sopenharmony_ci* Partition numbering -- In most Linux tools, partitions 1-4 are primaries 153cf200d32Sopenharmony_ci and partitions 5 and up are logicals. Although a legal partition table 154cf200d32Sopenharmony_ci loaded into FixParts will initially conform to this convention, some 155cf200d32Sopenharmony_ci types of damaged table might not, and various changes you make can also 156cf200d32Sopenharmony_ci cause deviations. When FixParts writes the partition table, its numbering 157cf200d32Sopenharmony_ci will be altered to conform to the standard MBR conventions, but you 158cf200d32Sopenharmony_ci should use the explicit labeling of partitions as primary or logical 159cf200d32Sopenharmony_ci rather than the partition numbers to determine a partition's status. 160cf200d32Sopenharmony_ci 161cf200d32Sopenharmony_ciInstalling 162cf200d32Sopenharmony_ci---------- 163cf200d32Sopenharmony_ci 164cf200d32Sopenharmony_ciTo compile GPT fdisk, you must have appropriate development tools installed, 165cf200d32Sopenharmony_cimost notably the GNU Compiler Collection (GCC) and its g++ compiler for C++. 166cf200d32Sopenharmony_ciI've also tested compilation with Clang, which seems to work; however, I've 167cf200d32Sopenharmony_cinot done extensive testing of the resulting binaries, beyond checking a few 168cf200d32Sopenharmony_cibasics. See the README.Windows files for additional notes on compiling the 169cf200d32Sopenharmony_cisoftware for Windows. In addition, note these requirements: 170cf200d32Sopenharmony_ci 171cf200d32Sopenharmony_ci* On Linux, FreeBSD, macOS, and Solaris, libuuid must be installed. This is 172cf200d32Sopenharmony_ci the standard for Linux and macOS, although you may need to install a 173cf200d32Sopenharmony_ci package called uuid-dev or something similar to get the headers. On 174cf200d32Sopenharmony_ci FreeBSD, the e2fsprogs-libuuid port must be installed. 175cf200d32Sopenharmony_ci 176cf200d32Sopenharmony_ci* The ICU library (http://site.icu-project.org), which provides support for 177cf200d32Sopenharmony_ci Unicode partition names, is optional on all platforms except Windows, on 178cf200d32Sopenharmony_ci which it's not supported. Using this library was required to get proper 179cf200d32Sopenharmony_ci UTF-16 partition name support in GPT fdisk versions prior to 0.8.9, but 180cf200d32Sopenharmony_ci as of that version it should no longer be required. Nonetheless, you can 181cf200d32Sopenharmony_ci use it if you're having problems with the new UTF-16 support. This 182cf200d32Sopenharmony_ci library is normally installed in Linux and macOS, but you may need to 183cf200d32Sopenharmony_ci install the development headers (libicu-dev or something similar in 184cf200d32Sopenharmony_ci Linux; or the libicu36-dev Fink package in macOS). To compile with ICU 185cf200d32Sopenharmony_ci support, you must modify the Makefile: Look for commented-out lines that 186cf200d32Sopenharmony_ci refer to USE_UTF16, -licuuc, -licudata, or -licucore. Uncomment them and 187cf200d32Sopenharmony_ci comment out the equivalents that lack these lines. 188cf200d32Sopenharmony_ci 189cf200d32Sopenharmony_ci* The cgdisk program requires the ncurses library and its development files 190cf200d32Sopenharmony_ci (headers). Most Linux distributions install ncurses by default, but you 191cf200d32Sopenharmony_ci may need to install a package called libncurses5-dev, ncurses-devel, or 192cf200d32Sopenharmony_ci something similar to obtain the header files. On my macOS development 193cf200d32Sopenharmony_ci system, I installed the nurses Homebrew ("brew") package; however, other 194cf200d32Sopenharmony_ci Unix-style software repositories are available and may work for you (see 195cf200d32Sopenharmony_ci the next item). If you're having problems installing ncurses, you can 196cf200d32Sopenharmony_ci compile gdisk and/or sgdisk without cgdisk by specifying only the targets 197cf200d32Sopenharmony_ci you want to compile to make. 198cf200d32Sopenharmony_ci 199cf200d32Sopenharmony_ci* The sgdisk program requires the popt library and its development files 200cf200d32Sopenharmony_ci (headers). Most Linux distributions install popt by default, but you may 201cf200d32Sopenharmony_ci need to install a package called popt-dev, popt-devel, or something 202cf200d32Sopenharmony_ci similar to obtain the header files. MacOS users can find a version of 203cf200d32Sopenharmony_ci popt for Mac OS from Darwin Ports (http://popt.darwinports.com), MacPorts 204cf200d32Sopenharmony_ci (https://trac.macports.org/browser/trunk/dports/devel/popt/Portfile), Fink 205cf200d32Sopenharmony_ci (http://www.finkproject.org), or brew (http://macappstore.org/popt/); 206cf200d32Sopenharmony_ci however, you'll first need to install the relevant environment 207cf200d32Sopenharmony_ci (instructions exist on the relevant projects' pages). When I re-built my 208cf200d32Sopenharmony_ci Mac build environment in February of 2020, I found that brew was, by far, 209cf200d32Sopenharmony_ci the easiest of these to install. Some of the others seem to have been 210cf200d32Sopenharmony_ci abandoned, but I didn't investigate thoroughly. I'm leaving the references 211cf200d32Sopenharmony_ci in case they might be useful in the future. Instead of installing one of 212cf200d32Sopenharmony_ci These ports, you can compile gdisk and/or cgdisk alone, without sgdisk; 213cf200d32Sopenharmony_ci gdisk and cgdisk don't require popt. 214cf200d32Sopenharmony_ci 215cf200d32Sopenharmony_ciWhen all the necessary development tools and libraries are installed, you 216cf200d32Sopenharmony_cican uncompress the package and type "make" at the command prompt in the 217cf200d32Sopenharmony_ciresulting directory. (Beginning with version 1.0.9, GPT fdisk provides a 218cf200d32Sopenharmony_ciconsolidated Makefile for all supported OSes. Earlier versions used 219cf200d32Sopenharmony_ciOS-specific Makefiles, such as Makefile.mac and Makefile.freebsd, which are 220cf200d32Sopenharmony_cistill provided, but are deprecated.) You must use GNU make (gmake on 221cf200d32Sopenharmony_ciFreeBSD) with this Makefile. You may also need to add header (include) 222cf200d32Sopenharmony_cidirectories or library directories by setting the CXXFLAGS environment 223cf200d32Sopenharmony_civariable or by editing the Makefile. The result should be program files 224cf200d32Sopenharmony_cicalled gdisk, cgdisk, sgdisk, and fixparts (or variants with "32.exe" or 225cf200d32Sopenharmony_ci"64.exe" added for Windows binaries). Typing "make gdisk", "make cgdisk", 226cf200d32Sopenharmony_ci"make sgdisk", or "make fixparts" will compile only the requested programs. 227cf200d32Sopenharmony_ciYou can use these programs in place or copy the files to a suitable 228cf200d32Sopenharmony_cidirectory, such as /usr/local/sbin. You can copy the man pages (gdisk.8, 229cf200d32Sopenharmony_cicgdisk.8, sgdisk.8, and fixparts.8) to /usr/local/man/man8 to make them 230cf200d32Sopenharmony_ciavailable. 231cf200d32Sopenharmony_ci 232cf200d32Sopenharmony_ciCross-compiling is possible, but is not well-tested, except for compiling 233cf200d32Sopenharmony_ciWindows binaries on Linux. (See README.Windows for details.) To 234cf200d32Sopenharmony_cicross-compile, specify the TARGET environment variable when launching make, 235cf200d32Sopenharmony_cias in "TARGET=win64 make" to compile for 64-bit (x86-64, X64, AMD64) Windows 236cf200d32Sopenharmony_cion a non-Windows platform. Supported TARGET values are linux, freebsd, 237cf200d32Sopenharmony_cisolaris, macos, win32, and win64. 238cf200d32Sopenharmony_ci 239cf200d32Sopenharmony_ciCaveats 240cf200d32Sopenharmony_ci------- 241cf200d32Sopenharmony_ci 242cf200d32Sopenharmony_ciDISK PARTITIONING SOFTWARE IS DANGEROUS! Although the GPT fdisk project has 243cf200d32Sopenharmony_ciexisted since 2009, I do not claim it is entirely bug-free; in fact a glance 244cf200d32Sopenharmony_ciat the revision history shows recent bug fixes. I believe all 245cf200d32Sopenharmony_cidata-corruption bugs to be squashed, but I know full well that the odds of 246cf200d32Sopenharmony_cimy missing something are high. This is particularly true for large 247cf200d32Sopenharmony_ci(over-2TiB) drives and use in exotic environments. 248cf200d32Sopenharmony_ci 249cf200d32Sopenharmony_ciMy main development platform is a system running the 64-bit version of 250cf200d32Sopenharmony_ciUbuntu Linux. I've also tested on several other 32- and 64-bit Linux 251cf200d32Sopenharmony_cidistributions, Intel-based macOS 10 and 11, 64-bit FreeBSD 7.1, and Windows 252cf200d32Sopenharmony_ci7 and 10. Other environments qualify as "exotic," and even macOS and Windows 253cf200d32Sopenharmony_ciare borderline exotic in this context, since I use Linux almost exclusively, 254cf200d32Sopenharmony_ciand my impression is that GPT fdisk is far more commonly used on Linux than 255cf200d32Sopenharmony_ciin other OSes. 256cf200d32Sopenharmony_ci 257cf200d32Sopenharmony_ciRedistribution 258cf200d32Sopenharmony_ci-------------- 259cf200d32Sopenharmony_ci 260cf200d32Sopenharmony_ciThis program is licensed under terms of the GNU GPL (see the file COPYING). 261cf200d32Sopenharmony_ci 262cf200d32Sopenharmony_ciAcknowledgements 263cf200d32Sopenharmony_ci---------------- 264cf200d32Sopenharmony_ci 265cf200d32Sopenharmony_ciThis code is mostly my own; however, I've used three functions from two 266cf200d32Sopenharmony_ciother GPLed programs: 267cf200d32Sopenharmony_ci 268cf200d32Sopenharmony_ci- The code used to generate CRCs is taken from the efone program by 269cf200d32Sopenharmony_ci Krzysztof Dabrowski and ElysiuM deeZine. (See the crc32.h and 270cf200d32Sopenharmony_ci crc32.cc source code files.) 271cf200d32Sopenharmony_ci 272cf200d32Sopenharmony_ci- A function to find the disk size is taken from Linux fdisk by A. V. Le 273cf200d32Sopenharmony_ci Blanc. This code has subsequently been heavily modified. 274cf200d32Sopenharmony_ci 275cf200d32Sopenharmony_ciAdditional code contributors include: 276cf200d32Sopenharmony_ci 277cf200d32Sopenharmony_ci- Yves Blusseau (1otnwmz02@sneakemail.com) 278cf200d32Sopenharmony_ci 279cf200d32Sopenharmony_ci- David Hubbard (david.c.hubbard@gmail.com) 280cf200d32Sopenharmony_ci 281cf200d32Sopenharmony_ci- Justin Maggard (justin.maggard@netgear.com) 282cf200d32Sopenharmony_ci 283cf200d32Sopenharmony_ci- Dwight Schauer (das@teegra.net) 284cf200d32Sopenharmony_ci 285cf200d32Sopenharmony_ci- Florian Zumbiehl (florz@florz.de) 286cf200d32Sopenharmony_ci 287cf200d32Sopenharmony_ci- Guillaume Delacour (contributed the gdisk_test.sh script) 288