Partitioning a disk is a foundational task in Linux system administration, enabling you to organize storage, separate data, and configure bootable volumes. While the older Master Boot Record (MBR) partition scheme was once dominant, it has critical limitations: support for disks up to only 2 TiB, a maximum of 4 primary partitions, and lack of built-in redundancy.

The GUID Partition Table (GPT) addresses these shortcomings, supporting disks larger than 2 TiB, up to 128 primary partitions (by default), and including robust error-checking via CRC32 checksums. To work with GPT on Linux, two powerful tools stand out: gdisk (interactive) and sgdisk (command-line/scriptable).

This blog will guide you through mastering GPT partition management using gdisk and sgdisk, from basic operations like creating/deleting partitions to advanced tasks like recovery and scripting.

Dec 2, 20252025-12

Table of Contents#

  1. Understanding GPT
  2. What Are gdisk and sgdisk?
  3. Installing gdisk and sgdisk
  4. Basic gdisk Operations
  5. Advanced gdisk Features
  6. Using sgdisk for Scripting
  7. Best Practices and Safety Tips
  8. Conclusion
  9. References

Understanding GPT#

Before diving into tools, let’s clarify how GPT works. A GPT disk structure includes:

  • Primary GPT Header: Stores partition table metadata (e.g., number of partitions, disk size) and a CRC32 checksum for integrity.
  • Partition Entries: A list of up to 128 partitions (configurable), each with a unique GUID, type GUID, start/end sectors, and name.
  • Secondary GPT Header: A backup of the primary header, stored at the end of the disk, to recover from corruption.
  • Protective MBR: A legacy MBR (for backward compatibility with older tools) that marks the entire disk as a single "protective" partition, preventing accidental overwrites.

Key GPT advantages:

  • Supports disks up to 8 ZiB (vs. MBR’s 2 TiB).
  • Up to 128 partitions (extendable with larger disks).
  • Unique partition GUIDs (avoids conflicts).
  • Built-in redundancy (secondary header).

What Are gdisk and sgdisk?#

gdisk (GPT fdisk) and sgdisk (scriptable GPT fdisk) are part of the gdisk package, developed by Rod Smith. They are the go-to tools for creating, modifying, and maintaining GPT partition tables on Linux.

  • gdisk: An interactive tool with a menu-driven interface (similar to fdisk), ideal for manual partitioning.
  • sgdisk: A non-interactive, command-line tool designed for scripting and automation. It supports all gdisk features but via flags/arguments.

Installing gdisk and sgdisk#

gdisk and sgdisk are preinstalled on many Linux distributions, but if not, install them via your package manager:

Ubuntu/Debian:#

sudo apt update && sudo apt install gdisk -y

Fedora/RHEL/CentOS:#

sudo dnf install gdisk -y  # Fedora/RHEL 8+  # or  sudo yum install gdisk -y  # RHEL 7 and older  

Arch Linux:#

sudo pacman -S gdisk --noconfirm

Verify installation:

gdisk --version  # Should return "GPT fdisk (gdisk) version X.Y.Z"  sgdisk --version  # Same version as gdisk  

Basic gdisk Operations#

gdisk is best for manual, interactive partitioning. Let’s walk through core tasks using a hypothetical disk /dev/sdX (replace X with your disk identifier, e.g., abc). Always confirm the disk with lsblk first to avoid data loss!

Step 1: Launch gdisk#

Start gdisk with root privileges on the target disk (not a partition, e.g., /dev/sda, not /dev/sda1):

sudo gdisk /dev/sdX

You’ll see the gdisk main menu:

GPT fdisk (gdisk) version 1.0.9

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help):  

Step 2: Print Current Partitions (p)#

To list existing partitions, type p and press Enter:

Command (? for help): p
Disk /dev/sdX: 41943040 sectors, 20.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): A1B2C3D4-E5F6-7890-ABCD-EF1234567890
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 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         41943006   20.0 GiB    8300  Linux filesystem

This output shows:

  • Disk size, sector alignment, and free space.
  • Partition number, start/end sectors, size, type code (e.g., 8300 = Linux filesystem), and name.

Step 3: Create a New Partition (n)#

To add a new partition:

  1. Type n and press Enter.
  2. Partition number: Defaults to the next available (e.g., 1 if no partitions exist). Press Enter to accept.
  3. First sector: Defaults to the first available sector (usually 2048, aligned for performance). Press Enter.
  4. Last sector: Specify size with +<size><unit> (e.g., +10G for 10 GiB, +500M for 500 MiB) or press Enter to use all free space.
  5. Partition type: Defaults to Linux filesystem (8300). Press Enter to accept, or type L to list all types later.

Example:

Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 2048
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8300

Step 4: Delete a Partition (d)#

To delete a partition:

  1. Type d and press Enter.
  2. Enter the partition number to delete (e.g., 1).

Example:

Command (? for help): d
Partition number (1-128): 1

Step 5: Change Partition Type (t)#

