Manage Linux Disk Partition with gdisk Command (2024)

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.

Manage Linux Disk Partition with gdisk Command (1)

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.

Manage Linux Disk Partition with gdisk Command (2)

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.

Manage Linux Disk Partition with gdisk Command (3)

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.

Manage Linux Disk Partition with gdisk Command (4)

Let us create two more 1 Gb partitions.

Manage Linux Disk Partition with gdisk Command (5)

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.

Manage Linux Disk Partition with gdisk Command (6)

Changing / modifying partition type

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

Manage Linux Disk Partition with gdisk Command (7)

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.

Manage Linux Disk Partition with gdisk Command (8)

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.

Manage Linux Disk Partition with gdisk Command (9)

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

Manage Linux Disk Partition with gdisk Command (10)

Getting help

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

Manage Linux Disk Partition with gdisk Command (11)

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.

Manage Linux Disk Partition with gdisk Command (12)

Verifying partition

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

Manage Linux Disk Partition with gdisk Command (13)

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.

Linux File System Types Explained
The mkfs command on Linux
The mount command on Linux Temporary Mounting
The /etc/fstab file on Linux Explained

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.

By ComputerNetworkingNotes Updated on 2024-05-06 02:00:02 IST

ComputerNetworkingNotes Linux TutorialsManage Linux Disk Partition with gdisk Command

Manage Linux Disk Partition with gdisk Command (2024)

FAQs

How to manage disk partition in Linux? ›

Process to Partition a Disk in Linux
  1. #1 Identify the Disk. Identify the disk you want to partition. ...
  2. #2 Start the Partitioning Tool. This command opens the fdisk utility for the specified disk ( /dev/sda in this example). ...
  3. #3 Create a New Partition. ...
  4. #5 Save Changes. ...
  5. #6 Format the New Partition.
Oct 29, 2023

How to resize partition using gdisk? ›

If the data partition is a GPT partition, do the following:
  1. Access the device using gdisk . ...
  2. Print all the information about the old partition using the i partition-number command and note the "Partition unique GUID". ...
  3. Delete the old partition. ...
  4. Re-create the new partition with same start but different size when prompted.

What is the difference between fdisk and gdisk in Linux? ›

GDisk is a DOS command-line utility, GDisk.exe, that includes all the features of FDisk and additional features. Everything you can do with FDisk you can do with GDisk. Partitions that are created with GDisk are indistinguishable from those that are created with FDisk.

How to format a disk with Gdisk? ›

Linux Format Drive Using the Command Line (gdisk Utility)
  1. Step 1: Select a Drive to Format. First, choose a disk that you want to format. ...
  2. Step 2: Create a New Linux Partition for Disk. Now, we will create the partition on the device path “/dev/sda.” ...
  3. Step 3: Format Disk with Ext4. ...
  4. Step 4: Mount Partition.

What is the difference between Gdisk and Sgdisk? ›

gdisk is text-mode interactive, sgdisk is command-line, and cgdisk has a curses-based interface.

What is the disk management command in Linux? ›

The fdisk command is a powerful tool used to create and manage disk partitions in Linux. You can list all partitions on your system with a simple command: sudo fdisk -l . In this example, we use the sudo fdisk -l command to list all the disk partitions on the system.

How do I manage disk space in Linux? ›

The du command helps to find space-consuming files and directories, their sizes, and which folders occupy the most disk space. The du command lets you manage disk space, optimize storage utilization, and finds spaces in the Linux system.

How do I manage storage partitions? ›

To format an existing partition (volume)

The select Control Panel > System and Security > Administrative Tools, and then double-click Computer Management. In the left pane, under Storage, select Disk Management. Right-click the volume that you want to format, and then select Format.

How do I reduce disk partition in Linux? ›

Extending Or Shrinking a Linux Partition
  1. Step 1: Identify the Partition. ...
  2. Step 2: Back Up Your Data. ...
  3. Step 3: Start fdisk. ...
  4. Step 4: Delete the Partition. ...
  5. Step 5: Create a New Partition. ...
  6. Step 6: Save the Changes. ...
  7. Step 7: Resize the File System.
Mar 11, 2024

How to extend a Linux disk partition? ›

If you want to extend a partition that is mounted to an instance, run the umount command to unmount the partition, extend the partition, wait until the partition works as expected, and then run the mount command to remount the partition. Perform operations based on the kernel version of the Linux instance.

How to increase partition size using disk management? ›

In Computer Management, select [Disk Management]③. In Disk Management, right-click the existing volume you want to extend④, then select [Extend Volume]⑤. If there is no unallocated space, but you want to extend the existing volume, you are able to delete a separate volume, and then it will become unallocated space.

What is the Gdisk command in Linux? ›

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.

How to use fdisk to manage partitions on Linux? ›

How to Use Fdisk Format Partition in Linux?
  1. Use the "sudo fdisk /dev/sda" to view all fdisk commands. Type "m" to view all actions performed on /dev/sda.
  2. Use the command: "sudo fdisk /dev/sda" to access the partition. And then, you can type "n" to create a new partition. ...
  3. Format the partition with the mkfs command.
Jan 11, 2024

Should I use fdisk or parted? ›

parted comes with better functionality than fdisk, such as resizing (shrinking and extending partitions). In addition, it supports multiple partition table formats, including MS-DOS and GPT.

How to use disk as RAM in Linux? ›

To use a Linux ramdisk, go through the following steps:
  1. Create a directory to serve as the mount point for the ramdisk, e.g., mkdir /media/ramdisk.
  2. Mount the ramdisk with mount -t tmpfs -o size=SIZE tmpfs /media/ramdisk. ...
  3. The ramdisk is now mounted and ready for usage.
Jul 30, 2023

How to use disk usage in Linux? ›

How to Check Disk Space in Linux
  1. Method 1: Check Disk Space via df Command. Display Usage in Megabytes and Gigabytes. ...
  2. Method 2: Check Disk Space via the du Command.
  3. Method 3: Check Disk Space via the pydf Command.
  4. Method 4: Check Disk Space via the fdisk -l Command.
  5. Method 5: Check Disk Space via the lsblk Command.
May 8, 2024

How to benchmark disk on Linux? ›

Linux Disk Speed Test via the Graphical Method
  1. Open the Disk Utility. Quickly launch the Disk Utility by typing "Disk" into the program menu or by looking for it in the menu under System → Administration → Disk Utility.
  2. Choose the disk for the test. ...
  3. Run the Benchmark Feature. ...
  4. Run the tests.
Jan 21, 2024

How to partition GPT disk Linux? ›

Creating a Disk Partition in Linux
  1. List the partitions using the parted -l command to identify the storage device you want to partition. ...
  2. Open the storage device. ...
  3. Set the partition table type to gpt , then enter Yes to accept it. ...
  4. Review the partition table of the storage device.
Jan 28, 2021

Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 5599

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.