A fourth tool, FixParts, is part of the GPTfdisk source package but is different enough that I distribute its binariesseparately. (Many Linux distributions package all four programs together ina package called gdisk, though.) This page documents use of thegdisk tool. Separate pages provide similar documentation forcgdisk and sgdisk, Acgdisk Walkthrough and Ansgdisk Walkthrough.
The GPT fdisk package includes a man page thatdocuments the gdisk program in the usual way. If you want to readup on all its options, please refer to that document. This page takes adifferent approach: It walks you through some common operations, explainingeach one.
You must launch gdisk with administrative privileges on allplatforms except when editing disk image files or, on some systems,removable disks. Under Linux, FreeBSD, or macOS, this is done by logging inas the root user, using su to acquire rootprivileges, or using the sudo utility. Under Windows, you canright-click the Command Prompt program and select the "Run asAdministrator" option, then use the resulting window to rungdisk.
You launch gdisk in much the same way as fdisk,although gdisk supports very few command-line arguments. (You maypass it the -l option to see the partition table and then quit,but that's it.) You must know the name of the device file that's used toaccess the disk. This varies from one OS to another:
When gdisk starts, it performs a scan for four types ofexisting partition tables and displays the results. MBR is the commonMaster Boot Record partitioning system; BSD is the Berkeley StandardDistribution (BSD) disklabel system used on some computers that run a BSD(FreeBSD, OpenBSD, etc.); APM is the Apple Partition Map used on 680x0- andPowerPC-based Macintoshes; and GPT is of course the GUID Partition Table.The BSD and APM scans report either present or notpresent, but GPT can report three states (present, notpresent, or damaged), and there are four MBR states (MBRonly, protective, hybrid, or not present).The normal state for an MBR-only disk is as shown above, and the normalstate for a GPT disk is MBR: protective and GPT: present.Hybrid MBRs (described on my Hybrid MBRs page)change the MBR state to hybrid. Other combinations are possible,some of which indicate that a disk has been re-partitioned for a newpartitioning system without completely erasing the old partition table.
When starting gdisk on a disk with existing MBR or BSD disklabelpartitions and no GPT, the program displays a message surrounded byasterisks about converting the existing partitions to GPT, as just shown.This message is intended to scare you away if you launch gdisk onthe wrong disk by accident or if you don't know what you're doing.
The Code column of the output shows the partition type code.Because GPT employs a 16-byte GUID number for storing partition type codes,displaying this raw code would be both space-consuming and difficult forpeople to parse. Thus, gdisk translates the GUID code into avariant of MBR type codes. In particular, the codes used by gdisk(when they're equivalent to MBR type codes) are the MBR codes multiplied byhexadecimal 0x0100, and displayed in hexadecimal. The 0x0700 code shownabove is therefore equivalent to an MBR code of 0x07, which is the code forHPFS/NTFS.
The Name column displays a free-form text string that you canmodify. Starting with version 0.4.0, gdisk places the nameassociated with the partition type code in this field when it creates orconverts partitions, but you can change this default, as describedshortly. Note that GPT fdisk accesses the GPT data structure'sname for a partition. Most filesystems also support a name that's part ofthe filesystem's data structure. The two can match, but they don'thave to, and GPT fdisk makes no attempt to synchronize the two names.Confusingly, some partitioning tools, including GParted under Linux andboth Disk Utility and diskutil under macOS, display the filesystem name andhide the GPT name.
With the existence of this single partition as (presumably) sufficientidentification that this is the disk you want to modify, the dcommand deletes the partition. Since only one partition exists,gdisk doesn't prompt you for a number; it just responds Using1 and then displays another command prompt.
The second partition, which will hold a Linux ext4 filesystem, receivesthe 0x8300 type code. Quite a few specialized Linux type codes have sprung up, most associated with the Discoverable Partitions Specification (DPS), which is intended to enable a Linux OS to figure out where to mount partitions even in the absence of an /etc/fstab file, which is the traditional way to handle this task. As of 2022, most distributions still create an /etc/fstab file and set the Linux filesystem type code (shown as 0x8300 in gdisk) on all Linux filesystem partitions. (LVM, swap, and some other partitions get their appropriate type codes.)
In this case, all but 2 MiB of the disk is allocated to partitions and noproblems were found. That unused space is at the start and end of the diskand is a result of the default alignment value of 2048. See the Partitioning Advice page for more on why partitionalignment is important. If gdisk found problems, such asoverlapping partitions or mismatched main and backup partition tables, itwould report them here. Of course, gdisk includes safeguards toensure that you can't create such problems yourself. The v option'ssanity checks are intended to help you spot problems that might result fromdata corruption.
Once you're done making your changes, be they relatively simplepartition creations or recovery from disk damage, you can exit fromgdisk. The q command exits without saving your changes,while w writes your changes to disk. Both commands exist on boththe main and experts' menus.
Note for FreeBSD users: A limitation of the FreeBSDversion of gdisk is that it can't ordinarily write to a disk ifany partitions from that disk are mounted. If you want to change your bootdisk, you'll need to do so from an emergency system or type sysctlkern.geom.debugflags=16 prior to launching gdisk.
You're now back at your shell prompt. Because gdisk doesn'tcreate filesystems, you'll need to do that at your shell prompt. In thecase of the 3-partition disk in this example, you can create twofilesystems in Linux:
Caution: If gdisk reports that the kernel is still usingthe old partition table, do not try to create filesystems on orotherwise use your new partitions until you've rebooted! Your newfilesystem may be written to the wrong partition (possibly one withvaluable data) or written to a location on the disk where you won't be ableto find it.
GPT fdisk (consisting of the gdisk, cgdisk,sgdisk, and fixparts programs) is a set of text-modepartitioning tools for Linux, FreeBSD, macOS, and Windows. Thegdisk, cgdisk, and sgdisk programs work onGlobally Unique Identifier (GUID) Partition Table (GPT) disks, rather thanon the older (and once more common) Master Boot Record (MBR)partition tables. The fixparts program repairs certain types ofdamage to MBR disks and enables changing partition types from primary tological and vice-versa. You can learn more about fixparts on its dedicated Web page. Ifgdisk, cgdisk, and sgdisk sound interesting toyou, then read on (or skip straight to the "Obtaining GPT fdisk" link ifyou don't need the GPT pep talk). If you don't know what a GPT is, be sureto read the first section!
gdisk, cgdisk and sgdisk all have the same functionality but provide different user interfaces. gdisk is text-mode interactive, sgdisk is command-line, and cgdisk has a curses-based interface. This article covers gdisk(8) and sgdisk(8) utilities.
Using sgdisk you can create a binary backup consisting of the protective MBR, the main GPT header, the backup GPT header, and one copy of the partition table. The example below will save the partition table of /dev/sda to a file sgdisk-sda.bin:
The following shows how to use gdisk to perform both the creation of a partition table and the creation of the actual partitions. Alternatively, you may use the curses-based version called cgdisk; however, the following instructions do not apply to it. See cgdisk(8) for its usage.
gdisk performs partition alignment automatically on a 2048 512-byte sector (1 MiB) block size base which should be compatible with all Advanced Format HDDs and the vast majority of SSDs if not all.
Select the partition's type by entering gdisk's internal type code or specifying the partition type GUID manually. The default, Linux filesystem (GUID 0FC63DAF-8483-4772-8E79-3D69D8477DE4, gdisk's internal code 8300), should be fine for most use cases.
gdisk, sgdisk and cgdisk have the ability to convert MBR and BSD disklabels to GPT without data loss. Upon conversion, all the MBR primary partitions and the logical partitions become GPT partitions with the correct partition type GUIDs and Unique partition GUIDs created for each partition.
There is no package for the EFI version of gdisk, but Rod Smith provides a prebuilt gdisk-1.04 EFI binary on SourceForge. Download gdisk-efi-*.zip and extract the archive. To use it, copy gdisk_x64.efi to the EFI system partition and launch it from your boot loader or UEFI Shell.
The overlapping data suggests that you did not have a GPT disk to begin with, but in fact had an MBR disk and were booting in BIOS mode. (One of gdisk's features is that it automatically converts from MBR to GPT when you launch it on an MBR disk.) If I'm right, you can probably restore the MBR as follows:
OTOH, if you're positive that the disk was in GPT form and that the computer was booting Windows in EFI mode, then your partition table was simply damaged to begin with. In this case, you may need to delete that final partition and re-create it a little bit smaller to make it fit; gdisk won't let you save a damaged partition table.
I installed a new hard drive in my home server today, went to format it in gdisk (larger than 2 TB), set the changes and wrote them. Then I noticed that I had selected the wrong disk, in fact I just overwrote the partition table on my startup disk instead.
35fe9a5643