Partition types are defined by GUIDs or 4-character codes (e.g., 8300 for Linux, EF00 for EFI System). To change a partition’s type:

  1. Type t and press Enter.
  2. Enter the partition number.
  3. Enter the type code (e.g., EF00 for EFI, 8200 for Linux swap). Type L to list all codes.

Example:

Command (? for help): t
Partition number (1-128): 1
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI system partition'

Step 6: Rename a Partition (c)#

To add a human-readable name (e.g., "Linux-root" or "EFI-System"):

  1. Type c and press Enter.
  2. Enter the partition number.
  3. Enter the new name.

Example:

Command (? for help): c
Partition number (1-128): 1
Enter name: EFI-System

Step 7: Write Changes (w)#

Critical: All changes are temporary until written to disk. To save:

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/sdX.
The operation has completed successfully.

Type Y to confirm. This is irreversible—ensure you’ve selected the correct disk!

Step 8: Quit Without Saving (q)#

To exit without saving changes:

Command (? for help): q

Advanced gdisk Features#

gdisk includes a "Recovery and Transformation" menu for advanced tasks like repairing corrupted GPT tables or converting MBR to GPT. Access it by typing r from the main menu:

Verify Disk Integrity (v)#

Check for errors (e.g., unallocated space, CRC mismatches):

Command (? for help): r

Recovery/transformation command (? for help): v

No problems found. 20971520 free sectors (10.0 GiB) available in 1 segment, the largest of which is 20971520 (10.0 GiB) in size.

Rebuild GPT Header (b)#

If the primary GPT header is corrupted, rebuild it from the secondary header:

Recovery/transformation command (? for help): b

Convert MBR to GPT (g)#

To non-destructively convert an MBR disk to GPT (back up data first!):

Recovery/transformation command (? for help): g

Warning! This will destroy the currently defined partitions! Proceed? (Y/N): Y

This preserves data but replaces the MBR with GPT.

Restore GPT from Backup (l)#

If you backed up GPT data earlier (via sgdisk --backup), restore it:

Recovery/transformation command (? for help): l
Enter backup filename to load: /path/to/backup.gpt

Using sgdisk for Scripting#

sgdisk automates partitioning with command-line flags, making it ideal for scripts. Below are common use cases.

List Partitions (--list)#

sgdisk --list /dev/sdX

Create a Partition#

Syntax: sgdisk -n <partnum>:<start>:<end> -t <partnum>:<type> -c <partnum>:<name> /dev/sdX

Example: Create a 10 GiB Linux partition (8300) named "Linux-Data" on /dev/sdX:

sgdisk -n 1:2048:+10G -t 1:8300 -c 1:"Linux-Data" /dev/sdX
  • -n 1:2048:+10G: New partition 1, starts at sector 2048, ends at 2048 + 10G.
  • -t 1:8300: Set type code 8300 (Linux filesystem) for partition 1.
  • -c 1:"Linux-Data": Name partition 1 "Linux-Data".

Delete a Partition#

Delete partition 1 on /dev/sdX:

sgdisk --delete=1 /dev/sdX

Backup/Restore GPT#

Backup GPT data to a file:

sgdisk --backup=/path/to/backup.gpt /dev/sdX

Restore from backup:

sgdisk --load-backup=/path/to/backup.gpt /dev/sdX

Verify Disk#

Check for errors:

sgdisk --verify /dev/sdX

Sample Script: Automate Partitioning#

#!/bin/bashDISK="/dev/sdX"  # Replace with your disk # Wipe existing GPT (CAUTION: Destroys data!)sgdisk --zap-all $DISK # Create EFI partition (512 MiB, type EF00)sgdisk -n 1:2048:+512M -t 1:EF00 -c 1:"EFI-System" $DISK # Create Linux root partition (remaining space, type 8300)sgdisk -n 2:0:0 -t 2:8300 -c 2:"Linux-Root" $DISK echo "Partitioning complete. Verify with: sgdisk --list $DISK"

Run with sudo bash script.sh.

Best Practices and Safety Tips#

  1. Backup Data First: Always back up critical data before modifying partitions.
  2. Identify the Correct Disk: Use lsblk or fdisk -l to confirm the target disk (e.g., /dev/sda vs. /dev/sdb).
  3. Unmount Partitions: Ensure no partitions on the target disk are mounted (use umount /dev/sdX1 if needed).
  4. Verify Before Writing: In gdisk, use p to review changes and v to check for errors before w.
  5. Avoid Interruptions: Do not power off the system while writing changes (w in gdisk or sgdisk commands).
  6. Test in a VM: Practice partitioning on a virtual machine (e.g., VirtualBox) before working on physical disks.

Conclusion#

GPT is the modern standard for disk partitioning, and gdisk/sgdisk are indispensable tools for managing it on Linux. Whether you need interactive partitioning with gdisk or automated scripting with sgdisk, these tools offer flexibility, power, and reliability. By following best practices and understanding GPT’s structure, you can confidently manage disks of any size while avoiding data loss.

References#

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