Home / Alt manpages / gdisk(8)

  • gdisk(8)
  • Admin command
  • linux

Safely Inspect and Edit a GPT Disk with gdisk

You will finish with a disciplined workflow for inspecting a GPT disk, saving a backup of the partition data, making one controlled change, checking it, and either discarding or writing the result. The examples use gdisk 1.0.10 from Debian package version 1.0.10-1build1, as installed on this machine.

Allow 15 to 30 minutes for an inspection. A real partition-table change needs a maintenance window, a verified backup and a recovery plan. You need a shell and the correct disk path. Reading a disk normally needs no elevated privilege, but opening a block device or writing its partition table may require sudo. A raw disk image is safer for practice; gdisk does not accept compressed disk-image formats.

Warning

A partition-table mistake can make data inaccessible or a system unbootable. Identify the device twice before using any command that writes. Do not practise on a mounted system disk.

1. Identify the device without guessing

Start with read-only inventory. The output is host-specific, so do not copy a device name from an example into a write command:

$ lsblk -o NAME,PATH,SIZE,TYPE,FSTYPE,MOUNTPOINTS
$ readlink -f /dev/disk/by-id/REPLACE_WITH_A_DISK_ID

Prefer a stable path under /dev/disk/by-id when one is available. Confirm the size, serial or other identifying detail against the machine or storage inventory. Use the whole disk, such as /dev/sdb, rather than a partition such as /dev/sdb1.

Checkpoint: set a shell variable only after you have checked the path. This variable is a placeholder and must be replaced:

$ DISK=/dev/disk/by-id/REPLACE_WITH_THE_CONFIRMED_DISK
$ printf 'target: %s\n' "$DISK"
target: /dev/disk/by-id/REPLACE_WITH_THE_CONFIRMED_DISK

Do not run the next steps with the literal placeholder. If the disk contains mounted filesystems, stop and arrange an appropriate maintenance process before changing its table.

2. Inspect the current table

The -l option lists the table and exits. It is the useful non-interactive check for scripts and first inspections:

$ sudo gdisk -l "$DISK"
GPT fdisk (gdisk) version 1.0.10

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

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          206847   100.0 MiB   EF00  EFI System

Your device will have different sectors, sizes and names. The useful checks are that the expected disk is listed, GPT is present, and the partitions match your inventory. On a raw, empty 64 MiB image, gdisk 1.0.10 reports no existing GPT and creates new entries in memory; that message does not mean it has written anything.

Checkpoint: if the scan reports an unexpected MBR or BSD disklabel, do not press w. gdisk can convert those formats to GPT, but conversion can disrupt booting and can mis-handle unrecognised type codes. Exit with q and investigate first.

3. Open the interactive menu and make a backup

Launch gdisk on the confirmed device:

$ sudo gdisk "$DISK"
GPT fdisk (gdisk) version 1.0.10
Command (? for help):

Most work happens in this menu. Press p to print the basic partition summary. Press i and enter a partition number when you need its detailed information, including its unique GUID. Press v to verify the disk. Verification checks items such as CRCs and agreement between the main and backup GPT data, but it does not automatically repair most problems.

Before changing anything, press b and enter a new backup filename outside a temporary directory. This saves the current in-memory protective MBR, GPT headers and one partition-table copy. If you have already made changes in this session, the backup contains those changes, so make the backup first:

Command (? for help): b
Enter backup filename to save: /root/gpt-backup-REPLACE_WITH_DATE.bin
The operation has completed successfully.

Keep the backup with the disk's change record. It is binary data, not a text export, and restoring it is a separate recovery-menu operation. It is not a substitute for a filesystem or application backup.

4. Make one small, reviewable change

For a representative metadata change, use c to change a partition's GPT name. This does not rename the filesystem. Select the partition number and enter the new name:

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

For a new partition, n asks for a partition number, start sector and end sector, followed by a type code and name. Pressing Enter accepts gdisk's displayed defaults. Relative positions such as +2G are measured from the relevant default boundary. Review every prompt: a default is a location chosen from the current free space, not a promise that the result suits your operating system.

Partition type codes are gdisk's four-digit hexadecimal shortcuts for GPT type GUIDs. Use l to list them, optionally filtered by a case-insensitive search such as linux. Do not confuse a GPT partition name with a filesystem label.

Checkpoint: press p, then v. Compare the proposed start, end, size, code and name with your change record. If anything is wrong, press q. gdisk discards the in-memory edits and leaves the disk unchanged.

5. Write only after the final review

Irreversible boundary: the w command writes the in-memory table to the disk and exits. It is the point at which a mistake becomes a real partition-table change:

Command (? for help): w
Do you want to proceed? (Y/N): N

The N above is the safe response while practising. When you have independently checked the target, backup and proposed table, answer Y to the confirmation prompt. Do not use w merely to leave the program. Use q when you want to discard changes.

After an authorised write, rerun the read-only listing and verification:

$ sudo gdisk -l "$DISK"
$ sudo parted --script "$DISK" print
$ sudo gdisk "$DISK"
Command (? for help): v

The exact output varies by disk. Check that the intended partition exists with the intended boundaries and type, and that verification reports no new problem. If the change was only a GPT name, confirm the name in the gdisk -l output or with p.

6. Handle common traps

A disk image must be raw. A compressed image such as a .gz file cannot be opened directly. Decompress it to a separate raw file first, and keep the original. A file-backed test image is an ordinary unprivileged workflow:

$ truncate -s 64M /tmp/gpt-test.img
$ gdisk -l /tmp/gpt-test.img
GPT fdisk (gdisk) version 1.0.10
Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present
Creating new GPT entries in memory.

This output is an in-memory view. It does not create a usable partition table until you deliberately use the interactive w command. Do not use this example as evidence that a real disk is empty: a wrong path can be just as dangerous as a wrong command.

If gdisk reports damaged headers or mismatched main and backup data, stop before selecting a recovery option. The recovery and transformation menu includes commands that rebuild headers, load backup data, convert GPT to MBR and create hybrid MBRs. Those are specialist operations; preserve the original device image first and use the backup only after establishing which copy is trustworthy.

Done means

  • The target disk was identified by a stable path and checked against its physical or inventory details.
  • gdisk -l showed the expected partition table before editing.
  • A binary backup was made before any in-memory change.
  • p and v were used to review the proposed table.
  • q was used to undo an unwanted session, or w was used only after an explicit final review.
  • A post-write listing and verification confirmed the intended result.