Difference between revisions of "GPT fdisk"

From ArchWiki
Jump to navigation Jump to search
(Create partitions: split into subsections)
(Partition number: describe partition number)
Line 83: Line 83:
==== Partition number ====
==== Partition number ====
Partition number describes the order of partitions in the partition table. It is advised to choose the default number suggested by ''gdisk''.
==== First and last sector ====
==== First and last sector ====

Revision as of 11:54, 15 July 2018

Category:File systems Category:System recovery

GPT fdisk (consisting of the gdisk, cgdisk, sgdisk, and fixparts programs) is a set of text-mode partitioning tools. They work on Globally Unique Identifier (GUID) Partition Table (GPT) disks, rather than on the older (and once more common) Master Boot Record (MBR) partition tables.

This article covers gdisk(8) and sgdisk(8) utilities.

  • For basic partitioning functionality with a text user interface, cgdisk(8) can be used.
  • GPT fdisk website has detailed walkthroughs for gdisk, cgdisk and sgdisk.


Install the gptfdisk package.

List partitions

To list partition tables and partitions on a device, you can run the following, where device is a name like /dev/sda:

# gdisk -l /dev/sda

or alternatively the same action using sgdisk:

# sgdisk -pP /dev/sda

To list the Master Boot Record partition table and partitions run:

# sgdisk -OP /dev/sda

Backup and restore partition table

Before making changes to a hard disk, you may want to backup the partition table and partition scheme of the drive. You can also use a backup to copy the same partition layout to numerous drives.

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:

# sgdisk -b=sgdisk-sda.bin /dev/sda

You can later restore the backup by running:

# sgdisk -l=sgdisk-sda.bin /dev/sda

If you want to clone your current device's partition layout (/dev/sda in this case) to another drive (/dev/sdc) run:

# sgdisk -R=/dev/sdc /dev/sda

If both drives will be in the same computer, you need to randomize the GUIDs:

# sgdisk -G /dev/sdc

Create a partition table and partitions

The first step to partitioning a disk is making a partition table. After that, the actual partitions are created according to the desired partition scheme.

Before beginning, you may wish to backup your current partition table and scheme.

The following shows how to use gdisk to perform both the creation of a partition table and the creation of the actual partitions.

Start the partition manipulator

The utility for editing the partition table is called gdisk. 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 sector (1 MiB) block size base which should be compatible with the vast majority of SSDs if not all.

To use gdisk, run the program with the name of the device you want to change/edit. This example uses /dev/sda:

# gdisk /dev/sda

Create new table

Warning: If you create a new partition table on a disk with data on it, it will erase all the data on the disk. Make sure this is what you want to do.

To create a new GUID Partition Table and clear all current partition data, type o at the prompt. Skip this step if the table you require has already been created.

Create partitions

Create a new partition with the n command. You enter the partition number, first sector, last sector and the partition type.

Partition number

Partition number describes the order of partitions in the partition table. It is advised to choose the default number suggested by gdisk.

First and last sector

The first and last sectors of the partition can be specified in sector numbers or as positions measured in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P);

The position can be specified in:

  • absolute terms from the star of the disk. E.g. 40M as a first sector specifies a position 40MiB from the start of the disk.
  • relative terms by preceding the size with +size or -size. E.g. +2G to specify a point 2 GiB after the default start sector, or -200M to specify a point 200 MiB before the last available sector.

Pressing the Enter key with no input specifies the default value, which is the start of the largest available block for the first sector and the end of the same block for the last sector.

Tip: When partitioning it is always a good idea to specify partition sizes using relative terms with the +<size>{M,G,...} notation. Such partitions are always aligned according to the device properties.

Partition type

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: Suggest using Discoverable Partitions. (Discuss in Talk:GPT fdisk#)

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.


Repeat this procedure until you have the partitions you desire.

Write changes to disk

Write the table to disk and exit via the w command.

Tips and tricks

Convert between MBR and GPT

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements. See Help:Style for reference.Tango-edit-clear.png

Reason: Remove opinions "one of the best". Consolidate long note. (Discuss in Talk:GPT fdisk#)

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. See Rod Smith's Converting to or from GPT for more info.

After conversion, the bootloaders will need to be reinstalled to configure them to boot from GPT.

  • GPT stores a secondary table at the end of disk. This data structure consumes 33 512-byte sectors by default. MBR does not have a similar data structure at its end, which means that the last partition on an MBR disk sometimes extends to the very end of the disk and prevents complete conversion. If this happens to you, you must abandon the conversion and resize the final partition.
  • If your boot loader is GRUB, it needs a BIOS boot partition.
  • There are known corruption issues with the backup GPT table on laptops that are Intel chipset based, and run in RAID mode. The solution is to use AHCI instead of RAID, if possible.

To convert an MBR partition table to GPT, use sgdisk.

# sgdisk -g /dev/sda

To convert GPT to MBR use the m option. Note that it is not possible to convert more than four partitions from GPT to MBR.

# sgdisk -m /dev/sda

Sort partitions

This applies for when a new partition is created in the space between two partitions or a partition is deleted. /dev/sda is used in this example.

# sgdisk -s /dev/sda

After sorting the partitions if you are not using Persistent block device naming, it might be required to adjust the /etc/fstab and/or the /etc/crypttab configuration files.

Note: The kernel must read the new partition table for the partitions (e.g. /dev/sda1) to be usable. Reboot the system or tell the kernel to reread the partition table.

Recover GPT header

In case main GPT header or backup GPT header gets damaged, you can recover one from the other with gdisk. /dev/sda is used in this example.

# gdisk /dev/sda

choose r for recovery and transformation options (experts only). From there choose either

  • b: use backup GPT header (rebuilding main)
  • d: use main GPT header (rebuilding backup)

When done write the table to disk and exit via the w command.

See also