Manage Linux Disk Partition with gdisk Command

The gdisk is a disk management utility. It allows us to create, manage, and delete standard GPT partitions on Linux. There are two types of partitions: advanced and standard.

Advanced partitions are the modern way to manage storage devices. They can span multiple disks. LVM and RAID are the advanced partition types. Standard partitions are the classical way to manage storage devices. They reside on a single disk. MBR and GPT are standard partition types. A system uses firmware to detect hardware and boot. There are two types of firmware: BIOS and UEFI . BIOS is the classical firmware. It supports only MBR partitions. UEFI is the modern firmware. It supports both MBR and GPT partitions.

We have three tools to create and manage standard partitions: fdisk, gdisk, and parted. The fdisk creates only MBR partitions. The gdisk creates only GPT partitions. The parted creates both. We can create partitions only on the disk having free unparted space. If free unparted space is not available on the attached disk, you can attach a new hard disk. The following tutorials explain how to create a virtual machine and add a new hard disk to the virtual machine for practice.

RHCE Practice Lab Setup in VMWare and in VirtualBox
Adding and removing hard disks in VMware Workstation

Creating and managing GPT partitions

The lsblk command lists all attached hard disks and their partitions.

the lsblk command

Select a blank hard disk and specify its absolute path as an argument to the gdisk command.

The gdisk command scans the specified hard disk for existing partitions and prints the result. If it finds a partition table, it displays a warning message indicating this action could corrupt the existing partition table. For example, if you used the same disk to practice the fdisk command, you would get the following message.

Found invalid GPT and valid MBR; converting MBR to GPT format in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE. Exit by typing 'q' if you don't want to convert your MBR partitions to GPT format.

It happens when you delete MBR partitions but keep the partition table. Deleting MBR partitions does not delete the partition table.

contain partition table

For example, I used this disk to explain the fdisk command. At the end of the tutorial, I deleted all MBR partitions but did not delete the partition table. Since I did not delete the partition table, the gdisk command found it and displayed this message.

I kept this partition table to explain this message. MBR uses the first 512 bytes of the hard disk to store the partition table. If you want to delete the MBR partition table, use the following command after deleting all MBR partitions.

#dd if=/dev/zero of=/dev/sda bs=512 count=1

To confirm this disk contains no partitions, we can use the p command.

no partitions

As we can see in the above output, this disk contains no partitions. Since it contains no partitions, we can use it to create partitions.

Creating new partitions

The n command creates a new partition. It needs four arguments: partition number, first sector, last sector, and partition type. It also uses a default value for all arguments. The following table lists the default value of each.

Partition number Next partition after the existing partition
First sector First available sector on the disk
Last sector The last sector of the disk
Partition type Linux File System

If we select the default value, it creates a single partition of all available disk space. If we want to create multiple partitions, we need to specify the size of each partition in the third step. We can provide the partition size in sectors or the standard data units. For example, to create a 1Gb partition, we will specify +1G as an argument.

The following image shows the steps to create a 1 Gb partition.

creating new partitions

Let us create two more 1 Gb partitions.

create new partitions

By default, it creates a partition for a standard Linux file system. If we want to use the partition for any other file system, we must specify that type in the fourth step. For example, if we want to use the partition for LVM, we must change the partition type to LVM in the fourth step.

Let us create a partition for LVM.

create lvm partition

Changing / modifying partition type

The gdisk command supports many partition types. The l command prints the list of all supported partition types.

listing partitions

The t command allows us to change the partition type of an existing partition. It needs two arguments: partition number and hexacode of the new partition type.

Let us change an LVM partition type into a standard Linux partition type.

change partition types

Modifying/deleting partitions

The d command deletes a partition. It needs the partition number as an argument. Let us delete the last two partitions we created in this exercise.

delete partitions

Now, let us create a single partition in place of the deleted partitions.

modify partition

Getting help

If you need help at the gdisk command prompt, use ?. The ? command prints a short description of all commands.

getting help

Saving information

The w command saves partition information. The kernel does not actively monitor partition information. The partprobe command forces the kernel to reread the partition information.

saving partition information

Verifying partition

We can use the lsblk command to view and verify the new partitions.

verifying partitions

Creating file systems and mounting partitions

The gdisk command only creates partitions. It does not create a file system on the partition. To use a partition, you must create a file system on it. After creating a file system, you need to mount the partition on the Linux file system. The following tutorials explain this process.

That's all for this tutorial. In this tutorial, we learned how to use the gdisk command to create, manage, and delete partitions on Linux.

ComputerNetworkingNotes Linux Tutorials Manage Linux Disk Partition with gdisk Command