In this guide, you will learn how to manage disk partitions in Linux using gdisk. gdisk
an interactive GUID partition table (GPT) manipulator is a text-mode menu-driven program for creation and manipulation of partition tables. It will automatically convert an old-style Master Boot Record (MBR) partition table or BSD disklabel stored without an MBR carrier partition to the newer Globally Unique Identifier (GUID) Partition Table (GPT) format, or will load a GUID partition table.
Manage Disk Partitions in Linux using gdisk
Understanding MBR and GPT
There are two main ways of storing partition information on hard disks. The first one is MBR (Master Boot Record), and the second one is GPT (GUID Partition Table).
MBR
This is a remnant from the early days of MS-DOS (more specifically, PC-DOS 2.0 from 1983) and for decades was the standard partitioning scheme on PCs. The partition table is stored on the first sector of a disk, called the Boot Sector, along with a boot loader, which on Linux systems is usually the GRUB bootloader. But MBR has a series of limitations that hinder its use on modern systems, like the inability to address disks of more than 2 TB in size, and the limit of only 4 primary partitions per disk.
GUID
A partitioning system that addresses many of the limitations of MBR. There is no practical limit on disk size, and the maximum number of partitions are limited only by the operating system itself. It is more commonly found on more modern machines that use UEFI instead of the old PC BIOS.
Managing GUID Partitions with gdisk
gdisk
utility is used when dealing with GPT partitioned disks. Its interface is modeled after fdisk
, with an interactive prompt and the same (or very similar) commands.
Syntax;
gdisk [ -l ] device
NOTE; All disk-related operations need to be done as the user root
(the system administrator), or with root privileges using sudo
.
List Partition Table For Specific Device
Using -l
option, gdisk
will list the partition table for the specified device and then exits.
sudo gdisk -l /dev/sdb
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 32383920 sectors, 15.4 GiB
Model: VBOX HARDDISK
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 9618E03F-8D0D-42B3-A70F-3655453B6929
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 32383886
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 32383886 15.4 GiB 8300 Linux filesystem
Changing GPT Name of a Partition
using c
command will change the GPT name of a partition. This name is encoded as a UTF-16 string, but proper entry and display of anything beyond basic ASCII values requires suitable locale and font support. For the most part, Linux ignores the partition name, but it may be important in some OSes. GPT fdisk sets a default name based on the partition type code. Note that the GPT partition name is different from the filesystem name, which is encoded in the filesystem’s data structures.
example;
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): c
Using 1
Enter name: thehero
We can confirm the above output by displaying basic partition summary data below.
Printing the Current Partition Table
Using p
command, it will display basic partition summary data. This includes partition numbers, starting and ending sector numbers, partition sizes, gdisk’s partition types codes, and partition names.
example;
Command (? for help): p
Disk /dev/sdb: 32383920 sectors, 15.4 GiB
Model: VBOX HARDDISK
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 9618E03F-8D0D-42B3-A70F-3655453B6929
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 32383886
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 32383886 15.4 GiB 8300 thehero
From the above output, we obtain the following features;
- Each disk has a unique Disk Identifier (GUID). This is a 128 bit hexadecimal number, assigned randomly when the partition table is created. Since there are 3.4 × 1038 possible values to this number, the chances that 2 random disks have the same GUID are pretty slim. The GUID can be used to identify which filesystems to mount at boot time (and where), eliminating the need to use the device path to do so (like
/dev/sdb
). - See the phrase
Partition table holds up to 128 entries
? That’s right, you can have up to 128 partitions on a GPT disk. Because of this, there is no need for primary and extended partitions. - The free space is listed on the last line, so there is no need for an equivalent of the
F
command from fdisk.
Verifying Disk
Use v
command to verify disk. This option checks for a variety of problems, such as incorrect CRCs and mismatched main and backup data. This option does not automatically correct most problems, though; for that, you must use options on the recovery & transformation menu. If no problems are found, this command displays a summary of unallocated disk space.
example;
Command (? for help): v
No problems found. 2014 free sectors (1007.0 KiB) available in 1
segments, the largest of which is 2014 (1007.0 KiB) in size.
Changing Partition Type Code
Use t
command to change a single partition’s type code. You enter the type code using a two-byte hexadecimal number, as described earlier. You may also enter a GUID directly, if you have one and gdisk doesn’t know it.
Command (? for help): t
Using 1
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): L
Type search string, or <Enter> to show all codes:
0700 Microsoft basic data 0c01 Microsoft reserved
2700 Windows RE 3000 ONIE boot
3001 ONIE config 3900 Plan 9
4100 PowerPC PReP boot 4200 Windows LDM data
4201 Windows LDM metadata 4202 Windows Storage Spaces
7501 IBM GPFS 7f00 ChromeOS kernel
7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem
8301 Linux reserved 8302 Linux /home
8303 Linux x86 root (/) 8304 Linux x86-64 root (/)
8305 Linux ARM64 root (/) 8306 Linux /srv
8307 Linux ARM32 root (/) 8308 Linux dm-crypt
8309 Linux LUKS 830a Linux IA-64 root (/)
830b Linux x86 root verity 830c Linux x86-64 root verity
830d Linux ARM32 root verity 830e Linux ARM64 root verity
830f Linux IA-64 root verity 8310 Linux /var
8311 Linux /var/tmp 8400 Intel Rapid Start
8500 Container Linux /usr 8501 Container Linux resizable rootfs
8502 Container Linux /OEM customization 8503 Container Linux root on RAID
8e00 Linux LVM a000 Android bootloader
a001 Android bootloader 2 a002 Android boot 1
Press the <Enter> key to see more codes, q to quit: q
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'
Creating a Partition using gdisk
Just like fdisk, n
command is used to create a partition. The main difference is that besides the partition number and the first and last sector (or size), you can also specify the partition type during the creation. GPT partitions support many more types than MBR. You can check a list of all the supported types by using the L
command.
exmple;
Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-32383886, default = 18876416) or {+-}size{KMGTP}: +3G
Last sector (25167872-32383886, default = 32383886) or {+-}size{KMGTP}: +2G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'
Deleting a Partition using gdisk
Use d
command to delete a partition. Unlike fdisk
, the first partition will not be automatically selected if it is the only one on the disk.
example;
Command (? for help): d
Partition number (1-3): 2
The second partition has been deleted, so the table will look like this;
Number Start (sector) End (sector) Size Code Name
1 2048 12584959 6.0 GiB 8300
3 18876416 32383886 6.4 GiB 8300 Linux filesystem
On GPT disks, partitions can be easily reordered, or “sorted”, to avoid gaps in the numbering sequence. To do this, simply use the s
command.
Number Start (sector) End (sector) Size Code Name
1 2048 12584959 6.0 GiB 8300
2 18876416 32383886 6.4 GiB 8300 Linux filesystem
In the above output, we notice that the third partition became the second one.
gdisk Recovery Options
GPT disks store backup copies of the GPT header and partition table, making it easy to recover disks in case this data has been damaged. gdisk
provides features to aid in those recovery tasks, accessed with the r
command.
You can rebuild a corrupt main GPT header or partition table with b
and c
, respectively, or use the main header and table to rebuild a backup with d
and e
. You can also convert a MBR to a GPT with f
, and do the opposite with g
, among other operations.
Writing Data to Disk with gdisk
Use w
command to write data. This command to save changes made to disk.
example;
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
Displaying gdisk Help
To dispaly help, use ?
command.
example;
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Conclusion
This marks the end of our guide on manage disk partitions in Linux using gdisk.
For more information and usage of gdisk
comand check their man
pages.
man gdisk
Similar Articles:
Example Usage of df Command on Linux