util-linux fdisk is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk. Hard disks are divided into partitions and this division is described in the partition table.
GPT fdisk, as implemented in the gdisk program and its associated utilities, works "on Globally Unique Identifier (GUID) Partition Table (GPT) disks, rather than on the more common (through at least early 2013) Master Boot Record (MBR) partition tables."
This article coversand its related utility, as well as the analogous and utilities.
- 1 Installation
- 2 List partitions
- 3 Backup and restore partition table
- 4 Create a partition table and partitions
- 5 Tips and tricks
- 6 See also
To use fdisk and its associated utilities, thepackage, which is part of the group is required.
To use gdisk and its associated utilities, install the package.
To list partition tables and partitions on a device, you can run the following, where device is a name like
# fdisk -l /dev/sda
Or for the gdisk:
# gdisk -l /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.
The MBR is stored in the the first 512 bytes of the disk. It consist of 3 parts:
- The first 446 bytes contain the boot loader.
- The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
- The last 2 bytes contain an identifier
To save the MBR as
# dd if=/dev/sdX of=/path/to/mbr_file.img bs=512 count=1
You can also extract the MBR from a full dd disk image:
# dd if=/path/to/disk.img of=/path/to/mbr_file.img bs=512 count=1
To restore (be careful, this destroys the existing partition table and with it access to all data on the disk):
# dd if=/path/to/mbr_file.img of=/dev/sdX bs=512 count=1
If you only want to restore the boot loader, but not the primary partition table entries, just restore the first 446 bytes of the MBR:
# dd if=/path/to/mbr_file.img of=/dev/sdX bs=446 count=1
To restore only the partition table, one must use:
# dd if=/path/to/mbr_file.img of=/dev/sdX bs=1 skip=446 count=64
To erase the MBR (may be useful if you have to do a full reinstall of another operating system) only the first 446 bytes are zeroed because the rest of the data contains the partition table:
# dd if=/dev/zero of=/dev/sdX bs=446 count=1
For both GPT and MBR you can use sfdisk to save the partition layout of your device to a file with the
--dump option. Run the following command for device
# sfdisk -d /dev/sda > sda.dump
The file should look something like this for a single ext4 partition that is 1GB in size:
label: gpt label-id: AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE device: /dev/sda unit: sectors first-lba: 34 last-lba: 1048576 /dev/sda1 : start=2048, size=1048576, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=BBF1CD36-9262-463E-A4FB-81E32C12BDE7
To later restore this layout you can run:
# sfdisk /dev/sda < sda.dump
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 -b=sgdisk-sda.bak /dev/sda
You can later restore the backup by running:
# sgdisk -l=sgdisk-sda.bak /dev/sda
If you want to clone your current device's partition layout (
/dev/sda in this case) to another drive (
# sgdisk -R=/dev/sdc /dev/sda
If both drives will be in the same computer, you need to randomize the GUID's:
# 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. See the partition table article to help decide whether to use MBR or GPT.
Before beginning, you may wish to backup your current partition table and scheme.
The following shows how to use both gdisk and fdisk to perform both the creation of a partition table and the creation of the actual partitions. Differences are noted when necessary.
Start the partition manipulator
Start either fdisk or gdisk as instructed in the following sections. Then continue with #Create new table.
Using MBR, the utility for editing the partition table is called fdisk. Recent versions of fdisk have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. fdisk automatically aligns all partitions to 2048 sectors, or 1024 KiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.
Start fdisk against your drive as root. In this example we are using
# fdisk /dev/sda
This opens the fdisk dialogue where you can type in commands.
Using GPT, 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 for its usage.
gdisk performs partition alignment automatically on a 2048 sector (or 1024KiB) block size base which should be compatible with the vast majority of SSDs if not all. GNU Parted also supports GPT, but is less user-friendly for aligning partitions.
To use gdisk, run the program with the name of the device you want to change/edit. This example uses
# gdisk /dev/sda
Create new table
To create a new MBR 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 a new partition with the
n command. You enter a partition type (fdisk only), partition number, starting sector, and an ending sector.
For fdisk, when prompted, specify the partition type, type
p to create a primary partition or
e to create an extended one. There may be up to four primary partitions.
Both start and end sectors can be specified in absolute terms as sector numbers or as positions measured in kibibytes (
K), mebibytes (
M), gibibytes (
G), tebibytes (
T), or pebibytes (
P); for instance,
40M specifies a position 40MiB from the start of the disk. You can specify locations relative to the start or end of the specified default range by preceding the number by a
- symbol, as in
+2G to specify a point 2GiB after the default start sector, or
-200M to specify a point 200MiB 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 start sector and the end of the same block for the end sector.
Select the partition's type id. The default,
Linux filesystem, should be fine for most use. Press
l to show the codes list. You can make the partition bootable by typing
Repeat this procedure until you have the partitions you desire.
Write changes to disk
Write the table to disk and exit via the
Tips and tricks
Convert between MBR and GPT
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.
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
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.
# sfdisk -r /dev/sda
# 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.
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
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